2006-01-14 [colin] 1.9.100cvs149
[claws.git] / src / imap_gtk.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto & the Sylpheed-Claws 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
53 static GtkItemFactoryEntry imap_popup_entries[] =
54 {
55         {N_("/Create _new folder..."),   NULL, new_folder_cb,    0, NULL},
56         {N_("/_Rename folder..."),       NULL, rename_folder_cb, 0, NULL},
57         {N_("/M_ove folder..."),         NULL, move_folder_cb,   0, NULL},
58         {N_("/_Delete folder..."),       NULL, delete_folder_cb, 0, NULL},
59         {N_("/---"),                     NULL, NULL,             0, "<Separator>"},
60         {N_("/Synchronise"),             NULL, sync_cb,         0, NULL},
61         {N_("/Down_load messages"),      NULL, download_cb,      0, NULL},
62         {N_("/---"),                     NULL, NULL,             0, "<Separator>"},
63         {N_("/_Check for new messages"), NULL, update_tree_cb,   0, NULL},
64         {N_("/C_heck for new folders"),  NULL, update_tree_cb,   1, NULL},
65         {N_("/R_ebuild folder tree"),    NULL, update_tree_cb,   2, NULL},
66         {N_("/---"),                     NULL, NULL,             0, "<Separator>"},
67 };
68
69 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item);
70
71 static FolderViewPopup imap_popup =
72 {
73         "imap",
74         "<IMAPFolder>",
75         NULL,
76         set_sensitivity
77 };
78
79 void imap_gtk_init(void)
80 {
81         guint i, n_entries;
82
83         n_entries = sizeof(imap_popup_entries) /
84                 sizeof(imap_popup_entries[0]);
85         for (i = 0; i < n_entries; i++)
86                 imap_popup.entries = g_slist_append(imap_popup.entries, &imap_popup_entries[i]);
87
88         folderview_register_popup(&imap_popup);
89 }
90
91 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item)
92 {
93         gboolean folder_is_normal = 
94                         item != NULL &&
95                         item->stype == F_NORMAL &&
96                         !folder_has_parent_of_type(item, F_OUTBOX) &&
97                         !folder_has_parent_of_type(item, F_DRAFT) &&
98                         !folder_has_parent_of_type(item, F_QUEUE) &&
99                         !folder_has_parent_of_type(item, F_TRASH);
100 #define SET_SENS(name, sens) \
101         menu_set_sensitive(factory, name, sens)
102
103         SET_SENS("/Create new folder...",   item->no_sub == FALSE);
104         SET_SENS("/Rename folder...",       item->stype == F_NORMAL && folder_item_parent(item) != NULL);
105         SET_SENS("/Move folder...",         folder_is_normal && folder_item_parent(item) != NULL);
106         SET_SENS("/Delete folder...",       item->stype == F_NORMAL && folder_item_parent(item) != NULL);
107
108         SET_SENS("/Check for new messages", folder_item_parent(item) == NULL);
109         SET_SENS("/Check for new folders",  folder_item_parent(item) == NULL);
110         SET_SENS("/Rebuild folder tree",    folder_item_parent(item) == NULL);
111
112 #undef SET_SENS
113 }
114
115 static void new_folder_cb(FolderView *folderview, guint action,
116                           GtkWidget *widget)
117 {
118         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
119         FolderItem *item;
120         FolderItem *new_item;
121         gchar *new_folder;
122         gchar *name;
123         gchar *p;
124
125         if (!folderview->selected) return;
126
127         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
128         g_return_if_fail(item != NULL);
129         g_return_if_fail(item->folder != NULL);
130         g_return_if_fail(item->folder->account != NULL);
131
132         new_folder = input_dialog
133                 (_("New folder"),
134                  _("Input the name of new folder:\n"
135                    "(if you want to create a folder to store subfolders\n"
136                    "and no mails, append '/' at the end of the name)"),
137                  _("NewFolder"));
138         if (!new_folder) return;
139         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
140
141         p = strchr(new_folder, G_DIR_SEPARATOR);
142         if (p && *(p + 1) != '\0') {
143                 alertpanel_error(_("'%c' can't be included in folder name."),
144                                  G_DIR_SEPARATOR);
145                 return;
146         }
147
148         name = trim_string(new_folder, 32);
149         AUTORELEASE_STR(name, {g_free(name); return;});
150
151         /* find whether the directory already exists */
152         if (folder_find_child_item_by_name(item, new_folder)) {
153                 alertpanel_error(_("The folder '%s' already exists."), name);
154                 return;
155         }
156
157         new_item = folder_create_folder(item, new_folder);
158         if (!new_item) {
159                 alertpanel_error(_("Can't create the folder '%s'."), name);
160                 return;
161         }
162         folder_write_list();
163 }
164
165 static void rename_folder_cb(FolderView *folderview, guint action,
166                              GtkWidget *widget)
167 {
168         FolderItem *item;
169         gchar *new_folder;
170         gchar *name;
171         gchar *message;
172         gchar *old_path;
173         gchar *old_id;
174         gchar *new_id;
175         gchar *base;
176
177         item = folderview_get_selected_item(folderview);
178         g_return_if_fail(item != NULL);
179         g_return_if_fail(item->path != NULL);
180         g_return_if_fail(item->folder != NULL);
181
182         name = trim_string(item->name, 32);
183         message = g_strdup_printf(_("Input new name for '%s':"), name);
184         base = g_path_get_basename(item->path);
185         new_folder = input_dialog(_("Rename folder"), message, base);
186         g_free(base);
187         g_free(message);
188         g_free(name);
189         if (!new_folder) return;
190         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
191
192 /*
193         TODO: check new name for IMAP namespace separator
194         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
195                 alertpanel_error(_("`%c' can't be included in folder name."),
196                                  G_DIR_SEPARATOR);
197                 return;
198         }
199 */
200         if (folder_find_child_item_by_name(folder_item_parent(item), new_folder)) {
201                 name = trim_string(new_folder, 32);
202                 alertpanel_error(_("The folder '%s' already exists."), name);
203                 g_free(name);
204                 return;
205         }
206
207         Xstrdup_a(old_path, item->path, {g_free(new_folder); return;});
208
209         old_id = folder_item_get_identifier(item);
210         
211         if (folder_item_rename(item, new_folder) < 0) {
212                 alertpanel_error(_("The folder could not be renamed.\n"
213                                    "The new folder name is not allowed."));
214                 g_free(old_id);
215                 return;
216         }
217
218         /* if (FOLDER_TYPE(item->folder) == F_MH)
219                 prefs_filtering_rename_path(old_path, item->path); */
220         new_id = folder_item_get_identifier(item);
221         prefs_filtering_rename_path(old_id, new_id);
222
223         g_free(old_id);
224         g_free(new_id);
225
226         folder_item_prefs_save_config(item);
227         folder_write_list();
228 }
229
230 static void move_folder_cb(FolderView *folderview, guint action, GtkWidget *widget)
231 {
232         FolderItem *from_folder = NULL, *to_folder = NULL;
233
234         from_folder = folderview_get_selected_item(folderview);
235         if (!from_folder || from_folder->folder->klass != imap_get_class())
236                 return;
237
238         to_folder = foldersel_folder_sel(from_folder->folder, FOLDER_SEL_MOVE, NULL);
239         if (!to_folder)
240                 return;
241         
242         folderview_move_folder(folderview, from_folder, to_folder);
243 }
244
245 static void delete_folder_cb(FolderView *folderview, guint action,
246                              GtkWidget *widget)
247 {
248         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
249         FolderItem *item;
250         gchar *message, *name;
251         AlertValue avalue;
252         gchar *old_path;
253         gchar *old_id;
254
255         if (!folderview->selected) return;
256
257         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
258         g_return_if_fail(item != NULL);
259         g_return_if_fail(item->path != NULL);
260         g_return_if_fail(item->folder != NULL);
261
262         name = trim_string(item->name, 32);
263         AUTORELEASE_STR(name, {g_free(name); return;});
264         message = g_strdup_printf
265                 (_("All folders and messages under '%s' will be permanently deleted. "
266                    "Recovery will not be possible.\n\n"
267                    "Do you really want to delete?"), name);
268         avalue = alertpanel_full(_("Delete folder"), message,
269                                  GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
270                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
271         g_free(message);
272         if (avalue != G_ALERTALTERNATE) return;
273
274         Xstrdup_a(old_path, item->path, return);
275         old_id = folder_item_get_identifier(item);
276
277         if (folderview->opened == folderview->selected ||
278             gtk_ctree_is_ancestor(ctree,
279                                   folderview->selected,
280                                   folderview->opened)) {
281                 summary_clear_all(folderview->summaryview);
282                 folderview->opened = NULL;
283         }
284
285         if (item->folder->klass->remove_folder(item->folder, item) < 0) {
286                 folder_item_scan(item);
287                 alertpanel_error(_("Can't remove the folder '%s'."), name);
288                 g_free(old_id);
289                 return;
290         }
291
292         folder_write_list();
293
294         prefs_filtering_delete_path(old_id);
295         g_free(old_id);
296
297 }
298
299 static void update_tree_cb(FolderView *folderview, guint action,
300                            GtkWidget *widget)
301 {
302         FolderItem *item;
303
304         item = folderview_get_selected_item(folderview);
305         g_return_if_fail(item != NULL);
306
307         summary_show(folderview->summaryview, NULL);
308
309         g_return_if_fail(item->folder != NULL);
310
311         if (action == 0)
312                 folderview_check_new(item->folder);
313         else if (action == 1)
314                 folderview_rescan_tree(item->folder, FALSE);
315         else if (action == 2)
316                 folderview_rescan_tree(item->folder, TRUE);
317 }
318
319 static void sync_cb(FolderView *folderview, guint action,
320                            GtkWidget *widget)
321 {
322         FolderItem *item;
323
324         item = folderview_get_selected_item(folderview);
325         g_return_if_fail(item != NULL);
326         folder_synchronise(item->folder);
327 }
328
329 void imap_gtk_synchronise(FolderItem *item)
330 {
331         MainWindow *mainwin = mainwindow_get_mainwindow();
332         FolderView *folderview = mainwin->folderview;
333         
334         g_return_if_fail(item != NULL);
335         g_return_if_fail(item->folder != NULL);
336
337         main_window_cursor_wait(mainwin);
338         inc_lock();
339         main_window_lock(mainwin);
340         gtk_widget_set_sensitive(folderview->ctree, FALSE);
341         main_window_progress_on(mainwin);
342         GTK_EVENTS_FLUSH();
343         if (folder_item_fetch_all_msg(item) < 0) {
344                 gchar *name;
345
346                 name = trim_string(item->name, 32);
347                 alertpanel_error(_("Error occurred while downloading messages in '%s'."), name);
348                 g_free(name);
349         }
350         folder_set_ui_func(item->folder, NULL, NULL);
351         main_window_progress_off(mainwin);
352         gtk_widget_set_sensitive(folderview->ctree, TRUE);
353         main_window_unlock(mainwin);
354         inc_unlock();
355         main_window_cursor_normal(mainwin);
356
357 }
358 static void download_cb(FolderView *folderview, guint action,
359                         GtkWidget *widget)
360 {
361         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
362         FolderItem *item;
363
364         if (!folderview->selected) return;
365
366         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
367         imap_gtk_synchronise(item);
368 }