1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-30 19:46:56 +00:00
transmission/gtk/tr-torrent.c

305 lines
8.5 KiB
C
Raw Normal View History

/******************************************************************************
* $Id$
*
2008-01-01 17:20:20 +00:00
* Copyright (c) 2006-2008 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.
*****************************************************************************/
2006-07-16 19:39:23 +00:00
#include <string.h>
#include <unistd.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <libtransmission/transmission.h>
#include <libtransmission/utils.h> /* tr_truncd() */
2006-07-16 19:39:23 +00:00
#include "tr-prefs.h"
#include "tr-torrent.h"
#include "conf.h"
#include "notify.h"
2006-07-16 19:39:23 +00:00
#include "util.h"
2008-02-11 15:27:36 +00:00
struct TrTorrentPrivate
{
tr_torrent * handle;
gboolean do_remove;
2008-02-11 15:27:36 +00:00
};
static void
tr_torrent_init( GTypeInstance * instance,
gpointer g_class UNUSED )
{
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
#ifdef REFDBG
2008-02-11 15:27:36 +00:00
g_message( "torrent %p init", self );
#endif
2008-02-11 15:27:36 +00:00
}
2008-02-11 15:27:36 +00:00
static int
isDisposed( const TrTorrent * tor )
2008-02-11 15:27:36 +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
{
GObjectClass * parent;
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 )
{
if( self->priv->do_remove )
tr_torrentRemove( self->priv->handle );
else
tr_torrentFree( self->priv->handle );
}
2008-02-11 15:27:36 +00:00
self->priv = NULL;
}
2006-07-16 19:39:23 +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
}
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
tr_torrent_class_init( gpointer g_class,
gpointer g_class_data UNUSED )
2007-10-12 20:50:03 +00:00
{
GObjectClass *gobject_class = G_OBJECT_CLASS( g_class );
2008-02-11 15:27:36 +00:00
gobject_class->dispose = tr_torrent_dispose;
g_type_class_add_private( g_class, sizeof( struct TrTorrentPrivate ) );
2007-10-12 20:50:03 +00:00
}
GType
tr_torrent_get_type( void )
2007-10-12 20:50:03 +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
}
tr_torrent *
tr_torrent_handle( TrTorrent *tor )
{
2008-02-11 15:27:36 +00:00
return isDisposed( tor ) ? NULL : tor->priv->handle;
2006-07-16 19:39:23 +00:00
}
const tr_stat *
tr_torrent_stat( TrTorrent *tor )
{
2008-02-11 15:27:36 +00:00
tr_torrent * handle = tr_torrent_handle( tor );
2008-02-11 15:27:36 +00:00
return handle ? tr_torrentStatCached( handle ) : NULL;
2006-07-16 19:39:23 +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-02-11 15:27:36 +00:00
return handle ? tr_torrentInfo( handle ) : NULL;
2006-07-16 19:39:23 +00:00
}
static gboolean
notifyInMainThread( gpointer user_data )
{
tr_notify_send( TR_TORRENT( user_data ) );
return FALSE;
}
static void
completenessChangedCallback( tr_torrent * tor,
tr_completeness completeness,
void * user_data )
{
if( ( completeness != TR_LEECH ) && ( tr_torrentStat( tor )->sizeWhenDone != 0 ) )
gtr_idle_add( notifyInMainThread, user_data );
}
2007-05-23 19:26:29 +00:00
static TrTorrent *
maketorrent( tr_torrent * tor )
2007-05-23 19:26:29 +00:00
{
TrTorrent * gtor = g_object_new( TR_TORRENT_TYPE, NULL );
gtor->priv->handle = tor;
tr_torrentSetCompletenessCallback( tor, completenessChangedCallback, gtor );
return gtor;
2007-05-23 19:26:29 +00:00
}
2007-07-23 03:03:45 +00:00
TrTorrent*
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-10-28 19:49:33 +00:00
tr_torrent_new_ctor( tr_session * session,
tr_ctor * ctor,
int * errcode )
2007-05-23 19:26:29 +00:00
{
tr_torrent * tor;
uint8_t doTrash = FALSE;
2006-07-16 19:39:23 +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 );
tr_ctorSetDeleteSource( ctor, FALSE );
tor = tr_torrentNew( ctor, errcode );
if( tor && doTrash )
{
2008-10-28 19:49:33 +00:00
const char * config = tr_sessionGetConfigDir( session );
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_remove( source );
}
return tor ? maketorrent( tor ) : NULL;
2007-05-24 10:37:07 +00:00
}
char *
tr_torrent_status_str( TrTorrent * gtor )
{
char * top = NULL;
const tr_stat * st = tr_torrent_stat( gtor );
const int tpeers = MAX ( st->peersConnected, 0 );
const int upeers = MAX ( st->peersGettingFromUs, 0 );
const int eta = st->eta;
switch( st->activity )
{
case TR_STATUS_CHECK_WAIT:
top =
g_strdup_printf( _( "Waiting to verify local data (%.1f%% tested)" ),
tr_truncd( 100 * st->recheckProgress, 1 ) );
break;
case TR_STATUS_CHECK:
top =
g_strdup_printf( _( "Verifying local data (%.1f%% tested)" ),
tr_truncd( 100 * st->recheckProgress, 1 ) );
break;
case TR_STATUS_DOWNLOAD:
if( eta < 0 )
top = g_strdup_printf( _( "Remaining time unknown" ) );
else
{
char timestr[128];
tr_strltime( timestr, eta, sizeof( timestr ) );
/* %s is # of minutes */
top = g_strdup_printf( _( "%1$s remaining" ), timestr );
}
break;
case TR_STATUS_SEED:
top = g_strdup_printf(
ngettext( "Seeding to %1$'d of %2$'d connected peer",
"Seeding to %1$'d of %2$'d connected peers",
tpeers ),
upeers, tpeers );
break;
case TR_STATUS_STOPPED:
top = g_strdup( _( "Stopped" ) );
break;
default:
top = g_strdup( "???" );
break;
}
return top;
}
void
tr_torrent_set_remove_flag( TrTorrent * gtor,
gboolean do_remove )
{
if( !isDisposed( gtor ) )
gtor->priv->do_remove = do_remove;
}
void
tr_torrent_delete_files( TrTorrent * gtor )
{
tr_torrentDeleteLocalData( tr_torrent_handle( gtor ), tr_file_trash_or_remove );
}
void
tr_torrent_open_folder( TrTorrent * gtor )
{
tr_torrent * tor = tr_torrent_handle( gtor );
const tr_info * info = tr_torrent_info( gtor );
char * path = info->fileCount == 1
? g_build_filename( tr_torrentGetDownloadDir(
tor ), NULL )
: g_build_filename( tr_torrentGetDownloadDir(
tor ), info->name, NULL );
gtr_open_file( path );
g_free( path );
}