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