Clean mailmbox plugin warnings
[claws.git] / src / plugins / mailmbox / plugin_gtk.c
1 /*
2  * mailmbox Plugin -- mbox support for Sylpheed
3  * Copyright (C) 2003-2005 Christoph Hohmann, 
4  *                         Hoa v. Dinh, 
5  *                         Alfons Hoogervorst
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #  include "claws-features.h"
26 #endif
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30
31 #include <gtk/gtk.h>
32
33 #include "plugin.h"
34 #include "folder.h"
35 #include "mailmbox_folder.h"
36 #include "mainwindow.h"
37 #include "folderview.h"
38 #include "inputdialog.h"
39 #include "foldersel.h"
40 #include "alertpanel.h"
41 #include "main.h"
42 #include "menu.h"
43 #include "account.h"
44 #include "prefs_actions.h"
45 #include "summaryview.h"
46 #include "folder_item_prefs.h"
47
48 static void add_mailbox(GtkAction *action, gpointer callback_data);
49 static void new_folder_cb(GtkAction *action, gpointer data);
50 static void delete_folder_cb(GtkAction *action, gpointer data);
51 static void rename_folder_cb(GtkAction *action, gpointer data);
52 static void move_folder_cb(GtkAction *action, gpointer data);
53 static void copy_folder_cb(GtkAction *action, gpointer data);
54 static void update_tree_cb(GtkAction *action, gpointer data);
55 static void remove_mailbox_cb(GtkAction *action, gpointer data);
56
57 static GtkActionEntry claws_mailmbox_popup_entries[] = 
58 {
59         {"FolderViewPopup/CreateNewFolder",     NULL, N_("Create _new folder..."), NULL, NULL, G_CALLBACK(new_folder_cb) },
60         {"FolderViewPopup/RenameFolder",        NULL, N_("_Rename folder..."), NULL, NULL, G_CALLBACK(rename_folder_cb) },
61         {"FolderViewPopup/MoveFolder",          NULL, N_("M_ove folder..."), NULL, NULL, G_CALLBACK(move_folder_cb) },
62         {"FolderViewPopup/CopyFolder",          NULL, N_("Cop_y folder..."), NULL, NULL, G_CALLBACK(copy_folder_cb) },
63         {"FolderViewPopup/DeleteFolder",        NULL, N_("_Delete folder..."), NULL, NULL, G_CALLBACK(delete_folder_cb) },
64         {"FolderViewPopup/CheckNewMessages",    NULL, N_("_Check for new messages"), NULL, NULL, G_CALLBACK(update_tree_cb) }, /*0*/
65         {"FolderViewPopup/CheckNewFolders",     NULL, N_("C_heck for new folders"), NULL, NULL, G_CALLBACK(update_tree_cb) }, /*1*/
66         {"FolderViewPopup/RebuildTree",         NULL, N_("R_ebuild folder tree"), NULL, NULL, G_CALLBACK(update_tree_cb) }, /*2*/
67         {"FolderViewPopup/RemoveMailbox",       NULL, N_("Remove _mailbox..."), NULL, NULL, G_CALLBACK(remove_mailbox_cb) },
68 };                      
69 static void set_sensitivity(GtkUIManager *ui_manager, FolderItem *item);
70 static void add_menuitems(GtkUIManager *ui_manager, FolderItem *item);
71
72 static FolderViewPopup claws_mailmbox_popup =
73 {
74         "mailmbox",
75         "<MailmboxFolder>",
76         claws_mailmbox_popup_entries,
77         G_N_ELEMENTS(claws_mailmbox_popup_entries),
78         NULL, 0,
79         NULL, 0, 0, NULL,
80         add_menuitems,
81         set_sensitivity
82 };
83
84 static GtkActionEntry mainwindow_add_mailbox[] = {{
85         "File/AddMailbox/Mbox",
86         NULL, N_("mbox (etPan!)..."), NULL, NULL, G_CALLBACK(add_mailbox)
87 }};
88
89 static guint main_menu_id = 0;
90
91 gint plugin_gtk_init(gchar **error)
92 {
93         MainWindow *mainwin = mainwindow_get_mainwindow();
94
95         folderview_register_popup(&claws_mailmbox_popup);
96
97         gtk_action_group_add_actions(mainwin->action_group, mainwindow_add_mailbox,
98                         1, (gpointer)mainwin);
99         MENUITEM_ADDUI_ID_MANAGER(mainwin->ui_manager, "/Menu/File/AddMailbox", "Mbox", 
100                           "File/AddMailbox/Mbox", GTK_UI_MANAGER_MENUITEM,
101                           main_menu_id)
102
103         return 0;
104 }
105
106 void plugin_gtk_done(void)
107 {
108         MainWindow *mainwin = mainwindow_get_mainwindow();
109         
110         if (mainwin == NULL || claws_is_exiting())
111                 return;
112
113         folderview_unregister_popup(&claws_mailmbox_popup);
114
115         MENUITEM_REMUI_MANAGER(mainwin->ui_manager,mainwin->action_group, "File/AddMailbox/RSSyl", main_menu_id);
116         main_menu_id = 0;
117 }
118
119 static void add_menuitems(GtkUIManager *ui_manager, FolderItem *item)
120 {
121         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "CreateNewFolder", "FolderViewPopup/CreateNewFolder", GTK_UI_MANAGER_MENUITEM)
122         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorMbox1", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
123         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RenameFolder", "FolderViewPopup/RenameFolder", GTK_UI_MANAGER_MENUITEM)
124         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "MoveFolder", "FolderViewPopup/MoveFolder", GTK_UI_MANAGER_MENUITEM)
125         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "CopyFolder", "FolderViewPopup/CopyFolder", GTK_UI_MANAGER_MENUITEM)
126         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorMbox2", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
127         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "DeleteFolder", "FolderViewPopup/DeleteFolder", GTK_UI_MANAGER_MENUITEM)
128         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorMbox3", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
129         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "CheckNewMessages", "FolderViewPopup/CheckNewMessages", GTK_UI_MANAGER_MENUITEM)
130         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "CheckNewFolders", "FolderViewPopup/CheckNewFolders", GTK_UI_MANAGER_MENUITEM)
131         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RebuildTree", "FolderViewPopup/RebuildTree", GTK_UI_MANAGER_MENUITEM)
132         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorMbox4", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
133         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RemoveMailbox", "FolderViewPopup/RemoveMailbox", GTK_UI_MANAGER_MENUITEM)
134         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorMbox5", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
135 }
136
137 static void set_sensitivity(GtkUIManager *ui_manager, FolderItem *item)
138 {
139         gboolean folder_is_normal = 
140                         item != NULL &&
141                         item->stype == F_NORMAL &&
142                         !folder_has_parent_of_type(item, F_OUTBOX) &&
143                         !folder_has_parent_of_type(item, F_DRAFT) &&
144                         !folder_has_parent_of_type(item, F_QUEUE) &&
145                         !folder_has_parent_of_type(item, F_TRASH);
146 #define SET_SENS(name, sens) \
147         cm_menu_set_sensitive_full(ui_manager, "Popup/"name, sens)
148
149         SET_SENS("FolderViewPopup/CreateNewFolder",   item->stype != F_INBOX);
150         SET_SENS("FolderViewPopup/RenameFolder",       item->stype == F_NORMAL && folder_item_parent(item) != NULL);
151         SET_SENS("FolderViewPopup/MoveFolder",      folder_is_normal && folder_item_parent(item) != NULL);
152         SET_SENS("FolderViewPopup/DeleteFolder",            item->stype == F_NORMAL && folder_item_parent(item) != NULL);
153
154         SET_SENS("FolderViewPopup/CheckNewMessages", folder_item_parent(item) == NULL);
155         SET_SENS("FolderViewPopup/CheckNewFolders",  folder_item_parent(item) == NULL);
156         SET_SENS("FolderViewPopup/RebuildTree",    folder_item_parent(item) == NULL);
157
158         SET_SENS("FolderViewPopup/RemoveMailbox",         folder_item_parent(item) == NULL);
159
160 #undef SET_SENS
161 }
162
163 #define DO_ACTION(name, act)    { if (!strcmp(a_name, name)) act; }
164
165 static void update_tree_cb(GtkAction *action, gpointer data)
166 {
167         FolderView *folderview = (FolderView *)data;
168         FolderItem *item;
169         const gchar *a_name = gtk_action_get_name(action);
170
171         item = folderview_get_selected_item(folderview);
172         g_return_if_fail(item != NULL);
173
174         summary_show(folderview->summaryview, NULL);
175
176         g_return_if_fail(item->folder != NULL);
177
178         DO_ACTION("FolderViewPopup/CheckNewMessages", folderview_check_new(item->folder));
179         DO_ACTION("FolderViewPopup/CheckNewFolders", folderview_rescan_tree(item->folder, FALSE));
180         DO_ACTION("FolderViewPopup/RebuildTree", folderview_rescan_tree(item->folder, TRUE));
181 }
182
183 static void add_mailbox(GtkAction *action, gpointer callback_data)
184 {
185         MainWindow *mainwin = (MainWindow *) callback_data;
186         gchar *path, *basename;
187         Folder *folder;
188
189         path = input_dialog(_("Add mailbox"),
190                             _("Input the location of mailbox.\n"
191                               "If the existing mailbox is specified, it will be\n"
192                               "scanned automatically."),
193                             "Mail");
194         if (!path) return;
195         if (folder_find_from_path(path)) {
196                 alertpanel_error(_("The mailbox '%s' already exists."), path);
197                 g_free(path);
198                 return;
199         }
200         basename = g_path_get_basename(path);
201         folder = folder_new(folder_get_class_from_string("mailmbox"), 
202                             !strcmp(path, "Mail") ? _("Mailbox") : basename,
203                             path);
204         g_free(basename);                           
205         g_free(path);
206
207
208         if (folder->klass->create_tree(folder) < 0) {
209                 alertpanel_error(_("Creation of the mailbox failed.\n"
210                                    "Maybe some files already exist, or you don't have the permission to write there."));
211                 folder_destroy(folder);
212                 return;
213         }
214
215         folder_add(folder);
216         folder_scan_tree(folder, TRUE);
217
218         folderview_set(mainwin->folderview);
219
220         return;
221 }
222
223 static void new_folder_cb(GtkAction *action, gpointer data)
224 {
225         FolderView *folderview = (FolderView *)data;
226         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
227         FolderItem *item;
228         FolderItem *new_item;
229         gchar *new_folder;
230         gchar *name;
231         gchar *p;
232
233         if (!folderview->selected) return;
234
235         item = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
236         g_return_if_fail(item != NULL);
237         g_return_if_fail(item->folder != NULL);
238
239         new_folder = input_dialog(_("New folder"),
240                                   _("Input the name of new folder:"),
241                                   _("NewFolder"));
242         if (!new_folder) return;
243         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
244
245         p = strchr(new_folder, G_DIR_SEPARATOR);
246         if (p == NULL)
247                 p = strchr(new_folder, '.');
248         if (p) {
249                 alertpanel_error(_("'%c' can't be included in folder name."),
250                                  p[0]);
251                 return;
252         }
253
254         name = trim_string(new_folder, 32);
255         AUTORELEASE_STR(name, {g_free(name); return;});
256
257         /* find whether the directory already exists */
258         p = g_strconcat(item->path ? item->path : "", ".", new_folder, NULL);
259         if (folder_find_child_item_by_name(item, p)) {
260                 g_free(p);
261                 alertpanel_error(_("The folder '%s' already exists."), name);
262                 return;
263         }
264         g_free(p);
265
266         new_item = folder_create_folder(item, new_folder);
267         if (!new_item) {
268                 alertpanel_error(_("Can't create the folder '%s'."), name);
269                 return;
270         }
271
272         folder_write_list();
273 }
274
275 static void remove_mailbox_cb(GtkAction *action, gpointer data)
276 {
277         FolderView *folderview = (FolderView *)data;
278         FolderItem *item;
279         gchar *name;
280         gchar *message;
281         AlertValue avalue;
282
283         item = folderview_get_selected_item(folderview);
284         g_return_if_fail(item != NULL);
285         g_return_if_fail(item->folder != NULL);
286         if (folder_item_parent(item)) return;
287
288         name = trim_string(item->folder->name, 32);
289         message = g_strdup_printf
290                 (_("Really remove the mailbox '%s'?\n"
291                    "(The messages are NOT deleted from the disk)"), name);
292         avalue = alertpanel_full(_("Remove mailbox"), message,
293                                  GTK_STOCK_CANCEL, _("_Remove"), NULL, FALSE,
294                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
295         g_free(message);
296         g_free(name);
297         if (avalue != G_ALERTALTERNATE) return;
298
299         folderview_unselect(folderview);
300         summary_clear_all(folderview->summaryview);
301
302         folder_destroy(item->folder);
303 }
304
305 static void delete_folder_cb(GtkAction *action, gpointer data)
306 {
307         FolderView *folderview = (FolderView *)data;
308         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
309         FolderItem *item;
310         gchar *message, *name;
311         AlertValue avalue;
312         gchar *old_id;
313
314         item = folderview_get_selected_item(folderview);
315         g_return_if_fail(item != NULL);
316         g_return_if_fail(item->path != NULL);
317         g_return_if_fail(item->folder != NULL);
318
319         name = trim_string(item->name, 32);
320         AUTORELEASE_STR(name, {g_free(name); return;});
321         message = g_strdup_printf
322                 (_("All folder(s) and message(s) under '%s' will be deleted.\n"
323                    "Do you really want to delete?"), name);
324         avalue = alertpanel_full(_("Delete folder"), message,
325                                  GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
326                                  NULL, ALERT_NOTICE, G_ALERTDEFAULT);
327         g_free(message);
328         if (avalue != G_ALERTALTERNATE) return;
329
330         old_id = folder_item_get_identifier(item);
331
332         if (folderview->opened == folderview->selected ||
333             gtk_cmctree_is_ancestor(ctree,
334                                   folderview->selected,
335                                   folderview->opened)) {
336                 summary_clear_all(folderview->summaryview);
337                 folderview->opened = NULL;
338         }
339
340         if (item->folder->klass->remove_folder(item->folder, item) < 0) {
341                 alertpanel_error(_("Can't remove the folder '%s'."), name);
342                 if (folderview->opened == folderview->selected)
343                         summary_show(folderview->summaryview,
344                                      folderview->summaryview->folder_item);
345                 g_free(old_id);
346                 return;
347         }
348
349         folder_write_list();
350
351         prefs_filtering_delete_path(old_id);
352         g_free(old_id);
353
354 }
355
356 static void move_folder_cb(GtkAction *action, gpointer data)
357 {
358         FolderView *folderview = (FolderView *)data;
359         FolderItem *from_folder = NULL, *to_folder = NULL;
360
361         from_folder = folderview_get_selected_item(folderview);
362         if (!from_folder || from_folder->folder->klass != claws_mailmbox_get_class())
363                 return;
364
365         to_folder = foldersel_folder_sel(from_folder->folder, FOLDER_SEL_MOVE, NULL, FALSE);
366         if (!to_folder)
367                 return;
368
369         folderview_move_folder(folderview, from_folder, to_folder, 0);
370 }
371
372 static void copy_folder_cb(GtkAction *action, gpointer data)
373 {
374         FolderView *folderview = (FolderView *)data;
375         FolderItem *from_folder = NULL, *to_folder = NULL;
376
377         from_folder = folderview_get_selected_item(folderview);
378         if (!from_folder || from_folder->folder->klass != claws_mailmbox_get_class())
379                 return;
380
381         to_folder = foldersel_folder_sel(from_folder->folder, FOLDER_SEL_MOVE, NULL, FALSE);
382         if (!to_folder)
383                 return;
384
385         folderview_move_folder(folderview, from_folder, to_folder, 1);
386 }
387
388 static void rename_folder_cb(GtkAction *action, gpointer data)
389 {
390         FolderView *folderview = (FolderView *)data;
391         FolderItem *item, *parent;
392         gchar *new_folder;
393         gchar *name;
394         gchar *message;
395         gchar *old_id;
396         gchar *new_id;
397         gchar *p;
398
399         item = folderview_get_selected_item(folderview);
400         g_return_if_fail(item != NULL);
401         g_return_if_fail(item->path != NULL);
402         g_return_if_fail(item->folder != NULL);
403
404         name = trim_string(item->name, 32);
405         message = g_strdup_printf(_("Input new name for '%s':"), name);
406         new_folder = input_dialog(_("Rename folder"), message, item->name);
407         g_free(message);
408         g_free(name);
409         if (!new_folder) return;
410         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
411
412         p = strchr(new_folder, G_DIR_SEPARATOR);
413         if (p == NULL)
414                 p = strchr(new_folder, '.');
415         if (p) {
416                 alertpanel_error(_("'%c' can't be included in folder name."),
417                                  p[0]);
418                 return;
419         }
420
421         parent = folder_item_parent(item);
422         p = g_strconcat(parent->path ? parent->path : "", ".", new_folder, NULL);
423         if (folder_find_child_item_by_name(parent, p)) {
424                 name = trim_string(new_folder, 32);
425                 alertpanel_error(_("The folder '%s' already exists."), name);
426                 g_free(name);
427                 return;
428         }
429
430         old_id = folder_item_get_identifier(item);
431
432         if (folder_item_rename(item, new_folder) < 0) {
433                 alertpanel_error(_("The folder could not be renamed.\n"
434                                    "The new folder name is not allowed."));
435                 g_free(old_id);
436                 return;
437         }
438
439         new_id = folder_item_get_identifier(item);
440         prefs_filtering_rename_path(old_id, new_id);
441         account_rename_path(old_id, new_id);
442         prefs_actions_rename_path(old_id, new_id);
443
444         g_free(old_id);
445         g_free(new_id);
446
447         folder_item_prefs_save_config_recursive(item);
448         folder_write_list();
449 }
450