1 | /* |
---|
2 | * This file Copyright (C) 2008-2010 Mnemosyne LLC |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: util.h 11088 2010-08-01 19:13:34Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef GTR_UTIL_H |
---|
14 | #define GTR_UTIL_H |
---|
15 | |
---|
16 | #include <sys/types.h> |
---|
17 | #include <glib.h> |
---|
18 | #include <gtk/gtk.h> |
---|
19 | |
---|
20 | #include <libtransmission/transmission.h> |
---|
21 | |
---|
22 | extern const int mem_K; |
---|
23 | extern const char * mem_K_str; |
---|
24 | extern const char * mem_M_str; |
---|
25 | extern const char * mem_G_str; |
---|
26 | extern const char * mem_T_str; |
---|
27 | |
---|
28 | extern const int disk_K; |
---|
29 | extern const char * disk_K_str; |
---|
30 | extern const char * disk_M_str; |
---|
31 | extern const char * disk_G_str; |
---|
32 | extern const char * disk_T_str; |
---|
33 | |
---|
34 | extern const int speed_K; |
---|
35 | extern const char * speed_K_str; |
---|
36 | extern const char * speed_M_str; |
---|
37 | extern const char * speed_G_str; |
---|
38 | extern const char * speed_T_str; |
---|
39 | |
---|
40 | /* portability wrapper around g_warn_if_fail() for older versions of glib */ |
---|
41 | #ifdef g_warn_if_fail |
---|
42 | #define gtr_warn_if_fail(expr) g_warn_if_fail(expr) |
---|
43 | #else |
---|
44 | #define gtr_warn_if_fail(expr) do { if G_LIKELY (expr) ; else \ |
---|
45 | g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "%s:%d func(): %s: invariant failed: %s", \ |
---|
46 | __FILE__, __LINE__, G_STRFUNC, #expr ); } while(0) |
---|
47 | #endif |
---|
48 | |
---|
49 | /* macro to shut up "unused parameter" warnings */ |
---|
50 | #ifndef UNUSED |
---|
51 | #define UNUSED G_GNUC_UNUSED |
---|
52 | #endif |
---|
53 | |
---|
54 | enum |
---|
55 | { |
---|
56 | GTR_UNICODE_UP, |
---|
57 | GTR_UNICODE_DOWN, |
---|
58 | GTR_UNICODE_INF |
---|
59 | }; |
---|
60 | const char * gtr_get_unicode_string( int ); |
---|
61 | |
---|
62 | /* return a percent formatted string of either x.xx, xx.x or xxx */ |
---|
63 | char* tr_strlpercent( char * buf, double x, size_t buflen ); |
---|
64 | |
---|
65 | /* return a human-readable string for the size given in bytes. */ |
---|
66 | char* tr_strlsize( char * buf, guint64 size, size_t buflen ); |
---|
67 | |
---|
68 | /* return a human-readable string for the given ratio. */ |
---|
69 | char* tr_strlratio( char * buf, double ratio, size_t buflen ); |
---|
70 | |
---|
71 | /* return a human-readable string for the time given in seconds. */ |
---|
72 | char* tr_strltime( char * buf, int secs, size_t buflen ); |
---|
73 | |
---|
74 | /* similar to asctime, but is utf8-clean */ |
---|
75 | char* gtr_localtime( time_t time ); |
---|
76 | |
---|
77 | |
---|
78 | int gtr_compare_double( const double a, const double b, int decimal_places ); |
---|
79 | |
---|
80 | |
---|
81 | /*** |
---|
82 | **** |
---|
83 | ***/ |
---|
84 | |
---|
85 | /* http://www.legaltorrents.com/some/announce/url --> legaltorrents.com */ |
---|
86 | char* gtr_get_host_from_url( const char * url ); |
---|
87 | |
---|
88 | gboolean gtr_is_supported_url( const char * str ); |
---|
89 | |
---|
90 | gboolean gtr_is_magnet_link( const char * str ); |
---|
91 | |
---|
92 | gboolean gtr_is_hex_hashcode( const char * str ); |
---|
93 | |
---|
94 | |
---|
95 | /* create a copy of a GSList of strings, this dups the actual strings too */ |
---|
96 | GSList * dupstrlist( GSList * list ); |
---|
97 | |
---|
98 | /* joins a GSList of strings into one string using an optional separator */ |
---|
99 | char * joinstrlist( GSList *list, char * sep ); |
---|
100 | |
---|
101 | /* free a GSList of strings */ |
---|
102 | void freestrlist( GSList *list ); |
---|
103 | |
---|
104 | /* decodes a string that has been urlencoded */ |
---|
105 | char * decode_uri( const char * uri ); |
---|
106 | |
---|
107 | /*** |
---|
108 | **** |
---|
109 | ***/ |
---|
110 | |
---|
111 | typedef enum |
---|
112 | { |
---|
113 | GTR_LOCKFILE_SUCCESS = 0, |
---|
114 | GTR_LOCKFILE_EOPEN, |
---|
115 | GTR_LOCKFILE_ELOCK |
---|
116 | } |
---|
117 | gtr_lockfile_state_t; |
---|
118 | |
---|
119 | gtr_lockfile_state_t gtr_lockfile( const char * filename ); |
---|
120 | |
---|
121 | /*** |
---|
122 | **** |
---|
123 | ***/ |
---|
124 | |
---|
125 | void gtr_open_file( const char * path ); |
---|
126 | |
---|
127 | gboolean gtr_dbus_add_torrent( const char * filename ); |
---|
128 | |
---|
129 | gboolean gtr_dbus_present_window( void ); |
---|
130 | |
---|
131 | char* gtr_get_help_url( void ); |
---|
132 | |
---|
133 | /*** |
---|
134 | **** |
---|
135 | ***/ |
---|
136 | |
---|
137 | /* backwards-compatible wrapper around g_mkdir_with_parents() */ |
---|
138 | int gtr_mkdir_with_parents( const char *name, int mode ); |
---|
139 | |
---|
140 | /* backwards-compatible wrapper around gdk_threads_add_timeout_seconds() */ |
---|
141 | guint gtr_timeout_add_seconds( guint seconds, GSourceFunc func, gpointer data ); |
---|
142 | |
---|
143 | /* backwards-compatible wrapper around gdk_threads_add_idle() */ |
---|
144 | void gtr_idle_add( GSourceFunc func, gpointer data ); |
---|
145 | |
---|
146 | /* backwards-compatible wrapper around gtk_orientable_set_orientation() */ |
---|
147 | void gtr_toolbar_set_orientation( GtkToolbar * tb, GtkOrientation orientation ); |
---|
148 | |
---|
149 | /* backwards-compatible wrapper around gtk_widget_set_tooltip_text() */ |
---|
150 | void gtr_widget_set_tooltip_text( GtkWidget * w, const char * tip ); |
---|
151 | |
---|
152 | /* backwards-compatible wrapper around gtk_widget_get_realized() */ |
---|
153 | gboolean gtr_widget_get_realized( GtkWidget * w ); |
---|
154 | |
---|
155 | /* backwards-compatible wrapper around gtk_widget_set_visible() */ |
---|
156 | void gtr_widget_set_visible( GtkWidget *, gboolean ); |
---|
157 | |
---|
158 | /* backwards-compatible wrapper around g_object_ref_sink() */ |
---|
159 | gpointer gtr_object_ref_sink( gpointer object ); |
---|
160 | |
---|
161 | /* backwards-comparible wrapper around g_strcmp0() */ |
---|
162 | int gtr_strcmp0( const char * str1, const char * str2 ); |
---|
163 | |
---|
164 | /*** |
---|
165 | **** |
---|
166 | ***/ |
---|
167 | |
---|
168 | /* create a button with the specified mnemonic and stock icon */ |
---|
169 | GtkWidget * gtr_button_new_from_stock( const char * stock, |
---|
170 | const char * mnemonic ); |
---|
171 | |
---|
172 | |
---|
173 | /*** |
---|
174 | **** |
---|
175 | ***/ |
---|
176 | |
---|
177 | GtkWidget * gtr_priority_combo_new( void ); |
---|
178 | #define gtr_priority_combo_get_value(w) gtr_combo_box_get_active_enum(w) |
---|
179 | #define gtr_priority_combo_set_value(w,val) gtr_combo_box_set_active_enum(w,val) |
---|
180 | |
---|
181 | GtkWidget * gtr_combo_box_new_enum ( const char * text_1, ... ); |
---|
182 | int gtr_combo_box_get_active_enum ( GtkComboBox * ); |
---|
183 | void gtr_combo_box_set_active_enum ( GtkComboBox *, int value ); |
---|
184 | |
---|
185 | /*** |
---|
186 | **** |
---|
187 | ***/ |
---|
188 | |
---|
189 | void gtr_unrecognized_url_dialog( GtkWidget * parent, const char * url ); |
---|
190 | |
---|
191 | void gtr_http_failure_dialog( GtkWidget * parent, const char * url, long response_code ); |
---|
192 | |
---|
193 | void addTorrentErrorDialog( GtkWidget * window_or_child, |
---|
194 | int err, |
---|
195 | const char * filename ); |
---|
196 | |
---|
197 | /* pop up the context menu if a user right-clicks. |
---|
198 | if the row they right-click on isn't selected, select it. */ |
---|
199 | gboolean on_tree_view_button_pressed( GtkWidget * view, |
---|
200 | GdkEventButton * event, |
---|
201 | gpointer unused ); |
---|
202 | |
---|
203 | /* if the click didn't specify a row, clear the selection */ |
---|
204 | gboolean on_tree_view_button_released( GtkWidget * view, |
---|
205 | GdkEventButton * event, |
---|
206 | gpointer unused ); |
---|
207 | |
---|
208 | |
---|
209 | /* move a file to the trashcan if GIO is available; otherwise, delete it */ |
---|
210 | int gtr_file_trash_or_remove( const char * filename ); |
---|
211 | |
---|
212 | #endif /* GTR_UTIL_H */ |
---|