source: branches/2.0x/gtk/tr-torrent.c @ 10885

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

(2.0x gtk) #3320 -- update the gtk+ client to match the new completeness' callback signature from r10878

  • Property svn:keywords set to Date Rev Author Id
File size: 6.6 KB
Line 
1/******************************************************************************
2 * $Id: tr-torrent.c 10885 2010-06-26 21:01: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#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
93static void
94tr_torrent_class_init( gpointer              g_class,
95                       gpointer g_class_data UNUSED )
96{
97    GObjectClass *gobject_class = G_OBJECT_CLASS( g_class );
98
99    gobject_class->dispose = tr_torrent_dispose;
100    g_type_class_add_private( g_class, sizeof( struct TrTorrentPrivate ) );
101}
102
103GType
104tr_torrent_get_type( void )
105{
106    static GType type = 0;
107
108    if( !type )
109    {
110        static const GTypeInfo info = {
111            sizeof ( TrTorrentClass ),
112            NULL,                     /* base_init */
113            NULL,                     /* base_finalize */
114            tr_torrent_class_init,    /* class_init */
115            NULL,                     /* class_finalize */
116            NULL,                     /* class_data */
117            sizeof ( TrTorrent ),
118            0,                        /* n_preallocs */
119            tr_torrent_init,          /* instance_init */
120            NULL,
121        };
122        type = g_type_register_static( G_TYPE_OBJECT, "TrTorrent", &info, 0 );
123    }
124    return type;
125}
126
127tr_torrent *
128tr_torrent_handle( TrTorrent *tor )
129{
130    return isDisposed( tor ) ? NULL : tor->priv->handle;
131}
132
133const tr_stat *
134tr_torrent_stat( TrTorrent *tor )
135{
136    tr_torrent * handle = tr_torrent_handle( tor );
137
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
146    return handle ? tr_torrentInfo( handle ) : NULL;
147}
148
149static gboolean
150notifyInMainThread( gpointer user_data )
151{
152    tr_notify_send( TR_TORRENT( user_data ) );
153    return FALSE;
154}
155
156static void
157completenessChangedCallback( tr_torrent       * tor,
158                             tr_completeness    completeness,
159                             tr_bool            wasRunning,
160                             void *             user_data )
161{
162    if( wasRunning && ( completeness != TR_LEECH )
163                   && ( tr_torrentStat( tor )->sizeWhenDone != 0 ) )
164        gtr_idle_add( notifyInMainThread, user_data );
165}
166
167static TrTorrent *
168maketorrent( tr_torrent * tor )
169{
170    TrTorrent * gtor = g_object_new( TR_TORRENT_TYPE, NULL );
171
172    gtor->priv->handle = tor;
173    tr_torrentSetCompletenessCallback( tor, completenessChangedCallback, gtor );
174    return gtor;
175}
176
177TrTorrent*
178tr_torrent_new_preexisting( tr_torrent * tor )
179{
180    return maketorrent( tor );
181}
182
183TrTorrent *
184tr_torrent_new_ctor( tr_session   * session,
185                     tr_ctor      * ctor,
186                     int          * errcode )
187{
188    tr_torrent * tor;
189    uint8_t      doTrash = FALSE;
190
191    /* let the gtk client handle the removal, since libT
192     * doesn't have any concept of the glib trash API */
193    tr_ctorGetDeleteSource( ctor, &doTrash );
194    tr_ctorSetDeleteSource( ctor, FALSE );
195    tor = tr_torrentNew( ctor, errcode );
196
197    if( tor && doTrash )
198    {
199        const char * config = tr_sessionGetConfigDir( session );
200        const char * source = tr_ctorGetSourceFile( ctor );
201        const int is_internal = source && ( strstr( source, config ) == source );
202
203        /* #1294: don't delete the source .torrent file if it's our internal copy */
204        if( !is_internal )
205            gtr_file_trash_or_remove( source );
206    }
207
208    return tor ? maketorrent( tor ) : NULL;
209}
210
211void
212tr_torrent_set_remove_flag( TrTorrent * gtor,
213                            gboolean    do_remove )
214{
215    if( !isDisposed( gtor ) )
216        gtor->priv->do_remove = do_remove;
217}
218
219void
220tr_torrent_open_folder( TrTorrent * gtor )
221{
222    const tr_torrent * tor =  tr_torrent_handle( gtor );
223
224    if( tor != NULL )
225    {
226        const tr_info * info = tr_torrent_info( gtor );
227        const gboolean single = info->fileCount == 1;
228        const char * currentDir = tr_torrentGetCurrentDir( tor );
229        if( single )
230            gtr_open_file( currentDir );
231        else {
232            char * path = g_build_filename( currentDir, info->name, NULL );
233            gtr_open_file( path );
234            g_free( path );
235        }
236    }
237}
Note: See TracBrowser for help on using the repository browser.