source: trunk/gtk/open.c @ 5021

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

(gtk) more open dialog tweaks based on feedback from Lacrocivious

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