2006-08-13 00:26:52 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2007-02-07 07:35:33 +00:00
|
|
|
* Copyright (c) 2006-2007 Transmission authors and contributors
|
2006-08-13 00:26:52 +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.
|
|
|
|
*****************************************************************************/
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
|
2007-07-18 23:04:26 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/bencode.h>
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-03-31 19:19:27 +00:00
|
|
|
#include "tr_prefs.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "tr_torrent.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
enum {
|
|
|
|
TR_TORRENT_HANDLE = 1,
|
|
|
|
TR_TORRENT_DIR,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
tr_torrent_init(GTypeInstance *instance, gpointer g_class);
|
|
|
|
static void
|
|
|
|
tr_torrent_set_property(GObject *object, guint property_id,
|
|
|
|
const GValue *value, GParamSpec *pspec);
|
|
|
|
static void
|
|
|
|
tr_torrent_get_property(GObject *object, guint property_id,
|
|
|
|
GValue *value, GParamSpec *pspec);
|
|
|
|
static void
|
|
|
|
tr_torrent_class_init(gpointer g_class, gpointer g_class_data);
|
|
|
|
static void
|
|
|
|
tr_torrent_dispose(GObject *obj);
|
|
|
|
static void
|
|
|
|
tr_torrent_set_folder(TrTorrent *tor);
|
|
|
|
|
2007-04-04 00:32:58 +00:00
|
|
|
static gpointer
|
|
|
|
tracker_boxed_fake_copy( gpointer boxed )
|
|
|
|
{
|
|
|
|
return boxed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tracker_boxed_fake_free( gpointer boxed SHUTUP )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GType
|
|
|
|
tr_tracker_boxed_get_type( void )
|
|
|
|
{
|
|
|
|
static GType type = 0;
|
|
|
|
|
|
|
|
if( 0 == type )
|
|
|
|
{
|
|
|
|
type = g_boxed_type_register_static( "TrTrackerBoxed",
|
|
|
|
tracker_boxed_fake_copy,
|
|
|
|
tracker_boxed_fake_free );
|
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
GType
|
|
|
|
tr_torrent_get_type(void) {
|
|
|
|
static GType type = 0;
|
|
|
|
|
|
|
|
if(0 == type) {
|
|
|
|
static const GTypeInfo info = {
|
|
|
|
sizeof (TrTorrentClass),
|
|
|
|
NULL, /* base_init */
|
|
|
|
NULL, /* base_finalize */
|
|
|
|
tr_torrent_class_init, /* class_init */
|
|
|
|
NULL, /* class_finalize */
|
|
|
|
NULL, /* class_data */
|
|
|
|
sizeof (TrTorrent),
|
|
|
|
0, /* n_preallocs */
|
|
|
|
tr_torrent_init, /* instance_init */
|
|
|
|
NULL,
|
|
|
|
};
|
2007-02-07 07:35:33 +00:00
|
|
|
type = g_type_register_static(G_TYPE_OBJECT, "TrTorrent", &info, 0);
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tr_torrent_class_init(gpointer g_class, gpointer g_class_data SHUTUP) {
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS(g_class);
|
|
|
|
GParamSpec *pspec;
|
|
|
|
|
|
|
|
gobject_class->set_property = tr_torrent_set_property;
|
|
|
|
gobject_class->get_property = tr_torrent_get_property;
|
|
|
|
gobject_class->dispose = tr_torrent_dispose;
|
|
|
|
|
|
|
|
pspec = g_param_spec_pointer("torrent-handle", "Torrent handle",
|
|
|
|
"Torrent handle from libtransmission",
|
|
|
|
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
|
|
|
|
g_object_class_install_property(gobject_class, TR_TORRENT_HANDLE, pspec);
|
|
|
|
|
|
|
|
pspec = g_param_spec_string("download-directory", "Download directory",
|
|
|
|
"Directory to download files to", NULL,
|
|
|
|
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
|
|
|
|
g_object_class_install_property(gobject_class, TR_TORRENT_DIR, pspec);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tr_torrent_init(GTypeInstance *instance, gpointer g_class SHUTUP) {
|
|
|
|
TrTorrent *self = (TrTorrent *)instance;
|
|
|
|
|
2007-05-23 06:25:15 +00:00
|
|
|
#ifdef REFDBG
|
|
|
|
fprintf( stderr, "torrent %p init\n", self );
|
|
|
|
#endif
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
self->handle = NULL;
|
2007-06-26 18:45:03 +00:00
|
|
|
self->lastStatTime = 0;
|
2006-07-16 19:39:23 +00:00
|
|
|
self->dir = NULL;
|
|
|
|
self->delfile = NULL;
|
2007-05-23 06:25:15 +00:00
|
|
|
self->severed = FALSE;
|
2006-07-16 19:39:23 +00:00
|
|
|
self->disposed = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tr_torrent_set_property(GObject *object, guint property_id,
|
|
|
|
const GValue *value, GParamSpec *pspec) {
|
|
|
|
TrTorrent *self = (TrTorrent*)object;
|
|
|
|
|
2007-05-23 06:25:15 +00:00
|
|
|
if(self->severed)
|
2006-07-16 19:39:23 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
switch(property_id) {
|
|
|
|
case TR_TORRENT_HANDLE:
|
|
|
|
g_assert(NULL == self->handle);
|
|
|
|
self->handle = g_value_get_pointer(value);
|
|
|
|
if(NULL != self->handle && NULL != self->dir)
|
|
|
|
tr_torrent_set_folder(self);
|
|
|
|
break;
|
|
|
|
case TR_TORRENT_DIR:
|
|
|
|
g_assert(NULL == self->dir);
|
|
|
|
self->dir = g_value_dup_string(value);
|
|
|
|
if(NULL != self->handle && NULL != self->dir)
|
|
|
|
tr_torrent_set_folder(self);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tr_torrent_get_property(GObject *object, guint property_id,
|
|
|
|
GValue *value, GParamSpec *pspec) {
|
|
|
|
TrTorrent *self = (TrTorrent*)object;
|
|
|
|
|
2007-05-23 06:25:15 +00:00
|
|
|
if(self->severed)
|
2006-07-16 19:39:23 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
switch(property_id) {
|
|
|
|
case TR_TORRENT_HANDLE:
|
|
|
|
g_value_set_pointer(value, self->handle);
|
|
|
|
break;
|
|
|
|
case TR_TORRENT_DIR:
|
|
|
|
g_value_set_string(value, (NULL != self->dir ? self->dir :
|
|
|
|
tr_torrentGetFolder(self->handle)));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tr_torrent_dispose(GObject *obj) {
|
|
|
|
GObjectClass *parent = g_type_class_peek(g_type_parent(TR_TORRENT_TYPE));
|
|
|
|
TrTorrent *self = (TrTorrent*)obj;
|
|
|
|
|
|
|
|
if(self->disposed)
|
|
|
|
return;
|
|
|
|
self->disposed = TRUE;
|
|
|
|
|
2007-05-23 06:25:15 +00:00
|
|
|
#ifdef REFDBG
|
|
|
|
fprintf( stderr, "torrent %p dispose\n", self );
|
|
|
|
#endif
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-05-23 06:25:15 +00:00
|
|
|
if( !self->severed )
|
|
|
|
tr_torrent_sever( self );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-06 00:30:13 +00:00
|
|
|
g_free (self->delfile);
|
|
|
|
g_free (self->dir);
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
/* Chain up to the parent class */
|
|
|
|
parent->dispose(obj);
|
|
|
|
}
|
|
|
|
|
2007-05-23 06:25:15 +00:00
|
|
|
void
|
|
|
|
tr_torrent_sever( TrTorrent * self )
|
|
|
|
{
|
2007-05-28 15:23:28 +00:00
|
|
|
g_return_if_fail (TR_IS_TORRENT( self ));
|
2007-05-23 06:25:15 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
if( !self->severed )
|
2007-05-23 06:25:15 +00:00
|
|
|
{
|
|
|
|
self->severed = TRUE;
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
if( self->handle )
|
|
|
|
tr_torrentClose( self->handle );
|
2007-05-23 06:25:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_torrent *
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrent_handle(TrTorrent *tor)
|
|
|
|
{
|
|
|
|
g_assert( TR_IS_TORRENT(tor) );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
return tor->severed ? NULL : tor->handle;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
static tr_stat*
|
2007-06-28 17:43:53 +00:00
|
|
|
refreshStat( TrTorrent * tor )
|
|
|
|
{
|
|
|
|
tor->lastStatTime= time( NULL );
|
|
|
|
tor->stat = *tr_torrentStat( tor->handle );
|
|
|
|
return &tor->stat;
|
|
|
|
}
|
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
const tr_stat *
|
2007-06-26 18:45:03 +00:00
|
|
|
tr_torrent_stat(TrTorrent *tor)
|
|
|
|
{
|
|
|
|
g_assert( TR_IS_TORRENT(tor) );
|
|
|
|
|
2007-06-28 17:43:53 +00:00
|
|
|
if( !tor->severed && tor->lastStatTime!=time(NULL) )
|
|
|
|
refreshStat( tor );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
return &tor->stat;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
const tr_info *
|
2006-07-16 19:39:23 +00:00
|
|
|
tr_torrent_info(TrTorrent *tor) {
|
|
|
|
TR_IS_TORRENT(tor);
|
|
|
|
|
2007-05-23 06:25:15 +00:00
|
|
|
if(tor->severed)
|
2006-07-16 19:39:23 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return tr_torrentInfo(tor->handle);
|
|
|
|
}
|
|
|
|
|
2007-05-24 09:18:03 +00:00
|
|
|
void
|
|
|
|
tr_torrent_start( TrTorrent * self )
|
|
|
|
{
|
|
|
|
TR_IS_TORRENT( self );
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
if( !self->severed )
|
|
|
|
tr_torrentStart( self->handle );
|
2007-05-24 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_torrent_stop( TrTorrent * self )
|
|
|
|
{
|
|
|
|
TR_IS_TORRENT( self );
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
if( !self->severed )
|
|
|
|
tr_torrentStop( self->handle );
|
2007-05-24 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
2007-05-23 19:26:29 +00:00
|
|
|
static TrTorrent *
|
2007-09-20 20:14:13 +00:00
|
|
|
maketorrent( tr_torrent * handle )
|
2007-05-23 19:26:29 +00:00
|
|
|
{
|
|
|
|
tr_torrentDisablePex( handle,
|
|
|
|
!tr_prefs_get_bool_with_default( PREF_ID_PEX ) );
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
return g_object_new( TR_TORRENT_TYPE,
|
|
|
|
"torrent-handle", handle,
|
|
|
|
NULL);
|
2007-05-23 19:26:29 +00:00
|
|
|
}
|
|
|
|
|
2007-07-23 03:03:45 +00:00
|
|
|
TrTorrent*
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_torrent_new_preexisting( tr_torrent * tor )
|
2007-07-23 03:03:45 +00:00
|
|
|
{
|
|
|
|
return maketorrent( tor );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
TrTorrent *
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_torrent_new( tr_handle * back, const char *torrent, const char *dir,
|
2007-05-23 19:26:29 +00:00
|
|
|
enum tr_torrent_action act, gboolean paused, char **err )
|
|
|
|
{
|
2006-07-16 19:39:23 +00:00
|
|
|
TrTorrent *ret;
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_torrent *handle;
|
2007-06-26 18:45:03 +00:00
|
|
|
int errcode, flags;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
g_assert(NULL != dir);
|
|
|
|
|
|
|
|
*err = NULL;
|
|
|
|
|
|
|
|
errcode = -1;
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
flags = ( TR_TOR_COPY == act || TR_TOR_MOVE == act ? TR_FLAG_SAVE : 0 );
|
|
|
|
if( paused )
|
|
|
|
flags |= TR_FLAG_PAUSED;
|
|
|
|
|
2007-06-27 05:14:38 +00:00
|
|
|
handle = tr_torrentInit( back, torrent, dir, flags, &errcode );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
if(NULL == handle) {
|
|
|
|
switch(errcode) {
|
|
|
|
case TR_EINVALID:
|
|
|
|
*err = g_strdup_printf(_("%s: not a valid torrent file"), torrent);
|
|
|
|
break;
|
|
|
|
case TR_EDUPLICATE:
|
|
|
|
*err = g_strdup_printf(_("%s: torrent is already open"), torrent);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*err = g_strdup(torrent);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
ret = maketorrent( handle );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-05-23 19:26:29 +00:00
|
|
|
if( TR_TOR_MOVE == act )
|
2006-07-16 19:39:23 +00:00
|
|
|
ret->delfile = g_strdup(torrent);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-05-24 10:37:07 +00:00
|
|
|
TrTorrent *
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_torrent_new_with_data( tr_handle * back, uint8_t * data, size_t size,
|
2007-05-24 10:37:07 +00:00
|
|
|
const char * dir, gboolean paused, char ** err )
|
|
|
|
{
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_torrent * handle;
|
|
|
|
int errcode;
|
|
|
|
int flags;
|
2007-05-24 10:37:07 +00:00
|
|
|
|
|
|
|
g_assert( NULL != dir );
|
|
|
|
|
|
|
|
*err = NULL;
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
flags = TR_FLAG_SAVE;
|
|
|
|
if( paused )
|
|
|
|
flags |= TR_FLAG_PAUSED;
|
|
|
|
|
2007-05-24 10:37:07 +00:00
|
|
|
errcode = -1;
|
2007-06-27 05:14:38 +00:00
|
|
|
handle = tr_torrentInitData( back, data, size, dir, flags, &errcode );
|
2007-05-24 10:37:07 +00:00
|
|
|
|
|
|
|
if( NULL == handle )
|
|
|
|
{
|
|
|
|
switch( errcode )
|
|
|
|
{
|
|
|
|
case TR_EINVALID:
|
|
|
|
*err = g_strdup( _("not a valid torrent file") );
|
|
|
|
break;
|
|
|
|
case TR_EDUPLICATE:
|
|
|
|
*err = g_strdup( _("torrent is already open") );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*err = g_strdup( "" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
return maketorrent( handle );
|
2007-05-24 10:37:07 +00:00
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
TrTorrent *
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_torrent_new_with_state( tr_handle * back, benc_val_t * state,
|
2007-05-23 19:26:29 +00:00
|
|
|
gboolean forcedpause, char ** err )
|
2007-02-19 22:09:05 +00:00
|
|
|
{
|
2007-06-06 00:30:13 +00:00
|
|
|
TrTorrent * ret;
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_torrent * handle;
|
2007-05-23 19:26:29 +00:00
|
|
|
int ii, errcode;
|
2007-06-26 18:45:03 +00:00
|
|
|
int flags;
|
2006-07-16 19:39:23 +00:00
|
|
|
benc_val_t *name, *data;
|
|
|
|
char *torrent, *hash, *dir;
|
2007-06-06 00:30:13 +00:00
|
|
|
gboolean paused = FALSE;
|
|
|
|
gboolean seeding_cap_enabled = FALSE;
|
|
|
|
gdouble seeding_cap = 0.0;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
*err = NULL;
|
|
|
|
|
|
|
|
if(TYPE_DICT != state->type)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
torrent = hash = dir = NULL;
|
|
|
|
|
|
|
|
for(ii = 0; ii + 1 < state->val.l.count; ii += 2) {
|
|
|
|
name = state->val.l.vals + ii;
|
|
|
|
data = state->val.l.vals + ii + 1;
|
|
|
|
if(TYPE_STR == name->type &&
|
|
|
|
(TYPE_STR == data->type || TYPE_INT == data->type)) {
|
2007-06-06 00:30:13 +00:00
|
|
|
char * key = name->val.s.s;
|
|
|
|
char * val = data->val.s.s;
|
|
|
|
if (!strcmp (key, "torrent")) torrent = val;
|
|
|
|
else if (!strcmp (key, "hash")) hash = val;
|
|
|
|
else if (!strcmp (key, "dir")) dir = val;
|
|
|
|
else if (!strcmp (key, "paused")) paused = !!data->val.i;
|
|
|
|
else if (!strcmp (key, "seeding-cap-ratio")) seeding_cap = (data->val.i / 100.0);
|
|
|
|
else if (!strcmp (key, "seeding-cap-enabled")) seeding_cap_enabled = !!data->val.i;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if((NULL != torrent && NULL != hash) ||
|
|
|
|
(NULL == torrent && NULL == hash) || NULL == dir)
|
|
|
|
return NULL;
|
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
flags = 0;
|
2007-06-28 17:43:53 +00:00
|
|
|
if( paused || forcedpause )
|
2007-06-26 18:45:03 +00:00
|
|
|
flags |= TR_FLAG_PAUSED;
|
|
|
|
|
2007-05-23 19:26:29 +00:00
|
|
|
if( NULL != hash )
|
2007-06-26 18:45:03 +00:00
|
|
|
handle = tr_torrentInitSaved(back, hash, dir, flags, &errcode);
|
2007-05-23 19:26:29 +00:00
|
|
|
else
|
2007-06-27 05:14:38 +00:00
|
|
|
handle = tr_torrentInit(back, torrent, dir, flags, &errcode);
|
2007-05-23 19:26:29 +00:00
|
|
|
|
|
|
|
if(NULL == handle) {
|
|
|
|
torrent = ( NULL == hash ? torrent : hash );
|
|
|
|
switch(errcode) {
|
|
|
|
case TR_EINVALID:
|
|
|
|
*err = g_strdup_printf(_("%s: not a valid torrent file"), torrent);
|
|
|
|
break;
|
|
|
|
case TR_EDUPLICATE:
|
|
|
|
*err = g_strdup_printf(_("%s: torrent is already open"), torrent);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*err = g_strdup(torrent);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
2007-02-19 22:09:05 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
ret = maketorrent( handle );
|
2007-06-06 00:30:13 +00:00
|
|
|
ret->seeding_cap = seeding_cap;
|
|
|
|
ret->seeding_cap_enabled = seeding_cap_enabled;
|
|
|
|
return ret;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-05-23 17:59:35 +00:00
|
|
|
gboolean
|
|
|
|
tr_torrent_get_state( TrTorrent * tor, benc_val_t * state )
|
|
|
|
{
|
2007-09-20 20:14:13 +00:00
|
|
|
const tr_info * inf;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-05-23 17:59:35 +00:00
|
|
|
TR_IS_TORRENT( tor );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-05-23 17:59:35 +00:00
|
|
|
if( tor->severed )
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-05-23 17:59:35 +00:00
|
|
|
inf = tr_torrentInfo( tor->handle );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-05-23 17:59:35 +00:00
|
|
|
tr_bencInit( state, TYPE_DICT );
|
|
|
|
if( tr_bencDictReserve( state, 3 ) )
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-05-23 17:59:35 +00:00
|
|
|
if( TR_FLAG_SAVE & inf->flags )
|
|
|
|
{
|
|
|
|
tr_bencInitStr( tr_bencDictAdd( state, "hash" ),
|
|
|
|
inf->hashString, -1, 1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_bencInitStr( tr_bencDictAdd( state, "torrent" ),
|
|
|
|
inf->torrent, -1, 1 );
|
|
|
|
}
|
|
|
|
tr_bencInitStr( tr_bencDictAdd( state, "dir" ),
|
|
|
|
tr_torrentGetFolder( tor->handle ), -1, 1 );
|
2007-06-28 17:43:53 +00:00
|
|
|
tr_bencInitInt( tr_bencDictAdd( state, "paused" ),
|
|
|
|
(refreshStat(tor)->status & TR_STATUS_INACTIVE) ? 1 : 0);
|
2007-06-06 00:30:13 +00:00
|
|
|
tr_bencInitInt( tr_bencDictAdd( state, "seeding-cap-ratio" ),
|
2007-07-20 03:24:04 +00:00
|
|
|
(int)(tor->seeding_cap * 100.0)); /* two decimal places */
|
2007-06-06 00:30:13 +00:00
|
|
|
tr_bencInitInt( tr_bencDictAdd( state, "seeding-cap-enabled" ),
|
|
|
|
tor->seeding_cap_enabled ? 1 : 0);
|
2007-05-23 17:59:35 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX this should probably be done with a signal */
|
|
|
|
void
|
|
|
|
tr_torrent_state_saved(TrTorrent *tor) {
|
|
|
|
TR_IS_TORRENT(tor);
|
|
|
|
|
2007-05-23 06:25:15 +00:00
|
|
|
if(tor->severed)
|
2006-07-16 19:39:23 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if(NULL != tor->delfile) {
|
|
|
|
unlink(tor->delfile);
|
|
|
|
g_free(tor->delfile);
|
|
|
|
tor->delfile = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tr_torrent_set_folder(TrTorrent *tor) {
|
|
|
|
char *wd;
|
|
|
|
|
|
|
|
if(NULL != tor->dir)
|
|
|
|
tr_torrentSetFolder(tor->handle, tor->dir);
|
|
|
|
else {
|
|
|
|
wd = g_new(char, MAX_PATH_LENGTH + 1);
|
|
|
|
tr_torrentSetFolder(tor->handle,
|
|
|
|
(NULL == getcwd(wd, MAX_PATH_LENGTH + 1) ? "." : wd));
|
|
|
|
g_free(wd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-06 00:30:13 +00:00
|
|
|
void
|
2007-07-18 05:27:45 +00:00
|
|
|
tr_torrent_check_seeding_cap ( TrTorrent *gtor)
|
|
|
|
{
|
2007-09-20 20:14:13 +00:00
|
|
|
const tr_stat * st = tr_torrent_stat( gtor );
|
2007-06-06 00:30:13 +00:00
|
|
|
if ((gtor->seeding_cap_enabled) && (st->ratio >= gtor->seeding_cap))
|
|
|
|
tr_torrent_stop (gtor);
|
|
|
|
}
|
|
|
|
void
|
2007-07-18 05:27:45 +00:00
|
|
|
tr_torrent_set_seeding_cap_ratio ( TrTorrent *gtor, gdouble ratio )
|
|
|
|
{
|
2007-06-06 00:30:13 +00:00
|
|
|
gtor->seeding_cap = ratio;
|
|
|
|
tr_torrent_check_seeding_cap (gtor);
|
|
|
|
}
|
|
|
|
void
|
2007-07-18 05:27:45 +00:00
|
|
|
tr_torrent_set_seeding_cap_enabled ( TrTorrent *gtor, gboolean b )
|
|
|
|
{
|
2007-06-06 00:30:13 +00:00
|
|
|
if ((gtor->seeding_cap_enabled = b))
|
|
|
|
tr_torrent_check_seeding_cap (gtor);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
tr_torrent_status_str ( TrTorrent * gtor )
|
|
|
|
{
|
|
|
|
char * top = 0;
|
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
const tr_stat * st = tr_torrent_stat( gtor );
|
2007-06-06 00:30:13 +00:00
|
|
|
|
2007-07-15 17:19:07 +00:00
|
|
|
const int tpeers = MAX (st->peersConnected, 0);
|
2007-07-16 00:50:43 +00:00
|
|
|
const int upeers = MAX (st->peersGettingFromUs, 0);
|
2007-06-06 00:30:13 +00:00
|
|
|
const int eta = st->eta;
|
2007-07-24 01:33:59 +00:00
|
|
|
double prog = st->percentDone * 100.0; /* [0...100] */
|
2007-06-06 00:30:13 +00:00
|
|
|
|
2007-06-26 18:45:03 +00:00
|
|
|
switch( st->status )
|
2007-06-06 00:30:13 +00:00
|
|
|
{
|
2007-06-26 18:45:03 +00:00
|
|
|
case TR_STATUS_CHECK_WAIT:
|
2007-07-24 01:33:59 +00:00
|
|
|
prog = st->recheckProgress * 100.0; /* [0...100] */
|
|
|
|
top = g_strdup_printf( _("Waiting to check existing files (checked %.1f%%)"), prog );
|
2007-06-26 18:45:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_CHECK:
|
2007-07-24 01:33:59 +00:00
|
|
|
prog = st->recheckProgress * 100.0; /* [0...100] */
|
|
|
|
top = g_strdup_printf( _("Checking existing files (checked %.1f%%)"), prog );
|
2007-06-26 18:45:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_DOWNLOAD:
|
|
|
|
if( eta < 0 )
|
|
|
|
top = g_strdup_printf( _("Stalled (%.1f%%)"), prog );
|
|
|
|
else {
|
|
|
|
char * timestr = readabletime(eta);
|
2007-07-08 05:51:55 +00:00
|
|
|
top = g_strdup_printf( _("%s remaining (%.1f%%)"), timestr, prog );
|
2007-06-26 18:45:03 +00:00
|
|
|
g_free(timestr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_DONE:
|
|
|
|
top = g_strdup_printf(
|
|
|
|
ngettext( "Uploading to %d of %d peer",
|
|
|
|
"Uploading to %d of %d peers", tpeers ),
|
|
|
|
upeers, tpeers );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_SEED:
|
|
|
|
top = g_strdup_printf(
|
2007-07-08 05:51:55 +00:00
|
|
|
ngettext( "Seeding to %d of %d peer",
|
|
|
|
"Seeding to %d of %d peers", tpeers ),
|
2007-06-26 18:45:03 +00:00
|
|
|
upeers, tpeers );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_STOPPING:
|
|
|
|
top = g_strdup( _("Stopping...") );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_STOPPED:
|
2007-06-30 13:53:10 +00:00
|
|
|
top = g_strdup_printf( _("Stopped (%.1f%%)"), prog );
|
2007-06-26 18:45:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
top = g_strdup_printf("Unrecognized state: %d", st->status );
|
|
|
|
break;
|
|
|
|
|
2007-06-06 00:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return top;
|
|
|
|
}
|