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