Statistics
| Revision:

root / trunk / client / gui / main.c @ 1919

History | View | Annotate | Download (22.6 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 
21
#include 
22
#include 
23
#include "gui.h"
24
#include "config_gui.h"
25
#include "donate.xpm"
26

                
27
#ifdef HAVE_LIBPEAS
28
#include 
29
#endif
30

                
31
#ifdef DARWIN
32
#include 
33
#endif
34

                
35
#ifdef WIN32
36
#include 
37
#endif
38

                
39
NntpgrabGlue *glue = NULL;
40

                
41
#if GTK_CHECK_VERSION(3,0,0)
42
static GtkApplication *app = NULL;
43
#endif
44

                
45
void debug_initialize(void);
46
void info_initialize(void);
47
void config_initialize(void);
48
void config_gui_initialize(void);
49
void config_display_initialize(void);
50
void queue_initialize(void);
51
void callbacks_initialize(void);
52
void nzb_initialize(void);
53
void connections_initialize(void);
54
void notify_initialize(void);
55
void monitor_initialize(void);
56
void gui_par2_initialize(void);
57
void nzbcreator_initialize(void);
58
void auto_import_initialize(void);
59
void autoshutdown_initialize(void);
60
void gui_plugins_initialize(void);
61
void gui_plugins_cleanup(void);
62

                
63
// auto_import.c
64
void    disable_auto_import(void);
65

                
66
static const char *nzb_file = NULL;
67
static gboolean initialized = FALSE;
68

                
69
G_MODULE_EXPORT void
70
on_btnQuit_clicked(GtkWidget *caller, gpointer data)
71
{
72
    GtkWidget *windowMain = nntpgrab_gui_base_get_widget("windowMain");
73

                
74
    gtk_widget_hide(windowMain);
75

                
76
#if GTK_CHECK_VERSION(3,0,0)
77
    gtk_application_remove_window(app, GTK_WINDOW(windowMain));
78
#else
79
    gtk_main_quit();
80
#endif
81
}
82

                
83
static void
84
load_auto_load_plugins(void)
85
{
86
    ConfigGUIOpts opts = config_gui_get_opts();
87
    NNTPGrabPluginInfo info;
88
    GList *list;
89

                
90
    list = opts.auto_load_plugins;
91
    while (list) {
92
        char *errmsg = NULL;
93
        const char *plugin_name = list->data;
94

                
95
        if (!nntpgrab_glue_plugins_get_plugin_info(glue, plugin_name, &info) ||
96
            info.is_loaded == TRUE) {
97

                
98
            /* Plugin isn't found or is already loaded. Ignore */
99
            list = g_list_next(list);
100
            continue;
101
        }
102

                
103
        if (!nntpgrab_glue_plugins_load_plugin(glue, plugin_name, &errmsg)) {
104
            GtkWidget *windowMain = nntpgrab_gui_base_get_widget("windowMain");
105
            char msg[1024];
106

                
107
            memset(&msg, 0, sizeof(msg));
108
            snprintf(msg, sizeof(msg) - 1, _("Unable to load plugin '%s': %s"), plugin_name, errmsg);
109
            nntpgrab_gui_base_dialog_show(windowMain, msg, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK);
110
            g_free(errmsg);
111
        }
112

                
113
        list = g_list_next(list);
114
    }
115
}
116

                
117
static void
118
bring_to_front_cb(void *user_data)
119
{
120
    GtkWidget *windowMain = nntpgrab_gui_base_get_widget("windowMain");
121

                
122
    gtk_window_deiconify(GTK_WINDOW(windowMain));
123
    gtk_widget_show(windowMain);
124
    gtk_window_present(GTK_WINDOW(windowMain));
125
}
126

                
127
static gboolean
128
register_frontend(void)
129
{
130
    char *errmsg = NULL;
131

                
132
    /* Try to register this NNTPGrab instance */
133
    if (!nntpgrab_utils_register_frontend(do_open_nzb_file, bring_to_front_cb, NULL, &errmsg)) {
134
        /* Find out whether there's already another NNTPGrab frontend active */
135
        ngboolean is_running = FALSE;
136
        char *errmsg2 = NULL;
137
        if (nntpgrab_utils_test_is_frontend_already_running(&is_running, &errmsg2)) {
138
            if (is_running) {
139
                /* Another NNTPGrab frontend is already running, close this instance */
140
                return FALSE;
141
            } else {
142
                /* No NNTPGrab was already running, show the error and continue without IPC support */
143
                GtkWidget *windowMain = nntpgrab_gui_base_get_widget("windowMain");
144
                nntpgrab_gui_base_dialog_show(windowMain, errmsg, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK);
145
            }
146
        } else {
147
            /* The check failed, show the error and continue without IPC support */
148
            GtkWidget *windowMain = nntpgrab_gui_base_get_widget("windowMain");
149
            nntpgrab_gui_base_dialog_show(windowMain, errmsg2, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK);
150
            g_free(errmsg2);
151
        }
152
    }
153

                
154
    return TRUE;
155
}
156

                
157
static gboolean
158
initialisation_inside_mainloop(gpointer data)
159
{
160
    GtkWidget *windowMain = nntpgrab_gui_base_get_widget("windowMain");
161
    GtkWidget *windowConnect = nntpgrab_gui_base_get_widget("windowConnect");
162
    char *errmsg = NULL;
163
    GtkWidget *cmbConnectHostname;
164
    GtkWidget *spinConnectPort;
165
    char *hostname;
166
    int port;
167
    char *warnings = NULL;
168
#if GTK_CHECK_VERSION(2,90,0)
169
    GtkTreeIter iter;
170
    GtkTreeModel *model;
171
#endif
172

                
173
    gtk_widget_show(windowMain);
174

                
175
#if GTK_CHECK_VERSION(3,0,0)
176
    gtk_application_add_window(app, GTK_WINDOW(windowMain));
177
    gtk_application_remove_window(app, GTK_WINDOW(windowConnect));
178
#endif
179

                
180
    cmbConnectHostname = nntpgrab_gui_base_get_widget("cmbConnectHostname");
181
    spinConnectPort = nntpgrab_gui_base_get_widget("spinConnectPort");
182
    hostname = g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(cmbConnectHostname)))));
183
    port = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spinConnectPort));
184

                
185
    glue = nntpgrab_glue_new();
186

                
187
    if (!nntpgrab_glue_init(glue, NNTPGRAB_GLUE_VERSION, &errmsg)) {
188
        nntpgrab_glue_destroy(glue);
189
        glue = NULL;
190
        g_free(hostname);
191
        g_critical(_("NNTPGrab Initialisation FAILED:\n%s\n"), errmsg);
192
        g_free(errmsg);
193
        g_idle_add((GSourceFunc) gtk_main_quit, NULL);
194
        return FALSE;
195
    }
196

                
197
    if (GPOINTER_TO_INT(data) == 0) {       // Standalone mode
198
        GObject *menuitemKillServer = nntpgrab_gui_base_get_object("menuitemKillServer");
199
        GValue val;
200
        memset(&val, 0, sizeof(val));
201
        g_value_init(&val, G_TYPE_BOOLEAN);
202
        g_value_set_boolean (&val, FALSE);
203
        g_object_set_property(G_OBJECT(menuitemKillServer), "sensitive", &val);
204

                
205
        if (hostname) {
206
            g_free(hostname);
207
            hostname = NULL;
208
        }
209
    }
210

                
211
    if (!nntpgrab_glue_connect(glue, hostname, port, NULL, NULL, FALSE, &errmsg, &warnings)) {
212
        if (warnings) {
213
            GtkWidget *windowMain = nntpgrab_gui_base_get_widget("windowMain");
214
            nntpgrab_gui_base_dialog_show(windowMain, warnings, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK);
215
            g_free(warnings);
216
        }
217

                
218
        g_free(hostname);
219
        g_critical(_("Unable to connect to NNTPGrab server:\n%s"), errmsg);
220
        g_free(errmsg);
221
        g_idle_add((GSourceFunc) gtk_main_quit, NULL);
222
        return FALSE;
223
    }
224

                
225
    if (warnings) {
226
        GtkWidget *windowMain = nntpgrab_gui_base_get_widget("windowMain");
227
        nntpgrab_gui_base_dialog_show(windowMain, warnings, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK);
228
        g_free(warnings);
229
    }
230

                
231
    g_free(hostname);
232

                
233
    gtk_widget_destroy(windowConnect);
234

                
235
#if !defined(DARWIN)
236
    if (!register_frontend()) {
237
        /* Another NNTPGrab frontend is already running, close this instance */
238
        g_idle_add((GSourceFunc) gtk_main_quit, NULL);
239
        return FALSE;
240
    }
241
#endif
242

                
243
    debug_initialize();
244
    callbacks_initialize();
245
    config_initialize();
246
    config_display_initialize();
247
#ifndef DARWIN
248
    nzb_initialize();
249
#endif
250
    connections_initialize();
251
    queue_initialize();
252
    notify_initialize();
253
    monitor_initialize();
254
    gui_par2_initialize();
255
    nzbcreator_initialize();
256
    auto_import_initialize();
257
    autoshutdown_initialize();
258
    gui_plugins_initialize();
259

                
260
    /* Load any optional plugins which need to be auto-loaded */
261
    load_auto_load_plugins();
262

                
263
    // Did we receive a NZB file on the command line ?
264
    if (nzb_file) {
265
        do_open_nzb_file(nzb_file, NULL);
266
        nzb_file = NULL;
267
    }
268

                
269
    initialized = TRUE;
270

                
271
    return FALSE;
272
}
273

                
274
G_MODULE_EXPORT void
275
on_btnConnectToServer_clicked(GtkWidget *caller, gpointer data)
276
{
277
    GtkWidget *windowConnect = nntpgrab_gui_base_get_widget("windowConnect");
278

                
279
    // Save the hostname into the GUI Configuration
280
    ConfigGUIOpts opts = config_gui_get_opts();
281
    GtkWidget *cmbConnectHostname = nntpgrab_gui_base_get_widget("cmbConnectHostname");
282
    const char *hostname = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(cmbConnectHostname))));
283

                
284
    // Have we seen this hostname before ?
285
    if (!strcmp(hostname, opts.hostname1)) {
286
        // No need to do anything, item is already at the top of the list
287
    } else if (!strcmp(hostname, opts.hostname2)) {
288
        // Swap positions
289
        strncpy(opts.hostname2, opts.hostname1, sizeof(opts.hostname2));
290
        strncpy(opts.hostname1, hostname, sizeof(opts.hostname1));
291
    } else if (!strcmp(hostname, opts.hostname3)) {
292
        // Push items 1 and 2 one place down and set the first item to the hostname
293
        strncpy(opts.hostname3, opts.hostname2, sizeof(opts.hostname3));
294
        strncpy(opts.hostname2, opts.hostname1, sizeof(opts.hostname2));
295
        strncpy(opts.hostname1, hostname, sizeof(opts.hostname1));
296
    } else if (!strcmp(hostname, opts.hostname4)) {
297
        strncpy(opts.hostname4, opts.hostname3, sizeof(opts.hostname4));
298
        strncpy(opts.hostname3, opts.hostname2, sizeof(opts.hostname3));
299
        strncpy(opts.hostname2, opts.hostname1, sizeof(opts.hostname2));
300
        strncpy(opts.hostname1, hostname, sizeof(opts.hostname1));
301
    } else if (!strcmp(hostname, opts.hostname5)) {
302
        strncpy(opts.hostname5, opts.hostname4, sizeof(opts.hostname5));
303
        strncpy(opts.hostname4, opts.hostname3, sizeof(opts.hostname4));
304
        strncpy(opts.hostname3, opts.hostname2, sizeof(opts.hostname3));
305
        strncpy(opts.hostname2, opts.hostname1, sizeof(opts.hostname2));
306
        strncpy(opts.hostname1, hostname, sizeof(opts.hostname1));
307
    } else {
308
        // Hostname not seen at all, move all the items one place down (forget
309
        // about the last item) and put the hostname in front
310
        strncpy(opts.hostname5, opts.hostname4, sizeof(opts.hostname5));
311
        strncpy(opts.hostname4, opts.hostname3, sizeof(opts.hostname4));
312
        strncpy(opts.hostname3, opts.hostname2, sizeof(opts.hostname3));
313
        strncpy(opts.hostname2, opts.hostname1, sizeof(opts.hostname2));
314
        strncpy(opts.hostname1, hostname, sizeof(opts.hostname1));
315
    }
316

                
317
    config_gui_set_opts(opts);
318

                
319
    gtk_widget_set_sensitive(windowConnect, FALSE);
320
    g_idle_add(initialisation_inside_mainloop, GINT_TO_POINTER(1));
321
}
322

                
323
G_MODULE_EXPORT void
324
on_btnStartStandalone_clicked(GtkWidget *caller, gpointer data)
325
{
326
    GtkWidget *windowConnect = nntpgrab_gui_base_get_widget("windowConnect");
327

                
328
    gtk_widget_set_sensitive(windowConnect, FALSE);
329
    g_idle_add(initialisation_inside_mainloop, GINT_TO_POINTER(0));
330
}
331

                
332
G_MODULE_EXPORT void
333
on_btnConnectQuit_clicked(GtkWidget *caller, gpointer data)
334
{
335
    GtkWidget *windowConnect = nntpgrab_gui_base_get_widget("windowConnect");
336

                
337
    gtk_widget_destroy(windowConnect);
338

                
339
    gtk_main_quit();
340
}
341

                
342
G_MODULE_EXPORT void
343
on_menuitemManagePlugins_activate(GtkWidget *caller, gpointer data)
344
{
345
    GtkWidget *windowPlugins = nntpgrab_gui_base_get_widget("windowPlugins");
346

                
347
    gtk_window_present(GTK_WINDOW(windowPlugins));
348
}
349

                
350
G_MODULE_EXPORT void
351
on_menuitemKillServer_activate(GtkWidget *caller, gpointer data)
352
{
353
    nntpgrab_glue_kill_server(glue);
354
}
355

                
356
gboolean
357
gui_get_is_initialized()
358
{
359
    return initialized;
360
}
361

                
362
static GtkWidget *
363
create_user_interface(void)
364
{
365
    GtkWidget *btnDonate;
366
    GtkWidget *icon;
367
    GdkPixbuf *pixbuf;
368
    GtkWidget *windowMain;
369
    GtkWidget *windowConnect;
370
    GtkWidget *cmbConnectHostname;
371
    GtkListStore *store;
372
    ConfigGUIOpts opts;
373
    GtkTreeIter iter;
374
#ifdef DARWIN
375
    GtkWidget *quit_menu_widget;
376
#endif
377

                
378
    /* Initialize libxml now to prevent possible race conditions */
379
    xmlInitGlobals();
380
    xmlInitParser();
381

                
382
    nntpgrab_gui_base_create_ui("nntpgrab_gui");
383

                
384
    windowMain = nntpgrab_gui_base_get_widget("windowMain");
385
    windowConnect = nntpgrab_gui_base_get_widget("windowConnect");
386

                
387
#ifdef DARWIN
388
    ige_mac_menu_set_global_key_handler_enabled (TRUE);
389
    ige_mac_menu_connect_window_key_handler (GTK_WINDOW(nntpgrab_gui_base_get_widget("windowMain")));
390

                
391
    quit_menu_widget = gtk_ui_manager_get_widget(GTK_UI_MANAGER(nntpgrab_gui_base_get_object("uimanager1")), "ui/menubar1/menuitem1/imagemenuitem5");
392
    g_warn_if_fail(quit_menu_widget != NULL);
393

                
394
    ige_mac_menu_set_quit_menu_item (GTK_MENU_ITEM (quit_menu_widget));
395

                
396
    ige_mac_menu_set_menu_bar(GTK_MENU_SHELL(nntpgrab_gui_base_get_widget("menubar1")));
397
    gtk_widget_hide(nntpgrab_gui_base_get_widget("menubar1"));
398
#endif
399

                
400
    // Set the donate icon of the window
401
    btnDonate = nntpgrab_gui_base_get_widget("btnDonate");
402
    pixbuf = gdk_pixbuf_new_from_xpm_data((const char **) &donate_xpm);
403
    icon = gtk_image_new_from_pixbuf(pixbuf);
404
    gtk_widget_show(icon);
405
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(btnDonate), icon);
406
    g_object_unref(pixbuf);
407

                
408
#ifdef DARWIN
409
    /* Due to the way file associations work on Mac OS X, we need to make 
410
     * sure the NZB dialog can be used before the file association is set up */
411
    nzb_initialize();
412

                
413
    /* And the file assocation needs to be set up as soon as possible.. */
414
    if (!register_frontend()) {
415
        return NULL;
416
    }
417
#endif
418

                
419
    // We need to have the GUI configuration loaded at this point because the
420
    // Network GUI requires data from it
421
    config_gui_initialize();
422

                
423
    opts = config_gui_get_opts();
424

                
425
    if (opts.show_connect_dialog || nntpgrab_utils_test_is_server_already_running()) {
426
        // Show the connect dialog so the user can enter where the NNTPGrab server is running
427
        windowConnect = nntpgrab_gui_base_get_widget("windowConnect");
428
        cmbConnectHostname = nntpgrab_gui_base_get_widget("cmbConnectHostname");
429

                
430
        gtk_widget_show(windowConnect);
431

                
432
        store = gtk_list_store_new(1, G_TYPE_STRING);
433
        gtk_combo_box_set_model(GTK_COMBO_BOX(cmbConnectHostname), GTK_TREE_MODEL(store));
434
#if GTK_CHECK_VERSION(3,0,0)
435
        gtk_combo_box_set_entry_text_column(GTK_COMBO_BOX(cmbConnectHostname), 0);
436
#else
437
        gtk_combo_box_entry_set_text_column(GTK_COMBO_BOX_ENTRY(cmbConnectHostname), 0);
438
#endif
439

                
440
        if (strlen(opts.hostname1) > 0) {
441
            gtk_list_store_append(store, &iter);
442
            gtk_list_store_set(store, &iter, 0, opts.hostname1, -1);
443
        }
444

                
445
        if (strlen(opts.hostname2) > 0) {
446
            gtk_list_store_append(store, &iter);
447
            gtk_list_store_set(store, &iter, 0, opts.hostname2, -1);
448
        }
449

                
450
        if (strlen(opts.hostname3) > 0) {
451
            gtk_list_store_append(store, &iter);
452
            gtk_list_store_set(store, &iter, 0, opts.hostname3, -1);
453
        }
454

                
455
        if (strlen(opts.hostname4) > 0) {
456
            gtk_list_store_append(store, &iter);
457
            gtk_list_store_set(store, &iter, 0, opts.hostname4, -1);
458
        }
459

                
460
        if (strlen(opts.hostname5) > 0) {
461
            gtk_list_store_append(store, &iter);
462
            gtk_list_store_set(store, &iter, 0, opts.hostname5, -1);
463
        }
464

                
465
        gtk_combo_box_set_active(GTK_COMBO_BOX(cmbConnectHostname), 0);
466

                
467
        if (!opts.show_connect_dialog) {
468
            /* If we got here, the user is running the NNTPGrab Server */
469
            GtkWidget *chkAlwaysStandalone = nntpgrab_gui_base_get_widget("chkAlwaysStandalone");
470
            gtk_widget_hide(chkAlwaysStandalone);
471
        }
472

                
473
        // If the NNTPGrab Server is also running on localhost, disable the 'Standalone' button
474
        if (nntpgrab_utils_test_is_server_already_running()) {
475
            GtkWidget *btnStartStandalone = nntpgrab_gui_base_get_widget("btnStartStandalone");
476
            GtkWidget *btnConnectToServer = nntpgrab_gui_base_get_widget("btnConnectToServer");
477
            gtk_widget_set_sensitive(btnStartStandalone, FALSE);
478
            gtk_widget_set_can_default(btnConnectToServer, TRUE);
479
            gtk_widget_grab_default(btnConnectToServer);
480
        }
481
    } else {
482
        g_idle_add(initialisation_inside_mainloop, GINT_TO_POINTER(0));
483
    }
484

                
485
    /* The update information can already be fetched now */
486
    info_initialize();
487

                
488
    if (opts.show_connect_dialog || nntpgrab_utils_test_is_server_already_running()) {
489
        return windowConnect;
490
    } else {
491
        return windowMain;
492
    }
493
}
494

                
495
#if GTK_CHECK_VERSION(3,0,0)
496
static void
497
activate(GtkApplication *app, gpointer data)
498
{
499
    GList *list;
500
    GtkWidget *window;
501

                
502
    list = gtk_application_get_windows(app);
503
    if (list) {
504
        gtk_widget_show(list->data);
505
        gtk_window_present(GTK_WINDOW(list->data));
506
    } else {
507
        window = create_user_interface();
508
        gtk_window_set_application(GTK_WINDOW(window), app);
509
        gtk_widget_show(window);
510
    }
511
}
512

                
513
static int
514
command_line(GtkApplication *app, GApplicationCommandLine *cmdline, gpointer data)
515
{
516
    char **argv;
517
    int argc;
518
    int i;
519

                
520
    argv = g_application_command_line_get_arguments(cmdline, &argc);
521
    for (i = 1; i < argc; i++) {
522
        if (g_file_test(argv[i], G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
523
            if (!initialized) {
524
                /* No windows found yet, assume were starting up for the first time */
525
                if (nzb_file) {
526
                    g_free((char*)nzb_file);
527
                }
528

                
529
                nzb_file = g_strdup(argv[i]);
530
                break;
531
            } else {
532
                do_open_nzb_file(argv[i], NULL);
533
            }
534
        }
535
    }
536

                
537
    if (!initialized) {
538
        activate(app, data);
539
    }
540

                
541
    return 0;
542
}
543
#endif
544

                
545
int
546
main(int argc, char *argv[])
547
{
548
    int status = 0;
549
    char *errmsg = NULL;
550
    gboolean is_running = FALSE;
551
    int i;
552
#ifdef HAVE_LIBPEAS
553
    PeasEngine *engine;
554
    const char *plugin_dir;
555
#endif
556

                
557
    g_set_application_name("NNTPGrab");
558
    g_set_prgname("NNTPGrab");
559

                
560
    nntpgrab_utils_perform_base_initialization();
561

                
562
#if GTK_CHECK_VERSION(3,0,0)
563
    app = gtk_application_new("org.NNTPGrab.NNTPGrab.GTK3", G_APPLICATION_HANDLES_COMMAND_LINE);
564

                
565
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
566
    g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);
567
#else
568
    gtk_init(&argc, &argv);
569
#endif
570

                
571
    /* Is there already another NNTPGrab instance running ? */
572
    if (!nntpgrab_utils_test_is_frontend_already_running(&is_running, &errmsg)) {
573
        /* Unable to check whether NNTPGrab is running */
574
        char *errmsg2 = g_strdup_printf(_("Unable to check whether NNTPGrab is already running:\n%s"), errmsg);
575
        nntpgrab_gui_base_dialog_show(NULL, errmsg2, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK);
576
        g_free(errmsg2);
577
        g_free(errmsg);
578

                
579
        return 1;
580
    }
581

                
582
    if (argc > 1) {
583
        /* We have an extra argument! Probably the location of a NZB file 
584
         * Is one our arguments really a filename ? */
585
        for (i = 1; i < argc; i++) {
586
            char *filename;
587

                
588
            if (!strncmp(argv[i], "--debug", 7)) {
589
                g_setenv("NNTPGRAB_DEBUG", "1", TRUE);
590
            }
591

                
592
#ifdef WIN32
593
            filename = g_convert(argv[i], -1, "utf-8", "windows-1252", NULL, NULL, NULL);
594
#else
595
            filename = g_strdup(argv[i]);
596
#endif
597
            if (g_file_test(filename, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
598
                nzb_file = g_strdup(filename);
599
            }
600
            g_free(filename);
601
        }
602

                
603
        if (nzb_file) {
604
            /* Is there already another NNTPGrab instance running ? */
605
            if (is_running) {
606
                /* Send a message to that other process and commit suicide */
607
                if (!nntpgrab_utils_send_open_nzb_file_request(nzb_file, &errmsg)) {
608
                    nntpgrab_gui_base_dialog_show(NULL, errmsg, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK);
609
                    g_free(errmsg);
610
                }
611

                
612
                if (!nntpgrab_utils_send_bring_to_front_request(&errmsg)) {
613
                    nntpgrab_gui_base_dialog_show(NULL, errmsg, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK);
614
                    g_free(errmsg);
615
                }
616

                
617
                return 2;
618
            }
619

                
620
            /* Nope, open the NZB Import dialog as soon as we're ready to go 
621
             * This will be done automatically due to the fact that nzb_file is a global variable */
622
        }
623
    }
624

                
625
    if (is_running) {
626
        /* There's already another frontend active. Stop execution of this one */
627
        if (!nntpgrab_utils_send_bring_to_front_request(&errmsg)) {
628
            char *errmsg2 = g_strdup_printf(_("Unable to send 'bring to front' request to already running NNTPGrab instance:\n%s"), errmsg);
629
            nntpgrab_gui_base_dialog_show(NULL, errmsg2, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK);
630
            g_free(errmsg2);
631
            g_free(errmsg);
632
        }
633

                
634
        return 3;
635
    }
636

                
637
#ifdef HAVE_LIBPEAS
638
    engine = peas_engine_get_default ();
639
    if (g_getenv("NNTPGRAB_PLUGINS_GTK_DIR")) {
640
        plugin_dir = g_getenv("NNTPGRAB_PLUGINS_GTK_DIR");
641
    } else {
642
        plugin_dir = PLUGIN_GTK_DIR;
643
    }
644
    peas_engine_add_search_path (engine, plugin_dir, plugin_dir);
645

                
646
    peas_engine_enable_loader(engine, "gjs");
647
    peas_engine_enable_loader(engine, "python");
648
    peas_engine_enable_loader(engine, "seed");
649
#endif
650

                
651
#if GTK_CHECK_VERSION(3,0,0)
652
    status = g_application_run(G_APPLICATION(app), argc, argv);
653

                
654
    g_object_unref(app);
655
#else
656
    if (!create_user_interface()) {
657
        return 4;
658
    }
659

                
660
    /* start the event loop */
661
    gtk_main();
662
#endif
663

                
664
    if (nntpgrab_gui_base_tray_icon_get_is_shown()) {
665
        nntpgrab_gui_base_tray_destroy();
666
    }
667

                
668
    /* Unload the GUI plugins before cleaning up the NNTPGrabGlue */
669
    gui_plugins_cleanup();
670

                
671
    if (glue) {
672
        // Always try to disable the auto import even if it isn't in use
673
        if (initialized) {
674
            disable_auto_import();
675
        }
676

                
677
        nntpgrab_glue_destroy(glue);
678
    }
679

                
680
    if (initialized) {
681
        gtk_widget_destroy(nntpgrab_gui_base_get_widget("windowMain"));
682
        gtk_widget_destroy(nntpgrab_gui_base_get_widget("windowConfig"));
683
        gtk_widget_destroy(nntpgrab_gui_base_get_widget("windowNZB"));
684
        gtk_widget_destroy(nntpgrab_gui_base_get_widget("windowDebug"));
685
        gtk_widget_destroy(nntpgrab_gui_base_get_widget("windowSetCollectionName"));
686
        gtk_widget_destroy(nntpgrab_gui_base_get_widget("windowPlugins"));
687

                
688
        nntpgrab_gui_base_destroy_ui();
689
    }
690

                
691
    return status;
692
}
693

                
694
#ifdef WIN32
695
static HINSTANCE instance = NULL;
696

                
697
HINSTANCE
698
get_hinstance(void)
699
{
700
    return instance;
701
}
702

                
703
int _stdcall
704
WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, char *lpszCmdLine, int nCmdShow)
705
{
706
    instance = hInstance;
707
    return main(__argc, __argv);
708
}
709

                
710
#endif