source: trunk/gtk/main.c @ 5996

Last change on this file since 5996 was 5996, checked in by charles, 14 years ago

make the `new torrent' dialog a little prettier

  • Property svn:keywords set to Date Rev Author Id
File size: 39.4 KB
Line 
1/******************************************************************************
2 * $Id: main.c 5996 2008-06-02 15:07:26Z 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
74static 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
105struct 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
123static GtkUIManager * myUIManager = NULL;
124
125static void
126appsetup( TrWindow * wind, GSList * args,
127          struct cbdata *,
128          gboolean paused, gboolean minimized );
129static void
130winsetup( struct cbdata * cbdata, TrWindow * wind );
131static void
132wannaquit( void * vdata );
133static void
134setupdrag(GtkWidget *widget, struct cbdata *data);
135static void
136gotdrag(GtkWidget *widget, GdkDragContext *dc, gint x, gint y,
137        GtkSelectionData *sel, guint info, guint time, gpointer gdata);
138
139static void
140coreerr( TrCore * core, enum tr_core_err code, const char * msg,
141         gpointer gdata );
142static void
143onAddTorrent( TrCore *, tr_ctor *, gpointer );
144static void
145prefschanged( TrCore * core, const char * key, gpointer data );
146static gboolean
147updatemodel(gpointer gdata);
148
149struct counts_data
150{
151    int totalCount;
152    int activeCount;
153    int inactiveCount;
154};
155
156static void
157accumulateStatusForeach( 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
175static void
176accumulateCanUpdateForeach (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
186static void
187refreshTorrentActions( 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
217static void
218selectionChangedCB( GtkTreeSelection * s, gpointer unused UNUSED )
219{
220    refreshTorrentActions( s );
221}
222
223static void
224onMainWindowSizeAllocated( 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
243static void
244windowStateChanged( 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
253static sig_atomic_t global_sigcount = 0;
254
255static void
256fatalsig( 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
268static void
269setupsighandlers( 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
282struct rpc_data
283{
284    int type;
285    int torrentId;
286    tr_torrent * tor;
287    struct cbdata * cbdata;
288};
289
290static int
291onRPCIdle( 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
317static void
318onRPCChanged( 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
334int
335main( 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
455static void
456appsetup( 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 */
517static gboolean
518idle_hide_mainwindow( gpointer window )
519{
520    gtk_widget_hide( window );
521    return FALSE;
522}
523static void
524hideMainWindow( 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
553static void
554clearTag( guint * tag )
555{
556    if( *tag )
557        g_source_remove( *tag );
558    *tag = 0;
559}
560
561static void
562toggleMainWindow( 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
589static gboolean
590winclose( 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
602static void
603rowChangedCB( 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
612static void
613winsetup( 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
632static gpointer
633quitThreadFunc( 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
661static void
662do_exit_cb( GtkWidget *w UNUSED, gpointer data UNUSED )
663{
664    exit( 0 );
665}
666
667static void
668wannaquit( 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 = tr_button_new_from_stock( GTK_STOCK_QUIT, _( "_Quit Now" ) );
703    g_signal_connect(w, "clicked", G_CALLBACK(do_exit_cb), NULL);
704    gtk_container_add(GTK_CONTAINER(b), w);
705    gtk_table_attach(GTK_TABLE(p), b, 1, 2, 2, 3, GTK_FILL, GTK_FILL, 0, 10 );
706
707    gtk_widget_show_all(r);
708
709    /* clear the UI */
710    gtk_list_store_clear( GTK_LIST_STORE( tr_core_model( cbdata->core ) ) );
711
712    /* shut down libT */
713    g_thread_create( quitThreadFunc, vdata, TRUE, NULL );
714}
715
716static void
717gotdrag( GtkWidget         * widget UNUSED,
718         GdkDragContext    * dc,
719         gint                x UNUSED,
720         gint                y UNUSED,
721         GtkSelectionData  * sel,
722         guint               info UNUSED,
723         guint               time,
724         gpointer            gdata )
725{
726    struct cbdata * data = gdata;
727    GSList * paths = NULL;
728    GSList * freeme = NULL;
729
730#if 0
731    int i;
732    char *sele = gdk_atom_name(sel->selection);
733    char *targ = gdk_atom_name(sel->target);
734    char *type = gdk_atom_name(sel->type);
735
736    g_message( "dropped file: sel=%s targ=%s type=%s fmt=%i len=%i",
737               sele, targ, type, sel->format, sel->length );
738    g_free(sele);
739    g_free(targ);
740    g_free(type);
741    if( sel->format == 8 ) {
742        for( i=0; i<sel->length; ++i )
743            fprintf(stderr, "%02X ", sel->data[i]);
744        fprintf(stderr, "\n");
745    }
746#endif
747
748    if( ( sel->format == 8 ) &&
749        ( sel->selection == gdk_atom_intern( "XdndSelection", FALSE ) ) )
750    {
751        int i;
752        char * str = g_strndup( (char*)sel->data, sel->length );
753        gchar ** files = g_strsplit_set( str, "\r\n", -1 );
754        for( i=0; files && files[i]; ++i )
755        {
756            char * filename;
757            if( !*files[i] ) /* empty filename... */
758                continue;
759
760            /* decode the filename */
761            filename = decode_uri( files[i] );
762            freeme = g_slist_prepend( freeme, filename );
763            if( !g_utf8_validate( filename, -1, NULL ) )
764                continue;
765
766            /* walk past "file://", if present */
767            if( g_str_has_prefix( filename, "file:" ) ) {
768                filename += 5;
769                while( g_str_has_prefix( filename, "//" ) )
770                    ++filename;
771            }
772
773            /* if the file doesn't exist, the first part
774               might be a hostname ... walk past it. */
775            if( !g_file_test( filename, G_FILE_TEST_EXISTS ) ) {
776                char * pch = strchr( filename + 1, '/' );
777                if( pch != NULL )
778                    filename = pch;
779            }
780
781            /* finally, add it to the list of torrents to try adding */
782            if( g_file_test( filename, G_FILE_TEST_EXISTS ) )
783                paths = g_slist_prepend( paths, g_strdup( filename ) );
784        }
785
786        /* try to add any torrents we found */
787        if( paths )
788        {
789            paths = g_slist_reverse( paths );
790            tr_core_add_list_defaults( data->core, paths );
791            tr_core_torrents_added( data->core );
792        }
793
794        freestrlist( freeme );
795        g_strfreev( files );
796        g_free( str );
797    }
798
799    gtk_drag_finish(dc, (NULL != paths), FALSE, time);
800}
801
802static void
803setupdrag(GtkWidget *widget, struct cbdata *data) {
804  GtkTargetEntry targets[] = {
805    { "STRING",     0, 0 },
806    { "text/plain", 0, 0 },
807    { "text/uri-list", 0, 0 },
808  };
809
810  g_signal_connect(widget, "drag_data_received", G_CALLBACK(gotdrag), data);
811
812  gtk_drag_dest_set(widget, GTK_DEST_DEFAULT_ALL, targets,
813                    ALEN(targets), GDK_ACTION_COPY | GDK_ACTION_MOVE);
814}
815
816static void
817coreerr( TrCore * core UNUSED, enum tr_core_err code, const char * msg,
818         gpointer gdata )
819{
820    struct cbdata * cbdata = gdata;
821    char          * joined;
822
823    switch( code )
824    {
825        case TR_CORE_ERR_ADD_TORRENT:
826            cbdata->errqueue = g_slist_append( cbdata->errqueue,
827                                               g_strdup( msg ) );
828            return;
829        case TR_CORE_ERR_NO_MORE_TORRENTS:
830            if( cbdata->errqueue )
831            {
832                joined = joinstrlist( cbdata->errqueue, "\n" );
833                errmsg( cbdata->wind,
834                        ngettext( "Failed to load torrent file: %s",
835                                  "Failed to load torrent files: %s",
836                                  g_slist_length( cbdata->errqueue ) ),
837                        joined );
838                g_slist_foreach( cbdata->errqueue, (GFunc) g_free, NULL );
839                g_slist_free( cbdata->errqueue );
840                cbdata->errqueue = NULL;
841                g_free( joined );
842            }
843            return;
844        case TR_CORE_ERR_SAVE_STATE:
845            errmsg( cbdata->wind, "%s", msg );
846            return;
847    }
848
849    g_assert_not_reached();
850}
851
852#if GTK_CHECK_VERSION(2,8,0)
853static void
854on_main_window_focus_in( GtkWidget      * widget UNUSED,
855                         GdkEventFocus  * event UNUSED,
856                         gpointer         gdata )
857{
858    struct cbdata * cbdata = gdata;
859    gtk_window_set_urgency_hint( GTK_WINDOW( cbdata->wind ), FALSE );
860}
861#endif
862
863static void
864onAddTorrent( TrCore * core, tr_ctor * ctor, gpointer gdata )
865{
866    struct cbdata * cbdata = gdata;
867    GtkWidget * w = addSingleTorrentDialog( cbdata->wind, core, ctor );
868#if GTK_CHECK_VERSION(2,8,0)
869    g_signal_connect( w, "focus-in-event", G_CALLBACK(on_main_window_focus_in),  cbdata );
870    gtk_window_set_urgency_hint( cbdata->wind, TRUE );
871#endif
872}
873
874static void
875prefschanged( TrCore * core UNUSED, const char * key, gpointer data )
876{
877    struct cbdata * cbdata = data;
878    tr_handle     * tr     = tr_core_handle( cbdata->core );
879
880    if( !strcmp( key, PREF_KEY_ENCRYPTED_ONLY ) )
881    {
882        const gboolean crypto_only = pref_flag_get( key );
883        tr_sessionSetEncryption( tr, crypto_only ? TR_ENCRYPTION_REQUIRED
884                                                 : TR_ENCRYPTION_PREFERRED );
885    }
886    else if( !strcmp( key, PREF_KEY_PORT ) )
887    {
888        const int port = pref_int_get( key );
889        tr_sessionSetPeerPort( tr, port );
890    }
891    else if( !strcmp( key, PREF_KEY_TRAY_ICON_ENABLED ) )
892    {
893        const int show = pref_flag_get( key );
894        action_sensitize ( "close", show );
895        if( show && !cbdata->icon )
896            cbdata->icon = tr_icon_new( cbdata->core );
897        else if( !show && cbdata->icon ) {
898            g_object_unref( cbdata->icon );
899            cbdata->icon = NULL;
900        }
901    }
902    else if( !strcmp( key, PREF_KEY_DL_LIMIT_ENABLED ) )
903    {
904        const gboolean b = pref_flag_get( key );
905        tr_sessionSetSpeedLimitEnabled( tr, TR_DOWN, b );
906    }
907    else if( !strcmp( key, PREF_KEY_DL_LIMIT ) )
908    {
909        const int limit = pref_int_get( key );
910        tr_sessionSetSpeedLimit( tr, TR_DOWN, limit );
911    }
912    else if( !strcmp( key, PREF_KEY_UL_LIMIT_ENABLED ) )
913    {
914        const gboolean b = pref_flag_get( key );
915        tr_sessionSetSpeedLimitEnabled( tr, TR_UP, b );
916    }
917    else if( !strcmp( key, PREF_KEY_UL_LIMIT ) )
918    {
919        const int limit = pref_int_get( key );
920        tr_sessionSetSpeedLimit( tr, TR_UP, limit );
921    }
922    else if( !strcmp( key, PREF_KEY_NAT ) )
923    {
924        const gboolean enabled = pref_flag_get( key );
925        tr_sessionSetPortForwardingEnabled( tr, enabled );
926    }
927    else if( !strcmp( key, PREF_KEY_PEX ) )
928    {
929        const gboolean b = pref_flag_get( key );
930        tr_sessionSetPortForwardingEnabled( tr, b );
931    }
932    else if( !strcmp( key, PREF_KEY_RPC_ENABLED ) )
933    {
934        tr_sessionSetRPCEnabled( tr, pref_flag_get( key ) );
935    }
936    else if( !strcmp( key, PREF_KEY_RPC_PORT ) )
937    {
938        tr_sessionSetRPCPort( tr, pref_int_get( key ) );
939    }
940    else if( !strcmp( key, PREF_KEY_RPC_ENABLED ) )
941    {
942        tr_sessionSetRPCEnabled( tr, pref_flag_get( key ) );
943    }
944}
945
946gboolean
947updatemodel(gpointer gdata) {
948  struct cbdata *data = gdata;
949
950  if( !data->closing && 0 < global_sigcount )
951  {
952      wannaquit( data );
953      return FALSE;
954  }
955
956  /* update the torrent data in the model */
957  tr_core_update( data->core );
958
959  /* update the main window's statusbar and toolbar buttons */
960  if( data->wind )
961      tr_window_update( data->wind );
962
963  return TRUE;
964}
965
966static void
967aboutDialogActivateLink( GtkAboutDialog * dialog UNUSED,
968                         const gchar    * link_,
969                         gpointer         user_data UNUSED )
970{
971    gtr_open_file( link_ );
972}
973
974static void
975about ( GtkWindow * parent )
976{
977    const char *authors[] =
978    {
979        "Charles Kerr (Backend; GTK+)",
980        "Mitchell Livingston (Backend; OS X)",
981        "Eric Petit (Backend; OS X)",
982        "Josh Elsasser (Daemon; Backend; GTK+)",
983        "Bryan Varner (BeOS)", 
984        NULL
985    };
986
987    gtk_about_dialog_set_url_hook( aboutDialogActivateLink, NULL, NULL );
988
989    gtk_show_about_dialog( parent,
990        "name", g_get_application_name(),
991        "comments", _("A fast and easy BitTorrent client"),
992        "version", LONG_VERSION_STRING,
993        "website", "http://www.transmissionbt.com/",
994        "copyright",_("Copyright 2005-2008 The Transmission Project"),
995        "logo-icon-name", "transmission",
996#ifdef SHOW_LICENSE
997        "license", LICENSE,
998        "wrap-license", TRUE,
999#endif
1000        "authors", authors,
1001        /* Translators: translate "translator-credits" as your name
1002           to have it appear in the credits in the "About" dialog */
1003        "translator-credits", _("translator-credits"),
1004        NULL );
1005}
1006
1007static void
1008startTorrentForeach (GtkTreeModel * model,
1009                     GtkTreePath  * path UNUSED,
1010                     GtkTreeIter  * iter,
1011                     gpointer       data UNUSED)
1012{
1013    tr_torrent * tor = NULL;
1014    gtk_tree_model_get( model, iter, MC_TORRENT_RAW, &tor, -1 );
1015    tr_torrentStart( tor );
1016}
1017
1018static void
1019stopTorrentForeach (GtkTreeModel * model,
1020                    GtkTreePath  * path UNUSED,
1021                    GtkTreeIter  * iter,
1022                    gpointer       data UNUSED)
1023{
1024    tr_torrent * tor = NULL;
1025    gtk_tree_model_get( model, iter, MC_TORRENT_RAW, &tor, -1 );
1026    tr_torrentStop( tor );
1027}
1028
1029static void
1030updateTrackerForeach (GtkTreeModel * model,
1031                      GtkTreePath  * path UNUSED,
1032                      GtkTreeIter  * iter,
1033                      gpointer       data UNUSED)
1034{
1035    tr_torrent * tor = NULL;
1036    gtk_tree_model_get( model, iter, MC_TORRENT_RAW, &tor, -1 );
1037    tr_torrentManualUpdate( tor );
1038}
1039
1040static void
1041detailsClosed( gpointer user_data, GObject * details )
1042{
1043    struct cbdata * data = user_data;
1044    gpointer hashString = g_hash_table_lookup( data->details2tor, details );
1045    g_hash_table_remove( data->details2tor, details );
1046    g_hash_table_remove( data->tor2details, hashString );
1047}
1048
1049static void
1050openFolderForeach( GtkTreeModel * model,
1051                   GtkTreePath  * path UNUSED,
1052                   GtkTreeIter  * iter,
1053                   gpointer       user_data UNUSED )
1054{
1055    TrTorrent * gtor = NULL;
1056    gtk_tree_model_get( model, iter, MC_TORRENT, &gtor, -1 );
1057    tr_torrent_open_folder( gtor );
1058    g_object_unref( G_OBJECT( gtor ) );
1059}
1060
1061static void
1062showInfoForeach (GtkTreeModel * model,
1063                 GtkTreePath  * path UNUSED,
1064                 GtkTreeIter  * iter,
1065                 gpointer       user_data )
1066{
1067    const char * hashString;
1068    struct cbdata * data = user_data;
1069    TrTorrent * tor = NULL;
1070    GtkWidget * w;
1071
1072    gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 );
1073    hashString = tr_torrent_info(tor)->hashString;
1074    w = g_hash_table_lookup( data->tor2details, hashString );
1075    if( w != NULL )
1076        gtk_window_present( GTK_WINDOW( w ) );
1077    else {
1078        w = torrent_inspector_new( GTK_WINDOW( data->wind ), tor );
1079        gtk_widget_show( w );
1080        g_hash_table_insert( data->tor2details, (gpointer)hashString, w );
1081        g_hash_table_insert( data->details2tor, w, (gpointer)hashString );
1082        g_object_weak_ref( G_OBJECT( w ), detailsClosed, data );
1083    }
1084
1085    g_object_unref( G_OBJECT( tor ) );
1086}
1087
1088static void
1089recheckTorrentForeach (GtkTreeModel * model,
1090                       GtkTreePath  * path UNUSED,
1091                       GtkTreeIter  * iter,
1092                       gpointer       data UNUSED)
1093{
1094    TrTorrent * gtor = NULL;
1095    gtk_tree_model_get( model, iter, MC_TORRENT, &gtor, -1 );
1096    tr_torrentVerify( tr_torrent_handle( gtor ) );
1097    g_object_unref( G_OBJECT( gtor ) );
1098}
1099
1100static gboolean
1101msgwinclosed( void )
1102{
1103  action_toggle( "toggle-message-log", FALSE );
1104  return FALSE;
1105}
1106
1107static void
1108accumulateSelectedTorrents( GtkTreeModel * model,
1109                            GtkTreePath  * path UNUSED,
1110                            GtkTreeIter  * iter,
1111                            gpointer       gdata )
1112{
1113    GSList ** data = ( GSList** ) gdata;
1114    TrTorrent * tor = NULL;
1115    gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 );
1116    *data = g_slist_prepend( *data, tor );
1117}
1118
1119static void
1120removeSelected( struct cbdata * data, gboolean delete_files )
1121{
1122    GSList * l = NULL;
1123    GtkTreeSelection * s = tr_window_get_selection( data->wind );
1124    gtk_tree_selection_selected_foreach( s, accumulateSelectedTorrents, &l );
1125    gtk_tree_selection_unselect_all( s );
1126    if( l ) {
1127        l = g_slist_reverse( l );
1128        confirmRemove( data->wind, data->core, l, delete_files );
1129    }
1130}
1131
1132void
1133doAction ( const char * action_name, gpointer user_data )
1134{
1135    struct cbdata * data = user_data;
1136    gboolean changed = FALSE;
1137
1138    if ( !strcmp (action_name, "add-torrent-menu") ||
1139         !strcmp( action_name, "add-torrent-toolbar" ))
1140    {
1141        addDialog( data->wind, data->core );
1142    }
1143    else if (!strcmp (action_name, "show-stats"))
1144    {
1145        GtkWidget * dialog = stats_dialog_create( data->wind,
1146                                                  data->core );
1147        gtk_widget_show( dialog );
1148    }
1149    else if (!strcmp (action_name, "start-torrent"))
1150    {
1151        GtkTreeSelection * s = tr_window_get_selection(data->wind);
1152        gtk_tree_selection_selected_foreach( s, startTorrentForeach, NULL );
1153        changed |= gtk_tree_selection_count_selected_rows( s ) != 0;
1154    }
1155    else if (!strcmp (action_name, "pause-torrent"))
1156    {
1157        GtkTreeSelection * s = tr_window_get_selection(data->wind);
1158        gtk_tree_selection_selected_foreach( s, stopTorrentForeach, NULL );
1159        changed |= gtk_tree_selection_count_selected_rows( s ) != 0;
1160    }
1161    else if (!strcmp (action_name, "verify-torrent"))
1162    {
1163        GtkTreeSelection * s = tr_window_get_selection(data->wind);
1164        gtk_tree_selection_selected_foreach( s, recheckTorrentForeach, NULL );
1165        changed |= gtk_tree_selection_count_selected_rows( s ) != 0;
1166    }
1167    else if (!strcmp (action_name, "open-torrent-folder"))
1168    {
1169        GtkTreeSelection * s = tr_window_get_selection(data->wind);
1170        gtk_tree_selection_selected_foreach( s, openFolderForeach, data );
1171    }
1172    else if (!strcmp (action_name, "show-torrent-details"))
1173    {
1174        GtkTreeSelection * s = tr_window_get_selection(data->wind);
1175        gtk_tree_selection_selected_foreach( s, showInfoForeach, data );
1176    }
1177    else if (!strcmp( action_name, "update-tracker"))
1178    {
1179        GtkTreeSelection * s = tr_window_get_selection(data->wind);
1180        gtk_tree_selection_selected_foreach( s, updateTrackerForeach, data->wind );
1181    }
1182    else if (!strcmp (action_name, "new-torrent"))
1183    {
1184        GtkWidget * w = make_meta_ui( GTK_WINDOW( data->wind ),
1185                                      tr_core_handle( data->core ) );
1186        gtk_widget_show_all( w );
1187    }
1188    else if( !strcmp( action_name, "remove-torrent" ) )
1189    {
1190        removeSelected( data, FALSE );
1191    }
1192    else if( !strcmp( action_name, "delete-torrent" ) )
1193    {
1194        removeSelected( data, TRUE );
1195    }
1196    else if (!strcmp (action_name, "close"))
1197    {
1198        if( data->wind != NULL )
1199            winclose( NULL, NULL, data );
1200    }
1201    else if (!strcmp (action_name, "quit"))
1202    {
1203        askquit( data->core, data->wind, wannaquit, data );
1204    }
1205    else if (!strcmp (action_name, "select-all"))
1206    {
1207        GtkTreeSelection * s = tr_window_get_selection(data->wind);
1208        gtk_tree_selection_select_all( s );
1209    }
1210    else if (!strcmp (action_name, "deselect-all"))
1211    {
1212        GtkTreeSelection * s = tr_window_get_selection(data->wind);
1213        gtk_tree_selection_unselect_all( s );
1214    }
1215    else if (!strcmp (action_name, "edit-preferences"))
1216    {
1217        if( NULL == data->prefs )
1218        {
1219            data->prefs = tr_prefs_dialog_new( G_OBJECT(data->core), data->wind );
1220            g_signal_connect( data->prefs, "destroy",
1221                             G_CALLBACK( gtk_widget_destroyed ), &data->prefs );
1222            gtk_widget_show( GTK_WIDGET( data->prefs ) );
1223        }
1224    }
1225    else if (!strcmp (action_name, "toggle-message-log"))
1226    {
1227        if( !data->msgwin )
1228        {
1229            GtkWidget * win = msgwin_new( data->core );
1230            g_signal_connect( win, "destroy", G_CALLBACK( msgwinclosed ), 
1231                             NULL );
1232            data->msgwin = win;
1233        }
1234        else
1235        {
1236            action_toggle("toggle-message-log", FALSE);
1237            gtk_widget_destroy( data->msgwin );
1238            data->msgwin = NULL;
1239        }
1240    }
1241    else if (!strcmp (action_name, "show-about-dialog"))
1242    {
1243        about( data->wind );
1244    }
1245    else if (!strcmp (action_name, "help"))
1246    {
1247        char * url = gtr_get_help_url( );
1248        gtr_open_file( url );
1249        g_free( url );
1250    }
1251    else if (!strcmp (action_name, "toggle-main-window"))
1252    {
1253        toggleMainWindow( data );
1254    }
1255    else g_error ("Unhandled action: %s", action_name );
1256
1257    if( changed )
1258        updatemodel( data );
1259}
Note: See TracBrowser for help on using the repository browser.