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

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

(gtk) use tr_torrentDelete() when user hits `cancel' in the "open torrent" dialog

File size: 7.6 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        file_list_set_torrent( data->list, NULL );
38
39        tr_torrent_set_delete_flag( data->gtor, TRUE );
40        g_object_unref( G_OBJECT( data->gtor ) );
41        data->gtor = NULL;
42    }
43}
44
45static void
46openResponseCB( GtkDialog * dialog, gint response, gpointer gdata )
47{
48    struct OpenData * data = gdata;
49
50    if( data->gtor )
51    {
52        if( response != GTK_RESPONSE_ACCEPT )
53            deleteOldTorrent( data );
54        else {
55            if( gtk_toggle_button_get_active( data->run_check ) )
56                tr_torrentStart( tr_torrent_handle( data->gtor ) );
57            tr_core_add_torrent( data->core, data->gtor );
58            if( gtk_toggle_button_get_active( data->delete_check ) )
59                g_unlink( data->filename );
60        }
61    }
62
63    tr_ctorFree( data->ctor );
64    g_free( data->filename );
65    g_free( data->destination );
66    g_free( data );
67    gtk_widget_destroy( GTK_WIDGET( dialog ) );
68}
69
70static void
71updateTorrent( struct OpenData * o )
72{
73    if( o->gtor )
74        tr_torrentSetFolder( tr_torrent_handle( o->gtor ), o->destination );
75
76    file_list_set_torrent( o->list, o->gtor );
77}
78
79static void
80sourceChanged( GtkFileChooserButton * b, gpointer gdata )
81{
82    struct OpenData * data = gdata;
83
84    deleteOldTorrent( data );
85
86    g_free( data->filename );
87    data->filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( b ) );
88
89    /* maybe instantiate a torrent */
90    if( data->filename ) {
91        int err = 0;
92        tr_torrent * torrent;
93        tr_handle * handle = tr_core_handle( data->core );
94        tr_ctorSetMetainfoFromFile( data->ctor, data->filename );
95        tr_ctorSetDestination( data->ctor, TR_FORCE, data->destination );
96        tr_ctorSetPaused( data->ctor, TR_FORCE, TRUE );
97        tr_ctorSetDeleteSource( data->ctor, FALSE );
98        if(( torrent = tr_torrentNew( handle, data->ctor, &err )))
99            data->gtor = tr_torrent_new_preexisting( torrent );
100    }
101
102    updateTorrent( data );
103}
104
105static void
106verifyRequested( GtkButton * button UNUSED, gpointer gdata )
107{
108    struct OpenData * data = gdata;
109    if( data->gtor )
110        tr_torrentRecheck( tr_torrent_handle( data->gtor ) );
111}
112
113static void
114destinationChanged( GtkFileChooserButton * b, gpointer gdata )
115{
116    struct OpenData * data = gdata;
117
118    g_free( data->destination );
119    data->destination = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( b ) );
120
121    updateTorrent( data );
122    verifyRequested( NULL, data );
123}
124
125/****
126*****
127****/
128
129void
130makeaddwind( GtkWindow  * parent,
131             TrCore     * core,
132             tr_ctor    * ctor )
133{
134    int row;
135    int col;
136    const char * str;
137    GtkWidget * w;
138    GtkWidget * d;
139    GtkWidget * t;
140    GtkWidget * l;
141    GtkFileFilter * filter;
142    struct OpenData * data;
143    uint8_t flag;
144
145    /* make the dialog */
146    d = gtk_dialog_new_with_buttons( _( "Open Torrent" ), parent,
147            GTK_DIALOG_DESTROY_WITH_PARENT|GTK_DIALOG_NO_SEPARATOR,
148            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
149            GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
150            NULL );
151
152    if( tr_ctorGetDestination( ctor, TR_FORCE, &str ) )
153        g_assert_not_reached( );
154    data = g_new0( struct OpenData, 1 );
155    data->core = core;
156    data->ctor = ctor;
157    data->filename = g_strdup( tr_ctorGetSourceFile( ctor ) );
158    data->destination = g_strdup( str );
159    g_signal_connect( G_OBJECT( d ), "response",
160                      G_CALLBACK( openResponseCB ), data );
161
162    t = gtk_table_new( 6, 2, FALSE );
163    gtk_container_set_border_width( GTK_CONTAINER( t ), GUI_PAD_BIG );
164    gtk_table_set_row_spacings( GTK_TABLE( t ), GUI_PAD );
165    gtk_table_set_col_spacings( GTK_TABLE( t ), GUI_PAD_BIG );
166
167    row = col = 0;
168    l = gtk_label_new_with_mnemonic( _( "_Torrent file:" ) );
169    gtk_misc_set_alignment( GTK_MISC( l ), 0.0f, 0.5f );
170    gtk_table_attach( GTK_TABLE( t ), l, col, col+1, row, row+1, GTK_FILL, 0, 0, 0 );
171    ++col;
172    w = gtk_file_chooser_button_new( _( "Select Torrent" ),
173                                     GTK_FILE_CHOOSER_ACTION_OPEN );
174    gtk_table_attach( GTK_TABLE( t ), w, col, col+1, row, row+1, ~0, 0, 0, 0 );
175    gtk_label_set_mnemonic_widget( GTK_LABEL( l ), w );
176    filter = gtk_file_filter_new( );
177    gtk_file_filter_set_name( filter, _( "Torrent files" ) );
178    gtk_file_filter_add_pattern( filter, "*.torrent" );
179    gtk_file_chooser_add_filter( GTK_FILE_CHOOSER( w ), filter );
180    filter = gtk_file_filter_new( );
181    gtk_file_filter_set_name( filter, _( "All files" ) );
182    gtk_file_filter_add_pattern( filter, "*" );
183    gtk_file_chooser_add_filter( GTK_FILE_CHOOSER( w ), filter );
184    g_signal_connect( w, "selection-changed",
185                      G_CALLBACK( sourceChanged ), data );
186    if( data->filename )
187        if( !gtk_file_chooser_set_filename( GTK_FILE_CHOOSER( w ), data->filename ) )
188            g_warning( "couldn't select '%s'", data->filename );
189
190    ++row;
191    col = 0;
192    l = gtk_label_new_with_mnemonic( _( "Download _to:" ) );
193    gtk_misc_set_alignment( GTK_MISC( l ), 0.0f, 0.5f );
194    gtk_table_attach( GTK_TABLE( t ), l, col, col+1, row, row+1, GTK_FILL, 0, 0, 0 );
195    ++col;
196    w = gtk_file_chooser_button_new( _( "Destination" ), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER );
197    if( !gtk_file_chooser_set_filename( GTK_FILE_CHOOSER( w ), data->destination ) )
198        g_warning( "couldn't select '%s'", data->destination );
199    gtk_table_attach( GTK_TABLE( t ), w, col, col+1, row, row+1, ~0, 0, 0, 0 );
200    gtk_label_set_mnemonic_widget( GTK_LABEL( l ), w );
201    g_signal_connect( w, "selection-changed", G_CALLBACK( destinationChanged ), data );
202
203    ++row;
204    col = 0;
205    w = file_list_new( NULL );
206    gtk_widget_set_size_request ( w, 466u, 300u );
207    data->list = w;
208    gtk_table_attach_defaults( GTK_TABLE( t ), w, col, col+2, row, row+1 );
209
210    ++row;
211    col = 0;
212    w = gtk_button_new_with_mnemonic( _( "Verify Local Data" ) );
213    gtk_table_attach( GTK_TABLE( t ), w, col, col+1, row, row+1, GTK_FILL, 0, 0, 0 );
214    g_signal_connect( w, "clicked", G_CALLBACK( verifyRequested ), data );
215
216    ++row;
217    col = 0;
218    w = gtk_check_button_new_with_mnemonic( _( "_Delete original torrent file" ) );
219    data->delete_check = GTK_TOGGLE_BUTTON( w );
220    if( tr_ctorGetDeleteSource( ctor, &flag ) )
221        g_assert_not_reached( );
222    gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( w ), flag );
223    gtk_table_attach( GTK_TABLE( t ), w, col, col+2, row, row+1, GTK_FILL, 0, 0, 0 );
224
225    ++row;
226    col = 0;
227    w = gtk_check_button_new_with_mnemonic( _( "_Start when added" ) );
228    data->run_check = GTK_TOGGLE_BUTTON( w );
229    if( tr_ctorGetPaused( ctor, TR_FORCE, &flag ) )
230        g_assert_not_reached( );
231    gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( w ), !flag );
232    gtk_table_attach( GTK_TABLE( t ), w, col, col+2, row, row+1, GTK_FILL, 0, 0, 0 );
233
234    gtk_box_pack_start_defaults( GTK_BOX( GTK_DIALOG( d )->vbox ), t );
235    gtk_widget_show_all( d );
236}
Note: See TracBrowser for help on using the repository browser.