bcf47babbd158944581001b44154261ce299d291
[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         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
180         FolderItem *item;
181         FolderItem *new_item;
182         gchar *new_folder;
183         gchar *name;
184         gchar *p;
185         gchar separator = '/';
186         
187         if (!folderview->selected) return;
188
189         item = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
190         cm_return_if_fail(item != NULL);
191         cm_return_if_fail(item->folder != NULL);
192         cm_return_if_fail(item->folder->account != NULL);
193
194         new_folder = input_dialog_with_checkbtn
195                 (_("New folder"),
196                  _("Input the name of new folder:\n"
197                    "(if you want to create a folder to store subfolders\n"
198                    "only and no mail, append '/' to the folder name)"),
199                  _("NewFolder"),
200                  _("Inherit properties from parent folder"),
201                  &(prefs_common.inherit_folder_props));
202
203         if (!new_folder) return;
204         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
205
206         separator = imap_get_path_separator_for_item(item);
207
208         p = strchr(new_folder, separator);
209         if (p && *(p + 1) != '\0') {
210                 alertpanel_error(_("'%c' can't be included in folder name."),
211                                  separator);
212                 return;
213         }
214         p = strchr(new_folder, '/');
215         if (p && *(p + 1) != '\0') {
216                 alertpanel_error(_("'%c' can't be included in folder name."),
217                                  '/');
218                 return;
219         }
220
221         name = trim_string(new_folder, 32);
222         AUTORELEASE_STR(name, {g_free(name); return;});
223
224         /* find whether the directory already exists */
225         if (folder_find_child_item_by_name(item, new_folder)) {
226                 alertpanel_error(_("The folder '%s' already exists."), name);
227                 return;
228         }
229
230         new_item = folder_create_folder(item, new_folder);
231         if (!new_item) {
232                 alertpanel_error(_("Can't create the folder '%s'."), name);
233                 return;
234         }
235
236         if (prefs_common.inherit_folder_props) {
237                 folder_item_prefs_copy_prefs(item, new_item);
238         }
239
240         folder_write_list();
241 }
242
243 static void rename_folder_cb(GtkAction *action, gpointer data)
244 {
245         FolderView *folderview = (FolderView *)data;
246         FolderItem *item;
247         gchar *new_folder;
248         gchar *name;
249         gchar *message;
250         gchar *old_path;
251         gchar *old_id;
252         gchar *new_id;
253         gchar *base;
254         gchar separator = '/';
255
256         item = folderview_get_selected_item(folderview);
257         cm_return_if_fail(item != NULL);
258         cm_return_if_fail(item->path != NULL);
259         cm_return_if_fail(item->folder != NULL);
260
261         name = trim_string(item->name, 32);
262         message = g_strdup_printf(_("Input new name for '%s':"), name);
263         base = g_path_get_basename(item->path);
264         new_folder = input_dialog(_("Rename folder"), message, base);
265         g_free(base);
266         g_free(message);
267         g_free(name);
268         if (!new_folder) return;
269         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
270
271         separator = imap_get_path_separator_for_item(item);
272         if (strchr(new_folder, separator) != NULL) {
273                 alertpanel_error(_("'%c' can't be included in folder name."),
274                                  separator);
275                 return;
276         }
277         if (strchr(new_folder, '/') != NULL) {
278                 alertpanel_error(_("`%c' can't be included in folder name."),
279                                  '/');
280                 return;
281         }
282
283         if (folder_find_child_item_by_name(folder_item_parent(item), new_folder)) {
284                 name = trim_string(new_folder, 32);
285                 alertpanel_error(_("The folder '%s' already exists."), name);
286                 g_free(name);
287                 return;
288         }
289
290         Xstrdup_a(old_path, item->path, {g_free(new_folder); return;});
291
292         old_id = folder_item_get_identifier(item);
293         
294         if (folder_item_rename(item, new_folder) < 0) {
295                 alertpanel_error(_("The folder could not be renamed.\n"
296                                    "The new folder name is not allowed."));
297                 g_free(old_id);
298                 return;
299         }
300
301         new_id = folder_item_get_identifier(item);
302         prefs_filtering_rename_path(old_id, new_id);
303         account_rename_path(old_id, new_id);
304         prefs_actions_rename_path(old_id, new_id);
305         g_free(old_id);
306         g_free(new_id);
307
308         folder_item_prefs_save_config_recursive(item);
309         folder_write_list();
310 }
311
312 static void move_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 != imap_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, 0);
326 }
327
328 static void copy_folder_cb(GtkAction *action, gpointer data)
329 {
330         FolderView *folderview = (FolderView *)data;
331         FolderItem *from_folder = NULL, *to_folder = NULL;
332
333         from_folder = folderview_get_selected_item(folderview);
334         if (!from_folder || from_folder->folder->klass != imap_get_class())
335                 return;
336
337         to_folder = foldersel_folder_sel(from_folder->folder, FOLDER_SEL_MOVE, NULL, TRUE);
338         if (!to_folder)
339                 return;
340         
341         folderview_move_folder(folderview, from_folder, to_folder, 1);
342 }
343
344 static void delete_folder_cb(GtkAction *action, gpointer data)
345 {
346         FolderView *folderview = (FolderView *)data;
347         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
348         FolderItem *item;
349         gchar *message, *name;
350         AlertValue avalue;
351         gchar *old_path;
352         gchar *old_id;
353
354         if (!folderview->selected) return;
355
356         item = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
357         cm_return_if_fail(item != NULL);
358         cm_return_if_fail(item->path != NULL);
359         cm_return_if_fail(item->folder != NULL);
360
361         name = trim_string(item->name, 32);
362         AUTORELEASE_STR(name, {g_free(name); return;});
363         message = g_markup_printf_escaped
364                 (_("All folders and messages under '%s' will be permanently deleted. "
365                    "Recovery will not be possible.\n\n"
366                    "Do you really want to delete?"), name);
367         avalue = alertpanel_full(_("Delete folder"), message,
368                                  GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
369                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
370         g_free(message);
371         if (avalue != G_ALERTALTERNATE) return;
372
373         Xstrdup_a(old_path, item->path, return);
374         old_id = folder_item_get_identifier(item);
375
376         if (folderview->opened == folderview->selected ||
377             gtk_cmctree_is_ancestor(ctree,
378                                   folderview->selected,
379                                   folderview->opened)) {
380                 summary_clear_all(folderview->summaryview);
381                 folderview->opened = NULL;
382         }
383
384         if (item->folder->klass->remove_folder(item->folder, item) < 0) {
385                 folder_item_scan(item);
386                 alertpanel_error(_("Can't remove the folder '%s'."), name);
387                 g_free(old_id);
388                 return;
389         }
390
391         folder_write_list();
392
393         prefs_filtering_delete_path(old_id);
394         g_free(old_id);
395
396 }
397
398 #define DO_ACTION(name, act)    { if (!strcmp(a_name, name)) act; }
399
400 static void update_tree_cb(GtkAction *action, gpointer data)
401 {
402         FolderView *folderview = (FolderView *)data;
403         FolderItem *item;
404         const gchar *a_name = gtk_action_get_name(action);
405
406         item = folderview_get_selected_item(folderview);
407         cm_return_if_fail(item != NULL);
408
409         summary_show(folderview->summaryview, NULL);
410
411         cm_return_if_fail(item->folder != NULL);
412
413         DO_ACTION("FolderViewPopup/CheckNewMessages", folderview_check_new(item->folder));
414         DO_ACTION("FolderViewPopup/CheckNewFolders", folderview_rescan_tree(item->folder, FALSE));
415         DO_ACTION("FolderViewPopup/RebuildTree", folderview_rescan_tree(item->folder, TRUE));
416 }
417
418 static void sync_cb(GtkAction *action, gpointer data)
419 {
420         FolderView *folderview = (FolderView *)data;
421         FolderItem *item;
422
423         item = folderview_get_selected_item(folderview);
424         cm_return_if_fail(item != NULL);
425         folder_synchronise(item->folder);
426 }
427
428 void imap_gtk_synchronise(FolderItem *item, gint days)
429 {
430         MainWindow *mainwin = mainwindow_get_mainwindow();
431         FolderView *folderview = mainwin->folderview;
432         
433         cm_return_if_fail(item != NULL);
434         cm_return_if_fail(item->folder != NULL);
435
436         main_window_cursor_wait(mainwin);
437         inc_lock();
438         main_window_lock(mainwin);
439         gtk_widget_set_sensitive(folderview->ctree, FALSE);
440         main_window_progress_on(mainwin);
441         GTK_EVENTS_FLUSH();
442         if (item->no_select == FALSE) {
443                 GSList *mlist;
444                 GSList *cur;
445                 gint num = 0;
446                 gint total = item->total_msgs;
447                 time_t t = time(NULL);
448
449                 mlist = folder_item_get_msg_list(item);
450                 for (cur = mlist; cur != NULL; cur = cur->next) {
451                         MsgInfo *msginfo = (MsgInfo *)cur->data;
452                         gint age = (t - msginfo->date_t) / (60*60*24);
453                         if (days == 0 || age <= days)
454                                 imap_cache_msg(msginfo->folder, msginfo->msgnum);
455                         statusbar_progress_all(num++,total, 100);
456                         if (num % 100 == 0)
457                                 GTK_EVENTS_FLUSH();
458                 }
459
460                 statusbar_progress_all(0,0,0);
461                 procmsg_msg_list_free(mlist);
462         }
463
464         folder_set_ui_func(item->folder, NULL, NULL);
465         main_window_progress_off(mainwin);
466         gtk_widget_set_sensitive(folderview->ctree, TRUE);
467         main_window_unlock(mainwin);
468         inc_unlock();
469         main_window_cursor_normal(mainwin);
470 }
471
472 static void chk_update_val(GtkWidget *widget, gpointer data)
473 {
474         gboolean *val = (gboolean *)data;
475         *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
476 }
477
478 static gboolean imap_gtk_subscribe_func(GNode *node, gpointer data)
479 {
480         FolderItem *item = node->data;
481         gboolean action = GPOINTER_TO_INT(data);
482         
483         if (item->path)
484                 imap_subscribe(item->folder, item, NULL, action);
485
486         return FALSE;
487 }
488
489 static void subscribe_cb_full(FolderView *folderview, guint action)
490 {
491         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
492         FolderItem *item;
493         gchar *message, *name;
494         AlertValue avalue;
495         GtkWidget *rec_chk;
496         gboolean recurse = FALSE;
497
498         if (!folderview->selected) return;
499
500         item = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
501         cm_return_if_fail(item != NULL);
502         cm_return_if_fail(item->folder != NULL);
503
504         name = trim_string(item->name, 32);
505         AUTORELEASE_STR(name, {g_free(name); return;});
506         
507         if (action && item->folder->account->imap_subsonly) {
508                 GList *child_list = NULL;
509                 GList *transc_list = NULL;
510
511                 message = g_markup_printf_escaped
512                         (_("Do you want to search for unsubscribed subfolders of '%s'?"),
513                          name);
514
515                 rec_chk = gtk_check_button_new_with_label(_("Search recursively"));
516
517                 g_signal_connect(G_OBJECT(rec_chk), "toggled", 
518                                 G_CALLBACK(chk_update_val), &recurse);
519
520                 avalue = alertpanel_full(_("Subscriptions"), message,
521                                          GTK_STOCK_CANCEL, _("+_Search"), NULL, FALSE,
522                                          rec_chk, ALERT_QUESTION, G_ALERTDEFAULT);
523                 g_free(message);
524                 if (avalue != G_ALERTALTERNATE) return;
525                 
526                 child_list = imap_scan_subtree(item->folder, item, TRUE, recurse);
527                 
528                 if (child_list) {
529                         GList *cur;
530                         int r = -1;
531                         gchar *msg = g_strdup_printf(_("Choose a subfolder of %s to subscribe to: "),
532                                         item->name); 
533                         gchar *child_folder = NULL;
534                         
535                         for (cur = child_list; cur; cur = cur->next) {
536                                 transc_list = g_list_append(transc_list, 
537                                         imap_modified_utf7_to_utf8(cur->data, FALSE));
538                         }
539                         
540                         transc_list = g_list_sort(transc_list, g_str_equal);
541                         
542                         child_folder = input_dialog_combo(_("Subscribe"), 
543                                         msg,
544                                         transc_list->next?_("All of them"):transc_list->data, transc_list);
545                         g_free(msg);
546                         if (child_folder && strcmp(child_folder, _("All of them"))) {
547                                 gchar *transc_folder = imap_utf8_to_modified_utf7(child_folder, FALSE);
548                                 r = imap_subscribe(item->folder, NULL, transc_folder, TRUE);
549                                 g_free(transc_folder);
550                         } else if (child_folder) {
551                                 for (cur = child_list; cur; cur = cur->next) 
552                                         r = imap_subscribe(item->folder, NULL, (gchar *)cur->data, TRUE);
553                         }
554                         g_free(child_folder);
555                         for (cur = child_list; cur; cur = cur->next) 
556                                 g_free((gchar *)cur->data);
557                         for (cur = transc_list; cur; cur = cur->next) 
558                                 g_free((gchar *)cur->data);
559                         if (r == 0)
560                                 folderview_fast_rescan_tree(item->folder);
561                 } else {
562                         alertpanel_notice(_("This folder is already subscribed and "
563                                   "has no unsubscribed subfolders.\n\nIf there are new folders, "
564                                   "created and subscribed to from another client, use \"Check "
565                                   "for new folders\" at the mailbox's root folder."));
566                 }
567                 g_list_free(child_list);
568                 return;
569         }
570         message = g_markup_printf_escaped
571                 (_("Do you want to %s the '%s' folder?"),
572                    action?_("subscribe"):_("unsubscribe"), name);
573         
574         rec_chk = gtk_check_button_new_with_label(_("Apply to subfolders"));
575         
576         g_signal_connect(G_OBJECT(rec_chk), "toggled", 
577                         G_CALLBACK(chk_update_val), &recurse);
578
579         avalue = alertpanel_full(_("Subscriptions"), message,
580                                  GTK_STOCK_CANCEL, action?_("+_Subscribe"):_("+_Unsubscribe"), NULL, FALSE,
581                                  rec_chk, ALERT_QUESTION, G_ALERTDEFAULT);
582         g_free(message);
583         if (avalue != G_ALERTALTERNATE) return;
584         
585         
586         if (!action) {
587                 if (folderview->opened == folderview->selected ||
588                     gtk_cmctree_is_ancestor(ctree,
589                                           folderview->selected,
590                                           folderview->opened)) {
591                         summary_clear_all(folderview->summaryview);
592                         folderview->opened = NULL;
593                 }
594         }
595
596         if (recurse) {
597                 g_node_traverse(item->node, G_PRE_ORDER,
598                         G_TRAVERSE_ALL, -1, imap_gtk_subscribe_func, GINT_TO_POINTER(action));
599         } else {
600                 imap_subscribe(item->folder, item, NULL, action);
601         }
602
603         if (!action && item->folder->account->imap_subsonly)
604                 folderview_fast_rescan_tree(item->folder);
605 }
606
607 static void subscribe_cb(GtkAction *action, gpointer data)
608 {
609         subscribe_cb_full((FolderView *)data, 1);
610 }
611
612 static void unsubscribe_cb(GtkAction *action, gpointer data)
613 {
614         subscribe_cb_full((FolderView *)data, 0);
615 }
616
617 static void subscribed_cb(GtkAction *action, gpointer data)
618 {
619         FolderView *folderview = (FolderView *)data;
620         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
621         FolderItem *item = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
622         
623         if (!item || !item->folder || !item->folder->account)
624                 return;
625         if (item->folder->account->imap_subsonly == gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)))
626                 return;
627
628         if (folderview->opened == folderview->selected ||
629             gtk_cmctree_is_ancestor(ctree,
630                                   folderview->selected,
631                                   folderview->opened)) {
632                 summary_clear_all(folderview->summaryview);
633                 folderview->opened = NULL;
634         }
635
636         item->folder->account->imap_subsonly = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
637         folderview_fast_rescan_tree(item->folder);
638 }
639
640 static void download_cb(GtkAction *action, gpointer data)
641 {
642         FolderView *folderview = (FolderView *)data;
643         GtkCMCTree *ctree = GTK_CMCTREE(folderview->ctree);
644         FolderItem *item;
645
646         if (!folderview->selected) return;
647
648         item = gtk_cmctree_node_get_row_data(ctree, folderview->selected);
649         imap_gtk_synchronise(item, 0);
650 }