1 #include <string.h>
2 #include "download_setting_view.h"
3 #include "urlgfe.h"
4 #include "utility.h"
5
6 // signal handler
on_choose_directory(DownloadSettingView * view)7 static void on_choose_directory(DownloadSettingView* view)
8 {
9 GtkFileSelection* selector;
10
11 selector = (GtkFileSelection*)gtk_file_selection_new (_("Choose a directory"));
12 gtk_file_selection_complete (selector,
13 gtk_entry_get_text (GTK_ENTRY(view->entry_dir)));
14 gtk_widget_set_sensitive (selector->file_list, FALSE);
15 gtk_widget_hide (selector->file_list);
16 gtk_window_set_transient_for (GTK_WINDOW(selector),
17 SETTING_VIEW(view)->parent);
18
19 if (gtk_dialog_run (GTK_DIALOG(selector))==GTK_RESPONSE_OK ) {
20 gtk_entry_set_text (GTK_ENTRY(view->entry_dir),
21 gtk_file_selection_get_filename (selector));
22 }
23 gtk_widget_destroy (GTK_WIDGET(selector));
24 }
25
on_key_release_event(DownloadSettingView * view)26 static gboolean on_key_release_event (DownloadSettingView* view)
27 {
28 download_setting_view_url_changed (view);
29 return FALSE;
30 }
31
on_paste_clipboard(GtkEntry * e,DownloadSettingView * view)32 static void on_paste_clipboard (GtkEntry* e, DownloadSettingView* view)
33 {
34 download_setting_view_url_changed (view);
35 }
36
on_destroy(DownloadSettingView * view)37 static void on_destroy (DownloadSettingView* view)
38 {
39 g_string_free (view->last_filename, TRUE);
40 }
41
42 // DownloadSettingView method
43 // return : if URL is valid or MULTI_DOWNLOAD mode return TRUE.
download_setting_view_url_changed(DownloadSettingView * view)44 gboolean download_setting_view_url_changed (DownloadSettingView* view)
45 {
46 UrlPart up;
47
48 url_part (&up, gtk_entry_get_text (GTK_ENTRY(view->entry_url)));
49
50 if ( view->last_filename->str && up.name_beg==NULL ) {
51 g_string_set_size (view->last_filename, 0);
52 gtk_entry_set_text (GTK_ENTRY(view->entry_filename), "");
53 }
54 else if ( up.name_beg && strcmp (up.name_beg, view->last_filename->str)!=0 )
55 {
56 gtk_entry_set_text (GTK_ENTRY(view->entry_filename),
57 up.name_beg);
58 g_string_assign (view->last_filename,
59 up.name_beg);
60 }
61
62 return (up.protocol_beg && up.address_beg) ? TRUE : FALSE;
63 }
64
download_setting_view_init(DownloadSettingView * view)65 void download_setting_view_init (DownloadSettingView* view)
66 {
67 GtkWidget* hbox;
68 GtkWidget* vbox;
69 GtkWidget* label;
70 GtkWidget* frame;
71 GtkSizeGroup* size_group;
72
73 // initial base class : ProxySettingView
74 proxy_setting_view_init (&view->proxy_setting_view);
75 proxy_setting_view_set_multiple (PROXY_SETTING_VIEW (view), TRUE);
76
77 view->multiple = FALSE;
78 view->last_filename = g_string_sized_new (40);
79
80 /* create widgets */
81 view->self = gtk_vbox_new (FALSE, 3);
82 view->radio_auto = gtk_radio_button_new_with_mnemonic (NULL,
83 _("Au_tomatic"));
84 view->radio_manual = gtk_radio_button_new_with_mnemonic_from_widget (
85 (GtkRadioButton*)view->radio_auto,
86 _("_Manual"));
87 size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
88
89 // pack URL label & entry
90 hbox = gtk_hbox_new (FALSE, 3);
91 view->entry_url = gtk_entry_new ();
92 gtk_entry_set_width_chars (GTK_ENTRY(view->entry_url), 30);
93 view->label_url = gtk_label_new_with_mnemonic (_("_URL :"));
94 gtk_size_group_add_widget (size_group,
95 view->label_url);
96 gtk_label_set_mnemonic_widget (GTK_LABEL(view->label_url),
97 view->entry_url);
98 gtk_box_pack_start (GTK_BOX(hbox), view->label_url, FALSE, FALSE, 0);
99 gtk_box_pack_start (GTK_BOX(hbox), view->entry_url, TRUE, TRUE, 0);
100 gtk_box_pack_start (GTK_BOX(view->self), hbox, FALSE, FALSE, 0);
101
102 // pack "Save to" & folder button
103 hbox = gtk_hbox_new (FALSE, 3);
104 view->button_folder = gtk_button_new ();
105 gtk_container_add (GTK_CONTAINER (view->button_folder),
106 gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_SMALL_TOOLBAR));
107 gtk_button_set_relief (GTK_BUTTON (view->button_folder),
108 GTK_RELIEF_NONE);
109 view->combo_dir = gtk_combo_new ();
110 view->entry_dir = GTK_COMBO(view->combo_dir)->entry;
111 label = gtk_label_new_with_mnemonic (_("S_ave to :"));
112 gtk_size_group_add_widget (size_group, label);
113 gtk_label_set_mnemonic_widget(GTK_LABEL(label),
114 view->combo_dir);
115 gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
116 gtk_box_pack_start (GTK_BOX(hbox), view->combo_dir, TRUE, TRUE, 0);
117 gtk_box_pack_start (GTK_BOX(hbox), view->button_folder, FALSE, FALSE, 0);
118 gtk_box_pack_start (GTK_BOX(view->self), hbox, FALSE, FALSE, 0);
119
120 // pack "rename" label & entry
121 hbox = gtk_hbox_new (FALSE, 3);
122 view->entry_filename = gtk_entry_new ();
123 view->label_filename = gtk_label_new_with_mnemonic (_("Re_name :"));
124 gtk_size_group_add_widget (size_group, view->label_filename);
125 gtk_label_set_mnemonic_widget (GTK_LABEL(view->label_filename),
126 view->entry_filename);
127 gtk_box_pack_start (GTK_BOX(hbox), view->label_filename, FALSE, FALSE, 0);
128 gtk_box_pack_start (GTK_BOX(hbox), view->entry_filename, TRUE, TRUE, 0);
129 gtk_box_pack_start (GTK_BOX(view->self), hbox, FALSE, FALSE, 0);
130
131 // pack "Referer" label & entry
132 hbox = gtk_hbox_new (FALSE, 3);
133 view->entry_referer = gtk_entry_new ();
134 view->label_referer = gtk_label_new_with_mnemonic (_("Referer :"));
135 gtk_size_group_add_widget (size_group, view->label_referer);
136 gtk_label_set_mnemonic_widget (GTK_LABEL(view->label_referer),
137 view->entry_referer);
138 gtk_box_pack_start (GTK_BOX(hbox), view->label_referer, FALSE, FALSE, 0);
139 gtk_box_pack_start (GTK_BOX(hbox), view->entry_referer, TRUE, TRUE, 0);
140 gtk_box_pack_start (GTK_BOX(view->self), hbox, FALSE, FALSE, 0);
141
142 hbox = gtk_hbox_new (FALSE, 3);
143 frame = gtk_frame_new (_("Start"));
144 vbox = gtk_vbox_new (FALSE, 3);
145 gtk_box_pack_start (GTK_BOX(vbox), view->radio_auto, FALSE, FALSE, 0);
146 gtk_box_pack_start (GTK_BOX(vbox), view->radio_manual, FALSE, FALSE, 0);
147 gtk_container_set_border_width (GTK_CONTAINER(vbox), 3);
148 gtk_container_add (GTK_CONTAINER(frame), vbox);
149 gtk_box_pack_start (GTK_BOX(hbox), frame, FALSE, FALSE, 0);
150 gtk_box_pack_start (GTK_BOX(view->self), hbox, FALSE, FALSE, 0);
151 vbox = gtk_vbox_new (FALSE, 3);
152 gtk_container_set_border_width (GTK_CONTAINER(vbox), 3);
153 frame = gtk_frame_new (_("Other"));
154 gtk_container_add (GTK_CONTAINER(frame), vbox);
155 gtk_box_pack_start (GTK_BOX(hbox), frame, FALSE, FALSE, 0);
156
157 size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
158 hbox = gtk_hbox_new (FALSE, 3);
159 view->spin_retries = gtk_spin_button_new_with_range (0.0, 99.0, 1.0);
160 label = gtk_label_new_with_mnemonic (_("Number of retr_ies :"));
161 gtk_label_set_mnemonic_widget (GTK_LABEL(label),
162 view->spin_retries);
163 gtk_size_group_add_widget (size_group, label);
164 gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
165 gtk_box_pack_start (GTK_BOX(hbox), view->spin_retries, FALSE, FALSE, 0);
166 gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
167
168 hbox = gtk_hbox_new (FALSE, 3);
169 view->spin_delay = gtk_spin_button_new_with_range (0.0, 600.0, 1.0);
170 label = gtk_label_new_with_mnemonic (_("R_etry delay (second) :"));
171 gtk_label_set_mnemonic_widget (GTK_LABEL(label), view->spin_delay);
172 gtk_size_group_add_widget (size_group, label);
173 gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
174 gtk_box_pack_start (GTK_BOX(hbox), view->spin_delay, FALSE, FALSE, 0);
175 gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
176
177 hbox = gtk_hbox_new(FALSE, 3);
178 view->spin_redirect = gtk_spin_button_new_with_range (0.0, 30.0, 1.0);
179 label = gtk_label_new_with_mnemonic (_("_Redirection limit :"));
180 gtk_label_set_mnemonic_widget (GTK_LABEL(label),
181 view->spin_redirect);
182 gtk_size_group_add_widget (size_group, label);
183 gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
184 gtk_box_pack_start (GTK_BOX(hbox), view->spin_redirect, FALSE, FALSE, 0);
185 gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
186
187 gtk_widget_show_all (GTK_WIDGET(view->self));
188
189 /* add proxy setting view in a frame */
190 frame = gtk_frame_new (_("Proxy Setting"));
191 gtk_container_add (GTK_CONTAINER(frame),
192 view->proxy_setting_view.self);
193 gtk_container_set_border_width (GTK_CONTAINER(view->proxy_setting_view.self), 5);
194 gtk_box_pack_start (GTK_BOX(view->self), frame, FALSE, FALSE, 0);
195 gtk_widget_show (frame);
196
197 /* change setting view container */
198 SETTING_VIEW (view)->view_container = view->self;
199
200 g_signal_connect_swapped (view->self, "destroy",
201 G_CALLBACK(on_destroy), view);
202 g_signal_connect_swapped (view->button_folder, "clicked",
203 G_CALLBACK(on_choose_directory), view);
204 g_signal_connect_swapped (view->entry_url, "key-release-event",
205 G_CALLBACK(on_key_release_event), view);
206 g_signal_connect_after (view->entry_url, "paste-clipboard",
207 G_CALLBACK(on_paste_clipboard), view);
208 }
209
download_setting_view_set(DownloadSettingView * view,DownloadSetting * setting)210 void download_setting_view_set (DownloadSettingView* view,
211 DownloadSetting* setting)
212 {
213 if( setting==NULL )
214 return;
215 proxy_setting_view_set (&view->proxy_setting_view,
216 &setting->proxy);
217
218 gtk_entry_set_text (GTK_ENTRY(view->entry_dir),
219 STR_N2E(setting->directory));
220 gtk_spin_button_set_value (GTK_SPIN_BUTTON(view->spin_redirect),
221 setting->n_redirect);
222 gtk_spin_button_set_value (GTK_SPIN_BUTTON(view->spin_retries),
223 setting->n_retries);
224 gtk_spin_button_set_value (GTK_SPIN_BUTTON(view->spin_delay),
225 setting->retry_delay);
226
227 gtk_entry_set_text (GTK_ENTRY(view->entry_referer),
228 STR_N2E(setting->referer));
229
230 if( view->multiple==FALSE && setting->multiple==FALSE )
231 {
232 gtk_entry_set_text (GTK_ENTRY(view->entry_url),
233 STR_N2E(setting->url));
234 gtk_entry_set_text (GTK_ENTRY(view->entry_filename),
235 STR_N2E(setting->filename));
236 if (setting->filename==NULL)
237 download_setting_view_url_changed (view);
238 }
239
240 switch (setting->start_mode) {
241 case DOWNLOAD_START_AUTO:
242 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(view->radio_auto),
243 TRUE);
244 break;
245 case DOWNLOAD_START_MANUAL:
246 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(view->radio_manual),
247 TRUE);
248 break;
249 }
250 }
251
download_setting_view_get(DownloadSettingView * view,DownloadSetting * setting)252 void download_setting_view_get (DownloadSettingView* view,
253 DownloadSetting* setting)
254 {
255 if( setting==NULL )
256 return;
257 proxy_setting_view_get (&view->proxy_setting_view,
258 &setting->proxy);
259
260 download_setting_set_directory (setting,
261 gtk_entry_get_text (GTK_ENTRY(view->entry_dir)) );
262
263 setting->n_redirect = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(view->spin_redirect));
264 setting->n_retries = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(view->spin_retries));
265 setting->retry_delay = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(view->spin_delay));
266
267 download_setting_set_referer (setting,
268 gtk_entry_get_text (GTK_ENTRY(view->entry_referer)) );
269
270 if ( view->multiple==FALSE ) {
271 download_setting_set_filename (setting,
272 gtk_entry_get_text (GTK_ENTRY(view->entry_filename)) );
273 download_setting_set_url (setting,
274 gtk_entry_get_text (GTK_ENTRY(view->entry_url)) );
275 }
276
277 if ( gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(view->radio_auto)) )
278 download_setting_set_start_mode (setting, DOWNLOAD_START_AUTO);
279 else
280 download_setting_set_start_mode (setting, DOWNLOAD_START_MANUAL);
281 }
282
download_setting_view_check_field(DownloadSettingView * view)283 gint download_setting_view_check_field (DownloadSettingView* view)
284 {
285 const gchar *dir, *fname, *url;
286 gboolean valid_url;
287
288 dir = gtk_entry_get_text (GTK_ENTRY(view->entry_dir));
289 fname = gtk_entry_get_text (GTK_ENTRY(view->entry_filename));
290 url = gtk_entry_get_text (GTK_ENTRY(view->entry_dir));
291
292 // complete filename from URL & check URL.
293 if (*gtk_entry_get_text (GTK_ENTRY(view->entry_url))==0)
294 valid_url = download_setting_view_url_changed (view);
295
296 if ( view->multiple ) {
297 valid_url = TRUE;
298 if (*dir==0)
299 return DOWNLOAD_SETTING_UNCOMPLETE;
300 }
301 else if ( *dir==0 || *fname==0 || *url==0 )
302 return DOWNLOAD_SETTING_UNCOMPLETE_MULTIPlE;
303
304 if (valid_url)
305 return DOWNLOAD_SETTING_COMPLETE;
306
307 return DOWNLOAD_SETTING_UNCOMPLETE_URL;
308 }
309
download_setting_view_set_history(DownloadSettingView * dsv,GList * directory_history)310 void download_setting_view_set_history (DownloadSettingView* dsv,
311 GList* directory_history)
312 {
313 if (directory_history) {
314 gtk_combo_set_popdown_strings (GTK_COMBO (dsv->combo_dir),
315 directory_history);
316 }
317 }
318
download_setting_view_set_multiple(DownloadSettingView * view,gboolean multiple)319 void download_setting_view_set_multiple (DownloadSettingView *view,
320 gboolean multiple)
321 {
322 view->multiple = multiple;
323
324 if ( multiple ) {
325 gtk_widget_hide (view->entry_url);
326 gtk_widget_hide (view->entry_filename);
327 gtk_widget_hide (view->label_url);
328 gtk_widget_hide (view->label_filename);
329 gtk_widget_set_sensitive (view->entry_url, FALSE);
330 gtk_widget_set_sensitive (view->entry_filename, FALSE);
331 gtk_widget_set_sensitive (view->label_url, FALSE);
332 gtk_widget_set_sensitive (view->label_filename, FALSE);
333 }
334 else {
335 gtk_widget_show (view->entry_url);
336 gtk_widget_show (view->entry_filename);
337 gtk_widget_show (view->label_url);
338 gtk_widget_show (view->label_filename);
339 gtk_widget_set_sensitive (view->entry_url, TRUE);
340 gtk_widget_set_sensitive (view->entry_filename, TRUE);
341 gtk_widget_set_sensitive (view->label_url, TRUE);
342 gtk_widget_set_sensitive (view->label_filename, TRUE);
343 }
344 }
345
346