source: trunk/gtk/main.c @ 3209

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

now that the gtk+ prefs are unfucked, add an "ignore unencrypted peers" preference.

  • Property svn:keywords set to Date Rev Author Id
File size: 33.4 KB
Line 
1/******************************************************************************
2 * $Id: main.c 3209 2007-09-28 00:46:22Z 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        PREF_KEY_ENCRYPTED_ONLY
752    };
753
754    for( i=0; i<G_N_ELEMENTS(keys); ++i )
755        prefschanged( NULL, keys[i], cbdata );
756}
757
758static void
759prefschanged( TrCore * core UNUSED, const char * key, gpointer data )
760{
761    struct cbdata * cbdata = data;
762    tr_handle     * tr     = tr_core_handle( cbdata->core );
763
764    if( !strcmp( key, PREF_KEY_ENCRYPTED_ONLY ) )
765    {
766        const gboolean crypto_only = pref_flag_get( key );
767        tr_setEncryptionMode( tr, crypto_only ? TR_ENCRYPTION_REQUIRED
768                                              : TR_ENCRYPTION_PREFERRED );
769    }
770    else if( !strcmp( key, PREF_KEY_PORT ) )
771    {
772        const int port = pref_int_get( key );
773        tr_setBindPort( tr, port );
774    }
775    else if( !strcmp( key, PREF_KEY_DL_LIMIT_ENABLED ) )
776    {
777        const gboolean b = pref_flag_get( key );
778        tr_setUseGlobalSpeedLimit( tr, TR_DOWN, b );
779    }
780    else if( !strcmp( key, PREF_KEY_DL_LIMIT ) )
781    {
782        const int limit = pref_int_get( key );
783        tr_setGlobalSpeedLimit( tr, TR_DOWN, limit );
784    }
785    else if( !strcmp( key, PREF_KEY_UL_LIMIT_ENABLED ) )
786    {
787        const gboolean b = pref_flag_get( key );
788        tr_setUseGlobalSpeedLimit( tr, TR_UP, b );
789    }
790    else if( !strcmp( key, PREF_KEY_UL_LIMIT ) )
791    {
792        const int limit = pref_int_get( key );
793        tr_setGlobalSpeedLimit( tr, TR_UP, limit );
794    }
795    else if( !strcmp( key, PREF_KEY_NAT ) )
796    {
797        const gboolean enabled = pref_flag_get( key );
798        tr_natTraversalEnable( tr, enabled );
799    }
800    else if( !strcmp( key, PREF_KEY_SYSTRAY ) )
801    {
802        if( pref_flag_get( key ) )
803        {
804            makeicon( cbdata );
805        }
806        else if( cbdata->icon )
807        {
808            g_object_unref( cbdata->icon );
809            cbdata->icon = NULL;
810        }
811    }
812    else if( !strcmp( key, PREF_KEY_PEX ) )
813    {
814        gboolean enabled = pref_flag_get( key );
815        tr_torrentIterate( tr, setpex, &enabled );
816    }
817}
818
819void
820setpex( tr_torrent * tor, void * arg )
821{
822    gboolean * val;
823
824    val = arg;
825    tr_torrentDisablePex( tor, !(*val) );
826}
827
828gboolean
829updatemodel(gpointer gdata) {
830  struct cbdata *data = gdata;
831  float up, down;
832
833  if( !data->closing && 0 < global_sigcount )
834  {
835      wannaquit( data );
836      return FALSE;
837  }
838
839  /* update the torrent data in the model */
840  tr_core_update( data->core );
841
842  /* update the main window's statusbar and toolbar buttons */
843  if( NULL != data->wind )
844  {
845      tr_torrentRates( tr_core_handle( data->core ), &down, &up );
846      tr_window_update( data->wind, down, up );
847  }
848
849  /* update the message window */
850  msgwin_update();
851
852  return TRUE;
853}
854
855/* returns a GList containing a GtkTreeRowReference to each selected row */
856static GList *
857getselection( struct cbdata * cbdata )
858{
859    GList * rows = NULL;
860
861    if( NULL != cbdata->wind )
862    {
863        GList * ii;
864        GtkTreeSelection *s = tr_window_get_selection(cbdata->wind);
865        GtkTreeModel * model = tr_core_model( cbdata->core );
866        rows = gtk_tree_selection_get_selected_rows( s, NULL );
867        for( ii = rows; NULL != ii; ii = ii->next )
868        {
869            GtkTreeRowReference * ref = gtk_tree_row_reference_new(
870                model, ii->data );
871            gtk_tree_path_free( ii->data );
872            ii->data = ref;
873        }
874    }
875
876    return rows;
877}
878
879static void
880about ( void )
881{
882  GtkWidget * w = gtk_about_dialog_new ();
883  GtkAboutDialog * a = GTK_ABOUT_DIALOG (w);
884  const char *authors[] = { "Eric Petit (Back-end; OS X)",
885                            "Josh Elsasser (Back-end; GTK+)",
886                            "Mitchell Livingston (Back-end; OS X)",
887                            "Charles Kerr (Back-end; GTK+)",
888                            "Bryan Varner (BeOS)", 
889                            NULL };
890  gtk_about_dialog_set_version (a, LONG_VERSION_STRING );
891#ifdef SHOW_LICENSE
892  gtk_about_dialog_set_license (a, LICENSE);
893  gtk_about_dialog_set_wrap_license (a, TRUE);
894#endif
895  gtk_about_dialog_set_logo_icon_name( a, "transmission-logo" );
896  gtk_about_dialog_set_comments( a, _("A GTK+ BitTorrent Client.") );
897  gtk_about_dialog_set_website( a, "http://transmission.m0k.org/" );
898  gtk_about_dialog_set_copyright( a, _("Copyright 2005-2007 The Transmission Project") );
899  gtk_about_dialog_set_authors( a, authors );
900  gtk_about_dialog_set_translator_credits( a, _("translator-credits") );
901  g_signal_connect_swapped( w, "response", G_CALLBACK (gtk_widget_destroy), w );
902  gtk_widget_show_all( w );
903}
904
905static void
906startTorrentForeach (GtkTreeModel * model,
907                     GtkTreePath  * path UNUSED,
908                     GtkTreeIter  * iter,
909                     gpointer       data UNUSED)
910{
911    TrTorrent * tor = NULL;
912    gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 );
913    tr_torrent_start( tor );
914    g_object_unref( G_OBJECT( tor ) );
915}
916
917static void
918stopTorrentForeach (GtkTreeModel * model,
919                    GtkTreePath  * path UNUSED,
920                    GtkTreeIter  * iter,
921                    gpointer       data UNUSED)
922{
923    TrTorrent * tor = NULL;
924    gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 );
925    tr_torrent_stop( tor );
926    g_object_unref( G_OBJECT( tor ) );
927}
928
929static void
930updateTrackerForeach (GtkTreeModel * model,
931                      GtkTreePath  * path UNUSED,
932                      GtkTreeIter  * iter,
933                      gpointer       data UNUSED)
934{
935    TrTorrent * tor = NULL;
936    gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 );
937    tr_manualUpdate( tr_torrent_handle( tor ) );
938    g_object_unref( G_OBJECT( tor ) );
939}
940
941static void
942showInfoForeach (GtkTreeModel * model,
943                 GtkTreePath  * path UNUSED,
944                 GtkTreeIter  * iter,
945                 gpointer       data UNUSED)
946{
947    TrTorrent * tor = NULL;
948    GtkWidget * w;
949    gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 );
950    w = torrent_inspector_new( GTK_WINDOW(data), tor );
951    gtk_widget_show( w );
952    g_object_unref( G_OBJECT( tor ) );
953}
954
955static void
956recheckTorrentForeach (GtkTreeModel * model,
957                       GtkTreePath  * path UNUSED,
958                       GtkTreeIter  * iter,
959                       gpointer       data UNUSED)
960{
961    TrTorrent * gtor = NULL;
962    gtk_tree_model_get( model, iter, MC_TORRENT, &gtor, -1 );
963    tr_torrentRecheck( tr_torrent_handle( gtor ) );
964    g_object_unref( G_OBJECT( gtor ) );
965}
966
967static gboolean
968msgwinclosed()
969{
970  action_toggle( "toggle-debug-window", FALSE );
971  return FALSE;
972}
973
974void
975doAction ( const char * action_name, gpointer user_data )
976{
977    struct cbdata * data = (struct cbdata *) user_data;
978    gboolean changed = FALSE;
979
980    if (!strcmp (action_name, "add-torrent"))
981    {
982        makeaddwind( data->wind, data->core );
983    }
984    else if (!strcmp (action_name, "start-torrent"))
985    {
986        GtkTreeSelection * s = tr_window_get_selection(data->wind);
987        gtk_tree_selection_selected_foreach( s, startTorrentForeach, NULL );
988        changed |= gtk_tree_selection_count_selected_rows( s ) != 0;
989    }
990    else if (!strcmp (action_name, "stop-torrent"))
991    {
992        GtkTreeSelection * s = tr_window_get_selection(data->wind);
993        gtk_tree_selection_selected_foreach( s, stopTorrentForeach, NULL );
994        changed |= gtk_tree_selection_count_selected_rows( s ) != 0;
995    }
996    else if (!strcmp (action_name, "recheck-torrent"))
997    {
998        GtkTreeSelection * s = tr_window_get_selection(data->wind);
999        gtk_tree_selection_selected_foreach( s, recheckTorrentForeach, NULL );
1000        changed |= gtk_tree_selection_count_selected_rows( s ) != 0;
1001    }
1002    else if (!strcmp (action_name, "show-torrent-inspector"))
1003    {
1004        GtkTreeSelection * s = tr_window_get_selection(data->wind);
1005        gtk_tree_selection_selected_foreach( s, showInfoForeach, data->wind );
1006    }
1007    else if (!strcmp( action_name, "update-tracker"))
1008    {
1009        GtkTreeSelection * s = tr_window_get_selection(data->wind);
1010        gtk_tree_selection_selected_foreach( s, updateTrackerForeach, data->wind );
1011    }
1012    else if (!strcmp (action_name, "create-torrent"))
1013    {
1014        GtkWidget * w = make_meta_ui( GTK_WINDOW( data->wind ), tr_core_handle( data->core ) );
1015        gtk_widget_show_all( w );
1016    }
1017    else if (!strcmp (action_name, "remove-torrent"))
1018    {
1019        /* this modifies the model's contents, so can't use foreach */
1020        GList *l, *sel = getselection( data );
1021        GtkTreeModel *model = tr_core_model( data->core );
1022        for( l=sel; l!=NULL; l=l->next )
1023        {
1024            GtkTreeIter iter;
1025            GtkTreeRowReference * reference = (GtkTreeRowReference *) l->data;
1026            GtkTreePath * path = gtk_tree_row_reference_get_path( reference );
1027            gtk_tree_model_get_iter( model, &iter, path );
1028            tr_core_delete_torrent( data->core, &iter );
1029            gtk_tree_row_reference_free( reference );
1030            changed = TRUE;
1031        }
1032        g_list_free( sel );
1033    }
1034    else if (!strcmp (action_name, "close"))
1035    {
1036        if( data->wind != NULL )
1037            winclose( NULL, NULL, data );
1038    }
1039    else if (!strcmp (action_name, "quit"))
1040    {
1041        askquit( data->core, data->wind, wannaquit, data );
1042    }
1043    else if (!strcmp (action_name, "select-all"))
1044    {
1045        GtkTreeSelection * s = tr_window_get_selection(data->wind);
1046        gtk_tree_selection_select_all( s );
1047    }
1048    else if (!strcmp (action_name, "unselect-all"))
1049    {
1050        GtkTreeSelection * s = tr_window_get_selection(data->wind);
1051        gtk_tree_selection_unselect_all( s );
1052    }
1053    else if (!strcmp (action_name, "edit-preferences"))
1054    {
1055        if( NULL == data->prefs )
1056        {
1057            data->prefs = tr_prefs_dialog_new( G_OBJECT(data->core), data->wind );
1058            g_signal_connect( data->prefs, "destroy",
1059                             G_CALLBACK( gtk_widget_destroyed ), &data->prefs );
1060            gtk_widget_show( GTK_WIDGET( data->prefs ) );
1061        }
1062    }
1063    else if (!strcmp (action_name, "toggle-debug-window"))
1064    {
1065        if( !data->msgwin )
1066        {
1067            GtkWidget * win = msgwin_create( data->core );
1068            g_signal_connect( win, "destroy", G_CALLBACK( msgwinclosed ), 
1069                             NULL );
1070            data->msgwin = win;
1071        }
1072        else
1073        {
1074            action_toggle("toggle-debug-window", FALSE);
1075            gtk_widget_destroy( data->msgwin );
1076            data->msgwin = NULL;
1077        }
1078    }
1079    else if (!strcmp (action_name, "show-about-dialog"))
1080    {
1081        about();
1082    }
1083    else if (!strcmp (action_name, "toggle-main-window"))
1084    {
1085        GtkWidget * w = GTK_WIDGET (data->wind);
1086        if (GTK_WIDGET_VISIBLE(w))
1087            gtk_widget_hide (w);
1088        else
1089            gtk_window_present (GTK_WINDOW(w));
1090    }
1091    else g_error ("Unhandled action: %s", action_name );
1092
1093    if(changed)
1094    {
1095        updatemodel( data );
1096        tr_core_save( data->core );
1097    }
1098}
1099
1100
1101static void
1102setupsighandlers(void) {
1103  int sigs[] = {SIGHUP, SIGINT, SIGQUIT, SIGTERM};
1104  struct sigaction sa;
1105  int ii;
1106
1107  memset(&sa, 0,  sizeof(sa));
1108  sa.sa_handler = fatalsig;
1109  for(ii = 0; ii < ALEN(sigs); ii++)
1110    sigaction(sigs[ii], &sa, NULL);
1111}
1112
1113static void
1114fatalsig(int sig) {
1115  struct sigaction sa;
1116
1117  if(SIGCOUNT_MAX <= ++global_sigcount) {
1118    memset(&sa, 0,  sizeof(sa));
1119    sa.sa_handler = SIG_DFL;
1120    sigaction(sig, &sa, NULL);
1121    raise(sig);
1122  }
1123}
Note: See TracBrowser for help on using the repository browser.