Remove obsolete comment in RSSyl's OPML import.
[claws.git] / src / plugins / rssyl / rssyl_cb_menu.c
1 /*
2  * Claws-Mail-- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
4  * This file (C) 2005 Andrej Kacian <andrej@kacian.sk>
5  *
6  * - callback handler functions for folderview rssyl context menu items
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 /* Global includes */
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31
32 /* Claws Mail includes */
33 #include <folderview.h>
34 #include <alertpanel.h>
35 #include <gtk/inputdialog.h>
36 #include <prefs_common.h>
37 #include <folder_item_prefs.h>
38 #include <filesel.h>
39 #include <inc.h>
40
41 /* Local includes */
42 #include "libfeed/parser_opml.h"
43 #include "rssyl_gtk.h"
44 #include "rssyl_feed.h"
45 #include "rssyl_feed_props.h"
46 #include "rssyl_update_feed.h"
47 #include "rssyl_subscribe.h"
48 #include "opml_import.h"
49
50 void rssyl_new_feed_cb(GtkAction *action,
51                 gpointer data)
52 {
53         FolderView *folderview = (FolderView*)data;
54         FolderItem *item;
55         gchar *url;
56
57         debug_print("RSSyl: new_feed_cb\n");
58
59         g_return_if_fail(folderview->selected != NULL);
60
61         item = folderview_get_selected_item(folderview);
62         g_return_if_fail(item != NULL);
63         g_return_if_fail(item->folder != NULL);
64
65         url = input_dialog(_("Subscribe feed"),
66                         _("Input the URL of the news feed you wish to subscribe:"),
67                         "");
68         if( url == NULL )       /* User cancelled */
69                 return;
70
71         rssyl_subscribe(item, url, TRUE);
72
73         g_free(url);
74 }
75
76 void rssyl_new_folder_cb(GtkAction *action,
77                 gpointer data)
78 {
79         FolderView *folderview = (FolderView*)data;
80         FolderItem *item;
81         FolderItem *new_item;
82         gchar *new_folder, *p, *tmp;
83         gint i = 1;
84
85         if (!folderview->selected) return;
86
87         item = folderview_get_selected_item(folderview);
88         g_return_if_fail(item != NULL);
89         g_return_if_fail(item->folder != NULL);
90
91         new_folder = input_dialog(_("New folder"),
92                                   _("Input the name of new folder:"),
93                                   _("NewFolder"));
94         if (!new_folder) return;
95
96         p = strchr(new_folder, G_DIR_SEPARATOR);
97         if (p) {
98                 alertpanel_error(_("'%c' can't be used in folder name."),
99                                  G_DIR_SEPARATOR);
100                 g_free(new_folder);
101                 return;
102         }
103
104         if (!folder_local_name_ok(new_folder)) {
105                 g_free(new_folder);
106                 return;
107         }
108
109         /* Find an unused name for new folder */
110         /* TODO: Perhaps stop after X attempts? */
111         tmp = g_strdup(new_folder);
112         while (folder_find_child_item_by_name(item, tmp)) {
113                 debug_print("RSSyl: Folder '%s' already exists, trying another name\n",
114                                 new_folder);
115                 g_free(tmp);
116                 tmp = g_strdup_printf("%s__%d", new_folder, ++i);
117         }
118
119         g_free(new_folder);
120         new_folder = tmp;
121
122         new_item = folder_create_folder(item, new_folder);
123         if (!new_item) {
124                 alertpanel_error(_("Can't create the folder '%s'."), new_folder);
125                 g_free(new_folder);
126                 return;
127         }
128
129         g_free(new_folder);
130
131         folder_write_list();
132 }
133
134 void rssyl_remove_folder_cb(GtkAction *action,
135                              gpointer data)
136 {
137         FolderView *folderview = (FolderView*)data;
138         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
139         FolderItem *item;
140         gchar *message, *name;
141         AlertValue avalue;
142         gchar *old_id;
143
144         item = folderview_get_selected_item(folderview);
145         g_return_if_fail(item != NULL);
146         g_return_if_fail(item->path != NULL);
147         g_return_if_fail(item->folder != NULL);
148
149         name = trim_string(item->name, 32);
150         AUTORELEASE_STR(name, {g_free(name); return;});
151         message = g_strdup_printf
152                 (_("All folders and messages under '%s' will be permanently deleted. "
153                    "Recovery will not be possible.\n\n"
154                    "Do you really want to delete?"), name);
155         avalue = alertpanel_full(_("Delete folder"), message,
156                                  GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
157                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
158         g_free(message);
159         if (avalue != G_ALERTALTERNATE) return;
160
161         old_id = folder_item_get_identifier(item);
162
163         if (folderview->opened == folderview->selected ||
164             gtk_cmctree_is_ancestor(ctree,
165                                   folderview->selected,
166                                   folderview->opened)) {
167                 summary_clear_all(folderview->summaryview);
168                 folderview->opened = NULL;
169         }
170
171         if (item->folder->klass->remove_folder(item->folder, item) < 0) {
172                 folder_item_scan(item);
173                 alertpanel_error(_("Can't remove the folder '%s'."), name);
174                 g_free(old_id);
175                 return;
176         }
177
178         folder_write_list();
179
180         prefs_filtering_delete_path(old_id);
181         g_free(old_id);
182
183 }
184
185 void rssyl_rename_cb(GtkAction *action,
186                              gpointer *data)
187 {
188         FolderItem *item;
189         gchar *new_folder;
190         gchar *name;
191         gchar *message;
192         FolderView *folderview = (FolderView*)data;
193         item = folderview_get_selected_item(folderview);
194         g_return_if_fail(item != NULL);
195         g_return_if_fail(item->path != NULL);
196         g_return_if_fail(item->folder != NULL);
197
198         name = trim_string(item->name, 32);
199         message = g_strdup_printf(_("Input new name for '%s':"), name);
200         new_folder = input_dialog(_("Rename folder"), message, name);
201         g_free(message);
202         g_free(name);
203         if (!new_folder) return;
204
205         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
206                 alertpanel_error(_("'%c' can't be included in folder name."),
207                                  G_DIR_SEPARATOR);
208                 g_free(new_folder);
209                 return;
210         }
211
212         if (!folder_local_name_ok(new_folder)) {
213                 g_free(new_folder);
214                 return;
215         }
216
217         if (folder_find_child_item_by_name(folder_item_parent(item), new_folder)) {
218                 name = trim_string(new_folder, 32);
219                 alertpanel_error(_("The folder '%s' already exists."), name);
220                 g_free(name);
221                 g_free(new_folder);
222                 return;
223         }
224
225         if (folder_item_rename(item, new_folder) < 0) {
226                 alertpanel_error(_("The folder could not be renamed.\n"
227                                    "The new folder name is not allowed."));
228                 g_free(new_folder);
229                 return;
230         }
231         g_free(new_folder);
232
233         folder_item_prefs_save_config(item);
234         folder_write_list();
235 }
236
237 void rssyl_refresh_feed_cb(GtkAction *action,
238                 gpointer data)
239 {
240         FolderView *folderview = (FolderView*)data;
241         FolderItem *item = NULL;
242         RFolderItem *ritem = NULL;
243
244         item = folderview_get_selected_item(folderview);
245         g_return_if_fail(item != NULL);
246         g_return_if_fail(item->folder != NULL);
247
248         ritem = (RFolderItem *)item;
249
250         /* Offline check */
251         if( prefs_common_get_prefs()->work_offline &&
252                         !inc_offline_should_override(TRUE,
253                                         ngettext("Claws Mail needs network access in order "
254                                         "to update the feed.",
255                                         "Claws Mail needs network access in order "
256                                         "to update feeds.", 1))) {
257                 return;
258         }
259
260         /* Update feed, displaying errors if any. */
261         rssyl_update_feed(ritem, TRUE);
262 }
263
264 void rssyl_prop_cb(GtkAction *action, gpointer data)
265 {
266         FolderView *folderview = (FolderView*)data;
267         FolderItem *item;
268         RFolderItem *ritem;
269
270         item = folderview_get_selected_item(folderview);
271         g_return_if_fail(item != NULL);
272         g_return_if_fail(item->folder != NULL);
273
274         debug_print("RSSyl: rssyl_prop_cb() for '%s'\n", item->name);
275
276         ritem = (RFolderItem *)item;
277
278         rssyl_gtk_prop(ritem);
279 }
280
281 void rssyl_update_all_cb( GtkAction *action, gpointer data)
282 {
283         FolderItem *item;
284         FolderView *folderview = (FolderView*)data;
285
286         item = folderview_get_selected_item(folderview);
287         g_return_if_fail(item != NULL);
288         g_return_if_fail(item->folder != NULL);
289
290         debug_print("RSSyl: rssyl_update_all_cb(): clicked on '%s'\n", item->name);
291
292         if( item->folder->klass != rssyl_folder_get_class() ) {
293                 debug_print("RSSyl: this is not a RSSyl folder, returning\n");
294                 return;
295         }
296
297         rssyl_update_recursively(item);
298 }
299
300 void rssyl_remove_mailbox_cb(GtkAction *action, gpointer data)
301 {
302         FolderView *folderview = (FolderView *)data;
303         FolderItem *item = NULL;
304         gchar *n, *message;
305         AlertValue avalue;
306
307         item = folderview_get_selected_item(folderview);
308
309         g_return_if_fail(item != NULL);
310         g_return_if_fail(item->folder != NULL);
311
312         if( folder_item_parent(item) )
313                 return;
314
315         n = trim_string(item->folder->name, 32);
316         message = g_strdup_printf(_("Really remove the feed tree `%s' ?\n"), n);
317         avalue = alertpanel_full(_("Remove feed tree"), message,
318                                  GTK_STOCK_CANCEL, _("_Remove"), NULL, FALSE,
319                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
320         g_free(message);
321         g_free(n);
322
323         if( avalue != G_ALERTALTERNATE )
324                 return;
325
326         folderview_unselect(folderview);
327         summary_clear_all(folderview->summaryview);
328
329         n = folder_item_get_path(item);
330         if( remove_dir_recursive(n) < 0 ) {
331                 g_warning("can't remove directory '%s'", n);
332                 g_free(n);
333                 return;
334         }
335
336         g_free(n);
337         folder_destroy(item->folder);
338 }
339
340 void rssyl_import_feed_list_cb(GtkAction *action, gpointer data)
341 {
342         FolderView *folderview = (FolderView *)data;
343         FolderItem *item = NULL;
344         gchar *path = NULL;
345         OPMLImportCtx *ctx = NULL;
346
347         debug_print("RSSyl: import_feed_list_cb\n");
348
349         /* Ask user for a file to import */
350         path = filesel_select_file_open_with_filter(
351                         _("Select an OPML file"), NULL, "*.opml");
352         if (!is_file_exist(path)) {
353                 g_free(path);
354                 return;
355         }
356
357         /* Find the destination folder for the import */
358         g_return_if_fail(folderview->selected != NULL);
359         item = folderview_get_selected_item(folderview);
360         g_return_if_fail(item != NULL);
361         g_return_if_fail(item->folder != NULL);
362
363         ctx = malloc( sizeof(OPMLImportCtx) );
364         ctx->failures = 0;
365         ctx->depth = rssyl_folder_depth(item) + 1;
366         ctx->current = NULL;
367         ctx->current = g_slist_append(ctx->current, item);
368
369         /* Start the whole shebang - call libfeed's OPML parser with correct
370          * user function */
371         opml_process(path, rssyl_opml_import_func, (gpointer)ctx);
372
373         g_free(ctx);
374 }