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

Last change on this file since 9503 was 9503, checked in by charles, 12 years ago

(trunk gtk) possible fix for "open folder" in single-file torrents

  • Property svn:keywords set to Date Rev Author Id
File size: 8.4 KB
Line 
1/******************************************************************************
2 * $Id: tr-torrent.c 9503 2009-11-09 06:58:18Z 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#include <libtransmission/utils.h> /* tr_truncd() */
33
34#include "tr-prefs.h"
35#include "tr-torrent.h"
36#include "conf.h"
37#include "notify.h"
38#include "util.h"
39
40struct TrTorrentPrivate
41{
42    tr_torrent *  handle;
43    gboolean      do_remove;
44};
45
46
47static void
48tr_torrent_init( GTypeInstance *  instance,
49                 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
59#ifdef REFDBG
60    g_message( "torrent %p init", self );
61#endif
62}
63
64static int
65isDisposed( const TrTorrent * tor )
66{
67    return !tor || !TR_IS_TORRENT( tor ) || !tor->priv;
68}
69
70static void
71tr_torrent_dispose( GObject * o )
72{
73    GObjectClass * parent;
74    TrTorrent *    self = TR_TORRENT( o );
75
76    if( !isDisposed( self ) )
77    {
78        if( self->priv->handle )
79        {
80            if( self->priv->do_remove )
81                tr_torrentRemove( self->priv->handle );
82            else
83                tr_torrentFree( self->priv->handle );
84        }
85
86        self->priv = NULL;
87    }
88
89    parent = g_type_class_peek( g_type_parent( TR_TORRENT_TYPE ) );
90    parent->dispose( o );
91}
92
93void
94tr_torrent_clear( TrTorrent * tor )
95{
96    g_return_if_fail( tor );
97    g_return_if_fail( tor->priv );
98
99    tor->priv->handle = NULL;
100}
101
102static void
103tr_torrent_class_init( gpointer              g_class,
104                       gpointer g_class_data UNUSED )
105{
106    GObjectClass *gobject_class = G_OBJECT_CLASS( g_class );
107
108    gobject_class->dispose = tr_torrent_dispose;
109    g_type_class_add_private( g_class, sizeof( struct TrTorrentPrivate ) );
110}
111
112GType
113tr_torrent_get_type( void )
114{
115    static GType type = 0;
116
117    if( !type )
118    {
119        static const GTypeInfo info = {
120            sizeof ( TrTorrentClass ),
121            NULL,                     /* base_init */
122            NULL,                     /* base_finalize */
123            tr_torrent_class_init,    /* class_init */
124            NULL,                     /* class_finalize */
125            NULL,                     /* class_data */
126            sizeof ( TrTorrent ),
127            0,                        /* n_preallocs */
128            tr_torrent_init,          /* instance_init */
129            NULL,
130        };
131        type = g_type_register_static( G_TYPE_OBJECT, "TrTorrent", &info, 0 );
132    }
133    return type;
134}
135
136tr_torrent *
137tr_torrent_handle( TrTorrent *tor )
138{
139    return isDisposed( tor ) ? NULL : tor->priv->handle;
140}
141
142const tr_stat *
143tr_torrent_stat( TrTorrent *tor )
144{
145    tr_torrent * handle = tr_torrent_handle( tor );
146
147    return handle ? tr_torrentStatCached( handle ) : NULL;
148}
149
150const tr_info *
151tr_torrent_info( TrTorrent * tor )
152{
153    tr_torrent * handle = tr_torrent_handle( tor );
154
155    return handle ? tr_torrentInfo( handle ) : NULL;
156}
157
158static gboolean
159notifyInMainThread( gpointer user_data )
160{
161    tr_notify_send( TR_TORRENT( user_data ) );
162    return FALSE;
163}
164
165static void
166completenessChangedCallback( tr_torrent       * tor,
167                             tr_completeness    completeness,
168                             void *             user_data )
169{
170    if( ( completeness != TR_LEECH ) && ( tr_torrentStat( tor )->sizeWhenDone != 0 ) )
171        gtr_idle_add( notifyInMainThread, user_data );
172}
173
174static TrTorrent *
175maketorrent( tr_torrent * tor )
176{
177    TrTorrent * gtor = g_object_new( TR_TORRENT_TYPE, NULL );
178
179    gtor->priv->handle = tor;
180    tr_torrentSetCompletenessCallback( tor, completenessChangedCallback, gtor );
181    return gtor;
182}
183
184TrTorrent*
185tr_torrent_new_preexisting( tr_torrent * tor )
186{
187    return maketorrent( tor );
188}
189
190TrTorrent *
191tr_torrent_new_ctor( tr_session   * session,
192                     tr_ctor      * ctor,
193                     int          * errcode )
194{
195    tr_torrent * tor;
196    uint8_t      doTrash = FALSE;
197
198    /* let the gtk client handle the removal, since libT
199     * doesn't have any concept of the glib trash API */
200    tr_ctorGetDeleteSource( ctor, &doTrash );
201    tr_ctorSetDeleteSource( ctor, FALSE );
202    tor = tr_torrentNew( ctor, errcode );
203
204    if( tor && doTrash )
205    {
206        const char * config = tr_sessionGetConfigDir( session );
207        const char * source = tr_ctorGetSourceFile( ctor );
208        const int is_internal = source && ( strstr( source, config ) == source );
209
210        /* #1294: don't delete the source .torrent file if it's our internal copy */
211        if( !is_internal )
212            tr_file_trash_or_remove( source );
213    }
214
215    return tor ? maketorrent( tor ) : NULL;
216}
217
218char *
219tr_torrent_status_str( TrTorrent * gtor )
220{
221    char *          top = NULL;
222
223    const tr_stat * st = tr_torrent_stat( gtor );
224
225    const int       tpeers = MAX ( st->peersConnected, 0 );
226    const int       upeers = MAX ( st->peersGettingFromUs, 0 );
227    const int       eta = st->eta;
228
229    switch( st->activity )
230    {
231        case TR_STATUS_CHECK_WAIT:
232            top =
233                g_strdup_printf( _( "Waiting to verify local data (%.1f%% tested)" ),
234                                 tr_truncd( 100 * st->recheckProgress, 1 ) );
235            break;
236
237        case TR_STATUS_CHECK:
238            top =
239                g_strdup_printf( _( "Verifying local data (%.1f%% tested)" ),
240                                 tr_truncd( 100 * st->recheckProgress, 1 ) );
241            break;
242
243        case TR_STATUS_DOWNLOAD:
244
245            if( eta < 0 )
246                top = g_strdup_printf( _( "Remaining time unknown" ) );
247            else
248            {
249                char timestr[128];
250                tr_strltime( timestr, eta, sizeof( timestr ) );
251                /* %s is # of minutes */
252                top = g_strdup_printf( _( "%1$s remaining" ), timestr );
253            }
254            break;
255
256        case TR_STATUS_SEED:
257            top = g_strdup_printf(
258                ngettext( "Seeding to %1$'d of %2$'d connected peer",
259                          "Seeding to %1$'d of %2$'d connected peers",
260                          tpeers ),
261                upeers, tpeers );
262            break;
263
264        case TR_STATUS_STOPPED:
265            top = g_strdup( _( "Stopped" ) );
266            break;
267
268        default:
269            top = g_strdup( "???" );
270            break;
271    }
272
273    return top;
274}
275
276void
277tr_torrent_set_remove_flag( TrTorrent * gtor,
278                            gboolean    do_remove )
279{
280    if( !isDisposed( gtor ) )
281        gtor->priv->do_remove = do_remove;
282}
283
284void
285tr_torrent_delete_files( TrTorrent * gtor )
286{
287    tr_torrentDeleteLocalData( tr_torrent_handle( gtor ), tr_file_trash_or_remove );
288}
289
290void
291tr_torrent_open_folder( TrTorrent * gtor )
292{
293    const tr_torrent * tor =  tr_torrent_handle( gtor );
294    const tr_info * info = tr_torrent_info( gtor );
295    const gboolean single = info->fileCount == 1;
296    char * path = single ? g_build_filename( tr_torrentGetCurrentDir( tor ), NULL )
297                         : g_build_filename( tr_torrentGetCurrentDir( tor ), info->name, NULL );
298    gtr_open_file( path );
299    g_free( path );
300}
Note: See TracBrowser for help on using the repository browser.