Statistics
| Revision:

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

History | View | Annotate | Download (21.1 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 DARWIN
28
#include 
29
#endif
30

                
31
#ifdef WIN32
32
#include 
33
#endif
34

                
35
NntpgrabGlue *glue = NULL;
36

                
37
#if GTK_CHECK_VERSION(3,0,0)
38
static GtkApplication *app = NULL;
39
#endif
40

                
41
void debug_initialize(void);
42
void info_initialize(void);
43
void config_initialize(void);
44
void config_gui_initialize(void);
45
void config_display_initialize(void);
46
void queue_initialize(void);
47
void callbacks_initialize(void);
48
void nzb_initialize(void);
49
void connections_initialize(void);
50
void notify_initialize(void);
51
void monitor_initialize(void);
52
void gui_par2_initialize(void);
53
void nzbcreator_initialize(void);
54
void auto_import_initialize(void);
55
void autoshutdown_initialize(void);
56
void gui_plugins_initialize(void);
57

                
58
gboolean    ipc_initialize(void);
59
void        ipc_cleanup(void);
60
void        ipc_send_open_nzb_file_request(const char *nzb_file);
61
gboolean    ipc_test_is_nntpgrab_already_running(gboolean also_check_server);
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 gboolean
118
initialisation_inside_mainloop(gpointer data)
119
{
120
    GtkWidget *windowMain = nntpgrab_gui_base_get_widget("windowMain");
121
    GtkWidget *windowConnect = nntpgrab_gui_base_get_widget("windowConnect");
122
    char *errmsg = NULL;
123
    GtkWidget *cmbConnectHostname;
124
    GtkWidget *spinConnectPort;
125
    char *hostname;
126
    int port;
127
    char *warnings = NULL;
128
#if GTK_CHECK_VERSION(2,90,0)
129
    GtkTreeIter iter;
130
    GtkTreeModel *model;
131
#endif
132

                
133
#ifdef WIN32
134
    WORD wVersionRequested;
135
    WSADATA wsaData;
136
    int ret;
137

                
138
    // Initialise Winsock
139
    wVersionRequested = MAKEWORD(2, 2);
140

                
141
    ret = WSAStartup(wVersionRequested, &wsaData);
142
    if (ret != 0) {
143
        /* Tell the user that we could not find a usable */
144
        /* WinSock DLL. */
145
        g_error(_("Winsock could not be initialised"));
146
        return -1;
147
    }
148

                
149
    /* Confirm that the WinSock DLL supports 2.2. */
150
    /* Note that if the DLL supports versions greater */
151
    /* than 2.2 in addition to 2.2, it will still return */
152
    /* 2.2 in wVersion since that is the version we */
153
    /* requested. */
154

                
155
    if (LOBYTE( wsaData.wVersion ) != 2 ||
156
        HIBYTE( wsaData.wVersion ) != 2) {
157
        /* Tell the user that we could not find a usable */
158
        /* WinSock DLL. */
159
        WSACleanup();
160

                
161
        g_error(_("Winsock 2.2 or higher is required for this program"));
162

                
163
        return -1;
164
    }
165
#endif
166

                
167
    gtk_widget_show(windowMain);
168

                
169
#if GTK_CHECK_VERSION(3,0,0)
170
    gtk_application_add_window(app, GTK_WINDOW(windowMain));
171
    gtk_application_remove_window(app, GTK_WINDOW(windowConnect));
172
#endif
173

                
174
    cmbConnectHostname = nntpgrab_gui_base_get_widget("cmbConnectHostname");
175
    spinConnectPort = nntpgrab_gui_base_get_widget("spinConnectPort");
176
    hostname = g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(cmbConnectHostname)))));
177
    port = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spinConnectPort));
178

                
179
    glue = nntpgrab_glue_new();
180

                
181
    if (!nntpgrab_glue_init(glue, NNTPGRAB_GLUE_VERSION, &errmsg)) {
182
        nntpgrab_glue_destroy(glue);
183
        glue = NULL;
184
        g_free(hostname);
185
        g_critical(_("NNTPGrab Initialisation FAILED:\n%s\n"), errmsg);
186
        g_free(errmsg);
187
        g_idle_add((GSourceFunc) gtk_main_quit, NULL);
188
        return FALSE;
189
    }
190

                
191
    if (GPOINTER_TO_INT(data) == 0) {       // Standalone mode
192
        GObject *menuitemKillServer = nntpgrab_gui_base_get_object("menuitemKillServer");
193
        GValue val;
194
        memset(&val, 0, sizeof(val));
195
        g_value_init(&val, G_TYPE_BOOLEAN);
196
        g_value_set_boolean (&val, FALSE);
197
        g_object_set_property(G_OBJECT(menuitemKillServer), "sensitive", &val);
198

                
199
        if (hostname) {
200
            g_free(hostname);
201
            hostname = NULL;
202
        }
203
    }
204

                
205
    if (!nntpgrab_glue_connect(glue, hostname, port, NULL, NULL, FALSE, &errmsg, &warnings)) {
206
        if (warnings) {
207
            GtkWidget *windowMain = nntpgrab_gui_base_get_widget("windowMain");
208
            nntpgrab_gui_base_dialog_show(windowMain, warnings, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK);
209
            g_free(warnings);
210
        }
211

                
212
        g_free(hostname);
213
        g_critical(_("Unable to connect to NNTPGrab server:\n%s"), errmsg);
214
        g_free(errmsg);
215
        g_idle_add((GSourceFunc) gtk_main_quit, NULL);
216
        return FALSE;
217
    }
218

                
219
    if (warnings) {
220
        GtkWidget *windowMain = nntpgrab_gui_base_get_widget("windowMain");
221
        nntpgrab_gui_base_dialog_show(windowMain, warnings, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK);
222
        g_free(warnings);
223
    }
224

                
225
    g_free(hostname);
226

                
227
    gtk_widget_destroy(windowConnect);
228

                
229
#if !defined(DARWIN) && !GTK_CHECK_VERSION(3,0,0)
230
    if (!ipc_initialize()) {
231
        // There is already another NNTPGrab process active!
232
        g_idle_add((GSourceFunc) gtk_main_quit, NULL);
233
        return FALSE;
234
    }
235
#endif
236

                
237
    debug_initialize();
238
    callbacks_initialize();
239
    config_initialize();
240
    config_display_initialize();
241
#ifndef DARWIN
242
    nzb_initialize();
243
#endif
244
    connections_initialize();
245
    queue_initialize();
246
    notify_initialize();
247
    monitor_initialize();
248
    gui_par2_initialize();
249
    nzbcreator_initialize();
250
    auto_import_initialize();
251
    autoshutdown_initialize();
252
    gui_plugins_initialize();
253

                
254
    /* Load any optional plugins which need to be auto-loaded */
255
    load_auto_load_plugins();
256

                
257
    // Did we receive a NZB file on the command line ?
258
    if (nzb_file) {
259
        do_open_nzb_file(nzb_file);
260
        nzb_file = NULL;
261
    }
262

                
263
    initialized = TRUE;
264

                
265
    return FALSE;
266
}
267

                
268
G_MODULE_EXPORT void
269
on_btnConnectToServer_clicked(GtkWidget *caller, gpointer data)
270
{
271
    GtkWidget *windowConnect = nntpgrab_gui_base_get_widget("windowConnect");
272

                
273
    // Save the hostname into the GUI Configuration
274
    ConfigGUIOpts opts = config_gui_get_opts();
275
    GtkWidget *cmbConnectHostname = nntpgrab_gui_base_get_widget("cmbConnectHostname");
276
    char *hostname = gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(cmbConnectHostname))));
277

                
278
    // Have we seen this hostname before ?
279
    if (!strcmp(hostname, opts.hostname1)) {
280
        // No need to do anything, item is already at the top of the list
281
    } else if (!strcmp(hostname, opts.hostname2)) {
282
        // Swap positions
283
        strncpy(opts.hostname2, opts.hostname1, sizeof(opts.hostname2));
284
        strncpy(opts.hostname1, hostname, sizeof(opts.hostname1));
285
    } else if (!strcmp(hostname, opts.hostname3)) {
286
        // Push items 1 and 2 one place down and set the first item to the hostname
287
        strncpy(opts.hostname3, opts.hostname2, sizeof(opts.hostname3));
288
        strncpy(opts.hostname2, opts.hostname1, sizeof(opts.hostname2));
289
        strncpy(opts.hostname1, hostname, sizeof(opts.hostname1));
290
    } else if (!strcmp(hostname, opts.hostname4)) {
291
        strncpy(opts.hostname4, opts.hostname3, sizeof(opts.hostname4));
292
        strncpy(opts.hostname3, opts.hostname2, sizeof(opts.hostname3));
293
        strncpy(opts.hostname2, opts.hostname1, sizeof(opts.hostname2));
294
        strncpy(opts.hostname1, hostname, sizeof(opts.hostname1));
295
    } else if (!strcmp(hostname, opts.hostname5)) {
296
        strncpy(opts.hostname5, opts.hostname4, sizeof(opts.hostname5));
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 {
302
        // Hostname not seen at all, move all the items one place down (forget
303
        // about the last item) and put the hostname in front
304
        strncpy(opts.hostname5, opts.hostname4, sizeof(opts.hostname5));
305
        strncpy(opts.hostname4, opts.hostname3, sizeof(opts.hostname4));
306
        strncpy(opts.hostname3, opts.hostname2, sizeof(opts.hostname3));
307
        strncpy(opts.hostname2, opts.hostname1, sizeof(opts.hostname2));
308
        strncpy(opts.hostname1, hostname, sizeof(opts.hostname1));
309
    }
310

                
311
    config_gui_set_opts(opts);
312

                
313
    gtk_widget_set_sensitive(windowConnect, FALSE);
314
    g_idle_add(initialisation_inside_mainloop, GINT_TO_POINTER(1));
315
}
316

                
317
G_MODULE_EXPORT void
318
on_btnStartStandalone_clicked(GtkWidget *caller, gpointer data)
319
{
320
    GtkWidget *windowConnect = nntpgrab_gui_base_get_widget("windowConnect");
321

                
322
    gtk_widget_set_sensitive(windowConnect, FALSE);
323
    g_idle_add(initialisation_inside_mainloop, GINT_TO_POINTER(0));
324
}
325

                
326
G_MODULE_EXPORT void
327
on_btnConnectQuit_clicked(GtkWidget *caller, gpointer data)
328
{
329
    GtkWidget *windowConnect = nntpgrab_gui_base_get_widget("windowConnect");
330

                
331
    gtk_widget_destroy(windowConnect);
332

                
333
    gtk_main_quit();
334
}
335

                
336
G_MODULE_EXPORT void
337
on_menuitemManagePlugins_activate(GtkWidget *caller, gpointer data)
338
{
339
    GtkWidget *windowPlugins = nntpgrab_gui_base_get_widget("windowPlugins");
340

                
341
    gtk_window_present(GTK_WINDOW(windowPlugins));
342
}
343

                
344
G_MODULE_EXPORT void
345
on_menuitemKillServer_activate(GtkWidget *caller, gpointer data)
346
{
347
    nntpgrab_glue_kill_server(glue);
348
}
349

                
350
#ifdef _MSC_VER
351
void AppInvalidParameterHandler(const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line,        uintptr_t pReserved ) {
352
        fprintf(
353
                stderr,
354
                "Invalid parameter detected in function %s. File: %s Line: %d\n",
355
                function,
356
                file,
357
                line
358
        );
359
        fprintf(
360
                stderr,
361
                "Expression: %s\n",
362
                expression
363
        );
364
        // Cause a Debug Breakpoint.
365
        DebugBreak();
366
}
367
#endif
368

                
369
gboolean
370
gui_get_is_initialized()
371
{
372
    return initialized;
373
}
374

                
375
static GtkWidget *
376
create_user_interface(void)
377
{
378
    GtkWidget *btnDonate;
379
    GtkWidget *icon;
380
    GdkPixbuf *pixbuf;
381
    GtkWidget *windowMain;
382
    GtkWidget *windowConnect;
383
    GtkWidget *cmbConnectHostname;
384
    GtkListStore *store;
385
    ConfigGUIOpts opts;
386
    GtkTreeIter iter;
387
#ifdef DARWIN
388
    GtkWidget *quit_menu_widget;
389
#endif
390

                
391
    /* Initialize libxml now to prevent possible race conditions */
392
    xmlInitGlobals();
393
    xmlInitParser();
394

                
395
    nntpgrab_gui_base_create_ui("nntpgrab_gui");
396

                
397
     windowMain = nntpgrab_gui_base_get_widget("windowMain");
398
     windowConnect = nntpgrab_gui_base_get_widget("windowConnect");
399

                
400
#ifdef DARWIN
401
    ige_mac_menu_set_global_key_handler_enabled (TRUE);
402
    ige_mac_menu_connect_window_key_handler (GTK_WINDOW(nntpgrab_gui_base_get_widget("windowMain")));
403

                
404
    quit_menu_widget = gtk_ui_manager_get_widget(GTK_UI_MANAGER(nntpgrab_gui_base_get_object("uimanager1")), "ui/menubar1/menuitem1/imagemenuitem5");
405
    g_warn_if_fail(quit_menu_widget != NULL);
406

                
407
    ige_mac_menu_set_quit_menu_item (GTK_MENU_ITEM (quit_menu_widget));
408

                
409
    ige_mac_menu_set_menu_bar(GTK_MENU_SHELL(nntpgrab_gui_base_get_widget("menubar1")));
410
    gtk_widget_hide(nntpgrab_gui_base_get_widget("menubar1"));
411
#endif
412

                
413
    // Set the donate icon of the window
414
    btnDonate = nntpgrab_gui_base_get_widget("btnDonate");
415
    pixbuf = gdk_pixbuf_new_from_xpm_data((const char **) &donate_xpm);
416
    icon = gtk_image_new_from_pixbuf(pixbuf);
417
    gtk_widget_show(icon);
418
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(btnDonate), icon);
419
    g_object_unref(pixbuf);
420

                
421
#ifdef DARWIN
422
    // Due to the way file associations work on Mac OS X, we need to make
423
    // sure the NZB dialog can be used before the file association is set up
424
    nzb_initialize();
425

                
426
    // And the file assocation needs to be set up as soon as possible..
427
    if (!ipc_initialize()) {
428
        // There is already another NNTPGrab process active!
429
        return NULL;
430
    }
431
#endif
432

                
433
    // We need to have the GUI configuration loaded at this point because the
434
    // Network GUI requires data from it
435
    config_gui_initialize();
436

                
437
    opts = config_gui_get_opts();
438

                
439
    if (opts.show_connect_dialog || nntpgrab_utils_test_is_server_already_running()) {
440
        // Show the connect dialog so the user can enter where the NNTPGrab server is running
441
        windowConnect = nntpgrab_gui_base_get_widget("windowConnect");
442
        cmbConnectHostname = nntpgrab_gui_base_get_widget("cmbConnectHostname");
443

                
444
        gtk_widget_show(windowConnect);
445

                
446
        store = gtk_list_store_new(1, G_TYPE_STRING);
447
        gtk_combo_box_set_model(GTK_COMBO_BOX(cmbConnectHostname), GTK_TREE_MODEL(store));
448
#if GTK_CHECK_VERSION(3,0,0)
449
        gtk_combo_box_set_entry_text_column(GTK_COMBO_BOX(cmbConnectHostname), 0);
450
#else
451
        gtk_combo_box_entry_set_text_column(GTK_COMBO_BOX_ENTRY(cmbConnectHostname), 0);
452
#endif
453

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

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

                
464
        if (strlen(opts.hostname3) > 0) {
465
            gtk_list_store_append(store, &iter);
466
            gtk_list_store_set(store, &iter, 0, opts.hostname3, -1);
467
        }
468

                
469
        if (strlen(opts.hostname4) > 0) {
470
            gtk_list_store_append(store, &iter);
471
            gtk_list_store_set(store, &iter, 0, opts.hostname4, -1);
472
        }
473

                
474
        if (strlen(opts.hostname5) > 0) {
475
            gtk_list_store_append(store, &iter);
476
            gtk_list_store_set(store, &iter, 0, opts.hostname5, -1);
477
        }
478

                
479
        gtk_combo_box_set_active(GTK_COMBO_BOX(cmbConnectHostname), 0);
480

                
481
        if (!opts.show_connect_dialog) {
482
            /* If we got here, the user is running the NNTPGrab Server */
483
            GtkWidget *chkAlwaysStandalone = nntpgrab_gui_base_get_widget("chkAlwaysStandalone");
484
            gtk_widget_hide(chkAlwaysStandalone);
485
        }
486

                
487
        // If the NNTPGrab Server is also running on localhost, disable the 'Standalone' button
488
        if (nntpgrab_utils_test_is_server_already_running()) {
489
            GtkWidget *btnStartStandalone = nntpgrab_gui_base_get_widget("btnStartStandalone");
490
            GtkWidget *btnConnectToServer = nntpgrab_gui_base_get_widget("btnConnectToServer");
491
            gtk_widget_set_sensitive(btnStartStandalone, FALSE);
492
            gtk_widget_set_can_default(btnConnectToServer, TRUE);
493
            gtk_widget_grab_default(btnConnectToServer);
494
        }
495
    } else {
496
        g_idle_add(initialisation_inside_mainloop, GINT_TO_POINTER(0));
497
    }
498

                
499
    /* The update information can already be fetched now */
500
    info_initialize();
501

                
502
    if (opts.show_connect_dialog) {
503
        return windowConnect;
504
    } else {
505
        return windowMain;
506
    }
507
}
508

                
509
#if GTK_CHECK_VERSION(3,0,0)
510
static void
511
activate(GtkApplication *app, gpointer data)
512
{
513
    GList *list;
514
    GtkWidget *window;
515

                
516
    list = gtk_application_get_windows(app);
517
    if (list) {
518
        gtk_widget_show(list->data);
519
        gtk_window_present(GTK_WINDOW(list->data));
520
    } else {
521
        window = create_user_interface();
522
        gtk_window_set_application(GTK_WINDOW(window), app);
523
        gtk_widget_show(window);
524
    }
525
}
526

                
527
static int
528
command_line(GtkApplication *app, GApplicationCommandLine *cmdline, gpointer data)
529
{
530
    char **argv;
531
    int argc;
532
    int i;
533

                
534
    argv = g_application_command_line_get_arguments(cmdline, &argc);
535
    for (i = 1; i < argc; i++) {
536
        if (g_file_test(argv[i], G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
537
            if (!initialized) {
538
                /* No windows found yet, assume were starting up for the first time */
539
                if (nzb_file) {
540
                    g_free((char*)nzb_file);
541
                }
542

                
543
                nzb_file = g_strdup(argv[i]);
544
                break;
545
            } else {
546
                do_open_nzb_file(argv[i]);
547
            }
548
        }
549
    }
550

                
551
    if (!initialized) {
552
        activate(app, data);
553
    }
554

                
555
    return 0;
556
}
557
#endif
558

                
559
int
560
main(int argc, char *argv[])
561
{
562
    int status = 0;
563
#if !GTK_CHECK_VERSION(3,0,0)
564
    int i;
565
#endif
566

                
567
#ifdef WIN32
568
#ifdef _MSC_VER
569
    _set_invalid_parameter_handler(AppInvalidParameterHandler);
570
#else
571
    // exchndl.dll is part of Dr. MinGW. By loading this DLL, every time the
572
    // program crashes a .RPT file will be created containing the backtrace
573
    LoadLibraryA("exchndl.dll");
574
#endif
575
#endif
576

                
577
    g_set_application_name("NNTPGrab");
578
    g_set_prgname("NNTPGrab");
579

                
580
    nntpgrab_utils_perform_base_initialization();
581

                
582
#if GTK_CHECK_VERSION(3,0,0)
583
    app = gtk_application_new("org.NNTPGrab.NNTPGrab", G_APPLICATION_HANDLES_COMMAND_LINE);
584

                
585
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
586
    g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);
587

                
588
    status = g_application_run(G_APPLICATION(app), argc, argv);
589

                
590
    g_object_unref(app);
591
#else
592
    gtk_init(&argc, &argv);
593

                
594
    if (argc > 1) {
595
        /* We have an extra argument! Probably the location of a NZB file 
596
         * Is one our arguments really a filename ? */
597
        for (i = 1; i < argc; i++) {
598
            char *filename;
599

                
600
            if (!strncmp(argv[i], "--debug", 7)) {
601
                g_setenv("NNTPGRAB_DEBUG", "1", TRUE);
602
            }
603

                
604
#ifdef WIN32
605
            filename = g_convert(argv[i], -1, "utf-8", "windows-1252", NULL, NULL, NULL);
606
#else
607
            filename = g_strdup(argv[i]);
608
#endif
609
            if (g_file_test(filename, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
610
                nzb_file = g_strdup(filename);
611
            }
612
            g_free(filename);
613
        }
614

                
615
        if (nzb_file) {
616
            /* Is there already another NNTPGrab instance running ? */
617
            if (ipc_test_is_nntpgrab_already_running(FALSE)) {
618
                /* Send a message to that other process and commit suicide */
619
                ipc_send_open_nzb_file_request(nzb_file);
620
                return(1);
621
            }
622

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

                
628
    if (!create_user_interface()) {
629
        return -1;
630
    }
631

                
632
    /* start the event loop */
633
    gtk_main();
634
#endif
635

                
636
    if (nntpgrab_gui_base_tray_icon_get_is_shown()) {
637
        nntpgrab_gui_base_tray_destroy();
638
    }
639

                
640
    if (glue) {
641
        // Always try to disable the auto import even if it isn't in use
642
        if (initialized) {
643
            disable_auto_import();
644
        }
645

                
646
        nntpgrab_glue_destroy(glue);
647
    }
648

                
649
    if (initialized) {
650
        gtk_widget_destroy(nntpgrab_gui_base_get_widget("windowMain"));
651
        gtk_widget_destroy(nntpgrab_gui_base_get_widget("windowConfig"));
652
        gtk_widget_destroy(nntpgrab_gui_base_get_widget("windowNZB"));
653
        gtk_widget_destroy(nntpgrab_gui_base_get_widget("windowDebug"));
654
        gtk_widget_destroy(nntpgrab_gui_base_get_widget("windowSetCollectionName"));
655
        gtk_widget_destroy(nntpgrab_gui_base_get_widget("windowPlugins"));
656

                
657
        nntpgrab_gui_base_destroy_ui();
658
    }
659

                
660
    return status;
661
}
662

                
663
#ifdef WIN32
664
static HINSTANCE instance = NULL;
665

                
666
HINSTANCE
667
get_hinstance(void)
668
{
669
    return instance;
670
}
671

                
672
int _stdcall
673
WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, char *lpszCmdLine, int nCmdShow)
674
{
675
    instance = hInstance;
676
    return main(__argc, __argv);
677
}
678

                
679
#endif