906f28ce7302942bbb524e6ef201db919034c654
[claws.git] / src / mh_gtk.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto & the Sylpheed-Claws 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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28
29 #include <gtk/gtk.h>
30
31 #include "utils.h"
32 #include "folder.h"
33 #include "folderview.h"
34 #include "menu.h"
35 #include "account.h"
36 #include "alertpanel.h"
37 #include "inputdialog.h"
38 #include "mh.h"
39 #include "foldersel.h"
40
41 static void new_folder_cb(FolderView *folderview, guint action, GtkWidget *widget);
42 static void delete_folder_cb(FolderView *folderview, guint action, GtkWidget *widget);
43 static void rename_folder_cb(FolderView *folderview, guint action, GtkWidget *widget);
44 static void move_folder_cb(FolderView *folderview, guint action, GtkWidget *widget);
45 static void update_tree_cb(FolderView *folderview, guint action, GtkWidget *widget);
46 static void remove_mailbox_cb(FolderView *folderview, guint action, GtkWidget *widget);
47
48 static GtkItemFactoryEntry mh_popup_entries[] =
49 {
50         {N_("/Create _new folder..."),   NULL, new_folder_cb,     0, NULL},
51         {N_("/_Rename folder..."),       NULL, rename_folder_cb,  0, NULL},
52         {N_("/M_ove folder..."),         NULL, move_folder_cb,    0, NULL},
53         {N_("/_Delete folder..."),       NULL, delete_folder_cb,  0, NULL},
54         {N_("/---"),                     NULL, NULL,              0, "<Separator>"},
55         {N_("/_Check for new messages"), NULL, update_tree_cb,    0, NULL},
56         {N_("/C_heck for new folders"),  NULL, update_tree_cb,    1, NULL},
57         {N_("/R_ebuild folder tree"),    NULL, update_tree_cb,    2, NULL},
58         {N_("/---"),                     NULL, NULL,              0, "<Separator>"},
59         {N_("/Remove _mailbox..."),      NULL, remove_mailbox_cb, 0, NULL},
60         {N_("/---"),                     NULL, NULL,              0, "<Separator>"},
61 };
62
63 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item);
64
65 static FolderViewPopup mh_popup =
66 {
67         "mh",
68         "<MHFolder>",
69         NULL,
70         set_sensitivity
71 };
72
73 void mh_gtk_init(void)
74 {
75         guint i, n_entries;
76
77         n_entries = sizeof(mh_popup_entries) /
78                 sizeof(mh_popup_entries[0]);
79         for (i = 0; i < n_entries; i++)
80                 mh_popup.entries = g_slist_append(mh_popup.entries, &mh_popup_entries[i]);
81
82         folderview_register_popup(&mh_popup);
83 }
84
85 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item)
86 {
87         gboolean folder_is_normal = 
88                         item != NULL &&
89                         item->stype == F_NORMAL &&
90                         !folder_has_parent_of_type(item, F_OUTBOX) &&
91                         !folder_has_parent_of_type(item, F_DRAFT) &&
92                         !folder_has_parent_of_type(item, F_QUEUE) &&
93                         !folder_has_parent_of_type(item, F_TRASH);
94 #define SET_SENS(name, sens) \
95         menu_set_sensitive(factory, name, sens)
96
97         SET_SENS("/Create new folder...",   TRUE);
98         SET_SENS("/Rename folder...",       item->stype == F_NORMAL && folder_item_parent(item) != NULL);
99         SET_SENS("/Move folder...",         folder_is_normal && folder_item_parent(item) != NULL);
100         SET_SENS("/Delete folder...",       item->stype == F_NORMAL && folder_item_parent(item) != NULL);
101
102         SET_SENS("/Check for new messages", folder_item_parent(item) == NULL);
103         SET_SENS("/Check for new folders",  folder_item_parent(item) == NULL);
104         SET_SENS("/Rebuild folder tree",    folder_item_parent(item) == NULL);
105
106         SET_SENS("/Remove mailbox...",         folder_item_parent(item) == NULL);
107
108 #undef SET_SENS
109 }
110
111 static void new_folder_cb(FolderView *folderview, guint action,
112                           GtkWidget *widget)
113 {
114         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
115         FolderItem *item;
116         FolderItem *new_item;
117         gchar *new_folder;
118         gchar *name;
119         gchar *p;
120
121         if (!folderview->selected) return;
122
123         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
124         g_return_if_fail(item != NULL);
125         g_return_if_fail(item->folder != NULL);
126
127         new_folder = input_dialog(_("New folder"),
128                                   _("Input the name of new folder:"),
129                                   _("NewFolder"));
130         if (!new_folder) return;
131         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
132
133         p = strchr(new_folder, G_DIR_SEPARATOR);
134         if (p) {
135                 alertpanel_error(_("'%c' can't be included in folder name."),
136                                  G_DIR_SEPARATOR);
137                 return;
138         }
139
140         name = trim_string(new_folder, 32);
141         AUTORELEASE_STR(name, {g_free(name); return;});
142
143         /* find whether the directory already exists */
144         if (folder_find_child_item_by_name(item, new_folder)) {
145                 alertpanel_error(_("The folder '%s' already exists."), name);
146                 return;
147         }
148
149         new_item = folder_create_folder(item, new_folder);
150         if (!new_item) {
151                 alertpanel_error(_("Can't create the folder '%s'."), name);
152                 return;
153         }
154
155         folder_write_list();
156 }
157
158 static void delete_folder_cb(FolderView *folderview, guint action,
159                              GtkWidget *widget)
160 {
161         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
162         FolderItem *item;
163         gchar *message, *name;
164         AlertValue avalue;
165         gchar *old_path;
166         gchar *old_id;
167
168         item = folderview_get_selected_item(folderview);
169         g_return_if_fail(item != NULL);
170         g_return_if_fail(item->path != NULL);
171         g_return_if_fail(item->folder != NULL);
172
173         name = trim_string(item->name, 32);
174         AUTORELEASE_STR(name, {g_free(name); return;});
175         message = g_strdup_printf
176                 (_("All folders and messages under '%s' will be permanently deleted. "
177                    "Recovery will not be possible.\n\n"
178                    "Do you really want to delete?"), name);
179         avalue = alertpanel_full(_("Delete folder"), message,
180                                  GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
181                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
182         g_free(message);
183         if (avalue != G_ALERTALTERNATE) return;
184
185         Xstrdup_a(old_path, item->path, return);
186         old_id = folder_item_get_identifier(item);
187
188         if (folderview->opened == folderview->selected ||
189             gtk_ctree_is_ancestor(ctree,
190                                   folderview->selected,
191                                   folderview->opened)) {
192                 summary_clear_all(folderview->summaryview);
193                 folderview->opened = NULL;
194         }
195
196         if (item->folder->klass->remove_folder(item->folder, item) < 0) {
197                 folder_item_scan(item);
198                 alertpanel_error(_("Can't remove the folder '%s'."), name);
199                 g_free(old_id);
200                 return;
201         }
202
203         folder_write_list();
204
205         prefs_filtering_delete_path(old_id);
206         g_free(old_id);
207
208 }
209
210 static void rename_folder_cb(FolderView *folderview, guint action,
211                              GtkWidget *widget)
212 {
213         FolderItem *item;
214         gchar *new_folder;
215         gchar *name;
216         gchar *message;
217         gchar *old_path;
218         gchar *old_id;
219         gchar *new_id;
220         gchar *base;
221
222         item = folderview_get_selected_item(folderview);
223         g_return_if_fail(item != NULL);
224         g_return_if_fail(item->path != NULL);
225         g_return_if_fail(item->folder != NULL);
226
227         name = trim_string(item->name, 32);
228         message = g_strdup_printf(_("Input new name for '%s':"), name);
229         base = g_path_get_basename(item->path);
230         new_folder = input_dialog(_("Rename folder"), message, base);
231         g_free(message);
232         g_free(name);
233         g_free(base);
234         if (!new_folder) return;
235         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
236
237         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
238                 alertpanel_error(_("'%c' can't be included in folder name."),
239                                  G_DIR_SEPARATOR);
240                 return;
241         }
242
243         if (folder_find_child_item_by_name(folder_item_parent(item), new_folder)) {
244                 name = trim_string(new_folder, 32);
245                 alertpanel_error(_("The folder '%s' already exists."), name);
246                 g_free(name);
247                 return;
248         }
249
250         Xstrdup_a(old_path, item->path, {g_free(new_folder); return;});
251
252         old_id = folder_item_get_identifier(item);
253         
254         if (folder_item_rename(item, new_folder) < 0) {
255                 alertpanel_error(_("The folder could not be renamed.\n"
256                                    "The new folder name is not allowed."));
257                 g_free(old_id);
258                 return;
259         }
260
261         /* if (FOLDER_TYPE(item->folder) == F_MH)
262                 prefs_filtering_rename_path(old_path, item->path); */
263         new_id = folder_item_get_identifier(item);
264         prefs_filtering_rename_path(old_id, new_id);
265
266         g_free(old_id);
267         g_free(new_id);
268
269         folder_item_prefs_save_config(item);
270         folder_write_list();
271 }
272
273 static void move_folder_cb(FolderView *folderview, guint action, GtkWidget *widget)
274 {
275         FolderItem *from_folder = NULL, *to_folder = NULL;
276
277         from_folder = folderview_get_selected_item(folderview);
278         if (!from_folder || from_folder->folder->klass != mh_get_class())
279                 return;
280
281         to_folder = foldersel_folder_sel(from_folder->folder, FOLDER_SEL_MOVE, NULL);
282         if (!to_folder)
283                 return;
284         
285         folderview_move_folder(folderview, from_folder, to_folder);
286 }
287
288 static void update_tree_cb(FolderView *folderview, guint action,
289                            GtkWidget *widget)
290 {
291         FolderItem *item;
292
293         item = folderview_get_selected_item(folderview);
294         g_return_if_fail(item != NULL);
295
296         summary_show(folderview->summaryview, NULL);
297
298         g_return_if_fail(item->folder != NULL);
299
300         if (action == 0)
301                 folderview_check_new(item->folder);
302         else if (action == 1)
303                 folderview_rescan_tree(item->folder, FALSE);
304         else if (action == 2)
305                 folderview_rescan_tree(item->folder, TRUE);
306 }
307
308 static void remove_mailbox_cb(FolderView *folderview, guint action,
309                               GtkWidget *widget)
310 {
311         FolderItem *item;
312         gchar *name;
313         gchar *message;
314         AlertValue avalue;
315
316         item = folderview_get_selected_item(folderview);
317         g_return_if_fail(item != NULL);
318         g_return_if_fail(item->folder != NULL);
319         if (folder_item_parent(item)) return;
320
321         name = trim_string(item->folder->name, 32);
322         message = g_strdup_printf
323                 (_("Really remove the mailbox '%s' ?\n"
324                    "(The messages are NOT deleted from the disk)"), name);
325         avalue = alertpanel_full(_("Remove mailbox"), message,
326                                  GTK_STOCK_CANCEL, _("_Remove"), NULL, FALSE,
327                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
328                             
329         g_free(message);
330         g_free(name);
331         if (avalue != G_ALERTALTERNATE) return;
332
333         folderview_unselect(folderview);
334         summary_clear_all(folderview->summaryview);
335
336         folder_destroy(item->folder);
337 }
338