2007-05-23 00:49:31 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Transmission authors and contributors
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2007-08-04 01:34:00 +00:00
|
|
|
#include <inttypes.h>
|
2007-07-19 03:55:00 +00:00
|
|
|
#include <stdio.h>
|
2007-05-23 00:49:31 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
|
2007-07-18 23:04:26 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2007-07-23 03:03:45 +00:00
|
|
|
#include <libtransmission/utils.h>
|
2007-05-23 01:47:42 +00:00
|
|
|
|
2007-05-23 04:39:06 +00:00
|
|
|
#include "conf.h"
|
2007-05-23 00:49:31 +00:00
|
|
|
#include "tr_core.h"
|
2007-05-24 02:50:28 +00:00
|
|
|
#include "tr_prefs.h"
|
2007-05-23 00:49:31 +00:00
|
|
|
#include "tr_torrent.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void
|
2007-05-23 17:59:35 +00:00
|
|
|
tr_core_marshal_err( GClosure * closure, GValue * ret SHUTUP, guint count,
|
|
|
|
const GValue * vals, gpointer hint SHUTUP,
|
|
|
|
gpointer marshal )
|
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
g_return_if_fail( 3 == count );
|
|
|
|
|
|
|
|
inst = g_value_peek_pointer( vals );
|
|
|
|
errcode = g_value_get_int( vals + 1 );
|
|
|
|
errstr = g_value_get_string( vals + 2 );
|
|
|
|
gdata = closure->data;
|
|
|
|
|
|
|
|
callback = (TRMarshalErr) ( NULL == marshal ?
|
|
|
|
cclosure->callback : marshal );
|
|
|
|
callback( inst, errcode, errstr, gdata );
|
2007-05-23 00:49:31 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
static void
|
2007-05-24 02:50:28 +00:00
|
|
|
tr_core_marshal_prompt( GClosure * closure, GValue * ret SHUTUP, guint count,
|
|
|
|
const GValue * vals, gpointer hint SHUTUP,
|
|
|
|
gpointer marshal )
|
|
|
|
{
|
|
|
|
typedef void (*TRMarshalPrompt)
|
|
|
|
( gpointer, GList *, enum tr_torrent_action, gboolean, gpointer );
|
|
|
|
TRMarshalPrompt callback;
|
|
|
|
GCClosure * cclosure = (GCClosure*) closure;
|
|
|
|
GList * paths;
|
|
|
|
enum tr_torrent_action action;
|
|
|
|
gboolean paused;
|
|
|
|
gpointer inst, gdata;
|
|
|
|
|
|
|
|
g_return_if_fail( 4 == count );
|
|
|
|
|
|
|
|
inst = g_value_peek_pointer( vals );
|
|
|
|
paths = g_value_get_pointer( vals + 1 );
|
|
|
|
action = g_value_get_int( vals + 2 );
|
|
|
|
paused = g_value_get_boolean( vals + 3 );
|
|
|
|
gdata = closure->data;
|
|
|
|
|
|
|
|
callback = (TRMarshalPrompt) ( NULL == marshal ?
|
|
|
|
cclosure->callback : marshal );
|
|
|
|
callback( inst, paths, action, paused, gdata );
|
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
static void
|
2007-05-24 10:37:07 +00:00
|
|
|
tr_core_marshal_data( GClosure * closure, GValue * ret SHUTUP, guint count,
|
|
|
|
const GValue * vals, gpointer hint SHUTUP,
|
|
|
|
gpointer marshal )
|
|
|
|
{
|
|
|
|
typedef void (*TRMarshalPrompt)
|
|
|
|
( gpointer, uint8_t *, size_t, gboolean, gpointer );
|
|
|
|
TRMarshalPrompt callback;
|
|
|
|
GCClosure * cclosure = (GCClosure*) closure;
|
|
|
|
uint8_t * data;
|
|
|
|
size_t size;
|
|
|
|
gboolean paused;
|
|
|
|
gpointer inst, gdata;
|
|
|
|
|
|
|
|
g_return_if_fail( 4 == count );
|
|
|
|
|
|
|
|
inst = g_value_peek_pointer( vals );
|
2007-05-27 16:31:16 +00:00
|
|
|
data = (uint8_t *) g_value_get_string( vals + 1 );
|
2007-05-24 10:37:07 +00:00
|
|
|
size = g_value_get_uint( vals + 2 );
|
|
|
|
paused = g_value_get_boolean( vals + 3 );
|
|
|
|
gdata = closure->data;
|
|
|
|
|
|
|
|
callback = (TRMarshalPrompt) ( NULL == marshal ?
|
|
|
|
cclosure->callback : marshal );
|
|
|
|
callback( inst, data, size, paused, gdata );
|
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
static void
|
|
|
|
tr_core_dispose( GObject * obj )
|
|
|
|
{
|
|
|
|
TrCore * self = (TrCore *) obj;
|
|
|
|
GObjectClass * parent;
|
|
|
|
|
|
|
|
if( self->disposed )
|
|
|
|
return;
|
|
|
|
|
2007-11-09 16:11:10 +00:00
|
|
|
self->disposed = TRUE;
|
2007-10-16 14:16:01 +00:00
|
|
|
pref_save( NULL );
|
|
|
|
parent = g_type_class_peek( g_type_parent( TR_CORE_TYPE ) );
|
|
|
|
parent->dispose( obj );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
tr_core_class_init( gpointer g_class, gpointer g_class_data SHUTUP )
|
|
|
|
{
|
|
|
|
GObjectClass * gobject_class;
|
|
|
|
TrCoreClass * core_class;
|
|
|
|
|
|
|
|
gobject_class = G_OBJECT_CLASS( g_class );
|
|
|
|
gobject_class->dispose = tr_core_dispose;
|
|
|
|
|
|
|
|
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 );
|
|
|
|
core_class->promptsig = g_signal_new( "directory-prompt",
|
|
|
|
G_TYPE_FROM_CLASS( g_class ),
|
|
|
|
G_SIGNAL_RUN_LAST, 0, NULL, NULL,
|
|
|
|
tr_core_marshal_prompt, G_TYPE_NONE,
|
|
|
|
3, G_TYPE_POINTER, G_TYPE_INT,
|
|
|
|
G_TYPE_BOOLEAN );
|
|
|
|
core_class->promptdatasig = g_signal_new( "directory-prompt-data",
|
|
|
|
G_TYPE_FROM_CLASS( g_class ),
|
|
|
|
G_SIGNAL_RUN_LAST, 0, NULL, NULL,
|
|
|
|
tr_core_marshal_data,
|
|
|
|
G_TYPE_NONE, 3, G_TYPE_STRING,
|
|
|
|
G_TYPE_UINT, G_TYPE_BOOLEAN );
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
compareProgress( GtkTreeModel * model,
|
|
|
|
GtkTreeIter * a,
|
|
|
|
GtkTreeIter * b,
|
|
|
|
gpointer user_data UNUSED )
|
|
|
|
{
|
2007-10-16 14:32:16 +00:00
|
|
|
int ia, ib;
|
2007-10-16 14:16:01 +00:00
|
|
|
gfloat rateUpA, rateUpB;
|
|
|
|
gfloat rateDownA, rateDownB;
|
|
|
|
gfloat percentDoneA, percentDoneB;
|
2007-10-16 14:32:16 +00:00
|
|
|
guint64 uploadedEverA, uploadedEverB;
|
2007-10-16 14:16:01 +00:00
|
|
|
|
|
|
|
gtk_tree_model_get( model, a, MC_PROG_D, &percentDoneA,
|
|
|
|
MC_DRATE, &rateDownA,
|
|
|
|
MC_URATE, &rateUpA,
|
2007-10-16 14:32:16 +00:00
|
|
|
MC_UP, &uploadedEverA,
|
2007-10-16 14:16:01 +00:00
|
|
|
-1 );
|
|
|
|
gtk_tree_model_get( model, b, MC_PROG_D, &percentDoneB,
|
|
|
|
MC_DRATE, &rateDownB,
|
|
|
|
MC_URATE, &rateUpB,
|
2007-10-16 14:32:16 +00:00
|
|
|
MC_UP, &uploadedEverB,
|
2007-10-16 14:16:01 +00:00
|
|
|
-1 );
|
2007-10-16 14:32:16 +00:00
|
|
|
|
2007-10-18 21:32:37 +00:00
|
|
|
ia = (int)( rateUpA + rateDownA );
|
|
|
|
ib = (int)( rateUpB + rateDownB );
|
2007-10-16 14:16:01 +00:00
|
|
|
if( ia != ib )
|
|
|
|
return ia - ib;
|
|
|
|
|
2007-10-18 21:32:37 +00:00
|
|
|
ia = (int)( 100.0 * percentDoneA );
|
|
|
|
ib = (int)( 100.0 * percentDoneB );
|
2007-10-16 14:16:01 +00:00
|
|
|
if( ia != ib )
|
|
|
|
return ia - ib;
|
|
|
|
|
2007-10-16 14:32:16 +00:00
|
|
|
if( uploadedEverA != uploadedEverB )
|
|
|
|
return uploadedEverA < uploadedEverB ? -1 : 1;
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-17 18:31:12 +00:00
|
|
|
#define STR_REVERSE "reverse-"
|
|
|
|
#define STR_PROGRESS "progress"
|
|
|
|
#define STR_NAME "name"
|
|
|
|
|
|
|
|
static void
|
|
|
|
onSortColumnChanged( GtkTreeSortable * sortable, gpointer unused UNUSED )
|
|
|
|
{
|
|
|
|
int column;
|
|
|
|
GtkSortType order;
|
|
|
|
if( gtk_tree_sortable_get_sort_column_id( sortable, &column, &order ) )
|
|
|
|
{
|
|
|
|
GString * gstr = g_string_new( NULL );
|
|
|
|
switch( column ) {
|
|
|
|
case MC_PROG_D: g_string_assign( gstr, STR_PROGRESS ); break;
|
|
|
|
default: g_string_assign( gstr, STR_NAME ); break;
|
|
|
|
}
|
|
|
|
if( order == GTK_SORT_DESCENDING )
|
|
|
|
g_string_prepend( gstr, STR_REVERSE );
|
|
|
|
pref_string_set( PREF_KEY_SORT_COLUMN, gstr->str );
|
|
|
|
g_string_free( gstr, TRUE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_core_set_sort_column_from_prefs( TrCore * core )
|
|
|
|
{
|
|
|
|
char * val = pref_string_get( PREF_KEY_SORT_COLUMN );
|
|
|
|
char * freeme = val;
|
|
|
|
gint column;
|
|
|
|
GtkSortType order = GTK_SORT_ASCENDING;
|
|
|
|
if( g_str_has_prefix( val, STR_REVERSE ) ) {
|
|
|
|
order = GTK_SORT_DESCENDING;
|
|
|
|
val += strlen( STR_REVERSE );
|
|
|
|
}
|
|
|
|
if( !strcmp( val, STR_PROGRESS ) )
|
|
|
|
column = MC_PROG_D;
|
|
|
|
else /* default */
|
|
|
|
column = MC_NAME;
|
|
|
|
gtk_tree_sortable_set_sort_column_id ( GTK_TREE_SORTABLE( core->model ), column, order );
|
|
|
|
g_free( freeme );
|
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
static void
|
2007-05-23 00:49:31 +00:00
|
|
|
tr_core_init( GTypeInstance * instance, gpointer g_class SHUTUP )
|
|
|
|
{
|
|
|
|
TrCore * self = (TrCore *) instance;
|
|
|
|
GtkListStore * store;
|
|
|
|
|
|
|
|
/* column types for the model used to store torrent information */
|
|
|
|
/* keep this in sync with the enum near the bottom of tr_core.h */
|
|
|
|
GType types[] =
|
|
|
|
{
|
2007-05-24 09:18:03 +00:00
|
|
|
/* info->name, info->totalSize, info->hashString, status, */
|
|
|
|
G_TYPE_STRING, G_TYPE_UINT64, G_TYPE_STRING, G_TYPE_INT,
|
2007-06-18 03:40:41 +00:00
|
|
|
/* error, errorString, percentComplete, percentDone, rateDownload, rateUpload, */
|
|
|
|
G_TYPE_INT, G_TYPE_STRING, G_TYPE_FLOAT, G_TYPE_FLOAT, G_TYPE_FLOAT, G_TYPE_FLOAT,
|
2007-07-15 17:19:07 +00:00
|
|
|
/* eta, peersConnected, peersUploading, peersDownloading, seeders, */
|
|
|
|
G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT,
|
2007-05-24 09:18:03 +00:00
|
|
|
/* leechers, completedFromTracker, downloaded, uploaded */
|
|
|
|
G_TYPE_INT, G_TYPE_INT, G_TYPE_UINT64, G_TYPE_UINT64,
|
2007-11-15 04:19:53 +00:00
|
|
|
/* ratio, left, TrTorrent object, ID for IPC */
|
|
|
|
G_TYPE_FLOAT, G_TYPE_UINT64, TR_TORRENT_TYPE, G_TYPE_INT,
|
2007-05-23 00:49:31 +00:00
|
|
|
};
|
|
|
|
|
2007-05-23 06:25:15 +00:00
|
|
|
#ifdef REFDBG
|
|
|
|
fprintf( stderr, "core %p init\n", self );
|
|
|
|
#endif
|
|
|
|
|
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-17 18:31:12 +00:00
|
|
|
g_signal_connect( store, "sort-column-changed", G_CALLBACK(onSortColumnChanged), NULL );
|
2007-05-23 00:49:31 +00:00
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
gtk_tree_sortable_set_sort_func( GTK_TREE_SORTABLE(store),
|
|
|
|
MC_PROG_D,
|
|
|
|
compareProgress,
|
|
|
|
NULL, NULL );
|
|
|
|
|
2007-05-23 00:49:31 +00:00
|
|
|
self->model = GTK_TREE_MODEL( store );
|
2007-05-23 04:39:06 +00:00
|
|
|
self->handle = tr_init( "gtk" );
|
2007-05-24 07:51:37 +00:00
|
|
|
self->nextid = 1;
|
2007-05-23 01:47:42 +00:00
|
|
|
self->quitting = FALSE;
|
2007-05-23 00:49:31 +00:00
|
|
|
self->disposed = FALSE;
|
|
|
|
}
|
|
|
|
|
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-10-16 14:16:01 +00:00
|
|
|
if( 0 == 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 )
|
|
|
|
{
|
|
|
|
return g_object_new( TR_CORE_TYPE, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkTreeModel *
|
|
|
|
tr_core_model( TrCore * self )
|
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
g_return_val_if_fail (TR_IS_CORE(self), NULL);
|
2007-05-23 00:49:31 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
return self->disposed ? NULL : self->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 *
|
2007-05-23 01:47:42 +00:00
|
|
|
tr_core_handle( TrCore * self )
|
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
g_return_val_if_fail (TR_IS_CORE(self), NULL);
|
2007-05-23 06:25:15 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
return self->disposed ? NULL : self->handle;
|
2007-05-23 01:47:42 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
static void
|
|
|
|
tr_core_insert( TrCore * self, TrTorrent * tor )
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
const tr_info * inf;
|
|
|
|
|
|
|
|
gtk_list_store_append( GTK_LIST_STORE( self->model ), &iter );
|
|
|
|
inf = tr_torrent_info( tor );
|
|
|
|
gtk_list_store_set( GTK_LIST_STORE( self->model ), &iter,
|
|
|
|
MC_NAME, inf->name,
|
|
|
|
MC_SIZE, inf->totalSize,
|
|
|
|
MC_HASH, inf->hashString,
|
|
|
|
MC_TORRENT, tor,
|
|
|
|
MC_ID, self->nextid,
|
|
|
|
-1);
|
|
|
|
g_object_unref( tor );
|
|
|
|
self->nextid++;
|
|
|
|
}
|
|
|
|
|
2007-05-23 02:45:28 +00:00
|
|
|
int
|
2007-10-15 20:58:39 +00:00
|
|
|
tr_core_load( TrCore * self, gboolean paused )
|
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-05-23 02:45:28 +00:00
|
|
|
|
2007-05-23 01:47:42 +00:00
|
|
|
TR_IS_CORE( self );
|
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
path = getdownloaddir( );
|
2007-07-23 03:03:45 +00:00
|
|
|
|
2007-10-26 03:02:23 +00:00
|
|
|
torrents = tr_loadTorrents ( self->handle, path, paused, &count );
|
2007-07-23 03:03:45 +00:00
|
|
|
for( i=0; i<count; ++i )
|
|
|
|
tr_core_insert( self, tr_torrent_new_preexisting( torrents[i] ) );
|
|
|
|
tr_free( torrents );
|
2007-05-23 02:45:28 +00:00
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
g_free( path );
|
2007-05-23 02:45:28 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2007-05-24 02:50:28 +00:00
|
|
|
tr_core_add( TrCore * self, const char * path, enum tr_torrent_action act,
|
|
|
|
gboolean paused )
|
|
|
|
{
|
|
|
|
GList * list;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
TR_IS_CORE( self );
|
|
|
|
|
|
|
|
list = g_list_append( NULL, (void *) path );
|
|
|
|
ret = tr_core_add_list( self, list, act, paused );
|
|
|
|
g_list_free( list );
|
|
|
|
|
|
|
|
return 1 == ret;
|
|
|
|
}
|
|
|
|
|
2007-10-16 14:16:01 +00:00
|
|
|
static void
|
|
|
|
tr_core_errsig( TrCore * self, enum tr_core_err type, const char * msg )
|
|
|
|
{
|
|
|
|
TrCoreClass * class;
|
|
|
|
|
|
|
|
class = g_type_class_peek( TR_CORE_TYPE );
|
|
|
|
g_signal_emit( self, class->errsig, 0, type, msg );
|
|
|
|
}
|
|
|
|
|
2007-05-24 02:50:28 +00:00
|
|
|
gboolean
|
|
|
|
tr_core_add_dir( TrCore * self, const char * path, const char * dir,
|
|
|
|
enum tr_torrent_action act, gboolean paused )
|
2007-05-23 02:45:28 +00:00
|
|
|
{
|
|
|
|
TrTorrent * tor;
|
2007-05-23 17:59:35 +00:00
|
|
|
char * errstr;
|
2007-05-23 02:45:28 +00:00
|
|
|
|
|
|
|
TR_IS_CORE( self );
|
|
|
|
|
2007-05-23 17:59:35 +00:00
|
|
|
errstr = NULL;
|
2007-05-23 19:26:29 +00:00
|
|
|
tor = tr_torrent_new( self->handle, path, dir, act, paused, &errstr );
|
2007-05-23 02:45:28 +00:00
|
|
|
if( NULL == tor )
|
|
|
|
{
|
2007-05-23 17:59:35 +00:00
|
|
|
tr_core_errsig( self, TR_CORE_ERR_ADD_TORRENT, errstr );
|
|
|
|
g_free( errstr );
|
2007-05-23 02:45:28 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2007-05-23 17:59:35 +00:00
|
|
|
g_assert( NULL == errstr );
|
2007-05-23 02:45:28 +00:00
|
|
|
|
|
|
|
tr_core_insert( self, tor );
|
|
|
|
|
|
|
|
return TRUE;
|
2007-05-23 01:47:42 +00:00
|
|
|
}
|
|
|
|
|
2007-05-24 02:50:28 +00:00
|
|
|
int
|
|
|
|
tr_core_add_list( TrCore * self, GList * paths, enum tr_torrent_action act,
|
|
|
|
gboolean paused )
|
|
|
|
{
|
2007-09-27 20:57:58 +00:00
|
|
|
char * dir;
|
|
|
|
int count;
|
2007-05-24 02:50:28 +00:00
|
|
|
|
|
|
|
TR_IS_CORE( self );
|
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
if( pref_flag_get( PREF_KEY_DIR_ASK ) )
|
2007-05-24 02:50:28 +00:00
|
|
|
{
|
2007-09-27 20:57:58 +00:00
|
|
|
TrCoreClass * class = g_type_class_peek( TR_CORE_TYPE );
|
2007-05-24 02:50:28 +00:00
|
|
|
g_signal_emit( self, class->promptsig, 0, paths, act, paused );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
dir = getdownloaddir();
|
2007-05-24 02:50:28 +00:00
|
|
|
count = 0;
|
2007-06-27 18:34:38 +00:00
|
|
|
for( ; paths; paths=paths->next )
|
2007-09-27 20:57:58 +00:00
|
|
|
if( tr_core_add_dir( self, paths->data, dir, act, paused ) )
|
2007-05-24 02:50:28 +00:00
|
|
|
count++;
|
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
g_free( dir );
|
2007-05-24 02:50:28 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2007-05-24 10:37:07 +00:00
|
|
|
gboolean
|
|
|
|
tr_core_add_data( TrCore * self, uint8_t * data, size_t size, gboolean paused )
|
|
|
|
{
|
2007-09-27 20:57:58 +00:00
|
|
|
gboolean ret;
|
|
|
|
char * path;
|
2007-05-24 10:37:07 +00:00
|
|
|
TR_IS_CORE( self );
|
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
if( pref_flag_get( PREF_KEY_DIR_ASK ) )
|
2007-05-24 10:37:07 +00:00
|
|
|
{
|
2007-06-27 18:34:38 +00:00
|
|
|
TrCoreClass * class = g_type_class_peek( TR_CORE_TYPE );
|
2007-05-24 10:37:07 +00:00
|
|
|
g_signal_emit( self, class->promptdatasig, 0, data, size, paused );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-09-27 20:57:58 +00:00
|
|
|
path = getdownloaddir( );
|
|
|
|
ret = tr_core_add_data_dir( self, data, size, path, paused );
|
|
|
|
g_free( path );
|
|
|
|
return ret;
|
2007-05-24 10:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
tr_core_add_data_dir( TrCore * self, uint8_t * data, size_t size,
|
|
|
|
const char * dir, gboolean paused )
|
|
|
|
{
|
|
|
|
TrTorrent * tor;
|
2007-06-27 18:34:38 +00:00
|
|
|
char * errstr = NULL;
|
2007-05-24 10:37:07 +00:00
|
|
|
|
|
|
|
TR_IS_CORE( self );
|
|
|
|
|
|
|
|
tor = tr_torrent_new_with_data( self->handle, data, size, dir,
|
|
|
|
paused, &errstr );
|
|
|
|
if( NULL == tor )
|
|
|
|
{
|
|
|
|
tr_core_errsig( self, TR_CORE_ERR_ADD_TORRENT, errstr );
|
|
|
|
g_free( errstr );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
g_assert( NULL == errstr );
|
|
|
|
|
|
|
|
tr_core_insert( self, tor );
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2007-05-23 17:59:35 +00:00
|
|
|
void
|
|
|
|
tr_core_torrents_added( TrCore * self )
|
|
|
|
{
|
|
|
|
TR_IS_CORE( 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;
|
|
|
|
|
2007-05-23 01:47:42 +00:00
|
|
|
TR_IS_CORE( self );
|
|
|
|
|
2007-05-23 06:25:15 +00:00
|
|
|
gtk_tree_model_get( self->model, iter, MC_TORRENT, &tor, -1 );
|
|
|
|
gtk_list_store_remove( GTK_LIST_STORE( self->model ), iter );
|
2007-07-23 03:03:45 +00:00
|
|
|
tr_torrentRemoveSaved( tr_torrent_handle( tor ) );
|
2007-05-23 06:25:15 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrent_sever( tor );
|
2007-05-23 01:47:42 +00:00
|
|
|
}
|
|
|
|
|
2007-05-23 02:45:28 +00:00
|
|
|
void
|
|
|
|
tr_core_update( TrCore * self )
|
2007-05-23 01:47:42 +00:00
|
|
|
{
|
2007-05-23 02:45:28 +00:00
|
|
|
GtkTreeIter iter;
|
|
|
|
TrTorrent * tor;
|
2007-09-20 20:14:13 +00:00
|
|
|
const tr_stat * st;
|
2007-05-23 02:45:28 +00:00
|
|
|
|
2007-05-23 01:47:42 +00:00
|
|
|
TR_IS_CORE( self );
|
|
|
|
|
2007-06-27 18:34:38 +00:00
|
|
|
if( gtk_tree_model_get_iter_first( self->model, &iter ) ) do
|
2007-05-23 02:45:28 +00:00
|
|
|
{
|
2007-06-27 18:34:38 +00:00
|
|
|
gtk_tree_model_get( self->model, &iter, MC_TORRENT, &tor, -1 );
|
|
|
|
st = tr_torrent_stat( tor );
|
|
|
|
g_object_unref( tor );
|
|
|
|
tr_torrent_check_seeding_cap ( tor );
|
|
|
|
|
|
|
|
/* XXX find out if setting the same data emits changed signal */
|
|
|
|
gtk_list_store_set( GTK_LIST_STORE( self->model ), &iter,
|
|
|
|
MC_STAT, st->status,
|
|
|
|
MC_ERR, st->error,
|
|
|
|
MC_TERR, st->errorString,
|
|
|
|
MC_PROG_C, st->percentComplete,
|
|
|
|
MC_PROG_D, st->percentDone,
|
|
|
|
MC_DRATE, st->rateDownload,
|
|
|
|
MC_URATE, st->rateUpload,
|
|
|
|
MC_ETA, st->eta,
|
2007-07-15 17:19:07 +00:00
|
|
|
MC_PEERS, st->peersConnected,
|
2007-07-16 00:50:43 +00:00
|
|
|
MC_UPEERS, st->peersGettingFromUs,
|
|
|
|
MC_DPEERS, st->peersSendingToUs,
|
2007-06-27 18:34:38 +00:00
|
|
|
MC_SEED, st->seeders,
|
|
|
|
MC_LEECH, st->leechers,
|
|
|
|
MC_DONE, st->completedFromTracker,
|
2007-09-26 01:55:04 +00:00
|
|
|
MC_DOWN, st->downloadedEver,
|
|
|
|
MC_UP, st->uploadedEver,
|
2007-11-15 04:19:53 +00:00
|
|
|
MC_RATIO, st->ratio,
|
2007-09-26 01:55:04 +00:00
|
|
|
MC_LEFT, st->leftUntilDone,
|
2007-06-27 18:34:38 +00:00
|
|
|
-1 );
|
2007-05-23 02:45:28 +00:00
|
|
|
}
|
2007-06-27 18:34:38 +00:00
|
|
|
while( gtk_tree_model_iter_next( self->model, &iter ) );
|
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
|
|
|
|
tr_core_quit( TrCore * self )
|
|
|
|
{
|
|
|
|
TrCoreClass * class;
|
|
|
|
|
|
|
|
TR_IS_CORE( self );
|
|
|
|
|
|
|
|
class = g_type_class_peek( TR_CORE_TYPE );
|
|
|
|
g_signal_emit( self, class->quitsig, 0 );
|
|
|
|
}
|
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
|
|
|
|
commitPrefsChange( TrCore * self, const char * key )
|
|
|
|
{
|
|
|
|
TrCoreClass * class = g_type_class_peek( TR_CORE_TYPE );
|
|
|
|
pref_save( NULL );
|
|
|
|
g_signal_emit( self, class->prefsig, 0, key );
|
|
|
|
}
|
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
|
|
|
}
|