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