2007-05-23 00:49:31 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-01-01 17:20:20 +00:00
|
|
|
* Copyright (c) 2007-2008 Transmission authors and contributors
|
2007-05-23 00:49:31 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2008-02-14 17:18:00 +00:00
|
|
|
#include <string.h> /* strcmp, strlen */
|
2007-05-23 00:49:31 +00:00
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <glib/gi18n.h>
|
2008-03-09 15:27:08 +00:00
|
|
|
#ifdef HAVE_GIO
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <gio/gio.h>
|
2008-03-09 15:27:08 +00:00
|
|
|
#endif
|
2008-05-18 16:44:30 +00:00
|
|
|
#ifdef HAVE_DBUS_GLIB
|
2008-09-23 19:11:04 +00:00
|
|
|
#include <dbus/dbus-glib.h>
|
2008-05-18 16:44:30 +00:00
|
|
|
#endif
|
2007-05-23 00:49:31 +00:00
|
|
|
|
2007-07-18 23:04:26 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2009-04-07 20:13:08 +00:00
|
|
|
#include <libtransmission/bencode.h>
|
|
|
|
#include <libtransmission/rpcimpl.h>
|
|
|
|
#include <libtransmission/json.h>
|
2008-02-28 20:23:02 +00:00
|
|
|
#include <libtransmission/utils.h> /* tr_free */
|
2009-11-22 16:20:22 +00:00
|
|
|
#include <libtransmission/web.h>
|
2007-05-23 01:47:42 +00:00
|
|
|
|
2007-05-23 04:39:06 +00:00
|
|
|
#include "conf.h"
|
2009-06-11 16:17:48 +00:00
|
|
|
#include "notify.h"
|
2008-02-26 19:58:03 +00:00
|
|
|
#include "tr-core.h"
|
2008-05-18 16:44:30 +00:00
|
|
|
#ifdef HAVE_DBUS_GLIB
|
2008-09-23 19:11:04 +00:00
|
|
|
#include "tr-core-dbus.h"
|
2008-05-18 16:44:30 +00:00
|
|
|
#endif
|
2008-02-26 19:58:03 +00:00
|
|
|
#include "tr-prefs.h"
|
|
|
|
#include "tr-torrent.h"
|
2007-05-23 00:49:31 +00:00
|
|
|
#include "util.h"
|
2008-08-17 12:39:26 +00:00
|
|
|
#include "actions.h"
|
2007-05-23 00:49:31 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
static void maybeInhibitHibernation( TrCore * core );
|
2008-05-24 23:23:20 +00:00
|
|
|
|
2008-05-28 15:05:17 +00:00
|
|
|
static gboolean our_instance_adds_remote_torrents = FALSE;
|
|
|
|
|
2008-02-09 17:29:05 +00:00
|
|
|
struct TrCorePrivate
|
|
|
|
{
|
2008-03-09 15:27:08 +00:00
|
|
|
#ifdef HAVE_GIO
|
2008-09-23 19:11:04 +00:00
|
|
|
GFileMonitor * monitor;
|
|
|
|
gulong monitor_tag;
|
|
|
|
char * monitor_path;
|
|
|
|
GSList * monitor_files;
|
|
|
|
guint monitor_idle_tag;
|
2008-03-09 15:27:08 +00:00
|
|
|
#endif
|
2008-10-27 20:12:53 +00:00
|
|
|
gboolean adding_from_watch_dir;
|
2008-09-23 19:11:04 +00:00
|
|
|
gboolean inhibit_allowed;
|
|
|
|
gboolean have_inhibit_cookie;
|
|
|
|
gboolean dbus_error;
|
|
|
|
guint inhibit_cookie;
|
|
|
|
GtkTreeModel * model;
|
2008-10-28 19:49:33 +00:00
|
|
|
tr_session * session;
|
2008-02-09 17:29:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
isDisposed( const TrCore * core )
|
|
|
|
{
|
|
|
|
return !core || !core->priv;
|
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
static void
|
|
|
|
tr_core_dispose( GObject * obj )
|
|
|
|
{
|
2008-02-09 17:29:05 +00:00
|
|
|
TrCore * core = TR_CORE( obj );
|
2007-10-16 14:16:01 +00:00
|
|
|
|
2008-02-09 17:29:05 +00:00
|
|
|
if( !isDisposed( core ) )
|
|
|
|
{
|
2008-02-14 17:18:00 +00:00
|
|
|
GObjectClass * parent;
|
|
|
|
|
2008-02-09 17:29:05 +00:00
|
|
|
core->priv = NULL;
|
2007-10-16 14:16:01 +00:00
|
|
|
|
2008-02-14 17:18:00 +00:00
|
|
|
parent = g_type_class_peek( g_type_parent( TR_CORE_TYPE ) );
|
|
|
|
parent->dispose( obj );
|
|
|
|
}
|
2007-10-16 14:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_core_class_init( gpointer g_class,
|
|
|
|
gpointer g_class_data UNUSED )
|
2007-10-16 14:16:01 +00:00
|
|
|
{
|
|
|
|
GObjectClass * gobject_class;
|
2009-04-07 20:13:08 +00:00
|
|
|
TrCoreClass * cc;
|
2007-10-16 14:16:01 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
g_type_class_add_private( g_class, sizeof( struct TrCorePrivate ) );
|
2008-02-14 17:18:00 +00:00
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
gobject_class = G_OBJECT_CLASS( g_class );
|
|
|
|
gobject_class->dispose = tr_core_dispose;
|
|
|
|
|
2009-04-07 20:13:08 +00:00
|
|
|
cc = TR_CORE_CLASS( g_class );
|
|
|
|
|
|
|
|
cc->blocklistSignal = g_signal_new( "blocklist-updated", /* name */
|
|
|
|
G_TYPE_FROM_CLASS( g_class ), /* applies to TrCore */
|
|
|
|
G_SIGNAL_RUN_FIRST, /* when to invoke */
|
|
|
|
0, NULL, NULL, /* accumulator */
|
|
|
|
g_cclosure_marshal_VOID__INT, /* marshaler */
|
|
|
|
G_TYPE_NONE, /* return type */
|
|
|
|
1, G_TYPE_INT ); /* signal arguments */
|
|
|
|
|
|
|
|
cc->portSignal = g_signal_new( "port-tested",
|
|
|
|
G_TYPE_FROM_CLASS( g_class ),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
0, NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__BOOLEAN,
|
|
|
|
G_TYPE_NONE,
|
|
|
|
1, G_TYPE_BOOLEAN );
|
|
|
|
|
|
|
|
cc->errsig = g_signal_new( "error",
|
|
|
|
G_TYPE_FROM_CLASS( g_class ),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
0, NULL, NULL,
|
2009-04-07 23:44:38 +00:00
|
|
|
g_cclosure_marshal_VOID__UINT_POINTER,
|
2009-04-07 20:13:08 +00:00
|
|
|
G_TYPE_NONE,
|
2009-04-07 23:44:38 +00:00
|
|
|
2, G_TYPE_UINT, G_TYPE_POINTER );
|
2009-04-07 20:13:08 +00:00
|
|
|
|
|
|
|
cc->promptsig = g_signal_new( "add-torrent-prompt",
|
|
|
|
G_TYPE_FROM_CLASS( g_class ),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
0, NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__POINTER,
|
|
|
|
G_TYPE_NONE,
|
|
|
|
1, G_TYPE_POINTER );
|
|
|
|
|
|
|
|
cc->quitsig = g_signal_new( "quit",
|
|
|
|
G_TYPE_FROM_CLASS( g_class ),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
0, NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__VOID,
|
|
|
|
G_TYPE_NONE,
|
|
|
|
0 );
|
|
|
|
|
|
|
|
cc->prefsig = g_signal_new( "prefs-changed",
|
|
|
|
G_TYPE_FROM_CLASS( g_class ),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
0, NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__STRING,
|
|
|
|
G_TYPE_NONE,
|
|
|
|
1, G_TYPE_STRING );
|
2008-05-18 16:44:30 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_DBUS_GLIB
|
|
|
|
{
|
|
|
|
DBusGConnection * bus = dbus_g_bus_get( DBUS_BUS_SESSION, NULL );
|
2008-09-23 19:11:04 +00:00
|
|
|
DBusGProxy * bus_proxy = NULL;
|
2008-05-18 16:44:30 +00:00
|
|
|
if( bus )
|
2008-09-23 19:11:04 +00:00
|
|
|
bus_proxy =
|
|
|
|
dbus_g_proxy_new_for_name( bus, "org.freedesktop.DBus",
|
|
|
|
"/org/freedesktop/DBus",
|
|
|
|
"org.freedesktop.DBus" );
|
|
|
|
if( bus_proxy )
|
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
int result = 0;
|
|
|
|
dbus_g_proxy_call( bus_proxy, "RequestName", NULL,
|
2008-09-23 19:11:04 +00:00
|
|
|
G_TYPE_STRING,
|
|
|
|
"com.transmissionbt.Transmission",
|
2008-05-18 16:44:30 +00:00
|
|
|
G_TYPE_UINT, 0,
|
|
|
|
G_TYPE_INVALID,
|
|
|
|
G_TYPE_UINT, &result,
|
|
|
|
G_TYPE_INVALID );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( ( our_instance_adds_remote_torrents = result == 1 ) )
|
|
|
|
dbus_g_object_type_install_info(
|
|
|
|
TR_CORE_TYPE,
|
|
|
|
&
|
|
|
|
dbus_glib_tr_core_object_info );
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2007-10-16 14:16:01 +00:00
|
|
|
}
|
|
|
|
|
2008-02-14 17:18:00 +00:00
|
|
|
/***
|
|
|
|
**** SORTING
|
|
|
|
***/
|
|
|
|
|
2009-02-18 06:03:45 +00:00
|
|
|
static gboolean
|
|
|
|
isValidETA( int t )
|
|
|
|
{
|
|
|
|
return ( t != TR_ETA_NOT_AVAIL ) && ( t != TR_ETA_UNKNOWN );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
compareETA( int a, int b )
|
|
|
|
{
|
|
|
|
const gboolean a_valid = isValidETA( a );
|
|
|
|
const gboolean b_valid = isValidETA( b );
|
|
|
|
|
|
|
|
if( !a_valid && !b_valid ) return 0;
|
|
|
|
if( !a_valid ) return -1;
|
|
|
|
if( !b_valid ) return 1;
|
|
|
|
return a < b ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
2007-12-19 07:10:47 +00:00
|
|
|
static int
|
2009-02-18 06:03:45 +00:00
|
|
|
compareDouble( double a, double b )
|
2007-12-19 07:10:47 +00:00
|
|
|
{
|
|
|
|
if( a < b ) return -1;
|
2008-02-14 17:18:00 +00:00
|
|
|
if( a > b ) return 1;
|
2007-12-19 07:10:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-06 14:07:41 +00:00
|
|
|
static int
|
2009-02-18 06:03:45 +00:00
|
|
|
compareRatio( double a, double b )
|
2008-04-06 14:07:41 +00:00
|
|
|
{
|
|
|
|
if( (int)a == TR_RATIO_INF && (int)b == TR_RATIO_INF ) return 0;
|
|
|
|
if( (int)a == TR_RATIO_INF ) return 1;
|
|
|
|
if( (int)b == TR_RATIO_INF ) return -1;
|
|
|
|
return compareDouble( a, b );
|
|
|
|
}
|
|
|
|
|
2008-06-02 04:41:55 +00:00
|
|
|
static int
|
2009-02-18 06:03:45 +00:00
|
|
|
compareTime( time_t a, time_t b )
|
2008-06-02 04:41:55 +00:00
|
|
|
{
|
|
|
|
if( a < b ) return -1;
|
|
|
|
if( a > b ) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-06 14:07:41 +00:00
|
|
|
static int
|
2009-02-18 06:03:45 +00:00
|
|
|
compareByRatio( GtkTreeModel * model,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer user_data UNUSED )
|
2008-04-06 14:07:41 +00:00
|
|
|
{
|
2009-02-18 06:03:45 +00:00
|
|
|
tr_torrent *ta, *tb;
|
2008-04-06 14:07:41 +00:00
|
|
|
const tr_stat *sa, *sb;
|
|
|
|
|
|
|
|
gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 );
|
|
|
|
gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 );
|
|
|
|
|
|
|
|
sa = tr_torrentStatCached( ta );
|
|
|
|
sb = tr_torrentStatCached( tb );
|
|
|
|
|
|
|
|
return compareRatio( sa->ratio, sb->ratio );
|
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
static int
|
2009-08-13 03:52:21 +00:00
|
|
|
compareByActivity( GtkTreeModel * model,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
2007-12-19 02:46:30 +00:00
|
|
|
gpointer user_data UNUSED )
|
2007-10-16 14:16:01 +00:00
|
|
|
{
|
2009-08-12 13:42:49 +00:00
|
|
|
int i;
|
2009-08-07 20:49:06 +00:00
|
|
|
tr_torrent *ta, *tb;
|
2007-12-19 02:46:30 +00:00
|
|
|
const tr_stat *sa, *sb;
|
2009-08-12 13:42:49 +00:00
|
|
|
double aUp, aDown, bUp, bDown;
|
2007-12-27 18:56:54 +00:00
|
|
|
|
2009-08-07 20:49:06 +00:00
|
|
|
gtk_tree_model_get( model, a, MC_SPEED_UP, &aUp,
|
|
|
|
MC_SPEED_DOWN, &aDown,
|
|
|
|
MC_TORRENT_RAW, &ta,
|
|
|
|
-1 );
|
|
|
|
gtk_tree_model_get( model, b, MC_SPEED_UP, &bUp,
|
|
|
|
MC_SPEED_DOWN, &bDown,
|
|
|
|
MC_TORRENT_RAW, &tb,
|
|
|
|
-1 );
|
2007-10-16 14:16:01 +00:00
|
|
|
|
2009-08-13 03:52:21 +00:00
|
|
|
if(( i = compareDouble( aUp+aDown, bUp+bDown )))
|
2007-12-19 07:10:47 +00:00
|
|
|
return i;
|
2007-10-16 14:16:01 +00:00
|
|
|
|
2009-08-07 20:49:06 +00:00
|
|
|
sa = tr_torrentStatCached( ta );
|
|
|
|
sb = tr_torrentStatCached( tb );
|
2007-12-19 02:46:30 +00:00
|
|
|
if( sa->uploadedEver != sb->uploadedEver )
|
|
|
|
return sa->uploadedEver < sa->uploadedEver ? -1 : 1;
|
2007-10-16 14:32:16 +00:00
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-19 02:46:30 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
compareByName( GtkTreeModel * model,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
2007-12-19 02:46:30 +00:00
|
|
|
gpointer user_data UNUSED )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int ret;
|
2007-12-19 02:46:30 +00:00
|
|
|
char *ca, *cb;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-12-19 02:46:30 +00:00
|
|
|
gtk_tree_model_get( model, a, MC_NAME_COLLATED, &ca, -1 );
|
|
|
|
gtk_tree_model_get( model, b, MC_NAME_COLLATED, &cb, -1 );
|
|
|
|
ret = strcmp( ca, cb );
|
|
|
|
g_free( cb );
|
|
|
|
g_free( ca );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-06-02 04:41:55 +00:00
|
|
|
static int
|
2009-03-01 00:11:53 +00:00
|
|
|
compareByAge( GtkTreeModel * model,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer user_data UNUSED )
|
2008-06-02 04:41:55 +00:00
|
|
|
{
|
|
|
|
tr_torrent *ta, *tb;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-06-02 04:41:55 +00:00
|
|
|
gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 );
|
|
|
|
gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 );
|
2008-09-23 19:11:04 +00:00
|
|
|
return compareTime( tr_torrentStatCached( ta )->addedDate,
|
|
|
|
tr_torrentStatCached( tb )->addedDate );
|
2008-06-02 04:41:55 +00:00
|
|
|
}
|
|
|
|
|
2009-03-01 00:11:53 +00:00
|
|
|
static int
|
|
|
|
compareBySize( GtkTreeModel * model,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer user_data UNUSED )
|
|
|
|
{
|
|
|
|
tr_torrent *t;
|
|
|
|
const tr_info *ia, *ib;
|
|
|
|
|
|
|
|
gtk_tree_model_get( model, a, MC_TORRENT_RAW, &t, -1 );
|
|
|
|
ia = tr_torrentInfo( t );
|
|
|
|
gtk_tree_model_get( model, b, MC_TORRENT_RAW, &t, -1 );
|
|
|
|
ib = tr_torrentInfo( t );
|
|
|
|
|
|
|
|
if( ia->totalSize < ib->totalSize ) return 1;
|
|
|
|
if( ia->totalSize > ib->totalSize ) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-19 02:46:30 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
compareByProgress( GtkTreeModel * model,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
2007-12-19 02:46:30 +00:00
|
|
|
gpointer user_data UNUSED )
|
|
|
|
{
|
2009-03-01 00:11:53 +00:00
|
|
|
int ret;
|
|
|
|
tr_torrent * t;
|
2007-12-19 02:46:30 +00:00
|
|
|
const tr_stat *sa, *sb;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-03-01 00:11:53 +00:00
|
|
|
gtk_tree_model_get( model, a, MC_TORRENT_RAW, &t, -1 );
|
|
|
|
sa = tr_torrentStatCached( t );
|
|
|
|
gtk_tree_model_get( model, b, MC_TORRENT_RAW, &t, -1 );
|
|
|
|
sb = tr_torrentStatCached( t );
|
2007-12-19 07:10:47 +00:00
|
|
|
ret = compareDouble( sa->percentDone, sb->percentDone );
|
|
|
|
if( !ret )
|
2008-04-06 14:07:41 +00:00
|
|
|
ret = compareRatio( sa->ratio, sb->ratio );
|
2007-12-19 02:46:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-02-18 06:03:45 +00:00
|
|
|
static int
|
|
|
|
compareByETA( GtkTreeModel * model,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer user_data UNUSED )
|
|
|
|
{
|
|
|
|
tr_torrent *ta, *tb;
|
|
|
|
|
|
|
|
gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 );
|
|
|
|
gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 );
|
|
|
|
|
|
|
|
return compareETA( tr_torrentStatCached( ta )->eta,
|
|
|
|
tr_torrentStatCached( tb )->eta );
|
|
|
|
}
|
|
|
|
|
2007-12-19 07:03:28 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
compareByState( GtkTreeModel * model,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer user_data )
|
2007-12-19 07:03:28 +00:00
|
|
|
{
|
2008-02-04 18:40:56 +00:00
|
|
|
int sa, sb, ret;
|
|
|
|
|
|
|
|
/* first by state */
|
2008-10-20 17:54:56 +00:00
|
|
|
gtk_tree_model_get( model, a, MC_ACTIVITY, &sa, -1 );
|
|
|
|
gtk_tree_model_get( model, b, MC_ACTIVITY, &sb, -1 );
|
2008-02-04 18:40:56 +00:00
|
|
|
ret = sa - sb;
|
|
|
|
|
|
|
|
/* second by progress */
|
|
|
|
if( !ret )
|
|
|
|
ret = compareByProgress( model, a, b, user_data );
|
|
|
|
|
|
|
|
return ret;
|
2007-12-19 07:03:28 +00:00
|
|
|
}
|
|
|
|
|
2007-12-19 02:46:30 +00:00
|
|
|
static int
|
2009-03-01 00:11:53 +00:00
|
|
|
compareByTracker( GtkTreeModel * model,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer user_data UNUSED )
|
2007-12-19 02:46:30 +00:00
|
|
|
{
|
2009-11-26 04:06:49 +00:00
|
|
|
const tr_torrent * ta;
|
|
|
|
const tr_torrent * tb;
|
|
|
|
const tr_info * aInf;
|
|
|
|
const tr_info * bInf;
|
|
|
|
const char * aTracker;
|
|
|
|
const char * bTracker;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-12-19 07:03:28 +00:00
|
|
|
gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 );
|
|
|
|
gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 );
|
2009-11-26 04:06:49 +00:00
|
|
|
|
|
|
|
aInf = tr_torrentInfo( ta );
|
|
|
|
bInf = tr_torrentInfo( tb );
|
|
|
|
aTracker = aInf->trackerCount > 0 ? aInf->trackers[0].announce : NULL;
|
|
|
|
bTracker = bInf->trackerCount > 0 ? bInf->trackers[0].announce : NULL;
|
|
|
|
|
|
|
|
if( !aTracker && !bTracker ) return 0;
|
|
|
|
if( !aTracker ) return -1;
|
|
|
|
if( !bTracker ) return 1;
|
|
|
|
return strcmp( aTracker, bTracker );
|
2007-12-19 02:46:30 +00:00
|
|
|
}
|
|
|
|
|
2007-10-17 18:31:12 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
setSort( TrCore * core,
|
|
|
|
const char * mode,
|
|
|
|
gboolean isReversed )
|
2007-10-17 18:31:12 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const int col = MC_TORRENT_RAW;
|
2008-04-06 14:07:41 +00:00
|
|
|
GtkTreeIterCompareFunc sort_func;
|
2008-09-23 19:11:04 +00:00
|
|
|
GtkSortType type =
|
|
|
|
isReversed ? GTK_SORT_ASCENDING : GTK_SORT_DESCENDING;
|
|
|
|
GtkTreeSortable * sortable =
|
|
|
|
GTK_TREE_SORTABLE( tr_core_model( core ) );
|
2007-12-19 02:46:30 +00:00
|
|
|
|
|
|
|
if( !strcmp( mode, "sort-by-activity" ) )
|
2008-04-06 14:07:41 +00:00
|
|
|
sort_func = compareByActivity;
|
2008-06-02 04:41:55 +00:00
|
|
|
else if( !strcmp( mode, "sort-by-age" ) )
|
|
|
|
sort_func = compareByAge;
|
2007-12-19 02:46:30 +00:00
|
|
|
else if( !strcmp( mode, "sort-by-progress" ) )
|
2008-04-06 14:07:41 +00:00
|
|
|
sort_func = compareByProgress;
|
2009-09-08 18:59:49 +00:00
|
|
|
else if( !strcmp( mode, "sort-by-time-left" ) )
|
2009-02-18 06:03:45 +00:00
|
|
|
sort_func = compareByETA;
|
2008-04-06 14:07:41 +00:00
|
|
|
else if( !strcmp( mode, "sort-by-ratio" ) )
|
|
|
|
sort_func = compareByRatio;
|
2007-12-19 02:46:30 +00:00
|
|
|
else if( !strcmp( mode, "sort-by-state" ) )
|
2008-04-06 14:07:41 +00:00
|
|
|
sort_func = compareByState;
|
2007-12-19 02:46:30 +00:00
|
|
|
else if( !strcmp( mode, "sort-by-tracker" ) )
|
2008-04-06 14:07:41 +00:00
|
|
|
sort_func = compareByTracker;
|
2009-03-01 00:11:53 +00:00
|
|
|
else if( !strcmp( mode, "sort-by-size" ) )
|
|
|
|
sort_func = compareBySize;
|
|
|
|
else {
|
2008-04-06 14:07:41 +00:00
|
|
|
sort_func = compareByName;
|
2007-12-19 02:46:30 +00:00
|
|
|
type = isReversed ? GTK_SORT_DESCENDING : GTK_SORT_ASCENDING;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-04-06 14:07:41 +00:00
|
|
|
gtk_tree_sortable_set_sort_func( sortable, col, sort_func, NULL, NULL );
|
2007-12-19 02:46:30 +00:00
|
|
|
gtk_tree_sortable_set_sort_column_id( sortable, col, type );
|
2007-10-17 18:31:12 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 22:11:36 +00:00
|
|
|
static void
|
|
|
|
tr_core_apply_defaults( tr_ctor * ctor )
|
|
|
|
{
|
|
|
|
if( tr_ctorGetPaused( ctor, TR_FORCE, NULL ) )
|
2010-03-17 19:23:03 +00:00
|
|
|
tr_ctorSetPaused( ctor, TR_FORCE, !pref_flag_get( TR_PREFS_KEY_START ) );
|
2008-03-09 22:11:36 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_ctorGetDeleteSource( ctor, NULL ) )
|
|
|
|
tr_ctorSetDeleteSource( ctor,
|
2010-03-17 19:23:03 +00:00
|
|
|
pref_flag_get( TR_PREFS_KEY_TRASH_ORIGINAL ) );
|
2008-03-09 22:11:36 +00:00
|
|
|
|
2008-05-12 16:33:17 +00:00
|
|
|
if( tr_ctorGetPeerLimit( ctor, TR_FORCE, NULL ) )
|
|
|
|
tr_ctorSetPeerLimit( ctor, TR_FORCE,
|
2008-12-13 23:17:36 +00:00
|
|
|
pref_int_get( TR_PREFS_KEY_PEER_LIMIT_TORRENT ) );
|
2008-03-09 22:11:36 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( tr_ctorGetDownloadDir( ctor, TR_FORCE, NULL ) )
|
|
|
|
{
|
2008-12-13 23:17:36 +00:00
|
|
|
const char * path = pref_string_get( TR_PREFS_KEY_DOWNLOAD_DIR );
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_ctorSetDownloadDir( ctor, TR_FORCE, path );
|
2008-03-09 22:11:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-07 02:03:12 +00:00
|
|
|
static int
|
|
|
|
tr_strcmp( const void * a,
|
|
|
|
const void * b )
|
|
|
|
{
|
|
|
|
if( a && b ) return strcmp( a, b );
|
|
|
|
if( a ) return 1;
|
|
|
|
if( b ) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-11 06:23:31 +00:00
|
|
|
#ifdef HAVE_GIO
|
|
|
|
|
|
|
|
struct watchdir_file
|
|
|
|
{
|
|
|
|
char * filename;
|
|
|
|
time_t mtime;
|
|
|
|
};
|
|
|
|
|
2008-08-22 14:52:20 +00:00
|
|
|
static int
|
2009-10-11 06:23:31 +00:00
|
|
|
compare_watchdir_file_to_filename( const void * a, const void * filename )
|
2008-08-22 14:52:20 +00:00
|
|
|
{
|
2009-10-11 06:23:31 +00:00
|
|
|
return strcmp( ((const struct watchdir_file*)a)->filename, filename );
|
2008-08-22 14:52:20 +00:00
|
|
|
}
|
|
|
|
|
2009-10-11 06:23:31 +00:00
|
|
|
static void
|
|
|
|
watchdir_file_update_mtime( struct watchdir_file * file )
|
|
|
|
{
|
|
|
|
GFile * gfile = g_file_new_for_path( file->filename );
|
|
|
|
GFileInfo * info = g_file_query_info( gfile, G_FILE_ATTRIBUTE_TIME_MODIFIED, 0, NULL, NULL );
|
|
|
|
|
|
|
|
file->mtime = g_file_info_get_attribute_uint64( info, G_FILE_ATTRIBUTE_TIME_MODIFIED );
|
|
|
|
|
|
|
|
g_object_unref( G_OBJECT( info ) );
|
|
|
|
g_object_unref( G_OBJECT( gfile ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct watchdir_file*
|
|
|
|
watchdir_file_new( const char * filename )
|
|
|
|
{
|
|
|
|
struct watchdir_file * f;
|
|
|
|
|
|
|
|
f = g_new( struct watchdir_file, 1 );
|
|
|
|
f->filename = g_strdup( filename );
|
|
|
|
watchdir_file_update_mtime( f );
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
watchdir_file_free( struct watchdir_file * f )
|
|
|
|
{
|
|
|
|
g_free( f->filename );
|
|
|
|
g_free( f );
|
|
|
|
}
|
2010-01-05 23:47:50 +00:00
|
|
|
|
2008-03-09 22:11:36 +00:00
|
|
|
static gboolean
|
2008-03-12 02:11:37 +00:00
|
|
|
watchFolderIdle( gpointer gcore )
|
2008-03-09 22:11:36 +00:00
|
|
|
{
|
2009-10-11 06:23:31 +00:00
|
|
|
GSList * l;
|
|
|
|
GSList * addme = NULL;
|
|
|
|
GSList * monitor_files = NULL;
|
2008-03-18 01:22:11 +00:00
|
|
|
TrCore * core = TR_CORE( gcore );
|
2009-10-11 06:23:31 +00:00
|
|
|
const time_t now = time( NULL );
|
|
|
|
struct TrCorePrivate * p = core->priv;
|
|
|
|
|
|
|
|
/* of the monitor_files, make a list of those that haven't
|
|
|
|
* changed lately, since they should be ready to add */
|
|
|
|
for( l=p->monitor_files; l!=NULL; l=l->next ) {
|
|
|
|
struct watchdir_file * f = l->data;
|
|
|
|
watchdir_file_update_mtime( f );
|
|
|
|
if( f->mtime + 2 >= now )
|
|
|
|
monitor_files = g_slist_prepend( monitor_files, f );
|
|
|
|
else {
|
|
|
|
addme = g_slist_prepend( addme, g_strdup( f->filename ) );
|
|
|
|
watchdir_file_free( f );
|
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-10-11 06:23:31 +00:00
|
|
|
/* add the torrents from that list */
|
2008-10-27 20:12:53 +00:00
|
|
|
core->priv->adding_from_watch_dir = TRUE;
|
2009-10-11 06:23:31 +00:00
|
|
|
tr_core_add_list_defaults( core, addme, TRUE );
|
2008-10-27 20:12:53 +00:00
|
|
|
core->priv->adding_from_watch_dir = FALSE;
|
2008-03-12 02:11:37 +00:00
|
|
|
|
2009-10-11 06:23:31 +00:00
|
|
|
/* update the monitor_files list */
|
|
|
|
g_slist_free( p->monitor_files );
|
|
|
|
p->monitor_files = monitor_files;
|
|
|
|
|
|
|
|
/* if monitor_files is nonempty, keep checking every second */
|
|
|
|
if( core->priv->monitor_files )
|
|
|
|
return TRUE;
|
2008-03-12 02:11:37 +00:00
|
|
|
core->priv->monitor_idle_tag = 0;
|
|
|
|
return FALSE;
|
2009-10-11 06:23:31 +00:00
|
|
|
|
2008-03-09 22:11:36 +00:00
|
|
|
}
|
|
|
|
|
2008-03-13 01:06:54 +00:00
|
|
|
static void
|
2009-10-11 06:23:31 +00:00
|
|
|
maybeAddTorrent( TrCore * core, const char * filename )
|
2008-03-13 01:06:54 +00:00
|
|
|
{
|
|
|
|
const gboolean isTorrent = g_str_has_suffix( filename, ".torrent" );
|
|
|
|
|
|
|
|
if( isTorrent )
|
|
|
|
{
|
|
|
|
struct TrCorePrivate * p = core->priv;
|
|
|
|
|
2009-10-11 06:23:31 +00:00
|
|
|
if( !g_slist_find_custom( p->monitor_files, filename, (GCompareFunc)compare_watchdir_file_to_filename ) )
|
|
|
|
p->monitor_files = g_slist_append( p->monitor_files, watchdir_file_new( filename ) );
|
|
|
|
|
2008-03-13 01:06:54 +00:00
|
|
|
if( !p->monitor_idle_tag )
|
2008-12-22 05:39:03 +00:00
|
|
|
p->monitor_idle_tag = gtr_timeout_add_seconds( 1, watchFolderIdle, core );
|
2008-03-13 01:06:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-09 15:27:08 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
watchFolderChanged( GFileMonitor * monitor UNUSED,
|
|
|
|
GFile * file,
|
2008-03-09 15:27:08 +00:00
|
|
|
GFile * other_type UNUSED,
|
2008-09-23 19:11:04 +00:00
|
|
|
GFileMonitorEvent event_type,
|
|
|
|
gpointer core )
|
2008-03-09 15:27:08 +00:00
|
|
|
{
|
|
|
|
if( event_type == G_FILE_MONITOR_EVENT_CREATED )
|
|
|
|
{
|
|
|
|
char * filename = g_file_get_path( file );
|
2008-03-13 01:06:54 +00:00
|
|
|
maybeAddTorrent( core, filename );
|
2008-03-09 15:27:08 +00:00
|
|
|
g_free( filename );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
scanWatchDir( TrCore * core )
|
|
|
|
{
|
|
|
|
const gboolean isEnabled = pref_flag_get( PREF_KEY_DIR_WATCH_ENABLED );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-03-09 15:27:08 +00:00
|
|
|
if( isEnabled )
|
|
|
|
{
|
2009-05-14 17:18:17 +00:00
|
|
|
const char * dirname = pref_string_get( PREF_KEY_DIR_WATCH );
|
|
|
|
GDir * dir = g_dir_open( dirname, 0, NULL );
|
|
|
|
|
2010-01-05 23:23:26 +00:00
|
|
|
if( dir != NULL )
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2010-01-05 23:23:26 +00:00
|
|
|
const char * basename;
|
|
|
|
while(( basename = g_dir_read_name( dir )))
|
|
|
|
{
|
|
|
|
char * filename = g_build_filename( dirname, basename, NULL );
|
|
|
|
maybeAddTorrent( core, filename );
|
|
|
|
g_free( filename );
|
|
|
|
}
|
|
|
|
|
|
|
|
g_dir_close( dir );
|
2008-03-13 01:06:54 +00:00
|
|
|
}
|
2008-03-09 15:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
updateWatchDir( TrCore * core )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * filename = pref_string_get( PREF_KEY_DIR_WATCH );
|
|
|
|
const gboolean isEnabled = pref_flag_get(
|
|
|
|
PREF_KEY_DIR_WATCH_ENABLED );
|
2008-03-12 02:11:37 +00:00
|
|
|
struct TrCorePrivate * p = TR_CORE( core )->priv;
|
2008-03-09 15:27:08 +00:00
|
|
|
|
2008-03-12 02:11:37 +00:00
|
|
|
if( p->monitor && ( !isEnabled || tr_strcmp( filename, p->monitor_path ) ) )
|
2008-03-09 15:27:08 +00:00
|
|
|
{
|
2008-03-12 02:11:37 +00:00
|
|
|
g_signal_handler_disconnect( p->monitor, p->monitor_tag );
|
|
|
|
g_free( p->monitor_path );
|
|
|
|
g_file_monitor_cancel( p->monitor );
|
|
|
|
g_object_unref( G_OBJECT( p->monitor ) );
|
|
|
|
p->monitor_path = NULL;
|
|
|
|
p->monitor = NULL;
|
|
|
|
p->monitor_tag = 0;
|
2008-03-09 15:27:08 +00:00
|
|
|
}
|
2008-03-09 22:11:36 +00:00
|
|
|
|
2008-03-12 02:11:37 +00:00
|
|
|
if( isEnabled && !p->monitor )
|
2008-03-09 15:27:08 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
GFile * file = g_file_new_for_path( filename );
|
2008-03-09 15:27:08 +00:00
|
|
|
GFileMonitor * m = g_file_monitor_directory( file, 0, NULL, NULL );
|
|
|
|
scanWatchDir( core );
|
2008-03-12 02:11:37 +00:00
|
|
|
p->monitor = m;
|
|
|
|
p->monitor_path = g_strdup( filename );
|
|
|
|
p->monitor_tag = g_signal_connect( m, "changed",
|
2008-09-23 19:11:04 +00:00
|
|
|
G_CALLBACK(
|
|
|
|
watchFolderChanged ), core );
|
2008-03-09 15:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-03-09 15:27:08 +00:00
|
|
|
#endif
|
|
|
|
|
2007-12-19 18:42:33 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
prefsChanged( TrCore * core,
|
|
|
|
const char * key,
|
|
|
|
gpointer data UNUSED )
|
2007-12-19 02:46:30 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !strcmp( key, PREF_KEY_SORT_MODE )
|
|
|
|
|| !strcmp( key, PREF_KEY_SORT_REVERSED ) )
|
2007-12-19 18:42:33 +00:00
|
|
|
{
|
2008-06-12 16:25:36 +00:00
|
|
|
const char * mode = pref_string_get( PREF_KEY_SORT_MODE );
|
2008-09-23 19:11:04 +00:00
|
|
|
gboolean isReversed = pref_flag_get( PREF_KEY_SORT_REVERSED );
|
2007-12-19 18:42:33 +00:00
|
|
|
setSort( core, mode, isReversed );
|
|
|
|
}
|
2008-12-13 23:17:36 +00:00
|
|
|
else if( !strcmp( key, TR_PREFS_KEY_PEER_LIMIT_GLOBAL ) )
|
2007-12-22 00:06:38 +00:00
|
|
|
{
|
|
|
|
const uint16_t val = pref_int_get( key );
|
2008-10-28 19:49:33 +00:00
|
|
|
tr_sessionSetPeerLimit( tr_core_session( core ), val );
|
2007-12-22 00:06:38 +00:00
|
|
|
}
|
2008-12-14 22:57:08 +00:00
|
|
|
else if( !strcmp( key, TR_PREFS_KEY_PEER_LIMIT_TORRENT ) )
|
|
|
|
{
|
|
|
|
const uint16_t val = pref_int_get( key );
|
|
|
|
tr_sessionSetPeerLimitPerTorrent( tr_core_session( core ), val );
|
|
|
|
}
|
2008-10-08 18:26:55 +00:00
|
|
|
else if( !strcmp( key, PREF_KEY_INHIBIT_HIBERNATION ) )
|
2008-05-24 23:23:20 +00:00
|
|
|
{
|
2008-07-02 18:49:07 +00:00
|
|
|
maybeInhibitHibernation( core );
|
2008-05-24 23:23:20 +00:00
|
|
|
}
|
2008-03-09 15:27:08 +00:00
|
|
|
#ifdef HAVE_GIO
|
2008-09-23 19:11:04 +00:00
|
|
|
else if( !strcmp( key, PREF_KEY_DIR_WATCH )
|
|
|
|
|| !strcmp( key, PREF_KEY_DIR_WATCH_ENABLED ) )
|
2008-03-09 15:27:08 +00:00
|
|
|
{
|
|
|
|
updateWatchDir( core );
|
|
|
|
}
|
|
|
|
#endif
|
2007-10-17 18:31:12 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_core_init( GTypeInstance * instance,
|
|
|
|
gpointer g_class UNUSED )
|
2007-05-23 00:49:31 +00:00
|
|
|
{
|
2009-08-07 20:49:06 +00:00
|
|
|
GtkListStore * store;
|
2008-02-09 17:29:05 +00:00
|
|
|
struct TrCorePrivate * p;
|
2009-08-07 20:49:06 +00:00
|
|
|
TrCore * self = (TrCore *) instance;
|
2007-05-23 00:49:31 +00:00
|
|
|
|
|
|
|
/* column types for the model used to store torrent information */
|
|
|
|
/* keep this in sync with the enum near the bottom of tr_core.h */
|
2009-08-07 20:49:06 +00:00
|
|
|
GType types[] = { G_TYPE_STRING, /* name */
|
|
|
|
G_TYPE_STRING, /* collated name */
|
|
|
|
TR_TORRENT_TYPE, /* TrTorrent object */
|
|
|
|
G_TYPE_POINTER, /* tr_torrent* */
|
2009-08-12 13:42:49 +00:00
|
|
|
G_TYPE_DOUBLE, /* tr_stat.pieceUploadSpeed */
|
|
|
|
G_TYPE_DOUBLE, /* tr_stat.pieceDownloadSpeed */
|
2009-08-07 20:49:06 +00:00
|
|
|
G_TYPE_INT }; /* tr_stat.status */
|
2007-05-23 00:49:31 +00:00
|
|
|
|
2008-02-09 17:29:05 +00:00
|
|
|
p = self->priv = G_TYPE_INSTANCE_GET_PRIVATE( self,
|
|
|
|
TR_CORE_TYPE,
|
|
|
|
struct TrCorePrivate );
|
|
|
|
|
2007-05-23 00:49:31 +00:00
|
|
|
/* create the model used to store torrent data */
|
2009-07-22 15:55:48 +00:00
|
|
|
g_assert( G_N_ELEMENTS( types ) == MC_ROW_COUNT );
|
2007-05-23 00:49:31 +00:00
|
|
|
store = gtk_list_store_newv( MC_ROW_COUNT, types );
|
2007-10-16 14:16:01 +00:00
|
|
|
|
2008-02-09 17:29:05 +00:00
|
|
|
p->model = GTK_TREE_MODEL( store );
|
2008-05-18 16:44:30 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_DBUS_GLIB
|
2008-05-28 15:05:17 +00:00
|
|
|
if( our_instance_adds_remote_torrents )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
|
|
|
DBusGConnection * bus = dbus_g_bus_get( DBUS_BUS_SESSION, NULL );
|
|
|
|
if( bus )
|
2008-09-23 19:11:04 +00:00
|
|
|
dbus_g_connection_register_g_object(
|
|
|
|
bus,
|
|
|
|
"/com/transmissionbt/Transmission",
|
|
|
|
G_OBJECT( self ) );
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
#endif
|
2007-05-23 00:49:31 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
GType
|
|
|
|
tr_core_get_type( void )
|
2007-05-23 00:49:31 +00:00
|
|
|
{
|
2007-10-16 14:16:01 +00:00
|
|
|
static GType type = 0;
|
2007-05-23 06:25:15 +00:00
|
|
|
|
2007-12-20 01:33:51 +00:00
|
|
|
if( !type )
|
2007-05-23 04:39:06 +00:00
|
|
|
{
|
2007-10-16 14:16:01 +00:00
|
|
|
static const GTypeInfo info =
|
|
|
|
{
|
|
|
|
sizeof( TrCoreClass ),
|
2008-09-23 19:11:04 +00:00
|
|
|
NULL, /* base_init */
|
|
|
|
NULL, /* base_finalize */
|
|
|
|
tr_core_class_init, /* class_init */
|
|
|
|
NULL, /* class_finalize */
|
|
|
|
NULL, /* class_data */
|
2007-10-16 14:16:01 +00:00
|
|
|
sizeof( TrCore ),
|
2008-09-23 19:11:04 +00:00
|
|
|
0, /* n_preallocs */
|
|
|
|
tr_core_init, /* instance_init */
|
2007-10-16 14:16:01 +00:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
type = g_type_register_static( G_TYPE_OBJECT, "TrCore", &info, 0 );
|
2007-05-23 04:39:06 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
return type;
|
2007-05-23 00:49:31 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-05-23 00:49:31 +00:00
|
|
|
TrCore *
|
2008-10-28 19:49:33 +00:00
|
|
|
tr_core_new( tr_session * session )
|
2007-05-23 00:49:31 +00:00
|
|
|
{
|
2007-12-19 18:42:33 +00:00
|
|
|
TrCore * core = TR_CORE( g_object_new( TR_CORE_TYPE, NULL ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-10-28 19:49:33 +00:00
|
|
|
core->priv->session = session;
|
2007-12-19 19:16:36 +00:00
|
|
|
|
|
|
|
/* init from prefs & listen to pref changes */
|
|
|
|
prefsChanged( core, PREF_KEY_SORT_MODE, NULL );
|
|
|
|
prefsChanged( core, PREF_KEY_SORT_REVERSED, NULL );
|
2008-03-09 15:27:08 +00:00
|
|
|
prefsChanged( core, PREF_KEY_DIR_WATCH_ENABLED, NULL );
|
2008-12-13 23:17:36 +00:00
|
|
|
prefsChanged( core, TR_PREFS_KEY_PEER_LIMIT_GLOBAL, NULL );
|
2008-10-08 18:26:55 +00:00
|
|
|
prefsChanged( core, PREF_KEY_INHIBIT_HIBERNATION, NULL );
|
2008-12-13 23:17:36 +00:00
|
|
|
g_signal_connect( core, "prefs-changed", G_CALLBACK( prefsChanged ), NULL );
|
2007-12-19 19:16:36 +00:00
|
|
|
|
2007-12-19 18:42:33 +00:00
|
|
|
return core;
|
2007-05-23 00:49:31 +00:00
|
|
|
}
|
|
|
|
|
2008-04-17 20:35:18 +00:00
|
|
|
void
|
|
|
|
tr_core_close( TrCore * core )
|
|
|
|
{
|
2008-10-28 19:49:33 +00:00
|
|
|
tr_session * session = tr_core_session( core );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-10-28 19:49:33 +00:00
|
|
|
if( session )
|
2008-04-17 20:35:18 +00:00
|
|
|
{
|
2008-10-28 19:49:33 +00:00
|
|
|
core->priv->session = NULL;
|
2008-12-13 23:17:36 +00:00
|
|
|
pref_save( session );
|
2008-10-28 19:49:33 +00:00
|
|
|
tr_sessionClose( session );
|
2008-04-17 20:35:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-23 00:49:31 +00:00
|
|
|
GtkTreeModel *
|
2008-02-09 17:29:05 +00:00
|
|
|
tr_core_model( TrCore * core )
|
2007-05-23 00:49:31 +00:00
|
|
|
{
|
2008-02-09 17:29:05 +00:00
|
|
|
return isDisposed( core ) ? NULL : core->priv->model;
|
2007-05-23 00:49:31 +00:00
|
|
|
}
|
2007-05-23 01:47:42 +00:00
|
|
|
|
2008-10-28 19:49:33 +00:00
|
|
|
tr_session *
|
|
|
|
tr_core_session( TrCore * core )
|
2007-05-23 01:47:42 +00:00
|
|
|
{
|
2008-10-28 19:49:33 +00:00
|
|
|
return isDisposed( core ) ? NULL : core->priv->session;
|
2008-02-09 17:29:05 +00:00
|
|
|
}
|
|
|
|
|
2007-12-19 02:46:30 +00:00
|
|
|
static char*
|
|
|
|
doCollate( const char * in )
|
|
|
|
{
|
2010-02-06 05:21:25 +00:00
|
|
|
char * ret;
|
|
|
|
char * casefold;
|
|
|
|
const char * end = in ? in + strlen( in ) : NULL;
|
2007-12-19 02:46:30 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
while( in < end )
|
|
|
|
{
|
2007-12-19 02:46:30 +00:00
|
|
|
const gunichar ch = g_utf8_get_char( in );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !g_unichar_isalnum ( ch ) ) /* eat everything before the first alnum
|
|
|
|
*/
|
2007-12-19 02:46:30 +00:00
|
|
|
in += g_unichar_to_utf8( ch, NULL );
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( in == end )
|
|
|
|
return g_strdup ( "" );
|
2007-12-19 02:46:30 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
casefold = g_utf8_casefold( in, end - in );
|
2007-12-19 02:46:30 +00:00
|
|
|
ret = g_utf8_collate_key( casefold, -1 );
|
|
|
|
g_free( casefold );
|
2008-02-14 17:18:00 +00:00
|
|
|
|
2007-12-19 02:46:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-02-13 03:00:21 +00:00
|
|
|
void
|
2009-06-11 16:17:48 +00:00
|
|
|
tr_core_add_torrent( TrCore * self,
|
|
|
|
TrTorrent * gtor,
|
|
|
|
gboolean doNotify )
|
2007-10-16 14:16:01 +00:00
|
|
|
{
|
2008-05-18 16:44:30 +00:00
|
|
|
const tr_info * inf = tr_torrent_info( gtor );
|
2009-08-07 20:49:06 +00:00
|
|
|
const tr_stat * st = tr_torrent_stat( gtor );
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrent * tor = tr_torrent_handle( gtor );
|
|
|
|
char * collated = doCollate( inf->name );
|
|
|
|
GtkListStore * store = GTK_LIST_STORE( tr_core_model( self ) );
|
|
|
|
GtkTreeIter unused;
|
2008-02-13 03:00:21 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
gtk_list_store_insert_with_values( store, &unused, 0,
|
2007-12-19 02:46:30 +00:00
|
|
|
MC_NAME, inf->name,
|
|
|
|
MC_NAME_COLLATED, collated,
|
2008-05-18 16:44:30 +00:00
|
|
|
MC_TORRENT, gtor,
|
|
|
|
MC_TORRENT_RAW, tor,
|
2009-08-12 13:42:49 +00:00
|
|
|
MC_SPEED_UP, st->pieceUploadSpeed,
|
|
|
|
MC_SPEED_DOWN, st->pieceDownloadSpeed,
|
2009-08-07 20:49:06 +00:00
|
|
|
MC_ACTIVITY, st->activity,
|
2008-09-23 19:11:04 +00:00
|
|
|
-1 );
|
2008-02-13 03:00:21 +00:00
|
|
|
|
2009-06-11 16:17:48 +00:00
|
|
|
if( doNotify )
|
|
|
|
tr_notify_added( inf->name );
|
|
|
|
|
2008-02-13 03:00:21 +00:00
|
|
|
/* cleanup */
|
2008-05-18 16:44:30 +00:00
|
|
|
g_object_unref( G_OBJECT( gtor ) );
|
2007-12-19 02:46:30 +00:00
|
|
|
g_free( collated );
|
2007-10-16 14:16:01 +00:00
|
|
|
}
|
|
|
|
|
2007-05-23 02:45:28 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_core_load( TrCore * self,
|
|
|
|
gboolean forcePaused )
|
2007-05-23 01:47:42 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int i;
|
|
|
|
int count = 0;
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_torrent ** torrents;
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ctor * ctor;
|
2007-05-23 02:45:28 +00:00
|
|
|
|
2008-10-28 19:49:33 +00:00
|
|
|
ctor = tr_ctorNew( tr_core_session( self ) );
|
2008-02-08 03:00:19 +00:00
|
|
|
if( forcePaused )
|
|
|
|
tr_ctorSetPaused( ctor, TR_FORCE, TRUE );
|
2008-05-12 16:33:17 +00:00
|
|
|
tr_ctorSetPeerLimit( ctor, TR_FALLBACK,
|
2008-12-13 23:17:36 +00:00
|
|
|
pref_int_get( TR_PREFS_KEY_PEER_LIMIT_TORRENT ) );
|
2007-12-21 22:18:40 +00:00
|
|
|
|
2008-10-28 19:49:33 +00:00
|
|
|
torrents = tr_sessionLoadTorrents ( tr_core_session( self ), ctor, &count );
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < count; ++i )
|
2009-06-11 16:17:48 +00:00
|
|
|
tr_core_add_torrent( self, tr_torrent_new_preexisting( torrents[i] ), FALSE );
|
2007-05-23 02:45:28 +00:00
|
|
|
|
2007-12-21 22:18:40 +00:00
|
|
|
tr_free( torrents );
|
|
|
|
tr_ctorFree( ctor );
|
|
|
|
|
2007-05-23 02:45:28 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2009-04-07 20:13:08 +00:00
|
|
|
static void
|
|
|
|
emitBlocklistUpdated( TrCore * core, int ruleCount )
|
|
|
|
{
|
|
|
|
g_signal_emit( core, TR_CORE_GET_CLASS( core )->blocklistSignal, 0, ruleCount );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
emitPortTested( TrCore * core, gboolean isOpen )
|
2008-09-02 20:59:00 +00:00
|
|
|
{
|
2009-04-07 20:13:08 +00:00
|
|
|
g_signal_emit( core, TR_CORE_GET_CLASS( core )->portSignal, 0, isOpen );
|
2008-09-02 20:59:00 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_core_errsig( TrCore * core,
|
|
|
|
enum tr_core_err type,
|
|
|
|
const char * msg )
|
2007-10-16 14:16:01 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
g_signal_emit( core, TR_CORE_GET_CLASS( core )->errsig, 0, type, msg );
|
2007-10-16 14:16:01 +00:00
|
|
|
}
|
|
|
|
|
2009-06-11 14:51:21 +00:00
|
|
|
static int
|
2009-06-11 16:17:48 +00:00
|
|
|
add_ctor( TrCore * core, tr_ctor * ctor, gboolean doPrompt, gboolean doNotify )
|
2009-06-11 14:51:21 +00:00
|
|
|
{
|
|
|
|
tr_info inf;
|
|
|
|
int err = tr_torrentParse( ctor, &inf );
|
|
|
|
|
|
|
|
switch( err )
|
|
|
|
{
|
2009-08-05 01:59:16 +00:00
|
|
|
case TR_PARSE_ERR:
|
2009-06-11 14:51:21 +00:00
|
|
|
break;
|
|
|
|
|
2009-08-05 01:59:16 +00:00
|
|
|
case TR_PARSE_DUPLICATE:
|
2009-06-11 14:51:21 +00:00
|
|
|
/* don't complain about .torrent files in the watch directory
|
|
|
|
* that have already been added... that gets annoying and we
|
|
|
|
* don't want to be nagging users to clean up their watch dirs */
|
|
|
|
if( !tr_ctorGetSourceFile(ctor) || !core->priv->adding_from_watch_dir )
|
|
|
|
tr_core_errsig( core, err, inf.name );
|
|
|
|
tr_metainfoFree( &inf );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if( doPrompt )
|
|
|
|
g_signal_emit( core, TR_CORE_GET_CLASS( core )->promptsig, 0, ctor );
|
|
|
|
else {
|
|
|
|
tr_session * session = tr_core_session( core );
|
|
|
|
TrTorrent * gtor = tr_torrent_new_ctor( session, ctor, &err );
|
|
|
|
if( !err )
|
2009-06-11 16:17:48 +00:00
|
|
|
tr_core_add_torrent( core, gtor, doNotify );
|
2009-06-11 14:51:21 +00:00
|
|
|
}
|
|
|
|
tr_metainfoFree( &inf );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2009-09-17 01:28:45 +00:00
|
|
|
void
|
|
|
|
tr_core_add_ctor( TrCore * core, tr_ctor * ctor )
|
|
|
|
{
|
2010-03-17 19:23:03 +00:00
|
|
|
const gboolean doStart = pref_flag_get( TR_PREFS_KEY_START );
|
2009-09-17 01:28:45 +00:00
|
|
|
const gboolean doPrompt = pref_flag_get( PREF_KEY_OPTIONS_PROMPT );
|
|
|
|
tr_core_apply_defaults( ctor );
|
|
|
|
add_ctor( core, ctor, doStart, doPrompt );
|
|
|
|
}
|
|
|
|
|
2009-06-11 14:51:21 +00:00
|
|
|
/* invoked remotely via dbus. */
|
|
|
|
gboolean
|
|
|
|
tr_core_add_metainfo( TrCore * core,
|
2009-11-29 20:35:48 +00:00
|
|
|
const char * payload,
|
2010-06-03 19:53:01 +00:00
|
|
|
const char * filename,
|
2009-12-07 04:06:14 +00:00
|
|
|
gboolean * setme_handled,
|
2009-06-11 14:51:21 +00:00
|
|
|
GError ** gerr UNUSED )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2008-10-28 19:49:33 +00:00
|
|
|
tr_session * session = tr_core_session( core );
|
2008-05-18 16:44:30 +00:00
|
|
|
|
2009-06-11 14:51:21 +00:00
|
|
|
if( !session )
|
|
|
|
{
|
2009-12-07 04:06:14 +00:00
|
|
|
*setme_handled = FALSE;
|
2009-06-11 14:51:21 +00:00
|
|
|
}
|
2009-11-29 20:35:48 +00:00
|
|
|
else if( gtr_is_supported_url( payload ) || gtr_is_magnet_link( payload ) )
|
|
|
|
{
|
|
|
|
tr_core_add_from_url( core, payload );
|
2009-12-07 04:06:14 +00:00
|
|
|
*setme_handled = TRUE;
|
2009-11-29 20:35:48 +00:00
|
|
|
}
|
2010-06-03 19:53:01 +00:00
|
|
|
else
|
2009-06-11 14:51:21 +00:00
|
|
|
{
|
|
|
|
tr_ctor * ctor;
|
2010-06-03 19:53:01 +00:00
|
|
|
gboolean has_metainfo = FALSE;
|
|
|
|
const gboolean do_prompt = pref_flag_get( PREF_KEY_OPTIONS_PROMPT );
|
2009-06-11 14:51:21 +00:00
|
|
|
|
2010-06-03 19:53:01 +00:00
|
|
|
/* create the constructor */
|
2009-06-11 14:51:21 +00:00
|
|
|
ctor = tr_ctorNew( session );
|
|
|
|
tr_core_apply_defaults( ctor );
|
|
|
|
|
2010-06-03 19:53:01 +00:00
|
|
|
if( !has_metainfo && g_file_test( filename, G_FILE_TEST_IS_REGULAR ) )
|
|
|
|
{
|
|
|
|
/* set the metainfo from a local file */
|
|
|
|
has_metainfo = !tr_ctorSetMetainfoFromFile( ctor, filename );
|
|
|
|
}
|
2009-06-11 14:51:21 +00:00
|
|
|
|
2010-06-03 19:53:01 +00:00
|
|
|
if( !has_metainfo )
|
|
|
|
{
|
|
|
|
/* base64-encoded metainfo */
|
|
|
|
int file_length;
|
|
|
|
char * file_contents = tr_base64_decode( payload, -1, &file_length );
|
|
|
|
has_metainfo = !tr_ctorSetMetainfo( ctor, (const uint8_t*)file_contents, file_length );
|
|
|
|
tr_free( file_contents );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( has_metainfo )
|
|
|
|
{
|
|
|
|
add_ctor( core, ctor, do_prompt, TRUE );
|
|
|
|
tr_core_torrents_added( core );
|
|
|
|
}
|
|
|
|
|
|
|
|
*setme_handled = has_metainfo;
|
2009-06-11 14:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-11-22 16:20:22 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
struct url_dialog_data
|
|
|
|
{
|
|
|
|
TrCore * core;
|
|
|
|
tr_ctor * ctor;
|
|
|
|
GtkDialog * dialog;
|
|
|
|
};
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
onURLDoneIdle( gpointer vdata )
|
|
|
|
{
|
|
|
|
struct url_dialog_data * data = vdata;
|
|
|
|
tr_core_add_ctor( data->core, data->ctor );
|
|
|
|
g_free( data );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
onURLDone( tr_session * session,
|
|
|
|
long response_code UNUSED,
|
|
|
|
const void * response,
|
|
|
|
size_t response_byte_count,
|
|
|
|
void * vdata )
|
|
|
|
{
|
|
|
|
struct url_dialog_data * data = vdata;
|
|
|
|
tr_ctor * ctor = tr_ctorNew( session );
|
|
|
|
|
|
|
|
/* FIME: error dialog */
|
|
|
|
|
|
|
|
if( tr_ctorSetMetainfo( ctor, response, response_byte_count ) )
|
|
|
|
{
|
|
|
|
tr_ctorFree( ctor );
|
|
|
|
g_free( data );
|
|
|
|
}
|
|
|
|
else /* move the work back to the gtk thread */
|
|
|
|
{
|
|
|
|
data->ctor = ctor;
|
|
|
|
gtr_idle_add( onURLDoneIdle, data );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_core_add_from_url( TrCore * core, const char * url )
|
|
|
|
{
|
2009-11-24 02:16:31 +00:00
|
|
|
tr_session * session = tr_core_session( core );
|
2010-02-05 13:59:44 +00:00
|
|
|
const gboolean is_magnet_link = gtr_is_magnet_link( url );
|
2009-11-24 02:16:31 +00:00
|
|
|
|
2010-02-05 13:59:44 +00:00
|
|
|
if( is_magnet_link || gtr_is_hex_hashcode( url ) )
|
2009-11-22 16:20:22 +00:00
|
|
|
{
|
2009-11-29 07:42:39 +00:00
|
|
|
int err;
|
|
|
|
char * tmp = NULL;
|
2009-11-24 02:16:31 +00:00
|
|
|
tr_ctor * ctor = tr_ctorNew( session );
|
2009-11-29 07:42:39 +00:00
|
|
|
|
|
|
|
if( gtr_is_hex_hashcode( url ) )
|
|
|
|
url = tmp = g_strdup_printf( "magnet:?xt=urn:btih:%s", url );
|
|
|
|
|
2010-02-02 22:45:22 +00:00
|
|
|
err = tr_ctorSetMetainfoFromMagnetLink( ctor, url );
|
2009-11-29 07:42:39 +00:00
|
|
|
|
2010-05-13 13:12:27 +00:00
|
|
|
if( !err )
|
|
|
|
tr_core_add_ctor( core, ctor );
|
|
|
|
else {
|
2010-02-05 13:59:44 +00:00
|
|
|
gtr_unrecognized_url_dialog( NULL, url );
|
|
|
|
tr_ctorFree( ctor );
|
|
|
|
}
|
2009-11-29 07:42:39 +00:00
|
|
|
|
|
|
|
g_free( tmp );
|
2009-11-22 16:20:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct url_dialog_data * data = g_new( struct url_dialog_data, 1 );
|
|
|
|
data->core = core;
|
2009-11-24 02:16:31 +00:00
|
|
|
tr_webRun( session, url, NULL, onURLDone, data );
|
2009-11-22 16:20:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-06-11 14:51:21 +00:00
|
|
|
static void
|
|
|
|
add_filename( TrCore * core,
|
|
|
|
const char * filename,
|
|
|
|
gboolean doStart,
|
2009-06-11 16:17:48 +00:00
|
|
|
gboolean doPrompt,
|
|
|
|
gboolean doNotify )
|
2009-06-11 14:51:21 +00:00
|
|
|
{
|
|
|
|
tr_session * session = tr_core_session( core );
|
2009-11-22 16:20:22 +00:00
|
|
|
|
|
|
|
if( session == NULL )
|
|
|
|
return;
|
|
|
|
|
2009-11-22 22:53:31 +00:00
|
|
|
if( gtr_is_supported_url( filename ) || gtr_is_magnet_link( filename ) )
|
2009-11-22 16:20:22 +00:00
|
|
|
{
|
|
|
|
tr_core_add_from_url( core, filename );
|
|
|
|
}
|
2009-11-29 08:29:57 +00:00
|
|
|
else if( g_file_test( filename, G_FILE_TEST_EXISTS ) )
|
2008-05-18 16:44:30 +00:00
|
|
|
{
|
2009-06-11 14:51:21 +00:00
|
|
|
int err;
|
2009-11-22 16:20:22 +00:00
|
|
|
|
2009-05-14 17:18:17 +00:00
|
|
|
tr_ctor * ctor = tr_ctorNew( session );
|
2009-11-22 16:20:22 +00:00
|
|
|
tr_ctorSetMetainfoFromFile( ctor, filename );
|
2008-05-18 16:44:30 +00:00
|
|
|
tr_core_apply_defaults( ctor );
|
|
|
|
tr_ctorSetPaused( ctor, TR_FORCE, !doStart );
|
2008-12-03 06:01:38 +00:00
|
|
|
|
2009-06-11 16:17:48 +00:00
|
|
|
err = add_ctor( core, ctor, doPrompt, doNotify );
|
2009-08-05 01:59:16 +00:00
|
|
|
if( err == TR_PARSE_ERR )
|
|
|
|
tr_core_errsig( core, TR_PARSE_ERR, filename );
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
2009-11-29 08:29:57 +00:00
|
|
|
else if( gtr_is_hex_hashcode( filename ) )
|
|
|
|
{
|
|
|
|
tr_core_add_from_url( core, filename );
|
|
|
|
}
|
2008-05-18 16:44:30 +00:00
|
|
|
}
|
|
|
|
|
2008-08-17 12:39:26 +00:00
|
|
|
gboolean
|
|
|
|
tr_core_present_window( TrCore * core UNUSED,
|
2008-09-23 19:11:04 +00:00
|
|
|
gboolean * success,
|
|
|
|
GError ** err UNUSED )
|
2008-08-17 12:39:26 +00:00
|
|
|
{
|
2010-04-17 00:41:24 +00:00
|
|
|
/* Setting the toggle-main-window GtkCheckMenuItem to
|
|
|
|
make sure its state is correctly set */
|
|
|
|
action_toggle( "toggle-main-window", TRUE);
|
|
|
|
|
2008-08-17 12:39:26 +00:00
|
|
|
*success = TRUE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-02-13 03:00:21 +00:00
|
|
|
void
|
2009-06-11 16:17:48 +00:00
|
|
|
tr_core_add_list( TrCore * core,
|
|
|
|
GSList * torrentFiles,
|
|
|
|
pref_flag_t start,
|
|
|
|
pref_flag_t prompt,
|
|
|
|
gboolean doNotify )
|
2007-05-24 10:37:07 +00:00
|
|
|
{
|
2010-03-17 19:23:03 +00:00
|
|
|
const gboolean doStart = pref_flag_eval( start, TR_PREFS_KEY_START );
|
2008-10-20 18:35:12 +00:00
|
|
|
const gboolean doPrompt = pref_flag_eval( prompt, PREF_KEY_OPTIONS_PROMPT );
|
|
|
|
GSList * l;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
for( l = torrentFiles; l != NULL; l = l->next )
|
2009-06-11 16:17:48 +00:00
|
|
|
add_filename( core, l->data, doStart, doPrompt, doNotify );
|
2008-10-20 18:35:12 +00:00
|
|
|
|
|
|
|
tr_core_torrents_added( core );
|
2008-03-18 01:22:11 +00:00
|
|
|
freestrlist( torrentFiles );
|
2007-05-24 10:37:07 +00:00
|
|
|
}
|
|
|
|
|
2007-05-23 17:59:35 +00:00
|
|
|
void
|
|
|
|
tr_core_torrents_added( TrCore * self )
|
|
|
|
{
|
2007-05-24 02:50:28 +00:00
|
|
|
tr_core_update( self );
|
2007-05-23 17:59:35 +00:00
|
|
|
tr_core_errsig( self, TR_CORE_ERR_NO_MORE_TORRENTS, NULL );
|
|
|
|
}
|
|
|
|
|
2008-02-19 03:57:03 +00:00
|
|
|
static gboolean
|
2008-09-23 19:11:04 +00:00
|
|
|
findTorrentInModel( TrCore * core,
|
|
|
|
int id,
|
|
|
|
GtkTreeIter * setme )
|
2008-02-19 03:57:03 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int match = 0;
|
|
|
|
GtkTreeIter iter;
|
2008-02-19 03:57:03 +00:00
|
|
|
GtkTreeModel * model = tr_core_model( core );
|
|
|
|
|
|
|
|
if( gtk_tree_model_iter_children( model, &iter, NULL ) ) do
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
|
|
|
tr_torrent * tor;
|
|
|
|
gtk_tree_model_get( model, &iter, MC_TORRENT_RAW, &tor, -1 );
|
|
|
|
match = tr_torrentId( tor ) == id;
|
|
|
|
}
|
|
|
|
while( !match && gtk_tree_model_iter_next( model, &iter ) );
|
2008-02-19 03:57:03 +00:00
|
|
|
|
|
|
|
if( match )
|
|
|
|
*setme = iter;
|
|
|
|
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-21 04:38:54 +00:00
|
|
|
tr_core_remove_torrent( TrCore * core, TrTorrent * gtor, gboolean deleteFiles )
|
2008-02-19 03:57:03 +00:00
|
|
|
{
|
2010-04-21 04:38:54 +00:00
|
|
|
const tr_torrent * tor = tr_torrent_handle( gtor );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-04-21 04:38:54 +00:00
|
|
|
if( tor != NULL )
|
|
|
|
tr_core_remove_torrent_from_id( core, tr_torrentId( tor ), deleteFiles );
|
2008-07-16 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-21 04:38:54 +00:00
|
|
|
tr_core_remove_torrent_from_id( TrCore * core, int id, gboolean deleteFiles )
|
2008-07-16 19:38:22 +00:00
|
|
|
{
|
2010-04-21 04:38:54 +00:00
|
|
|
GtkTreeIter iter;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-04-21 04:38:54 +00:00
|
|
|
if( findTorrentInModel( core, id, &iter ) )
|
2008-07-16 19:38:22 +00:00
|
|
|
{
|
2010-04-21 04:38:54 +00:00
|
|
|
TrTorrent * gtor = NULL;
|
|
|
|
tr_torrent * tor = NULL;
|
|
|
|
GtkTreeModel * model = tr_core_model( core );
|
2008-02-19 03:57:03 +00:00
|
|
|
|
2010-04-21 04:38:54 +00:00
|
|
|
gtk_tree_model_get( model, &iter, MC_TORRENT, >or,
|
|
|
|
MC_TORRENT_RAW, &tor,
|
|
|
|
-1 );
|
2008-02-19 03:57:03 +00:00
|
|
|
|
2010-04-21 04:38:54 +00:00
|
|
|
/* remove from the gui */
|
|
|
|
gtk_list_store_remove( GTK_LIST_STORE( model ), &iter );
|
2008-02-19 03:57:03 +00:00
|
|
|
|
2010-04-21 04:38:54 +00:00
|
|
|
/* maybe delete the downloaded files */
|
|
|
|
if( deleteFiles )
|
|
|
|
tr_torrentDeleteLocalData( tor, gtr_file_trash_or_remove );
|
|
|
|
|
|
|
|
/* remove the torrent */
|
|
|
|
tr_torrent_set_remove_flag( gtor, TRUE );
|
2010-04-21 19:23:27 +00:00
|
|
|
gtr_warn_if_fail( G_OBJECT( gtor )->ref_count == 1 );
|
2010-04-21 04:38:54 +00:00
|
|
|
g_object_unref( G_OBJECT( gtor ) ); /* remove the last refcount */
|
2008-07-16 19:38:22 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-19 03:57:03 +00:00
|
|
|
|
2008-02-09 17:29:05 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-11-23 15:36:31 +00:00
|
|
|
static gboolean
|
2009-08-07 20:49:06 +00:00
|
|
|
update_foreach( GtkTreeModel * model,
|
2007-11-23 15:36:31 +00:00
|
|
|
GtkTreePath * path UNUSED,
|
2009-08-07 20:49:06 +00:00
|
|
|
GtkTreeIter * iter,
|
2008-04-01 02:36:41 +00:00
|
|
|
gpointer data UNUSED )
|
2007-05-23 01:47:42 +00:00
|
|
|
{
|
2009-08-07 20:49:06 +00:00
|
|
|
int oldActivity, newActivity;
|
2009-08-12 13:42:49 +00:00
|
|
|
double oldUpSpeed, newUpSpeed;
|
|
|
|
double oldDownSpeed, newDownSpeed;
|
2009-08-07 20:49:06 +00:00
|
|
|
const tr_stat * st;
|
2008-04-01 02:36:41 +00:00
|
|
|
TrTorrent * gtor;
|
2008-01-16 16:03:18 +00:00
|
|
|
|
2009-08-07 20:49:06 +00:00
|
|
|
/* get the old states */
|
2008-04-01 02:36:41 +00:00
|
|
|
gtk_tree_model_get( model, iter,
|
|
|
|
MC_TORRENT, >or,
|
2008-10-20 17:54:56 +00:00
|
|
|
MC_ACTIVITY, &oldActivity,
|
2009-08-07 20:49:06 +00:00
|
|
|
MC_SPEED_UP, &oldUpSpeed,
|
|
|
|
MC_SPEED_DOWN, &oldDownSpeed,
|
2008-04-01 02:36:41 +00:00
|
|
|
-1 );
|
2009-08-07 20:49:06 +00:00
|
|
|
|
|
|
|
/* get the new states */
|
|
|
|
st = tr_torrentStat( tr_torrent_handle( gtor ) );
|
|
|
|
newActivity = st->activity;
|
|
|
|
newUpSpeed = st->pieceUploadSpeed;
|
|
|
|
newDownSpeed = st->pieceDownloadSpeed;
|
|
|
|
|
|
|
|
/* updating the model triggers off resort/refresh,
|
|
|
|
so don't do it unless something's actually changed... */
|
|
|
|
if( ( newActivity != oldActivity ) ||
|
2009-08-12 13:42:49 +00:00
|
|
|
( (int)(newUpSpeed*10.0) != (int)(oldUpSpeed*10.0) ) ||
|
|
|
|
( (int)(newDownSpeed*10.0) != (int)(oldDownSpeed*10.0) ) )
|
2009-08-07 20:49:06 +00:00
|
|
|
{
|
2007-12-28 06:12:53 +00:00
|
|
|
gtk_list_store_set( GTK_LIST_STORE( model ), iter,
|
2008-10-20 17:54:56 +00:00
|
|
|
MC_ACTIVITY, newActivity,
|
2009-08-07 20:49:06 +00:00
|
|
|
MC_SPEED_UP, newUpSpeed,
|
|
|
|
MC_SPEED_DOWN, newDownSpeed,
|
2007-12-28 06:12:53 +00:00
|
|
|
-1 );
|
2009-08-07 20:49:06 +00:00
|
|
|
}
|
2007-12-28 06:12:53 +00:00
|
|
|
|
2008-04-01 02:36:41 +00:00
|
|
|
/* cleanup */
|
2007-12-27 06:59:48 +00:00
|
|
|
g_object_unref( gtor );
|
2007-11-23 15:36:31 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_core_update( TrCore * self )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int column;
|
|
|
|
GtkSortType order;
|
2007-11-23 15:36:31 +00:00
|
|
|
GtkTreeSortable * sortable;
|
2008-09-23 19:11:04 +00:00
|
|
|
GtkTreeModel * model = tr_core_model( self );
|
2007-11-23 15:36:31 +00:00
|
|
|
|
|
|
|
/* pause sorting */
|
2008-02-09 17:29:05 +00:00
|
|
|
sortable = GTK_TREE_SORTABLE( model );
|
2007-11-23 15:36:31 +00:00
|
|
|
gtk_tree_sortable_get_sort_column_id( sortable, &column, &order );
|
2008-09-23 19:11:04 +00:00
|
|
|
gtk_tree_sortable_set_sort_column_id(
|
|
|
|
sortable, GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, order );
|
2007-11-23 15:36:31 +00:00
|
|
|
|
|
|
|
/* refresh the model */
|
2008-04-01 02:36:41 +00:00
|
|
|
gtk_tree_model_foreach( model, update_foreach, NULL );
|
2007-11-23 15:36:31 +00:00
|
|
|
|
|
|
|
/* resume sorting */
|
|
|
|
gtk_tree_sortable_set_sort_column_id( sortable, column, order );
|
2008-07-02 18:49:07 +00:00
|
|
|
|
|
|
|
/* maybe inhibit hibernation */
|
|
|
|
maybeInhibitHibernation( self );
|
2007-05-23 01:47:42 +00:00
|
|
|
}
|
2007-05-23 17:59:35 +00:00
|
|
|
|
2007-05-24 03:29:23 +00:00
|
|
|
void
|
2008-03-18 01:22:11 +00:00
|
|
|
tr_core_quit( TrCore * core )
|
2007-05-24 03:29:23 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
g_signal_emit( core, TR_CORE_GET_CLASS( core )->quitsig, 0 );
|
2007-05-24 03:29:23 +00:00
|
|
|
}
|
2007-05-24 13:55:57 +00:00
|
|
|
|
2008-05-24 23:23:20 +00:00
|
|
|
/**
|
|
|
|
*** Hibernate
|
|
|
|
**/
|
|
|
|
|
|
|
|
#ifdef HAVE_DBUS_GLIB
|
|
|
|
|
|
|
|
static DBusGProxy*
|
|
|
|
get_hibernation_inhibit_proxy( void )
|
|
|
|
{
|
|
|
|
DBusGConnection * conn;
|
2009-12-01 04:22:14 +00:00
|
|
|
GError * error = NULL;
|
|
|
|
const char * name = "org.gnome.SessionManager";
|
|
|
|
const char * path = "/org/gnome/SessionManager";
|
|
|
|
const char * interface = "org.gnome.SessionManager";
|
2008-05-24 23:23:20 +00:00
|
|
|
|
|
|
|
conn = dbus_g_bus_get( DBUS_BUS_SESSION, &error );
|
|
|
|
if( error )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
g_warning ( "DBUS cannot connect : %s", error->message );
|
|
|
|
g_error_free ( error );
|
2008-05-24 23:23:20 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-12-01 04:22:14 +00:00
|
|
|
return dbus_g_proxy_new_for_name ( conn, name, path, interface );
|
2008-05-24 23:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtr_inhibit_hibernation( guint * cookie )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
gboolean success = FALSE;
|
2008-05-24 23:23:20 +00:00
|
|
|
DBusGProxy * proxy = get_hibernation_inhibit_proxy( );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-24 23:23:20 +00:00
|
|
|
if( proxy )
|
|
|
|
{
|
2009-12-01 04:22:14 +00:00
|
|
|
GError * error = NULL;
|
|
|
|
const int toplevel_xid = 0;
|
2008-05-24 23:23:20 +00:00
|
|
|
const char * application = _( "Transmission Bittorrent Client" );
|
|
|
|
const char * reason = _( "BitTorrent Activity" );
|
2009-12-07 18:04:50 +00:00
|
|
|
const int flags = 4; /* Inhibit suspending the session or computer */
|
2009-12-01 04:22:14 +00:00
|
|
|
|
2008-05-24 23:23:20 +00:00
|
|
|
success = dbus_g_proxy_call( proxy, "Inhibit", &error,
|
|
|
|
G_TYPE_STRING, application,
|
2009-12-01 04:22:14 +00:00
|
|
|
G_TYPE_UINT, toplevel_xid,
|
2008-05-24 23:23:20 +00:00
|
|
|
G_TYPE_STRING, reason,
|
2009-12-01 04:22:14 +00:00
|
|
|
G_TYPE_UINT, flags,
|
|
|
|
G_TYPE_INVALID, /* sentinel - end of input args */
|
2008-05-24 23:23:20 +00:00
|
|
|
G_TYPE_UINT, cookie,
|
2009-12-01 04:22:14 +00:00
|
|
|
G_TYPE_INVALID /* senitnel - end of output args */ );
|
2008-05-24 23:23:20 +00:00
|
|
|
if( success )
|
2009-06-23 14:15:23 +00:00
|
|
|
tr_inf( "%s", _( "Disallowing desktop hibernation" ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
|
|
|
{
|
2009-06-23 14:15:23 +00:00
|
|
|
tr_err( _( "Couldn't disable desktop hibernation: %s" ),
|
2008-09-23 19:11:04 +00:00
|
|
|
error->message );
|
2008-05-24 23:23:20 +00:00
|
|
|
g_error_free( error );
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref( G_OBJECT( proxy ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return success != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtr_uninhibit_hibernation( guint inhibit_cookie )
|
|
|
|
{
|
|
|
|
DBusGProxy * proxy = get_hibernation_inhibit_proxy( );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-05-24 23:23:20 +00:00
|
|
|
if( proxy )
|
|
|
|
{
|
|
|
|
GError * error = NULL;
|
2009-12-01 04:22:14 +00:00
|
|
|
gboolean success = dbus_g_proxy_call( proxy, "Uninhibit", &error,
|
2008-05-24 23:23:20 +00:00
|
|
|
G_TYPE_UINT, inhibit_cookie,
|
|
|
|
G_TYPE_INVALID,
|
|
|
|
G_TYPE_INVALID );
|
|
|
|
if( success )
|
2009-06-23 14:15:23 +00:00
|
|
|
tr_inf( "%s", _( "Allowing desktop hibernation" ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
g_warning( "Couldn't uninhibit the system from suspending: %s.",
|
|
|
|
error->message );
|
2008-05-24 23:23:20 +00:00
|
|
|
g_error_free( error );
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref( G_OBJECT( proxy ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2008-07-02 18:49:07 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_core_set_hibernation_allowed( TrCore * core,
|
|
|
|
gboolean allowed )
|
2008-05-24 23:23:20 +00:00
|
|
|
{
|
|
|
|
#ifdef HAVE_DBUS_GLIB
|
|
|
|
g_return_if_fail( core );
|
|
|
|
g_return_if_fail( core->priv );
|
|
|
|
|
|
|
|
core->priv->inhibit_allowed = allowed != 0;
|
|
|
|
|
|
|
|
if( allowed && core->priv->have_inhibit_cookie )
|
|
|
|
{
|
|
|
|
gtr_uninhibit_hibernation( core->priv->inhibit_cookie );
|
|
|
|
core->priv->have_inhibit_cookie = FALSE;
|
|
|
|
}
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( !allowed
|
|
|
|
&& !core->priv->have_inhibit_cookie
|
|
|
|
&& !core->priv->dbus_error )
|
2008-05-24 23:23:20 +00:00
|
|
|
{
|
2008-07-06 00:56:19 +00:00
|
|
|
if( gtr_inhibit_hibernation( &core->priv->inhibit_cookie ) )
|
|
|
|
core->priv->have_inhibit_cookie = TRUE;
|
|
|
|
else
|
|
|
|
core->priv->dbus_error = TRUE;
|
2008-05-24 23:23:20 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-07-02 18:49:07 +00:00
|
|
|
static void
|
|
|
|
maybeInhibitHibernation( TrCore * core )
|
|
|
|
{
|
2010-04-29 23:08:11 +00:00
|
|
|
/* inhibit if it's enabled *AND* all the torrents are paused */
|
|
|
|
const gboolean inhibit = pref_flag_get( PREF_KEY_INHIBIT_HIBERNATION )
|
|
|
|
&& ( tr_core_get_active_torrent_count( core ) == 0 );
|
2008-07-02 18:49:07 +00:00
|
|
|
|
2008-10-08 18:26:55 +00:00
|
|
|
tr_core_set_hibernation_allowed( core, !inhibit );
|
2008-07-02 18:49:07 +00:00
|
|
|
}
|
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
/**
|
|
|
|
*** Prefs
|
|
|
|
**/
|
2007-06-27 18:34:38 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
commitPrefsChange( TrCore * core,
|
|
|
|
const char * key )
|
2007-09-27 20:57:58 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
g_signal_emit( core, TR_CORE_GET_CLASS( core )->prefsig, 0, key );
|
2008-12-14 22:14:00 +00:00
|
|
|
pref_save( tr_core_session( core ) );
|
2007-09-27 20:57:58 +00:00
|
|
|
}
|
2007-05-24 13:55:57 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_core_set_pref( TrCore * self,
|
|
|
|
const char * key,
|
|
|
|
const char * newval )
|
2007-09-27 20:57:58 +00:00
|
|
|
{
|
2008-06-12 16:25:36 +00:00
|
|
|
const char * oldval = pref_string_get( key );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
if( tr_strcmp( oldval, newval ) )
|
2007-05-24 13:55:57 +00:00
|
|
|
{
|
2007-09-27 20:57:58 +00:00
|
|
|
pref_string_set( key, newval );
|
|
|
|
commitPrefsChange( self, key );
|
2007-05-24 13:55:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_core_set_pref_bool( TrCore * self,
|
|
|
|
const char * key,
|
|
|
|
gboolean newval )
|
2007-05-24 13:55:57 +00:00
|
|
|
{
|
2007-09-27 20:57:58 +00:00
|
|
|
const gboolean oldval = pref_flag_get( key );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
if( oldval != newval )
|
|
|
|
{
|
|
|
|
pref_flag_set( key, newval );
|
|
|
|
commitPrefsChange( self, key );
|
|
|
|
}
|
2007-05-24 13:55:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_core_set_pref_int( TrCore * self,
|
|
|
|
const char * key,
|
|
|
|
int newval )
|
2007-05-24 13:55:57 +00:00
|
|
|
{
|
2007-09-27 20:57:58 +00:00
|
|
|
const int oldval = pref_int_get( key );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
if( oldval != newval )
|
|
|
|
{
|
|
|
|
pref_int_set( key, newval );
|
|
|
|
commitPrefsChange( self, key );
|
|
|
|
}
|
2007-05-24 13:55:57 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2009-02-13 18:23:56 +00:00
|
|
|
void
|
|
|
|
tr_core_set_pref_double( TrCore * self,
|
|
|
|
const char * key,
|
|
|
|
double newval )
|
|
|
|
{
|
|
|
|
const double oldval = pref_double_get( key );
|
|
|
|
|
|
|
|
if( oldval != newval )
|
|
|
|
{
|
|
|
|
pref_double_set( key, newval );
|
|
|
|
commitPrefsChange( self, key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-07 20:13:08 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** RPC Interface
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
/* #define DEBUG_RPC */
|
|
|
|
|
|
|
|
static int nextTag = 1;
|
|
|
|
|
|
|
|
typedef void ( server_response_func )( TrCore * core, tr_benc * response, gpointer user_data );
|
|
|
|
|
|
|
|
struct pending_request_data
|
|
|
|
{
|
|
|
|
TrCore * core;
|
|
|
|
server_response_func * responseFunc;
|
|
|
|
gpointer responseFuncUserData;
|
|
|
|
};
|
|
|
|
|
|
|
|
static GHashTable * pendingRequests = NULL;
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
readResponseIdle( void * vresponse )
|
|
|
|
{
|
|
|
|
GByteArray * response;
|
|
|
|
tr_benc top;
|
|
|
|
int64_t intVal;
|
|
|
|
int tag;
|
|
|
|
struct pending_request_data * data;
|
|
|
|
|
|
|
|
response = vresponse;
|
2009-06-01 18:57:37 +00:00
|
|
|
tr_jsonParse( NULL, response->data, response->len, &top, NULL );
|
2009-04-07 20:13:08 +00:00
|
|
|
tr_bencDictFindInt( &top, "tag", &intVal );
|
|
|
|
tag = (int)intVal;
|
|
|
|
|
|
|
|
data = g_hash_table_lookup( pendingRequests, &tag );
|
2009-06-15 20:29:26 +00:00
|
|
|
if( data && data->responseFunc )
|
2009-04-07 20:13:08 +00:00
|
|
|
(*data->responseFunc)(data->core, &top, data->responseFuncUserData );
|
|
|
|
|
|
|
|
tr_bencFree( &top );
|
|
|
|
g_hash_table_remove( pendingRequests, &tag );
|
|
|
|
g_byte_array_free( response, TRUE );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
readResponse( tr_session * session UNUSED,
|
|
|
|
const char * response,
|
|
|
|
size_t response_len,
|
|
|
|
void * unused UNUSED )
|
|
|
|
{
|
|
|
|
GByteArray * bytes = g_byte_array_new( );
|
|
|
|
#ifdef DEBUG_RPC
|
|
|
|
g_message( "response: [%*.*s]", (int)response_len, (int)response_len, response );
|
|
|
|
#endif
|
|
|
|
g_byte_array_append( bytes, (const uint8_t*)response, response_len );
|
2009-06-29 17:22:35 +00:00
|
|
|
gtr_idle_add( readResponseIdle, bytes );
|
2009-04-07 20:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sendRequest( TrCore * core, const char * json, int tag,
|
|
|
|
server_response_func * responseFunc, void * responseFuncUserData )
|
|
|
|
{
|
|
|
|
tr_session * session = tr_core_session( core );
|
|
|
|
|
|
|
|
if( pendingRequests == NULL )
|
|
|
|
{
|
2009-06-11 02:16:42 +00:00
|
|
|
pendingRequests = g_hash_table_new_full( g_int_hash, g_int_equal, g_free, g_free );
|
2009-04-07 20:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( session == NULL )
|
|
|
|
{
|
|
|
|
g_error( "GTK+ client doesn't support connections to remote servers yet." );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* remember this request */
|
|
|
|
struct pending_request_data * data;
|
|
|
|
data = g_new0( struct pending_request_data, 1 );
|
|
|
|
data->core = core;
|
|
|
|
data->responseFunc = responseFunc;
|
|
|
|
data->responseFuncUserData = responseFuncUserData;
|
2009-06-11 02:16:42 +00:00
|
|
|
g_hash_table_insert( pendingRequests, g_memdup( &tag, sizeof( int ) ), data );
|
2009-04-07 20:13:08 +00:00
|
|
|
|
|
|
|
/* make the request */
|
|
|
|
#ifdef DEBUG_RPC
|
|
|
|
g_message( "request: [%s]", json );
|
|
|
|
#endif
|
|
|
|
tr_rpc_request_exec_json( session, json, strlen( json ), readResponse, GINT_TO_POINTER(tag) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** Sending a test-port request via RPC
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
|
|
|
portTestResponseFunc( TrCore * core, tr_benc * response, gpointer userData UNUSED )
|
|
|
|
{
|
|
|
|
tr_benc * args;
|
|
|
|
tr_bool isOpen = FALSE;
|
|
|
|
|
|
|
|
if( tr_bencDictFindDict( response, "arguments", &args ) )
|
|
|
|
tr_bencDictFindBool( args, "port-is-open", &isOpen );
|
|
|
|
|
|
|
|
emitPortTested( core, isOpen );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_core_port_test( TrCore * core )
|
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
const int tag = nextTag++;
|
|
|
|
g_snprintf( buf, sizeof( buf ), "{ \"method\": \"port-test\", \"tag\": %d }", tag );
|
|
|
|
sendRequest( core, buf, tag, portTestResponseFunc, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
**** Updating a blocklist via RPC
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
|
|
|
blocklistResponseFunc( TrCore * core, tr_benc * response, gpointer userData UNUSED )
|
|
|
|
{
|
|
|
|
tr_benc * args;
|
|
|
|
int64_t ruleCount = 0;
|
|
|
|
|
|
|
|
if( tr_bencDictFindDict( response, "arguments", &args ) )
|
|
|
|
tr_bencDictFindInt( args, "blocklist-size", &ruleCount );
|
|
|
|
|
|
|
|
if( ruleCount > 0 )
|
|
|
|
pref_int_set( "blocklist-date", time( NULL ) );
|
|
|
|
|
|
|
|
emitBlocklistUpdated( core, ruleCount );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_core_blocklist_update( TrCore * core )
|
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
const int tag = nextTag++;
|
|
|
|
g_snprintf( buf, sizeof( buf ), "{ \"method\": \"blocklist-update\", \"tag\": %d }", tag );
|
|
|
|
sendRequest( core, buf, tag, blocklistResponseFunc, NULL );
|
|
|
|
}
|
2009-04-24 01:37:04 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_core_exec_json( TrCore * core, const char * json )
|
|
|
|
{
|
2009-06-15 20:29:26 +00:00
|
|
|
const int tag = nextTag++;
|
|
|
|
sendRequest( core, json, tag, NULL, NULL );
|
2009-04-24 01:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_core_exec( TrCore * core, const tr_benc * top )
|
|
|
|
{
|
2009-06-02 01:48:48 +00:00
|
|
|
char * json = tr_bencToStr( top, TR_FMT_JSON_LEAN, NULL );
|
2009-04-24 01:37:04 +00:00
|
|
|
tr_core_exec_json( core, json );
|
|
|
|
tr_free( json );
|
|
|
|
}
|
2010-03-14 05:13:02 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_core_torrent_changed( TrCore * core, int id )
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeModel * model = tr_core_model( core );
|
|
|
|
|
|
|
|
if( gtk_tree_model_get_iter_first( model, &iter ) ) do
|
|
|
|
{
|
|
|
|
tr_torrent * tor;
|
|
|
|
gtk_tree_model_get( model, &iter, MC_TORRENT_RAW, &tor, -1 );
|
|
|
|
if( tr_torrentId( tor ) == id )
|
|
|
|
{
|
|
|
|
GtkTreePath * path = gtk_tree_model_get_path( model, &iter );
|
|
|
|
gtk_tree_model_row_changed( model, path, &iter );
|
|
|
|
gtk_tree_path_free( path );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while( gtk_tree_model_iter_next( model, &iter ) );
|
|
|
|
}
|
|
|
|
|
2010-04-29 23:08:11 +00:00
|
|
|
int
|
|
|
|
tr_core_get_torrent_count( TrCore * core )
|
|
|
|
{
|
|
|
|
return gtk_tree_model_iter_n_children( tr_core_model( core ), NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_core_get_active_torrent_count( TrCore * core )
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeModel * model = tr_core_model( core );
|
|
|
|
int activeCount = 0;
|
|
|
|
|
|
|
|
if( gtk_tree_model_get_iter_first( model, &iter ) ) do
|
|
|
|
{
|
|
|
|
int activity;
|
|
|
|
gtk_tree_model_get( model, &iter, MC_ACTIVITY, &activity, -1 );
|
|
|
|
|
|
|
|
if( activity != TR_STATUS_STOPPED )
|
|
|
|
++activeCount;
|
|
|
|
}
|
|
|
|
while( gtk_tree_model_iter_next( model, &iter ) );
|
|
|
|
|
|
|
|
return activeCount;
|
|
|
|
}
|
|
|
|
|