[a2776f]: uget-gtk / UgDownloadDialog.c Maximize Restore History

Download this file

UgDownloadDialog.c    414 lines (375 with data), 14.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
*
* Copyright (C) 2005-2011 by C.H. Huang
* plushuang.tw@gmail.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* ---
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU Lesser General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*
*/
#include <UgUri.h>
#include <UgDownloadDialog.h>
#include <UgData-download.h>
#include <glib/gi18n.h>
// static functions
static void ug_download_dialog_set_page0 (UgDownloadDialog* ddialog, gboolean completed);
static void ug_download_dialog_set_page1 (UgDownloadDialog* ddialog, gboolean completed);
// signal handlers
static void on_dialog_show (GtkWidget *widget, UgDownloadDialog* ddialog);
static void on_button_back (GtkWidget* button, UgDownloadDialog* ddialog);
static void on_button_forward (GtkWidget* button, UgDownloadDialog* ddialog);
static void on_category_cursor_changed (GtkTreeView* view, UgDownloadDialog* ddialog);
UgDownloadDialog* ug_download_dialog_new (const gchar* title, GtkWindow* parent)
{
UgDownloadDialog* ddialog;
GtkWidget* widget;
GtkBox* box;
ddialog = g_malloc0 (sizeof (UgDownloadDialog));
ddialog->self = (GtkDialog*) gtk_dialog_new_with_buttons (title, parent,
GTK_DIALOG_DESTROY_WITH_PARENT, NULL);
box = (GtkBox*) gtk_dialog_get_content_area (ddialog->self);
widget = gtk_hbox_new (FALSE, 2);
gtk_box_pack_start (box, widget, TRUE, TRUE, 0);
ddialog->hbox = (GtkBox*) widget;
gtk_widget_show_all (GTK_WIDGET (box));
// back button
box = (GtkBox*) gtk_dialog_get_action_area (ddialog->self);
widget = gtk_button_new_from_stock (GTK_STOCK_GO_BACK);
gtk_box_pack_start (box, widget, FALSE, FALSE, 0);
ddialog->button_back = widget;
g_signal_connect (widget, "clicked", G_CALLBACK (on_button_back), ddialog);
// forward button
widget = gtk_button_new_from_stock (GTK_STOCK_GO_FORWARD);
gtk_box_pack_start (box, widget, FALSE, FALSE, 0);
ddialog->button_forward = widget;
g_signal_connect (widget, "clicked", G_CALLBACK (on_button_forward), ddialog);
// OK & cancel buttons
gtk_dialog_add_button (ddialog->self, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
gtk_dialog_add_button (ddialog->self, GTK_STOCK_OK, GTK_RESPONSE_OK);
gtk_dialog_set_default_response (ddialog->self, GTK_RESPONSE_OK);
// form (Page 1)
ug_proxy_form_init (&ddialog->proxy, TRUE);
ug_download_form_init (&ddialog->download,
&ddialog->proxy, (GtkWindow*) ddialog->self);
// initialize page status
ddialog->page.array[1] = ug_download_from_use_notebook (&ddialog->download, NULL, NULL);
ddialog->page.completed[1] = FALSE;
ddialog->page.completed[0] = TRUE;
ug_download_dialog_set_current_page (ddialog, 1);
// set callback
ddialog->download.notify.func = (gpointer) ug_download_dialog_set_page1;
ddialog->download.notify.data = ddialog;
// signal
g_signal_connect_after (ddialog->self, "show",
G_CALLBACK (on_dialog_show), ddialog);
return ddialog;
}
void ug_download_dialog_free (UgDownloadDialog* ddialog)
{
// remove pages from container and destroy them.
g_object_ref (ddialog->page.current);
gtk_container_remove (GTK_CONTAINER (ddialog->hbox), ddialog->page.current);
if (ddialog->selector.self) {
ug_selector_finalize (&ddialog->selector);
gtk_widget_destroy (ddialog->selector.self);
}
if (ddialog->download.page1) {
gtk_widget_destroy (ddialog->page.array[1]);
// gtk_widget_destroy (ddialog->download.page1);
// gtk_widget_destroy (ddialog->download.page2);
}
if (ddialog->batch.self)
gtk_widget_destroy (ddialog->batch.self);
// free external data
if (ddialog->dataset)
ug_dataset_unref (ddialog->dataset);
// destroy dialog
gtk_widget_destroy (GTK_WIDGET (ddialog->self));
g_free (ddialog);
}
void ug_download_dialog_use_batch (UgDownloadDialog* ddialog)
{
GtkRequisition requisition;
// (Page 0)
ug_batch_form_init (&ddialog->batch);
ug_download_form_set_multiple (&ddialog->download, TRUE);
gtk_widget_size_request (ddialog->download.page1, &requisition);
gtk_widget_set_size_request (ddialog->batch.self,
requisition.width, requisition.height);
// setup page and button
ddialog->page.array[0] = ddialog->batch.self;
ddialog->page.completed[0] = FALSE;
ddialog->page.completed[1] = TRUE;
ug_download_dialog_set_current_page (ddialog, 0);
gtk_widget_show (ddialog->button_back);
gtk_widget_show (ddialog->button_forward);
// set callback
ddialog->batch.notify.func = (gpointer) ug_download_dialog_set_page0;
ddialog->batch.notify.data = ddialog;
}
void ug_download_dialog_use_selector (UgDownloadDialog* ddialog)
{
GtkRequisition requisition;
// (Page 0)
ug_selector_init (&ddialog->selector, (GtkWindow*) ddialog->self);
ug_download_form_set_multiple (&ddialog->download, TRUE);
gtk_widget_size_request (ddialog->download.page1, &requisition);
gtk_widget_set_size_request (ddialog->selector.self,
requisition.width, requisition.height);
// setup page and button
ddialog->page.array[0] = ddialog->selector.self;
ddialog->page.completed[0] = FALSE;
ddialog->page.completed[1] = TRUE;
ug_download_dialog_set_current_page (ddialog, 0);
gtk_widget_show (ddialog->button_back);
gtk_widget_show (ddialog->button_forward);
// set callback
ddialog->selector.notify.func = (gpointer) ug_download_dialog_set_page0;
ddialog->selector.notify.data = ddialog;
}
void ug_download_dialog_set_category (UgDownloadDialog* ddialog, UgCategoryWidget* cwidget)
{
GtkTreeModel* model;
GtkTreePath* path;
GtkWidget* scrolled;
GtkBox* vbox;
ddialog->category_view = ug_category_view_new ();
ug_category_view_set_icon_visible (ddialog->category_view, FALSE);
model = cwidget->filter;
gtk_tree_view_set_model (ddialog->category_view, model);
gtk_widget_set_size_request ((GtkWidget*) ddialog->category_view, 165, 100);
// signal
g_signal_connect (ddialog->category_view, "cursor-changed",
G_CALLBACK (on_category_cursor_changed), ddialog);
// select current category and emit "cursor-changed" signal.
gtk_tree_view_get_cursor (cwidget->view, &path, NULL);
if (path == NULL)
path = gtk_tree_path_new_from_indices (0, -1);
else if (gtk_tree_path_get_depth (path) == 2)
gtk_tree_path_up (path);
gtk_tree_view_set_cursor (ddialog->category_view, path, NULL, FALSE);
gtk_tree_path_free (path);
// scrolled window
scrolled = gtk_scrolled_window_new (NULL, NULL);
gtk_widget_show (scrolled);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled),
GTK_SHADOW_IN);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_container_add (GTK_CONTAINER (scrolled),
GTK_WIDGET (ddialog->category_view));
// pack vbox
vbox = (GtkBox*) gtk_vbox_new (FALSE, 2);
gtk_box_pack_start (vbox, gtk_label_new (_("Category")), FALSE, FALSE, 0);
gtk_box_pack_start (vbox, (GtkWidget*) scrolled, TRUE, TRUE, 0);
gtk_widget_show_all ((GtkWidget*) vbox);
// pack hbox
gtk_box_pack_start (ddialog->hbox, (GtkWidget*) vbox, TRUE, TRUE, 1);
}
void ug_download_dialog_set_current_page (UgDownloadDialog* ddialog, gint nth_page)
{
if (ddialog->page.current) {
g_object_ref (ddialog->page.current);
gtk_container_remove (GTK_CONTAINER (ddialog->hbox), ddialog->page.current);
ddialog->page.current = NULL;
}
if (nth_page == 0 && ddialog->page.array[0]) {
ddialog->page.current = ddialog->page.array[0];
gtk_box_pack_end (ddialog->hbox, ddialog->page.current, TRUE, TRUE, 1);
gtk_widget_set_sensitive (ddialog->button_back, FALSE);
gtk_widget_set_sensitive (ddialog->button_forward, TRUE);
}
else if (nth_page == 1) {
ddialog->page.current = ddialog->page.array[1];
gtk_box_pack_end (ddialog->hbox, ddialog->page.current, TRUE, TRUE, 1);
gtk_widget_set_sensitive (ddialog->button_back, TRUE);
gtk_widget_set_sensitive (ddialog->button_forward, FALSE);
}
}
void ug_download_dialog_get (UgDownloadDialog* ddialog, UgDataset* dataset)
{
ug_proxy_form_get (&ddialog->proxy, dataset);
ug_download_form_get (&ddialog->download, dataset);
}
void ug_download_dialog_set (UgDownloadDialog* ddialog, UgDataset* dataset)
{
ug_proxy_form_set (&ddialog->proxy, dataset, FALSE);
ug_download_form_set (&ddialog->download, dataset, FALSE);
}
UgCategory* ug_download_dialog_get_category (UgDownloadDialog* ddialog)
{
UgCategory* category;
GtkTreeModel* model;
GtkTreePath* path;
GtkTreeIter iter;
gtk_tree_view_get_cursor (ddialog->category_view, &path, NULL);
model = gtk_tree_view_get_model (ddialog->category_view);
if (path) {
gtk_tree_model_get_iter (model, &iter, path);
gtk_tree_path_free (path);
}
else if (gtk_tree_model_get_iter_first (model, &iter) == FALSE)
return NULL;
gtk_tree_model_get (model, &iter, 0, &category, -1);
return category;
}
GList* ug_download_dialog_get_downloads (UgDownloadDialog* ddialog)
{
UgDataset* dataset;
UgDataCommon* common;
GList* list;
GList* link;
// batch
if (ddialog->batch.self) {
list = ug_batch_form_get_list (&ddialog->batch, FALSE);
for (link = list; link; link = link->next) {
dataset = ug_dataset_new ();
ug_download_dialog_get (ddialog, dataset);
common = ug_dataset_realloc (dataset, UG_DATA_COMMON_I, 0);
g_free (common->url);
common->url = link->data;
ug_download_complete_data (dataset);
link->data = dataset;
}
return list;
}
// selector
if (ddialog->selector.self) {
dataset = ug_dataset_new ();
ug_download_dialog_get (ddialog, dataset);
list = ug_selector_get_selected_downloads (&ddialog->selector);
for (link = list; link; link = link->next) {
ug_data_assign (link->data, dataset);
ug_download_complete_data (link->data);
}
ug_dataset_unref (dataset);
return list;
}
// external data
if (ddialog->dataset) {
// ug_download_dialog_free() will call ug_dataset_unref()
dataset = ddialog->dataset;
ug_dataset_ref (dataset);
}
else
dataset = ug_dataset_new ();
ug_download_dialog_get (ddialog, dataset);
list = g_list_prepend (NULL, dataset);
return list;
}
// ----------------------------------------------------------------------------
// static functions
//
static void ug_download_dialog_decide_completed (UgDownloadDialog* ddialog)
{
GtkTreePath* path;
gboolean completed;
// enable or disable GTK_RESPONSE_OK
if (ddialog->page.completed[0]==TRUE && ddialog->page.completed[1]==TRUE)
completed = TRUE;
else
completed = FALSE;
// if no category selected, disable GTK_RESPONSE_OK.
if (ddialog->category_view) {
gtk_tree_view_get_cursor (ddialog->category_view, &path, NULL);
if (path)
gtk_tree_path_free (path);
else
completed = FALSE;
}
gtk_dialog_set_response_sensitive (ddialog->self, GTK_RESPONSE_OK, completed);
}
static void ug_download_dialog_set_page0 (UgDownloadDialog* ddialog, gboolean completed)
{
ddialog->page.completed[0] = completed;
gtk_widget_set_sensitive (ddialog->button_forward, completed);
ug_download_dialog_decide_completed (ddialog);
}
static void ug_download_dialog_set_page1 (UgDownloadDialog* ddialog, gboolean completed)
{
ddialog->page.completed[1] = completed;
ug_download_dialog_decide_completed (ddialog);
}
// ----------------------------------------------------------------------------
// signal handler
//
static void on_dialog_show (GtkWidget *widget, UgDownloadDialog* ddialog)
{
// emit callback & set focus
if (ddialog->batch.self) {
ug_batch_form_update_preview (&ddialog->batch);
gtk_window_set_focus ((GtkWindow*) ddialog->self,
(GtkWidget*) ddialog->batch.entry);
}
else if (ddialog->selector.self) {
ug_selector_count_selected (&ddialog->selector);
gtk_window_set_focus ((GtkWindow*) ddialog->self,
(GtkWidget*) ddialog->selector.notebook);
}
else if (gtk_widget_is_sensitive (ddialog->download.url_entry)) {
gtk_window_set_focus ((GtkWindow*) ddialog->self,
(GtkWidget*) ddialog->download.url_entry);
}
else {
gtk_window_set_focus ((GtkWindow*) ddialog->self,
(GtkWidget*) ddialog->download.folder_entry);
}
}
static void on_button_back (GtkWidget* button, UgDownloadDialog* ddialog)
{
ug_download_dialog_set_current_page (ddialog, 0);
}
static void on_button_forward (GtkWidget* button, UgDownloadDialog* ddialog)
{
ug_download_dialog_set_current_page (ddialog, 1);
}
static void on_category_cursor_changed (GtkTreeView* view, UgDownloadDialog* ddialog)
{
GtkTreeModel* model;
GtkTreePath* path;
GtkTreeIter iter;
UgCategory* category;
gtk_tree_view_get_cursor (view, &path, NULL);
if (path == NULL) {
gtk_dialog_set_response_sensitive (ddialog->self, GTK_RESPONSE_OK, FALSE);
return;
}
if (ddialog->page.completed[0]==TRUE && ddialog->page.completed[1]==TRUE)
gtk_dialog_set_response_sensitive (ddialog->self, GTK_RESPONSE_OK, TRUE);
// apply settings
model = gtk_tree_view_get_model (view);
gtk_tree_view_get_cursor (view, &path, NULL);
gtk_tree_model_get_iter (model, &iter, path);
gtk_tree_path_free (path);
gtk_tree_model_get (model, &iter, 0, &category, -1);
if (category->defaults) {
ug_download_form_set (&ddialog->download, category->defaults, TRUE);
ug_proxy_form_set (&ddialog->proxy, category->defaults, TRUE);
}
}