transmission/gtk/main.c

1036 lines
32 KiB
C
Raw Normal View History

/******************************************************************************
* $Id$
*
* Copyright (c) 2005-2007 Transmission authors and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
2006-07-16 19:39:23 +00:00
#include <sys/param.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include "actions.h"
2006-07-16 19:39:23 +00:00
#include "conf.h"
#include "dialogs.h"
#include "ipc.h"
#include "makemeta-ui.h"
2006-08-20 22:45:29 +00:00
#include "msgwin.h"
2007-11-27 03:29:51 +00:00
#include "stats.h"
#include "torrent-inspector.h"
#include "tr_cell_renderer_progress.h"
#include "tr_core.h"
2007-02-19 22:09:05 +00:00
#include "tr_icon.h"
#include "tr_prefs.h"
#include "tr_torrent.h"
#include "tr_window.h"
2006-07-16 19:39:23 +00:00
#include "util.h"
#include "ui.h"
2006-07-16 19:39:23 +00:00
#include <libtransmission/transmission.h>
#include <libtransmission/version.h>
2007-02-19 22:09:05 +00:00
2006-07-16 19:39:23 +00:00
/* interval in milliseconds to update the torrent list display */
#define UPDATE_INTERVAL 1000
/* interval in milliseconds to check for stopped torrents and update display */
#define EXIT_CHECK_INTERVAL 500
2007-02-19 22:09:05 +00:00
/* number of fatal signals required to cause an immediate exit */
#define SIGCOUNT_MAX 3
2007-06-09 17:08:25 +00:00
#if GTK_CHECK_VERSION(2,8,0)
#define SHOW_LICENSE
static const char * LICENSE =
"The Transmission binaries and most of its source code is distributed "
"license. "
"\n\n"
"Some files are copyrighted by Charles Kerr and are covered by "
"the GPL version 2. Works owned by the Transmission project "
"are granted a special exemption to clause 2(b) so that the bulk "
"of its code can remain under the MIT license. This exemption does "
"not extend to original or derived works not owned by the "
"Transmission project. "
"\n\n"
"Permission is hereby granted, free of charge, to any person obtaining "
"a copy of this software and associated documentation files (the "
"'Software'), to deal in the Software without restriction, including "
"without limitation the rights to use, copy, modify, merge, publish, "
"distribute, sublicense, and/or sell copies of the Software, and to "
"permit persons to whom the Software is furnished to do so, subject to "
"the following conditions: "
"\n\n"
"The above copyright notice and this permission notice shall be included "
"in all copies or substantial portions of the Software. "
"\n\n"
"THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, "
"EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
"MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. "
"IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY "
"CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
"TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE "
"SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.";
2007-06-09 17:08:25 +00:00
#endif
2006-07-16 19:39:23 +00:00
struct cbdata {
2007-02-19 22:09:05 +00:00
GtkWindow * wind;
TrCore * core;
GtkWidget * icon;
GtkWidget * msgwin;
GtkWidget * prefs;
2007-02-19 22:09:05 +00:00
guint timer;
gboolean closing;
GList * errqueue;
2006-07-16 19:39:23 +00:00
};
#define CBDATA_PTR "callback-data-pointer"
static GtkUIManager * myUIManager = NULL;
static sig_atomic_t global_sigcount = 0;
2007-02-19 22:09:05 +00:00
static gboolean
sendremote( GList * files, gboolean sendquit );
static void
appsetup( TrWindow * wind, GList * args,
struct cbdata * , gboolean paused );
static void
2007-02-19 22:09:05 +00:00
winsetup( struct cbdata * cbdata, TrWindow * wind );
static void
2007-02-19 22:09:05 +00:00
makeicon( struct cbdata * cbdata );
static void
wannaquit( void * vdata );
static void
2006-07-16 19:39:23 +00:00
setupdrag(GtkWidget *widget, struct cbdata *data);
static void
2006-07-16 19:39:23 +00:00
gotdrag(GtkWidget *widget, GdkDragContext *dc, gint x, gint y,
GtkSelectionData *sel, guint info, guint time, gpointer gdata);
2007-02-19 22:09:05 +00:00
static void
coreerr( TrCore * core, enum tr_core_err code, const char * msg,
gpointer gdata );
static void
coreprompt( TrCore *, GList *, enum tr_torrent_action, gboolean, gpointer );
static void
2007-05-24 10:37:07 +00:00
corepromptdata( TrCore *, uint8_t *, size_t, gboolean, gpointer );
static void
2007-10-13 03:15:02 +00:00
initializeFromPrefs( struct cbdata * cbdata );
2007-02-19 22:09:05 +00:00
static void
prefschanged( TrCore * core, const char * key, gpointer data );
2007-03-31 19:19:27 +00:00
static void
setpex( tr_torrent * tor, void * arg );
static gboolean
2006-07-16 19:39:23 +00:00
updatemodel(gpointer gdata);
2007-02-19 22:09:05 +00:00
static GList *
getselection( struct cbdata * cbdata );
static void
2006-07-16 19:39:23 +00:00
setupsighandlers(void);
static void
2006-07-16 19:39:23 +00:00
fatalsig(int sig);
struct counts_data
{
int totalCount;
int activeCount;
int inactiveCount;
};
static void
accumulateStatusForeach (GtkTreeModel * model,
GtkTreePath * path UNUSED,
GtkTreeIter * iter,
gpointer user_data )
{
int status = 0;
struct counts_data * counts = user_data;
++counts->totalCount;
gtk_tree_model_get( model, iter, MC_STAT, &status, -1 );
if( TR_STATUS_IS_ACTIVE( status ) )
++counts->activeCount;
else
++counts->inactiveCount;
}
static void
accumulateCanUpdateForeach (GtkTreeModel * model,
GtkTreePath * path UNUSED,
GtkTreeIter * iter,
gpointer accumulated_status)
{
TrTorrent * gtor = NULL;
gtk_tree_model_get( model, iter, MC_TORRENT, &gtor, -1 );
*(int*)accumulated_status |=
tr_torrentCanManualUpdate( tr_torrent_handle( gtor ) );
g_object_unref( G_OBJECT( gtor ) );
}
static void
refreshTorrentActions( GtkTreeSelection * s )
{
int canUpdate;
struct counts_data counts;
counts.activeCount = 0;
counts.inactiveCount = 0;
counts.totalCount = 0;
gtk_tree_selection_selected_foreach( s, accumulateStatusForeach, &counts );
action_sensitize( "pause-torrent", counts.activeCount!=0 );
action_sensitize( "start-torrent", counts.inactiveCount!=0 );
action_sensitize( "remove-torrent", counts.totalCount!=0 );
action_sensitize( "verify-torrent", counts.totalCount!=0 );
action_sensitize( "show-torrent-details", counts.totalCount!=0 );
canUpdate = 0;
gtk_tree_selection_selected_foreach( s, accumulateCanUpdateForeach, &canUpdate );
action_sensitize( "update-tracker", canUpdate!=0 );
}
static void
selectionChangedCB( GtkTreeSelection * s, gpointer unused UNUSED )
{
refreshTorrentActions( s );
}
2006-07-16 19:39:23 +00:00
int
2007-02-19 22:09:05 +00:00
main( int argc, char ** argv )
{
char * err;
struct cbdata * cbdata = g_new (struct cbdata, 1);
GList * argfiles;
2007-10-12 20:50:03 +00:00
gboolean didinit = FALSE;
gboolean didlock = FALSE;
gboolean sendquit = FALSE;
gboolean startpaused = FALSE;
char * domain = "transmission";
GOptionEntry entries[] = {
2007-10-13 03:15:02 +00:00
{ "paused", 'p', 0, G_OPTION_ARG_NONE, &startpaused,
_("Start with all torrents paused"), NULL },
{ "quit", 'q', 0, G_OPTION_ARG_NONE, &sendquit,
_( "Request that the running instance quit"), NULL },
{ NULL, 0, 0, 0, NULL, NULL, NULL }
};
/* bind the gettext domain */
bindtextdomain( domain, TRANSMISSIONLOCALEDIR );
bind_textdomain_codeset( domain, "UTF-8" );
textdomain( domain );
g_set_application_name( _( "Transmission" ) );
2007-02-19 22:09:05 +00:00
/* initialize gtk */
g_thread_init( NULL );
gtk_init_with_args( &argc, &argv, _("[torrent files]"), entries, domain, NULL );
myUIManager = gtk_ui_manager_new ();
actions_init ( myUIManager, cbdata );
gtk_ui_manager_add_ui_from_string (myUIManager, fallback_ui_file, -1, NULL);
gtk_ui_manager_ensure_update (myUIManager);
gtk_window_set_default_icon_name ( "transmission-logo" );
argfiles = checkfilenames( argc-1, argv+1 );
2007-02-19 22:09:05 +00:00
didinit = cf_init( tr_getPrefsDirectory(), NULL );
2007-10-13 03:15:02 +00:00
didlock = didinit && sendremote( argfiles, sendquit );
setupsighandlers( ); /* set up handlers for fatal signals */
2006-07-16 19:39:23 +00:00
2007-02-19 22:09:05 +00:00
if( ( didinit || cf_init( tr_getPrefsDirectory(), &err ) ) &&
( didlock || cf_lock( &err ) ) )
{
/* create main window now to be a parent to any error dialogs */
2007-10-12 20:50:03 +00:00
GtkWindow * mainwind = GTK_WINDOW( tr_window_new( myUIManager ) );
2006-07-16 19:39:23 +00:00
tr_prefs_init_global( );
2006-08-20 22:45:29 +00:00
2007-10-12 20:50:03 +00:00
/* set message level here before tr_init() */
msgwin_loadpref( );
appsetup( mainwind, argfiles, cbdata, startpaused );
2007-02-19 22:09:05 +00:00
}
else
{
gtk_widget_show( errmsg_full( NULL, (callbackfunc_t)gtk_main_quit,
NULL, "%s", err ) );
g_free( err );
}
2006-07-16 19:39:23 +00:00
2007-02-19 22:09:05 +00:00
freestrlist(argfiles);
2006-07-16 19:39:23 +00:00
2007-02-19 22:09:05 +00:00
gtk_main();
2006-07-16 19:39:23 +00:00
2007-02-19 22:09:05 +00:00
return 0;
}
2006-07-16 19:39:23 +00:00
2007-02-19 22:09:05 +00:00
static gboolean
sendremote( GList * files, gboolean sendquit )
{
2007-10-13 03:15:02 +00:00
const gboolean didlock = cf_lock( NULL );
2007-02-19 22:09:05 +00:00
2007-10-13 03:15:02 +00:00
/* send files if there's another instance, otherwise start normally */
if( !didlock && files )
exit( ipc_sendfiles_blocking( files ) ? 0 : 1 );
2007-02-19 22:09:05 +00:00
2007-10-13 03:15:02 +00:00
/* either send a quit message or exit if no other instance */
2007-02-19 22:09:05 +00:00
if( sendquit )
2007-10-13 03:15:02 +00:00
exit( didlock ? 0 : !ipc_sendquit_blocking() );
2006-07-16 19:39:23 +00:00
2007-02-19 22:09:05 +00:00
return didlock;
2006-07-16 19:39:23 +00:00
}
2007-02-19 22:09:05 +00:00
static void
appsetup( TrWindow * wind, GList * args,
struct cbdata * cbdata, gboolean paused )
2007-02-19 22:09:05 +00:00
{
enum tr_torrent_action action;
2007-02-19 22:09:05 +00:00
/* fill out cbdata */
cbdata->wind = NULL;
cbdata->core = tr_core_new();
2007-02-19 22:09:05 +00:00
cbdata->icon = NULL;
cbdata->msgwin = NULL;
2007-02-19 22:09:05 +00:00
cbdata->prefs = NULL;
cbdata->timer = 0;
cbdata->closing = FALSE;
cbdata->errqueue = NULL;
/* set up core handlers */
g_signal_connect( cbdata->core, "error", G_CALLBACK( coreerr ), cbdata );
g_signal_connect( cbdata->core, "directory-prompt",
G_CALLBACK( coreprompt ), cbdata );
2007-05-24 10:37:07 +00:00
g_signal_connect( cbdata->core, "directory-prompt-data",
G_CALLBACK( corepromptdata ), cbdata );
g_signal_connect_swapped( cbdata->core, "quit",
G_CALLBACK( wannaquit ), cbdata );
g_signal_connect( cbdata->core, "prefs-changed",
G_CALLBACK( prefschanged ), cbdata );
2007-02-19 22:09:05 +00:00
/* apply a few prefs */
2007-10-13 03:15:02 +00:00
initializeFromPrefs( cbdata );
2007-02-19 22:09:05 +00:00
/* add torrents from command-line and saved state */
2007-07-23 03:03:45 +00:00
tr_core_load( cbdata->core, paused );
if( NULL != args )
{
action = tr_prefs_get_action( PREF_KEY_ADDIPC );
tr_core_add_list( cbdata->core, args, action, paused );
}
tr_core_torrents_added( cbdata->core );
/* set up the ipc socket */
ipc_socket_setup( GTK_WINDOW( wind ), cbdata->core );
2007-02-19 22:09:05 +00:00
/* set up main window */
winsetup( cbdata, wind );
/* start model update timer */
cbdata->timer = g_timeout_add( UPDATE_INTERVAL, updatemodel, cbdata );
updatemodel( cbdata );
2006-07-16 19:39:23 +00:00
/* show the window */
gtk_widget_show( GTK_WIDGET(wind) );
2006-07-16 19:39:23 +00:00
}
static gboolean
2007-10-13 03:15:02 +00:00
winclose( GtkWidget * w UNUSED, GdkEvent * event UNUSED, gpointer gdata )
{
2007-10-13 03:15:02 +00:00
struct cbdata * cbdata = gdata;
2007-02-19 22:09:05 +00:00
if( cbdata->icon != NULL )
gtk_widget_hide( GTK_WIDGET( cbdata->wind ) );
else
askquit( cbdata->core, cbdata->wind, wannaquit, cbdata );
2007-02-19 22:09:05 +00:00
return TRUE; /* don't propagate event further */
2007-02-19 22:09:05 +00:00
}
static void
rowChangedCB( GtkTreeModel * model UNUSED,
GtkTreePath * path,
GtkTreeIter * iter UNUSED,
gpointer sel)
2007-02-19 22:09:05 +00:00
{
if( gtk_tree_selection_path_is_selected ( sel, path ) )
refreshTorrentActions( GTK_TREE_SELECTION(sel) );
2007-02-19 22:09:05 +00:00
}
static void
winsetup( struct cbdata * cbdata, TrWindow * wind )
2007-02-19 22:09:05 +00:00
{
GtkTreeModel * model;
GtkTreeSelection * sel;
2007-02-19 22:09:05 +00:00
g_assert( NULL == cbdata->wind );
cbdata->wind = GTK_WINDOW( wind );
2007-02-19 22:09:05 +00:00
sel = tr_window_get_selection( cbdata->wind );
g_signal_connect( sel, "changed", G_CALLBACK(selectionChangedCB), NULL );
selectionChangedCB( sel, NULL );
model = tr_core_model( cbdata->core );
gtk_tree_view_set_model ( gtk_tree_selection_get_tree_view(sel), model );
g_signal_connect( model, "row-changed", G_CALLBACK(rowChangedCB), sel );
g_signal_connect( wind, "delete-event", G_CALLBACK( winclose ), cbdata );
refreshTorrentActions( sel );
setupdrag( GTK_WIDGET(wind), cbdata );
2006-07-16 19:39:23 +00:00
}
static void
makeicon( struct cbdata * cbdata )
2007-02-19 22:09:05 +00:00
{
2007-10-13 03:15:02 +00:00
if( cbdata->icon == NULL )
cbdata->icon = tr_icon_new( );
2007-02-19 22:09:05 +00:00
}
static gpointer
quitThreadFunc( gpointer gdata )
2007-02-19 22:09:05 +00:00
{
struct cbdata * cbdata = gdata;
2007-02-19 22:09:05 +00:00
tr_close( tr_core_handle( cbdata->core ) );
2006-07-16 19:39:23 +00:00
/* shutdown the gui */
if( cbdata->prefs )
2007-02-19 22:09:05 +00:00
gtk_widget_destroy( GTK_WIDGET( cbdata->prefs ) );
if( cbdata->wind )
2007-02-19 22:09:05 +00:00
gtk_widget_destroy( GTK_WIDGET( cbdata->wind ) );
g_object_unref( cbdata->core );
if( cbdata->icon )
2007-02-19 22:09:05 +00:00
g_object_unref( cbdata->icon );
if( cbdata->errqueue ) {
g_list_foreach( cbdata->errqueue, (GFunc)g_free, NULL );
g_list_free( cbdata->errqueue );
}
2007-02-19 22:09:05 +00:00
g_free( cbdata );
/* exit the gtk main loop */
gtk_main_quit( );
return NULL;
}
/* since there are no buttons in the dialog, gtk tries to
* select one of the labels, which looks ugly... so force
* the dialog's primary and secondary labels to be unselectable */
static void
deselectLabels( GtkWidget * w, gpointer unused UNUSED )
{
if( GTK_IS_LABEL( w ) )
gtk_label_set_selectable( GTK_LABEL(w), FALSE );
else if( GTK_IS_CONTAINER( w ) )
gtk_container_foreach( GTK_CONTAINER(w), deselectLabels, NULL );
}
static void
wannaquit( void * vdata )
{
GtkWidget * w;
#if GTK_CHECK_VERSION(2,10,0)
GtkWidget * i;
#endif
struct cbdata * cbdata = vdata;
/* stop the update timer */
if( cbdata->timer ) {
g_source_remove( cbdata->timer );
cbdata->timer = 0;
}
w = gtk_message_dialog_new( cbdata->wind,
GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO,
GTK_BUTTONS_NONE,
_("Closing Connections" ) );
#if GTK_CHECK_VERSION(2,10,0)
i = gtk_image_new_from_stock( GTK_STOCK_NETWORK, GTK_ICON_SIZE_DIALOG );
gtk_widget_show( i );
gtk_message_dialog_set_image( GTK_MESSAGE_DIALOG(w), i );
#endif
gtk_message_dialog_format_secondary_text( GTK_MESSAGE_DIALOG(w),
_("Sending upload/download totals to tracker..." ) );
gtk_container_foreach( GTK_CONTAINER(GTK_DIALOG(w)->vbox), deselectLabels, NULL );
gtk_widget_show( w );
/* clear the UI */
gtk_list_store_clear( GTK_LIST_STORE( tr_core_model( cbdata->core ) ) );
gtk_widget_set_sensitive( GTK_WIDGET( cbdata->wind ), FALSE );
/* shut down libT */
g_thread_create( quitThreadFunc, vdata, TRUE, NULL );
2006-07-16 19:39:23 +00:00
}
static void
gotdrag( GtkWidget * widget UNUSED,
GdkDragContext * dc,
gint x UNUSED,
gint y UNUSED,
GtkSelectionData * sel,
guint info UNUSED,
guint time,
gpointer gdata )
{
struct cbdata * data = gdata;
GList * paths = NULL;
GList * freeme = NULL;
2006-07-16 19:39:23 +00:00
#ifdef DND_DEBUG
char *sele = gdk_atom_name(sel->selection);
char *targ = gdk_atom_name(sel->target);
char *type = gdk_atom_name(sel->type);
2007-10-13 03:15:02 +00:00
g_message( "dropped file: sel=%s targ=%s type=%s fmt=%i len=%i",
sele, targ, type, sel->format, sel->length );
g_free(sele);
g_free(targ);
g_free(type);
if( sel->format == 8 ) {
for( i=0; i<sel->length; ++i )
fprintf(stderr, "%02X ", sel->data[i]);
fprintf(stderr, "\n");
}
2006-07-16 19:39:23 +00:00
#endif
if( ( sel->format == 8 ) &&
( sel->selection == gdk_atom_intern( "XdndSelection", FALSE ) ) )
{
int i;
char * str = g_strndup( (char*)sel->data, sel->length );
gchar ** files = g_strsplit_set( str, "\r\n", -1 );
for( i=0; files && files[i]; ++i )
{
char * filename;
if( !*files[i] ) /* empty filename... */
continue;
/* decode the filename */
filename = urldecode( files[i], -1 );
freeme = g_list_prepend( freeme, filename );
if( !g_utf8_validate( filename, -1, NULL ) )
continue;
/* walk past "file://", if present */
if( g_str_has_prefix( filename, "file:" ) ) {
filename += 5;
while( g_str_has_prefix( filename, "//" ) )
++filename;
}
/* if the file doesn't exist, the first part
might be a hostname ... walk past it. */
if( !g_file_test( filename, G_FILE_TEST_EXISTS ) ) {
char * pch = strchr( filename + 1, '/' );
if( pch != NULL )
filename = pch;
}
2006-07-16 19:39:23 +00:00
/* finally, add it to the list of torrents to try adding */
if( g_file_test( filename, G_FILE_TEST_EXISTS ) )
paths = g_list_prepend( paths, filename );
2006-07-16 19:39:23 +00:00
}
/* try to add any torrents we found */
if( paths != NULL )
{
enum tr_torrent_action action = tr_prefs_get_action( PREF_KEY_ADDSTD );
paths = g_list_reverse( paths );
tr_core_add_list( data->core, paths, action, FALSE );
tr_core_torrents_added( data->core );
g_list_free( paths );
}
freestrlist( freeme );
g_strfreev( files );
g_free( str );
2007-02-19 22:09:05 +00:00
}
2006-07-16 19:39:23 +00:00
gtk_drag_finish(dc, (NULL != paths), FALSE, time);
2006-07-16 19:39:23 +00:00
}
static void
2006-07-16 19:39:23 +00:00
setupdrag(GtkWidget *widget, struct cbdata *data) {
GtkTargetEntry targets[] = {
{ "STRING", 0, 0 },
{ "text/plain", 0, 0 },
{ "text/uri-list", 0, 0 },
};
g_signal_connect(widget, "drag_data_received", G_CALLBACK(gotdrag), data);
gtk_drag_dest_set(widget, GTK_DEST_DEFAULT_ALL, targets,
ALEN(targets), GDK_ACTION_COPY | GDK_ACTION_MOVE);
}
static void
2007-10-13 03:15:02 +00:00
coreerr( TrCore * core UNUSED, enum tr_core_err code, const char * msg,
gpointer gdata )
{
struct cbdata * cbdata = gdata;
char * joined;
switch( code )
{
case TR_CORE_ERR_ADD_TORRENT:
cbdata->errqueue = g_list_append( cbdata->errqueue,
g_strdup( msg ) );
return;
case TR_CORE_ERR_NO_MORE_TORRENTS:
if( NULL != cbdata->errqueue )
{
joined = joinstrlist( cbdata->errqueue, "\n" );
errmsg( cbdata->wind,
ngettext( "Failed to load torrent file:\n%s",
"Failed to load torrent files:\n%s",
g_list_length( cbdata->errqueue ) ),
joined );
g_list_foreach( cbdata->errqueue, (GFunc) g_free, NULL );
g_list_free( cbdata->errqueue );
cbdata->errqueue = NULL;
g_free( joined );
}
return;
case TR_CORE_ERR_SAVE_STATE:
errmsg( cbdata->wind, "%s", msg );
return;
}
g_assert_not_reached();
}
void
coreprompt( TrCore * core, GList * paths, enum tr_torrent_action act,
gboolean paused, gpointer gdata )
{
struct cbdata * cbdata = gdata;
2007-05-24 10:37:07 +00:00
promptfordir( cbdata->wind, core, paths, NULL, 0, act, paused );
}
void
corepromptdata( TrCore * core, uint8_t * data, size_t size,
gboolean paused, gpointer gdata )
{
struct cbdata * cbdata = gdata;
promptfordir( cbdata->wind, core, NULL, data, size, TR_TOR_LEAVE, paused );
}
2007-02-19 22:09:05 +00:00
static void
2007-10-13 03:15:02 +00:00
initializeFromPrefs( struct cbdata * cbdata )
2007-02-19 22:09:05 +00:00
{
size_t i;
const char * keys[] =
2007-02-19 22:09:05 +00:00
{
PREF_KEY_PORT,
PREF_KEY_DL_LIMIT_ENABLED,
PREF_KEY_DL_LIMIT,
PREF_KEY_UL_LIMIT_ENABLED,
PREF_KEY_UL_LIMIT,
PREF_KEY_NAT,
PREF_KEY_PEX,
PREF_KEY_SYSTRAY,
PREF_KEY_SORT_COLUMN,
PREF_KEY_ENCRYPTED_ONLY
2007-02-19 22:09:05 +00:00
};
for( i=0; i<G_N_ELEMENTS(keys); ++i )
prefschanged( NULL, keys[i], cbdata );
2007-02-19 22:09:05 +00:00
}
static void
prefschanged( TrCore * core UNUSED, const char * key, gpointer data )
2007-02-19 22:09:05 +00:00
{
2007-07-23 03:03:45 +00:00
struct cbdata * cbdata = data;
tr_handle * tr = tr_core_handle( cbdata->core );
2007-02-19 22:09:05 +00:00
if( !strcmp( key, PREF_KEY_ENCRYPTED_ONLY ) )
{
const gboolean crypto_only = pref_flag_get( key );
tr_setEncryptionMode( tr, crypto_only ? TR_ENCRYPTION_REQUIRED
: TR_ENCRYPTION_PREFERRED );
}
else if( !strcmp( key, PREF_KEY_PORT ) )
2007-02-19 22:09:05 +00:00
{
const int port = pref_int_get( key );
tr_setBindPort( tr, port );
}
else if( !strcmp( key, PREF_KEY_DL_LIMIT_ENABLED ) )
{
const gboolean b = pref_flag_get( key );
tr_setUseGlobalSpeedLimit( tr, TR_DOWN, b );
}
else if( !strcmp( key, PREF_KEY_DL_LIMIT ) )
{
const int limit = pref_int_get( key );
tr_setGlobalSpeedLimit( tr, TR_DOWN, limit );
}
else if( !strcmp( key, PREF_KEY_UL_LIMIT_ENABLED ) )
{
const gboolean b = pref_flag_get( key );
tr_setUseGlobalSpeedLimit( tr, TR_UP, b );
}
else if( !strcmp( key, PREF_KEY_UL_LIMIT ) )
{
const int limit = pref_int_get( key );
tr_setGlobalSpeedLimit( tr, TR_UP, limit );
}
else if( !strcmp( key, PREF_KEY_NAT ) )
{
const gboolean enabled = pref_flag_get( key );
tr_natTraversalEnable( tr, enabled );
}
else if( !strcmp( key, PREF_KEY_SYSTRAY ) )
{
if( pref_flag_get( key ) )
{
makeicon( cbdata );
}
else if( cbdata->icon )
{
g_object_unref( cbdata->icon );
cbdata->icon = NULL;
}
}
else if( !strcmp( key, PREF_KEY_SORT_COLUMN ) )
{
tr_core_set_sort_column_from_prefs( cbdata->core );
}
else if( !strcmp( key, PREF_KEY_PEX ) )
{
gboolean enabled = pref_flag_get( key );
tr_torrentIterate( tr, setpex, &enabled );
2007-02-19 22:09:05 +00:00
}
}
2007-03-31 19:19:27 +00:00
void
setpex( tr_torrent * tor, void * arg )
2007-03-31 19:19:27 +00:00
{
2007-11-27 03:29:51 +00:00
const gboolean * val = arg;
tr_torrentDisablePex( tor, !*val );
2007-03-31 19:19:27 +00:00
}
2006-07-16 19:39:23 +00:00
gboolean
updatemodel(gpointer gdata) {
struct cbdata *data = gdata;
float up, down;
2007-02-19 22:09:05 +00:00
if( !data->closing && 0 < global_sigcount )
{
wannaquit( data );
return FALSE;
2006-07-16 19:39:23 +00:00
}
/* update the torrent data in the model */
tr_core_update( data->core );
2006-07-16 19:39:23 +00:00
/* update the main window's statusbar and toolbar buttons */
2007-02-19 22:09:05 +00:00
if( NULL != data->wind )
{
tr_torrentRates( tr_core_handle( data->core ), &down, &up );
tr_window_update( data->wind, down, up );
2007-02-19 22:09:05 +00:00
}
2006-07-16 19:39:23 +00:00
2006-08-20 22:45:29 +00:00
/* update the message window */
msgwin_update();
2006-07-16 19:39:23 +00:00
return TRUE;
}
2007-10-13 03:15:02 +00:00
/* returns a GList of GtkTreeRowReferences to each selected row */
2007-02-19 22:09:05 +00:00
static GList *
getselection( struct cbdata * cbdata )
{
GList * rows = NULL;
2007-02-19 22:09:05 +00:00
if( NULL != cbdata->wind )
2007-02-19 22:09:05 +00:00
{
GList * ii;
GtkTreeSelection *s = tr_window_get_selection(cbdata->wind);
GtkTreeModel * model = tr_core_model( cbdata->core );
rows = gtk_tree_selection_get_selected_rows( s, NULL );
for( ii = rows; NULL != ii; ii = ii->next )
{
GtkTreeRowReference * ref = gtk_tree_row_reference_new(
model, ii->data );
gtk_tree_path_free( ii->data );
ii->data = ref;
}
2007-02-19 22:09:05 +00:00
}
return rows;
}
static void
about ( void )
{
GtkWidget * w = gtk_about_dialog_new ();
GtkAboutDialog * a = GTK_ABOUT_DIALOG (w);
2007-10-13 03:15:02 +00:00
const char *authors[] = { "Charles Kerr (Backend; GTK+)",
"Mitchell Livingston (Backend; OS X)",
"Eric Petit (Backend; OS X)",
"Josh Elsasser (Daemon; Backend; GTK+)",
"Bryan Varner (BeOS)",
NULL };
gtk_about_dialog_set_version (a, LONG_VERSION_STRING );
2007-06-09 17:08:25 +00:00
#ifdef SHOW_LICENSE
gtk_about_dialog_set_license (a, LICENSE);
gtk_about_dialog_set_wrap_license (a, TRUE);
2007-06-09 17:08:25 +00:00
#endif
gtk_about_dialog_set_logo_icon_name( a, "transmission-logo" );
2007-10-13 14:02:10 +00:00
gtk_about_dialog_set_comments( a, _("A fast and easy BitTorrent client") );
gtk_about_dialog_set_website( a, "http://transmission.m0k.org/" );
gtk_about_dialog_set_copyright( a, _("Copyright 2005-2007 The Transmission Project") );
gtk_about_dialog_set_authors( a, authors );
/* note to translators: put yourself here for credit in the "About" dialog */
gtk_about_dialog_set_translator_credits( a, _("translator-credits") );
g_signal_connect_swapped( w, "response", G_CALLBACK (gtk_widget_destroy), w );
gtk_widget_show_all( w );
}
static void
startTorrentForeach (GtkTreeModel * model,
GtkTreePath * path UNUSED,
GtkTreeIter * iter,
gpointer data UNUSED)
{
TrTorrent * tor = NULL;
gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 );
tr_torrent_start( tor );
g_object_unref( G_OBJECT( tor ) );
}
static void
stopTorrentForeach (GtkTreeModel * model,
GtkTreePath * path UNUSED,
GtkTreeIter * iter,
gpointer data UNUSED)
{
TrTorrent * tor = NULL;
gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 );
tr_torrent_stop( tor );
g_object_unref( G_OBJECT( tor ) );
}
static void
updateTrackerForeach (GtkTreeModel * model,
GtkTreePath * path UNUSED,
GtkTreeIter * iter,
gpointer data UNUSED)
{
TrTorrent * tor = NULL;
gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 );
tr_manualUpdate( tr_torrent_handle( tor ) );
g_object_unref( G_OBJECT( tor ) );
}
static void
showInfoForeach (GtkTreeModel * model,
GtkTreePath * path UNUSED,
GtkTreeIter * iter,
gpointer data UNUSED)
{
TrTorrent * tor = NULL;
GtkWidget * w;
gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 );
w = torrent_inspector_new( GTK_WINDOW(data), tor );
gtk_widget_show( w );
g_object_unref( G_OBJECT( tor ) );
}
2006-07-16 19:39:23 +00:00
static void
recheckTorrentForeach (GtkTreeModel * model,
GtkTreePath * path UNUSED,
GtkTreeIter * iter,
gpointer data UNUSED)
{
TrTorrent * gtor = NULL;
gtk_tree_model_get( model, iter, MC_TORRENT, &gtor, -1 );
tr_torrentRecheck( tr_torrent_handle( gtor ) );
g_object_unref( G_OBJECT( gtor ) );
}
2006-07-16 19:39:23 +00:00
static gboolean
msgwinclosed()
{
action_toggle( "toggle-message-log", FALSE );
return FALSE;
}
void
doAction ( const char * action_name, gpointer user_data )
{
struct cbdata * data = (struct cbdata *) user_data;
gboolean changed = FALSE;
if (!strcmp (action_name, "add-torrent"))
{
makeaddwind( data->wind, data->core );
2006-07-16 19:39:23 +00:00
}
2007-11-27 03:29:51 +00:00
else if (!strcmp (action_name, "show-stats"))
{
GtkWidget * dialog = stats_dialog_create( data->wind,
data->core );
gtk_widget_show( dialog );
}
else if (!strcmp (action_name, "start-torrent"))
{
GtkTreeSelection * s = tr_window_get_selection(data->wind);
gtk_tree_selection_selected_foreach( s, startTorrentForeach, NULL );
changed |= gtk_tree_selection_count_selected_rows( s ) != 0;
}
else if (!strcmp (action_name, "pause-torrent"))
{
GtkTreeSelection * s = tr_window_get_selection(data->wind);
gtk_tree_selection_selected_foreach( s, stopTorrentForeach, NULL );
changed |= gtk_tree_selection_count_selected_rows( s ) != 0;
}
2007-10-10 18:20:52 +00:00
else if (!strcmp (action_name, "verify-torrent"))
{
GtkTreeSelection * s = tr_window_get_selection(data->wind);
gtk_tree_selection_selected_foreach( s, recheckTorrentForeach, NULL );
changed |= gtk_tree_selection_count_selected_rows( s ) != 0;
}
else if (!strcmp (action_name, "show-torrent-details"))
{
GtkTreeSelection * s = tr_window_get_selection(data->wind);
gtk_tree_selection_selected_foreach( s, showInfoForeach, data->wind );
}
else if (!strcmp( action_name, "update-tracker"))
{
GtkTreeSelection * s = tr_window_get_selection(data->wind);
gtk_tree_selection_selected_foreach( s, updateTrackerForeach, data->wind );
}
else if (!strcmp (action_name, "create-torrent"))
{
2007-10-13 03:15:02 +00:00
GtkWidget * w = make_meta_ui( GTK_WINDOW( data->wind ),
tr_core_handle( data->core ) );
gtk_widget_show_all( w );
}
else if (!strcmp (action_name, "remove-torrent"))
{
/* this modifies the model's contents, so can't use foreach */
GList *l, *sel = getselection( data );
GtkTreeModel *model = tr_core_model( data->core );
for( l=sel; l!=NULL; l=l->next )
{
GtkTreeIter iter;
GtkTreeRowReference * reference = (GtkTreeRowReference *) l->data;
GtkTreePath * path = gtk_tree_row_reference_get_path( reference );
gtk_tree_model_get_iter( model, &iter, path );
tr_core_delete_torrent( data->core, &iter );
gtk_tree_row_reference_free( reference );
changed = TRUE;
}
g_list_free( sel );
}
else if (!strcmp (action_name, "close"))
{
if( data->wind != NULL )
winclose( NULL, NULL, data );
}
else if (!strcmp (action_name, "quit"))
{
askquit( data->core, data->wind, wannaquit, data );
}
2007-07-30 15:51:37 +00:00
else if (!strcmp (action_name, "select-all"))
{
GtkTreeSelection * s = tr_window_get_selection(data->wind);
gtk_tree_selection_select_all( s );
}
else if (!strcmp (action_name, "unselect-all"))
{
GtkTreeSelection * s = tr_window_get_selection(data->wind);
gtk_tree_selection_unselect_all( s );
}
else if (!strcmp (action_name, "edit-preferences"))
{
if( NULL == data->prefs )
{
data->prefs = tr_prefs_dialog_new( G_OBJECT(data->core), data->wind );
g_signal_connect( data->prefs, "destroy",
G_CALLBACK( gtk_widget_destroyed ), &data->prefs );
gtk_widget_show( GTK_WIDGET( data->prefs ) );
}
}
else if (!strcmp (action_name, "toggle-message-log"))
{
if( !data->msgwin )
{
GtkWidget * win = msgwin_create( data->core );
g_signal_connect( win, "destroy", G_CALLBACK( msgwinclosed ),
NULL );
data->msgwin = win;
}
else
{
action_toggle("toggle-message-log", FALSE);
gtk_widget_destroy( data->msgwin );
data->msgwin = NULL;
}
}
else if (!strcmp (action_name, "show-about-dialog"))
{
about();
}
else if (!strcmp (action_name, "toggle-main-window"))
{
GtkWidget * w = GTK_WIDGET (data->wind);
if (GTK_WIDGET_VISIBLE(w))
gtk_widget_hide (w);
else
gtk_window_present (GTK_WINDOW(w));
}
else g_error ("Unhandled action: %s", action_name );
2006-07-16 19:39:23 +00:00
2007-10-12 20:50:03 +00:00
if( changed )
updatemodel( data );
2006-07-16 19:39:23 +00:00
}
static void
2006-07-16 19:39:23 +00:00
setupsighandlers(void) {
2007-02-19 22:09:05 +00:00
int sigs[] = {SIGHUP, SIGINT, SIGQUIT, SIGTERM};
2006-07-16 19:39:23 +00:00
struct sigaction sa;
2007-02-19 22:09:05 +00:00
int ii;
2006-07-16 19:39:23 +00:00
memset(&sa, 0, sizeof(sa));
2006-07-16 19:39:23 +00:00
sa.sa_handler = fatalsig;
for(ii = 0; ii < ALEN(sigs); ii++)
sigaction(sigs[ii], &sa, NULL);
}
static void
2006-07-16 19:39:23 +00:00
fatalsig(int sig) {
struct sigaction sa;
if(SIGCOUNT_MAX <= ++global_sigcount) {
memset(&sa, 0, sizeof(sa));
2006-07-16 19:39:23 +00:00
sa.sa_handler = SIG_DFL;
sigaction(sig, &sa, NULL);
raise(sig);
}
}