Statistics
| Revision:

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

History | View | Annotate | Download (4.8 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_automation.h"
23
#ifdef HAVE_LIBNOTIFY
24
#include 
25
#endif
26

                
27
static NNTPGrabAutoImport *auto_import = NULL;
28

                
29
static void
30
on_nzb_imported(NNTPGrabAutoImport *auto_import, const char *filename, const char *collection_name, gpointer data)
31
{
32
#ifdef HAVE_LIBNOTIFY
33
    NotifyNotification *notification;
34
    char *msg;
35

                
36
    if (auto_import) {
37
        /* The frontend has imported an NZB file */
38
        msg = g_strdup_printf(_("The file '%s'\nwas successfully imported with the collection name '%s'"), filename, collection_name);
39
    } else {
40
        /* The backend (which could be a different host) has imported an NZB file */
41
        if (nntpgrab_glue_get_is_standalone(glue)) {
42
            msg = g_strdup_printf(_("The file '%s'\nwas successfully imported with the collection name '%s'"), filename, collection_name);
43
        } else {
44
            msg = g_strdup_printf(_("The file '%s'\non the NNTPGrab Server was successfully\nimported with the collection name '%s'"), filename, collection_name);
45
        }
46
    }
47
#ifdef HAVE_LIBNOTIFY_0_7
48
    notification = notify_notification_new(_("NZB File Imported"), msg, NULL);
49
#else
50
    notification = notify_notification_new(_("NZB File Imported"), msg, NULL, NULL);
51
#endif
52
    g_free(msg);
53
    notify_notification_show(notification, NULL);
54
#endif
55
}
56

                
57
static void
58
on_plugin_event(NntpgrabGlue *obj, const char *plugin_name, const char *event_name, const char **params, gpointer data)
59
{
60
    if (!strcmp(plugin_name, "Auto-import") && !strcmp(event_name, "nzb_imported")) {
61
        on_nzb_imported(NULL, params[0], params[1], data);
62
    }
63
}
64

                
65
void
66
auto_import_import_nzb_file(NntpgrabGlue *obj, NNTPGrabNZB *nzbfile, const char *collection_name, char **warnings)
67
{
68
    NGList *list;
69
    char *errmsg = NULL;
70

                
71
    list = nzbfile->files;
72

                
73
    if (warnings) {
74
        *warnings = NULL;
75
    }
76

                
77
    while (list) {
78
        NNTPGrabNZBFile *file = list->data;
79

                
80
        if (!nntpgrab_glue_schedular_add_task_to_queue(glue, collection_name, file->subject, file->poster, file->stamp, file->filesize, file->groups, file->segments, &errmsg)) {
81
            if (warnings) {
82
                if (*warnings) {
83
                    char *tmp = g_strdup_printf("%s\n%s", *warnings, errmsg);
84
                    g_free(*warnings);
85
                    *warnings = tmp;
86
                } else {
87
                    *warnings = g_strdup_printf(_("File could not be added to the download queue:\n%s"), errmsg);
88
                }
89
                g_free(errmsg);
90
            }
91
        }
92

                
93
        list = ng_list_next(list);
94
    }
95
}
96

                
97
void
98
enable_auto_import(void)
99
{
100
#if GLIB_CHECK_VERSION(2,16,0)
101
    char *errmsg = NULL;
102
    ConfigGUIOpts opts = config_gui_get_opts();
103

                
104
    /* 
105
     * Refuse to enable the auto import when were running in standalone mode as
106
     * the auto import will be done by the auto-import plugin in that situation
107
     */
108
    g_return_if_fail(nntpgrab_glue_get_is_standalone(glue) == FALSE);
109

                
110
    if (!nntpgrab_automation_enable_auto_import(auto_import, opts.auto_import_directory, &errmsg)) {
111
        GtkWidget *windowMain = nntpgrab_gui_base_get_widget("windowMain");
112

                
113
        nntpgrab_gui_base_dialog_show(windowMain, errmsg, GTK_MESSAGE_INFO, GTK_BUTTONS_OK);
114

                
115
        g_free(errmsg);
116
    }
117
#endif
118
}
119

                
120
void
121
disable_auto_import(void)
122
{
123
#if GLIB_CHECK_VERSION(2,16,0)
124
    nntpgrab_automation_disable_auto_import(auto_import);
125
#endif
126
}
127

                
128
void
129
auto_import_initialize(void)
130
{
131
#if GLIB_CHECK_VERSION(2,16,0)
132
#ifndef __APPLE__
133
    ConfigGUIOpts opts = config_gui_get_opts();
134
#endif
135

                
136
    auto_import = nntpgrab_automation_auto_import_new((ConfigGetOptsFunc) nntpgrab_glue_config_get_opts, (ImportNZBFileFunc) auto_import_import_nzb_file, glue);
137
    ng_signal_connect(auto_import, "nzb_imported", NG_CALLBACK(on_nzb_imported), NULL);
138

                
139
    nntpgrab_glue_signal_connect(glue, "plugin_event", NG_CALLBACK(on_plugin_event), NULL);
140

                
141
    /* Don't enable the auto-import in standalone mode */
142
    if (nntpgrab_glue_get_is_standalone(glue)) {
143
        return;
144
    }
145

                
146
#ifndef __APPLE__
147
    if (opts.enable_auto_import) {
148
        enable_auto_import();
149
    }
150
#endif
151
#endif
152
}
153