source: trunk/gtk/open-dialog.c @ 5064

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

(gtk) add ability to delete downloaded files when deleting a torrent

File size: 7.7 KB
Line 
1/*
2 * This file Copyright (C) 2008 Charles Kerr <charles@rebelbase.com>
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: hig.h 4404 2008-01-01 17:20:20Z charles $
11 */
12
13#include <glib/gi18n.h>
14#include <glib/gstdio.h>
15#include <gtk/gtk.h>
16#include "file-list.h"
17#include "hig.h"
18#include "open-dialog.h"
19
20struct OpenData
21{
22    TrCore * core;
23    GtkWidget * list;
24    GtkToggleButton * run_check;
25    GtkToggleButton * delete_check;
26    char * filename;
27    char * destination;
28    TrTorrent * gtor;
29    tr_ctor * ctor;
30};
31
32static void
33deleteOldTorrent( struct OpenData * data )
34{
35    if( data->gtor )
36    {
37        tr_torrent * tor;
38
39        file_list_set_torrent( data->list, NULL );
40
41        tor = tr_torrent_handle( data->gtor );
42        tr_torrentRemoveSaved( tor );
43
44        g_object_unref( G_OBJECT( data->gtor ) );
45        data->gtor = NULL;
46    }
47}
48
49static void
50openResponseCB( GtkDialog * dialog, gint response, gpointer gdata )
51{
52    struct OpenData * data = gdata;
53
54    if( data->gtor )
55    {
56        if( response != GTK_RESPONSE_ACCEPT )
57            deleteOldTorrent( data );
58        else {
59            if( gtk_toggle_button_get_active( data->run_check ) )
60                tr_torrentStart( tr_torrent_handle( data->gtor ) );
61            tr_core_add_torrent( data->core, data->gtor );
62            if( gtk_toggle_button_get_active( data->delete_check ) )
63                g_unlink( data->filename );
64        }
65    }
66
67    tr_ctorFree( data->ctor );
68    g_free( data->filename );
69    g_free( data->destination );
70    g_free( data );
71    gtk_widget_destroy( GTK_WIDGET( dialog ) );
72}
73
74static void
75updateTorrent( struct OpenData * o )
76{
77    if( o->gtor )
78        tr_torrentSetFolder( tr_torrent_handle( o->gtor ), o->destination );
79
80    file_list_set_torrent( o->list, o->gtor );
81}
82
83static void
84sourceChanged( GtkFileChooserButton * b, gpointer gdata )
85{
86    struct OpenData * data = gdata;
87
88    deleteOldTorrent( data );
89
90    g_free( data->filename );
91    data->filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( b ) );
92
93    /* maybe instantiate a torrent */
94    if( data->filename ) {
95        int err = 0;
96        tr_torrent * torrent;
97        tr_handle * handle = tr_core_handle( data->core );
98        tr_ctorSetMetainfoFromFile( data->ctor, data->filename );
99        tr_ctorSetDestination( data->ctor, TR_FORCE, data->destination );
100        tr_ctorSetPaused( data->ctor, TR_FORCE, TRUE );
101        tr_ctorSetDeleteSource( data->ctor, FALSE );
102        if(( torrent = tr_torrentNew( handle, data->ctor, &err )))
103            data->gtor = tr_torrent_new_preexisting( torrent );
104    }
105
106    updateTorrent( data );
107}
108
109static void
110verifyRequested( GtkButton * button UNUSED, gpointer gdata )
111{
112    struct OpenData * data = gdata;
113    if( data->gtor )
114        tr_torrentRecheck( tr_torrent_handle( data->gtor ) );
115}
116
117static void
118destinationChanged( GtkFileChooserButton * b, gpointer gdata )
119{
120    struct OpenData * data = gdata;
121
122    g_free( data->destination );
123    data->destination = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( b ) );
124
125    updateTorrent( data );
126    verifyRequested( NULL, data );
127}
128
129/****
130*****
131****/
132
133void
134makeaddwind( GtkWindow  * parent,
135             TrCore     * core,
136             tr_ctor    * ctor )
137{
138    int row;
139    int col;
140    const char * str;
141    GtkWidget * w;
142    GtkWidget * d;
143    GtkWidget * t;
144    GtkWidget * l;
145    GtkFileFilter * filter;
146    struct OpenData * data;
147    uint8_t flag;
148
149    /* make the dialog */
150    d = gtk_dialog_new_with_buttons( _( "Open Torrent" ), parent,
151            GTK_DIALOG_DESTROY_WITH_PARENT|GTK_DIALOG_NO_SEPARATOR,
152            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
153            GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
154            NULL );
155
156    if( tr_ctorGetDestination( ctor, TR_FORCE, &str ) )
157        g_assert_not_reached( );
158    data = g_new0( struct OpenData, 1 );
159    data->core = core;
160    data->ctor = ctor;
161    data->filename = g_strdup( tr_ctorGetSourceFile( ctor ) );
162    data->destination = g_strdup( str );
163    g_signal_connect( G_OBJECT( d ), "response",
164                      G_CALLBACK( openResponseCB ), data );
165
166    t = gtk_table_new( 6, 2, FALSE );
167    gtk_container_set_border_width( GTK_CONTAINER( t ), GUI_PAD_BIG );
168    gtk_table_set_row_spacings( GTK_TABLE( t ), GUI_PAD );
169    gtk_table_set_col_spacings( GTK_TABLE( t ), GUI_PAD_BIG );
170
171    row = col = 0;
172    l = gtk_label_new_with_mnemonic( _( "_Torrent file:" ) );
173    gtk_misc_set_alignment( GTK_MISC( l ), 0.0f, 0.5f );
174    gtk_table_attach( GTK_TABLE( t ), l, col, col+1, row, row+1, GTK_FILL, 0, 0, 0 );
175    ++col;
176    w = gtk_file_chooser_button_new( _( "Select Torrent" ),
177                                     GTK_FILE_CHOOSER_ACTION_OPEN );
178    gtk_table_attach( GTK_TABLE( t ), w, col, col+1, row, row+1, ~0, 0, 0, 0 );
179    gtk_label_set_mnemonic_widget( GTK_LABEL( l ), w );
180    filter = gtk_file_filter_new( );
181    gtk_file_filter_set_name( filter, _( "Torrent files" ) );
182    gtk_file_filter_add_pattern( filter, "*.torrent" );
183    gtk_file_chooser_add_filter( GTK_FILE_CHOOSER( w ), filter );
184    filter = gtk_file_filter_new( );
185    gtk_file_filter_set_name( filter, _( "All files" ) );
186    gtk_file_filter_add_pattern( filter, "*" );
187    gtk_file_chooser_add_filter( GTK_FILE_CHOOSER( w ), filter );
188    g_signal_connect( w, "selection-changed",
189                      G_CALLBACK( sourceChanged ), data );
190    if( data->filename )
191        if( !gtk_file_chooser_set_filename( GTK_FILE_CHOOSER( w ), data->filename ) )
192            g_warning( "couldn't select '%s'", data->filename );
193
194    ++row;
195    col = 0;
196    l = gtk_label_new_with_mnemonic( _( "Download _to:" ) );
197    gtk_misc_set_alignment( GTK_MISC( l ), 0.0f, 0.5f );
198    gtk_table_attach( GTK_TABLE( t ), l, col, col+1, row, row+1, GTK_FILL, 0, 0, 0 );
199    ++col;
200    w = gtk_file_chooser_button_new( _( "Destination" ), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER );
201    if( !gtk_file_chooser_set_filename( GTK_FILE_CHOOSER( w ), data->destination ) )
202        g_warning( "couldn't select '%s'", data->destination );
203    gtk_table_attach( GTK_TABLE( t ), w, col, col+1, row, row+1, ~0, 0, 0, 0 );
204    gtk_label_set_mnemonic_widget( GTK_LABEL( l ), w );
205    g_signal_connect( w, "selection-changed", G_CALLBACK( destinationChanged ), data );
206
207    ++row;
208    col = 0;
209    w = file_list_new( NULL );
210    gtk_widget_set_size_request ( w, 466u, 300u );
211    data->list = w;
212    gtk_table_attach_defaults( GTK_TABLE( t ), w, col, col+2, row, row+1 );
213
214    ++row;
215    col = 0;
216    w = gtk_button_new_with_mnemonic( _( "Verify Local Data" ) );
217    gtk_table_attach( GTK_TABLE( t ), w, col, col+1, row, row+1, GTK_FILL, 0, 0, 0 );
218    g_signal_connect( w, "clicked", G_CALLBACK( verifyRequested ), data );
219
220    ++row;
221    col = 0;
222    w = gtk_check_button_new_with_mnemonic( _( "_Delete original torrent file" ) );
223    data->delete_check = GTK_TOGGLE_BUTTON( w );
224    if( tr_ctorGetDeleteSource( ctor, &flag ) )
225        g_assert_not_reached( );
226    gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( w ), flag );
227    gtk_table_attach( GTK_TABLE( t ), w, col, col+2, row, row+1, GTK_FILL, 0, 0, 0 );
228
229    ++row;
230    col = 0;
231    w = gtk_check_button_new_with_mnemonic( _( "_Start when added" ) );
232    data->run_check = GTK_TOGGLE_BUTTON( w );
233    if( tr_ctorGetPaused( ctor, TR_FORCE, &flag ) )
234        g_assert_not_reached( );
235    gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( w ), !flag );
236    gtk_table_attach( GTK_TABLE( t ), w, col, col+2, row, row+1, GTK_FILL, 0, 0, 0 );
237
238    gtk_box_pack_start_defaults( GTK_BOX( GTK_DIALOG( d )->vbox ), t );
239    gtk_widget_show_all( d );
240}
Note: See TracBrowser for help on using the repository browser.