source: trunk/gtk/main.c @ 3206

Last change on this file since 3206 was 3206, checked in by charles, 15 years ago

preferences code refresh in the gtk+ client

  • Property svn:keywords set to Date Rev Author Id
File size: 33.1 KB
Line 
1/******************************************************************************
2 * $Id: main.c 3206 2007-09-27 20:57:58Z charles $
3 *
4 * Copyright (c) 2005-2007 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 <errno.h>
27#include <getopt.h>
28#include <signal.h>
29#include <string.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <time.h>
33#include <unistd.h>
34
35#include <gtk/gtk.h>
36#include <glib/gi18n.h>
37#include <glib/gstdio.h>
38
39#include "actions.h"
40#include "conf.h"
41#include "dialogs.h"
42#include "ipc.h"
43#include "makemeta-ui.h"
44#include "msgwin.h"
45#include "torrent-inspector.h"
46#include "tr_cell_renderer_progress.h"
47#include "tr_core.h"
48#include "tr_icon.h"
49#include "tr_prefs.h"
50#include "tr_torrent.h"
51#include "tr_window.h"
52#include "util.h"
53#include "ui.h"
54
55#include <libtransmission/transmission.h>
56#include <libtransmission/version.h>
57
58/* time in seconds to wait for torrents to stop when exiting */
59#define TRACKER_EXIT_TIMEOUT    10
60
61/* interval in milliseconds to update the torrent list display */
62#define UPDATE_INTERVAL         1000
63
64/* interval in milliseconds to check for stopped torrents and update display */
65#define EXIT_CHECK_INTERVAL     500
66
67/* number of fatal signals required to cause an immediate exit */
68#define SIGCOUNT_MAX            3
69
70#if GTK_CHECK_VERSION(2,8,0)
71#define SHOW_LICENSE
72static const char * LICENSE = 
73"The Transmission binaries and most of its source code is distributed "
74"license. "
75"\n\n"
76"Some files are copyrighted by Charles Kerr and are covered by "
77"the GPL version 2.  Works owned by the Transmission project "
78"are granted a special exemption to clause 2(b) so that the bulk "
79"of its code can remain under the MIT license.  This exemption does "
80"not extend to original or derived works not owned by the "
81"Transmission project. "
82"\n\n"
83"Permission is hereby granted, free of charge, to any person obtaining "
84"a copy of this software and associated documentation files (the "
85"'Software'), to deal in the Software without restriction, including "
86"without limitation the rights to use, copy, modify, merge, publish, "
87"distribute, sublicense, and/or sell copies of the Software, and to "
88"permit persons to whom the Software is furnished to do so, subject to "
89"the following conditions: "
90"\n\n"
91"The above copyright notice and this permission notice shall be included "
92"in all copies or substantial portions of the Software. "
93"\n\n"
94"THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, "
95"EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
96"MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. "
97"IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY "
98"CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
99"TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE "
100"SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.";
101#endif
102
103struct cbdata {
104    GtkWindow    * wind;
105    TrCore       * core;
106    GtkWidget    * icon;
107    GtkWidget    * msgwin;
108    GtkWidget    * prefs;
109    guint          timer;
110    gboolean       closing;
111    GList        * errqueue;
112};
113
114struct exitdata {
115    struct cbdata * cbdata;
116    time_t          started;
117    guint           timer;
118};
119
120#define CBDATA_PTR              "callback-data-pointer"
121
122static GtkUIManager * myUIManager = NULL;
123
124static sig_atomic_t global_sigcount = 0;
125
126static GList *
127readargs( int argc, char ** argv, gboolean * sendquit, gboolean * paused );
128static gboolean
129sendremote( GList * files, gboolean sendquit );
130static void
131gtksetup( int * argc, char *** argv, struct cbdata* );
132static void
133appsetup( TrWindow * wind, GList * args,
134          struct cbdata * , gboolean paused );
135static void
136winsetup( struct cbdata * cbdata, TrWindow * wind );
137static void
138makeicon( struct cbdata * cbdata );
139static void
140wannaquit( void * vdata );
141static gboolean
142exitcheck(gpointer gdata);
143static void
144setupdrag(GtkWidget *widget, struct cbdata *data);
145static void
146gotdrag(GtkWidget *widget, GdkDragContext *dc, gint x, gint y,
147        GtkSelectionData *sel, guint info, guint time, gpointer gdata);
148
149static void
150coreerr( TrCore * core, enum tr_core_err code, const char * msg,
151         gpointer gdata );
152static void
153coreprompt( TrCore *, GList *, enum tr_torrent_action, gboolean, gpointer );
154static void
155corepromptdata( TrCore *, uint8_t *, size_t, gboolean, gpointer );
156static void
157readinitialprefs( struct cbdata * cbdata );
158static void
159prefschanged( TrCore * core, const char * key, gpointer data );
160static void
161setpex( tr_torrent * tor, void * arg );
162static gboolean
163updatemodel(gpointer gdata);
164static GList *
165getselection( struct cbdata * cbdata );
166
167static void
168setupsighandlers(void);
169static void
170fatalsig(int sig);
171
172static void
173accumulateStatusForeach (GtkTreeModel * model,
174                         GtkTreePath  * path UNUSED,
175                         GtkTreeIter  * iter,
176                         gpointer       accumulated_status)
177{
178    int status = 0;
179    gtk_tree_model_get( model, iter, MC_STAT, &status, -1 );
180    *(int*)accumulated_status |= status;
181}
182
183static void
184accumulateCanUpdateForeach (GtkTreeModel * model,
185                            GtkTreePath  * path UNUSED,
186                            GtkTreeIter  * iter,
187                            gpointer       accumulated_status)
188{
189    TrTorrent * gtor = NULL;
190    gtk_tree_model_get( model, iter, MC_TORRENT, &gtor, -1 );
191    *(int*)accumulated_status |=
192        tr_torrentCanManualUpdate( tr_torrent_handle( gtor ) );
193    g_object_unref( G_OBJECT( gtor ) );
194}
195
196static void
197refreshTorrentActions( GtkTreeSelection * s )
198{
199    int status = 0;
200    gtk_tree_selection_selected_foreach( s, accumulateStatusForeach, &status );
201    action_sensitize( "stop-torrent", (status & TR_STATUS_ACTIVE) != 0);
202    action_sensitize( "start-torrent", (status & TR_STATUS_INACTIVE) != 0);
203    action_sensitize( "remove-torrent", status != 0);
204    action_sensitize( "recheck-torrent", status != 0);
205    action_sensitize( "show-torrent-inspector", status != 0);
206
207    status = 0;
208    gtk_tree_selection_selected_foreach( s, accumulateCanUpdateForeach, &status );
209    action_sensitize( "update-tracker", status != 0);
210}
211
212static void
213selectionChangedCB( GtkTreeSelection * s, gpointer unused UNUSED )
214{
215    refreshTorrentActions( s );
216}
217
218int
219main( int argc, char ** argv )
220{
221    struct cbdata * cbdata = g_new (struct cbdata, 1);
222    char       * err;
223    benc_val_t * state;
224    GList      * argfiles;
225    gboolean     didinit, didlock, sendquit, startpaused;
226
227    argfiles = readargs( argc, argv, &sendquit, &startpaused );
228    didinit = cf_init( tr_getPrefsDirectory(), NULL );
229    didlock = FALSE;
230    if( didinit )
231    {
232        /* maybe send remote commands, also try cf_lock() */
233        didlock = sendremote( argfiles, sendquit );
234    }
235    setupsighandlers();         /* set up handlers for fatal signals */
236    gtksetup( &argc, &argv, cbdata );   /* set up gtk and gettext */
237
238    if( ( didinit || cf_init( tr_getPrefsDirectory(), &err ) ) &&
239        ( didlock || cf_lock( &err ) ) )
240    {
241        GtkWindow  * mainwind;
242
243        /* create main window now to be a parent to any error dialogs */
244        mainwind = GTK_WINDOW( tr_window_new( myUIManager ) );
245
246        /* try to load prefs and saved state */
247        tr_prefs_init_global( );
248        state = cf_loadstate( &err );
249        if( NULL != err )
250        {
251            errmsg( mainwind, "%s", err );
252            g_free( err );
253        }
254
255        msgwin_loadpref();      /* set message level here before tr_init() */
256        appsetup( mainwind, argfiles, cbdata, startpaused );
257        cf_freestate( state );
258    }
259    else
260    {
261        gtk_widget_show( errmsg_full( NULL, (callbackfunc_t)gtk_main_quit,
262                                      NULL, "%s", err ) );
263        g_free( err );
264    }
265
266    freestrlist(argfiles);
267
268    gtk_main();
269
270    return 0;
271}
272
273GList *
274readargs( int argc, char ** argv, gboolean * sendquit, gboolean * startpaused )
275{
276    struct option opts[] =
277    {
278        { "help",    no_argument, NULL, 'h' },
279        { "paused",  no_argument, NULL, 'p' },
280        { "quit",    no_argument, NULL, 'q' },
281        { "version", no_argument, NULL, 'v' },
282        { NULL, 0, NULL, 0 }
283    };
284    int          opt;
285    const char * name;
286
287    *sendquit    = FALSE;
288    *startpaused = FALSE;
289
290    gtk_parse_args( &argc, &argv );
291    name = g_get_prgname();
292
293    while( 0 <= ( opt = getopt_long( argc, argv, "hpqv", opts, NULL ) ) )
294    {
295        switch( opt )
296        {
297            case 'p':
298                *startpaused = TRUE;
299                break;
300            case 'q':
301                *sendquit = TRUE;
302                break;
303            case 'v':
304            case 'h':
305                printf(
306_("usage: %s [-hpq] [files...]\n"
307  "\n"
308  "Transmission %s http://transmission.m0k.org/\n"
309  "A free, lightweight BitTorrent client with a simple, intuitive interface\n"
310  "\n"
311  "  -h --help    display this message and exit\n"
312  "  -p --paused  start with all torrents paused\n"
313  "  -q --quit    request that the running %s instance quit\n"
314  "\n"
315  "Only one instance of %s may run at one time. Multiple\n"
316  "torrent files may be loaded at startup by adding them to the command\n"
317  "line. If %s is already running, those torrents will be\n"
318  "opened in the running instance.\n"),
319                        name, LONG_VERSION_STRING,
320                        name, name, name );
321                exit(0);
322                break;
323        }
324    }
325
326    argc -= optind;
327    argv += optind;
328
329    return checkfilenames( argc, argv );
330}
331
332static gboolean
333sendremote( GList * files, gboolean sendquit )
334{
335    gboolean didlock;
336
337    didlock = cf_lock( NULL );
338
339    if( NULL != files )
340    {
341        /* send files if there's another instance, otherwise start normally */
342        if( !didlock )
343        {
344            exit( ipc_sendfiles_blocking( files ) ? 0 : 1 );
345        }
346    }
347
348    if( sendquit )
349    {
350        /* either send a quit message or exit if no other instance */
351        if( !didlock )
352        {
353            exit( ipc_sendquit_blocking() ? 0 : 1 );
354        }
355        exit( 0 );
356    }
357
358    return didlock;
359}
360
361static void
362gtksetup( int * argc, char *** argv, struct cbdata * callback_data )
363{
364
365    bindtextdomain( "transmission-gtk", LOCALEDIR );
366    bind_textdomain_codeset( "transmission-gtk", "UTF-8" );
367    textdomain( "transmission-gtk" );
368
369    g_set_application_name( _("Transmission") );
370    gtk_init( argc, argv );
371
372    /* connect up the actions */
373    myUIManager = gtk_ui_manager_new ();
374    actions_init ( myUIManager, callback_data );
375    gtk_ui_manager_add_ui_from_string (myUIManager, fallback_ui_file, -1, NULL);
376    gtk_ui_manager_ensure_update (myUIManager);
377
378    /* tweak some style properties in dialogs to get closer to the GNOME HiG */
379    gtk_rc_parse_string(
380        "style \"transmission-standard\"\n"
381        "{\n"
382        "    GtkDialog::action-area-border  = 6\n"
383        "    GtkDialog::button-spacing      = 12\n"
384        "    GtkDialog::content-area-border = 6\n"
385        "}\n"
386        "widget \"TransmissionDialog\" style \"transmission-standard\"\n" );
387
388    gtk_window_set_default_icon_name ( "transmission-logo" );
389}
390
391static void
392appsetup( TrWindow * wind, GList * args,
393          struct cbdata * cbdata, gboolean paused )
394{
395    enum tr_torrent_action action;
396
397    /* fill out cbdata */
398    cbdata->wind       = NULL;
399    cbdata->core       = tr_core_new();
400    cbdata->icon       = NULL;
401    cbdata->msgwin     = NULL;
402    cbdata->prefs      = NULL;
403    cbdata->timer      = 0;
404    cbdata->closing    = FALSE;
405    cbdata->errqueue   = NULL;
406
407    /* set up core handlers */
408    g_signal_connect( cbdata->core, "error", G_CALLBACK( coreerr ), cbdata );
409    g_signal_connect( cbdata->core, "directory-prompt",
410                      G_CALLBACK( coreprompt ), cbdata );
411    g_signal_connect( cbdata->core, "directory-prompt-data",
412                      G_CALLBACK( corepromptdata ), cbdata );
413    g_signal_connect_swapped( cbdata->core, "quit",
414                              G_CALLBACK( wannaquit ), cbdata );
415    g_signal_connect( cbdata->core, "prefs-changed",
416                      G_CALLBACK( prefschanged ), cbdata );
417
418    /* apply a few prefs */
419    readinitialprefs( cbdata );
420
421    /* add torrents from command-line and saved state */
422    tr_core_load( cbdata->core, paused );
423
424    if( NULL != args )
425    {
426        action = tr_prefs_get_action( PREF_KEY_ADDIPC );
427        tr_core_add_list( cbdata->core, args, action, paused );
428    }
429    tr_core_torrents_added( cbdata->core );
430
431    /* set up the ipc socket */
432    ipc_socket_setup( GTK_WINDOW( wind ), cbdata->core );
433
434    /* set up main window */
435    winsetup( cbdata, wind );
436
437    /* start model update timer */
438    cbdata->timer = g_timeout_add( UPDATE_INTERVAL, updatemodel, cbdata );
439    updatemodel( cbdata );
440
441    /* show the window */
442    gtk_widget_show( GTK_WIDGET(wind) );
443}
444
445static gboolean
446winclose( GtkWidget * widget UNUSED, GdkEvent * event UNUSED, gpointer gdata )
447{
448    struct cbdata * cbdata = (struct cbdata *) gdata;
449
450    if( cbdata->icon != NULL )
451        gtk_widget_hide( GTK_WIDGET( cbdata->wind ) );
452    else
453        askquit( cbdata->core, cbdata->wind, wannaquit, cbdata );
454
455    return TRUE; /* don't propagate event further */
456}
457
458static void
459rowChangedCB( GtkTreeModel  * model UNUSED,
460              GtkTreePath   * path UNUSED,
461              GtkTreeIter   * iter UNUSED,
462              gpointer        sel)
463{
464    refreshTorrentActions( GTK_TREE_SELECTION(sel) );
465}
466
467static void
468winsetup( struct cbdata * cbdata, TrWindow * wind )
469{
470    GtkTreeModel * model;
471    GtkTreeSelection * sel;
472
473    g_assert( NULL == cbdata->wind );
474    cbdata->wind = GTK_WINDOW( wind );
475
476    sel = tr_window_get_selection( cbdata->wind );
477    g_signal_connect( sel, "changed", G_CALLBACK(selectionChangedCB), NULL );
478    selectionChangedCB( sel, NULL );
479    model = tr_core_model( cbdata->core );
480    gtk_tree_view_set_model ( gtk_tree_selection_get_tree_view(sel), model );
481    g_signal_connect( model, "row-changed", G_CALLBACK(rowChangedCB), sel );
482    g_signal_connect( wind, "delete-event", G_CALLBACK( winclose ), cbdata );
483   
484    setupdrag( GTK_WIDGET(wind), cbdata );
485}
486
487static void
488makeicon( struct cbdata * cbdata )
489{
490    if( NULL == cbdata->icon )
491        cbdata->icon = tr_icon_new( );
492}
493
494static void
495wannaquit( void * vdata )
496{
497  struct cbdata * data;
498  struct exitdata *edata;
499
500  data = vdata;
501  if( data->closing )
502  {
503      return;
504  }
505  data->closing = TRUE;
506
507  /* stop the update timer */
508  if(0 < data->timer)
509    g_source_remove(data->timer);
510  data->timer = 0;
511
512  /* pause torrents and stop nat traversal */
513  tr_core_shutdown( data->core );
514
515  /* set things up to wait for torrents to stop */
516  edata = g_new0(struct exitdata, 1);
517  edata->cbdata = data;
518  edata->started = time(NULL);
519  /* check if torrents are still running */
520  if(exitcheck(edata)) {
521    /* yes, start the exit timer and disable widgets */
522    edata->timer = g_timeout_add(EXIT_CHECK_INTERVAL, exitcheck, edata);
523    if( NULL != data->wind )
524    {
525        gtk_widget_set_sensitive( GTK_WIDGET( data->wind ), FALSE );
526    }
527  }
528}
529
530static gboolean
531exitcheck( gpointer gdata )
532{
533    struct exitdata    * edata;
534    struct cbdata      * cbdata;
535
536    edata  = gdata;
537    cbdata = edata->cbdata;
538
539    /* keep waiting until we're ready to quit or we hit the exit timeout */
540    if( time( NULL ) - edata->started < TRACKER_EXIT_TIMEOUT )
541    {
542        if( !tr_core_quiescent( cbdata->core ) )
543        {
544            updatemodel( cbdata );
545            return TRUE;
546        }
547    }
548
549    /* exit otherwise */
550    if( 0 < edata->timer )
551    {
552        g_source_remove( edata->timer );
553    }
554    g_free( edata );
555    /* note that cbdata->prefs holds a reference to cbdata->core, and
556       it's destruction may trigger callbacks that use cbdata->core */
557    if( NULL != cbdata->prefs )
558    {
559        gtk_widget_destroy( GTK_WIDGET( cbdata->prefs ) );
560    }
561    if( NULL != cbdata->wind )
562    {
563        gtk_widget_destroy( GTK_WIDGET( cbdata->wind ) );
564    }
565    g_object_unref( cbdata->core );
566    if( NULL != cbdata->icon )
567    {
568        g_object_unref( cbdata->icon );
569    }
570    g_assert( 0 == cbdata->timer );
571    if( NULL != cbdata->errqueue )
572    {
573        g_list_foreach( cbdata->errqueue, (GFunc) g_free, NULL );
574        g_list_free( cbdata->errqueue );
575    }
576    g_free( cbdata );
577    gtk_main_quit();
578
579    return FALSE;
580}
581
582static void
583gotdrag(GtkWidget *widget SHUTUP, GdkDragContext *dc, gint x SHUTUP,
584        gint y SHUTUP, GtkSelectionData *sel, guint info SHUTUP, guint time,
585        gpointer gdata) {
586  struct cbdata *data = gdata;
587  char prefix[] = "file:";
588  char *files, *decoded, *deslashed, *hostless;
589  int ii, len;
590  GList *errs;
591  struct stat sb;
592  int prelen = strlen(prefix);
593  GList *paths, *freeables;
594  enum tr_torrent_action action;
595
596#ifdef DND_DEBUG
597  char *sele = gdk_atom_name(sel->selection);
598  char *targ = gdk_atom_name(sel->target);
599  char *type = gdk_atom_name(sel->type);
600
601  fprintf(stderr, "dropped file: sel=%s targ=%s type=%s fmt=%i len=%i\n",
602          sele, targ, type, sel->format, sel->length);
603  g_free(sele);
604  g_free(targ);
605  g_free(type);
606  if(8 == sel->format) {
607    for(ii = 0; ii < sel->length; ii++)
608      fprintf(stderr, "%02X ", sel->data[ii]);
609    fprintf(stderr, "\n");
610  }
611#endif
612
613  errs = NULL;
614  paths = NULL;
615  freeables = NULL;
616  if(gdk_atom_intern("XdndSelection", FALSE) == sel->selection &&
617     8 == sel->format) {
618    /* split file list on carriage returns and linefeeds */
619    files = g_new(char, sel->length + 1);
620    memcpy(files, sel->data, sel->length);
621    files[sel->length] = '\0';
622    for(ii = 0; '\0' != files[ii]; ii++)
623      if('\015' == files[ii] || '\012' == files[ii])
624        files[ii] = '\0';
625
626    /* try to get a usable filename out of the URI supplied and add it */
627    for(ii = 0; ii < sel->length; ii += len + 1) {
628      if('\0' == files[ii])
629        len = 0;
630      else {
631        len = strlen(files + ii);
632        /* de-urlencode the URI */
633        decoded = urldecode(files + ii, len);
634        freeables = g_list_append(freeables, decoded);
635        if(g_utf8_validate(decoded, -1, NULL)) {
636          /* remove the file: prefix */
637          if(prelen < len && 0 == strncmp(prefix, decoded, prelen)) {
638            deslashed = decoded + prelen;
639            /* trim excess / characters from the beginning */
640            while('/' == deslashed[0] && '/' == deslashed[1])
641              deslashed++;
642            /* if the file doesn't exist, the first part might be a hostname */
643            if(0 > g_stat(deslashed, &sb) &&
644               NULL != (hostless = strchr(deslashed + 1, '/')) &&
645               0 == g_stat(hostless, &sb))
646              deslashed = hostless;
647            /* finally, add it to the list of torrents to try adding */
648            paths = g_list_append(paths, deslashed);
649          }
650        }
651      }
652    }
653
654    /* try to add any torrents we found */
655    if( NULL != paths )
656    {
657        action = tr_prefs_get_action( PREF_KEY_ADDSTD );
658        tr_core_add_list( data->core, paths, action, FALSE );
659        tr_core_torrents_added( data->core );
660        g_list_free(paths);
661    }
662    freestrlist(freeables);
663    g_free(files);
664  }
665
666  gtk_drag_finish(dc, (NULL != paths), FALSE, time);
667}
668
669static void
670setupdrag(GtkWidget *widget, struct cbdata *data) {
671  GtkTargetEntry targets[] = {
672    { "STRING",     0, 0 },
673    { "text/plain", 0, 0 },
674    { "text/uri-list", 0, 0 },
675  };
676
677  g_signal_connect(widget, "drag_data_received", G_CALLBACK(gotdrag), data);
678
679  gtk_drag_dest_set(widget, GTK_DEST_DEFAULT_ALL, targets,
680                    ALEN(targets), GDK_ACTION_COPY | GDK_ACTION_MOVE);
681}
682
683static void
684coreerr( TrCore * core SHUTUP, enum tr_core_err code, const char * msg,
685         gpointer gdata )
686{
687    struct cbdata * cbdata = gdata;
688    char          * joined;
689
690    switch( code )
691    {
692        case TR_CORE_ERR_ADD_TORRENT:
693            cbdata->errqueue = g_list_append( cbdata->errqueue,
694                                              g_strdup( msg ) );
695            return;
696        case TR_CORE_ERR_NO_MORE_TORRENTS:
697            if( NULL != cbdata->errqueue )
698            {
699                joined = joinstrlist( cbdata->errqueue, "\n" );
700                errmsg( cbdata->wind,
701                        ngettext( "Failed to load torrent file:\n%s",
702                                  "Failed to load torrent files:\n%s",
703                                  g_list_length( cbdata->errqueue ) ),
704                        joined );
705                g_list_foreach( cbdata->errqueue, (GFunc) g_free, NULL );
706                g_list_free( cbdata->errqueue );
707                cbdata->errqueue = NULL;
708                g_free( joined );
709            }
710            return;
711        case TR_CORE_ERR_SAVE_STATE:
712            errmsg( cbdata->wind, "%s", msg );
713            return;
714    }
715
716    g_assert_not_reached();
717}
718
719void
720coreprompt( TrCore * core, GList * paths, enum tr_torrent_action act,
721            gboolean paused, gpointer gdata )
722{
723    struct cbdata * cbdata = gdata;
724
725    promptfordir( cbdata->wind, core, paths, NULL, 0, act, paused );
726}
727
728void
729corepromptdata( TrCore * core, uint8_t * data, size_t size,
730                gboolean paused, gpointer gdata )
731{
732    struct cbdata * cbdata = gdata;
733
734    promptfordir( cbdata->wind, core, NULL, data, size, TR_TOR_LEAVE, paused );
735}
736
737static void
738readinitialprefs( struct cbdata * cbdata )
739{
740    size_t i;
741    const char * keys[] =
742    {
743        PREF_KEY_PORT,
744        PREF_KEY_DL_LIMIT_ENABLED,
745        PREF_KEY_DL_LIMIT,
746        PREF_KEY_UL_LIMIT_ENABLED,
747        PREF_KEY_UL_LIMIT,
748        PREF_KEY_NAT,
749        PREF_KEY_PEX,
750        PREF_KEY_SYSTRAY
751    };
752
753    for( i=0; i<G_N_ELEMENTS(keys); ++i )
754        prefschanged( NULL, keys[i], cbdata );
755}
756
757static void
758prefschanged( TrCore * core UNUSED, const char * key, gpointer data )
759{
760    struct cbdata * cbdata = data;
761    tr_handle     * tr     = tr_core_handle( cbdata->core );
762
763    if( !strcmp( key, PREF_KEY_PORT ) )
764    {
765        const int port = pref_int_get( key );
766        tr_setBindPort( tr, port );
767    }
768    else if( !strcmp( key, PREF_KEY_DL_LIMIT_ENABLED ) )
769    {
770        const gboolean b = pref_flag_get( key );
771        tr_setUseGlobalSpeedLimit( tr, TR_DOWN, b );
772    }
773    else if( !strcmp( key, PREF_KEY_DL_LIMIT ) )
774    {
775        const int limit = pref_int_get( key );
776        tr_setGlobalSpeedLimit( tr, TR_DOWN, limit );
777    }
778    else if( !strcmp( key, PREF_KEY_UL_LIMIT_ENABLED ) )
779    {
780        const gboolean b = pref_flag_get( key );
781        tr_setUseGlobalSpeedLimit( tr, TR_UP, b );
782    }
783    else if( !strcmp( key, PREF_KEY_UL_LIMIT ) )
784    {
785        const int limit = pref_int_get( key );
786        tr_setGlobalSpeedLimit( tr, TR_UP, limit );
787    }
788    else if( !strcmp( key, PREF_KEY_NAT ) )
789    {
790        const gboolean enabled = pref_flag_get( key );
791        tr_natTraversalEnable( tr, enabled );
792    }
793    else if( !strcmp( key, PREF_KEY_SYSTRAY ) )
794    {
795        if( pref_flag_get( key ) ) {
796            makeicon( cbdata );
797        } else if( cbdata->icon ) {
798            g_object_unref( cbdata->icon );
799            cbdata->icon = NULL;
800        }
801    }
802    else if( !strcmp( key, PREF_KEY_PEX ) )
803    {
804        gboolean enabled = pref_flag_get( key );
805        tr_torrentIterate( tr, setpex, &enabled );
806    }
807}
808
809void
810setpex( tr_torrent * tor, void * arg )
811{
812    gboolean * val;
813
814    val = arg;
815    tr_torrentDisablePex( tor, !(*val) );
816}
817
818gboolean
819updatemodel(gpointer gdata) {
820  struct cbdata *data = gdata;
821  float up, down;
822
823  if( !data->closing && 0 < global_sigcount )
824  {
825      wannaquit( data );
826      return FALSE;
827  }
828
829  /* update the torrent data in the model */
830  tr_core_update( data->core );
831
832  /* update the main window's statusbar and toolbar buttons */
833  if( NULL != data->wind )
834  {
835      tr_torrentRates( tr_core_handle( data->core ), &down, &up );
836      tr_window_update( data->wind, down, up );
837  }
838
839  /* update the message window */
840  msgwin_update();
841
842  return TRUE;
843}
844
845/* returns a GList containing a GtkTreeRowReference to each selected row */
846static GList *
847getselection( struct cbdata * cbdata )
848{
849    GList * rows = NULL;
850
851    if( NULL != cbdata->wind )
852    {
853        GList * ii;
854        GtkTreeSelection *s = tr_window_get_selection(cbdata->wind);
855        GtkTreeModel * model = tr_core_model( cbdata->core );
856        rows = gtk_tree_selection_get_selected_rows( s, NULL );
857        for( ii = rows; NULL != ii; ii = ii->next )
858        {
859            GtkTreeRowReference * ref = gtk_tree_row_reference_new(
860                model, ii->data );
861            gtk_tree_path_free( ii->data );
862            ii->data = ref;
863        }
864    }
865
866    return rows;
867}
868
869static void
870about ( void )
871{
872  GtkWidget * w = gtk_about_dialog_new ();
873  GtkAboutDialog * a = GTK_ABOUT_DIALOG (w);
874  const char *authors[] = { "Eric Petit (Back-end; OS X)",
875                            "Josh Elsasser (Back-end; GTK+)",
876                            "Mitchell Livingston (Back-end; OS X)",
877                            "Charles Kerr (Back-end; GTK+)",
878                            "Bryan Varner (BeOS)", 
879                            NULL };
880  gtk_about_dialog_set_version (a, LONG_VERSION_STRING );
881#ifdef SHOW_LICENSE
882  gtk_about_dialog_set_license (a, LICENSE);
883  gtk_about_dialog_set_wrap_license (a, TRUE);
884#endif
885  gtk_about_dialog_set_logo_icon_name( a, "transmission-logo" );
886  gtk_about_dialog_set_comments( a, _("A GTK+ BitTorrent Client.") );
887  gtk_about_dialog_set_website( a, "http://transmission.m0k.org/" );
888  gtk_about_dialog_set_copyright( a, _("Copyright 2005-2007 The Transmission Project") );
889  gtk_about_dialog_set_authors( a, authors );
890  gtk_about_dialog_set_translator_credits( a, _("translator-credits") );
891  g_signal_connect_swapped( w, "response", G_CALLBACK (gtk_widget_destroy), w );
892  gtk_widget_show_all( w );
893}
894
895static void
896startTorrentForeach (GtkTreeModel * model,
897                     GtkTreePath  * path UNUSED,
898                     GtkTreeIter  * iter,
899                     gpointer       data UNUSED)
900{
901    TrTorrent * tor = NULL;
902    gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 );
903    tr_torrent_start( tor );
904    g_object_unref( G_OBJECT( tor ) );
905}
906
907static void
908stopTorrentForeach (GtkTreeModel * model,
909                    GtkTreePath  * path UNUSED,
910                    GtkTreeIter  * iter,
911                    gpointer       data UNUSED)
912{
913    TrTorrent * tor = NULL;
914    gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 );
915    tr_torrent_stop( tor );
916    g_object_unref( G_OBJECT( tor ) );
917}
918
919static void
920updateTrackerForeach (GtkTreeModel * model,
921                      GtkTreePath  * path UNUSED,
922                      GtkTreeIter  * iter,
923                      gpointer       data UNUSED)
924{
925    TrTorrent * tor = NULL;
926    gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 );
927    tr_manualUpdate( tr_torrent_handle( tor ) );
928    g_object_unref( G_OBJECT( tor ) );
929}
930
931static void
932showInfoForeach (GtkTreeModel * model,
933                 GtkTreePath  * path UNUSED,
934                 GtkTreeIter  * iter,
935                 gpointer       data UNUSED)
936{
937    TrTorrent * tor = NULL;
938    GtkWidget * w;
939    gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 );
940    w = torrent_inspector_new( GTK_WINDOW(data), tor );
941    gtk_widget_show( w );
942    g_object_unref( G_OBJECT( tor ) );
943}
944
945static void
946recheckTorrentForeach (GtkTreeModel * model,
947                       GtkTreePath  * path UNUSED,
948                       GtkTreeIter  * iter,
949                       gpointer       data UNUSED)
950{
951    TrTorrent * gtor = NULL;
952    gtk_tree_model_get( model, iter, MC_TORRENT, &gtor, -1 );
953    tr_torrentRecheck( tr_torrent_handle( gtor ) );
954    g_object_unref( G_OBJECT( gtor ) );
955}
956
957static gboolean
958msgwinclosed()
959{
960  action_toggle( "toggle-debug-window", FALSE );
961  return FALSE;
962}
963
964void
965doAction ( const char * action_name, gpointer user_data )
966{
967    struct cbdata * data = (struct cbdata *) user_data;
968    gboolean changed = FALSE;
969
970    if (!strcmp (action_name, "add-torrent"))
971    {
972        makeaddwind( data->wind, data->core );
973    }
974    else if (!strcmp (action_name, "start-torrent"))
975    {
976        GtkTreeSelection * s = tr_window_get_selection(data->wind);
977        gtk_tree_selection_selected_foreach( s, startTorrentForeach, NULL );
978        changed |= gtk_tree_selection_count_selected_rows( s ) != 0;
979    }
980    else if (!strcmp (action_name, "stop-torrent"))
981    {
982        GtkTreeSelection * s = tr_window_get_selection(data->wind);
983        gtk_tree_selection_selected_foreach( s, stopTorrentForeach, NULL );
984        changed |= gtk_tree_selection_count_selected_rows( s ) != 0;
985    }
986    else if (!strcmp (action_name, "recheck-torrent"))
987    {
988        GtkTreeSelection * s = tr_window_get_selection(data->wind);
989        gtk_tree_selection_selected_foreach( s, recheckTorrentForeach, NULL );
990        changed |= gtk_tree_selection_count_selected_rows( s ) != 0;
991    }
992    else if (!strcmp (action_name, "show-torrent-inspector"))
993    {
994        GtkTreeSelection * s = tr_window_get_selection(data->wind);
995        gtk_tree_selection_selected_foreach( s, showInfoForeach, data->wind );
996    }
997    else if (!strcmp( action_name, "update-tracker"))
998    {
999        GtkTreeSelection * s = tr_window_get_selection(data->wind);
1000        gtk_tree_selection_selected_foreach( s, updateTrackerForeach, data->wind );
1001    }
1002    else if (!strcmp (action_name, "create-torrent"))
1003    {
1004        GtkWidget * w = make_meta_ui( GTK_WINDOW( data->wind ), tr_core_handle( data->core ) );
1005        gtk_widget_show_all( w );
1006    }
1007    else if (!strcmp (action_name, "remove-torrent"))
1008    {
1009        /* this modifies the model's contents, so can't use foreach */
1010        GList *l, *sel = getselection( data );
1011        GtkTreeModel *model = tr_core_model( data->core );
1012        for( l=sel; l!=NULL; l=l->next )
1013        {
1014            GtkTreeIter iter;
1015            GtkTreeRowReference * reference = (GtkTreeRowReference *) l->data;
1016            GtkTreePath * path = gtk_tree_row_reference_get_path( reference );
1017            gtk_tree_model_get_iter( model, &iter, path );
1018            tr_core_delete_torrent( data->core, &iter );
1019            gtk_tree_row_reference_free( reference );
1020            changed = TRUE;
1021        }
1022        g_list_free( sel );
1023    }
1024    else if (!strcmp (action_name, "close"))
1025    {
1026        if( data->wind != NULL )
1027            winclose( NULL, NULL, data );
1028    }
1029    else if (!strcmp (action_name, "quit"))
1030    {
1031        askquit( data->core, data->wind, wannaquit, data );
1032    }
1033    else if (!strcmp (action_name, "select-all"))
1034    {
1035        GtkTreeSelection * s = tr_window_get_selection(data->wind);
1036        gtk_tree_selection_select_all( s );
1037    }
1038    else if (!strcmp (action_name, "unselect-all"))
1039    {
1040        GtkTreeSelection * s = tr_window_get_selection(data->wind);
1041        gtk_tree_selection_unselect_all( s );
1042    }
1043    else if (!strcmp (action_name, "edit-preferences"))
1044    {
1045        if( NULL == data->prefs )
1046        {
1047            data->prefs = tr_prefs_dialog_new( G_OBJECT(data->core), data->wind );
1048            g_signal_connect( data->prefs, "destroy",
1049                             G_CALLBACK( gtk_widget_destroyed ), &data->prefs );
1050            gtk_widget_show( GTK_WIDGET( data->prefs ) );
1051        }
1052    }
1053    else if (!strcmp (action_name, "toggle-debug-window"))
1054    {
1055        if( !data->msgwin )
1056        {
1057            GtkWidget * win = msgwin_create( data->core );
1058            g_signal_connect( win, "destroy", G_CALLBACK( msgwinclosed ), 
1059                             NULL );
1060            data->msgwin = win;
1061        }
1062        else
1063        {
1064            action_toggle("toggle-debug-window", FALSE);
1065            gtk_widget_destroy( data->msgwin );
1066            data->msgwin = NULL;
1067        }
1068    }
1069    else if (!strcmp (action_name, "show-about-dialog"))
1070    {
1071        about();
1072    }
1073    else if (!strcmp (action_name, "toggle-main-window"))
1074    {
1075        GtkWidget * w = GTK_WIDGET (data->wind);
1076        if (GTK_WIDGET_VISIBLE(w))
1077            gtk_widget_hide (w);
1078        else
1079            gtk_window_present (GTK_WINDOW(w));
1080    }
1081    else g_error ("Unhandled action: %s", action_name );
1082
1083    if(changed)
1084    {
1085        updatemodel( data );
1086        tr_core_save( data->core );
1087    }
1088}
1089
1090
1091static void
1092setupsighandlers(void) {
1093  int sigs[] = {SIGHUP, SIGINT, SIGQUIT, SIGTERM};
1094  struct sigaction sa;
1095  int ii;
1096
1097  memset(&sa, 0,  sizeof(sa));
1098  sa.sa_handler = fatalsig;
1099  for(ii = 0; ii < ALEN(sigs); ii++)
1100    sigaction(sigs[ii], &sa, NULL);
1101}
1102
1103static void
1104fatalsig(int sig) {
1105  struct sigaction sa;
1106
1107  if(SIGCOUNT_MAX <= ++global_sigcount) {
1108    memset(&sa, 0,  sizeof(sa));
1109    sa.sa_handler = SIG_DFL;
1110    sigaction(sig, &sa, NULL);
1111    raise(sig);
1112  }
1113}
Note: See TracBrowser for help on using the repository browser.