5b937b1c27bbec48b2fd596addce701c1f7d0ec2
[claws.git] / src / mh_gtk.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto & the Claws Mail Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include "defs.h"
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29
30 #include <gtk/gtk.h>
31
32 #include "utils.h"
33 #include "folder.h"
34 #include "folder_item_prefs.h"
35 #include "folderview.h"
36 #include "menu.h"
37 #include "account.h"
38 #include "alertpanel.h"
39 #include "inputdialog.h"
40 #include "mh.h"
41 #include "foldersel.h"
42 #include "prefs_common.h"
43 #include "prefs_actions.h"
44
45 static void new_folder_cb(GtkAction *action, gpointer data);
46 static void delete_folder_cb(GtkAction *action, gpointer data);
47 static void rename_folder_cb(GtkAction *action, gpointer data);
48 static void move_folder_cb(GtkAction *action, gpointer data);
49 static void copy_folder_cb(GtkAction *action, gpointer data);
50 static void update_tree_cb(GtkAction *action, gpointer data);
51 static void remove_mailbox_cb(GtkAction *action, gpointer data);
52
53 static GtkActionEntry mh_popup_entries[] = 
54 {
55         {"FolderViewPopup/CreateNewFolder",     NULL, N_("Create _new folder..."), NULL, NULL, G_CALLBACK(new_folder_cb) },
56         {"FolderViewPopup/RenameFolder",        NULL, N_("_Rename folder..."), NULL, NULL, G_CALLBACK(rename_folder_cb) },
57         {"FolderViewPopup/MoveFolder",          NULL, N_("M_ove folder..."), NULL, NULL, G_CALLBACK(move_folder_cb) },
58         {"FolderViewPopup/CopyFolder",          NULL, N_("Cop_y folder..."), NULL, NULL, G_CALLBACK(copy_folder_cb) },
59         {"FolderViewPopup/DeleteFolder",        NULL, N_("_Delete folder..."), NULL, NULL, G_CALLBACK(delete_folder_cb) },
60         {"FolderViewPopup/CheckNewMessages",    NULL, N_("_Check for new messages"), NULL, NULL, G_CALLBACK(update_tree_cb) }, /*0*/
61         {"FolderViewPopup/CheckNewFolders",     NULL, N_("C_heck for new folders"), NULL, NULL, G_CALLBACK(update_tree_cb) }, /*1*/
62         {"FolderViewPopup/RebuildTree",         NULL, N_("R_ebuild folder tree"), NULL, NULL, G_CALLBACK(update_tree_cb) }, /*2*/
63         {"FolderViewPopup/RemoveMailbox",       NULL, N_("Remove _mailbox..."), NULL, NULL, G_CALLBACK(remove_mailbox_cb) },
64 };                      
65 static void set_sensitivity(GtkUIManager *ui_manager, FolderItem *item);
66 static void add_menuitems(GtkUIManager *ui_manager, FolderItem *item);
67
68 static FolderViewPopup mh_popup =
69 {
70         "mh",
71         "<MHFolder>",
72         mh_popup_entries,
73         G_N_ELEMENTS(mh_popup_entries),
74         NULL, 0,
75         NULL, 0, 0, NULL,
76         add_menuitems,
77         set_sensitivity
78 };
79
80 void mh_gtk_init(void)
81 {
82         folderview_register_popup(&mh_popup);
83 }
84
85 static void add_menuitems(GtkUIManager *ui_manager, FolderItem *item)
86 {
87         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "CreateNewFolder", "FolderViewPopup/CreateNewFolder", GTK_UI_MANAGER_MENUITEM)
88         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorMH1", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
89         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RenameFolder", "FolderViewPopup/RenameFolder", GTK_UI_MANAGER_MENUITEM)
90         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "MoveFolder", "FolderViewPopup/MoveFolder", GTK_UI_MANAGER_MENUITEM)
91         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "CopyFolder", "FolderViewPopup/CopyFolder", GTK_UI_MANAGER_MENUITEM)
92         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorMH2", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
93         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "DeleteFolder", "FolderViewPopup/DeleteFolder", GTK_UI_MANAGER_MENUITEM)
94         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorMH3", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
95         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "CheckNewMessages", "FolderViewPopup/CheckNewMessages", GTK_UI_MANAGER_MENUITEM)
96         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "CheckNewFolders", "FolderViewPopup/CheckNewFolders", GTK_UI_MANAGER_MENUITEM)
97         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RebuildTree", "FolderViewPopup/RebuildTree", GTK_UI_MANAGER_MENUITEM)
98         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorMH4", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
99         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RemoveMailbox", "FolderViewPopup/RemoveMailbox", GTK_UI_MANAGER_MENUITEM)
100         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorMH5", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
101 }
102
103 static void set_sensitivity(GtkUIManager *ui_manager, FolderItem *item)
104 {
105         gboolean folder_is_normal = 
106                         item != NULL &&
107                         item->stype == F_NORMAL &&
108                         !folder_has_parent_of_type(item, F_OUTBOX) &&
109                         !folder_has_parent_of_type(item, F_DRAFT) &&
110                         !folder_has_parent_of_type(item, F_QUEUE) &&
111                         !folder_has_parent_of_type(item, F_TRASH);
112 #define SET_SENS(name, sens) \
113         cm_menu_set_sensitive_full(ui_manager, "Popup/"name, sens)
114
115         SET_SENS("FolderViewPopup/CreateNewFolder",   TRUE);
116         SET_SENS("FolderViewPopup/RenameFolder",       item && item->stype == F_NORMAL && folder_item_parent(item) != NULL);
117         SET_SENS("FolderViewPopup/MoveFolder",      folder_is_normal && folder_item_parent(item) != NULL);
118         SET_SENS("FolderViewPopup/DeleteFolder",            item && item->stype == F_NORMAL && folder_item_parent(item) != NULL);
119
120         SET_SENS("FolderViewPopup/CheckNewMessages", folder_item_parent(item) == NULL);
121         SET_SENS("FolderViewPopup/CheckNewFolders",  folder_item_parent(item) == NULL);
122         SET_SENS("FolderViewPopup/RebuildTree",    folder_item_parent(item) == NULL);
123
124         SET_SENS("FolderViewPopup/RemoveMailbox",         folder_item_parent(item) == NULL);
125
126 #undef SET_SENS
127 }
128
129 static void new_folder_cb(GtkAction *action, gpointer data)
130 {
131         FolderView *folderview = (FolderView *)data;
132         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
133         FolderItem *item;
134         FolderItem *new_item;
135         gchar *new_folder;
136         gchar *name;
137         gchar *p;
138
139         if (!folderview->selected) return;
140
141         item = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
142         cm_return_if_fail(item != NULL);
143         cm_return_if_fail(item->folder != NULL);
144
145         new_folder = input_dialog_with_checkbtn(_("New folder"),
146                                                 _("Input the name of new folder:"),
147                                                 _("NewFolder"),
148                                                 _("Inherit properties from parent folder"),
149                                                 &(prefs_common.inherit_folder_props));
150         if (!new_folder) return;
151         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
152
153         p = strchr(new_folder, G_DIR_SEPARATOR);
154         if (p) {
155                 alertpanel_error(_("'%c' can't be included in folder name."),
156                                  G_DIR_SEPARATOR);
157                 return;
158         }
159
160         name = trim_string(new_folder, 32);
161         AUTORELEASE_STR(name, {g_free(name); return;});
162
163         /* find whether the directory already exists */
164         if (folder_find_child_item_by_name(item, new_folder)) {
165                 alertpanel_error(_("The folder '%s' already exists."), name);
166                 return;
167         }
168
169         new_item = folder_create_folder(item, new_folder);
170         if (!new_item) {
171                 alertpanel_error(_("Can't create the folder '%s'."), name);
172                 return;
173         }
174
175         if (prefs_common.inherit_folder_props) {
176                 folder_item_prefs_copy_prefs(item, new_item);
177         }
178
179         folder_write_list();
180 }
181
182 static void delete_folder_cb(GtkAction *action, gpointer data)
183 {
184         FolderView *folderview = (FolderView *)data;
185         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
186         FolderItem *item;
187         gchar *message, *name;
188         AlertValue avalue;
189         gchar *old_path;
190         gchar *old_id;
191
192         item = folderview_get_selected_item(folderview);
193         cm_return_if_fail(item != NULL);
194         cm_return_if_fail(item->path != NULL);
195         cm_return_if_fail(item->folder != NULL);
196
197         name = trim_string(item->name, 32);
198         AUTORELEASE_STR(name, {g_free(name); return;});
199         message = g_markup_printf_escaped
200                 (_("All folders and messages under '%s' will be permanently deleted. "
201                    "Recovery will not be possible.\n\n"
202                    "Do you really want to delete?"), name);
203         avalue = alertpanel_full(_("Delete folder"), message,
204                                  GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
205                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
206         g_free(message);
207         if (avalue != G_ALERTALTERNATE) return;
208
209         Xstrdup_a(old_path, item->path, return);
210         old_id = folder_item_get_identifier(item);
211
212         if (folderview->opened == folderview->selected ||
213             gtk_cmctree_is_ancestor(ctree,
214                                   folderview->selected,
215                                   folderview->opened)) {
216                 summary_clear_all(folderview->summaryview);
217                 folderview->opened = NULL;
218         }
219
220         if (item->folder->klass->remove_folder(item->folder, item) < 0) {
221                 folder_item_scan(item);
222                 alertpanel_error(_("Can't remove the folder '%s'."), name);
223                 g_free(old_id);
224                 return;
225         }
226
227         folder_write_list();
228
229         prefs_filtering_delete_path(old_id);
230         g_free(old_id);
231
232 }
233
234 static void rename_folder_cb(GtkAction *action, gpointer data)
235 {
236         FolderView *folderview = (FolderView *)data;
237         FolderItem *item;
238         gchar *new_folder;
239         gchar *name;
240         gchar *message;
241         gchar *old_path;
242         gchar *old_id;
243         gchar *new_id;
244         gchar *base;
245
246         item = folderview_get_selected_item(folderview);
247         cm_return_if_fail(item != NULL);
248         cm_return_if_fail(item->path != NULL);
249         cm_return_if_fail(item->folder != NULL);
250
251         name = trim_string(item->name, 32);
252         message = g_strdup_printf(_("Input new name for '%s':"), name);
253         base = g_path_get_basename(item->path);
254         new_folder = input_dialog(_("Rename folder"), message, base);
255         g_free(message);
256         g_free(name);
257         g_free(base);
258         if (!new_folder) return;
259         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
260
261         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
262                 alertpanel_error(_("'%c' can't be included in folder name."),
263                                  G_DIR_SEPARATOR);
264                 return;
265         }
266
267         if (folder_find_child_item_by_name(folder_item_parent(item), new_folder)) {
268                 name = trim_string(new_folder, 32);
269                 alertpanel_error(_("The folder '%s' already exists."), name);
270                 g_free(name);
271                 return;
272         }
273
274         Xstrdup_a(old_path, item->path, {g_free(new_folder); return;});
275
276         old_id = folder_item_get_identifier(item);
277         
278         if (folder_item_rename(item, new_folder) < 0) {
279                 alertpanel_error(_("The folder could not be renamed.\n"
280                                    "The new folder name is not allowed."));
281                 g_free(old_id);
282                 return;
283         }
284
285         new_id = folder_item_get_identifier(item);
286         prefs_filtering_rename_path(old_id, new_id);
287         account_rename_path(old_id, new_id);
288         prefs_actions_rename_path(old_id, new_id);
289         g_free(old_id);
290         g_free(new_id);
291
292         folder_item_prefs_save_config_recursive(item);
293         folder_write_list();
294 }
295
296 static void move_folder_cb(GtkAction *action, gpointer data)
297 {
298         FolderView *folderview = (FolderView *)data;
299         FolderItem *from_folder = NULL, *to_folder = NULL;
300
301         from_folder = folderview_get_selected_item(folderview);
302         if (!from_folder || from_folder->folder->klass != mh_get_class())
303                 return;
304
305         to_folder = foldersel_folder_sel(from_folder->folder, FOLDER_SEL_MOVE, NULL, TRUE);
306         if (!to_folder)
307                 return;
308         
309         folderview_move_folder(folderview, from_folder, to_folder, 0);
310 }
311
312 static void copy_folder_cb(GtkAction *action, gpointer data)
313 {
314         FolderView *folderview = (FolderView *)data;
315         FolderItem *from_folder = NULL, *to_folder = NULL;
316
317         from_folder = folderview_get_selected_item(folderview);
318         if (!from_folder || from_folder->folder->klass != mh_get_class())
319                 return;
320
321         to_folder = foldersel_folder_sel(from_folder->folder, FOLDER_SEL_MOVE, NULL, TRUE);
322         if (!to_folder)
323                 return;
324         
325         folderview_move_folder(folderview, from_folder, to_folder, 1);
326 }
327
328 #define DO_ACTION(name, act)    { if (!strcmp(a_name, name)) act; }
329
330 static void update_tree_cb(GtkAction *action, gpointer data)
331 {
332         FolderView *folderview = (FolderView *)data;
333         FolderItem *item;
334         const gchar *a_name = gtk_action_get_name(action);
335
336         item = folderview_get_selected_item(folderview);
337         cm_return_if_fail(item != NULL);
338
339         summary_show(folderview->summaryview, NULL);
340
341         cm_return_if_fail(item->folder != NULL);
342
343         DO_ACTION("FolderViewPopup/CheckNewMessages", folderview_check_new(item->folder));
344         DO_ACTION("FolderViewPopup/CheckNewFolders", folderview_rescan_tree(item->folder, FALSE));
345         DO_ACTION("FolderViewPopup/RebuildTree", folderview_rescan_tree(item->folder, TRUE));
346 }
347
348 static void remove_mailbox_cb(GtkAction *action, gpointer data)
349 {
350         FolderView *folderview = (FolderView *)data;
351         FolderItem *item;
352         gchar *name;
353         gchar *message;
354         AlertValue avalue;
355
356         item = folderview_get_selected_item(folderview);
357         cm_return_if_fail(item != NULL);
358         cm_return_if_fail(item->folder != NULL);
359         if (folder_item_parent(item)) return;
360
361         name = trim_string(item->folder->name, 32);
362         message = g_markup_printf_escaped
363                 (_("Really remove the mailbox '%s' ?\n"
364                    "(The messages are NOT deleted from the disk)"), name);
365         avalue = alertpanel_full(_("Remove mailbox"), message,
366                                  GTK_STOCK_CANCEL, _("_Remove"), NULL, FALSE,
367                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
368                             
369         g_free(message);
370         g_free(name);
371         if (avalue != G_ALERTALTERNATE) return;
372
373         folderview_unselect(folderview);
374         summary_clear_all(folderview->summaryview);
375
376         folder_destroy(item->folder);
377 }
378