1 | /****************************************************************************** |
---|
2 | * $Id: main.c 5930 2008-05-25 13:22:14Z 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 <sys/param.h> |
---|
26 | #include <signal.h> |
---|
27 | #include <string.h> |
---|
28 | #include <stdio.h> |
---|
29 | #include <stdlib.h> |
---|
30 | #include <time.h> |
---|
31 | #include <unistd.h> |
---|
32 | |
---|
33 | #include <gtk/gtk.h> |
---|
34 | #include <glib/gi18n.h> |
---|
35 | #include <glib/gstdio.h> |
---|
36 | |
---|
37 | #include <gdk/gdk.h> |
---|
38 | #ifdef GDK_WINDOWING_X11 |
---|
39 | #include <X11/Xatom.h> |
---|
40 | #include <gdk/gdkx.h> |
---|
41 | #endif |
---|
42 | |
---|
43 | #include <libtransmission/version.h> |
---|
44 | |
---|
45 | #include "actions.h" |
---|
46 | #include "add-dialog.h" |
---|
47 | #include "conf.h" |
---|
48 | #include "details.h" |
---|
49 | #include "dialogs.h" |
---|
50 | #include "hig.h" |
---|
51 | #include "makemeta-ui.h" |
---|
52 | #include "msgwin.h" |
---|
53 | #include "notify.h" |
---|
54 | #include "stats.h" |
---|
55 | #include "tr-core.h" |
---|
56 | #include "tr-icon.h" |
---|
57 | #include "tr-prefs.h" |
---|
58 | #include "tr-torrent.h" |
---|
59 | #include "tr-window.h" |
---|
60 | #include "util.h" |
---|
61 | #include "ui.h" |
---|
62 | |
---|
63 | #include <libtransmission/transmission.h> |
---|
64 | #include <libtransmission/version.h> |
---|
65 | |
---|
66 | /* interval in milliseconds to update the torrent list display */ |
---|
67 | #define UPDATE_INTERVAL 1666 |
---|
68 | |
---|
69 | /* interval in milliseconds to check for stopped torrents and update display */ |
---|
70 | #define EXIT_CHECK_INTERVAL 500 |
---|
71 | |
---|
72 | #if GTK_CHECK_VERSION(2,8,0) |
---|
73 | #define SHOW_LICENSE |
---|
74 | static const char * LICENSE = |
---|
75 | "The Transmission binaries and most of its source code is distributed " |
---|
76 | "license. " |
---|
77 | "\n\n" |
---|
78 | "Some files are copyrighted by Charles Kerr and are covered by " |
---|
79 | "the GPL version 2. Works owned by the Transmission project " |
---|
80 | "are granted a special exemption to clause 2(b) so that the bulk " |
---|
81 | "of its code can remain under the MIT license. This exemption does " |
---|
82 | "not extend to original or derived works not owned by the " |
---|
83 | "Transmission project. " |
---|
84 | "\n\n" |
---|
85 | "Permission is hereby granted, free of charge, to any person obtaining " |
---|
86 | "a copy of this software and associated documentation files (the " |
---|
87 | "'Software'), to deal in the Software without restriction, including " |
---|
88 | "without limitation the rights to use, copy, modify, merge, publish, " |
---|
89 | "distribute, sublicense, and/or sell copies of the Software, and to " |
---|
90 | "permit persons to whom the Software is furnished to do so, subject to " |
---|
91 | "the following conditions: " |
---|
92 | "\n\n" |
---|
93 | "The above copyright notice and this permission notice shall be included " |
---|
94 | "in all copies or substantial portions of the Software. " |
---|
95 | "\n\n" |
---|
96 | "THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, " |
---|
97 | "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF " |
---|
98 | "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. " |
---|
99 | "IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY " |
---|
100 | "CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, " |
---|
101 | "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE " |
---|
102 | "SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."; |
---|
103 | #endif |
---|
104 | |
---|
105 | struct cbdata |
---|
106 | { |
---|
107 | gboolean minimized; |
---|
108 | gboolean closing; |
---|
109 | guint timer; |
---|
110 | guint idle_hide_mainwindow_tag; |
---|
111 | gpointer icon; |
---|
112 | GtkWindow * wind; |
---|
113 | TrCore * core; |
---|
114 | GtkWidget * msgwin; |
---|
115 | GtkWidget * prefs; |
---|
116 | GSList * errqueue; |
---|
117 | GHashTable * tor2details; |
---|
118 | GHashTable * details2tor; |
---|
119 | }; |
---|
120 | |
---|
121 | #define CBDATA_PTR "callback-data-pointer" |
---|
122 | |
---|
123 | static GtkUIManager * myUIManager = NULL; |
---|
124 | |
---|
125 | static void |
---|
126 | appsetup( TrWindow * wind, GSList * args, |
---|
127 | struct cbdata *, |
---|
128 | gboolean paused, gboolean minimized ); |
---|
129 | static void |
---|
130 | winsetup( struct cbdata * cbdata, TrWindow * wind ); |
---|
131 | static void |
---|
132 | wannaquit( void * vdata ); |
---|
133 | static void |
---|
134 | setupdrag(GtkWidget *widget, struct cbdata *data); |
---|
135 | static void |
---|
136 | gotdrag(GtkWidget *widget, GdkDragContext *dc, gint x, gint y, |
---|
137 | GtkSelectionData *sel, guint info, guint time, gpointer gdata); |
---|
138 | |
---|
139 | static void |
---|
140 | coreerr( TrCore * core, enum tr_core_err code, const char * msg, |
---|
141 | gpointer gdata ); |
---|
142 | static void |
---|
143 | onAddTorrent( TrCore *, tr_ctor *, gpointer ); |
---|
144 | static void |
---|
145 | prefschanged( TrCore * core, const char * key, gpointer data ); |
---|
146 | static gboolean |
---|
147 | updatemodel(gpointer gdata); |
---|
148 | |
---|
149 | struct counts_data |
---|
150 | { |
---|
151 | int totalCount; |
---|
152 | int activeCount; |
---|
153 | int inactiveCount; |
---|
154 | }; |
---|
155 | |
---|
156 | static void |
---|
157 | accumulateStatusForeach( GtkTreeModel * model, |
---|
158 | GtkTreePath * path UNUSED, |
---|
159 | GtkTreeIter * iter, |
---|
160 | gpointer user_data ) |
---|
161 | { |
---|
162 | int status = 0; |
---|
163 | struct counts_data * counts = user_data; |
---|
164 | |
---|
165 | ++counts->totalCount; |
---|
166 | |
---|
167 | gtk_tree_model_get( model, iter, MC_STATUS, &status, -1 ); |
---|
168 | |
---|
169 | if( TR_STATUS_IS_ACTIVE( status ) ) |
---|
170 | ++counts->activeCount; |
---|
171 | else |
---|
172 | ++counts->inactiveCount; |
---|
173 | } |
---|
174 | |
---|
175 | static void |
---|
176 | accumulateCanUpdateForeach (GtkTreeModel * model, |
---|
177 | GtkTreePath * path UNUSED, |
---|
178 | GtkTreeIter * iter, |
---|
179 | gpointer accumulated_status) |
---|
180 | { |
---|
181 | tr_torrent * tor; |
---|
182 | gtk_tree_model_get( model, iter, MC_TORRENT_RAW, &tor, -1 ); |
---|
183 | *(int*)accumulated_status |= tr_torrentCanManualUpdate( tor ); |
---|
184 | } |
---|
185 | |
---|
186 | static void |
---|
187 | refreshTorrentActions( GtkTreeSelection * s ) |
---|
188 | { |
---|
189 | int canUpdate; |
---|
190 | struct counts_data counts; |
---|
191 | |
---|
192 | counts.activeCount = 0; |
---|
193 | counts.inactiveCount = 0; |
---|
194 | counts.totalCount = 0; |
---|
195 | gtk_tree_selection_selected_foreach( s, accumulateStatusForeach, &counts ); |
---|
196 | action_sensitize( "pause-torrent", counts.activeCount!=0 ); |
---|
197 | action_sensitize( "start-torrent", counts.inactiveCount!=0 ); |
---|
198 | action_sensitize( "remove-torrent", counts.totalCount!=0 ); |
---|
199 | action_sensitize( "delete-torrent", counts.totalCount!=0 ); |
---|
200 | action_sensitize( "verify-torrent", counts.totalCount!=0 ); |
---|
201 | action_sensitize( "open-torrent-folder", counts.totalCount==1 ); |
---|
202 | action_sensitize( "show-torrent-details", counts.totalCount==1 ); |
---|
203 | |
---|
204 | canUpdate = 0; |
---|
205 | gtk_tree_selection_selected_foreach( s, accumulateCanUpdateForeach, &canUpdate ); |
---|
206 | action_sensitize( "update-tracker", canUpdate!=0 ); |
---|
207 | |
---|
208 | { |
---|
209 | GtkTreeView * view = gtk_tree_selection_get_tree_view( s ); |
---|
210 | GtkTreeModel * model = gtk_tree_view_get_model( view ); |
---|
211 | const int torrentCount = gtk_tree_model_iter_n_children( model, NULL ) != 0; |
---|
212 | action_sensitize( "select-all", torrentCount!=0 ); |
---|
213 | action_sensitize( "deselect-all", torrentCount!=0 ); |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | static void |
---|
218 | selectionChangedCB( GtkTreeSelection * s, gpointer unused UNUSED ) |
---|
219 | { |
---|
220 | refreshTorrentActions( s ); |
---|
221 | } |
---|
222 | |
---|
223 | static void |
---|
224 | onMainWindowSizeAllocated( GtkWidget * window, |
---|
225 | GtkAllocation * alloc UNUSED, |
---|
226 | gpointer gdata UNUSED ) |
---|
227 | { |
---|
228 | const gboolean isMaximized = window->window |
---|
229 | && ( gdk_window_get_state( window->window ) & GDK_WINDOW_STATE_MAXIMIZED ); |
---|
230 | |
---|
231 | if( !isMaximized ) |
---|
232 | { |
---|
233 | int x, y, w, h; |
---|
234 | gtk_window_get_position( GTK_WINDOW( window ), &x, &y ); |
---|
235 | gtk_window_get_size( GTK_WINDOW( window ), &w, &h ); |
---|
236 | pref_int_set( PREF_KEY_MAIN_WINDOW_X, x ); |
---|
237 | pref_int_set( PREF_KEY_MAIN_WINDOW_Y, y ); |
---|
238 | pref_int_set( PREF_KEY_MAIN_WINDOW_WIDTH, w ); |
---|
239 | pref_int_set( PREF_KEY_MAIN_WINDOW_HEIGHT, h ); |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | static void |
---|
244 | windowStateChanged( GtkWidget * widget UNUSED, GdkEventWindowState * event, gpointer gdata ) |
---|
245 | { |
---|
246 | if( event->changed_mask & GDK_WINDOW_STATE_ICONIFIED ) |
---|
247 | { |
---|
248 | struct cbdata * cbdata = gdata; |
---|
249 | cbdata->minimized = ( event->new_window_state & GDK_WINDOW_STATE_ICONIFIED ) ? 1 : 0; |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | static sig_atomic_t global_sigcount = 0; |
---|
254 | |
---|
255 | static void |
---|
256 | fatalsig( int sig ) |
---|
257 | { |
---|
258 | static const int SIGCOUNT_MAX = 3; /* revert to default handler after this many */ |
---|
259 | |
---|
260 | if( ++global_sigcount >= SIGCOUNT_MAX ) |
---|
261 | { |
---|
262 | signal( sig, SIG_DFL ); |
---|
263 | raise( sig ); |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | |
---|
268 | static void |
---|
269 | setupsighandlers( void ) |
---|
270 | { |
---|
271 | #ifdef G_OS_WIN32 |
---|
272 | const int sigs[] = { SIGINT, SIGTERM }; |
---|
273 | #else |
---|
274 | const int sigs[] = { SIGHUP, SIGINT, SIGQUIT, SIGTERM }; |
---|
275 | #endif |
---|
276 | guint i; |
---|
277 | |
---|
278 | for( i=0; i<G_N_ELEMENTS(sigs); ++i ) |
---|
279 | signal( sigs[i], fatalsig ); |
---|
280 | } |
---|
281 | |
---|
282 | struct rpc_data |
---|
283 | { |
---|
284 | int type; |
---|
285 | int torrentId; |
---|
286 | tr_torrent * tor; |
---|
287 | struct cbdata * cbdata; |
---|
288 | }; |
---|
289 | |
---|
290 | static int |
---|
291 | onRPCIdle( void * vdata ) |
---|
292 | { |
---|
293 | struct rpc_data * data = vdata; |
---|
294 | switch( data->type ) |
---|
295 | { |
---|
296 | case TR_RPC_TORRENT_ADDED: |
---|
297 | tr_core_add_torrent( data->cbdata->core, tr_torrent_new_preexisting( data->tor ) ); |
---|
298 | break; |
---|
299 | case TR_RPC_TORRENT_STARTED: |
---|
300 | /* this should be automatic */ |
---|
301 | break; |
---|
302 | case TR_RPC_TORRENT_STOPPED: |
---|
303 | /* this should be automatic */ |
---|
304 | break; |
---|
305 | case TR_RPC_TORRENT_REMOVING: |
---|
306 | /* FIXME */ |
---|
307 | break; |
---|
308 | case TR_RPC_TORRENT_CHANGED: |
---|
309 | case TR_RPC_SESSION_CHANGED: |
---|
310 | /* nothing interesting to do here */ |
---|
311 | break; |
---|
312 | } |
---|
313 | g_free( data ); |
---|
314 | return FALSE; |
---|
315 | } |
---|
316 | |
---|
317 | static void |
---|
318 | onRPCChanged( tr_handle * handle UNUSED, |
---|
319 | tr_rpc_callback_type type, |
---|
320 | struct tr_torrent * tor, |
---|
321 | void * cbdata ) |
---|
322 | { |
---|
323 | /* this callback is being invoked from the libtransmission thread, |
---|
324 | * so let's push the information over to the gtk+ thread where |
---|
325 | * it's safe to update the gui */ |
---|
326 | struct rpc_data * data = g_new0( struct rpc_data, 1 ); |
---|
327 | data->type = type; |
---|
328 | data->torrentId = tor ? tr_torrentId( tor ) : -1; |
---|
329 | data->tor = type == TR_RPC_TORRENT_REMOVING ? NULL : tor; |
---|
330 | data->cbdata = cbdata; |
---|
331 | g_idle_add( onRPCIdle, data ); |
---|
332 | } |
---|
333 | |
---|
334 | int |
---|
335 | main( int argc, char ** argv ) |
---|
336 | { |
---|
337 | char * err = NULL; |
---|
338 | struct cbdata * cbdata; |
---|
339 | GSList * argfiles; |
---|
340 | GError * gerr; |
---|
341 | gboolean didinit = FALSE; |
---|
342 | gboolean didlock = FALSE; |
---|
343 | gboolean startpaused = FALSE; |
---|
344 | gboolean startminimized = FALSE; |
---|
345 | char * domain = "transmission"; |
---|
346 | char * configDir = NULL; |
---|
347 | |
---|
348 | GOptionEntry entries[] = { |
---|
349 | { "paused", 'p', 0, G_OPTION_ARG_NONE, &startpaused, |
---|
350 | _("Start with all torrents paused"), NULL }, |
---|
351 | #ifdef STATUS_ICON_SUPPORTED |
---|
352 | { "minimized", 'm', 0, G_OPTION_ARG_NONE, &startminimized, |
---|
353 | _( "Start minimized in system tray"), NULL }, |
---|
354 | #endif |
---|
355 | { "config-dir", 'g', 0, G_OPTION_ARG_FILENAME, &configDir, |
---|
356 | _( "Where to look for configuration files" ), NULL }, |
---|
357 | { NULL, 0, 0, 0, NULL, NULL, NULL } |
---|
358 | }; |
---|
359 | |
---|
360 | cbdata = g_new0( struct cbdata, 1 ); |
---|
361 | cbdata->tor2details = g_hash_table_new( g_str_hash, g_str_equal ); |
---|
362 | cbdata->details2tor = g_hash_table_new( g_direct_hash, g_direct_equal ); |
---|
363 | |
---|
364 | /* bind the gettext domain */ |
---|
365 | bindtextdomain( domain, TRANSMISSIONLOCALEDIR ); |
---|
366 | bind_textdomain_codeset( domain, "UTF-8" ); |
---|
367 | textdomain( domain ); |
---|
368 | g_set_application_name( _( "Transmission" ) ); |
---|
369 | |
---|
370 | /* initialize gtk */ |
---|
371 | g_thread_init( NULL ); |
---|
372 | gerr = NULL; |
---|
373 | if( !gtk_init_with_args( &argc, &argv, _("[torrent files]"), entries, domain, &gerr ) ) { |
---|
374 | g_message( "%s", gerr->message ); |
---|
375 | g_clear_error( &gerr ); |
---|
376 | return 0; |
---|
377 | } |
---|
378 | if( configDir == NULL ) |
---|
379 | configDir = (char*) tr_getDefaultConfigDir( ); |
---|
380 | |
---|
381 | tr_notify_init( ); |
---|
382 | |
---|
383 | didinit = cf_init( configDir, NULL ); /* must come before actions_init */ |
---|
384 | tr_prefs_init_global( ); |
---|
385 | myUIManager = gtk_ui_manager_new (); |
---|
386 | actions_init ( myUIManager, cbdata ); |
---|
387 | gtk_ui_manager_add_ui_from_string (myUIManager, fallback_ui_file, -1, NULL); |
---|
388 | gtk_ui_manager_ensure_update (myUIManager); |
---|
389 | gtk_window_set_default_icon_name ( "transmission" ); |
---|
390 | |
---|
391 | setupsighandlers( ); /* set up handlers for fatal signals */ |
---|
392 | |
---|
393 | /* either get a lockfile s.t. this is the one instance of |
---|
394 | * transmission that's running, OR if there are files to |
---|
395 | * be added, delegate that to the running instance via dbus */ |
---|
396 | didlock = cf_lock( &err ); |
---|
397 | argfiles = checkfilenames( argc-1, argv+1 ); |
---|
398 | if( !didlock && argfiles ) |
---|
399 | { |
---|
400 | GSList * l; |
---|
401 | gboolean delegated = FALSE; |
---|
402 | for( l=argfiles; l; l=l->next ) |
---|
403 | delegated |= gtr_dbus_add_torrent( l->data ); |
---|
404 | if( delegated ) |
---|
405 | err = NULL; |
---|
406 | } |
---|
407 | |
---|
408 | if( didlock && ( didinit || cf_init( configDir, &err ) ) ) |
---|
409 | { |
---|
410 | tr_handle * h = tr_sessionInitFull( |
---|
411 | configDir, |
---|
412 | "gtk", |
---|
413 | pref_string_get( PREF_KEY_DOWNLOAD_DIR ), |
---|
414 | pref_flag_get( PREF_KEY_PEX ), |
---|
415 | pref_flag_get( PREF_KEY_NAT ), |
---|
416 | pref_int_get( PREF_KEY_PORT ), |
---|
417 | pref_flag_get( PREF_KEY_ENCRYPTED_ONLY ) |
---|
418 | ? TR_ENCRYPTION_REQUIRED |
---|
419 | : TR_ENCRYPTION_PREFERRED, |
---|
420 | pref_flag_get( PREF_KEY_UL_LIMIT_ENABLED ), |
---|
421 | pref_int_get( PREF_KEY_UL_LIMIT ), |
---|
422 | pref_flag_get( PREF_KEY_DL_LIMIT_ENABLED ), |
---|
423 | pref_int_get( PREF_KEY_DL_LIMIT ), |
---|
424 | pref_int_get( PREF_KEY_MAX_PEERS_GLOBAL ), |
---|
425 | pref_int_get( PREF_KEY_MSGLEVEL ), |
---|
426 | TRUE, /* message queueing */ |
---|
427 | pref_flag_get( PREF_KEY_BLOCKLIST_ENABLED ), |
---|
428 | pref_int_get( PREF_KEY_PEER_SOCKET_TOS ), |
---|
429 | pref_flag_get( PREF_KEY_RPC_ENABLED ), |
---|
430 | pref_int_get( PREF_KEY_RPC_PORT ), |
---|
431 | pref_string_get( PREF_KEY_RPC_ACL ) ); |
---|
432 | cbdata->core = tr_core_new( h ); |
---|
433 | |
---|
434 | /* create main window now to be a parent to any error dialogs */ |
---|
435 | GtkWindow * win = GTK_WINDOW( tr_window_new( myUIManager, cbdata->core ) ); |
---|
436 | g_signal_connect( win, "window-state-event", G_CALLBACK(windowStateChanged), cbdata ); |
---|
437 | g_signal_connect( win, "size-allocate", G_CALLBACK(onMainWindowSizeAllocated), cbdata ); |
---|
438 | |
---|
439 | appsetup( win, argfiles, cbdata, startpaused, startminimized ); |
---|
440 | tr_sessionSetRPCCallback( h, onRPCChanged, cbdata ); |
---|
441 | |
---|
442 | gtk_main(); |
---|
443 | } |
---|
444 | else if( err ) |
---|
445 | { |
---|
446 | gtk_widget_show( errmsg_full( NULL, (callbackfunc_t)gtk_main_quit, |
---|
447 | NULL, "%s", err ) ); |
---|
448 | g_free( err ); |
---|
449 | gtk_main(); |
---|
450 | } |
---|
451 | |
---|
452 | return 0; |
---|
453 | } |
---|
454 | |
---|
455 | static void |
---|
456 | appsetup( TrWindow * wind, GSList * torrentFiles, |
---|
457 | struct cbdata * cbdata, |
---|
458 | gboolean forcepause, gboolean minimized ) |
---|
459 | { |
---|
460 | const pref_flag_t start = forcepause ? PREF_FLAG_FALSE : PREF_FLAG_DEFAULT; |
---|
461 | const pref_flag_t prompt = PREF_FLAG_DEFAULT; |
---|
462 | |
---|
463 | /* fill out cbdata */ |
---|
464 | cbdata->wind = NULL; |
---|
465 | cbdata->icon = NULL; |
---|
466 | cbdata->msgwin = NULL; |
---|
467 | cbdata->prefs = NULL; |
---|
468 | cbdata->timer = 0; |
---|
469 | cbdata->closing = FALSE; |
---|
470 | cbdata->errqueue = NULL; |
---|
471 | cbdata->minimized = minimized; |
---|
472 | |
---|
473 | if( minimized ) |
---|
474 | pref_flag_set( PREF_KEY_TRAY_ICON_ENABLED, TRUE ); |
---|
475 | |
---|
476 | actions_set_core( cbdata->core ); |
---|
477 | |
---|
478 | /* set up core handlers */ |
---|
479 | g_signal_connect( cbdata->core, "error", G_CALLBACK( coreerr ), cbdata ); |
---|
480 | g_signal_connect( cbdata->core, "add-torrent-prompt", |
---|
481 | G_CALLBACK( onAddTorrent ), cbdata ); |
---|
482 | g_signal_connect_swapped( cbdata->core, "quit", |
---|
483 | G_CALLBACK( wannaquit ), cbdata ); |
---|
484 | g_signal_connect( cbdata->core, "prefs-changed", |
---|
485 | G_CALLBACK( prefschanged ), cbdata ); |
---|
486 | |
---|
487 | /* add torrents from command-line and saved state */ |
---|
488 | tr_core_load( cbdata->core, forcepause ); |
---|
489 | tr_core_add_list( cbdata->core, torrentFiles, start, prompt ); |
---|
490 | torrentFiles = NULL; |
---|
491 | tr_core_torrents_added( cbdata->core ); |
---|
492 | |
---|
493 | /* set up main window */ |
---|
494 | winsetup( cbdata, wind ); |
---|
495 | |
---|
496 | /* set up the icon */ |
---|
497 | prefschanged( cbdata->core, PREF_KEY_TRAY_ICON_ENABLED, cbdata ); |
---|
498 | |
---|
499 | /* start model update timer */ |
---|
500 | cbdata->timer = g_timeout_add( UPDATE_INTERVAL, updatemodel, cbdata ); |
---|
501 | updatemodel( cbdata ); |
---|
502 | |
---|
503 | /* either show the window or iconify it */ |
---|
504 | if( !minimized ) |
---|
505 | gtk_widget_show( GTK_WIDGET( wind ) ); |
---|
506 | else { |
---|
507 | gtk_window_iconify( wind ); |
---|
508 | gtk_window_set_skip_taskbar_hint( cbdata->wind, cbdata->icon != NULL ); |
---|
509 | } |
---|
510 | } |
---|
511 | |
---|
512 | |
---|
513 | /** |
---|
514 | * hideMainWindow, and the timeout hack in toggleMainWindow, |
---|
515 | * are loosely cribbed from Colin Walters' tr-shell.c in Rhythmbox |
---|
516 | */ |
---|
517 | static gboolean |
---|
518 | idle_hide_mainwindow( gpointer window ) |
---|
519 | { |
---|
520 | gtk_widget_hide( window ); |
---|
521 | return FALSE; |
---|
522 | } |
---|
523 | static void |
---|
524 | hideMainWindow( struct cbdata * cbdata ) |
---|
525 | { |
---|
526 | #if defined(STATUS_ICON_SUPPORTED) && defined(GDK_WINDOWING_X11) |
---|
527 | GdkRectangle bounds; |
---|
528 | gulong data[4]; |
---|
529 | Display *dpy; |
---|
530 | GdkWindow *gdk_window; |
---|
531 | |
---|
532 | gtk_status_icon_get_geometry( GTK_STATUS_ICON( cbdata->icon ), NULL, &bounds, NULL ); |
---|
533 | gdk_window = GTK_WIDGET (cbdata->wind)->window; |
---|
534 | dpy = gdk_x11_drawable_get_xdisplay (gdk_window); |
---|
535 | |
---|
536 | data[0] = bounds.x; |
---|
537 | data[1] = bounds.y; |
---|
538 | data[2] = bounds.width; |
---|
539 | data[3] = bounds.height; |
---|
540 | |
---|
541 | XChangeProperty (dpy, |
---|
542 | GDK_WINDOW_XID (gdk_window), |
---|
543 | gdk_x11_get_xatom_by_name_for_display (gdk_drawable_get_display (gdk_window), |
---|
544 | "_NET_WM_ICON_GEOMETRY"), |
---|
545 | XA_CARDINAL, 32, PropModeReplace, |
---|
546 | (guchar*)&data, 4); |
---|
547 | |
---|
548 | gtk_window_set_skip_taskbar_hint( cbdata->wind, TRUE ); |
---|
549 | #endif |
---|
550 | gtk_window_iconify( cbdata->wind ); |
---|
551 | } |
---|
552 | |
---|
553 | static void |
---|
554 | clearTag( guint * tag ) |
---|
555 | { |
---|
556 | if( *tag ) |
---|
557 | g_source_remove( *tag ); |
---|
558 | *tag = 0; |
---|
559 | } |
---|
560 | |
---|
561 | static void |
---|
562 | toggleMainWindow( struct cbdata * cbdata ) |
---|
563 | { |
---|
564 | GtkWindow * window = GTK_WINDOW( cbdata->wind ); |
---|
565 | const int hide = !cbdata->minimized; |
---|
566 | static int x=0, y=0; |
---|
567 | |
---|
568 | if( hide ) |
---|
569 | { |
---|
570 | gtk_window_get_position( window, &x, &y ); |
---|
571 | clearTag( &cbdata->idle_hide_mainwindow_tag ); |
---|
572 | hideMainWindow( cbdata ); |
---|
573 | cbdata->idle_hide_mainwindow_tag = g_timeout_add( 250, idle_hide_mainwindow, window ); |
---|
574 | } |
---|
575 | else |
---|
576 | { |
---|
577 | gtk_window_set_skip_taskbar_hint( window, FALSE ); |
---|
578 | gtk_window_move( window, x, y ); |
---|
579 | gtk_widget_show( GTK_WIDGET( window ) ); |
---|
580 | gtk_window_deiconify( window ); |
---|
581 | #if GTK_CHECK_VERSION(2,8,0) |
---|
582 | gtk_window_present_with_time( window, gtk_get_current_event_time( ) ); |
---|
583 | #else |
---|
584 | gtk_window_present( window ); |
---|
585 | #endif |
---|
586 | } |
---|
587 | } |
---|
588 | |
---|
589 | static gboolean |
---|
590 | winclose( GtkWidget * w UNUSED, GdkEvent * event UNUSED, gpointer gdata ) |
---|
591 | { |
---|
592 | struct cbdata * cbdata = gdata; |
---|
593 | |
---|
594 | if( cbdata->icon != NULL ) |
---|
595 | action_activate ("toggle-main-window"); |
---|
596 | else |
---|
597 | askquit( cbdata->core, cbdata->wind, wannaquit, cbdata ); |
---|
598 | |
---|
599 | return TRUE; /* don't propagate event further */ |
---|
600 | } |
---|
601 | |
---|
602 | static void |
---|
603 | rowChangedCB( GtkTreeModel * model UNUSED, |
---|
604 | GtkTreePath * path, |
---|
605 | GtkTreeIter * iter UNUSED, |
---|
606 | gpointer sel) |
---|
607 | { |
---|
608 | if( gtk_tree_selection_path_is_selected ( sel, path ) ) |
---|
609 | refreshTorrentActions( GTK_TREE_SELECTION(sel) ); |
---|
610 | } |
---|
611 | |
---|
612 | static void |
---|
613 | winsetup( struct cbdata * cbdata, TrWindow * wind ) |
---|
614 | { |
---|
615 | GtkTreeModel * model; |
---|
616 | GtkTreeSelection * sel; |
---|
617 | |
---|
618 | g_assert( NULL == cbdata->wind ); |
---|
619 | cbdata->wind = GTK_WINDOW( wind ); |
---|
620 | |
---|
621 | sel = tr_window_get_selection( cbdata->wind ); |
---|
622 | g_signal_connect( sel, "changed", G_CALLBACK(selectionChangedCB), NULL ); |
---|
623 | selectionChangedCB( sel, NULL ); |
---|
624 | model = tr_core_model( cbdata->core ); |
---|
625 | g_signal_connect( model, "row-changed", G_CALLBACK(rowChangedCB), sel ); |
---|
626 | g_signal_connect( wind, "delete-event", G_CALLBACK( winclose ), cbdata ); |
---|
627 | refreshTorrentActions( sel ); |
---|
628 | |
---|
629 | setupdrag( GTK_WIDGET(wind), cbdata ); |
---|
630 | } |
---|
631 | |
---|
632 | static gpointer |
---|
633 | quitThreadFunc( gpointer gdata ) |
---|
634 | { |
---|
635 | struct cbdata * cbdata = gdata; |
---|
636 | |
---|
637 | tr_core_close( cbdata->core ); |
---|
638 | |
---|
639 | /* shutdown the gui */ |
---|
640 | if( cbdata->prefs ) |
---|
641 | gtk_widget_destroy( GTK_WIDGET( cbdata->prefs ) ); |
---|
642 | if( cbdata->wind ) |
---|
643 | gtk_widget_destroy( GTK_WIDGET( cbdata->wind ) ); |
---|
644 | g_object_unref( cbdata->core ); |
---|
645 | if( cbdata->icon ) |
---|
646 | g_object_unref( cbdata->icon ); |
---|
647 | if( cbdata->errqueue ) { |
---|
648 | g_slist_foreach( cbdata->errqueue, (GFunc)g_free, NULL ); |
---|
649 | g_slist_free( cbdata->errqueue ); |
---|
650 | } |
---|
651 | |
---|
652 | g_hash_table_destroy( cbdata->details2tor ); |
---|
653 | g_hash_table_destroy( cbdata->tor2details ); |
---|
654 | g_free( cbdata ); |
---|
655 | |
---|
656 | /* exit the gtk main loop */ |
---|
657 | gtk_main_quit( ); |
---|
658 | return NULL; |
---|
659 | } |
---|
660 | |
---|
661 | static void |
---|
662 | do_exit_cb( GtkWidget *w UNUSED, gpointer data UNUSED ) |
---|
663 | { |
---|
664 | exit( 0 ); |
---|
665 | } |
---|
666 | |
---|
667 | static void |
---|
668 | wannaquit( void * vdata ) |
---|
669 | { |
---|
670 | GtkWidget * r, * p, * b, * w, *c; |
---|
671 | struct cbdata * cbdata = vdata; |
---|
672 | |
---|
673 | /* stop the update timer */ |
---|
674 | if( cbdata->timer ) { |
---|
675 | g_source_remove( cbdata->timer ); |
---|
676 | cbdata->timer = 0; |
---|
677 | } |
---|
678 | |
---|
679 | c = GTK_WIDGET( cbdata->wind ); |
---|
680 | gtk_container_remove( GTK_CONTAINER( c ), gtk_bin_get_child( GTK_BIN( c ) ) ); |
---|
681 | |
---|
682 | r = gtk_alignment_new(0.5, 0.5, 0.01, 0.01); |
---|
683 | gtk_container_add(GTK_CONTAINER(c), r); |
---|
684 | |
---|
685 | p = gtk_table_new(3, 2, FALSE); |
---|
686 | gtk_table_set_col_spacings( GTK_TABLE( p ), GUI_PAD_BIG ); |
---|
687 | gtk_container_add( GTK_CONTAINER( r ), p ); |
---|
688 | |
---|
689 | w = gtk_image_new_from_stock( GTK_STOCK_NETWORK, GTK_ICON_SIZE_DIALOG ); |
---|
690 | gtk_table_attach_defaults(GTK_TABLE(p), w, 0, 1, 0, 2 ); |
---|
691 | |
---|
692 | w = gtk_label_new( NULL ); |
---|
693 | gtk_label_set_markup( GTK_LABEL( w ), _( "<b>Closing Connections</b>" ) ); |
---|
694 | gtk_misc_set_alignment( GTK_MISC( w ), 0.0, 0.5 ); |
---|
695 | gtk_table_attach_defaults( GTK_TABLE( p ), w, 1, 2, 0, 1 ); |
---|
696 | |
---|
697 | w = gtk_label_new( _( "Sending upload/download totals to tracker..." ) ); |
---|
698 | gtk_misc_set_alignment( GTK_MISC( w ), 0.0, 0.5 ); |
---|
699 | gtk_table_attach_defaults( GTK_TABLE( p ), w, 1, 2, 1, 2 ); |
---|
700 | |
---|
701 | b = gtk_alignment_new(0.0, 1.0, 0.01, 0.01); |
---|
702 | w = gtk_button_new_with_label( _( "_Quit Immediately" ) ); |
---|
703 | gtk_button_set_image( GTK_BUTTON(w), gtk_image_new_from_stock( GTK_STOCK_QUIT, GTK_ICON_SIZE_BUTTON ) ); |
---|
704 | g_signal_connect(w, "clicked", G_CALLBACK(do_exit_cb), NULL); |
---|
705 | gtk_container_add(GTK_CONTAINER(b), w); |
---|
706 | gtk_table_attach(GTK_TABLE(p), b, 1, 2, 2, 3, GTK_FILL, GTK_FILL, 0, 10 ); |
---|
707 | |
---|
708 | gtk_widget_show_all(r); |
---|
709 | |
---|
710 | /* clear the UI */ |
---|
711 | gtk_list_store_clear( GTK_LIST_STORE( tr_core_model( cbdata->core ) ) ); |
---|
712 | |
---|
713 | /* shut down libT */ |
---|
714 | g_thread_create( quitThreadFunc, vdata, TRUE, NULL ); |
---|
715 | } |
---|
716 | |
---|
717 | static void |
---|
718 | gotdrag( GtkWidget * widget UNUSED, |
---|
719 | GdkDragContext * dc, |
---|
720 | gint x UNUSED, |
---|
721 | gint y UNUSED, |
---|
722 | GtkSelectionData * sel, |
---|
723 | guint info UNUSED, |
---|
724 | guint time, |
---|
725 | gpointer gdata ) |
---|
726 | { |
---|
727 | struct cbdata * data = gdata; |
---|
728 | GSList * paths = NULL; |
---|
729 | GSList * freeme = NULL; |
---|
730 | |
---|
731 | #if 0 |
---|
732 | int i; |
---|
733 | char *sele = gdk_atom_name(sel->selection); |
---|
734 | char *targ = gdk_atom_name(sel->target); |
---|
735 | char *type = gdk_atom_name(sel->type); |
---|
736 | |
---|
737 | g_message( "dropped file: sel=%s targ=%s type=%s fmt=%i len=%i", |
---|
738 | sele, targ, type, sel->format, sel->length ); |
---|
739 | g_free(sele); |
---|
740 | g_free(targ); |
---|
741 | g_free(type); |
---|
742 | if( sel->format == 8 ) { |
---|
743 | for( i=0; i<sel->length; ++i ) |
---|
744 | fprintf(stderr, "%02X ", sel->data[i]); |
---|
745 | fprintf(stderr, "\n"); |
---|
746 | } |
---|
747 | #endif |
---|
748 | |
---|
749 | if( ( sel->format == 8 ) && |
---|
750 | ( sel->selection == gdk_atom_intern( "XdndSelection", FALSE ) ) ) |
---|
751 | { |
---|
752 | int i; |
---|
753 | char * str = g_strndup( (char*)sel->data, sel->length ); |
---|
754 | gchar ** files = g_strsplit_set( str, "\r\n", -1 ); |
---|
755 | for( i=0; files && files[i]; ++i ) |
---|
756 | { |
---|
757 | char * filename; |
---|
758 | if( !*files[i] ) /* empty filename... */ |
---|
759 | continue; |
---|
760 | |
---|
761 | /* decode the filename */ |
---|
762 | filename = decode_uri( files[i] ); |
---|
763 | freeme = g_slist_prepend( freeme, filename ); |
---|
764 | if( !g_utf8_validate( filename, -1, NULL ) ) |
---|
765 | continue; |
---|
766 | |
---|
767 | /* walk past "file://", if present */ |
---|
768 | if( g_str_has_prefix( filename, "file:" ) ) { |
---|
769 | filename += 5; |
---|
770 | while( g_str_has_prefix( filename, "//" ) ) |
---|
771 | ++filename; |
---|
772 | } |
---|
773 | |
---|
774 | /* if the file doesn't exist, the first part |
---|
775 | might be a hostname ... walk past it. */ |
---|
776 | if( !g_file_test( filename, G_FILE_TEST_EXISTS ) ) { |
---|
777 | char * pch = strchr( filename + 1, '/' ); |
---|
778 | if( pch != NULL ) |
---|
779 | filename = pch; |
---|
780 | } |
---|
781 | |
---|
782 | /* finally, add it to the list of torrents to try adding */ |
---|
783 | if( g_file_test( filename, G_FILE_TEST_EXISTS ) ) |
---|
784 | paths = g_slist_prepend( paths, g_strdup( filename ) ); |
---|
785 | } |
---|
786 | |
---|
787 | /* try to add any torrents we found */ |
---|
788 | if( paths ) |
---|
789 | { |
---|
790 | paths = g_slist_reverse( paths ); |
---|
791 | tr_core_add_list_defaults( data->core, paths ); |
---|
792 | tr_core_torrents_added( data->core ); |
---|
793 | } |
---|
794 | |
---|
795 | freestrlist( freeme ); |
---|
796 | g_strfreev( files ); |
---|
797 | g_free( str ); |
---|
798 | } |
---|
799 | |
---|
800 | gtk_drag_finish(dc, (NULL != paths), FALSE, time); |
---|
801 | } |
---|
802 | |
---|
803 | static void |
---|
804 | setupdrag(GtkWidget *widget, struct cbdata *data) { |
---|
805 | GtkTargetEntry targets[] = { |
---|
806 | { "STRING", 0, 0 }, |
---|
807 | { "text/plain", 0, 0 }, |
---|
808 | { "text/uri-list", 0, 0 }, |
---|
809 | }; |
---|
810 | |
---|
811 | g_signal_connect(widget, "drag_data_received", G_CALLBACK(gotdrag), data); |
---|
812 | |
---|
813 | gtk_drag_dest_set(widget, GTK_DEST_DEFAULT_ALL, targets, |
---|
814 | ALEN(targets), GDK_ACTION_COPY | GDK_ACTION_MOVE); |
---|
815 | } |
---|
816 | |
---|
817 | static void |
---|
818 | coreerr( TrCore * core UNUSED, enum tr_core_err code, const char * msg, |
---|
819 | gpointer gdata ) |
---|
820 | { |
---|
821 | struct cbdata * cbdata = gdata; |
---|
822 | char * joined; |
---|
823 | |
---|
824 | switch( code ) |
---|
825 | { |
---|
826 | case TR_CORE_ERR_ADD_TORRENT: |
---|
827 | cbdata->errqueue = g_slist_append( cbdata->errqueue, |
---|
828 | g_strdup( msg ) ); |
---|
829 | return; |
---|
830 | case TR_CORE_ERR_NO_MORE_TORRENTS: |
---|
831 | if( cbdata->errqueue ) |
---|
832 | { |
---|
833 | joined = joinstrlist( cbdata->errqueue, "\n" ); |
---|
834 | errmsg( cbdata->wind, |
---|
835 | ngettext( "Failed to load torrent file: %s", |
---|
836 | "Failed to load torrent files: %s", |
---|
837 | g_slist_length( cbdata->errqueue ) ), |
---|
838 | joined ); |
---|
839 | g_slist_foreach( cbdata->errqueue, (GFunc) g_free, NULL ); |
---|
840 | g_slist_free( cbdata->errqueue ); |
---|
841 | cbdata->errqueue = NULL; |
---|
842 | g_free( joined ); |
---|
843 | } |
---|
844 | return; |
---|
845 | case TR_CORE_ERR_SAVE_STATE: |
---|
846 | errmsg( cbdata->wind, "%s", msg ); |
---|
847 | return; |
---|
848 | } |
---|
849 | |
---|
850 | g_assert_not_reached(); |
---|
851 | } |
---|
852 | |
---|
853 | #if GTK_CHECK_VERSION(2,8,0) |
---|
854 | static void |
---|
855 | on_main_window_focus_in( GtkWidget * widget UNUSED, |
---|
856 | GdkEventFocus * event UNUSED, |
---|
857 | gpointer gdata ) |
---|
858 | { |
---|
859 | struct cbdata * cbdata = gdata; |
---|
860 | gtk_window_set_urgency_hint( GTK_WINDOW( cbdata->wind ), FALSE ); |
---|
861 | } |
---|
862 | #endif |
---|
863 | |
---|
864 | static void |
---|
865 | onAddTorrent( TrCore * core, tr_ctor * ctor, gpointer gdata ) |
---|
866 | { |
---|
867 | struct cbdata * cbdata = gdata; |
---|
868 | GtkWidget * w = addSingleTorrentDialog( cbdata->wind, core, ctor ); |
---|
869 | #if GTK_CHECK_VERSION(2,8,0) |
---|
870 | g_signal_connect( w, "focus-in-event", G_CALLBACK(on_main_window_focus_in), cbdata ); |
---|
871 | gtk_window_set_urgency_hint( cbdata->wind, TRUE ); |
---|
872 | #endif |
---|
873 | } |
---|
874 | |
---|
875 | static void |
---|
876 | prefschanged( TrCore * core UNUSED, const char * key, gpointer data ) |
---|
877 | { |
---|
878 | struct cbdata * cbdata = data; |
---|
879 | tr_handle * tr = tr_core_handle( cbdata->core ); |
---|
880 | |
---|
881 | if( !strcmp( key, PREF_KEY_ENCRYPTED_ONLY ) ) |
---|
882 | { |
---|
883 | const gboolean crypto_only = pref_flag_get( key ); |
---|
884 | tr_sessionSetEncryption( tr, crypto_only ? TR_ENCRYPTION_REQUIRED |
---|
885 | : TR_ENCRYPTION_PREFERRED ); |
---|
886 | } |
---|
887 | else if( !strcmp( key, PREF_KEY_PORT ) ) |
---|
888 | { |
---|
889 | const int port = pref_int_get( key ); |
---|
890 | tr_sessionSetPeerPort( tr, port ); |
---|
891 | } |
---|
892 | else if( !strcmp( key, PREF_KEY_TRAY_ICON_ENABLED ) ) |
---|
893 | { |
---|
894 | const int show = pref_flag_get( key ); |
---|
895 | action_sensitize ( "close", show ); |
---|
896 | if( show && !cbdata->icon ) |
---|
897 | cbdata->icon = tr_icon_new( cbdata->core ); |
---|
898 | else if( !show && cbdata->icon ) { |
---|
899 | g_object_unref( cbdata->icon ); |
---|
900 | cbdata->icon = NULL; |
---|
901 | } |
---|
902 | } |
---|
903 | else if( !strcmp( key, PREF_KEY_DL_LIMIT_ENABLED ) ) |
---|
904 | { |
---|
905 | const gboolean b = pref_flag_get( key ); |
---|
906 | tr_sessionSetSpeedLimitEnabled( tr, TR_DOWN, b ); |
---|
907 | } |
---|
908 | else if( !strcmp( key, PREF_KEY_DL_LIMIT ) ) |
---|
909 | { |
---|
910 | const int limit = pref_int_get( key ); |
---|
911 | tr_sessionSetSpeedLimit( tr, TR_DOWN, limit ); |
---|
912 | } |
---|
913 | else if( !strcmp( key, PREF_KEY_UL_LIMIT_ENABLED ) ) |
---|
914 | { |
---|
915 | const gboolean b = pref_flag_get( key ); |
---|
916 | tr_sessionSetSpeedLimitEnabled( tr, TR_UP, b ); |
---|
917 | } |
---|
918 | else if( !strcmp( key, PREF_KEY_UL_LIMIT ) ) |
---|
919 | { |
---|
920 | const int limit = pref_int_get( key ); |
---|
921 | tr_sessionSetSpeedLimit( tr, TR_UP, limit ); |
---|
922 | } |
---|
923 | else if( !strcmp( key, PREF_KEY_NAT ) ) |
---|
924 | { |
---|
925 | const gboolean enabled = pref_flag_get( key ); |
---|
926 | tr_sessionSetPortForwardingEnabled( tr, enabled ); |
---|
927 | } |
---|
928 | else if( !strcmp( key, PREF_KEY_PEX ) ) |
---|
929 | { |
---|
930 | const gboolean b = pref_flag_get( key ); |
---|
931 | tr_sessionSetPortForwardingEnabled( tr, b ); |
---|
932 | } |
---|
933 | else if( !strcmp( key, PREF_KEY_RPC_ENABLED ) ) |
---|
934 | { |
---|
935 | tr_sessionSetRPCEnabled( tr, pref_flag_get( key ) ); |
---|
936 | } |
---|
937 | else if( !strcmp( key, PREF_KEY_RPC_PORT ) ) |
---|
938 | { |
---|
939 | tr_sessionSetRPCPort( tr, pref_int_get( key ) ); |
---|
940 | } |
---|
941 | else if( !strcmp( key, PREF_KEY_RPC_ENABLED ) ) |
---|
942 | { |
---|
943 | tr_sessionSetRPCEnabled( tr, pref_flag_get( key ) ); |
---|
944 | } |
---|
945 | } |
---|
946 | |
---|
947 | gboolean |
---|
948 | updatemodel(gpointer gdata) { |
---|
949 | struct cbdata *data = gdata; |
---|
950 | |
---|
951 | if( !data->closing && 0 < global_sigcount ) |
---|
952 | { |
---|
953 | wannaquit( data ); |
---|
954 | return FALSE; |
---|
955 | } |
---|
956 | |
---|
957 | /* update the torrent data in the model */ |
---|
958 | tr_core_update( data->core ); |
---|
959 | |
---|
960 | /* update the main window's statusbar and toolbar buttons */ |
---|
961 | if( data->wind ) |
---|
962 | tr_window_update( data->wind ); |
---|
963 | |
---|
964 | return TRUE; |
---|
965 | } |
---|
966 | |
---|
967 | static void |
---|
968 | aboutDialogActivateLink( GtkAboutDialog * dialog UNUSED, |
---|
969 | const gchar * link_, |
---|
970 | gpointer user_data UNUSED ) |
---|
971 | { |
---|
972 | gtr_open_file( link_ ); |
---|
973 | } |
---|
974 | |
---|
975 | static void |
---|
976 | about ( GtkWindow * parent ) |
---|
977 | { |
---|
978 | const char *authors[] = |
---|
979 | { |
---|
980 | "Charles Kerr (Backend; GTK+)", |
---|
981 | "Mitchell Livingston (Backend; OS X)", |
---|
982 | "Eric Petit (Backend; OS X)", |
---|
983 | "Josh Elsasser (Daemon; Backend; GTK+)", |
---|
984 | "Bryan Varner (BeOS)", |
---|
985 | NULL |
---|
986 | }; |
---|
987 | |
---|
988 | gtk_about_dialog_set_url_hook( aboutDialogActivateLink, NULL, NULL ); |
---|
989 | |
---|
990 | gtk_show_about_dialog( parent, |
---|
991 | "name", g_get_application_name(), |
---|
992 | "comments", _("A fast and easy BitTorrent client"), |
---|
993 | "version", LONG_VERSION_STRING, |
---|
994 | "website", "http://www.transmissionbt.com/", |
---|
995 | "copyright",_("Copyright 2005-2008 The Transmission Project"), |
---|
996 | "logo-icon-name", "transmission", |
---|
997 | #ifdef SHOW_LICENSE |
---|
998 | "license", LICENSE, |
---|
999 | "wrap-license", TRUE, |
---|
1000 | #endif |
---|
1001 | "authors", authors, |
---|
1002 | /* Translators: translate "translator-credits" as your name |
---|
1003 | to have it appear in the credits in the "About" dialog */ |
---|
1004 | "translator-credits", _("translator-credits"), |
---|
1005 | NULL ); |
---|
1006 | } |
---|
1007 | |
---|
1008 | static void |
---|
1009 | startTorrentForeach (GtkTreeModel * model, |
---|
1010 | GtkTreePath * path UNUSED, |
---|
1011 | GtkTreeIter * iter, |
---|
1012 | gpointer data UNUSED) |
---|
1013 | { |
---|
1014 | TrTorrent * tor = NULL; |
---|
1015 | gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 ); |
---|
1016 | tr_torrent_start( tor ); |
---|
1017 | g_object_unref( G_OBJECT( tor ) ); |
---|
1018 | } |
---|
1019 | |
---|
1020 | static void |
---|
1021 | stopTorrentForeach (GtkTreeModel * model, |
---|
1022 | GtkTreePath * path UNUSED, |
---|
1023 | GtkTreeIter * iter, |
---|
1024 | gpointer data UNUSED) |
---|
1025 | { |
---|
1026 | TrTorrent * tor = NULL; |
---|
1027 | gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 ); |
---|
1028 | tr_torrent_stop( tor ); |
---|
1029 | g_object_unref( G_OBJECT( tor ) ); |
---|
1030 | } |
---|
1031 | |
---|
1032 | static void |
---|
1033 | updateTrackerForeach (GtkTreeModel * model, |
---|
1034 | GtkTreePath * path UNUSED, |
---|
1035 | GtkTreeIter * iter, |
---|
1036 | gpointer data UNUSED) |
---|
1037 | { |
---|
1038 | TrTorrent * tor = NULL; |
---|
1039 | gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 ); |
---|
1040 | tr_torrentManualUpdate( tr_torrent_handle( tor ) ); |
---|
1041 | g_object_unref( G_OBJECT( tor ) ); |
---|
1042 | } |
---|
1043 | |
---|
1044 | static void |
---|
1045 | detailsClosed( gpointer user_data, GObject * details ) |
---|
1046 | { |
---|
1047 | struct cbdata * data = user_data; |
---|
1048 | gpointer hashString = g_hash_table_lookup( data->details2tor, details ); |
---|
1049 | g_hash_table_remove( data->details2tor, details ); |
---|
1050 | g_hash_table_remove( data->tor2details, hashString ); |
---|
1051 | } |
---|
1052 | |
---|
1053 | static void |
---|
1054 | openFolderForeach( GtkTreeModel * model, |
---|
1055 | GtkTreePath * path UNUSED, |
---|
1056 | GtkTreeIter * iter, |
---|
1057 | gpointer user_data UNUSED ) |
---|
1058 | { |
---|
1059 | TrTorrent * gtor = NULL; |
---|
1060 | gtk_tree_model_get( model, iter, MC_TORRENT, >or, -1 ); |
---|
1061 | tr_torrent_open_folder( gtor ); |
---|
1062 | g_object_unref( G_OBJECT( gtor ) ); |
---|
1063 | } |
---|
1064 | |
---|
1065 | static void |
---|
1066 | showInfoForeach (GtkTreeModel * model, |
---|
1067 | GtkTreePath * path UNUSED, |
---|
1068 | GtkTreeIter * iter, |
---|
1069 | gpointer user_data ) |
---|
1070 | { |
---|
1071 | const char * hashString; |
---|
1072 | struct cbdata * data = user_data; |
---|
1073 | TrTorrent * tor = NULL; |
---|
1074 | GtkWidget * w; |
---|
1075 | |
---|
1076 | gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 ); |
---|
1077 | hashString = tr_torrent_info(tor)->hashString; |
---|
1078 | w = g_hash_table_lookup( data->tor2details, hashString ); |
---|
1079 | if( w != NULL ) |
---|
1080 | gtk_window_present( GTK_WINDOW( w ) ); |
---|
1081 | else { |
---|
1082 | w = torrent_inspector_new( GTK_WINDOW( data->wind ), tor ); |
---|
1083 | gtk_widget_show( w ); |
---|
1084 | g_hash_table_insert( data->tor2details, (gpointer)hashString, w ); |
---|
1085 | g_hash_table_insert( data->details2tor, w, (gpointer)hashString ); |
---|
1086 | g_object_weak_ref( G_OBJECT( w ), detailsClosed, data ); |
---|
1087 | } |
---|
1088 | |
---|
1089 | g_object_unref( G_OBJECT( tor ) ); |
---|
1090 | } |
---|
1091 | |
---|
1092 | static void |
---|
1093 | recheckTorrentForeach (GtkTreeModel * model, |
---|
1094 | GtkTreePath * path UNUSED, |
---|
1095 | GtkTreeIter * iter, |
---|
1096 | gpointer data UNUSED) |
---|
1097 | { |
---|
1098 | TrTorrent * gtor = NULL; |
---|
1099 | gtk_tree_model_get( model, iter, MC_TORRENT, >or, -1 ); |
---|
1100 | tr_torrentVerify( tr_torrent_handle( gtor ) ); |
---|
1101 | g_object_unref( G_OBJECT( gtor ) ); |
---|
1102 | } |
---|
1103 | |
---|
1104 | static gboolean |
---|
1105 | msgwinclosed( void ) |
---|
1106 | { |
---|
1107 | action_toggle( "toggle-message-log", FALSE ); |
---|
1108 | return FALSE; |
---|
1109 | } |
---|
1110 | |
---|
1111 | static void |
---|
1112 | accumulateSelectedTorrents( GtkTreeModel * model, |
---|
1113 | GtkTreePath * path UNUSED, |
---|
1114 | GtkTreeIter * iter, |
---|
1115 | gpointer gdata ) |
---|
1116 | { |
---|
1117 | GSList ** data = ( GSList** ) gdata; |
---|
1118 | TrTorrent * tor = NULL; |
---|
1119 | gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 ); |
---|
1120 | *data = g_slist_prepend( *data, tor ); |
---|
1121 | } |
---|
1122 | |
---|
1123 | static void |
---|
1124 | removeSelected( struct cbdata * data, gboolean delete_files ) |
---|
1125 | { |
---|
1126 | GSList * l = NULL; |
---|
1127 | GtkTreeSelection * s = tr_window_get_selection( data->wind ); |
---|
1128 | gtk_tree_selection_selected_foreach( s, accumulateSelectedTorrents, &l ); |
---|
1129 | gtk_tree_selection_unselect_all( s ); |
---|
1130 | if( l ) { |
---|
1131 | l = g_slist_reverse( l ); |
---|
1132 | confirmRemove( data->wind, data->core, l, delete_files ); |
---|
1133 | } |
---|
1134 | } |
---|
1135 | |
---|
1136 | void |
---|
1137 | doAction ( const char * action_name, gpointer user_data ) |
---|
1138 | { |
---|
1139 | struct cbdata * data = user_data; |
---|
1140 | gboolean changed = FALSE; |
---|
1141 | |
---|
1142 | if ( !strcmp (action_name, "add-torrent-menu") || |
---|
1143 | !strcmp( action_name, "add-torrent-toolbar" )) |
---|
1144 | { |
---|
1145 | addDialog( data->wind, data->core ); |
---|
1146 | } |
---|
1147 | else if (!strcmp (action_name, "show-stats")) |
---|
1148 | { |
---|
1149 | GtkWidget * dialog = stats_dialog_create( data->wind, |
---|
1150 | data->core ); |
---|
1151 | gtk_widget_show( dialog ); |
---|
1152 | } |
---|
1153 | else if (!strcmp (action_name, "start-torrent")) |
---|
1154 | { |
---|
1155 | GtkTreeSelection * s = tr_window_get_selection(data->wind); |
---|
1156 | gtk_tree_selection_selected_foreach( s, startTorrentForeach, NULL ); |
---|
1157 | changed |= gtk_tree_selection_count_selected_rows( s ) != 0; |
---|
1158 | } |
---|
1159 | else if (!strcmp (action_name, "pause-torrent")) |
---|
1160 | { |
---|
1161 | GtkTreeSelection * s = tr_window_get_selection(data->wind); |
---|
1162 | gtk_tree_selection_selected_foreach( s, stopTorrentForeach, NULL ); |
---|
1163 | changed |= gtk_tree_selection_count_selected_rows( s ) != 0; |
---|
1164 | } |
---|
1165 | else if (!strcmp (action_name, "verify-torrent")) |
---|
1166 | { |
---|
1167 | GtkTreeSelection * s = tr_window_get_selection(data->wind); |
---|
1168 | gtk_tree_selection_selected_foreach( s, recheckTorrentForeach, NULL ); |
---|
1169 | changed |= gtk_tree_selection_count_selected_rows( s ) != 0; |
---|
1170 | } |
---|
1171 | else if (!strcmp (action_name, "open-torrent-folder")) |
---|
1172 | { |
---|
1173 | GtkTreeSelection * s = tr_window_get_selection(data->wind); |
---|
1174 | gtk_tree_selection_selected_foreach( s, openFolderForeach, data ); |
---|
1175 | } |
---|
1176 | else if (!strcmp (action_name, "show-torrent-details")) |
---|
1177 | { |
---|
1178 | GtkTreeSelection * s = tr_window_get_selection(data->wind); |
---|
1179 | gtk_tree_selection_selected_foreach( s, showInfoForeach, data ); |
---|
1180 | } |
---|
1181 | else if (!strcmp( action_name, "update-tracker")) |
---|
1182 | { |
---|
1183 | GtkTreeSelection * s = tr_window_get_selection(data->wind); |
---|
1184 | gtk_tree_selection_selected_foreach( s, updateTrackerForeach, data->wind ); |
---|
1185 | } |
---|
1186 | else if (!strcmp (action_name, "new-torrent")) |
---|
1187 | { |
---|
1188 | GtkWidget * w = make_meta_ui( GTK_WINDOW( data->wind ), |
---|
1189 | tr_core_handle( data->core ) ); |
---|
1190 | gtk_widget_show_all( w ); |
---|
1191 | } |
---|
1192 | else if( !strcmp( action_name, "remove-torrent" ) ) |
---|
1193 | { |
---|
1194 | removeSelected( data, FALSE ); |
---|
1195 | } |
---|
1196 | else if( !strcmp( action_name, "delete-torrent" ) ) |
---|
1197 | { |
---|
1198 | removeSelected( data, TRUE ); |
---|
1199 | } |
---|
1200 | else if (!strcmp (action_name, "close")) |
---|
1201 | { |
---|
1202 | if( data->wind != NULL ) |
---|
1203 | winclose( NULL, NULL, data ); |
---|
1204 | } |
---|
1205 | else if (!strcmp (action_name, "quit")) |
---|
1206 | { |
---|
1207 | askquit( data->core, data->wind, wannaquit, data ); |
---|
1208 | } |
---|
1209 | else if (!strcmp (action_name, "select-all")) |
---|
1210 | { |
---|
1211 | GtkTreeSelection * s = tr_window_get_selection(data->wind); |
---|
1212 | gtk_tree_selection_select_all( s ); |
---|
1213 | } |
---|
1214 | else if (!strcmp (action_name, "deselect-all")) |
---|
1215 | { |
---|
1216 | GtkTreeSelection * s = tr_window_get_selection(data->wind); |
---|
1217 | gtk_tree_selection_unselect_all( s ); |
---|
1218 | } |
---|
1219 | else if (!strcmp (action_name, "edit-preferences")) |
---|
1220 | { |
---|
1221 | if( NULL == data->prefs ) |
---|
1222 | { |
---|
1223 | data->prefs = tr_prefs_dialog_new( G_OBJECT(data->core), data->wind ); |
---|
1224 | g_signal_connect( data->prefs, "destroy", |
---|
1225 | G_CALLBACK( gtk_widget_destroyed ), &data->prefs ); |
---|
1226 | gtk_widget_show( GTK_WIDGET( data->prefs ) ); |
---|
1227 | } |
---|
1228 | } |
---|
1229 | else if (!strcmp (action_name, "toggle-message-log")) |
---|
1230 | { |
---|
1231 | if( !data->msgwin ) |
---|
1232 | { |
---|
1233 | GtkWidget * win = msgwin_new( data->core ); |
---|
1234 | g_signal_connect( win, "destroy", G_CALLBACK( msgwinclosed ), |
---|
1235 | NULL ); |
---|
1236 | data->msgwin = win; |
---|
1237 | } |
---|
1238 | else |
---|
1239 | { |
---|
1240 | action_toggle("toggle-message-log", FALSE); |
---|
1241 | gtk_widget_destroy( data->msgwin ); |
---|
1242 | data->msgwin = NULL; |
---|
1243 | } |
---|
1244 | } |
---|
1245 | else if (!strcmp (action_name, "show-about-dialog")) |
---|
1246 | { |
---|
1247 | about( data->wind ); |
---|
1248 | } |
---|
1249 | else if (!strcmp (action_name, "help")) |
---|
1250 | { |
---|
1251 | char * url = gtr_get_help_url( ); |
---|
1252 | gtr_open_file( url ); |
---|
1253 | g_free( url ); |
---|
1254 | } |
---|
1255 | else if (!strcmp (action_name, "toggle-main-window")) |
---|
1256 | { |
---|
1257 | toggleMainWindow( data ); |
---|
1258 | } |
---|
1259 | else g_error ("Unhandled action: %s", action_name ); |
---|
1260 | |
---|
1261 | if( changed ) |
---|
1262 | updatemodel( data ); |
---|
1263 | } |
---|