Further improve API isolation of folderview.
[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         FolderItem *item, *opened;
139         gchar *message, *name;
140         AlertValue avalue;
141         gchar *old_id;
142
143         item = folderview_get_selected_item(folderview);
144         g_return_if_fail(item != NULL);
145         g_return_if_fail(item->path != NULL);
146         g_return_if_fail(item->folder != NULL);
147         opened = folderview_get_opened_item(folderview);
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 (item == opened ||
164                         folder_is_child_of(item, opened)) {
165                 summary_clear_all(folderview->summaryview);
166                 folderview_close_opened(folderview, TRUE);
167         }
168
169         if (item->folder->klass->remove_folder(item->folder, item) < 0) {
170                 folder_item_scan(item);
171                 alertpanel_error(_("Can't remove the folder '%s'."), name);
172                 g_free(old_id);
173                 return;
174         }
175
176         folder_write_list();
177
178         prefs_filtering_delete_path(old_id);
179         g_free(old_id);
180
181 }
182
183 void rssyl_rename_cb(GtkAction *action,
184                              gpointer *data)
185 {
186         FolderItem *item;
187         gchar *new_folder;
188         gchar *name;
189         gchar *message;
190         FolderView *folderview = (FolderView*)data;
191         item = folderview_get_selected_item(folderview);
192         g_return_if_fail(item != NULL);
193         g_return_if_fail(item->path != NULL);
194         g_return_if_fail(item->folder != NULL);
195
196         name = trim_string(item->name, 32);
197         message = g_strdup_printf(_("Input new name for '%s':"), name);
198         new_folder = input_dialog(_("Rename folder"), message, name);
199         g_free(message);
200         g_free(name);
201         if (!new_folder) return;
202
203         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
204                 alertpanel_error(_("'%c' can't be included in folder name."),
205                                  G_DIR_SEPARATOR);
206                 g_free(new_folder);
207                 return;
208         }
209
210         if (!folder_local_name_ok(new_folder)) {
211                 g_free(new_folder);
212                 return;
213         }
214
215         if (folder_find_child_item_by_name(folder_item_parent(item), new_folder)) {
216                 name = trim_string(new_folder, 32);
217                 alertpanel_error(_("The folder '%s' already exists."), name);
218                 g_free(name);
219                 g_free(new_folder);
220                 return;
221         }
222
223         if (folder_item_rename(item, new_folder) < 0) {
224                 alertpanel_error(_("The folder could not be renamed.\n"
225                                    "The new folder name is not allowed."));
226                 g_free(new_folder);
227                 return;
228         }
229         g_free(new_folder);
230
231         folder_item_prefs_save_config(item);
232         folder_write_list();
233 }
234
235 void rssyl_refresh_feed_cb(GtkAction *action,
236                 gpointer data)
237 {
238         FolderView *folderview = (FolderView*)data;
239         FolderItem *item = NULL;
240         RFolderItem *ritem = NULL;
241
242         item = folderview_get_selected_item(folderview);
243         g_return_if_fail(item != NULL);
244         g_return_if_fail(item->folder != NULL);
245
246         ritem = (RFolderItem *)item;
247
248         /* Offline check */
249         if( prefs_common_get_prefs()->work_offline &&
250                         !inc_offline_should_override(TRUE,
251                                         ngettext("Claws Mail needs network access in order "
252                                         "to update the feed.",
253                                         "Claws Mail needs network access in order "
254                                         "to update feeds.", 1))) {
255                 return;
256         }
257
258         /* Update feed, displaying errors if any. */
259         rssyl_update_feed(ritem, TRUE);
260 }
261
262 void rssyl_prop_cb(GtkAction *action, gpointer data)
263 {
264         FolderView *folderview = (FolderView*)data;
265         FolderItem *item;
266         RFolderItem *ritem;
267
268         item = folderview_get_selected_item(folderview);
269         g_return_if_fail(item != NULL);
270         g_return_if_fail(item->folder != NULL);
271
272         debug_print("RSSyl: rssyl_prop_cb() for '%s'\n", item->name);
273
274         ritem = (RFolderItem *)item;
275
276         rssyl_gtk_prop(ritem);
277 }
278
279 void rssyl_update_all_cb( GtkAction *action, gpointer data)
280 {
281         FolderItem *item;
282         FolderView *folderview = (FolderView*)data;
283
284         item = folderview_get_selected_item(folderview);
285         g_return_if_fail(item != NULL);
286         g_return_if_fail(item->folder != NULL);
287
288         debug_print("RSSyl: rssyl_update_all_cb(): clicked on '%s'\n", item->name);
289
290         if( item->folder->klass != rssyl_folder_get_class() ) {
291                 debug_print("RSSyl: this is not a RSSyl folder, returning\n");
292                 return;
293         }
294
295         rssyl_update_recursively(item);
296 }
297
298 void rssyl_remove_mailbox_cb(GtkAction *action, gpointer data)
299 {
300         FolderView *folderview = (FolderView *)data;
301         FolderItem *item = NULL;
302         gchar *n, *message;
303         AlertValue avalue;
304
305         item = folderview_get_selected_item(folderview);
306
307         g_return_if_fail(item != NULL);
308         g_return_if_fail(item->folder != NULL);
309
310         if( folder_item_parent(item) )
311                 return;
312
313         n = trim_string(item->folder->name, 32);
314         message = g_strdup_printf(_("Really remove the feed tree `%s' ?\n"), n);
315         avalue = alertpanel_full(_("Remove feed tree"), message,
316                                  GTK_STOCK_CANCEL, _("_Remove"), NULL, FALSE,
317                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
318         g_free(message);
319         g_free(n);
320
321         if( avalue != G_ALERTALTERNATE )
322                 return;
323
324         folderview_unselect(folderview);
325         summary_clear_all(folderview->summaryview);
326
327         n = folder_item_get_path(item);
328         if( remove_dir_recursive(n) < 0 ) {
329                 g_warning("can't remove directory '%s'", n);
330                 g_free(n);
331                 return;
332         }
333
334         g_free(n);
335         folder_destroy(item->folder);
336 }
337
338 void rssyl_import_feed_list_cb(GtkAction *action, gpointer data)
339 {
340         FolderView *folderview = (FolderView *)data;
341         FolderItem *item = NULL;
342         gchar *path = NULL;
343         OPMLImportCtx *ctx = NULL;
344
345         debug_print("RSSyl: import_feed_list_cb\n");
346
347         /* Ask user for a file to import */
348         path = filesel_select_file_open_with_filter(
349                         _("Select an OPML file"), NULL, "*.opml");
350         if (!is_file_exist(path)) {
351                 g_free(path);
352                 return;
353         }
354
355         /* Find the destination folder for the import */
356         g_return_if_fail(folderview->selected != NULL);
357         item = folderview_get_selected_item(folderview);
358         g_return_if_fail(item != NULL);
359         g_return_if_fail(item->folder != NULL);
360
361         ctx = malloc( sizeof(OPMLImportCtx) );
362         ctx->failures = 0;
363         ctx->depth = rssyl_folder_depth(item) + 1;
364         ctx->current = NULL;
365         ctx->current = g_slist_append(ctx->current, item);
366
367         /* Start the whole shebang - call libfeed's OPML parser with correct
368          * user function */
369         opml_process(path, rssyl_opml_import_func, (gpointer)ctx);
370
371         g_free(ctx);
372 }