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