source: branches/1.3x/gtk/tr-torrent.c @ 6489

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

(1.3x) lots of C correctness tweaks suggested by sparse/cgcc

  • Property svn:keywords set to Date Rev Author Id
File size: 8.8 KB
Line 
1/******************************************************************************
2 * $Id: tr-torrent.c 6489 2008-08-11 19:05:00Z 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_remove;
43};
44
45
46static void
47tr_torrent_init(GTypeInstance *instance, gpointer g_class UNUSED )
48{
49    TrTorrent * self = TR_TORRENT( instance );
50    struct TrTorrentPrivate * p;
51
52    p = self->priv = G_TYPE_INSTANCE_GET_PRIVATE( self,
53                                                  TR_TORRENT_TYPE,
54                                                  struct TrTorrentPrivate );
55    p->handle = NULL;
56
57#ifdef REFDBG
58    g_message( "torrent %p init", self );
59#endif
60}
61
62static int
63isDisposed( const TrTorrent * tor )
64{
65    return !tor || !TR_IS_TORRENT( tor ) || !tor->priv;
66}
67
68static void
69tr_torrent_dispose( GObject * o )
70{
71    GObjectClass * parent;
72    TrTorrent * self = TR_TORRENT( o );
73
74    if( !isDisposed( self ) )
75    {
76        if( self->priv->handle )
77        {
78            if( self->priv->do_remove )
79                tr_torrentRemove( self->priv->handle );
80            else
81                tr_torrentFree( self->priv->handle );
82        }
83
84        self->priv = NULL;
85    }
86
87    parent = g_type_class_peek(g_type_parent(TR_TORRENT_TYPE));
88    parent->dispose( o );
89}
90
91void
92tr_torrent_clear( TrTorrent * tor )
93{
94    g_return_if_fail( tor );
95    g_return_if_fail( tor->priv );
96
97    tor->priv->handle = NULL;
98}
99
100static void
101tr_torrent_class_init( gpointer g_class, gpointer g_class_data UNUSED )
102{
103    GObjectClass *gobject_class = G_OBJECT_CLASS(g_class);
104    gobject_class->dispose = tr_torrent_dispose;
105    g_type_class_add_private( g_class, sizeof( struct TrTorrentPrivate ) );
106}
107
108GType
109tr_torrent_get_type( void )
110{
111  static GType type = 0;
112
113  if( !type )
114  {
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 */
122      sizeof (TrTorrent),
123      0,      /* n_preallocs */
124      tr_torrent_init, /* instance_init */
125      NULL,
126    };
127    type = g_type_register_static(G_TYPE_OBJECT, "TrTorrent", &info, 0);
128  }
129  return type;
130}
131
132tr_torrent *
133tr_torrent_handle(TrTorrent *tor)
134{
135    return isDisposed( tor ) ? NULL : tor->priv->handle;
136}
137
138const tr_stat *
139tr_torrent_stat(TrTorrent *tor)
140{
141    tr_torrent * handle = tr_torrent_handle( tor );
142    return handle ? tr_torrentStatCached( handle ) : NULL;
143}
144
145const tr_info *
146tr_torrent_info( TrTorrent * tor )
147{
148    tr_torrent * handle = tr_torrent_handle( tor );
149    return handle ? tr_torrentInfo( handle ) : NULL;
150}
151
152static gboolean
153notifyInMainThread( gpointer user_data )
154{
155    tr_notify_send( TR_TORRENT( user_data ) );
156    return FALSE;
157}
158static void
159statusChangedCallback( tr_torrent   * tor UNUSED,
160                       cp_status_t    status,
161                       void         * user_data )
162{
163    if( status == TR_CP_COMPLETE )
164        g_idle_add( notifyInMainThread, user_data );
165}
166static TrTorrent *
167maketorrent( tr_torrent * handle )
168{
169    TrTorrent * tor = g_object_new( TR_TORRENT_TYPE, NULL );
170    tor->priv->handle = handle;
171    tr_torrentSetStatusCallback( handle, statusChangedCallback, tor );
172    return tor;
173}
174
175TrTorrent*
176tr_torrent_new_preexisting( tr_torrent * tor )
177{
178    return maketorrent( tor );
179}
180
181TrTorrent *
182tr_torrent_new_ctor( tr_handle  * handle,
183                     tr_ctor    * ctor,
184                     char      ** err )
185{
186    tr_torrent * tor;
187    int errcode;
188    uint8_t doTrash = FALSE;
189
190    errcode = -1;
191    *err = NULL;
192
193    /* let the gtk client handle the removal, since libT
194     * doesn't have any concept of the glib trash API */
195    tr_ctorGetDeleteSource( ctor, &doTrash );
196    tr_ctorSetDeleteSource( ctor, FALSE );
197    tor = tr_torrentNew( handle, ctor, &errcode );
198
199    if( tor && doTrash )
200        tr_file_trash_or_unlink( tr_ctorGetSourceFile( ctor ) );
201 
202    if( !tor )
203    {
204        const char * filename = tr_ctorGetSourceFile( ctor );
205        if( !filename )
206            filename = "(null)";
207
208        switch( errcode )
209        {
210            case TR_EINVALID:
211                *err = g_strdup_printf( _( "File \"%s\" isn't a valid torrent" ), filename );
212                 break;
213            case TR_EDUPLICATE:
214                *err = g_strdup_printf( _( "File \"%s\" is already open" ), filename );
215                break;
216            default:
217                *err = g_strdup( filename );
218                break;
219        }
220
221        return NULL;
222    }
223
224    return maketorrent( tor );
225}
226
227char *
228tr_torrent_status_str ( TrTorrent * gtor )
229{
230    char * top = NULL;
231
232    const tr_stat * st = tr_torrent_stat( gtor );
233
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] */
238
239    switch( st->status )
240    {
241        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 );
244            break;
245
246        case TR_STATUS_CHECK:
247            prog = st->recheckProgress * 100.0; /* [0...100] */
248            top = g_strdup_printf( _("Verifying local data (%.1f%% tested)"), prog );
249            break;
250
251        case TR_STATUS_DOWNLOAD:
252
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 );
257            else {
258                char timestr[128];
259                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 );
263            }
264            break;
265
266        case TR_STATUS_SEED:
267            top = g_strdup_printf(
268                ngettext( "Seeding to %1$'d of %2$'d connected peer",
269                          "Seeding to %1$'d of %2$'d connected peers", tpeers ),
270                          upeers, tpeers );
271            break;
272
273        case TR_STATUS_STOPPED:
274            top = g_strdup_printf( _("Stopped (%.1f%%)"), prog );
275            break;
276
277        default:
278            top = g_strdup_printf( "???" );
279            break;
280
281    }
282
283    return top;
284}
285
286void
287tr_torrent_set_remove_flag( TrTorrent * gtor, gboolean do_remove )
288{
289    if( !isDisposed( gtor ) )
290        gtor->priv->do_remove = do_remove;
291}
292
293void
294tr_torrent_delete_files( TrTorrent * gtor )
295{
296    tr_file_index_t i;
297    const tr_info * info = tr_torrent_info( gtor );
298    const char * stop = tr_torrentGetDownloadDir( tr_torrent_handle( gtor ) );
299
300    for( i=0; info && i<info->fileCount; ++i )
301    {
302        char * file = g_build_filename( stop, info->files[i].name, NULL );
303        while( strcmp( stop, file ) && strlen(stop) < strlen(file) )
304        {
305            char * swap = g_path_get_dirname( file );
306            tr_file_trash_or_unlink( file );
307            g_free( file );
308            file = swap;
309        }
310        g_free( file );
311    }
312}
313
314void
315tr_torrent_open_folder( TrTorrent * gtor )
316{
317    tr_torrent * tor = tr_torrent_handle( gtor );
318    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 );
322    gtr_open_file( path );
323    g_free( path );
324}
Note: See TracBrowser for help on using the repository browser.