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
|
|
|
|
#include <gio/gio.h>
|
|
|
|
#endif
|
2007-05-23 00:49:31 +00:00
|
|
|
|
2007-07-18 23:04:26 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2008-02-28 20:23:02 +00:00
|
|
|
#include <libtransmission/utils.h> /* tr_free */
|
2007-05-23 01:47:42 +00:00
|
|
|
|
2007-05-23 04:39:06 +00:00
|
|
|
#include "conf.h"
|
2008-02-26 19:58:03 +00:00
|
|
|
#include "tr-core.h"
|
|
|
|
#include "tr-prefs.h"
|
|
|
|
#include "tr-torrent.h"
|
2007-05-23 00:49:31 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2008-02-09 17:29:05 +00:00
|
|
|
struct TrCorePrivate
|
|
|
|
{
|
2008-03-09 15:27:08 +00:00
|
|
|
#ifdef HAVE_GIO
|
|
|
|
GFileMonitor * monitor;
|
2008-03-09 22:11:36 +00:00
|
|
|
gulong monitor_tag;
|
|
|
|
char * monitor_path;
|
2008-03-18 01:22:11 +00:00
|
|
|
GSList * monitor_files;
|
2008-03-12 02:11:37 +00:00
|
|
|
guint monitor_idle_tag;
|
2008-03-09 15:27:08 +00:00
|
|
|
#endif
|
2008-02-09 17:29:05 +00:00
|
|
|
GtkTreeModel * model;
|
|
|
|
tr_handle * handle;
|
|
|
|
int nextid;
|
|
|
|
};
|
|
|
|
|
2007-05-23 00:49:31 +00:00
|
|
|
static void
|
2008-02-14 17:18:00 +00:00
|
|
|
tr_core_marshal_err( GClosure * closure, GValue * ret UNUSED,
|
|
|
|
guint count, const GValue * vals,
|
|
|
|
gpointer hint UNUSED, gpointer marshal )
|
2007-05-23 17:59:35 +00:00
|
|
|
{
|
2007-05-24 02:50:28 +00:00
|
|
|
typedef void (*TRMarshalErr)
|
|
|
|
( gpointer, enum tr_core_err, const char *, gpointer );
|
2007-05-23 17:59:35 +00:00
|
|
|
TRMarshalErr callback;
|
|
|
|
GCClosure * cclosure = (GCClosure*) closure;
|
|
|
|
enum tr_core_err errcode;
|
|
|
|
const char * errstr;
|
|
|
|
gpointer inst, gdata;
|
|
|
|
|
2008-02-14 17:18:00 +00:00
|
|
|
g_return_if_fail( count == 3 );
|
2007-05-23 17:59:35 +00:00
|
|
|
|
|
|
|
inst = g_value_peek_pointer( vals );
|
|
|
|
errcode = g_value_get_int( vals + 1 );
|
|
|
|
errstr = g_value_get_string( vals + 2 );
|
|
|
|
gdata = closure->data;
|
|
|
|
|
2008-02-14 17:18:00 +00:00
|
|
|
callback = (TRMarshalErr)( marshal ? marshal : cclosure->callback );
|
2007-05-23 17:59:35 +00:00
|
|
|
callback( inst, errcode, errstr, gdata );
|
2007-05-23 00:49:31 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
static void
|
2008-02-14 17:18:00 +00:00
|
|
|
tr_core_marshal_prompt( GClosure * closure, GValue * ret UNUSED,
|
|
|
|
guint count, const GValue * vals,
|
|
|
|
gpointer hint UNUSED, gpointer marshal )
|
2007-05-24 02:50:28 +00:00
|
|
|
{
|
2008-03-18 01:22:11 +00:00
|
|
|
typedef void (*TRMarshalPrompt)( gpointer, tr_ctor *, gpointer );
|
2007-05-24 02:50:28 +00:00
|
|
|
TRMarshalPrompt callback;
|
|
|
|
GCClosure * cclosure = (GCClosure*) closure;
|
2008-02-13 03:00:21 +00:00
|
|
|
gpointer ctor;
|
2007-05-24 02:50:28 +00:00
|
|
|
gpointer inst, gdata;
|
|
|
|
|
2008-03-18 01:22:11 +00:00
|
|
|
g_return_if_fail( count == 2 );
|
2007-05-24 10:37:07 +00:00
|
|
|
|
2008-02-13 03:00:21 +00:00
|
|
|
inst = g_value_peek_pointer( vals );
|
2008-03-18 01:22:11 +00:00
|
|
|
ctor = g_value_peek_pointer( vals + 1 );
|
2008-02-13 03:00:21 +00:00
|
|
|
gdata = closure->data;
|
2007-05-24 10:37:07 +00:00
|
|
|
|
2008-02-14 17:18:00 +00:00
|
|
|
callback = (TRMarshalPrompt)( marshal ? marshal : cclosure->callback );
|
2008-03-18 01:22:11 +00:00
|
|
|
callback( inst, ctor, gdata );
|
2007-05-24 10:37:07 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
pref_save( NULL );
|
|
|
|
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-01-19 05:04:38 +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;
|
|
|
|
TrCoreClass * core_class;
|
|
|
|
|
2008-02-14 17:18:00 +00:00
|
|
|
g_type_class_add_private( g_class, sizeof(struct TrCorePrivate) );
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
gobject_class = G_OBJECT_CLASS( g_class );
|
|
|
|
gobject_class->dispose = tr_core_dispose;
|
|
|
|
|
2008-02-09 17:29:05 +00:00
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
core_class = TR_CORE_CLASS( g_class );
|
|
|
|
core_class->errsig = g_signal_new( "error", G_TYPE_FROM_CLASS( g_class ),
|
|
|
|
G_SIGNAL_RUN_LAST, 0, NULL, NULL,
|
|
|
|
tr_core_marshal_err, G_TYPE_NONE,
|
|
|
|
2, G_TYPE_INT, G_TYPE_STRING );
|
2008-03-18 01:22:11 +00:00
|
|
|
core_class->promptsig = g_signal_new( "add-torrent-prompt",
|
2007-10-16 14:16:01 +00:00
|
|
|
G_TYPE_FROM_CLASS( g_class ),
|
|
|
|
G_SIGNAL_RUN_LAST, 0, NULL, NULL,
|
|
|
|
tr_core_marshal_prompt, G_TYPE_NONE,
|
2008-03-18 01:22:11 +00:00
|
|
|
1, G_TYPE_POINTER );
|
2007-10-16 14:16:01 +00:00
|
|
|
core_class->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 );
|
|
|
|
core_class->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-02-14 17:18:00 +00:00
|
|
|
/***
|
|
|
|
**** SORTING
|
|
|
|
***/
|
|
|
|
|
2007-12-19 07:10:47 +00:00
|
|
|
static int
|
|
|
|
compareDouble( double a, double b )
|
|
|
|
{
|
|
|
|
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
|
|
|
|
compareRatio( double a, double b )
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
compareByRatio( GtkTreeModel * model,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer user_data UNUSED )
|
|
|
|
{
|
|
|
|
tr_torrent *ta, *tb;
|
|
|
|
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
|
2007-12-19 02:46:30 +00:00
|
|
|
compareByActivity( GtkTreeModel * model,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer user_data UNUSED )
|
2007-10-16 14:16:01 +00:00
|
|
|
{
|
2007-12-19 07:10:47 +00:00
|
|
|
int i;
|
2007-12-27 18:56:54 +00:00
|
|
|
tr_torrent *ta, *tb;
|
2007-12-19 02:46:30 +00:00
|
|
|
const tr_stat *sa, *sb;
|
2007-12-27 18:56:54 +00:00
|
|
|
|
|
|
|
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 );
|
2007-10-16 14:16:01 +00:00
|
|
|
|
2007-12-19 07:10:47 +00:00
|
|
|
if(( i = compareDouble( sa->rateUpload + sa->rateDownload,
|
|
|
|
sb->rateUpload + sb->rateDownload ) ))
|
|
|
|
return i;
|
2007-10-16 14:16:01 +00:00
|
|
|
|
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
|
|
|
|
compareByName( GtkTreeModel * model,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer user_data UNUSED )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char *ca, *cb;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
compareByProgress( GtkTreeModel * model,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer user_data UNUSED )
|
|
|
|
{
|
|
|
|
int ret;
|
2007-12-27 18:56:54 +00:00
|
|
|
tr_torrent *ta, *tb;
|
2007-12-19 02:46:30 +00:00
|
|
|
const tr_stat *sa, *sb;
|
2007-12-27 18:56:54 +00:00
|
|
|
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 );
|
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;
|
|
|
|
}
|
|
|
|
|
2007-12-19 07:03:28 +00:00
|
|
|
static int
|
|
|
|
compareByState( GtkTreeModel * model,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
2008-02-04 18:40:56 +00:00
|
|
|
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 */
|
2007-12-28 06:22:40 +00:00
|
|
|
gtk_tree_model_get( model, a, MC_STATUS, &sa, -1 );
|
|
|
|
gtk_tree_model_get( model, b, MC_STATUS, &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
|
|
|
|
compareByTracker( GtkTreeModel * model,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer user_data UNUSED )
|
|
|
|
{
|
2007-12-19 07:03:28 +00:00
|
|
|
const 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 strcmp( tr_torrentInfo(ta)->primaryAddress,
|
|
|
|
tr_torrentInfo(tb)->primaryAddress );
|
2007-12-19 02:46:30 +00:00
|
|
|
}
|
|
|
|
|
2007-10-17 18:31:12 +00:00
|
|
|
static void
|
2007-12-19 02:46:30 +00:00
|
|
|
setSort( TrCore * core, const char * mode, gboolean isReversed )
|
2007-10-17 18:31:12 +00:00
|
|
|
{
|
2008-04-06 14:07:41 +00:00
|
|
|
const int col = MC_TORRENT_RAW;
|
|
|
|
GtkTreeIterCompareFunc sort_func;
|
2007-12-20 01:33:51 +00:00
|
|
|
GtkSortType type = isReversed ? GTK_SORT_ASCENDING : GTK_SORT_DESCENDING;
|
2008-04-06 14:07:41 +00:00
|
|
|
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;
|
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;
|
|
|
|
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;
|
2007-12-20 01:33:51 +00:00
|
|
|
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-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 ) )
|
|
|
|
tr_ctorSetPaused( ctor, TR_FORCE, !pref_flag_get( PREF_KEY_START ) );
|
|
|
|
|
|
|
|
if( tr_ctorGetDeleteSource( ctor, NULL ) )
|
|
|
|
tr_ctorSetDeleteSource( ctor, pref_flag_get( PREF_KEY_TRASH_ORIGINAL ) );
|
|
|
|
|
|
|
|
if( tr_ctorGetMaxConnectedPeers( ctor, TR_FORCE, NULL ) )
|
|
|
|
tr_ctorSetMaxConnectedPeers( ctor, TR_FORCE,
|
|
|
|
pref_int_get( PREF_KEY_MAX_PEERS_PER_TORRENT ) );
|
|
|
|
|
|
|
|
if( tr_ctorGetDestination( ctor, TR_FORCE, NULL ) ) {
|
|
|
|
char * path = pref_string_get( PREF_KEY_DIR_DEFAULT );
|
|
|
|
tr_ctorSetDestination( ctor, TR_FORCE, path );
|
|
|
|
g_free( path );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-09 15:27:08 +00:00
|
|
|
#ifdef HAVE_GIO
|
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
|
|
|
{
|
2008-03-18 01:22:11 +00:00
|
|
|
TrCore * core = TR_CORE( gcore );
|
2008-03-19 14:54:32 +00:00
|
|
|
tr_core_add_list_defaults( core, core->priv->monitor_files );
|
2008-03-12 02:11:37 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
core->priv->monitor_files = NULL;
|
|
|
|
core->priv->monitor_idle_tag = 0;
|
|
|
|
return FALSE;
|
2008-03-09 22:11:36 +00:00
|
|
|
}
|
|
|
|
|
2008-03-13 01:06:54 +00:00
|
|
|
static void
|
|
|
|
maybeAddTorrent( TrCore * core, const char * filename )
|
|
|
|
{
|
|
|
|
const gboolean isTorrent = g_str_has_suffix( filename, ".torrent" );
|
|
|
|
|
|
|
|
if( isTorrent )
|
|
|
|
{
|
|
|
|
struct TrCorePrivate * p = core->priv;
|
|
|
|
|
2008-03-18 01:22:11 +00:00
|
|
|
if( !g_slist_find_custom( p->monitor_files, filename, (GCompareFunc)strcmp ) )
|
|
|
|
p->monitor_files = g_slist_append( p->monitor_files, g_strdup( filename ) );
|
2008-03-13 01:06:54 +00:00
|
|
|
if( !p->monitor_idle_tag )
|
|
|
|
p->monitor_idle_tag = g_timeout_add( 1000, watchFolderIdle, core );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-09 15:27:08 +00:00
|
|
|
static void
|
|
|
|
watchFolderChanged( GFileMonitor * monitor UNUSED,
|
|
|
|
GFile * file,
|
|
|
|
GFile * other_type UNUSED,
|
|
|
|
GFileMonitorEvent event_type,
|
2008-03-12 02:11:37 +00:00
|
|
|
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 );
|
|
|
|
if( isEnabled )
|
|
|
|
{
|
|
|
|
char * dirname = pref_string_get( PREF_KEY_DIR_WATCH );
|
|
|
|
GDir * dir = g_dir_open( dirname, 0, NULL );
|
|
|
|
const char * basename;
|
2008-03-13 01:06:54 +00:00
|
|
|
while(( basename = g_dir_read_name( dir ))) {
|
|
|
|
char * filename = g_build_filename( dirname, basename, NULL );
|
|
|
|
maybeAddTorrent( core, filename );
|
|
|
|
g_free( filename );
|
|
|
|
}
|
2008-03-09 15:27:08 +00:00
|
|
|
g_free( dirname );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
updateWatchDir( TrCore * core )
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
GFile * file = g_file_new_for_path( filename );
|
|
|
|
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",
|
|
|
|
G_CALLBACK( watchFolderChanged ), core );
|
2008-03-09 15:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free( filename );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-12-19 18:42:33 +00:00
|
|
|
static void
|
|
|
|
prefsChanged( TrCore * core, const char * key, gpointer data UNUSED )
|
2007-12-19 02:46:30 +00:00
|
|
|
{
|
2008-02-14 17:18:00 +00:00
|
|
|
if( !strcmp( key, PREF_KEY_SORT_MODE ) ||
|
|
|
|
!strcmp( key, PREF_KEY_SORT_REVERSED ) )
|
2007-12-19 18:42:33 +00:00
|
|
|
{
|
|
|
|
char * mode = pref_string_get( PREF_KEY_SORT_MODE );
|
|
|
|
gboolean isReversed = pref_flag_get( PREF_KEY_SORT_REVERSED );
|
|
|
|
setSort( core, mode, isReversed );
|
|
|
|
g_free( mode );
|
|
|
|
}
|
2007-12-22 00:06:38 +00:00
|
|
|
else if( !strcmp( key, PREF_KEY_MAX_PEERS_GLOBAL ) )
|
|
|
|
{
|
|
|
|
const uint16_t val = pref_int_get( key );
|
2008-02-09 17:29:05 +00:00
|
|
|
tr_setGlobalPeerLimit( tr_core_handle( core ), val );
|
2007-12-22 00:06:38 +00:00
|
|
|
}
|
2008-03-09 15:27:08 +00:00
|
|
|
#ifdef HAVE_GIO
|
|
|
|
else if( !strcmp( key, PREF_KEY_DIR_WATCH ) ||
|
|
|
|
!strcmp( key, PREF_KEY_DIR_WATCH_ENABLED ) )
|
|
|
|
{
|
|
|
|
updateWatchDir( core );
|
|
|
|
}
|
|
|
|
#endif
|
2007-10-17 18:31:12 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
static void
|
2008-01-19 05:04:38 +00:00
|
|
|
tr_core_init( GTypeInstance * instance, gpointer g_class UNUSED )
|
2007-05-23 00:49:31 +00:00
|
|
|
{
|
2007-12-24 07:02:40 +00:00
|
|
|
tr_handle * h;
|
2007-05-23 00:49:31 +00:00
|
|
|
TrCore * self = (TrCore *) instance;
|
|
|
|
GtkListStore * store;
|
2008-02-09 17:29:05 +00:00
|
|
|
struct TrCorePrivate * p;
|
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 */
|
2007-12-19 02:46:30 +00:00
|
|
|
GType types[] = {
|
|
|
|
G_TYPE_STRING, /* name */
|
|
|
|
G_TYPE_STRING, /* collated name */
|
|
|
|
G_TYPE_STRING, /* hash string */
|
|
|
|
TR_TORRENT_TYPE, /* TrTorrent object */
|
|
|
|
G_TYPE_POINTER, /* tr_torrent* */
|
2007-12-28 06:12:53 +00:00
|
|
|
G_TYPE_INT, /* tr_stat()->status */
|
2007-12-19 02:46:30 +00:00
|
|
|
G_TYPE_INT /* ID for IPC */
|
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 );
|
|
|
|
|
|
|
|
|
2008-04-05 20:12:11 +00:00
|
|
|
h = tr_initFull( tr_getDefaultConfigDir( ),
|
|
|
|
"gtk",
|
2007-12-24 07:02:40 +00:00
|
|
|
pref_flag_get( PREF_KEY_PEX ),
|
|
|
|
pref_flag_get( PREF_KEY_NAT ),
|
|
|
|
pref_int_get( PREF_KEY_PORT ),
|
|
|
|
pref_flag_get( PREF_KEY_ENCRYPTED_ONLY )
|
|
|
|
? TR_ENCRYPTION_REQUIRED
|
|
|
|
: TR_ENCRYPTION_PREFERRED,
|
|
|
|
pref_flag_get( PREF_KEY_UL_LIMIT_ENABLED ),
|
|
|
|
pref_int_get( PREF_KEY_UL_LIMIT ),
|
|
|
|
pref_flag_get( PREF_KEY_DL_LIMIT_ENABLED ),
|
|
|
|
pref_int_get( PREF_KEY_DL_LIMIT ),
|
2007-12-24 16:02:36 +00:00
|
|
|
pref_int_get( PREF_KEY_MAX_PEERS_GLOBAL ),
|
2007-12-24 16:12:23 +00:00
|
|
|
pref_int_get( PREF_KEY_MSGLEVEL ),
|
2008-03-30 00:21:46 +00:00
|
|
|
TRUE, /* message queueing */
|
|
|
|
pref_flag_get( PREF_KEY_BLOCKLIST_ENABLED ) );
|
2007-12-24 07:02:40 +00:00
|
|
|
|
2007-05-23 00:49:31 +00:00
|
|
|
/* create the model used to store torrent data */
|
|
|
|
g_assert( ALEN( types ) == MC_ROW_COUNT );
|
|
|
|
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 );
|
|
|
|
p->handle = h;
|
|
|
|
p->nextid = 1;
|
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 ),
|
|
|
|
NULL, /* base_init */
|
|
|
|
NULL, /* base_finalize */
|
|
|
|
tr_core_class_init, /* class_init */
|
|
|
|
NULL, /* class_finalize */
|
|
|
|
NULL, /* class_data */
|
|
|
|
sizeof( TrCore ),
|
|
|
|
0, /* n_preallocs */
|
|
|
|
tr_core_init, /* instance_init */
|
|
|
|
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 *
|
|
|
|
tr_core_new( void )
|
|
|
|
{
|
2007-12-19 18:42:33 +00:00
|
|
|
TrCore * core = TR_CORE( g_object_new( TR_CORE_TYPE, NULL ) );
|
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 );
|
2007-12-22 00:06:38 +00:00
|
|
|
prefsChanged( core, PREF_KEY_MAX_PEERS_GLOBAL, NULL );
|
2007-12-22 16:34:47 +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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_handle *
|
2008-02-09 17:29:05 +00:00
|
|
|
tr_core_handle( TrCore * core )
|
2007-05-23 01:47:42 +00:00
|
|
|
{
|
2008-02-09 17:29:05 +00:00
|
|
|
return isDisposed( core ) ? NULL : core->priv->handle;
|
|
|
|
}
|
|
|
|
|
2008-04-01 02:36:41 +00:00
|
|
|
static gboolean
|
|
|
|
statsForeach( GtkTreeModel * model,
|
|
|
|
GtkTreePath * path UNUSED,
|
|
|
|
GtkTreeIter * iter,
|
|
|
|
gpointer gstats )
|
|
|
|
{
|
|
|
|
tr_torrent * tor;
|
|
|
|
struct core_stats * stats = gstats;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
gtk_tree_model_get( model, iter, MC_TORRENT_RAW, &tor, -1 );
|
|
|
|
status = tr_torrentGetStatus( tor );
|
|
|
|
|
|
|
|
if( status == TR_STATUS_DOWNLOAD )
|
|
|
|
++stats->downloadCount;
|
|
|
|
else if( status == TR_STATUS_SEED )
|
|
|
|
++stats->seedingCount;
|
2007-05-23 06:25:15 +00:00
|
|
|
|
2008-04-01 02:36:41 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_core_get_stats( const TrCore * core,
|
|
|
|
struct core_stats * setme )
|
2008-02-09 17:29:05 +00:00
|
|
|
{
|
2008-04-01 02:36:41 +00:00
|
|
|
memset( setme, 0, sizeof( struct core_stats ) );
|
|
|
|
|
|
|
|
if( !isDisposed( core ) )
|
|
|
|
{
|
|
|
|
tr_torrentRates( core->priv->handle,
|
|
|
|
&setme->clientDownloadSpeed,
|
|
|
|
&setme->clientUploadSpeed );
|
|
|
|
gtk_tree_model_foreach( core->priv->model,
|
|
|
|
statsForeach,
|
|
|
|
setme );
|
|
|
|
}
|
2007-05-23 01:47:42 +00:00
|
|
|
}
|
|
|
|
|
2007-12-19 02:46:30 +00:00
|
|
|
static char*
|
|
|
|
doCollate( const char * in )
|
|
|
|
{
|
|
|
|
const char * end = in + strlen( in );
|
|
|
|
char * casefold;
|
|
|
|
char * ret;
|
|
|
|
|
|
|
|
while( in < end ) {
|
|
|
|
const gunichar ch = g_utf8_get_char( in );
|
2008-03-16 15:18:19 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( in == end )
|
|
|
|
return g_strdup ("");
|
|
|
|
|
|
|
|
casefold = g_utf8_casefold( in, end-in );
|
|
|
|
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
|
|
|
|
tr_core_add_torrent( TrCore * self, TrTorrent * tor )
|
2007-10-16 14:16:01 +00:00
|
|
|
{
|
2007-12-01 20:07:53 +00:00
|
|
|
const tr_info * inf = tr_torrent_info( tor );
|
2007-12-28 06:12:53 +00:00
|
|
|
const tr_stat * torStat = tr_torrent_stat( tor );
|
2007-12-19 02:46:30 +00:00
|
|
|
char * collated = doCollate( inf->name );
|
2008-02-09 17:29:05 +00:00
|
|
|
GtkListStore * store = GTK_LIST_STORE( tr_core_model( self ) );
|
2007-12-03 19:43:21 +00:00
|
|
|
GtkTreeIter unused;
|
2008-02-13 03:00:21 +00:00
|
|
|
|
2008-02-09 17:29:05 +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,
|
|
|
|
MC_HASH, inf->hashString,
|
|
|
|
MC_TORRENT, tor,
|
2008-02-11 15:27:36 +00:00
|
|
|
MC_TORRENT_RAW, tr_torrent_handle( tor ),
|
2007-12-28 06:12:53 +00:00
|
|
|
MC_STATUS, torStat->status,
|
2008-02-09 17:29:05 +00:00
|
|
|
MC_ID, self->priv->nextid,
|
2007-12-01 20:07:53 +00:00
|
|
|
-1);
|
2008-02-13 03:00:21 +00:00
|
|
|
++self->priv->nextid;
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
g_object_unref( G_OBJECT( tor ) );
|
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-02-08 03:00:19 +00:00
|
|
|
tr_core_load( TrCore * self, gboolean forcePaused )
|
2007-05-23 01:47:42 +00:00
|
|
|
{
|
2007-07-23 03:03:45 +00:00
|
|
|
int i;
|
2007-06-27 18:34:38 +00:00
|
|
|
int count = 0;
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_torrent ** torrents;
|
2007-09-27 20:57:58 +00:00
|
|
|
char * path;
|
2007-12-21 22:18:40 +00:00
|
|
|
tr_ctor * ctor;
|
2007-05-23 02:45:28 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
path = getdownloaddir( );
|
2007-07-23 03:03:45 +00:00
|
|
|
|
2008-02-09 17:29:05 +00:00
|
|
|
ctor = tr_ctorNew( tr_core_handle( self ) );
|
2008-02-08 03:00:19 +00:00
|
|
|
if( forcePaused )
|
|
|
|
tr_ctorSetPaused( ctor, TR_FORCE, TRUE );
|
2007-12-21 22:18:40 +00:00
|
|
|
tr_ctorSetDestination( ctor, TR_FALLBACK, path );
|
2008-02-14 17:18:00 +00:00
|
|
|
tr_ctorSetMaxConnectedPeers( ctor, TR_FALLBACK,
|
|
|
|
pref_int_get( PREF_KEY_MAX_PEERS_PER_TORRENT ) );
|
2007-12-21 22:18:40 +00:00
|
|
|
|
2008-02-09 17:29:05 +00:00
|
|
|
torrents = tr_loadTorrents ( tr_core_handle( self ), ctor, &count );
|
2007-07-23 03:03:45 +00:00
|
|
|
for( i=0; i<count; ++i )
|
2008-02-13 03:00:21 +00:00
|
|
|
tr_core_add_torrent( self, tr_torrent_new_preexisting( torrents[i] ) );
|
2007-05-23 02:45:28 +00:00
|
|
|
|
2007-12-21 22:18:40 +00:00
|
|
|
tr_free( torrents );
|
|
|
|
tr_ctorFree( ctor );
|
2007-09-27 20:57:58 +00:00
|
|
|
g_free( path );
|
2007-12-21 22:18:40 +00:00
|
|
|
|
2007-05-23 02:45:28 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
static void
|
2008-03-18 01:22:11 +00:00
|
|
|
tr_core_errsig( TrCore * core, enum tr_core_err type, const char * msg )
|
2007-10-16 14:16:01 +00:00
|
|
|
{
|
2008-03-18 01:22:11 +00:00
|
|
|
g_signal_emit( core, TR_CORE_GET_CLASS(core)->errsig, 0, type, msg );
|
2007-10-16 14:16:01 +00:00
|
|
|
}
|
|
|
|
|
2008-02-13 03:00:21 +00:00
|
|
|
void
|
|
|
|
tr_core_add_ctor( TrCore * self, tr_ctor * ctor )
|
2007-05-24 02:50:28 +00:00
|
|
|
{
|
2008-02-13 03:00:21 +00:00
|
|
|
TrTorrent * tor;
|
2008-02-14 17:18:00 +00:00
|
|
|
char * errstr = NULL;
|
2007-05-24 02:50:28 +00:00
|
|
|
|
2008-02-13 03:00:21 +00:00
|
|
|
tr_core_apply_defaults( ctor );
|
2008-02-14 17:18:00 +00:00
|
|
|
|
|
|
|
if(( tor = tr_torrent_new_ctor( tr_core_handle( self ), ctor, &errstr )))
|
|
|
|
tr_core_add_torrent( self, tor );
|
|
|
|
else{
|
2008-02-13 03:00:21 +00:00
|
|
|
tr_core_errsig( self, TR_CORE_ERR_ADD_TORRENT, errstr );
|
|
|
|
g_free( errstr );
|
2007-05-24 02:50:28 +00:00
|
|
|
}
|
2008-03-18 01:22:11 +00:00
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_ctorFree( ctor );
|
2007-05-24 02:50:28 +00:00
|
|
|
}
|
|
|
|
|
2008-02-13 03:00:21 +00:00
|
|
|
void
|
2008-03-19 14:54:32 +00:00
|
|
|
tr_core_add_list( TrCore * core,
|
|
|
|
GSList * torrentFiles,
|
|
|
|
pref_flag_t start,
|
|
|
|
pref_flag_t prompt )
|
2007-05-24 10:37:07 +00:00
|
|
|
{
|
2008-03-19 14:54:32 +00:00
|
|
|
const gboolean doStart = pref_flag_eval( start, PREF_KEY_START );
|
|
|
|
const gboolean doPrompt = pref_flag_eval( prompt, PREF_KEY_OPTIONS_PROMPT );
|
|
|
|
|
2008-03-18 01:22:11 +00:00
|
|
|
if( torrentFiles && !isDisposed( core ) )
|
2007-05-24 10:37:07 +00:00
|
|
|
{
|
2008-03-18 01:22:11 +00:00
|
|
|
GSList * l;
|
2008-03-18 01:59:03 +00:00
|
|
|
tr_handle * handle = core->priv->handle;
|
2008-03-18 01:22:11 +00:00
|
|
|
|
|
|
|
for( l=torrentFiles; l!=NULL; l=l->next )
|
|
|
|
{
|
2008-03-18 01:59:03 +00:00
|
|
|
tr_ctor * ctor = tr_ctorNew( handle );
|
2008-03-18 01:22:11 +00:00
|
|
|
tr_core_apply_defaults( ctor );
|
2008-03-19 14:54:32 +00:00
|
|
|
tr_ctorSetPaused( ctor, TR_FORCE, !doStart );
|
2008-03-18 01:22:11 +00:00
|
|
|
if( tr_ctorSetMetainfoFromFile( ctor, l->data ) )
|
|
|
|
tr_ctorFree( ctor );
|
2008-03-18 01:59:03 +00:00
|
|
|
else if( tr_torrentParse( handle, ctor, NULL ) )
|
|
|
|
tr_ctorFree( ctor );
|
2008-03-18 01:22:11 +00:00
|
|
|
else if( doPrompt )
|
|
|
|
g_signal_emit( core, TR_CORE_GET_CLASS(core)->promptsig, 0, ctor );
|
|
|
|
else
|
|
|
|
tr_core_add_ctor( core, ctor );
|
|
|
|
}
|
2007-05-24 10:37:07 +00:00
|
|
|
}
|
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 );
|
|
|
|
}
|
|
|
|
|
2007-05-23 01:47:42 +00:00
|
|
|
void
|
2007-05-23 06:25:15 +00:00
|
|
|
tr_core_delete_torrent( TrCore * self, GtkTreeIter * iter )
|
2007-05-23 01:47:42 +00:00
|
|
|
{
|
2007-05-23 06:25:15 +00:00
|
|
|
TrTorrent * tor;
|
2008-02-09 17:29:05 +00:00
|
|
|
GtkTreeModel * model = tr_core_model( self );
|
2007-05-23 06:25:15 +00:00
|
|
|
|
2008-02-09 17:29:05 +00:00
|
|
|
gtk_tree_model_get( model, iter, MC_TORRENT, &tor, -1 );
|
|
|
|
gtk_list_store_remove( GTK_LIST_STORE( model ), iter );
|
2007-07-23 03:03:45 +00:00
|
|
|
tr_torrentRemoveSaved( tr_torrent_handle( tor ) );
|
2007-05-23 06:25:15 +00:00
|
|
|
|
2008-01-12 16:45:32 +00:00
|
|
|
g_object_unref( G_OBJECT( tor ) );
|
2007-05-23 01:47:42 +00:00
|
|
|
}
|
|
|
|
|
2008-02-19 03:57:03 +00:00
|
|
|
static gboolean
|
|
|
|
findTorrentInModel( TrCore * core, const TrTorrent * gtor, GtkTreeIter * setme )
|
|
|
|
{
|
|
|
|
int match = 0;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeModel * model = tr_core_model( core );
|
|
|
|
|
|
|
|
if( gtk_tree_model_iter_children( model, &iter, NULL ) ) do
|
|
|
|
{
|
|
|
|
TrTorrent * tmp;
|
|
|
|
gtk_tree_model_get( model, &iter, MC_TORRENT, &tmp, -1 );
|
|
|
|
match = tmp == gtor;
|
|
|
|
g_object_unref( G_OBJECT( tmp ) );
|
|
|
|
}
|
|
|
|
while( !match && gtk_tree_model_iter_next( model, &iter ) );
|
|
|
|
|
|
|
|
if( match )
|
|
|
|
*setme = iter;
|
|
|
|
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_core_remove_torrent( TrCore * self, TrTorrent * gtor, int deleteFiles )
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeModel * model = tr_core_model( self );
|
|
|
|
|
|
|
|
/* remove from the gui */
|
|
|
|
if( findTorrentInModel( self, gtor, &iter ) )
|
|
|
|
gtk_list_store_remove( GTK_LIST_STORE( model ), &iter );
|
|
|
|
|
|
|
|
/* maybe delete the downloaded files */
|
|
|
|
if( deleteFiles )
|
|
|
|
tr_torrent_delete_files( gtor );
|
|
|
|
|
|
|
|
/* delete the torrent */
|
2008-02-19 05:03:56 +00:00
|
|
|
tr_torrent_set_delete_flag( gtor, TRUE );
|
2008-02-19 03:57:03 +00:00
|
|
|
g_object_unref( G_OBJECT( gtor ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-09 17:29:05 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-11-23 15:36:31 +00:00
|
|
|
static gboolean
|
|
|
|
update_foreach( GtkTreeModel * model,
|
|
|
|
GtkTreePath * path UNUSED,
|
|
|
|
GtkTreeIter * iter,
|
2008-04-01 02:36:41 +00:00
|
|
|
gpointer data UNUSED )
|
2007-05-23 01:47:42 +00:00
|
|
|
{
|
2007-12-28 06:12:53 +00:00
|
|
|
int oldStatus;
|
2008-04-01 02:36:41 +00:00
|
|
|
int newStatus;
|
|
|
|
TrTorrent * gtor;
|
2008-01-16 16:03:18 +00:00
|
|
|
|
2008-04-01 02:36:41 +00:00
|
|
|
/* maybe update the status column in the model */
|
|
|
|
gtk_tree_model_get( model, iter,
|
|
|
|
MC_TORRENT, >or,
|
|
|
|
MC_STATUS, &oldStatus,
|
|
|
|
-1 );
|
|
|
|
newStatus = tr_torrentGetStatus( tr_torrent_handle( gtor ) );
|
|
|
|
if( newStatus != oldStatus )
|
2007-12-28 06:12:53 +00:00
|
|
|
gtk_list_store_set( GTK_LIST_STORE( model ), iter,
|
2008-04-01 02:36:41 +00:00
|
|
|
MC_STATUS, newStatus,
|
2007-12-28 06:12:53 +00:00
|
|
|
-1 );
|
|
|
|
|
2008-04-01 02:36:41 +00:00
|
|
|
/* check the seeding cap */
|
2007-12-27 06:59:48 +00:00
|
|
|
tr_torrent_check_seeding_cap ( gtor );
|
2007-05-23 01:47:42 +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 )
|
|
|
|
{
|
|
|
|
int column;
|
|
|
|
GtkSortType order;
|
|
|
|
GtkTreeSortable * sortable;
|
2008-02-09 17:29:05 +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 );
|
|
|
|
gtk_tree_sortable_set_sort_column_id( sortable, GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, order );
|
|
|
|
|
|
|
|
/* 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 );
|
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-03-18 01:22:11 +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
|
|
|
|
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-03-18 01:22:11 +00:00
|
|
|
commitPrefsChange( TrCore * core, const char * key )
|
2007-09-27 20:57:58 +00:00
|
|
|
{
|
|
|
|
pref_save( NULL );
|
2008-03-18 01:22:11 +00:00
|
|
|
g_signal_emit( core, TR_CORE_GET_CLASS(core)->prefsig, 0, key );
|
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
|
|
|
|
tr_core_set_pref( TrCore * self, const char * key, const char * newval )
|
|
|
|
{
|
|
|
|
char * oldval = pref_string_get( key );
|
|
|
|
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
|
|
|
}
|
2007-09-27 20:57:58 +00:00
|
|
|
g_free( oldval );
|
2007-05-24 13:55:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-27 20:57:58 +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 );
|
|
|
|
if( oldval != newval )
|
|
|
|
{
|
|
|
|
pref_flag_set( key, newval );
|
|
|
|
commitPrefsChange( self, key );
|
|
|
|
}
|
2007-05-24 13:55:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-27 20:57:58 +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 );
|
|
|
|
if( oldval != newval )
|
|
|
|
{
|
|
|
|
pref_int_set( key, newval );
|
|
|
|
commitPrefsChange( self, key );
|
|
|
|
}
|
2007-05-24 13:55:57 +00:00
|
|
|
}
|