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