0ca7336e049c14e7e12fcceb2233753460b55ecb
[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
202         if (!folder_local_name_ok(basename)) {
203                 g_free(path);
204                 g_free(basename);
205                 return;
206         }
207
208         folder = folder_new(folder_get_class_from_string("mailmbox"), 
209                             !strcmp(path, "Mail") ? _("Mailbox") : basename,
210                             path);
211         g_free(basename);
212         g_free(path);
213
214         if (folder->klass->create_tree(folder) < 0) {
215                 alertpanel_error(_("Creation of the mailbox failed.\n"
216                                    "Maybe some files already exist, or you don't have the permission to write there."));
217                 folder_destroy(folder);
218                 return;
219         }
220
221         folder_add(folder);
222         folder_scan_tree(folder, TRUE);
223
224         folderview_set(mainwin->folderview);
225
226         return;
227 }
228
229 static void new_folder_cb(GtkAction *action, gpointer data)
230 {
231         FolderView *folderview = (FolderView *)data;
232         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
233         FolderItem *item;
234         FolderItem *new_item;
235         gchar *new_folder;
236         gchar *name;
237         gchar *p;
238
239         if (!folderview->selected) return;
240
241         item = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
242         g_return_if_fail(item != NULL);
243         g_return_if_fail(item->folder != NULL);
244
245         new_folder = input_dialog(_("New folder"),
246                                   _("Input the name of new folder:"),
247                                   _("NewFolder"));
248         if (!new_folder) return;
249         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
250
251         p = strchr(new_folder, G_DIR_SEPARATOR);
252         if (p == NULL)
253                 p = strchr(new_folder, '.');
254         if (p) {
255                 alertpanel_error(_("'%c' can't be included in folder name."),
256                                  p[0]);
257                 return;
258         }
259
260         if (!folder_local_name_ok(new_folder))
261                 return;
262
263         name = trim_string(new_folder, 32);
264         AUTORELEASE_STR(name, {g_free(name); return;});
265
266         /* find whether the directory already exists */
267         p = g_strconcat(item->path ? item->path : "", ".", new_folder, NULL);
268         if (folder_find_child_item_by_name(item, p)) {
269                 g_free(p);
270                 alertpanel_error(_("The folder '%s' already exists."), name);
271                 return;
272         }
273         g_free(p);
274
275         new_item = folder_create_folder(item, new_folder);
276         if (!new_item) {
277                 alertpanel_error(_("Can't create the folder '%s'."), name);
278                 return;
279         }
280
281         folder_write_list();
282 }
283
284 static void remove_mailbox_cb(GtkAction *action, gpointer data)
285 {
286         FolderView *folderview = (FolderView *)data;
287         FolderItem *item;
288         gchar *name;
289         gchar *message;
290         AlertValue avalue;
291
292         item = folderview_get_selected_item(folderview);
293         g_return_if_fail(item != NULL);
294         g_return_if_fail(item->folder != NULL);
295         if (folder_item_parent(item)) return;
296
297         name = trim_string(item->folder->name, 32);
298         message = g_strdup_printf
299                 (_("Really remove the mailbox '%s'?\n"
300                    "(The messages are NOT deleted from the disk)"), name);
301         avalue = alertpanel_full(_("Remove mailbox"), message,
302                                  GTK_STOCK_CANCEL, _("_Remove"), NULL, FALSE,
303                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
304         g_free(message);
305         g_free(name);
306         if (avalue != G_ALERTALTERNATE) return;
307
308         folderview_unselect(folderview);
309         summary_clear_all(folderview->summaryview);
310
311         folder_destroy(item->folder);
312 }
313
314 static void delete_folder_cb(GtkAction *action, gpointer data)
315 {
316         FolderView *folderview = (FolderView *)data;
317         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
318         FolderItem *item;
319         gchar *message, *name;
320         AlertValue avalue;
321         gchar *old_id;
322
323         item = folderview_get_selected_item(folderview);
324         g_return_if_fail(item != NULL);
325         g_return_if_fail(item->path != NULL);
326         g_return_if_fail(item->folder != NULL);
327
328         name = trim_string(item->name, 32);
329         AUTORELEASE_STR(name, {g_free(name); return;});
330         message = g_strdup_printf
331                 (_("All folder(s) and message(s) under '%s' will be deleted.\n"
332                    "Do you really want to delete?"), name);
333         avalue = alertpanel_full(_("Delete folder"), message,
334                                  GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
335                                  NULL, ALERT_NOTICE, G_ALERTDEFAULT);
336         g_free(message);
337         if (avalue != G_ALERTALTERNATE) return;
338
339         old_id = folder_item_get_identifier(item);
340
341         if (folderview->opened == folderview->selected ||
342             gtk_cmctree_is_ancestor(ctree,
343                                   folderview->selected,
344                                   folderview->opened)) {
345                 summary_clear_all(folderview->summaryview);
346                 folderview->opened = NULL;
347         }
348
349         if (item->folder->klass->remove_folder(item->folder, item) < 0) {
350                 alertpanel_error(_("Can't remove the folder '%s'."), name);
351                 if (folderview->opened == folderview->selected)
352                         summary_show(folderview->summaryview,
353                                      folderview->summaryview->folder_item);
354                 g_free(old_id);
355                 return;
356         }
357
358         folder_write_list();
359
360         prefs_filtering_delete_path(old_id);
361         g_free(old_id);
362
363 }
364
365 static void move_folder_cb(GtkAction *action, gpointer data)
366 {
367         FolderView *folderview = (FolderView *)data;
368         FolderItem *from_folder = NULL, *to_folder = NULL;
369
370         from_folder = folderview_get_selected_item(folderview);
371         if (!from_folder || from_folder->folder->klass != claws_mailmbox_get_class())
372                 return;
373
374         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL, FALSE);
375         if (!to_folder)
376                 return;
377
378         folderview_move_folder(folderview, from_folder, to_folder, 0);
379 }
380
381 static void copy_folder_cb(GtkAction *action, gpointer data)
382 {
383         FolderView *folderview = (FolderView *)data;
384         FolderItem *from_folder = NULL, *to_folder = NULL;
385
386         from_folder = folderview_get_selected_item(folderview);
387         if (!from_folder || from_folder->folder->klass != claws_mailmbox_get_class())
388                 return;
389
390         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL, FALSE);
391         if (!to_folder)
392                 return;
393
394         folderview_move_folder(folderview, from_folder, to_folder, 1);
395 }
396
397 static void rename_folder_cb(GtkAction *action, gpointer data)
398 {
399         FolderView *folderview = (FolderView *)data;
400         FolderItem *item, *parent;
401         gchar *new_folder;
402         gchar *name;
403         gchar *message;
404         gchar *old_id;
405         gchar *new_id;
406         gchar *p;
407
408         item = folderview_get_selected_item(folderview);
409         g_return_if_fail(item != NULL);
410         g_return_if_fail(item->path != NULL);
411         g_return_if_fail(item->folder != NULL);
412
413         name = trim_string(item->name, 32);
414         message = g_strdup_printf(_("Input new name for '%s':"), name);
415         new_folder = input_dialog(_("Rename folder"), message, item->name);
416         g_free(message);
417         g_free(name);
418         if (!new_folder) return;
419         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
420
421         p = strchr(new_folder, G_DIR_SEPARATOR);
422         if (p == NULL)
423                 p = strchr(new_folder, '.');
424         if (p) {
425                 alertpanel_error(_("'%c' can't be included in folder name."),
426                                  p[0]);
427                 return;
428         }
429
430         if (!folder_local_name_ok(new_folder))
431                 return;
432
433         parent = folder_item_parent(item);
434         p = g_strconcat(parent->path ? parent->path : "", ".", new_folder, NULL);
435         if (folder_find_child_item_by_name(parent, p)) {
436                 name = trim_string(new_folder, 32);
437                 alertpanel_error(_("The folder '%s' already exists."), name);
438                 g_free(name);
439                 return;
440         }
441
442         old_id = folder_item_get_identifier(item);
443
444         if (folder_item_rename(item, new_folder) < 0) {
445                 alertpanel_error(_("The folder could not be renamed.\n"
446                                    "The new folder name is not allowed."));
447                 g_free(old_id);
448                 return;
449         }
450
451         new_id = folder_item_get_identifier(item);
452         prefs_filtering_rename_path(old_id, new_id);
453         account_rename_path(old_id, new_id);
454         prefs_actions_rename_path(old_id, new_id);
455
456         g_free(old_id);
457         g_free(new_id);
458
459         folder_item_prefs_save_config_recursive(item);
460         folder_write_list();
461 }
462