39
39
struct TrTorrentPrivate
47
tr_torrent_init(GTypeInstance *instance, gpointer g_class UNUSED )
47
tr_torrent_init( GTypeInstance * instance,
48
gpointer g_class UNUSED )
49
TrTorrent * self = TR_TORRENT( instance );
50
TrTorrent * self = TR_TORRENT( instance );
50
51
struct TrTorrentPrivate * p;
52
53
p = self->priv = G_TYPE_INSTANCE_GET_PRIVATE( self,
101
tr_torrent_class_init( gpointer g_class, gpointer g_class_data UNUSED )
102
tr_torrent_class_init( gpointer g_class,
103
gpointer g_class_data UNUSED )
103
GObjectClass *gobject_class = G_OBJECT_CLASS(g_class);
105
GObjectClass *gobject_class = G_OBJECT_CLASS( g_class );
104
107
gobject_class->dispose = tr_torrent_dispose;
105
108
g_type_class_add_private( g_class, sizeof( struct TrTorrentPrivate ) );
109
112
tr_torrent_get_type( void )
111
static GType type = 0;
114
static GType type = 0;
115
static const GTypeInfo info = {
116
sizeof (TrTorrentClass),
117
NULL, /* base_init */
118
NULL, /* base_finalize */
119
tr_torrent_class_init, /* class_init */
120
NULL, /* class_finalize */
121
NULL, /* class_data */
124
tr_torrent_init, /* instance_init */
127
type = g_type_register_static(G_TYPE_OBJECT, "TrTorrent", &info, 0);
118
static const GTypeInfo info = {
119
sizeof ( TrTorrentClass ),
120
NULL, /* base_init */
121
NULL, /* base_finalize */
122
tr_torrent_class_init, /* class_init */
123
NULL, /* class_finalize */
124
NULL, /* class_data */
125
sizeof ( TrTorrent ),
127
tr_torrent_init, /* instance_init */
130
type = g_type_register_static( G_TYPE_OBJECT, "TrTorrent", &info, 0 );
133
tr_torrent_handle(TrTorrent *tor)
136
tr_torrent_handle( TrTorrent *tor )
135
138
return isDisposed( tor ) ? NULL : tor->priv->handle;
139
tr_torrent_stat(TrTorrent *tor)
142
tr_torrent_stat( TrTorrent *tor )
141
144
tr_torrent * handle = tr_torrent_handle( tor );
142
146
return handle ? tr_torrentStatCached( handle ) : NULL;
155
160
tr_notify_send( TR_TORRENT( user_data ) );
159
statusChangedCallback( tr_torrent * tor UNUSED,
165
completenessChangedCallback( tr_torrent * tor UNUSED,
166
tr_completeness completeness,
163
if( status == TR_CP_COMPLETE )
169
if( completeness == TR_CP_COMPLETE )
164
170
g_idle_add( notifyInMainThread, user_data );
166
173
static TrTorrent *
167
maketorrent( tr_torrent * handle )
174
maketorrent( tr_torrent * tor )
169
TrTorrent * tor = g_object_new( TR_TORRENT_TYPE, NULL );
170
tor->priv->handle = handle;
171
tr_torrentSetStatusCallback( handle, statusChangedCallback, tor );
176
TrTorrent * gtor = g_object_new( TR_TORRENT_TYPE, NULL );
178
gtor->priv->handle = tor;
179
tr_torrentSetCompletenessCallback( tor, completenessChangedCallback, gtor );
194
202
* doesn't have any concept of the glib trash API */
195
203
tr_ctorGetDeleteSource( ctor, &doTrash );
196
204
tr_ctorSetDeleteSource( ctor, FALSE );
197
tor = tr_torrentNew( handle, ctor, &errcode );
205
tor = tr_torrentNew( session, ctor, &errcode );
199
207
if( tor && doTrash )
200
tr_file_trash_or_unlink( tr_ctorGetSourceFile( ctor ) );
209
const char * config = tr_sessionGetConfigDir( session );
210
const char * source = tr_ctorGetSourceFile( ctor );
211
const int is_internal = source && ( strstr( source, config ) == source );
213
/* #1294: don't delete the source .torrent file if it's our internal copy */
215
tr_file_trash_or_unlink( source );
204
220
const char * filename = tr_ctorGetSourceFile( ctor );
208
224
switch( errcode )
210
226
case TR_EINVALID:
211
*err = g_strdup_printf( _( "File \"%s\" isn't a valid torrent" ), filename );
229
"File \"%s\" isn't a valid torrent" ),
213
233
case TR_EDUPLICATE:
214
*err = g_strdup_printf( _( "File \"%s\" is already open" ), filename );
234
*err = g_strdup_printf( _(
235
"File \"%s\" is already open" ),
217
240
*err = g_strdup( filename );
228
tr_torrent_status_str ( TrTorrent * gtor )
251
tr_torrent_status_str( TrTorrent * gtor )
232
255
const tr_stat * st = tr_torrent_stat( gtor );
234
const int tpeers = MAX (st->peersConnected, 0);
235
const int upeers = MAX (st->peersGettingFromUs, 0);
236
const int eta = st->eta;
237
double prog = st->percentDone * 100.0; /* [0...100] */
257
const int tpeers = MAX ( st->peersConnected, 0 );
258
const int upeers = MAX ( st->peersGettingFromUs, 0 );
259
const int eta = st->eta;
261
switch( st->activity )
241
263
case TR_STATUS_CHECK_WAIT:
242
prog = st->recheckProgress * 100.0; /* [0...100] */
243
top = g_strdup_printf( _("Waiting to verify local data (%.1f%% tested)"), prog );
266
"Waiting to verify local data (%.1f%% tested)" ),
267
st->recheckProgress * 100.0 );
246
270
case TR_STATUS_CHECK:
247
prog = st->recheckProgress * 100.0; /* [0...100] */
248
top = g_strdup_printf( _("Verifying local data (%.1f%% tested)"), prog );
273
"Verifying local data (%.1f%% tested)" ),
274
st->recheckProgress * 100.0 );
251
277
case TR_STATUS_DOWNLOAD:
253
if( eta == TR_ETA_NOT_AVAIL )
254
top = g_strdup_printf( _("Data not fully available (%.1f%%)" ), prog );
255
else if( eta == TR_ETA_UNKNOWN )
256
top = g_strdup_printf( _( "Stalled (%.1f%%)" ), prog );
280
top = g_strdup_printf( _( "Remaining time unknown" ) );
258
283
char timestr[128];
259
284
tr_strltime( timestr, eta, sizeof( timestr ) );
260
/* %1$s is # of minutes
261
%2$.1f is a percentage of how much of the torrent is done */
262
top = g_strdup_printf( _("%1$s remaining (%2$.1f%%)"), timestr, prog );
285
/* %s is # of minutes */
286
top = g_strdup_printf( _( "%1$s remaining" ), timestr );
266
290
case TR_STATUS_SEED:
267
291
top = g_strdup_printf(
268
292
ngettext( "Seeding to %1$'d of %2$'d connected peer",
269
"Seeding to %1$'d of %2$'d connected peers", tpeers ),
293
"Seeding to %1$'d of %2$'d connected peers",
273
298
case TR_STATUS_STOPPED:
274
top = g_strdup_printf( _("Stopped (%.1f%%)"), prog );
299
top = g_strdup( _( "Stopped" ) );
278
top = g_strdup_printf( "???" );
303
top = g_strdup( "???" );
287
tr_torrent_set_remove_flag( TrTorrent * gtor, gboolean do_remove )
311
tr_torrent_set_remove_flag( TrTorrent * gtor,
289
314
if( !isDisposed( gtor ) )
290
315
gtor->priv->do_remove = do_remove;
296
321
tr_file_index_t i;
297
322
const tr_info * info = tr_torrent_info( gtor );
298
const char * stop = tr_torrentGetDownloadDir( tr_torrent_handle( gtor ) );
324
tr_torrentGetDownloadDir( tr_torrent_handle( gtor ) );
300
for( i=0; info && i<info->fileCount; ++i )
326
for( i = 0; info && i < info->fileCount; ++i )
302
328
char * file = g_build_filename( stop, info->files[i].name, NULL );
303
while( strcmp( stop, file ) && strlen(stop) < strlen(file) )
329
while( strcmp( stop, file ) && strlen( stop ) < strlen( file ) )
305
331
char * swap = g_path_get_dirname( file );
306
332
tr_file_trash_or_unlink( file );
315
342
tr_torrent_open_folder( TrTorrent * gtor )
317
tr_torrent * tor = tr_torrent_handle( gtor );
344
tr_torrent * tor = tr_torrent_handle( gtor );
318
345
const tr_info * info = tr_torrent_info( gtor );
319
char * path = info->fileCount == 1
320
? g_build_filename( tr_torrentGetDownloadDir(tor), NULL )
321
: g_build_filename( tr_torrentGetDownloadDir(tor), info->name, NULL );
346
char * path = info->fileCount == 1
347
? g_build_filename( tr_torrentGetDownloadDir(
349
: g_build_filename( tr_torrentGetDownloadDir(
350
tor ), info->name, NULL );
322
352
gtr_open_file( path );