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

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

(gtk) minor: use "Folder" instead of "Directory", as per the GNOME Desktop Terms list

  • Property svn:keywords set to Date Rev Author Id
File size: 8.0 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: open-dialog.c 5207 2008-03-06 20:55:07Z 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_torrentVerify( 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
129GtkWidget*
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    gtk_dialog_set_default_response( GTK_DIALOG( d ),
152                                     GTK_RESPONSE_ACCEPT );
153    gtk_dialog_set_alternative_button_order( GTK_DIALOG( d ),
154                                             GTK_RESPONSE_ACCEPT,
155                                             GTK_RESPONSE_CANCEL,
156                                             -1 );
157
158    if( tr_ctorGetDestination( ctor, TR_FORCE, &str ) )
159        g_assert_not_reached( );
160    data = g_new0( struct OpenData, 1 );
161    data->core = core;
162    data->ctor = ctor;
163    data->filename = g_strdup( tr_ctorGetSourceFile( ctor ) );
164    data->destination = g_strdup( str );
165    g_signal_connect( G_OBJECT( d ), "response",
166                      G_CALLBACK( openResponseCB ), data );
167
168    t = gtk_table_new( 6, 2, FALSE );
169    gtk_container_set_border_width( GTK_CONTAINER( t ), GUI_PAD_BIG );
170    gtk_table_set_row_spacings( GTK_TABLE( t ), GUI_PAD );
171    gtk_table_set_col_spacings( GTK_TABLE( t ), GUI_PAD_BIG );
172
173    row = col = 0;
174    l = gtk_label_new_with_mnemonic( _( "_Torrent file:" ) );
175    gtk_misc_set_alignment( GTK_MISC( l ), 0.0f, 0.5f );
176    gtk_table_attach( GTK_TABLE( t ), l, col, col+1, row, row+1, GTK_FILL, 0, 0, 0 );
177    ++col;
178    w = gtk_file_chooser_button_new( _( "Select Torrent" ),
179                                     GTK_FILE_CHOOSER_ACTION_OPEN );
180    gtk_table_attach( GTK_TABLE( t ), w, col, col+1, row, row+1, ~0, 0, 0, 0 );
181    gtk_label_set_mnemonic_widget( GTK_LABEL( l ), w );
182    filter = gtk_file_filter_new( );
183    gtk_file_filter_set_name( filter, _( "Torrent files" ) );
184    gtk_file_filter_add_pattern( filter, "*.torrent" );
185    gtk_file_chooser_add_filter( GTK_FILE_CHOOSER( w ), filter );
186    filter = gtk_file_filter_new( );
187    gtk_file_filter_set_name( filter, _( "All files" ) );
188    gtk_file_filter_add_pattern( filter, "*" );
189    gtk_file_chooser_add_filter( GTK_FILE_CHOOSER( w ), filter );
190    g_signal_connect( w, "selection-changed",
191                      G_CALLBACK( sourceChanged ), data );
192    if( data->filename )
193        if( !gtk_file_chooser_set_filename( GTK_FILE_CHOOSER( w ), data->filename ) )
194            g_warning( "couldn't select '%s'", data->filename );
195
196    ++row;
197    col = 0;
198    l = gtk_label_new_with_mnemonic( _( "Destination _folder:" ) );
199    gtk_misc_set_alignment( GTK_MISC( l ), 0.0f, 0.5f );
200    gtk_table_attach( GTK_TABLE( t ), l, col, col+1, row, row+1, GTK_FILL, 0, 0, 0 );
201    ++col;
202    w = gtk_file_chooser_button_new( _( "Destination" ), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER );
203    if( !gtk_file_chooser_set_filename( GTK_FILE_CHOOSER( w ), data->destination ) )
204        g_warning( "couldn't select '%s'", data->destination );
205    gtk_table_attach( GTK_TABLE( t ), w, col, col+1, row, row+1, ~0, 0, 0, 0 );
206    gtk_label_set_mnemonic_widget( GTK_LABEL( l ), w );
207    g_signal_connect( w, "selection-changed", G_CALLBACK( destinationChanged ), data );
208
209    ++row;
210    col = 0;
211    w = file_list_new( NULL );
212    gtk_widget_set_size_request ( w, 466u, 300u );
213    data->list = w;
214    gtk_table_attach_defaults( GTK_TABLE( t ), w, col, col+2, row, row+1 );
215
216    ++row;
217    col = 0;
218    w = gtk_button_new_with_mnemonic( _( "Verify Local Data" ) );
219    gtk_table_attach( GTK_TABLE( t ), w, col, col+1, row, row+1, GTK_FILL, 0, 0, 0 );
220    g_signal_connect( w, "clicked", G_CALLBACK( verifyRequested ), data );
221
222    ++row;
223    col = 0;
224    w = gtk_check_button_new_with_mnemonic( _( "_Delete original torrent file" ) );
225    data->delete_check = GTK_TOGGLE_BUTTON( w );
226    if( tr_ctorGetDeleteSource( ctor, &flag ) )
227        g_assert_not_reached( );
228    gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( w ), flag );
229    gtk_table_attach( GTK_TABLE( t ), w, col, col+2, row, row+1, GTK_FILL, 0, 0, 0 );
230
231    ++row;
232    col = 0;
233    w = gtk_check_button_new_with_mnemonic( _( "_Start when added" ) );
234    data->run_check = GTK_TOGGLE_BUTTON( w );
235    if( tr_ctorGetPaused( ctor, TR_FORCE, &flag ) )
236        g_assert_not_reached( );
237    gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( w ), !flag );
238    gtk_table_attach( GTK_TABLE( t ), w, col, col+2, row, row+1, GTK_FILL, 0, 0, 0 );
239
240    gtk_box_pack_start_defaults( GTK_BOX( GTK_DIALOG( d )->vbox ), t );
241    gtk_widget_show_all( d );
242    return d;
243}
Note: See TracBrowser for help on using the repository browser.