1 | /****************************************************************************** |
---|
2 | * $Id: tr-torrent.c 5868 2008-05-20 23:58:59Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2006-2008 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #include <string.h> |
---|
26 | #include <unistd.h> |
---|
27 | |
---|
28 | #include <gtk/gtk.h> |
---|
29 | #include <glib/gi18n.h> |
---|
30 | |
---|
31 | #include <libtransmission/transmission.h> |
---|
32 | |
---|
33 | #include "tr-prefs.h" |
---|
34 | #include "tr-torrent.h" |
---|
35 | #include "conf.h" |
---|
36 | #include "notify.h" |
---|
37 | #include "util.h" |
---|
38 | |
---|
39 | struct TrTorrentPrivate |
---|
40 | { |
---|
41 | tr_torrent * handle; |
---|
42 | gboolean do_remove; |
---|
43 | gboolean seeding_cap_enabled; |
---|
44 | gdouble seeding_cap; /* ratio to stop seeding at */ |
---|
45 | }; |
---|
46 | |
---|
47 | |
---|
48 | static void |
---|
49 | tr_torrent_init(GTypeInstance *instance, gpointer g_class UNUSED ) |
---|
50 | { |
---|
51 | TrTorrent * self = TR_TORRENT( instance ); |
---|
52 | struct TrTorrentPrivate * p; |
---|
53 | |
---|
54 | p = self->priv = G_TYPE_INSTANCE_GET_PRIVATE( self, |
---|
55 | TR_TORRENT_TYPE, |
---|
56 | struct TrTorrentPrivate ); |
---|
57 | p->handle = NULL; |
---|
58 | p->seeding_cap = 2.0; |
---|
59 | |
---|
60 | #ifdef REFDBG |
---|
61 | g_message( "torrent %p init", self ); |
---|
62 | #endif |
---|
63 | } |
---|
64 | |
---|
65 | static int |
---|
66 | isDisposed( const TrTorrent * self ) |
---|
67 | { |
---|
68 | return !self || !self->priv; |
---|
69 | } |
---|
70 | |
---|
71 | static void |
---|
72 | tr_torrent_dispose( GObject * o ) |
---|
73 | { |
---|
74 | GObjectClass * parent; |
---|
75 | TrTorrent * self = TR_TORRENT( o ); |
---|
76 | |
---|
77 | if( !isDisposed( self ) ) |
---|
78 | { |
---|
79 | if( self->priv->handle ) |
---|
80 | { |
---|
81 | if( self->priv->do_remove ) |
---|
82 | tr_torrentRemove( self->priv->handle ); |
---|
83 | else |
---|
84 | tr_torrentClose( self->priv->handle ); |
---|
85 | } |
---|
86 | |
---|
87 | self->priv = NULL; |
---|
88 | } |
---|
89 | |
---|
90 | parent = g_type_class_peek(g_type_parent(TR_TORRENT_TYPE)); |
---|
91 | parent->dispose( o ); |
---|
92 | } |
---|
93 | |
---|
94 | static void |
---|
95 | tr_torrent_class_init( gpointer g_class, gpointer g_class_data UNUSED ) |
---|
96 | { |
---|
97 | GObjectClass *gobject_class = G_OBJECT_CLASS(g_class); |
---|
98 | gobject_class->dispose = tr_torrent_dispose; |
---|
99 | g_type_class_add_private( g_class, sizeof( struct TrTorrentPrivate ) ); |
---|
100 | } |
---|
101 | |
---|
102 | GType |
---|
103 | tr_torrent_get_type( void ) |
---|
104 | { |
---|
105 | static GType type = 0; |
---|
106 | |
---|
107 | if( !type ) |
---|
108 | { |
---|
109 | static const GTypeInfo info = { |
---|
110 | sizeof (TrTorrentClass), |
---|
111 | NULL, /* base_init */ |
---|
112 | NULL, /* base_finalize */ |
---|
113 | tr_torrent_class_init, /* class_init */ |
---|
114 | NULL, /* class_finalize */ |
---|
115 | NULL, /* class_data */ |
---|
116 | sizeof (TrTorrent), |
---|
117 | 0, /* n_preallocs */ |
---|
118 | tr_torrent_init, /* instance_init */ |
---|
119 | NULL, |
---|
120 | }; |
---|
121 | type = g_type_register_static(G_TYPE_OBJECT, "TrTorrent", &info, 0); |
---|
122 | } |
---|
123 | return type; |
---|
124 | } |
---|
125 | |
---|
126 | tr_torrent * |
---|
127 | tr_torrent_handle(TrTorrent *tor) |
---|
128 | { |
---|
129 | g_assert( TR_IS_TORRENT(tor) ); |
---|
130 | |
---|
131 | return isDisposed( tor ) ? NULL : tor->priv->handle; |
---|
132 | } |
---|
133 | |
---|
134 | const tr_stat * |
---|
135 | tr_torrent_stat(TrTorrent *tor) |
---|
136 | { |
---|
137 | tr_torrent * handle = tr_torrent_handle( tor ); |
---|
138 | return handle ? tr_torrentStatCached( handle ) : NULL; |
---|
139 | } |
---|
140 | |
---|
141 | const tr_info * |
---|
142 | tr_torrent_info( TrTorrent * tor ) |
---|
143 | { |
---|
144 | tr_torrent * handle = tr_torrent_handle( tor ); |
---|
145 | return handle ? tr_torrentInfo( handle ) : NULL; |
---|
146 | } |
---|
147 | |
---|
148 | void |
---|
149 | tr_torrent_start( TrTorrent * self ) |
---|
150 | { |
---|
151 | tr_torrent * handle = tr_torrent_handle( self ); |
---|
152 | if( handle ) |
---|
153 | tr_torrentStart( handle ); |
---|
154 | } |
---|
155 | |
---|
156 | void |
---|
157 | tr_torrent_stop( TrTorrent * self ) |
---|
158 | { |
---|
159 | tr_torrent * handle = tr_torrent_handle( self ); |
---|
160 | if( handle ) |
---|
161 | tr_torrentStop( handle ); |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | static gboolean |
---|
166 | notifyInMainThread( gpointer user_data ) |
---|
167 | { |
---|
168 | tr_notify_send( TR_TORRENT( user_data ) ); |
---|
169 | return FALSE; |
---|
170 | } |
---|
171 | static void |
---|
172 | statusChangedCallback( tr_torrent * tor UNUSED, |
---|
173 | cp_status_t status, |
---|
174 | void * user_data ) |
---|
175 | { |
---|
176 | if( status == TR_CP_COMPLETE ) |
---|
177 | g_idle_add( notifyInMainThread, user_data ); |
---|
178 | } |
---|
179 | static TrTorrent * |
---|
180 | maketorrent( tr_torrent * handle ) |
---|
181 | { |
---|
182 | TrTorrent * tor = g_object_new( TR_TORRENT_TYPE, NULL ); |
---|
183 | tor->priv->handle = handle; |
---|
184 | tr_torrentSetStatusCallback( handle, statusChangedCallback, tor ); |
---|
185 | return tor; |
---|
186 | } |
---|
187 | |
---|
188 | TrTorrent* |
---|
189 | tr_torrent_new_preexisting( tr_torrent * tor ) |
---|
190 | { |
---|
191 | return maketorrent( tor ); |
---|
192 | } |
---|
193 | |
---|
194 | TrTorrent * |
---|
195 | tr_torrent_new_ctor( tr_handle * handle, |
---|
196 | tr_ctor * ctor, |
---|
197 | char ** err ) |
---|
198 | { |
---|
199 | tr_torrent * tor; |
---|
200 | int errcode; |
---|
201 | |
---|
202 | errcode = -1; |
---|
203 | *err = NULL; |
---|
204 | |
---|
205 | tr_ctorSetDeleteSource( ctor, FALSE ); |
---|
206 | tor = tr_torrentNew( handle, ctor, &errcode ); |
---|
207 | |
---|
208 | if( tor ) |
---|
209 | { |
---|
210 | uint8_t doTrash = FALSE; |
---|
211 | tr_ctorGetDeleteSource( ctor, &doTrash ); |
---|
212 | if( doTrash ) |
---|
213 | tr_file_trash_or_unlink( tr_ctorGetSourceFile( ctor ) ); |
---|
214 | } |
---|
215 | |
---|
216 | if( !tor ) |
---|
217 | { |
---|
218 | const char * filename = tr_ctorGetSourceFile( ctor ); |
---|
219 | if( !filename ) |
---|
220 | filename = "(null)"; |
---|
221 | |
---|
222 | switch( errcode ) |
---|
223 | { |
---|
224 | case TR_EINVALID: |
---|
225 | *err = g_strdup_printf( _( "File \"%s\" isn't a valid torrent" ), filename ); |
---|
226 | break; |
---|
227 | case TR_EDUPLICATE: |
---|
228 | *err = g_strdup_printf( _( "File \"%s\" is already open" ), filename ); |
---|
229 | break; |
---|
230 | default: |
---|
231 | *err = g_strdup( filename ); |
---|
232 | break; |
---|
233 | } |
---|
234 | |
---|
235 | return NULL; |
---|
236 | } |
---|
237 | |
---|
238 | return maketorrent( tor ); |
---|
239 | } |
---|
240 | |
---|
241 | void |
---|
242 | tr_torrent_check_seeding_cap ( TrTorrent *gtor) |
---|
243 | { |
---|
244 | const tr_stat * st = tr_torrent_stat( gtor ); |
---|
245 | if ((gtor->priv->seeding_cap_enabled) && (st->ratio >= gtor->priv->seeding_cap)) |
---|
246 | tr_torrent_stop (gtor); |
---|
247 | } |
---|
248 | void |
---|
249 | tr_torrent_set_seeding_cap_ratio ( TrTorrent *gtor, gdouble ratio ) |
---|
250 | { |
---|
251 | gtor->priv->seeding_cap = ratio; |
---|
252 | tr_torrent_check_seeding_cap (gtor); |
---|
253 | } |
---|
254 | void |
---|
255 | tr_torrent_set_seeding_cap_enabled ( TrTorrent *gtor, gboolean b ) |
---|
256 | { |
---|
257 | if ((gtor->priv->seeding_cap_enabled = b)) |
---|
258 | tr_torrent_check_seeding_cap (gtor); |
---|
259 | } |
---|
260 | |
---|
261 | char * |
---|
262 | tr_torrent_status_str ( TrTorrent * gtor ) |
---|
263 | { |
---|
264 | char * top = 0; |
---|
265 | |
---|
266 | const tr_stat * st = tr_torrent_stat( gtor ); |
---|
267 | |
---|
268 | const int tpeers = MAX (st->peersConnected, 0); |
---|
269 | const int upeers = MAX (st->peersGettingFromUs, 0); |
---|
270 | const int eta = st->eta; |
---|
271 | double prog = st->percentDone * 100.0; /* [0...100] */ |
---|
272 | |
---|
273 | switch( st->status ) |
---|
274 | { |
---|
275 | case TR_STATUS_CHECK_WAIT: |
---|
276 | prog = st->recheckProgress * 100.0; /* [0...100] */ |
---|
277 | top = g_strdup_printf( _("Waiting to verify local data (%.1f%% tested)"), prog ); |
---|
278 | break; |
---|
279 | |
---|
280 | case TR_STATUS_CHECK: |
---|
281 | prog = st->recheckProgress * 100.0; /* [0...100] */ |
---|
282 | top = g_strdup_printf( _("Verifying local data (%.1f%% tested)"), prog ); |
---|
283 | break; |
---|
284 | |
---|
285 | case TR_STATUS_DOWNLOAD: |
---|
286 | |
---|
287 | if( eta == TR_ETA_NOT_AVAIL ) |
---|
288 | top = g_strdup_printf( _("Not available (%.1f%%)" ), prog ); |
---|
289 | else if( eta == TR_ETA_UNKNOWN ) |
---|
290 | top = g_strdup_printf( _( "Stalled (%.1f%%)" ), prog ); |
---|
291 | else { |
---|
292 | char timestr[128]; |
---|
293 | tr_strltime( timestr, eta, sizeof( timestr ) ); |
---|
294 | /* %1$s is # of minutes |
---|
295 | %2$.1f is a percentage of how much of the torrent is done */ |
---|
296 | top = g_strdup_printf( _("%1$s remaining (%2$.1f%%)"), timestr, prog ); |
---|
297 | } |
---|
298 | break; |
---|
299 | |
---|
300 | case TR_STATUS_SEED: |
---|
301 | top = g_strdup_printf( |
---|
302 | ngettext( "Seeding to %1$'d of %2$'d connected peer", |
---|
303 | "Seeding to %1$'d of %2$'d connected peers", tpeers ), |
---|
304 | upeers, tpeers ); |
---|
305 | break; |
---|
306 | |
---|
307 | case TR_STATUS_STOPPED: |
---|
308 | top = g_strdup_printf( _("Stopped (%.1f%%)"), prog ); |
---|
309 | break; |
---|
310 | |
---|
311 | default: |
---|
312 | top = g_strdup_printf( "???" ); |
---|
313 | break; |
---|
314 | |
---|
315 | } |
---|
316 | |
---|
317 | return top; |
---|
318 | } |
---|
319 | |
---|
320 | void |
---|
321 | tr_torrent_set_remove_flag( TrTorrent * gtor, gboolean do_remove ) |
---|
322 | { |
---|
323 | if( !isDisposed( gtor ) ) |
---|
324 | gtor->priv->do_remove = do_remove; |
---|
325 | } |
---|
326 | |
---|
327 | void |
---|
328 | tr_torrent_delete_files( TrTorrent * gtor ) |
---|
329 | { |
---|
330 | tr_file_index_t i; |
---|
331 | const tr_info * info = tr_torrent_info( gtor ); |
---|
332 | const char * stop = tr_torrentGetDownloadDir( tr_torrent_handle( gtor ) ); |
---|
333 | |
---|
334 | for( i=0; info && i<info->fileCount; ++i ) |
---|
335 | { |
---|
336 | char * file = g_build_filename( stop, info->files[i].name, NULL ); |
---|
337 | while( strcmp( stop, file ) && strlen(stop) < strlen(file) ) |
---|
338 | { |
---|
339 | char * swap = g_path_get_dirname( file ); |
---|
340 | tr_file_trash_or_unlink( file ); |
---|
341 | g_free( file ); |
---|
342 | file = swap; |
---|
343 | } |
---|
344 | g_free( file ); |
---|
345 | } |
---|
346 | } |
---|
347 | |
---|
348 | void |
---|
349 | tr_torrent_open_folder( TrTorrent * gtor ) |
---|
350 | { |
---|
351 | tr_torrent * tor = tr_torrent_handle( gtor ); |
---|
352 | const tr_info * info = tr_torrent_info( gtor ); |
---|
353 | char * path = info->fileCount == 1 |
---|
354 | ? g_build_filename( tr_torrentGetDownloadDir(tor), NULL ) |
---|
355 | : g_build_filename( tr_torrentGetDownloadDir(tor), info->name, NULL ); |
---|
356 | gtr_open_file( path ); |
---|
357 | g_free( path ); |
---|
358 | } |
---|