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