source: trunk/gtk/tr-torrent.c @ 5313

Last change on this file since 5313 was 5313, checked in by charles, 14 years ago

more string work before the freeze. (1) use %'d to get thousands' grouping in end-user strings (2) follow HiG capitalization, quotation guidelines (3) fold a few more strings

  • Property svn:keywords set to Date Rev Author Id
File size: 9.3 KB
Line 
1/******************************************************************************
2 * $Id: tr-torrent.c 5313 2008-03-19 20:07:27Z 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
39struct TrTorrentPrivate
40{
41   tr_torrent * handle;
42   gboolean do_delete;
43   gboolean seeding_cap_enabled;
44   gdouble seeding_cap; /* ratio to stop seeding at */
45};
46
47
48static void
49tr_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
65static int
66isDisposed( const TrTorrent * self )
67{
68    return !self || !self->priv;
69}
70
71static void
72tr_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_delete )
82                tr_torrentDelete( 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
94static void
95tr_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
102GType
103tr_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
126tr_torrent *
127tr_torrent_handle(TrTorrent *tor)
128{
129    g_assert( TR_IS_TORRENT(tor) );
130
131    return isDisposed( tor ) ? NULL : tor->priv->handle;
132}
133
134const tr_stat *
135tr_torrent_stat(TrTorrent *tor)
136{
137    tr_torrent * handle = tr_torrent_handle( tor );
138    return handle ? tr_torrentStatCached( handle ) : NULL;
139}
140
141const tr_info *
142tr_torrent_info( TrTorrent * tor )
143{
144    tr_torrent * handle = tr_torrent_handle( tor );
145    return handle ? tr_torrentInfo( handle ) : NULL;
146}
147
148void
149tr_torrent_start( TrTorrent * self )
150{
151    tr_torrent * handle = tr_torrent_handle( self );
152    if( handle )
153        tr_torrentStart( handle );
154}
155
156void
157tr_torrent_stop( TrTorrent * self )
158{
159    tr_torrent * handle = tr_torrent_handle( self );
160    if( handle )
161        tr_torrentStop( handle );
162}
163
164
165static gboolean
166notifyInMainThread( gpointer user_data )
167{
168    if( pref_flag_get( PREF_KEY_NOTIFY ) )
169        tr_notify_send( TR_TORRENT( user_data ) );
170    return FALSE;
171}
172static void
173statusChangedCallback( tr_torrent   * tor UNUSED,
174                       cp_status_t    status,
175                       void         * user_data )
176{
177    if( status == TR_CP_COMPLETE )
178        g_idle_add( notifyInMainThread, user_data );
179}
180static TrTorrent *
181maketorrent( tr_torrent * handle )
182{
183    TrTorrent * tor = g_object_new( TR_TORRENT_TYPE, NULL );
184    tor->priv->handle = handle;
185    tr_torrentSetStatusCallback( handle, statusChangedCallback, tor );
186    return tor;
187}
188
189TrTorrent*
190tr_torrent_new_preexisting( tr_torrent * tor )
191{
192    return maketorrent( tor );
193}
194
195TrTorrent *
196tr_torrent_new_ctor( tr_handle  * handle,
197                     tr_ctor    * ctor,
198                     char      ** err )
199{
200    tr_torrent * tor;
201    int errcode;
202
203    errcode = -1;
204    *err = NULL;
205
206    tr_ctorSetDeleteSource( ctor, FALSE );
207    tor = tr_torrentNew( handle, ctor, &errcode );
208
209    if( tor )
210    {
211        uint8_t doTrash = FALSE;
212        tr_ctorGetDeleteSource( ctor, &doTrash );
213        if( doTrash )
214            tr_file_trash_or_unlink( tr_ctorGetSourceFile( ctor ) );
215    }
216 
217    if( !tor )
218    {
219        const char * filename = tr_ctorGetSourceFile( ctor );
220        if( !filename )
221            filename = "(null)";
222
223        switch( errcode )
224        {
225            case TR_EINVALID:
226                *err = g_strdup_printf( _( "File \"%s\" isn't a valid torrent" ), filename );
227                 break;
228            case TR_EDUPLICATE:
229                *err = g_strdup_printf( _( "File \"%s\" is already open" ), filename );
230                break;
231            default:
232                *err = g_strdup( filename );
233                break;
234        }
235
236        return NULL;
237    }
238
239    return maketorrent( tor );
240}
241
242void
243tr_torrent_check_seeding_cap ( TrTorrent *gtor)
244{
245  const tr_stat * st = tr_torrent_stat( gtor );
246  if ((gtor->priv->seeding_cap_enabled) && (st->ratio >= gtor->priv->seeding_cap))
247    tr_torrent_stop (gtor);
248}
249void
250tr_torrent_set_seeding_cap_ratio ( TrTorrent *gtor, gdouble ratio )
251{
252  gtor->priv->seeding_cap = ratio;
253  tr_torrent_check_seeding_cap (gtor);
254}
255void
256tr_torrent_set_seeding_cap_enabled ( TrTorrent *gtor, gboolean b )
257{
258  if ((gtor->priv->seeding_cap_enabled = b))
259    tr_torrent_check_seeding_cap (gtor);
260}
261
262char *
263tr_torrent_status_str ( TrTorrent * gtor )
264{
265    char * top = 0;
266
267    const tr_stat * st = tr_torrent_stat( gtor );
268
269    const int tpeers = MAX (st->peersConnected, 0);
270    const int upeers = MAX (st->peersGettingFromUs, 0);
271    const int eta = st->eta;
272    double prog = st->percentDone * 100.0; /* [0...100] */
273
274    switch( st->status )
275    {
276        case TR_STATUS_CHECK_WAIT:
277            prog = st->recheckProgress * 100.0; /* [0...100] */
278            top = g_strdup_printf( _("Waiting to verify local data (%.1f%% tested)"), prog );
279            break;
280
281        case TR_STATUS_CHECK:
282            prog = st->recheckProgress * 100.0; /* [0...100] */
283            top = g_strdup_printf( _("Verifying local data (%.1f%% tested)"), prog );
284            break;
285
286        case TR_STATUS_DOWNLOAD:
287            if( eta < 0 )
288                top = g_strdup_printf( _("Stalled (%.1f%%)"), prog );
289            else {
290                char timestr[128];
291                tr_strltime( timestr, eta, sizeof( timestr ) );
292                /* %1$s is # of minutes
293                   %2$.1f is a percentage of how much of the torrent is done */
294                top = g_strdup_printf( _("%1$s remaining (%2$.1f%%)"), timestr, prog );
295            }
296            break;
297
298        case TR_STATUS_DONE:
299            top = g_strdup_printf(
300                ngettext( "Uploading to %1$'d of %2$'d connected peer",
301                          "Uploading to %1$'d of %2$'d connected peers", tpeers ),
302                          upeers, tpeers );
303            break;
304
305        case TR_STATUS_SEED:
306            top = g_strdup_printf(
307                ngettext( "Seeding to %1$'d of %2$'d connected peer",
308                          "Seeding to %1$'d of %2$'d connected peers", tpeers ),
309                          upeers, tpeers );
310            break;
311
312        case TR_STATUS_STOPPED:
313            top = g_strdup_printf( _("Stopped (%.1f%%)"), prog );
314            break;
315
316        default:
317            top = g_strdup_printf( "???" );
318            break;
319
320    }
321
322    return top;
323}
324
325void
326tr_torrent_set_delete_flag( TrTorrent * gtor, gboolean do_delete )
327{
328    if( !isDisposed( gtor ) )
329        gtor->priv->do_delete = do_delete;
330}
331
332void
333tr_torrent_delete_files( TrTorrent * gtor )
334{
335    int i;
336    const tr_info * info = tr_torrent_info( gtor );
337    const char * stop = tr_torrentGetFolder( tr_torrent_handle( gtor ) );
338
339    for( i=0; info && i<info->fileCount; ++i )
340    {
341        char * file = g_build_filename( stop, info->files[i].name, NULL );
342        while( strcmp( stop, file ) && strlen(stop) < strlen(file) )
343        {
344            char * swap = g_path_get_dirname( file );
345            tr_file_trash_or_unlink( file );
346            g_free( file );
347            file = swap;
348        }
349        g_free( file );
350    }
351}
Note: See TracBrowser for help on using the repository browser.