1 | /* |
---|
2 | * This file Copyright (C) 2009-2010 Mnemosyne LLC |
---|
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:$ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <libtransmission/transmission.h> |
---|
14 | |
---|
15 | #include <glib/gi18n.h> |
---|
16 | #include <gtk/gtk.h> |
---|
17 | |
---|
18 | #include "conf.h" /* pref_string_get */ |
---|
19 | #include "hig.h" |
---|
20 | #include "relocate.h" |
---|
21 | #include "util.h" |
---|
22 | |
---|
23 | static char * previousLocation = NULL; |
---|
24 | |
---|
25 | struct UpdateData |
---|
26 | { |
---|
27 | GtkDialog * dialog; |
---|
28 | GtkDialog * moving_dialog; |
---|
29 | int done; |
---|
30 | }; |
---|
31 | |
---|
32 | /* every once in awhile, check to see if the move is done. |
---|
33 | * if so, delete the dialog */ |
---|
34 | static gboolean |
---|
35 | onTimer( gpointer gdata ) |
---|
36 | { |
---|
37 | struct UpdateData * data = gdata; |
---|
38 | const int done = data->done; |
---|
39 | |
---|
40 | if( done == TR_LOC_ERROR ) |
---|
41 | { |
---|
42 | const int flags = GTK_DIALOG_MODAL |
---|
43 | | GTK_DIALOG_DESTROY_WITH_PARENT; |
---|
44 | GtkWidget * w = gtk_message_dialog_new( GTK_WINDOW( data->moving_dialog ), |
---|
45 | flags, |
---|
46 | GTK_MESSAGE_ERROR, |
---|
47 | GTK_BUTTONS_CLOSE, |
---|
48 | "%s", |
---|
49 | _( "Couldn't move torrent" ) ); |
---|
50 | gtk_dialog_run( GTK_DIALOG( w ) ); |
---|
51 | gtk_widget_destroy( GTK_WIDGET( data->moving_dialog ) ); |
---|
52 | } |
---|
53 | else if( done != TR_LOC_MOVING ) |
---|
54 | { |
---|
55 | gtk_widget_destroy( GTK_WIDGET( data->dialog ) ); |
---|
56 | g_free( data ); |
---|
57 | } |
---|
58 | |
---|
59 | return !done; |
---|
60 | } |
---|
61 | |
---|
62 | static void |
---|
63 | onResponse( GtkDialog * dialog, int response, gpointer unused UNUSED ) |
---|
64 | { |
---|
65 | if( response == GTK_RESPONSE_APPLY ) |
---|
66 | { |
---|
67 | struct UpdateData * updateData; |
---|
68 | |
---|
69 | GtkWidget * w; |
---|
70 | GObject * d = G_OBJECT( dialog ); |
---|
71 | tr_torrent * tor = g_object_get_data( d, "torrent" ); |
---|
72 | GtkFileChooser * chooser = g_object_get_data( d, "chooser" ); |
---|
73 | GtkToggleButton * move_tb = g_object_get_data( d, "move_rb" ); |
---|
74 | char * location = gtk_file_chooser_get_filename( chooser ); |
---|
75 | const gboolean do_move = gtk_toggle_button_get_active( move_tb ); |
---|
76 | |
---|
77 | /* pop up a dialog saying that the work is in progress */ |
---|
78 | w = gtk_message_dialog_new( GTK_WINDOW( dialog ), |
---|
79 | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL, |
---|
80 | GTK_MESSAGE_INFO, |
---|
81 | GTK_BUTTONS_CLOSE, |
---|
82 | _( "Moving \"%s\"" ), |
---|
83 | tr_torrentInfo(tor)->name ); |
---|
84 | gtk_message_dialog_format_secondary_text( GTK_MESSAGE_DIALOG( w ), _( "This may take a moment..." ) ); |
---|
85 | gtk_dialog_set_response_sensitive( GTK_DIALOG( w ), GTK_RESPONSE_CLOSE, FALSE ); |
---|
86 | gtk_widget_show( w ); |
---|
87 | |
---|
88 | /* start the move and periodically check its status */ |
---|
89 | updateData = g_new( struct UpdateData, 1 ); |
---|
90 | updateData->dialog = dialog; |
---|
91 | updateData->moving_dialog = GTK_DIALOG( w ); |
---|
92 | updateData->done = FALSE; |
---|
93 | tr_torrentSetLocation( tor, location, do_move, NULL, &updateData->done ); |
---|
94 | gtr_timeout_add_seconds( 1, onTimer, updateData ); |
---|
95 | |
---|
96 | /* remember this location so that it can be the default next time */ |
---|
97 | g_free( previousLocation ); |
---|
98 | previousLocation = location; |
---|
99 | } |
---|
100 | else |
---|
101 | { |
---|
102 | gtk_widget_destroy( GTK_WIDGET( dialog ) ); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | GtkWidget* |
---|
107 | gtr_relocate_dialog_new( GtkWindow * parent, tr_torrent * tor ) |
---|
108 | { |
---|
109 | int row; |
---|
110 | GtkWidget * w; |
---|
111 | GtkWidget * d; |
---|
112 | GtkWidget * t; |
---|
113 | |
---|
114 | d = gtk_dialog_new_with_buttons( _( "Set Torrent Location" ), parent, |
---|
115 | GTK_DIALOG_DESTROY_WITH_PARENT | |
---|
116 | GTK_DIALOG_MODAL | |
---|
117 | GTK_DIALOG_NO_SEPARATOR, |
---|
118 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, |
---|
119 | GTK_STOCK_APPLY, GTK_RESPONSE_APPLY, |
---|
120 | NULL ); |
---|
121 | g_object_set_data( G_OBJECT( d ), "torrent", tor ); |
---|
122 | gtk_dialog_set_default_response( GTK_DIALOG( d ), |
---|
123 | GTK_RESPONSE_CANCEL ); |
---|
124 | gtk_dialog_set_alternative_button_order( GTK_DIALOG( d ), |
---|
125 | GTK_RESPONSE_APPLY, |
---|
126 | GTK_RESPONSE_CANCEL, |
---|
127 | -1 ); |
---|
128 | g_signal_connect( d, "response", G_CALLBACK( onResponse ), NULL ); |
---|
129 | |
---|
130 | row = 0; |
---|
131 | t = hig_workarea_create( ); |
---|
132 | hig_workarea_add_section_title( t, &row, _( "Location" ) ); |
---|
133 | |
---|
134 | if( previousLocation == NULL ) |
---|
135 | previousLocation = g_strdup( pref_string_get( TR_PREFS_KEY_DOWNLOAD_DIR ) ); |
---|
136 | w = gtk_file_chooser_button_new( _( "Set Torrent Location" ), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER ); |
---|
137 | gtk_file_chooser_set_filename( GTK_FILE_CHOOSER( w ), previousLocation ); |
---|
138 | g_object_set_data( G_OBJECT( d ), "chooser", w ); |
---|
139 | hig_workarea_add_row( t, &row, _( "Torrent _location:" ), w, NULL ); |
---|
140 | w = gtk_radio_button_new_with_mnemonic( NULL, _( "_Move from the current folder" ) ); |
---|
141 | g_object_set_data( G_OBJECT( d ), "move_rb", w ); |
---|
142 | hig_workarea_add_wide_control( t, &row, w ); |
---|
143 | w = gtk_radio_button_new_with_mnemonic_from_widget( GTK_RADIO_BUTTON( w ), _( "Local data is _already there" ) ); |
---|
144 | hig_workarea_add_wide_control( t, &row, w ); |
---|
145 | hig_workarea_finish( t, &row ); |
---|
146 | gtk_widget_show_all( t ); |
---|
147 | gtk_box_pack_start( GTK_BOX( GTK_DIALOG( d )->vbox ), t, TRUE, TRUE, 0 ); |
---|
148 | |
---|
149 | return d; |
---|
150 | } |
---|