1 | /****************************************************************************** |
---|
2 | * $Id: util.c 5213 2008-03-07 16:09:21Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2008 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #include <stdarg.h> |
---|
26 | #include <stdlib.h> /* free() */ |
---|
27 | #include <string.h> /* strcmp() */ |
---|
28 | |
---|
29 | #include <gtk/gtk.h> |
---|
30 | #include <glib/gi18n.h> |
---|
31 | |
---|
32 | #include <libevent/evhttp.h> |
---|
33 | |
---|
34 | #include <libtransmission/transmission.h> /* TR_RATIO_NA, TR_RATIO_INF */ |
---|
35 | |
---|
36 | #include "conf.h" |
---|
37 | #include "tr-prefs.h" |
---|
38 | #include "util.h" |
---|
39 | |
---|
40 | int |
---|
41 | tr_strcmp( const char * a, const char * b ) |
---|
42 | { |
---|
43 | if( a && b ) return strcmp( a, b ); |
---|
44 | if( a ) return 1; |
---|
45 | if( b ) return -1; |
---|
46 | return 0; |
---|
47 | } |
---|
48 | |
---|
49 | char* |
---|
50 | tr_strlratio( char * buf, double ratio, size_t buflen ) |
---|
51 | { |
---|
52 | if( (int)ratio == TR_RATIO_NA ) |
---|
53 | g_strlcpy( buf, _( "None" ), buflen ); |
---|
54 | else if( (int)ratio == TR_RATIO_INF ) |
---|
55 | g_strlcpy( buf, "\xE2\x88\x9E", buflen ); |
---|
56 | else if( ratio < 10.0 ) |
---|
57 | g_snprintf( buf, buflen, "%.2f", ratio ); |
---|
58 | else if( ratio < 100.0 ) |
---|
59 | g_snprintf( buf, buflen, "%.1f", ratio ); |
---|
60 | else |
---|
61 | g_snprintf( buf, buflen, "%.0f", ratio ); |
---|
62 | return buf; |
---|
63 | } |
---|
64 | |
---|
65 | #define KILOBYTE_FACTOR 1024.0 |
---|
66 | #define MEGABYTE_FACTOR (1024.0 * 1024.0) |
---|
67 | #define GIGABYTE_FACTOR (1024.0 * 1024.0 * 1024.0) |
---|
68 | |
---|
69 | char* |
---|
70 | tr_strlsize( char * buf, guint64 size, size_t buflen ) |
---|
71 | { |
---|
72 | #if GLIB_CHECK_VERSION(2,16,0) |
---|
73 | char * tmp = g_format_size_for_display( s ); |
---|
74 | g_strlcpy( buf, tmp, buflen ); |
---|
75 | g_free( tmp ); |
---|
76 | #else |
---|
77 | if( size < (guint64)KILOBYTE_FACTOR ) |
---|
78 | g_snprintf( buf, buflen, ngettext("%u byte", "%u bytes", (guint)size), (guint)size ); |
---|
79 | else { |
---|
80 | gdouble displayed_size; |
---|
81 | if (size < (guint64)MEGABYTE_FACTOR) { |
---|
82 | displayed_size = (gdouble) size / KILOBYTE_FACTOR; |
---|
83 | g_snprintf( buf, buflen, _("%.1f KB"), displayed_size ); |
---|
84 | } else if (size < (guint64)GIGABYTE_FACTOR) { |
---|
85 | displayed_size = (gdouble) size / MEGABYTE_FACTOR; |
---|
86 | g_snprintf( buf, buflen, _("%.1f MB"), displayed_size ); |
---|
87 | } else { |
---|
88 | displayed_size = (gdouble) size / GIGABYTE_FACTOR; |
---|
89 | g_snprintf( buf, buflen, _("%.1f GB"), displayed_size ); |
---|
90 | } |
---|
91 | } |
---|
92 | #endif |
---|
93 | return buf; |
---|
94 | } |
---|
95 | |
---|
96 | char* |
---|
97 | tr_strlspeed( char * buf, double KiBps, size_t buflen ) |
---|
98 | { |
---|
99 | const double speed = KiBps; |
---|
100 | |
---|
101 | if ( speed < 1000.0 ) /* 0.0 KB to 999.9 KB */ |
---|
102 | g_snprintf( buf, buflen, _( "%.1f KB/s" ), speed ); |
---|
103 | else if( speed < 102400.0 ) /* 0.98 MB to 99.99 MB */ |
---|
104 | g_snprintf( buf, buflen, _( "%.2f MB/s" ), (speed/1024) ); |
---|
105 | else if( speed < 1024000.0 ) /* 100.0 MB to 999.9 MB */ |
---|
106 | g_snprintf( buf, buflen, _( "%.1f MB/s" ), (speed/1024) ); |
---|
107 | else /* insane speeds */ |
---|
108 | g_snprintf( buf, buflen, _( "%.2f GB/s" ), (speed/1048576) ); |
---|
109 | |
---|
110 | return buf; |
---|
111 | } |
---|
112 | |
---|
113 | char* |
---|
114 | tr_strltime( char * buf, int seconds, size_t buflen ) |
---|
115 | { |
---|
116 | int hours; |
---|
117 | |
---|
118 | if( seconds < 0 ) |
---|
119 | seconds = 0; |
---|
120 | |
---|
121 | if( seconds < 60 ) |
---|
122 | { |
---|
123 | g_snprintf( buf, sizeof( buf ), ngettext( "%'d second", "%'d seconds", (int)seconds ), (int) seconds ); |
---|
124 | return buf; |
---|
125 | } |
---|
126 | |
---|
127 | if( seconds < ( 60 * 60 ) ) |
---|
128 | { |
---|
129 | const int minutes = ( seconds + 30 ) / 60; |
---|
130 | g_snprintf( buf, sizeof( buf ), ngettext( "%'d minute", "%'d minutes", minutes ), minutes ); |
---|
131 | return buf; |
---|
132 | } |
---|
133 | |
---|
134 | hours = seconds / ( 60 *60 ); |
---|
135 | |
---|
136 | if( seconds < ( 60 * 60 * 4 ) ) |
---|
137 | { |
---|
138 | char h[64]; |
---|
139 | char m[64]; |
---|
140 | |
---|
141 | const int minutes = ( seconds - hours * 60 * 60 + 30 ) / 60; |
---|
142 | |
---|
143 | g_snprintf( h, sizeof(h), ngettext( "%'d hour", "%'d hours", hours ), hours ); |
---|
144 | g_snprintf( m, sizeof(m), ngettext( "%'d minute", "%'d minutes", minutes ), minutes ); |
---|
145 | g_snprintf( buf, buflen, "%s, %s", h, m ); |
---|
146 | return buf; |
---|
147 | } |
---|
148 | |
---|
149 | g_snprintf( buf, sizeof( buf ), ngettext( "%'d hour", "%'d hours", hours ), hours ); |
---|
150 | return buf; |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | char * |
---|
155 | rfc822date (guint64 epoch_msec) |
---|
156 | { |
---|
157 | const time_t secs = epoch_msec / 1000; |
---|
158 | const struct tm tm = *localtime (&secs); |
---|
159 | char buf[128]; |
---|
160 | strftime( buf, sizeof(buf), "%a, %d %b %Y %T %Z", &tm ); |
---|
161 | return g_locale_to_utf8( buf, -1, NULL, NULL, NULL ); |
---|
162 | } |
---|
163 | |
---|
164 | gboolean |
---|
165 | mkdir_p( const char * path, mode_t mode ) |
---|
166 | { |
---|
167 | #if GLIB_CHECK_VERSION( 2, 8, 0) |
---|
168 | return !g_mkdir_with_parents( path, mode ); |
---|
169 | #else |
---|
170 | return !tr_mkdirp( path, mode ); |
---|
171 | #endif |
---|
172 | } |
---|
173 | |
---|
174 | GList * |
---|
175 | dupstrlist( GList * l ) |
---|
176 | { |
---|
177 | GList * ret = NULL; |
---|
178 | for( ; l!=NULL; l=l->next ) |
---|
179 | ret = g_list_prepend( ret, g_strdup( l->data ) ); |
---|
180 | return g_list_reverse( ret ); |
---|
181 | } |
---|
182 | |
---|
183 | char * |
---|
184 | joinstrlist(GList *list, char *sep) |
---|
185 | { |
---|
186 | GList *l; |
---|
187 | GString *gstr = g_string_new (NULL); |
---|
188 | for (l=list; l!=NULL; l=l->next) { |
---|
189 | g_string_append (gstr, (char*)l->data); |
---|
190 | if (l->next != NULL) |
---|
191 | g_string_append (gstr, (sep)); |
---|
192 | } |
---|
193 | return g_string_free (gstr, FALSE); |
---|
194 | } |
---|
195 | |
---|
196 | void |
---|
197 | freestrlist(GList *list) |
---|
198 | { |
---|
199 | g_list_foreach (list, (GFunc)g_free, NULL); |
---|
200 | g_list_free (list); |
---|
201 | } |
---|
202 | |
---|
203 | char * |
---|
204 | decode_uri( const char * uri ) |
---|
205 | { |
---|
206 | char * filename = evhttp_decode_uri( uri ); |
---|
207 | char * ret = g_strdup( filename ); |
---|
208 | free( filename ); |
---|
209 | return ret; |
---|
210 | } |
---|
211 | |
---|
212 | GList * |
---|
213 | checkfilenames( int argc, char **argv ) |
---|
214 | { |
---|
215 | int i; |
---|
216 | GList * ret = NULL; |
---|
217 | char * pwd = g_get_current_dir( ); |
---|
218 | |
---|
219 | for( i=0; i<argc; ++i ) |
---|
220 | { |
---|
221 | char * filename = g_path_is_absolute( argv[i] ) |
---|
222 | ? g_strdup ( argv[i] ) |
---|
223 | : g_build_filename( pwd, argv[i], NULL ); |
---|
224 | |
---|
225 | if( g_file_test( filename, G_FILE_TEST_EXISTS ) ) |
---|
226 | ret = g_list_append( ret, filename ); |
---|
227 | else |
---|
228 | g_free( filename ); |
---|
229 | } |
---|
230 | |
---|
231 | g_free( pwd ); |
---|
232 | return ret; |
---|
233 | } |
---|
234 | |
---|
235 | char * |
---|
236 | getdownloaddir( void ) |
---|
237 | { |
---|
238 | static char * wd = NULL; |
---|
239 | char * dir = pref_string_get( PREF_KEY_DIR_DEFAULT ); |
---|
240 | if ( dir == NULL ) { |
---|
241 | if( wd == NULL ) |
---|
242 | wd = g_get_current_dir(); |
---|
243 | dir = g_strdup( wd ); |
---|
244 | } |
---|
245 | return dir; |
---|
246 | } |
---|
247 | |
---|
248 | /** |
---|
249 | * don't use more than 50% the height of the screen, nor 80% the width. |
---|
250 | * but don't be too small, either -- set the minimums to 500 x 300 |
---|
251 | */ |
---|
252 | void |
---|
253 | sizingmagic( GtkWindow * wind, |
---|
254 | GtkScrolledWindow * scroll, |
---|
255 | GtkPolicyType hscroll, |
---|
256 | GtkPolicyType vscroll ) |
---|
257 | { |
---|
258 | int width; |
---|
259 | int height; |
---|
260 | GtkRequisition req; |
---|
261 | |
---|
262 | GdkScreen * screen = gtk_widget_get_screen( GTK_WIDGET( wind ) ); |
---|
263 | |
---|
264 | gtk_scrolled_window_set_policy( scroll, GTK_POLICY_NEVER, |
---|
265 | GTK_POLICY_NEVER ); |
---|
266 | |
---|
267 | gtk_widget_size_request( GTK_WIDGET( wind ), &req ); |
---|
268 | req.height = MAX( req.height, 300 ); |
---|
269 | height = MIN( req.height, gdk_screen_get_height( screen ) / 5 * 4 ); |
---|
270 | |
---|
271 | gtk_scrolled_window_set_policy( scroll, GTK_POLICY_NEVER, vscroll ); |
---|
272 | gtk_widget_size_request( GTK_WIDGET( wind ), &req ); |
---|
273 | req.width = MAX( req.width, 500 ); |
---|
274 | width = MIN( req.width, gdk_screen_get_width( screen ) / 2 ); |
---|
275 | |
---|
276 | gtk_window_set_default_size( wind, width, height ); |
---|
277 | gtk_scrolled_window_set_policy( scroll, hscroll, vscroll ); |
---|
278 | } |
---|
279 | |
---|
280 | static void |
---|
281 | errcb(GtkWidget *widget, int resp UNUSED, gpointer data) { |
---|
282 | GList *funcdata; |
---|
283 | callbackfunc_t func; |
---|
284 | |
---|
285 | if(NULL != data) { |
---|
286 | funcdata = g_list_first(data); |
---|
287 | func = (callbackfunc_t) funcdata->data; |
---|
288 | data = funcdata->next->data; |
---|
289 | func(data); |
---|
290 | g_list_free(funcdata); |
---|
291 | } |
---|
292 | |
---|
293 | gtk_widget_destroy(widget); |
---|
294 | } |
---|
295 | |
---|
296 | static GtkWidget * |
---|
297 | verrmsg_full( GtkWindow * wind, callbackfunc_t func, void * data, |
---|
298 | const char * format, va_list ap ) |
---|
299 | { |
---|
300 | GtkWidget *dialog; |
---|
301 | char *msg; |
---|
302 | GList *funcdata; |
---|
303 | |
---|
304 | msg = g_strdup_vprintf(format, ap); |
---|
305 | |
---|
306 | if(NULL == wind) |
---|
307 | dialog = gtk_message_dialog_new( |
---|
308 | NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", msg); |
---|
309 | else |
---|
310 | dialog = gtk_message_dialog_new(wind, |
---|
311 | GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, |
---|
312 | GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", msg); |
---|
313 | |
---|
314 | if(NULL == func) |
---|
315 | funcdata = NULL; |
---|
316 | else |
---|
317 | funcdata = g_list_append(g_list_append(NULL, (void *) func), data); |
---|
318 | g_signal_connect(dialog, "response", G_CALLBACK(errcb), funcdata); |
---|
319 | g_free(msg); |
---|
320 | |
---|
321 | return dialog; |
---|
322 | } |
---|
323 | |
---|
324 | void |
---|
325 | errmsg( GtkWindow * wind, const char * format, ... ) |
---|
326 | { |
---|
327 | GtkWidget * dialog; |
---|
328 | va_list ap; |
---|
329 | |
---|
330 | va_start( ap, format ); |
---|
331 | dialog = verrmsg_full( wind, NULL, NULL, format, ap ); |
---|
332 | va_end( ap ); |
---|
333 | |
---|
334 | if( NULL != wind && !GTK_WIDGET_MAPPED( GTK_WIDGET( wind ) ) ) |
---|
335 | { |
---|
336 | g_signal_connect_swapped( wind, "map", |
---|
337 | G_CALLBACK( gtk_widget_show ), dialog ); |
---|
338 | } |
---|
339 | else |
---|
340 | { |
---|
341 | gtk_widget_show( dialog ); |
---|
342 | } |
---|
343 | } |
---|
344 | |
---|
345 | GtkWidget * |
---|
346 | errmsg_full( GtkWindow * wind, callbackfunc_t func, void * data, |
---|
347 | const char * format, ... ) |
---|
348 | { |
---|
349 | GtkWidget * dialog; |
---|
350 | va_list ap; |
---|
351 | |
---|
352 | va_start( ap, format ); |
---|
353 | dialog = verrmsg_full( wind, func, data, format, ap ); |
---|
354 | va_end( ap ); |
---|
355 | |
---|
356 | return dialog; |
---|
357 | } |
---|
358 | |
---|
359 | typedef void (PopupFunc)(GtkWidget*, GdkEventButton*); |
---|
360 | |
---|
361 | /* pop up the context menu if a user right-clicks. |
---|
362 | if the row they right-click on isn't selected, select it. */ |
---|
363 | |
---|
364 | gboolean |
---|
365 | on_tree_view_button_pressed (GtkWidget * view, |
---|
366 | GdkEventButton * event, |
---|
367 | gpointer func) |
---|
368 | { |
---|
369 | GtkTreeView * tv = GTK_TREE_VIEW( view ); |
---|
370 | |
---|
371 | if (event->type == GDK_BUTTON_PRESS && event->button == 3) |
---|
372 | { |
---|
373 | GtkTreeSelection * selection = gtk_tree_view_get_selection(tv); |
---|
374 | GtkTreePath *path; |
---|
375 | if (gtk_tree_view_get_path_at_pos (tv, |
---|
376 | (gint) event->x, |
---|
377 | (gint) event->y, |
---|
378 | &path, NULL, NULL, NULL)) |
---|
379 | { |
---|
380 | if (!gtk_tree_selection_path_is_selected (selection, path)) |
---|
381 | { |
---|
382 | gtk_tree_selection_unselect_all (selection); |
---|
383 | gtk_tree_selection_select_path (selection, path); |
---|
384 | } |
---|
385 | gtk_tree_path_free(path); |
---|
386 | } |
---|
387 | |
---|
388 | ((PopupFunc*)func)(view, event); |
---|
389 | |
---|
390 | return TRUE; |
---|
391 | } |
---|
392 | |
---|
393 | return FALSE; |
---|
394 | } |
---|
395 | |
---|
396 | gpointer |
---|
397 | tr_object_ref_sink( gpointer object ) |
---|
398 | { |
---|
399 | #if GLIB_CHECK_VERSION(2,10,0) |
---|
400 | g_object_ref_sink( object ); |
---|
401 | #else |
---|
402 | g_object_ref( object ); |
---|
403 | gtk_object_sink( GTK_OBJECT( object ) ); |
---|
404 | #endif |
---|
405 | return object; |
---|
406 | } |
---|