2007-04-24 [paul] 2.9.1cvs21
[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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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                    "and no mails, append '/' at the end of the 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(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)
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
383                 mlist = folder_item_get_msg_list(item);
384                 for (cur = mlist; cur != NULL; cur = cur->next) {
385                         MsgInfo *msginfo = (MsgInfo *)cur->data;
386                         imap_cache_msg(msginfo->folder, msginfo->msgnum);
387                         statusbar_progress_all(num++,total, 100);
388                         if (num % 100 == 0)
389                                 GTK_EVENTS_FLUSH();
390                 }
391
392                 statusbar_progress_all(0,0,0);
393                 procmsg_msg_list_free(mlist);
394         }
395
396         folder_set_ui_func(item->folder, NULL, NULL);
397         main_window_progress_off(mainwin);
398         gtk_widget_set_sensitive(folderview->ctree, TRUE);
399         main_window_unlock(mainwin);
400         inc_unlock();
401         main_window_cursor_normal(mainwin);
402
403 }
404
405 static void chk_update_val(GtkWidget *widget, gpointer data)
406 {
407         gboolean *val = (gboolean *)data;
408         *val = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
409 }
410
411 static gboolean imap_gtk_subscribe_func(GNode *node, gpointer data)
412 {
413         FolderItem *item = node->data;
414         gboolean action = GPOINTER_TO_INT(data);
415         
416         if (item->path)
417                 imap_subscribe(item->folder, item, NULL, action);
418
419         return FALSE;
420 }
421
422 static void subscribe_cb(FolderView *folderview, guint action,
423                            GtkWidget *widget)
424 {
425         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
426         FolderItem *item;
427         gchar *message, *name;
428         AlertValue avalue;
429         GtkWidget *rec_chk;
430         gboolean recurse = FALSE;
431
432         if (!folderview->selected) return;
433
434         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
435         g_return_if_fail(item != NULL);
436         g_return_if_fail(item->folder != NULL);
437
438         name = trim_string(item->name, 32);
439         AUTORELEASE_STR(name, {g_free(name); return;});
440         
441         if (action && item->folder->account->imap_subsonly) {
442                 GList *child_list = NULL;
443                 
444                 message = g_strdup_printf
445                         (_("Do you want to search for unsubscribed subfolders of '%s'?"),
446                          name);
447
448                 rec_chk = gtk_check_button_new_with_label(_("Search recursively"));
449
450                 g_signal_connect(G_OBJECT(rec_chk), "toggled", 
451                                 G_CALLBACK(chk_update_val), &recurse);
452
453                 avalue = alertpanel_full(_("Subscriptions"), message,
454                                          GTK_STOCK_CANCEL, _("+_Search"), NULL, FALSE,
455                                          rec_chk, ALERT_QUESTION, G_ALERTDEFAULT);
456                 g_free(message);
457                 if (avalue != G_ALERTALTERNATE) return;
458                 
459                 child_list = imap_scan_subtree(item->folder, item, TRUE, recurse);
460                 if (child_list) {
461                         GList *cur;
462                         int r = -1;
463                         gchar *msg = g_strdup_printf(_("Choose a subfolder of %s to subscribe to: "),
464                                         item->name); 
465                         gchar *child_folder = input_dialog_combo(_("Subscribe"), 
466                                         msg,
467                                         child_list->next?_("All of them"):child_list->data, child_list, TRUE);
468                         g_free(msg);
469                         if (child_folder && strcmp(child_folder, _("All of them"))) {
470                                 r = imap_subscribe(item->folder, NULL, child_folder, TRUE);
471                         } else if (child_folder) {
472                                 for (cur = child_list; cur; cur = cur->next) 
473                                         r = imap_subscribe(item->folder, NULL, (gchar *)cur->data, TRUE);
474                         }
475                         g_free(child_folder);
476                         for (cur = child_list; cur; cur = cur->next) 
477                                 g_free((gchar *)cur->data);
478                         if (r == 0)
479                                 folderview_fast_rescan_tree(item->folder);
480                 } else {
481                         alertpanel_notice(_("This folder is already subscribed and "
482                                   "has no unsubscribed subfolders."));
483                 }
484                 g_list_free(child_list);
485                 return;
486         }
487         message = g_strdup_printf
488                 (_("Do you want to %s the '%s' folder?"),
489                    action?_("subscribe"):_("unsubscribe"), name);
490         
491         rec_chk = gtk_check_button_new_with_label(_("Apply to subfolders"));
492         
493         g_signal_connect(G_OBJECT(rec_chk), "toggled", 
494                         G_CALLBACK(chk_update_val), &recurse);
495
496         avalue = alertpanel_full(_("Subscriptions"), message,
497                                  GTK_STOCK_CANCEL, action?_("+_Subscribe"):_("+_Unsubscribe"), NULL, FALSE,
498                                  rec_chk, ALERT_QUESTION, G_ALERTDEFAULT);
499         g_free(message);
500         if (avalue != G_ALERTALTERNATE) return;
501         
502         
503         if (!action) {
504                 if (folderview->opened == folderview->selected ||
505                     gtk_ctree_is_ancestor(ctree,
506                                           folderview->selected,
507                                           folderview->opened)) {
508                         summary_clear_all(folderview->summaryview);
509                         folderview->opened = NULL;
510                 }
511         }
512
513         if (recurse) {
514                 g_node_traverse(item->node, G_PRE_ORDER,
515                         G_TRAVERSE_ALL, -1, imap_gtk_subscribe_func, GINT_TO_POINTER(action));
516         } else {
517                 imap_subscribe(item->folder, item, NULL, action);
518         }
519
520         if (!action && item->folder->account->imap_subsonly)
521                 folderview_fast_rescan_tree(item->folder);
522 }
523
524 static void subscribed_cb(FolderView *folderview, guint action,
525                            GtkWidget *widget)
526 {
527         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
528         FolderItem *item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
529         
530         if (!item || !item->folder || !item->folder->account)
531                 return;
532         if (item->folder->account->imap_subsonly == GTK_CHECK_MENU_ITEM(widget)->active)
533                 return;
534
535         item->folder->account->imap_subsonly = GTK_CHECK_MENU_ITEM(widget)->active;
536         folderview_fast_rescan_tree(item->folder);
537 }
538
539 static void download_cb(FolderView *folderview, guint action,
540                         GtkWidget *widget)
541 {
542         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
543         FolderItem *item;
544
545         if (!folderview->selected) return;
546
547         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
548         imap_gtk_synchronise(item);
549 }