f98cc1eaf4a0204c7a07e173e3bfe8e7867213e0
[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         FolderItem *item;
133         FolderItem *new_item;
134         gchar *new_folder;
135         gchar *name;
136         gchar *p;
137
138         if (!folderview->selected) return;
139
140         item = folderview_get_selected_item(folderview);
141         cm_return_if_fail(item != NULL);
142         cm_return_if_fail(item->folder != NULL);
143
144         new_folder = input_dialog_with_checkbtn(_("New folder"),
145                                                 _("Input the name of new folder:"),
146                                                 _("NewFolder"),
147                                                 _("Inherit properties from parent folder"),
148                                                 &(prefs_common.inherit_folder_props));
149         if (!new_folder) return;
150         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
151
152         p = strchr(new_folder, G_DIR_SEPARATOR);
153         if (p) {
154                 alertpanel_error(_("'%c' can't be included in folder name."),
155                                  G_DIR_SEPARATOR);
156                 return;
157         }
158
159         if (!folder_local_name_ok(new_folder))
160                 return;
161
162         name = trim_string(new_folder, 32);
163         AUTORELEASE_STR(name, {g_free(name); return;});
164
165         /* find whether the directory already exists */
166         if (folder_find_child_item_by_name(item, new_folder)) {
167                 alertpanel_error(_("The folder '%s' already exists."), name);
168                 return;
169         }
170
171         new_item = folder_create_folder(item, new_folder);
172         if (!new_item) {
173                 alertpanel_error(_("Can't create the folder '%s'."), name);
174                 return;
175         }
176
177         if (prefs_common.inherit_folder_props) {
178                 folder_item_prefs_copy_prefs(item, new_item);
179         }
180
181         folder_write_list();
182 }
183
184 static void delete_folder_cb(GtkAction *action, gpointer data)
185 {
186         FolderView *folderview = (FolderView *)data;
187         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
188         FolderItem *item;
189         gchar *message, *name;
190         AlertValue avalue;
191         gchar *old_id;
192         gint ret;
193
194         item = folderview_get_selected_item(folderview);
195         cm_return_if_fail(item != NULL);
196         cm_return_if_fail(item->path != NULL);
197         cm_return_if_fail(item->folder != NULL);
198
199         name = trim_string(item->name, 32);
200         AUTORELEASE_STR(name, {g_free(name); return;});
201         message = g_markup_printf_escaped
202                 (_("All folders and messages under '%s' will be permanently deleted. "
203                    "Recovery will not be possible.\n\n"
204                    "Do you really want to delete?"), name);
205         avalue = alertpanel_full(_("Delete folder"), message,
206                                  GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
207                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
208         g_free(message);
209         if (avalue != G_ALERTALTERNATE) return;
210
211         old_id = folder_item_get_identifier(item);
212
213         if (folderview->opened == folderview->selected ||
214             gtk_cmctree_is_ancestor(ctree,
215                                   folderview->selected,
216                                   folderview->opened)) {
217                 summary_clear_all(folderview->summaryview);
218                 folderview->opened = NULL;
219         }
220
221         if ((ret = item->folder->klass->remove_folder(item->folder, item)) < 0) {
222                 folder_item_scan(item);
223                 alertpanel_error(_("Can't remove the folder '%s'\n\n%s."),
224                                  name, g_strerror(-ret));
225                 g_free(old_id);
226                 return;
227         }
228
229         folder_write_list();
230
231         prefs_filtering_delete_path(old_id);
232         g_free(old_id);
233
234 }
235
236 static void rename_folder_cb(GtkAction *action, gpointer data)
237 {
238         FolderView *folderview = (FolderView *)data;
239         FolderItem *item;
240         gchar *new_folder;
241         gchar *name;
242         gchar *message;
243         gchar *old_id;
244         gchar *new_id;
245         gchar *base;
246
247         item = folderview_get_selected_item(folderview);
248         cm_return_if_fail(item != NULL);
249         cm_return_if_fail(item->path != NULL);
250         cm_return_if_fail(item->folder != NULL);
251
252         name = trim_string(item->name, 32);
253         message = g_strdup_printf(_("Input new name for '%s':"), name);
254         base = g_path_get_basename(item->path);
255         new_folder = input_dialog(_("Rename folder"), message, base);
256         g_free(message);
257         g_free(name);
258         g_free(base);
259         if (!new_folder) return;
260         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
261
262         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
263                 alertpanel_error(_("'%c' can't be included in folder name."),
264                                  G_DIR_SEPARATOR);
265                 return;
266         }
267
268         if (!folder_local_name_ok(new_folder))
269                 return;
270
271         if (folder_find_child_item_by_name(folder_item_parent(item), new_folder)) {
272                 name = trim_string(new_folder, 32);
273                 alertpanel_error(_("The folder '%s' already exists."), name);
274                 g_free(name);
275                 return;
276         }
277
278         old_id = folder_item_get_identifier(item);
279         
280         if (folder_item_rename(item, new_folder) < 0) {
281                 alertpanel_error(_("The folder could not be renamed.\n"
282                                    "The new folder name is not allowed."));
283                 g_free(old_id);
284                 return;
285         }
286
287         new_id = folder_item_get_identifier(item);
288         prefs_filtering_rename_path(old_id, new_id);
289         account_rename_path(old_id, new_id);
290         prefs_actions_rename_path(old_id, new_id);
291         g_free(old_id);
292         g_free(new_id);
293
294         folder_item_prefs_save_config_recursive(item);
295         folder_write_list();
296 }
297
298 static void move_folder_cb(GtkAction *action, gpointer data)
299 {
300         FolderView *folderview = (FolderView *)data;
301         FolderItem *from_folder = NULL, *to_folder = NULL;
302
303         from_folder = folderview_get_selected_item(folderview);
304         if (!from_folder || from_folder->folder->klass != mh_get_class())
305                 return;
306
307         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL, TRUE);
308         if (!to_folder)
309                 return;
310         
311         folderview_move_folder(folderview, from_folder, to_folder, 0);
312 }
313
314 static void copy_folder_cb(GtkAction *action, gpointer data)
315 {
316         FolderView *folderview = (FolderView *)data;
317         FolderItem *from_folder = NULL, *to_folder = NULL;
318
319         from_folder = folderview_get_selected_item(folderview);
320         if (!from_folder || from_folder->folder->klass != mh_get_class())
321                 return;
322
323         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL, TRUE);
324         if (!to_folder)
325                 return;
326         
327         folderview_move_folder(folderview, from_folder, to_folder, 1);
328 }
329
330 #define DO_ACTION(name, act)    { if (!strcmp(a_name, name)) act; }
331
332 static void update_tree_cb(GtkAction *action, gpointer data)
333 {
334         FolderView *folderview = (FolderView *)data;
335         FolderItem *item;
336         const gchar *a_name = gtk_action_get_name(action);
337
338         item = folderview_get_selected_item(folderview);
339         cm_return_if_fail(item != NULL);
340
341         summary_show(folderview->summaryview, NULL);
342
343         cm_return_if_fail(item->folder != NULL);
344
345         DO_ACTION("FolderViewPopup/CheckNewMessages", folderview_check_new(item->folder));
346         DO_ACTION("FolderViewPopup/CheckNewFolders", folderview_rescan_tree(item->folder, FALSE));
347         DO_ACTION("FolderViewPopup/RebuildTree", folderview_rescan_tree(item->folder, TRUE));
348 }
349
350 static void remove_mailbox_cb(GtkAction *action, gpointer data)
351 {
352         FolderView *folderview = (FolderView *)data;
353         FolderItem *item;
354         gchar *name;
355         gchar *message;
356         AlertValue avalue;
357
358         item = folderview_get_selected_item(folderview);
359         cm_return_if_fail(item != NULL);
360         cm_return_if_fail(item->folder != NULL);
361         if (folder_item_parent(item)) return;
362
363         name = trim_string(item->folder->name, 32);
364         message = g_markup_printf_escaped
365                 (_("Really remove the mailbox '%s'?\n"
366                    "(The messages are NOT deleted from the disk)"), name);
367         avalue = alertpanel_full(_("Remove mailbox"), message,
368                                  GTK_STOCK_CANCEL, _("_Remove"), NULL, FALSE,
369                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
370                             
371         g_free(message);
372         g_free(name);
373         if (avalue != G_ALERTALTERNATE) return;
374
375         folderview_unselect(folderview);
376         summary_clear_all(folderview->summaryview);
377
378         folder_destroy(item->folder);
379 }
380