Merge branch 'master' of ssh+git://git.claws-mail.org/home/git/claws
[claws.git] / src / imap_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 #include <time.h>
32
33 #include "utils.h"
34 #include "folder.h"
35 #include "folder_item_prefs.h"
36 #include "folderview.h"
37 #include "menu.h"
38 #include "account.h"
39 #include "alertpanel.h"
40 #include "foldersel.h"
41 #include "inputdialog.h"
42 #include "imap.h"
43 #include "inc.h"
44 #include "prefs_common.h"
45 #include "statusbar.h"
46 #include "summaryview.h"
47 #include "prefs_actions.h"
48
49 static void new_folder_cb(GtkAction *action, gpointer data);
50 static void rename_folder_cb(GtkAction *action, gpointer data);
51 static void move_folder_cb(GtkAction *action, gpointer data);
52 static void copy_folder_cb(GtkAction *action, gpointer data);
53 static void delete_folder_cb(GtkAction *action, gpointer data);
54 static void update_tree_cb(GtkAction *action, gpointer data);
55 static void download_cb(GtkAction *action, gpointer data);
56 static void sync_cb(GtkAction *action, gpointer data);
57 static void subscribed_cb(GtkAction *action, gpointer data);
58 static void subscribe_cb(GtkAction *action, gpointer data);
59 static void unsubscribe_cb(GtkAction *action, gpointer data);
60
61 static GtkActionEntry imap_popup_entries[] =
62 {
63         {"FolderViewPopup/CreateNewFolder",     NULL, N_("Create _new folder..."), NULL, NULL, G_CALLBACK(new_folder_cb) },
64
65         {"FolderViewPopup/RenameFolder",        NULL, N_("_Rename folder..."), NULL, NULL, G_CALLBACK(rename_folder_cb) },
66         {"FolderViewPopup/MoveFolder",          NULL, N_("M_ove folder..."), NULL, NULL, G_CALLBACK(move_folder_cb) },
67         {"FolderViewPopup/CopyFolder",          NULL, N_("Cop_y folder..."), NULL, NULL, G_CALLBACK(copy_folder_cb) },
68
69         {"FolderViewPopup/DeleteFolder",        NULL, N_("_Delete folder..."), NULL, NULL, G_CALLBACK(delete_folder_cb) },
70
71         {"FolderViewPopup/Synchronise",         NULL, N_("Synchronise"), NULL, NULL, G_CALLBACK(sync_cb) },
72         {"FolderViewPopup/DownloadMessages",    NULL, N_("Down_load messages"), NULL, NULL, G_CALLBACK(download_cb) },
73
74
75         {"FolderViewPopup/Subscriptions",       NULL, N_("S_ubscriptions") },
76         {"FolderViewPopup/Subscriptions/---",   NULL, "---", NULL, NULL, NULL }, 
77         {"FolderViewPopup/Subscriptions/Subscribe",     NULL, N_("_Subscribe..."), NULL, NULL, G_CALLBACK(subscribe_cb) },
78         {"FolderViewPopup/Subscriptions/Unsubscribe",   NULL, N_("_Unsubscribe..."), NULL, NULL, G_CALLBACK(unsubscribe_cb) },
79
80         {"FolderViewPopup/CheckNewMessages",    NULL, N_("_Check for new messages"), NULL, NULL, G_CALLBACK(update_tree_cb) }, /*0*/
81         {"FolderViewPopup/CheckNewFolders",     NULL, N_("C_heck for new folders"), NULL, NULL, G_CALLBACK(update_tree_cb) }, /*1*/
82         {"FolderViewPopup/RebuildTree",         NULL, N_("R_ebuild folder tree"), NULL, NULL, G_CALLBACK(update_tree_cb) }, /*2*/
83 };
84
85 static GtkToggleActionEntry imap_toggle_popup_entries[] =
86 {
87         {"FolderViewPopup/Subscriptions/ShowOnlySubs",  NULL, N_("Show only subscribed _folders"), NULL, NULL, G_CALLBACK(subscribed_cb) }, 
88 };
89
90 static void set_sensitivity(GtkUIManager *ui_manager, FolderItem *item);
91 static void add_menuitems(GtkUIManager *ui_manager, FolderItem *item);
92
93 static FolderViewPopup imap_popup =
94 {
95         "imap",
96         "<IMAPFolder>",
97         imap_popup_entries,
98         G_N_ELEMENTS(imap_popup_entries),
99         imap_toggle_popup_entries,
100         G_N_ELEMENTS(imap_toggle_popup_entries),
101         NULL, 0, 0, NULL,
102         add_menuitems,
103         set_sensitivity
104 };
105
106 void imap_gtk_init(void)
107 {
108         folderview_register_popup(&imap_popup);
109 }
110
111 static void add_menuitems(GtkUIManager *ui_manager, FolderItem *item)
112 {
113         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "CreateNewFolder", "FolderViewPopup/CreateNewFolder", GTK_UI_MANAGER_MENUITEM)
114         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorIMAP1", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
115         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RenameFolder", "FolderViewPopup/RenameFolder", GTK_UI_MANAGER_MENUITEM)
116         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "MoveFolder", "FolderViewPopup/MoveFolder", GTK_UI_MANAGER_MENUITEM)
117         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "CopyFolder", "FolderViewPopup/CopyFolder", GTK_UI_MANAGER_MENUITEM)
118         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorIMAP2", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
119         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "DeleteFolder", "FolderViewPopup/DeleteFolder", GTK_UI_MANAGER_MENUITEM)
120         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorIMAP3", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
121         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "Synchronise", "FolderViewPopup/Synchronise", GTK_UI_MANAGER_MENUITEM)
122         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "DownloadMessages", "FolderViewPopup/DownloadMessages", GTK_UI_MANAGER_MENUITEM)
123         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorIMAP4", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
124         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "Subscriptions", "FolderViewPopup/Subscriptions", GTK_UI_MANAGER_MENU)
125         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup/Subscriptions", "ShowOnlySubs", "FolderViewPopup/Subscriptions/ShowOnlySubs", GTK_UI_MANAGER_MENUITEM)
126         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup/Subscriptions", "SeparatorIMAP5", "FolderViewPopup/Subscriptions/---", GTK_UI_MANAGER_SEPARATOR)
127         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup/Subscriptions", "Subscribe", "FolderViewPopup/Subscriptions/Subscribe", GTK_UI_MANAGER_MENUITEM)
128         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup/Subscriptions", "Unsubscribe", "FolderViewPopup/Subscriptions/Unsubscribe", GTK_UI_MANAGER_MENUITEM)
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", "SeparatorIMAP6", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
133 }
134
135 static void set_sensitivity(GtkUIManager *ui_manager, FolderItem *item)
136 {
137         gboolean folder_is_normal = 
138                         item != NULL &&
139                         item->stype == F_NORMAL &&
140                         !folder_has_parent_of_type(item, F_OUTBOX) &&
141                         !folder_has_parent_of_type(item, F_DRAFT) &&
142                         !folder_has_parent_of_type(item, F_QUEUE) &&
143                         !folder_has_parent_of_type(item, F_TRASH);
144
145 #define SET_SENS(name, sens) \
146         cm_menu_set_sensitive_full(ui_manager, "Popup/"name, sens)
147
148         SET_SENS("FolderViewPopup/CreateNewFolder",   item && item->no_sub == FALSE);
149         SET_SENS("FolderViewPopup/RenameFolder",       item && item->stype == F_NORMAL && folder_item_parent(item) != NULL);
150         SET_SENS("FolderViewPopup/MoveFolder",      item && folder_is_normal && folder_item_parent(item) != NULL);
151         SET_SENS("FolderViewPopup/DeleteFolder",            item && item->stype == F_NORMAL && folder_item_parent(item) != NULL);
152
153         SET_SENS("FolderViewPopup/CheckNewMessages", folder_item_parent(item) == NULL);
154         SET_SENS("FolderViewPopup/CheckNewFolders",  folder_item_parent(item) == NULL);
155         SET_SENS("FolderViewPopup/RebuildTree",    folder_item_parent(item) == NULL);
156
157         SET_SENS("FolderViewPopup/Synchronise",    
158                         item ? (folder_item_parent(item) != NULL
159                         && folder_want_synchronise(item->folder))
160                         : FALSE);
161         SET_SENS("FolderViewPopup/DownloadMessages", item && !item->no_select);
162
163         SET_SENS("FolderViewPopup/CheckNewMessages", folder_item_parent(item) == NULL);
164         SET_SENS("FolderViewPopup/CheckNewFolders",  folder_item_parent(item) == NULL);
165         SET_SENS("FolderViewPopup/RebuildTree",    folder_item_parent(item) == NULL);
166         
167         SET_SENS("FolderViewPopup/Subscriptions/Unsubscribe",    item && item->stype == F_NORMAL && folder_item_parent(item) != NULL);
168         SET_SENS("FolderViewPopup/Subscriptions/Subscribe",    TRUE);
169         if (item && item->folder && item->folder->account)
170                 cm_toggle_menu_set_active_full(ui_manager, "Popup/FolderViewPopup/Subscriptions/ShowOnlySubs",
171                         item->folder->account->imap_subsonly);
172
173 #undef SET_SENS
174 }
175
176 static void new_folder_cb(GtkAction *action, gpointer data)
177 {
178         FolderView *folderview = (FolderView *)data;
179         FolderItem *item;
180         FolderItem *new_item;
181         gchar *new_folder;
182         gchar *name;
183         gchar *p;
184         gchar separator = '/';
185         
186         if (!folderview->selected) return;
187
188         item = folderview_get_selected_item(folderview);
189         cm_return_if_fail(item != NULL);
190         cm_return_if_fail(item->folder != NULL);
191         cm_return_if_fail(item->folder->account != NULL);
192
193         new_folder = input_dialog_with_checkbtn
194                 (_("New folder"),
195                  _("Input the name of new folder:\n"
196                    "(if you want to create a folder to store subfolders\n"
197                    "only and no mail, append '/' to the folder name)"),
198                  _("NewFolder"),
199                  _("Inherit properties from parent folder"),
200                  &(prefs_common.inherit_folder_props));
201
202         if (!new_folder) return;
203         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
204
205         separator = imap_get_path_separator_for_item(item);
206
207         p = strchr(new_folder, separator);
208         if (p && *(p + 1) != '\0') {
209                 alertpanel_error(_("'%c' can't be included in folder name."),
210                                  separator);
211                 return;
212         }
213         p = strchr(new_folder, '/');
214         if (p && *(p + 1) != '\0') {
215                 alertpanel_error(_("'%c' can't be included in folder name."),
216                                  '/');
217                 return;
218         }
219
220         name = trim_string(new_folder, 32);
221         AUTORELEASE_STR(name, {g_free(name); return;});
222
223         /* find whether the directory already exists */
224         if (folder_find_child_item_by_name(item, new_folder)) {
225                 alertpanel_error(_("The folder '%s' already exists."), name);
226                 return;
227         }
228
229         new_item = folder_create_folder(item, new_folder);
230         if (!new_item) {
231                 alertpanel_error(_("Can't create the folder '%s'."), name);
232                 return;
233         }
234
235         if (prefs_common.inherit_folder_props) {
236                 folder_item_prefs_copy_prefs(item, new_item);
237         }
238
239         folder_write_list();
240 }
241
242 static void rename_folder_cb(GtkAction *action, gpointer data)
243 {
244         FolderView *folderview = (FolderView *)data;
245         FolderItem *item;
246         gchar *new_folder;
247         gchar *name;
248         gchar *message;
249         gchar *old_id;
250         gchar *new_id;
251         gchar *base;
252         gchar separator = '/';
253
254         item = folderview_get_selected_item(folderview);
255         cm_return_if_fail(item != NULL);
256         cm_return_if_fail(item->path != NULL);
257         cm_return_if_fail(item->folder != NULL);
258
259         name = trim_string(item->name, 32);
260         message = g_strdup_printf(_("Input new name for '%s':"), name);
261         base = g_path_get_basename(item->path);
262         new_folder = input_dialog(_("Rename folder"), message, base);
263         g_free(base);
264         g_free(message);
265         g_free(name);
266         if (!new_folder) return;
267         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
268
269         separator = imap_get_path_separator_for_item(item);
270         if (strchr(new_folder, separator) != NULL) {
271                 alertpanel_error(_("'%c' can't be included in folder name."),
272                                  separator);
273                 return;
274         }
275         if (strchr(new_folder, '/') != NULL) {
276                 alertpanel_error(_("'%c' can't be included in folder name."),
277                                  '/');
278                 return;
279         }
280
281         if (folder_find_child_item_by_name(folder_item_parent(item), new_folder)) {
282                 name = trim_string(new_folder, 32);
283                 alertpanel_error(_("The folder '%s' already exists."), name);
284                 g_free(name);
285                 return;
286         }
287
288         old_id = folder_item_get_identifier(item);
289         
290         if (folder_item_rename(item, new_folder) < 0) {
291                 alertpanel_error(_("The folder could not be renamed.\n"
292                                    "The new folder name is not allowed."));
293                 g_free(old_id);
294                 return;
295         }
296
297         new_id = folder_item_get_identifier(item);
298         prefs_filtering_rename_path(old_id, new_id);
299         account_rename_path(old_id, new_id);
300         prefs_actions_rename_path(old_id, new_id);
301         g_free(old_id);
302         g_free(new_id);
303
304         folder_item_prefs_save_config_recursive(item);
305         folder_write_list();
306 }
307
308 static void move_folder_cb(GtkAction *action, gpointer data)
309 {
310         FolderView *folderview = (FolderView *)data;
311         FolderItem *from_folder = NULL, *to_folder = NULL;
312
313         from_folder = folderview_get_selected_item(folderview);
314         if (!from_folder || from_folder->folder->klass != imap_get_class())
315                 return;
316
317         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL, TRUE);
318         if (!to_folder)
319                 return;
320         
321         folderview_move_folder(folderview, from_folder, to_folder, 0);
322 }
323
324 static void copy_folder_cb(GtkAction *action, gpointer data)
325 {
326         FolderView *folderview = (FolderView *)data;
327         FolderItem *from_folder = NULL, *to_folder = NULL;
328
329         from_folder = folderview_get_selected_item(folderview);
330         if (!from_folder || from_folder->folder->klass != imap_get_class())
331                 return;
332
333         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL, TRUE);
334         if (!to_folder)
335                 return;
336         
337         folderview_move_folder(folderview, from_folder, to_folder, 1);
338 }
339
340 static void delete_folder_cb(GtkAction *action, gpointer data)
341 {
342         FolderView *folderview = (FolderView *)data;
343         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
344         FolderItem *item;
345         gchar *message, *name;
346         AlertValue avalue;
347         gchar *old_id;
348
349         if (!folderview->selected) return;
350
351         item = folderview_get_selected_item(folderview);
352         cm_return_if_fail(item != NULL);
353         cm_return_if_fail(item->path != NULL);
354         cm_return_if_fail(item->folder != NULL);
355
356         name = trim_string(item->name, 32);
357         AUTORELEASE_STR(name, {g_free(name); return;});
358         message = g_markup_printf_escaped
359                 (_("All folders and messages under '%s' will be permanently deleted. "
360                    "Recovery will not be possible.\n\n"
361                    "Do you really want to delete?"), name);
362         avalue = alertpanel_full(_("Delete folder"), message,
363                                  GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
364                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
365         g_free(message);
366         if (avalue != G_ALERTALTERNATE) return;
367
368         old_id = folder_item_get_identifier(item);
369
370         if (folderview->opened == folderview->selected ||
371             gtk_cmctree_is_ancestor(ctree,
372                                   folderview->selected,
373                                   folderview->opened)) {
374                 summary_clear_all(folderview->summaryview);
375                 folderview->opened = NULL;
376         }
377
378         if (item->folder->klass->remove_folder(item->folder, item) < 0) {
379                 folder_item_scan(item);
380                 alertpanel_error(_("Can't remove the folder '%s'."), name);
381                 g_free(old_id);
382                 return;
383         }
384
385         folder_write_list();
386
387         prefs_filtering_delete_path(old_id);
388         g_free(old_id);
389
390 }
391
392 #define DO_ACTION(name, act)    { if (!strcmp(a_name, name)) act; }
393
394 static void update_tree_cb(GtkAction *action, gpointer data)
395 {
396         FolderView *folderview = (FolderView *)data;
397         FolderItem *item;
398         const gchar *a_name = gtk_action_get_name(action);
399
400         item = folderview_get_selected_item(folderview);
401         cm_return_if_fail(item != NULL);
402
403         summary_show(folderview->summaryview, NULL);
404
405         cm_return_if_fail(item->folder != NULL);
406
407         DO_ACTION("FolderViewPopup/CheckNewMessages", folderview_check_new(item->folder));
408         DO_ACTION("FolderViewPopup/CheckNewFolders", folderview_rescan_tree(item->folder, FALSE));
409         DO_ACTION("FolderViewPopup/RebuildTree", folderview_rescan_tree(item->folder, TRUE));
410 }
411
412 static void sync_cb(GtkAction *action, gpointer data)
413 {
414         FolderView *folderview = (FolderView *)data;
415         FolderItem *item;
416
417         item = folderview_get_selected_item(folderview);
418         cm_return_if_fail(item != NULL);
419         folder_synchronise(item->folder);
420 }
421
422 void imap_gtk_synchronise(FolderItem *item, gint days)
423 {
424         MainWindow *mainwin = mainwindow_get_mainwindow();
425         FolderView *folderview = mainwin->folderview;
426         
427         cm_return_if_fail(item != NULL);
428         cm_return_if_fail(item->folder != NULL);
429
430         main_window_cursor_wait(mainwin);
431         inc_lock();
432         main_window_lock(mainwin);
433         gtk_widget_set_sensitive(folderview->ctree, FALSE);
434         main_window_progress_on(mainwin);
435         GTK_EVENTS_FLUSH();
436         if (item->no_select == FALSE) {
437                 GSList *mlist;
438                 GSList *cur;
439                 gint num = 0;
440                 gint total = item->total_msgs;
441                 time_t t = time(NULL);
442
443                 mlist = folder_item_get_msg_list(item);
444                 for (cur = mlist; cur != NULL; cur = cur->next) {
445                         MsgInfo *msginfo = (MsgInfo *)cur->data;
446                         gint age = (t - msginfo->date_t) / (60*60*24);
447                         if (days == 0 || age <= days)
448                                 imap_cache_msg(msginfo->folder, msginfo->msgnum);
449                         statusbar_progress_all(num++,total, 100);
450                         if (num % 100 == 0)
451                                 GTK_EVENTS_FLUSH();
452                 }
453
454                 statusbar_progress_all(0,0,0);
455                 procmsg_msg_list_free(mlist);
456         }
457
458         folder_set_ui_func(item->folder, NULL, NULL);
459         main_window_progress_off(mainwin);
460         gtk_widget_set_sensitive(folderview->ctree, TRUE);
461         main_window_unlock(mainwin);
462         inc_unlock();
463         main_window_cursor_normal(mainwin);
464 }
465
466 static void chk_update_val(GtkWidget *widget, gpointer data)
467 {
468         gboolean *val = (gboolean *)data;
469         *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
470 }
471
472 static gboolean imap_gtk_subscribe_func(GNode *node, gpointer data)
473 {
474         FolderItem *item = node->data;
475         gboolean action = GPOINTER_TO_INT(data);
476         
477         if (item->path)
478                 imap_subscribe(item->folder, item, NULL, action);
479
480         return FALSE;
481 }
482
483 static void subscribe_cb_full(FolderView *folderview, guint action)
484 {
485         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
486         FolderItem *item;
487         gchar *message, *name;
488         AlertValue avalue;
489         GtkWidget *rec_chk;
490         gboolean recurse = FALSE;
491
492         if (!folderview->selected) return;
493
494         item = folderview_get_selected_item(folderview);
495         cm_return_if_fail(item != NULL);
496         cm_return_if_fail(item->folder != NULL);
497
498         name = trim_string(item->name, 32);
499         AUTORELEASE_STR(name, {g_free(name); return;});
500         
501         if (action && item->folder->account->imap_subsonly) {
502                 GList *child_list = NULL;
503                 GList *transc_list = NULL;
504
505                 message = g_markup_printf_escaped
506                         (_("Do you want to search for unsubscribed subfolders of '%s'?"),
507                          name);
508
509                 rec_chk = gtk_check_button_new_with_label(_("Search recursively"));
510
511                 g_signal_connect(G_OBJECT(rec_chk), "toggled", 
512                                 G_CALLBACK(chk_update_val), &recurse);
513
514                 avalue = alertpanel_full(_("Subscriptions"), message,
515                                          GTK_STOCK_CANCEL, g_strconcat("+", _("_Search"), NULL),
516                                          NULL, FALSE, rec_chk, ALERT_QUESTION, G_ALERTDEFAULT);
517                 g_free(message);
518                 if (avalue != G_ALERTALTERNATE) return;
519                 
520                 child_list = imap_scan_subtree(item->folder, item, TRUE, recurse);
521                 
522                 if (child_list) {
523                         GList *cur;
524                         int r = -1;
525                         gchar *msg = g_strdup_printf(_("Choose a subfolder of %s to subscribe to: "),
526                                         item->name); 
527                         gchar *child_folder = NULL;
528                         
529                         for (cur = child_list; cur; cur = cur->next) {
530                                 transc_list = g_list_append(transc_list, 
531                                         imap_modified_utf7_to_utf8(cur->data, FALSE));
532                         }
533                         
534                         transc_list = g_list_sort(transc_list, g_str_equal);
535                         
536                         child_folder = input_dialog_combo(_("Subscribe"), 
537                                         msg,
538                                         transc_list->next?_("All of them"):transc_list->data, transc_list);
539                         g_free(msg);
540                         if (child_folder && strcmp(child_folder, _("All of them"))) {
541                                 gchar *transc_folder = imap_utf8_to_modified_utf7(child_folder, FALSE);
542                                 r = imap_subscribe(item->folder, NULL, transc_folder, TRUE);
543                                 g_free(transc_folder);
544                         } else if (child_folder) {
545                                 for (cur = child_list; cur; cur = cur->next) 
546                                         r = imap_subscribe(item->folder, NULL, (gchar *)cur->data, TRUE);
547                         }
548                         g_free(child_folder);
549                         for (cur = child_list; cur; cur = cur->next) 
550                                 g_free((gchar *)cur->data);
551                         for (cur = transc_list; cur; cur = cur->next) 
552                                 g_free((gchar *)cur->data);
553                         if (r == 0)
554                                 folderview_rescan_tree(item->folder, FALSE);
555                 } else {
556                         alertpanel_notice(_("This folder is already subscribed and "
557                                   "has no unsubscribed subfolders.\n\nIf there are new folders, "
558                                   "created and subscribed to from another client, use \"Check "
559                                   "for new folders\" at the mailbox's root folder."));
560                 }
561                 g_list_free(child_list);
562                 return;
563         }
564         message = g_markup_printf_escaped
565                 (_("Do you want to %s the '%s' folder?"),
566                    action?_("subscribe"):_("unsubscribe"), name);
567         
568         rec_chk = gtk_check_button_new_with_label(_("Apply to subfolders"));
569         
570         g_signal_connect(G_OBJECT(rec_chk), "toggled", 
571                         G_CALLBACK(chk_update_val), &recurse);
572
573         avalue = alertpanel_full(_("Subscriptions"), message,
574                                  GTK_STOCK_CANCEL, action?g_strconcat("+", _("_Subscribe"), NULL):
575                                  g_strconcat("+", _("_Unsubscribe"), NULL), NULL, FALSE,
576                                  rec_chk, ALERT_QUESTION, G_ALERTDEFAULT);
577         g_free(message);
578         if (avalue != G_ALERTALTERNATE) return;
579         
580         
581         if (!action) {
582                 if (folderview->opened == folderview->selected ||
583                     gtk_cmctree_is_ancestor(ctree,
584                                           folderview->selected,
585                                           folderview->opened)) {
586                         summary_clear_all(folderview->summaryview);
587                         folderview->opened = NULL;
588                 }
589         }
590
591         if (recurse) {
592                 g_node_traverse(item->node, G_PRE_ORDER,
593                         G_TRAVERSE_ALL, -1, imap_gtk_subscribe_func, GINT_TO_POINTER(action));
594         } else {
595                 imap_subscribe(item->folder, item, NULL, action);
596         }
597
598         if (!action && item->folder->account->imap_subsonly)
599                 folderview_rescan_tree(item->folder, FALSE);
600 }
601
602 static void subscribe_cb(GtkAction *action, gpointer data)
603 {
604         subscribe_cb_full((FolderView *)data, 1);
605 }
606
607 static void unsubscribe_cb(GtkAction *action, gpointer data)
608 {
609         subscribe_cb_full((FolderView *)data, 0);
610 }
611
612 static void subscribed_cb(GtkAction *action, gpointer data)
613 {
614         FolderView *folderview = (FolderView *)data;
615         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
616         FolderItem *item = folderview_get_selected_item(folderview);
617         
618         if (!item || !item->folder || !item->folder->account)
619                 return;
620         if (item->folder->account->imap_subsonly == gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)))
621                 return;
622
623         if (folderview->opened == folderview->selected ||
624             gtk_cmctree_is_ancestor(ctree,
625                                   folderview->selected,
626                                   folderview->opened)) {
627                 summary_clear_all(folderview->summaryview);
628                 folderview->opened = NULL;
629         }
630
631         item->folder->account->imap_subsonly = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
632         folderview_rescan_tree(item->folder, FALSE);
633 }
634
635 static void download_cb(GtkAction *action, gpointer data)
636 {
637         FolderView *folderview = (FolderView *)data;
638         FolderItem *item;
639
640         if (!folderview->selected) return;
641
642         item = folderview_get_selected_item(folderview);
643         imap_gtk_synchronise(item, 0);
644 }