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