Statistics
| Revision:

root / trunk / client / gui / nzb.c @ 1863

History | View | Annotate | Download (25.3 KB)

1
/* 
2
    Copyright (C) 2005-2010  Erik van Pienbroek
3

                
4
    This program is free software; you can redistribute it and/or modify
5
    it under the terms of the GNU General Public License as published by
6
    the Free Software Foundation; either version 2 of the License, or
7
    (at your option) any later version.
8

                
9
    This program is distributed in the hope that it will be useful,
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
    GNU General Public License for more details.
13

                
14
    You should have received a copy of the GNU General Public License
15
    along with this program; if not, write to the Free Software
16
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
*/
18

                
19
#include 
20
#include "gui.h"
21
#include "config_gui.h"
22
#include "nntpgrab_utils.h"
23
#include "nntpgrab_automation.h"
24

                
25
#if GLIB_CHECK_VERSION(2,16,0)
26
#include 
27
#endif
28

                
29
static NNTPGrabNZB *nzbfile = NULL;
30
static char *collection_name = NULL;
31
static gboolean quick_import = FALSE;
32
static GtkWidget *filechooserNZB = NULL;
33

                
34
G_MODULE_EXPORT void    on_btnNZBImport_clicked(GtkWidget *caller, gpointer data);
35
G_MODULE_EXPORT void    on_filechooserNZB_file_set(GtkWidget *caller, gpointer data);
36

                
37
static void
38
add_filters_to_filechooser(GtkFileChooser *file_chooser)
39
{
40
    GtkFileFilter *filter;
41

                
42
    filter = gtk_file_filter_new();
43
    gtk_file_filter_set_name(filter, "NZB files");
44
    gtk_file_filter_add_mime_type(filter, "application/x-nzb");
45
    gtk_file_filter_add_pattern(filter, "*.nzb");
46
    gtk_file_chooser_add_filter(file_chooser, filter);
47

                
48
    filter = gtk_file_filter_new();
49
    gtk_file_filter_set_name(filter, "All files");
50
    gtk_file_filter_add_pattern (filter, "*");
51
    gtk_file_chooser_add_filter(file_chooser, filter);
52
}
53

                
54
// Callbacks from the main window
55
G_MODULE_EXPORT void
56
on_btnNZBCancel_clicked(GtkWidget *caller, gpointer data)
57
{
58
    GtkWidget *windowNZB;
59

                
60
    windowNZB = nntpgrab_gui_base_get_widget("windowNZB");
61
    gtk_widget_hide(windowNZB);
62

                
63
    gtk_widget_destroy(filechooserNZB);
64
    filechooserNZB = NULL;
65

                
66
    if (nzbfile) {
67
        nntpgrab_utils_nzb_file_free(nzbfile);
68
        nzbfile = NULL;
69
    }
70

                
71
    if (collection_name) {
72
        g_free(collection_name);
73
        collection_name = NULL;
74
    }
75
}
76

                
77
G_MODULE_EXPORT void
78
on_btnImportNZB_clicked(GtkWidget *caller, gpointer data)
79
{
80
    GtkWidget *windowNZB;
81
    GtkWidget *treeNZBFiles;
82
    GtkWidget *btnNZBImport;
83
    GtkWidget *hboxNZBFile;
84
    GtkTreeModel *model;
85
    ConfigGUIOpts opts;
86

                
87
    windowNZB = nntpgrab_gui_base_get_widget("windowNZB");
88
    treeNZBFiles = nntpgrab_gui_base_get_widget("treeNZBFiles");
89
    btnNZBImport = nntpgrab_gui_base_get_widget("btnNZBImport");
90
    hboxNZBFile = nntpgrab_gui_base_get_widget("hboxNZBFile");
91

                
92
    model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeNZBFiles));
93
    gtk_list_store_clear(GTK_LIST_STORE(model));
94

                
95
    if (filechooserNZB != NULL) {
96
        on_btnNZBCancel_clicked(windowNZB, data);
97
    }
98

                
99
    g_return_if_fail(filechooserNZB == NULL);
100

                
101
    filechooserNZB = gtk_file_chooser_button_new(_("Import NZB"), GTK_FILE_CHOOSER_ACTION_OPEN);
102
    add_filters_to_filechooser(GTK_FILE_CHOOSER(filechooserNZB));
103
    opts = config_gui_get_opts();
104
    if (strlen(opts.last_used_nzb_directory) > 0) {
105
        gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(filechooserNZB), opts.last_used_nzb_directory);
106
    }
107
    g_signal_connect(G_OBJECT(filechooserNZB), "file_set", G_CALLBACK(on_filechooserNZB_file_set), NULL);
108
    gtk_box_pack_end(GTK_BOX(hboxNZBFile), filechooserNZB, TRUE, TRUE, 5);
109
    gtk_widget_show(filechooserNZB);
110

                
111
    gtk_widget_show(windowNZB);
112
    gtk_widget_set_sensitive(btnNZBImport, FALSE);
113

                
114
    quick_import = FALSE;
115
}
116

                
117
gboolean
118
nzb_perform_quick_import(const char *collection_name_tmp, const char *nzb_contents)
119
{
120
    GtkWidget *windowMain = nntpgrab_gui_base_get_widget("windowMain");
121
    char *errmsg = NULL;
122

                
123
    if (nzbfile) {
124
        nntpgrab_utils_nzb_file_free(nzbfile);
125
    }
126

                
127
    nzbfile = nntpgrab_utils_parse_nzb_file(nzb_contents, &errmsg);
128
    if (!nzbfile) {
129
        if (!errmsg) {
130
            errmsg = g_strdup_printf(_("Error occured while reading NZB File"));
131
        }
132

                
133
        nntpgrab_gui_base_dialog_show(windowMain, errmsg, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK);
134

                
135
        g_free(errmsg);
136

                
137
        return FALSE;
138
    }
139

                
140
    if (collection_name) {
141
        g_free(collection_name);
142
    }
143
    collection_name = g_strdup(collection_name_tmp);
144
    nntpgrab_utils_sanitize_collection_name(collection_name);
145

                
146
    quick_import = TRUE;
147

                
148
    on_btnNZBImport_clicked(NULL, NULL);
149

                
150
    return TRUE;
151
}
152

                
153
G_MODULE_EXPORT void
154
on_btnQuickImportNZB_clicked(GtkWidget *caller, gpointer data)
155
{
156
    GtkWidget *windowMain;
157
    GtkWidget *dialog;
158
    ConfigGUIOpts opts = config_gui_get_opts();
159

                
160
    windowMain = nntpgrab_gui_base_get_widget("windowMain");
161

                
162
    dialog = gtk_file_chooser_dialog_new ("Open File",
163
                        GTK_WINDOW(windowMain),
164
                        GTK_FILE_CHOOSER_ACTION_OPEN,
165
                        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
166
                        GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
167
                        NULL);
168

                
169
    add_filters_to_filechooser(GTK_FILE_CHOOSER(dialog));
170

                
171
    if (strlen(opts.last_used_nzb_directory) > 0) {
172
        gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), opts.last_used_nzb_directory);
173
    }
174

                
175
    if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
176
    {
177
        char *filename = NULL;
178
        char *uri;
179
        char *contents = NULL;
180
        char *collection_name;
181
        char *dirname;
182
        GError *err = NULL;
183
        gboolean ret;
184
#if GTK_CHECK_VERSION(2,10,0)
185
        GtkRecentManager *recent_manager;
186
#endif
187
#if GLIB_CHECK_VERSION(2,16,0)
188
        GFile *file;
189
#endif
190

                
191
        uri = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));
192
        filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
193

                
194
#if GLIB_CHECK_VERSION(2,16,0)
195
        file = g_file_new_for_uri(uri);
196
        ret = g_file_load_contents(file, NULL, &contents, NULL, NULL, &err);
197
        g_object_unref(file);
198
        if (!ret) {
199
            char *msg = g_strdup_printf("Unable to open uri '%s':\n%s", uri, err->message);
200
#else
201
        ret = g_file_get_contents(filename, &contents, NULL, &err);
202
        if (!ret) {
203
            char *msg = g_strdup_printf("Unable to open file '%s':\n%s", filename, err->message);
204
#endif
205
            nntpgrab_gui_base_dialog_show(windowMain, msg, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK);
206

                
207
            g_free(msg);
208
            g_error_free(err);
209
            g_free(uri);
210
            g_free(filename);
211
            gtk_widget_destroy (dialog);
212
            return;
213
        }
214

                
215
        collection_name = g_path_get_basename(filename);
216
        nntpgrab_utils_sanitize_text(collection_name, strlen(collection_name));
217
        nntpgrab_utils_strip_nzb_extension(collection_name);
218

                
219
        if (!nzb_perform_quick_import(collection_name, contents)) {
220
            g_free(uri);
221
            g_free(filename);
222
            g_free(contents);
223
            gtk_widget_destroy (dialog);
224
            return;
225
        }
226

                
227
#if GTK_CHECK_VERSION(2,10,0)
228
        // Add the filename to the recently used files list
229
        recent_manager = gtk_recent_manager_get_default();
230
        gtk_recent_manager_add_item(recent_manager, uri);
231
#endif
232

                
233
        dirname = g_path_get_dirname(filename);
234
        if (strlen(dirname) > 1) {
235
            ConfigGUIOpts opts = config_gui_get_opts();
236
            strncpy(opts.last_used_nzb_directory, dirname, sizeof(opts.last_used_nzb_directory) - 1);
237
            config_gui_set_opts(opts);
238
        }
239

                
240
        g_free(dirname);
241
        g_free (uri);
242
        g_free (filename);
243
    }
244

                
245
    gtk_widget_destroy (dialog);
246
}
247

                
248
static gboolean
249
nzb_model_foreach_add_func (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
250
{
251
    NNTPGrabNZBFile *file;
252
    gboolean flag;
253
    GtkWidget *windowNZB;
254
    char *errmsg = NULL;
255

                
256
    gtk_tree_model_get(model, iter, 5, &flag, 6, &file, -1);
257

                
258
    if (!flag) {
259
        return FALSE;
260
    }
261

                
262
    if (!nntpgrab_glue_schedular_add_task_to_queue(glue, collection_name, file->subject, file->poster, file->stamp, file->filesize, file->groups, file->segments, &errmsg)) {
263
        char *msg;
264

                
265
        if (errmsg) {
266
            msg = g_strdup_printf(_("File could not be added to the download queue:\n%s"), errmsg);
267
            g_free(errmsg);
268
        } else {
269
            msg = g_strdup_printf(_("File could not be added to the download queue"));
270
        }
271

                
272
        windowNZB = nntpgrab_gui_base_get_widget("windowNZB");
273
        nntpgrab_gui_base_dialog_show(windowNZB, msg, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK);
274
        g_free(msg);
275
    }
276

                
277
    return FALSE;
278
}
279

                
280
G_MODULE_EXPORT void
281
on_radioAddToNewCollection_toggled(GtkWidget *caller, gpointer data)
282
{
283
    GtkWidget *frameAddToNewCollection;
284
    GtkWidget *frameAddToExistingCollection;
285

                
286
    frameAddToNewCollection = nntpgrab_gui_base_get_widget("frameAddToNewCollection");
287
    frameAddToExistingCollection = nntpgrab_gui_base_get_widget("frameAddToExistingCollection");
288

                
289
    gtk_widget_set_sensitive(frameAddToNewCollection, TRUE);
290
    gtk_widget_set_sensitive(frameAddToExistingCollection, FALSE);
291
}
292

                
293
G_MODULE_EXPORT void
294
on_radioAddToExistingCollection_toggled(GtkWidget *caller, gpointer data)
295
{
296
    GtkWidget *frameAddToNewCollection;
297
    GtkWidget *frameAddToExistingCollection;
298

                
299
    frameAddToNewCollection = nntpgrab_gui_base_get_widget("frameAddToNewCollection");
300
    frameAddToExistingCollection = nntpgrab_gui_base_get_widget("frameAddToExistingCollection");
301

                
302
    gtk_widget_set_sensitive(frameAddToNewCollection, FALSE);
303
    gtk_widget_set_sensitive(frameAddToExistingCollection, TRUE);
304
}
305

                
306
G_MODULE_EXPORT void
307
on_btnSetCollectionNameCancel_clicked(GtkWidget *caller, gpointer data)
308
{
309
    gtk_widget_hide(nntpgrab_gui_base_get_widget("windowSetCollectionName"));
310
}
311

                
312
G_MODULE_EXPORT void
313
on_btnSetCollectionNameOK_clicked(GtkWidget *caller, gpointer data)
314
{
315
    GtkWidget *windowNZB;
316
    GtkWidget *treeNZBFiles;
317
    GtkWidget *radioAddToNewCollection;
318
    GtkWidget *entryCollectionName;
319
    GtkWidget *comboCollectionName;
320
    GtkTreeModel *store;
321
    char *errmsg = NULL;
322

                
323
    radioAddToNewCollection = nntpgrab_gui_base_get_widget("radioAddToNewCollection");
324
    entryCollectionName = nntpgrab_gui_base_get_widget("entryCollectionName");
325
    comboCollectionName = nntpgrab_gui_base_get_widget("comboCollectionName");
326

                
327
    // Set the right collection name
328
    g_free(collection_name);
329
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radioAddToNewCollection))) {
330
        collection_name = g_strdup(gtk_entry_get_text(GTK_ENTRY(entryCollectionName)));
331
    } else {
332
        GtkTreeIter iter;
333

                
334
        if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(comboCollectionName), &iter)) {
335
            // No item selected, use the entry field
336
            collection_name = g_strdup(gtk_entry_get_text(GTK_ENTRY(entryCollectionName)));
337
            nntpgrab_utils_sanitize_text(collection_name, strlen(collection_name));
338
        } else {
339
            store = gtk_combo_box_get_model(GTK_COMBO_BOX(comboCollectionName));
340
            gtk_tree_model_get(store, &iter, 0, &collection_name, -1);
341
        }
342
    }
343

                
344
    nntpgrab_utils_sanitize_collection_name(collection_name);
345

                
346
    if (quick_import) {
347
        errmsg = NULL;
348
        auto_import_import_nzb_file(glue, nzbfile, collection_name, &errmsg);
349
        if (errmsg) {
350
            GtkWidget *windowMain = nntpgrab_gui_base_get_widget("windowMain");
351

                
352
            nntpgrab_gui_base_dialog_show(windowMain, errmsg, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK);
353

                
354
            g_free(errmsg);
355
        }
356

                
357
        g_free(collection_name);
358
        collection_name = NULL;
359

                
360
        nntpgrab_utils_nzb_file_free(nzbfile);
361
        nzbfile = NULL;
362
    } else {
363
        treeNZBFiles = nntpgrab_gui_base_get_widget("treeNZBFiles");
364
        store = gtk_tree_view_get_model(GTK_TREE_VIEW(treeNZBFiles));
365

                
366
        gtk_tree_model_foreach(store, nzb_model_foreach_add_func, GINT_TO_POINTER(TRUE));
367
    }
368

                
369
    windowNZB = nntpgrab_gui_base_get_widget("windowNZB");
370

                
371
    if (!nntpgrab_glue_schedular_save_queue(glue, &errmsg)) {
372
        char *msg;
373
        if (errmsg) {
374
            msg = g_strdup_printf(_("Download queue could not be saved:\n%s"), errmsg);
375
            g_free(errmsg);
376
        } else {
377
            msg = g_strdup_printf(_("Download queue could not be saved"));
378
        }
379

                
380
        nntpgrab_gui_base_dialog_show(windowNZB, msg, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK);
381
        g_free(msg);
382
    }
383

                
384
    gtk_widget_hide(windowNZB);
385

                
386
    if (filechooserNZB) {
387
        gtk_widget_destroy(filechooserNZB);
388
        filechooserNZB = NULL;
389
    }
390

                
391
    if (nzbfile) {
392
        nntpgrab_utils_nzb_file_free(nzbfile);
393
        nzbfile = NULL;
394
    }
395

                
396
    if (collection_name) {
397
        g_free(collection_name);
398
        collection_name = NULL;
399
    }
400

                
401
    gtk_widget_hide(nntpgrab_gui_base_get_widget("windowSetCollectionName"));
402
}
403

                
404
static void
405
generate_stamp(time_t stamp, char *buf, int buf_len)
406
{
407
    struct tm *now;
408

                
409
    if ((now = localtime(&stamp)) == NULL) {
410
        // Date could not be parted
411
        buf[0] = '\0';
412
    }
413

                
414
    strftime(buf, buf_len, "%c", now);
415
}
416

                
417
// Callbacks from the NZB Import window
418
G_MODULE_EXPORT void
419
on_filechooserNZB_file_set(GtkWidget *caller, gpointer data)
420
{
421
    char *filename = NULL;
422
    char *errmsg = NULL;
423
    GtkWidget *windowNZB;
424
    GtkWidget *btnNZBImport;
425
    GtkWidget *treeNZBFiles;
426
    GtkTreeModel *store;
427
    NGList *list;
428
    GError *err = NULL;
429
    char *contents = NULL;
430
    char *uri = NULL;
431
    char *dirname;
432
#if GLIB_CHECK_VERSION(2,16,0)
433
    gboolean ret;
434
    GFile *file;
435
#endif
436
    time_t now = time(NULL);
437
#if GTK_CHECK_VERSION(2,10,0)
438
    GtkRecentManager *recent_manager;
439
#endif
440

                
441
    windowNZB = nntpgrab_gui_base_get_widget("windowNZB");
442
    btnNZBImport = nntpgrab_gui_base_get_widget("btnNZBImport");
443

                
444
    if (nzbfile) {
445
        nntpgrab_utils_nzb_file_free(nzbfile);
446
        nzbfile = NULL;
447
    }
448

                
449
    // Sometimes, the two functions here below can return NULL
450
    // See https://bugzilla.gnome.org/show_bug.cgi?id=538458 for details
451
    do {
452
        uri = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(caller));
453
        filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(caller));
454

                
455
        // Workaround for https://bugzilla.gnome.org/show_bug.cgi?id=355623
456
        if (GINT_TO_POINTER(data) == 0 && uri == NULL && filename == NULL) {
457
            return;
458
        }
459

                
460
        if (uri && filename) {
461
            break;
462
        }
463

                
464
        // Only zero or one of the two fields were found, wait a bit and try again
465
        if (uri) {
466
            g_free(uri);
467
        }
468

                
469
        if (filename) {
470
            g_free(filename);
471
        }
472

                
473
        while (g_main_context_iteration(NULL, FALSE));
474

                
475
        g_usleep(G_USEC_PER_SEC / 10);
476

                
477
        // If the filename is still not available after 10 seconds, timeout
478
        if (now + 10 < time(NULL)) {
479
            g_return_if_reached();
480
        }
481
    } while (TRUE);
482

                
483
    g_return_if_fail(uri != NULL);
484
    g_return_if_fail(filename != NULL);
485

                
486
#if GLIB_CHECK_VERSION(2,16,0)
487
    file = g_file_new_for_uri(uri);
488
    if (!file) {
489
        return;
490
    }
491
    ret = g_file_load_contents(file, NULL, &contents, NULL, NULL, &err);
492
    g_object_unref(file);
493
    if (!ret) {
494
        char *msg = g_strdup_printf("Unable to open uri '%s':\n%s", uri, err->message);
495
#else
496
    if (!g_file_get_contents(filename, &contents, NULL, &err)) {
497
        char *msg = g_strdup_printf("Unable to open file '%s':\n%s", filename, err->message);
498
#endif /* GLIB_CHECK_VERSION(2,16,0) */
499
        GtkWidget *windowMain = nntpgrab_gui_base_get_widget("windowMain");
500

                
501
        nntpgrab_gui_base_dialog_show(windowMain, msg, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK);
502

                
503
        g_free(msg);
504
        g_error_free(err);
505
        g_free(filename);
506
        g_free(uri);
507

                
508
        return;
509
    }
510

                
511
    nzbfile = nntpgrab_utils_parse_nzb_file(contents, &errmsg);
512
    g_free(contents);
513
    if (!nzbfile) {
514
        if (!errmsg) {
515
            errmsg = g_strdup_printf(_("Error occured while reading NZB File '%s'"), filename);
516
        }
517

                
518
        nntpgrab_gui_base_dialog_show(windowNZB, errmsg, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK);
519

                
520
        g_free(errmsg);
521
        g_free(filename);
522
        g_free(uri);
523

                
524
        return;
525
    }
526

                
527
#if GTK_CHECK_VERSION(2,10,0)
528
    // Add the filename to the recently used files list
529
    recent_manager = gtk_recent_manager_get_default();
530
    gtk_recent_manager_add_item(recent_manager, uri);
531
#endif
532

                
533
    collection_name = g_path_get_basename(filename);
534
    nntpgrab_utils_sanitize_text(collection_name, strlen(collection_name));
535
    nntpgrab_utils_strip_nzb_extension(collection_name);
536

                
537
    dirname = g_path_get_dirname(filename);
538
    if (strlen(dirname) > 1) {
539
        ConfigGUIOpts opts = config_gui_get_opts();
540
        strncpy(opts.last_used_nzb_directory, dirname, sizeof(opts.last_used_nzb_directory) - 1);
541
        config_gui_set_opts(opts);
542
    }
543
    g_free(dirname);
544
    g_free(filename);
545
    g_free(uri);
546

                
547
    treeNZBFiles = nntpgrab_gui_base_get_widget("treeNZBFiles");
548
    store = gtk_tree_view_get_model(GTK_TREE_VIEW(treeNZBFiles));
549

                
550
    gtk_list_store_clear(GTK_LIST_STORE(store));
551

                
552
    list = nzbfile->files;
553
    while (list) {
554
        NNTPGrabNZBFile *file = list->data;
555
        GtkTreeIter iter;
556
        char stamp_str[64];
557
        char filesize_str[64];
558

                
559
        gtk_list_store_append(GTK_LIST_STORE(store), &iter);
560

                
561
        memset(&stamp_str, 0, sizeof(stamp_str));
562
        generate_stamp(file->stamp, stamp_str, sizeof(stamp_str) - 1);
563

                
564
        memset(&filesize_str, 0, sizeof(filesize_str));
565
        nntpgrab_utils_calculate_file_size(file->filesize, filesize_str, sizeof(filesize_str) - 1);
566

                
567
        gtk_list_store_set(GTK_LIST_STORE(store), &iter, 0, file->subject, 1, file->poster, 2, (file->groups == NULL ? "" : file->groups->data), 3, stamp_str, 4, filesize_str, 5, TRUE, 6, file, -1);
568

                
569
        list = ng_list_next(list);
570
    }
571

                
572
    gtk_widget_set_sensitive(btnNZBImport, TRUE);
573
}
574

                
575
static gboolean
576
nzb_model_foreach_select_func (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
577
{
578
    gtk_list_store_set(GTK_LIST_STORE(model), iter, 5, GPOINTER_TO_INT(data), -1);
579

                
580
    return FALSE;
581
}
582

                
583
G_MODULE_EXPORT void
584
on_btnNZBSelectNone_clicked(GtkWidget *caller, gpointer data)
585
{
586
    GtkWidget *treeNZBFiles;
587
    GtkTreeModel *store;
588

                
589
    treeNZBFiles = nntpgrab_gui_base_get_widget("treeNZBFiles");
590
    store = gtk_tree_view_get_model(GTK_TREE_VIEW(treeNZBFiles));
591

                
592
    gtk_tree_model_foreach(store, nzb_model_foreach_select_func, GINT_TO_POINTER(FALSE));
593
}
594

                
595
G_MODULE_EXPORT void
596
on_btnNZBSelectAll_clicked(GtkWidget *caller, gpointer data)
597
{
598
    GtkWidget *treeNZBFiles;
599
    GtkTreeModel *store;
600

                
601
    treeNZBFiles = nntpgrab_gui_base_get_widget("treeNZBFiles");
602
    store = gtk_tree_view_get_model(GTK_TREE_VIEW(treeNZBFiles));
603

                
604
    gtk_tree_model_foreach(store, nzb_model_foreach_select_func, GINT_TO_POINTER(TRUE));
605
}
606

                
607
static void
608
foreach_collection_func(const char *collection_name, const char *poster, nguint64 total_size, nguint64 total_size_remaining, int position, gpointer data)
609
{
610
    GtkTreeIter iter;
611

                
612
    gtk_list_store_append(GTK_LIST_STORE(data), &iter);
613
    gtk_list_store_set(GTK_LIST_STORE(data), &iter, 0, collection_name, -1);
614
}
615

                
616
G_MODULE_EXPORT void
617
on_btnNZBImport_clicked(GtkWidget *caller, gpointer data)
618
{
619
    GtkWidget *windowSetCollectionName;
620
    GtkWidget *windowMain;
621
    GtkWidget *frameAddToNewCollection;
622
    GtkWidget *entryCollectionName;
623
    GtkWidget *frameAddToExistingCollection;
624
    GtkWidget *comboCollectionName;
625
    GtkWidget *radioAddToNewCollection;
626
    GtkWidget *radioAddToExistingCollection;
627
    GtkTreeModel *store;
628

                
629
    windowSetCollectionName = nntpgrab_gui_base_get_widget("windowSetCollectionName");
630
    windowMain = nntpgrab_gui_base_get_widget("windowMain");
631
    frameAddToNewCollection = nntpgrab_gui_base_get_widget("frameAddToNewCollection");
632
    entryCollectionName = nntpgrab_gui_base_get_widget("entryCollectionName");
633
    frameAddToExistingCollection = nntpgrab_gui_base_get_widget("frameAddToExistingCollection");
634
    comboCollectionName = nntpgrab_gui_base_get_widget("comboCollectionName");
635
    radioAddToNewCollection = nntpgrab_gui_base_get_widget("radioAddToNewCollection");
636
    radioAddToExistingCollection = nntpgrab_gui_base_get_widget("radioAddToExistingCollection");
637

                
638
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radioAddToNewCollection), TRUE);
639
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radioAddToExistingCollection), FALSE);
640

                
641
    gtk_widget_set_sensitive(frameAddToNewCollection, TRUE);
642
    gtk_widget_set_sensitive(frameAddToExistingCollection, FALSE);
643
    gtk_entry_set_text(GTK_ENTRY(entryCollectionName), collection_name);
644

                
645
    // Clear the contents of the combo box
646
    store = gtk_combo_box_get_model(GTK_COMBO_BOX(comboCollectionName));
647
    gtk_list_store_clear(GTK_LIST_STORE(store));
648

                
649
    nntpgrab_glue_schedular_foreach_task(glue, foreach_collection_func, NULL, NULL, store);
650
    gtk_combo_box_set_active (GTK_COMBO_BOX (comboCollectionName), 0);
651

                
652
    // FIXME: Due to a possible bug in GTK it isn't possible to set another transient window
653
    // when one is already attached
654
// if (quick_import) {
655
        gtk_window_set_transient_for(GTK_WINDOW(windowSetCollectionName), GTK_WINDOW(windowMain));
656
// } else {
657
// gtk_window_set_transient_for(GTK_WINDOW(windowSetCollectionName), GTK_WINDOW(windowNZB));
658
// }
659

                
660
    gtk_widget_show(windowSetCollectionName);
661
    gtk_window_present(GTK_WINDOW(windowSetCollectionName));
662
}
663

                
664
G_MODULE_EXPORT gboolean
665
on_windowNZB_delete_event(GtkWidget *widget, GdkEvent *event, gpointer user_data)
666
{
667
    gtk_widget_destroy(filechooserNZB);
668
    filechooserNZB = NULL;
669

                
670
    gtk_widget_hide(widget);
671

                
672
    return TRUE;
673
}
674

                
675
G_MODULE_EXPORT gboolean
676
on_windowSetCollectionName_delete_event(GtkWidget *widget, GdkEvent *event, gpointer user_data)
677
{
678
    gtk_widget_hide(widget);
679

                
680
    if (nzbfile) {
681
        nntpgrab_utils_nzb_file_free(nzbfile);
682
        nzbfile = NULL;
683
    }
684

                
685
    if (collection_name) {
686
        g_free(collection_name);
687
        collection_name = NULL;
688
    }
689

                
690
    return TRUE;
691
}
692

                
693
static void
694
download_check_toggled(GtkWidget *caller, gchar *path, gpointer data)
695
{
696
    gboolean value;
697
    GtkTreeIter iter;
698
    GtkWidget *treeNZBFiles;
699
    GtkTreeModel *model;
700

                
701
    treeNZBFiles = nntpgrab_gui_base_get_widget("treeNZBFiles");
702
    model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeNZBFiles));
703

                
704
    if (gtk_tree_model_get_iter_from_string(model, &iter, path)) {
705
        gtk_tree_model_get(model, &iter, 5, &value, -1);
706
        if (value) {
707
            value = FALSE;
708
        } else {
709
            value = TRUE;
710
        }
711
        gtk_list_store_set(GTK_LIST_STORE(model), &iter, 5, value, -1);
712
    }
713
}
714

                
715
void
716
nzb_initialize()
717
{
718
    GtkWidget *treeNZBFiles;
719
    GtkTreeSelection *selection;
720
    GtkListStore *store;
721
    GtkCellRenderer *cellRenderer;
722
    GtkWidget *comboCollectionName;
723

                
724
    treeNZBFiles = nntpgrab_gui_base_get_widget("treeNZBFiles");
725
    comboCollectionName = nntpgrab_gui_base_get_widget("comboCollectionName");
726

                
727
    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeNZBFiles));
728
    gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
729

                
730
    cellRenderer = gtk_cell_renderer_toggle_new();
731
    gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeNZBFiles), -1, _("Download"), cellRenderer, "active", 5, NULL);
732
    g_signal_connect((gpointer) cellRenderer, "toggled", G_CALLBACK(download_check_toggled), NULL);
733

                
734
    cellRenderer = gtk_cell_renderer_text_new();
735
    gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeNZBFiles), -1, _("Subject"), cellRenderer, "text", 0, NULL);
736

                
737
    cellRenderer = gtk_cell_renderer_text_new();
738
    gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeNZBFiles), -1, _("Poster"), cellRenderer, "text", 1, NULL);
739

                
740
    cellRenderer = gtk_cell_renderer_text_new();
741
    gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeNZBFiles), -1, _("Groups"), cellRenderer, "text", 2, NULL);
742

                
743
    cellRenderer = gtk_cell_renderer_text_new();
744
    gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeNZBFiles), -1, _("Stamp"), cellRenderer, "text", 3, NULL);
745

                
746
    cellRenderer = gtk_cell_renderer_text_new();
747
    gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeNZBFiles), -1, _("File size"), cellRenderer, "text", 4, NULL);
748

                
749
    store = gtk_list_store_new(7, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_POINTER);
750

                
751
    gtk_tree_view_set_model(GTK_TREE_VIEW(treeNZBFiles), GTK_TREE_MODEL(store));
752

                
753
    cellRenderer = gtk_cell_renderer_text_new();
754
    gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (comboCollectionName), cellRenderer, FALSE);
755
    gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (comboCollectionName), cellRenderer, "text", 0, NULL);
756

                
757
    store = gtk_list_store_new(1, G_TYPE_STRING);
758
    gtk_combo_box_set_model(GTK_COMBO_BOX(comboCollectionName), GTK_TREE_MODEL(store));
759
}
760

                
761
static gboolean
762
do_open_nzb_file_phase3(gpointer data)
763
{
764
    GtkWidget *btnImportNZB = nntpgrab_gui_base_get_widget("btnImportNZB");
765

                
766
    on_btnImportNZB_clicked(btnImportNZB, NULL);
767
    gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(filechooserNZB), (const char*) data);
768
    on_filechooserNZB_file_set(GTK_WIDGET(filechooserNZB), g_strdup((const char*) data));
769

                
770
    g_free(data);
771

                
772
    return FALSE;
773
}
774

                
775
static gboolean
776
do_open_nzb_file_phase2(gpointer data)
777
{
778
    gtk_window_deiconify(GTK_WINDOW(nntpgrab_gui_base_get_widget("windowMain")));
779
    gtk_window_present(GTK_WINDOW(nntpgrab_gui_base_get_widget("windowMain")));
780

                
781
    g_idle_add(do_open_nzb_file_phase3, data);
782

                
783
    return FALSE;
784
}
785

                
786
void
787
do_open_nzb_file(const char *nzb_file)
788
{
789
    g_idle_add(do_open_nzb_file_phase2, g_strdup(nzb_file));
790
}