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