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

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

remove dead code

  • Property svn:keywords set to Date Rev Author Id
File size: 8.6 KB
Line 
1/******************************************************************************
2 * $Id: tr-torrent.c 5947 2008-05-27 17:47:39Z 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 * self )
64{
65    return !self || !self->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
91static void
92tr_torrent_class_init( gpointer g_class, gpointer g_class_data UNUSED )
93{
94    GObjectClass *gobject_class = G_OBJECT_CLASS(g_class);
95    gobject_class->dispose = tr_torrent_dispose;
96    g_type_class_add_private( g_class, sizeof( struct TrTorrentPrivate ) );
97}
98
99GType
100tr_torrent_get_type( void )
101{
102  static GType type = 0;
103
104  if( !type )
105  {
106    static const GTypeInfo info = {
107      sizeof (TrTorrentClass),
108      NULL,   /* base_init */
109      NULL,   /* base_finalize */
110      tr_torrent_class_init,   /* class_init */
111      NULL,   /* class_finalize */
112      NULL,   /* class_data */
113      sizeof (TrTorrent),
114      0,      /* n_preallocs */
115      tr_torrent_init, /* instance_init */
116      NULL,
117    };
118    type = g_type_register_static(G_TYPE_OBJECT, "TrTorrent", &info, 0);
119  }
120  return type;
121}
122
123tr_torrent *
124tr_torrent_handle(TrTorrent *tor)
125{
126    g_assert( TR_IS_TORRENT(tor) );
127
128    return isDisposed( tor ) ? NULL : tor->priv->handle;
129}
130
131const tr_stat *
132tr_torrent_stat(TrTorrent *tor)
133{
134    tr_torrent * handle = tr_torrent_handle( tor );
135    return handle ? tr_torrentStatCached( handle ) : NULL;
136}
137
138const tr_info *
139tr_torrent_info( TrTorrent * tor )
140{
141    tr_torrent * handle = tr_torrent_handle( tor );
142    return handle ? tr_torrentInfo( handle ) : NULL;
143}
144
145static gboolean
146notifyInMainThread( gpointer user_data )
147{
148    tr_notify_send( TR_TORRENT( user_data ) );
149    return FALSE;
150}
151static void
152statusChangedCallback( tr_torrent   * tor UNUSED,
153                       cp_status_t    status,
154                       void         * user_data )
155{
156    if( status == TR_CP_COMPLETE )
157        g_idle_add( notifyInMainThread, user_data );
158}
159static TrTorrent *
160maketorrent( tr_torrent * handle )
161{
162    TrTorrent * tor = g_object_new( TR_TORRENT_TYPE, NULL );
163    tor->priv->handle = handle;
164    tr_torrentSetStatusCallback( handle, statusChangedCallback, tor );
165    return tor;
166}
167
168TrTorrent*
169tr_torrent_new_preexisting( tr_torrent * tor )
170{
171    return maketorrent( tor );
172}
173
174TrTorrent *
175tr_torrent_new_ctor( tr_handle  * handle,
176                     tr_ctor    * ctor,
177                     char      ** err )
178{
179    tr_torrent * tor;
180    int errcode;
181
182    errcode = -1;
183    *err = NULL;
184
185    tr_ctorSetDeleteSource( ctor, FALSE );
186    tor = tr_torrentNew( handle, ctor, &errcode );
187
188    if( tor )
189    {
190        uint8_t doTrash = FALSE;
191        tr_ctorGetDeleteSource( ctor, &doTrash );
192        if( doTrash )
193            tr_file_trash_or_unlink( tr_ctorGetSourceFile( ctor ) );
194    }
195 
196    if( !tor )
197    {
198        const char * filename = tr_ctorGetSourceFile( ctor );
199        if( !filename )
200            filename = "(null)";
201
202        switch( errcode )
203        {
204            case TR_EINVALID:
205                *err = g_strdup_printf( _( "File \"%s\" isn't a valid torrent" ), filename );
206                 break;
207            case TR_EDUPLICATE:
208                *err = g_strdup_printf( _( "File \"%s\" is already open" ), filename );
209                break;
210            default:
211                *err = g_strdup( filename );
212                break;
213        }
214
215        return NULL;
216    }
217
218    return maketorrent( tor );
219}
220
221char *
222tr_torrent_status_str ( TrTorrent * gtor )
223{
224    char * top = 0;
225
226    const tr_stat * st = tr_torrent_stat( gtor );
227
228    const int tpeers = MAX (st->peersConnected, 0);
229    const int upeers = MAX (st->peersGettingFromUs, 0);
230    const int eta = st->eta;
231    double prog = st->percentDone * 100.0; /* [0...100] */
232
233    switch( st->status )
234    {
235        case TR_STATUS_CHECK_WAIT:
236            prog = st->recheckProgress * 100.0; /* [0...100] */
237            top = g_strdup_printf( _("Waiting to verify local data (%.1f%% tested)"), prog );
238            break;
239
240        case TR_STATUS_CHECK:
241            prog = st->recheckProgress * 100.0; /* [0...100] */
242            top = g_strdup_printf( _("Verifying local data (%.1f%% tested)"), prog );
243            break;
244
245        case TR_STATUS_DOWNLOAD:
246
247            if( eta == TR_ETA_NOT_AVAIL )
248                top = g_strdup_printf( _("Not available (%.1f%%)" ), prog );
249            else if( eta == TR_ETA_UNKNOWN )
250                top = g_strdup_printf( _( "Stalled (%.1f%%)" ), prog );
251            else {
252                char timestr[128];
253                tr_strltime( timestr, eta, sizeof( timestr ) );
254                /* %1$s is # of minutes
255                   %2$.1f is a percentage of how much of the torrent is done */
256                top = g_strdup_printf( _("%1$s remaining (%2$.1f%%)"), timestr, prog );
257            }
258            break;
259
260        case TR_STATUS_SEED:
261            top = g_strdup_printf(
262                ngettext( "Seeding to %1$'d of %2$'d connected peer",
263                          "Seeding to %1$'d of %2$'d connected peers", tpeers ),
264                          upeers, tpeers );
265            break;
266
267        case TR_STATUS_STOPPED:
268            top = g_strdup_printf( _("Stopped (%.1f%%)"), prog );
269            break;
270
271        default:
272            top = g_strdup_printf( "???" );
273            break;
274
275    }
276
277    return top;
278}
279
280void
281tr_torrent_set_remove_flag( TrTorrent * gtor, gboolean do_remove )
282{
283    if( !isDisposed( gtor ) )
284        gtor->priv->do_remove = do_remove;
285}
286
287void
288tr_torrent_delete_files( TrTorrent * gtor )
289{
290    tr_file_index_t i;
291    const tr_info * info = tr_torrent_info( gtor );
292    const char * stop = tr_torrentGetDownloadDir( tr_torrent_handle( gtor ) );
293
294    for( i=0; info && i<info->fileCount; ++i )
295    {
296        char * file = g_build_filename( stop, info->files[i].name, NULL );
297        while( strcmp( stop, file ) && strlen(stop) < strlen(file) )
298        {
299            char * swap = g_path_get_dirname( file );
300            tr_file_trash_or_unlink( file );
301            g_free( file );
302            file = swap;
303        }
304        g_free( file );
305    }
306}
307
308void
309tr_torrent_open_folder( TrTorrent * gtor )
310{
311    tr_torrent * tor = tr_torrent_handle( gtor );
312    const tr_info * info = tr_torrent_info( gtor );
313    char * path = info->fileCount == 1
314        ? g_build_filename( tr_torrentGetDownloadDir(tor), NULL )
315        : g_build_filename( tr_torrentGetDownloadDir(tor), info->name, NULL );
316    gtr_open_file( path );
317    g_free( path );
318}
Note: See TracBrowser for help on using the repository browser.