2006-01-16 [colin] 1.9.100cvs160
[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         account_rename_path(old_id, new_id);
223
224         g_free(old_id);
225         g_free(new_id);
226
227         folder_item_prefs_save_config(item);
228         folder_write_list();
229 }
230
231 static void move_folder_cb(FolderView *folderview, guint action, GtkWidget *widget)
232 {
233         FolderItem *from_folder = NULL, *to_folder = NULL;
234
235         from_folder = folderview_get_selected_item(folderview);
236         if (!from_folder || from_folder->folder->klass != imap_get_class())
237                 return;
238
239         to_folder = foldersel_folder_sel(from_folder->folder, FOLDER_SEL_MOVE, NULL);
240         if (!to_folder)
241                 return;
242         
243         folderview_move_folder(folderview, from_folder, to_folder);
244 }
245
246 static void delete_folder_cb(FolderView *folderview, guint action,
247                              GtkWidget *widget)
248 {
249         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
250         FolderItem *item;
251         gchar *message, *name;
252         AlertValue avalue;
253         gchar *old_path;
254         gchar *old_id;
255
256         if (!folderview->selected) return;
257
258         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
259         g_return_if_fail(item != NULL);
260         g_return_if_fail(item->path != NULL);
261         g_return_if_fail(item->folder != NULL);
262
263         name = trim_string(item->name, 32);
264         AUTORELEASE_STR(name, {g_free(name); return;});
265         message = g_strdup_printf
266                 (_("All folders and messages under '%s' will be permanently deleted. "
267                    "Recovery will not be possible.\n\n"
268                    "Do you really want to delete?"), name);
269         avalue = alertpanel_full(_("Delete folder"), message,
270                                  GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
271                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
272         g_free(message);
273         if (avalue != G_ALERTALTERNATE) return;
274
275         Xstrdup_a(old_path, item->path, return);
276         old_id = folder_item_get_identifier(item);
277
278         if (folderview->opened == folderview->selected ||
279             gtk_ctree_is_ancestor(ctree,
280                                   folderview->selected,
281                                   folderview->opened)) {
282                 summary_clear_all(folderview->summaryview);
283                 folderview->opened = NULL;
284         }
285
286         if (item->folder->klass->remove_folder(item->folder, item) < 0) {
287                 folder_item_scan(item);
288                 alertpanel_error(_("Can't remove the folder '%s'."), name);
289                 g_free(old_id);
290                 return;
291         }
292
293         folder_write_list();
294
295         prefs_filtering_delete_path(old_id);
296         g_free(old_id);
297
298 }
299
300 static void update_tree_cb(FolderView *folderview, guint action,
301                            GtkWidget *widget)
302 {
303         FolderItem *item;
304
305         item = folderview_get_selected_item(folderview);
306         g_return_if_fail(item != NULL);
307
308         summary_show(folderview->summaryview, NULL);
309
310         g_return_if_fail(item->folder != NULL);
311
312         if (action == 0)
313                 folderview_check_new(item->folder);
314         else if (action == 1)
315                 folderview_rescan_tree(item->folder, FALSE);
316         else if (action == 2)
317                 folderview_rescan_tree(item->folder, TRUE);
318 }
319
320 static void sync_cb(FolderView *folderview, guint action,
321                            GtkWidget *widget)
322 {
323         FolderItem *item;
324
325         item = folderview_get_selected_item(folderview);
326         g_return_if_fail(item != NULL);
327         folder_synchronise(item->folder);
328 }
329
330 void imap_gtk_synchronise(FolderItem *item)
331 {
332         MainWindow *mainwin = mainwindow_get_mainwindow();
333         FolderView *folderview = mainwin->folderview;
334         
335         g_return_if_fail(item != NULL);
336         g_return_if_fail(item->folder != NULL);
337
338         main_window_cursor_wait(mainwin);
339         inc_lock();
340         main_window_lock(mainwin);
341         gtk_widget_set_sensitive(folderview->ctree, FALSE);
342         main_window_progress_on(mainwin);
343         GTK_EVENTS_FLUSH();
344         if (folder_item_fetch_all_msg(item) < 0) {
345                 gchar *name;
346
347                 name = trim_string(item->name, 32);
348                 alertpanel_error(_("Error occurred while downloading messages in '%s'."), name);
349                 g_free(name);
350         }
351         folder_set_ui_func(item->folder, NULL, NULL);
352         main_window_progress_off(mainwin);
353         gtk_widget_set_sensitive(folderview->ctree, TRUE);
354         main_window_unlock(mainwin);
355         inc_unlock();
356         main_window_cursor_normal(mainwin);
357
358 }
359 static void download_cb(FolderView *folderview, guint action,
360                         GtkWidget *widget)
361 {
362         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
363         FolderItem *item;
364
365         if (!folderview->selected) return;
366
367         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
368         imap_gtk_synchronise(item);
369 }