2006-08-13 00:26:52 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-01-01 17:20:20 +00:00
|
|
|
* Copyright (c) 2006-2008 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>
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-02-26 19:58:03 +00:00
|
|
|
#include "tr-prefs.h"
|
|
|
|
#include "tr-torrent.h"
|
2007-09-27 20:57:58 +00:00
|
|
|
#include "conf.h"
|
2008-02-24 15:42:31 +00:00
|
|
|
#include "notify.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2008-02-11 15:27:36 +00:00
|
|
|
struct TrTorrentPrivate
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrent * handle;
|
|
|
|
gboolean do_remove;
|
2008-02-11 15:27:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-04-04 00:32:58 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrent_init( GTypeInstance * instance,
|
|
|
|
gpointer g_class UNUSED )
|
2007-04-04 00:32:58 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
TrTorrent * self = TR_TORRENT( instance );
|
2008-02-11 15:27:36 +00:00
|
|
|
struct TrTorrentPrivate * p;
|
|
|
|
|
|
|
|
p = self->priv = G_TYPE_INSTANCE_GET_PRIVATE( self,
|
|
|
|
TR_TORRENT_TYPE,
|
|
|
|
struct TrTorrentPrivate );
|
|
|
|
p->handle = NULL;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-05-23 06:25:15 +00:00
|
|
|
#ifdef REFDBG
|
2008-02-11 15:27:36 +00:00
|
|
|
g_message( "torrent %p init", self );
|
2007-05-23 06:25:15 +00:00
|
|
|
#endif
|
2008-02-11 15:27:36 +00:00
|
|
|
}
|
2007-05-23 06:25:15 +00:00
|
|
|
|
2008-02-11 15:27:36 +00:00
|
|
|
static int
|
2008-06-01 05:36:23 +00:00
|
|
|
isDisposed( const TrTorrent * tor )
|
2008-02-11 15:27:36 +00:00
|
|
|
{
|
2008-06-01 05:36:23 +00:00
|
|
|
return !tor || !TR_IS_TORRENT( tor ) || !tor->priv;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-02-11 15:27:36 +00:00
|
|
|
tr_torrent_dispose( GObject * o )
|
2007-10-12 20:50:03 +00:00
|
|
|
{
|
2008-02-13 03:00:21 +00:00
|
|
|
GObjectClass * parent;
|
2008-09-23 19:11:04 +00:00
|
|
|
TrTorrent * self = TR_TORRENT( o );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-02-11 15:27:36 +00:00
|
|
|
if( !isDisposed( self ) )
|
|
|
|
{
|
|
|
|
if( self->priv->handle )
|
2008-02-19 05:03:56 +00:00
|
|
|
{
|
2008-05-20 23:58:59 +00:00
|
|
|
if( self->priv->do_remove )
|
|
|
|
tr_torrentRemove( self->priv->handle );
|
2008-02-19 05:03:56 +00:00
|
|
|
else
|
2008-05-21 17:14:58 +00:00
|
|
|
tr_torrentFree( self->priv->handle );
|
2008-02-19 05:03:56 +00:00
|
|
|
}
|
|
|
|
|
2008-02-11 15:27:36 +00:00
|
|
|
self->priv = NULL;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
parent = g_type_class_peek( g_type_parent( TR_TORRENT_TYPE ) );
|
2008-02-11 15:27:36 +00:00
|
|
|
parent->dispose( o );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-07-16 19:38:22 +00:00
|
|
|
void
|
|
|
|
tr_torrent_clear( TrTorrent * tor )
|
|
|
|
{
|
|
|
|
g_return_if_fail( tor );
|
|
|
|
g_return_if_fail( tor->priv );
|
|
|
|
|
|
|
|
tor->priv->handle = NULL;
|
|
|
|
}
|
|
|
|
|
2007-10-12 20:50:03 +00:00
|
|
|
static void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrent_class_init( gpointer g_class,
|
|
|
|
gpointer g_class_data UNUSED )
|
2007-10-12 20:50:03 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS( g_class );
|
|
|
|
|
2008-02-11 15:27:36 +00:00
|
|
|
gobject_class->dispose = tr_torrent_dispose;
|
2008-02-13 03:00:21 +00:00
|
|
|
g_type_class_add_private( g_class, sizeof( struct TrTorrentPrivate ) );
|
2007-10-12 20:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GType
|
2008-02-13 03:00:21 +00:00
|
|
|
tr_torrent_get_type( void )
|
2007-10-12 20:50:03 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
static GType type = 0;
|
|
|
|
|
|
|
|
if( !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,
|
|
|
|
};
|
|
|
|
type = g_type_register_static( G_TYPE_OBJECT, "TrTorrent", &info, 0 );
|
|
|
|
}
|
|
|
|
return type;
|
2007-10-12 20:50:03 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_torrent *
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrent_handle( TrTorrent *tor )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
2008-02-11 15:27:36 +00:00
|
|
|
return isDisposed( tor ) ? NULL : tor->priv->handle;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
const tr_stat *
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrent_stat( TrTorrent *tor )
|
2007-06-26 18:45:03 +00:00
|
|
|
{
|
2008-02-11 15:27:36 +00:00
|
|
|
tr_torrent * handle = tr_torrent_handle( tor );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-02-11 15:27:36 +00:00
|
|
|
return handle ? tr_torrentStatCached( handle ) : NULL;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
const tr_info *
|
2008-02-11 15:27:36 +00:00
|
|
|
tr_torrent_info( TrTorrent * tor )
|
|
|
|
{
|
|
|
|
tr_torrent * handle = tr_torrent_handle( tor );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-02-11 15:27:36 +00:00
|
|
|
return handle ? tr_torrentInfo( handle ) : NULL;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2008-02-24 15:42:31 +00:00
|
|
|
static gboolean
|
|
|
|
notifyInMainThread( gpointer user_data )
|
|
|
|
{
|
2008-04-01 15:48:23 +00:00
|
|
|
tr_notify_send( TR_TORRENT( user_data ) );
|
2008-02-24 15:42:31 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-02-24 15:42:31 +00:00
|
|
|
static void
|
|
|
|
statusChangedCallback( tr_torrent * tor UNUSED,
|
2008-09-23 19:11:04 +00:00
|
|
|
cp_status_t status,
|
|
|
|
void * user_data )
|
2008-02-24 15:42:31 +00:00
|
|
|
{
|
|
|
|
if( status == TR_CP_COMPLETE )
|
|
|
|
g_idle_add( notifyInMainThread, user_data );
|
|
|
|
}
|
2008-09-23 19:11:04 +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
|
|
|
{
|
2007-10-12 20:50:03 +00:00
|
|
|
TrTorrent * tor = g_object_new( TR_TORRENT_TYPE, NULL );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-02-11 15:27:36 +00:00
|
|
|
tor->priv->handle = handle;
|
2008-02-24 15:42:31 +00:00
|
|
|
tr_torrentSetStatusCallback( handle, statusChangedCallback, tor );
|
2007-10-12 20:50:03 +00:00
|
|
|
return tor;
|
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 *
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrent_new_ctor( tr_handle * handle,
|
|
|
|
tr_ctor * ctor,
|
|
|
|
char ** err )
|
2007-05-23 19:26:29 +00:00
|
|
|
{
|
2008-02-13 03:00:21 +00:00
|
|
|
tr_torrent * tor;
|
2008-09-23 19:11:04 +00:00
|
|
|
int errcode;
|
|
|
|
uint8_t doTrash = FALSE;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-02-13 03:00:21 +00:00
|
|
|
errcode = -1;
|
|
|
|
*err = NULL;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2008-07-02 00:51:04 +00:00
|
|
|
/* let the gtk client handle the removal, since libT
|
|
|
|
* doesn't have any concept of the glib trash API */
|
|
|
|
tr_ctorGetDeleteSource( ctor, &doTrash );
|
2008-03-09 15:27:08 +00:00
|
|
|
tr_ctorSetDeleteSource( ctor, FALSE );
|
2008-02-13 03:00:21 +00:00
|
|
|
tor = tr_torrentNew( handle, ctor, &errcode );
|
2008-03-09 15:27:08 +00:00
|
|
|
|
2008-07-02 00:51:04 +00:00
|
|
|
if( tor && doTrash )
|
2008-10-02 17:10:01 +00:00
|
|
|
{
|
|
|
|
const char * config = tr_sessionGetConfigDir( handle );
|
|
|
|
const char * source = tr_ctorGetSourceFile( ctor );
|
|
|
|
const int is_internal = source && ( strstr( source, config ) == source );
|
|
|
|
|
|
|
|
/* #1294: don't delete the source .torrent file if it's our internal copy */
|
|
|
|
if( !is_internal )
|
|
|
|
tr_file_trash_or_unlink( source );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-02-13 03:00:21 +00:00
|
|
|
if( !tor )
|
|
|
|
{
|
|
|
|
const char * filename = tr_ctorGetSourceFile( ctor );
|
|
|
|
if( !filename )
|
|
|
|
filename = "(null)";
|
|
|
|
|
|
|
|
switch( errcode )
|
|
|
|
{
|
|
|
|
case TR_EINVALID:
|
2008-09-23 19:11:04 +00:00
|
|
|
*err =
|
|
|
|
g_strdup_printf( _(
|
|
|
|
"File \"%s\" isn't a valid torrent" ),
|
|
|
|
filename );
|
|
|
|
break;
|
|
|
|
|
2008-02-13 03:00:21 +00:00
|
|
|
case TR_EDUPLICATE:
|
2008-09-23 19:11:04 +00:00
|
|
|
*err = g_strdup_printf( _(
|
|
|
|
"File \"%s\" is already open" ),
|
|
|
|
filename );
|
2008-02-13 03:00:21 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-02-13 03:00:21 +00:00
|
|
|
default:
|
|
|
|
*err = g_strdup( filename );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2007-05-24 10:37:07 +00:00
|
|
|
}
|
|
|
|
|
2008-02-13 03:00:21 +00:00
|
|
|
return maketorrent( tor );
|
2007-05-24 10:37:07 +00:00
|
|
|
}
|
|
|
|
|
2007-06-06 00:30:13 +00:00
|
|
|
char *
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrent_status_str( TrTorrent * gtor )
|
2007-06-06 00:30:13 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
char * top = NULL;
|
2007-06-06 00:30:13 +00:00
|
|
|
|
2007-09-20 20:14:13 +00:00
|
|
|
const tr_stat * st = tr_torrent_stat( gtor );
|
2007-06-06 00:30:13 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
const int tpeers = MAX ( st->peersConnected, 0 );
|
|
|
|
const int upeers = MAX ( st->peersGettingFromUs, 0 );
|
|
|
|
const int eta = st->eta;
|
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:
|
2008-09-23 19:11:04 +00:00
|
|
|
top =
|
|
|
|
g_strdup_printf( _(
|
|
|
|
"Waiting to verify local data (%.1f%% tested)" ),
|
2008-09-23 20:06:26 +00:00
|
|
|
st->recheckProgress * 100.0 );
|
2007-06-26 18:45:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_CHECK:
|
2008-09-23 19:11:04 +00:00
|
|
|
top =
|
|
|
|
g_strdup_printf( _(
|
|
|
|
"Verifying local data (%.1f%% tested)" ),
|
2008-09-23 20:06:26 +00:00
|
|
|
st->recheckProgress * 100.0 );
|
2007-06-26 18:45:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_DOWNLOAD:
|
2008-04-06 14:42:47 +00:00
|
|
|
|
2008-08-10 19:11:59 +00:00
|
|
|
if( eta < 0 )
|
2008-09-23 20:06:26 +00:00
|
|
|
top = g_strdup_printf( _( "Remaining time unknown" ) );
|
2008-09-23 19:11:04 +00:00
|
|
|
else
|
|
|
|
{
|
2007-12-26 06:38:33 +00:00
|
|
|
char timestr[128];
|
|
|
|
tr_strltime( timestr, eta, sizeof( timestr ) );
|
2008-09-23 20:06:26 +00:00
|
|
|
/* %s is # of minutes */
|
|
|
|
top = g_strdup_printf( _( "%1$s remaining" ), timestr );
|
2007-06-26 18:45:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_SEED:
|
|
|
|
top = g_strdup_printf(
|
2008-03-19 20:07:27 +00:00
|
|
|
ngettext( "Seeding to %1$'d of %2$'d connected peer",
|
2008-09-23 19:11:04 +00:00
|
|
|
"Seeding to %1$'d of %2$'d connected peers",
|
|
|
|
tpeers ),
|
|
|
|
upeers, tpeers );
|
2007-06-26 18:45:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_STATUS_STOPPED:
|
2008-09-23 20:06:26 +00:00
|
|
|
top = g_strdup( _( "Stopped" ) );
|
2007-06-26 18:45:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2008-09-23 20:06:26 +00:00
|
|
|
top = g_strdup( "???" );
|
2007-06-26 18:45:03 +00:00
|
|
|
break;
|
2007-06-06 00:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return top;
|
|
|
|
}
|
2008-02-19 03:57:03 +00:00
|
|
|
|
2008-02-19 05:03:56 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrent_set_remove_flag( TrTorrent * gtor,
|
|
|
|
gboolean do_remove )
|
2008-02-19 05:03:56 +00:00
|
|
|
{
|
|
|
|
if( !isDisposed( gtor ) )
|
2008-05-20 23:58:59 +00:00
|
|
|
gtor->priv->do_remove = do_remove;
|
2008-02-19 05:03:56 +00:00
|
|
|
}
|
|
|
|
|
2008-02-19 03:57:03 +00:00
|
|
|
void
|
|
|
|
tr_torrent_delete_files( TrTorrent * gtor )
|
|
|
|
{
|
2008-03-22 18:10:59 +00:00
|
|
|
tr_file_index_t i;
|
2008-02-19 03:57:03 +00:00
|
|
|
const tr_info * info = tr_torrent_info( gtor );
|
2008-09-23 19:11:04 +00:00
|
|
|
const char * stop =
|
|
|
|
tr_torrentGetDownloadDir( tr_torrent_handle( gtor ) );
|
2008-02-19 03:57:03 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; info && i < info->fileCount; ++i )
|
2008-02-19 03:57:03 +00:00
|
|
|
{
|
|
|
|
char * file = g_build_filename( stop, info->files[i].name, NULL );
|
2008-09-23 19:11:04 +00:00
|
|
|
while( strcmp( stop, file ) && strlen( stop ) < strlen( file ) )
|
2008-02-19 03:57:03 +00:00
|
|
|
{
|
|
|
|
char * swap = g_path_get_dirname( file );
|
2008-03-09 15:27:08 +00:00
|
|
|
tr_file_trash_or_unlink( file );
|
2008-02-19 03:57:03 +00:00
|
|
|
g_free( file );
|
|
|
|
file = swap;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-02-19 03:57:03 +00:00
|
|
|
g_free( file );
|
|
|
|
}
|
|
|
|
}
|
2008-04-11 02:21:33 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
tr_torrent_open_folder( TrTorrent * gtor )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_torrent * tor = tr_torrent_handle( gtor );
|
2008-04-11 02:21:33 +00:00
|
|
|
const tr_info * info = tr_torrent_info( gtor );
|
2008-09-23 19:11:04 +00:00
|
|
|
char * path = info->fileCount == 1
|
|
|
|
? g_build_filename( tr_torrentGetDownloadDir(
|
|
|
|
tor ), NULL )
|
|
|
|
: g_build_filename( tr_torrentGetDownloadDir(
|
|
|
|
tor ), info->name, NULL );
|
|
|
|
|
2008-04-11 02:21:33 +00:00
|
|
|
gtr_open_file( path );
|
|
|
|
g_free( path );
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|