2006-11-16 [paul] 2.6.0cvs50
[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 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
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         {"/---",                         NULL, NULL,             0, "<Separator>"},
60         {N_("/Synchronise"),             NULL, sync_cb,         0, NULL},
61         {N_("/Down_load messages"),      NULL, download_cb,      0, NULL},
62         {"/---",                         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         {"/---",                         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         gchar separator = '/';
125         
126         if (!folderview->selected) return;
127
128         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
129         g_return_if_fail(item != NULL);
130         g_return_if_fail(item->folder != NULL);
131         g_return_if_fail(item->folder->account != NULL);
132
133         new_folder = input_dialog
134                 (_("New folder"),
135                  _("Input the name of new folder:\n"
136                    "(if you want to create a folder to store subfolders\n"
137                    "and no mails, append '/' at the end of the name)"),
138                  _("NewFolder"));
139         if (!new_folder) return;
140         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
141
142         separator = imap_get_path_separator_for_item(item);
143
144         p = strchr(new_folder, separator);
145         if (p && *(p + 1) != '\0') {
146                 alertpanel_error(_("'%c' can't be included in folder name."),
147                                  separator);
148                 return;
149         }
150         p = strchr(new_folder, '/');
151         if (p && *(p + 1) != '\0') {
152                 alertpanel_error(_("'%c' can't be included in folder name."),
153                                  '/');
154                 return;
155         }
156
157         name = trim_string(new_folder, 32);
158         AUTORELEASE_STR(name, {g_free(name); return;});
159
160         /* find whether the directory already exists */
161         if (folder_find_child_item_by_name(item, new_folder)) {
162                 alertpanel_error(_("The folder '%s' already exists."), name);
163                 return;
164         }
165
166         new_item = folder_create_folder(item, new_folder);
167         if (!new_item) {
168                 alertpanel_error(_("Can't create the folder '%s'."), name);
169                 return;
170         }
171         folder_write_list();
172 }
173
174 static void rename_folder_cb(FolderView *folderview, guint action,
175                              GtkWidget *widget)
176 {
177         FolderItem *item;
178         gchar *new_folder;
179         gchar *name;
180         gchar *message;
181         gchar *old_path;
182         gchar *old_id;
183         gchar *new_id;
184         gchar *base;
185         gchar separator = '/';
186
187         item = folderview_get_selected_item(folderview);
188         g_return_if_fail(item != NULL);
189         g_return_if_fail(item->path != NULL);
190         g_return_if_fail(item->folder != NULL);
191
192         name = trim_string(item->name, 32);
193         message = g_strdup_printf(_("Input new name for '%s':"), name);
194         base = g_path_get_basename(item->path);
195         new_folder = input_dialog(_("Rename folder"), message, base);
196         g_free(base);
197         g_free(message);
198         g_free(name);
199         if (!new_folder) return;
200         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
201
202         separator = imap_get_path_separator_for_item(item);
203         if (strchr(new_folder, separator) != NULL) {
204                 alertpanel_error(_("`%c' can't be included in folder name."),
205                                  separator);
206                 return;
207         }
208         if (strchr(new_folder, '/') != NULL) {
209                 alertpanel_error(_("`%c' can't be included in folder name."),
210                                  '/');
211                 return;
212         }
213
214         if (folder_find_child_item_by_name(folder_item_parent(item), new_folder)) {
215                 name = trim_string(new_folder, 32);
216                 alertpanel_error(_("The folder '%s' already exists."), name);
217                 g_free(name);
218                 return;
219         }
220
221         Xstrdup_a(old_path, item->path, {g_free(new_folder); return;});
222
223         old_id = folder_item_get_identifier(item);
224         
225         if (folder_item_rename(item, new_folder) < 0) {
226                 alertpanel_error(_("The folder could not be renamed.\n"
227                                    "The new folder name is not allowed."));
228                 g_free(old_id);
229                 return;
230         }
231
232         /* if (FOLDER_TYPE(item->folder) == F_MH)
233                 prefs_filtering_rename_path(old_path, item->path); */
234         new_id = folder_item_get_identifier(item);
235         prefs_filtering_rename_path(old_id, new_id);
236         account_rename_path(old_id, new_id);
237
238         g_free(old_id);
239         g_free(new_id);
240
241         folder_item_prefs_save_config(item);
242         folder_write_list();
243 }
244
245 static void move_folder_cb(FolderView *folderview, guint action, GtkWidget *widget)
246 {
247         FolderItem *from_folder = NULL, *to_folder = NULL;
248
249         from_folder = folderview_get_selected_item(folderview);
250         if (!from_folder || from_folder->folder->klass != imap_get_class())
251                 return;
252
253         to_folder = foldersel_folder_sel(from_folder->folder, FOLDER_SEL_MOVE, NULL);
254         if (!to_folder)
255                 return;
256         
257         folderview_move_folder(folderview, from_folder, to_folder);
258 }
259
260 static void delete_folder_cb(FolderView *folderview, guint action,
261                              GtkWidget *widget)
262 {
263         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
264         FolderItem *item;
265         gchar *message, *name;
266         AlertValue avalue;
267         gchar *old_path;
268         gchar *old_id;
269
270         if (!folderview->selected) return;
271
272         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
273         g_return_if_fail(item != NULL);
274         g_return_if_fail(item->path != NULL);
275         g_return_if_fail(item->folder != NULL);
276
277         name = trim_string(item->name, 32);
278         AUTORELEASE_STR(name, {g_free(name); return;});
279         message = g_strdup_printf
280                 (_("All folders and messages under '%s' will be permanently deleted. "
281                    "Recovery will not be possible.\n\n"
282                    "Do you really want to delete?"), name);
283         avalue = alertpanel_full(_("Delete folder"), message,
284                                  GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
285                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
286         g_free(message);
287         if (avalue != G_ALERTALTERNATE) return;
288
289         Xstrdup_a(old_path, item->path, return);
290         old_id = folder_item_get_identifier(item);
291
292         if (folderview->opened == folderview->selected ||
293             gtk_ctree_is_ancestor(ctree,
294                                   folderview->selected,
295                                   folderview->opened)) {
296                 summary_clear_all(folderview->summaryview);
297                 folderview->opened = NULL;
298         }
299
300         if (item->folder->klass->remove_folder(item->folder, item) < 0) {
301                 folder_item_scan(item);
302                 alertpanel_error(_("Can't remove the folder '%s'."), name);
303                 g_free(old_id);
304                 return;
305         }
306
307         folder_write_list();
308
309         prefs_filtering_delete_path(old_id);
310         g_free(old_id);
311
312 }
313
314 static void update_tree_cb(FolderView *folderview, guint action,
315                            GtkWidget *widget)
316 {
317         FolderItem *item;
318
319         item = folderview_get_selected_item(folderview);
320         g_return_if_fail(item != NULL);
321
322         summary_show(folderview->summaryview, NULL);
323
324         g_return_if_fail(item->folder != NULL);
325
326         if (action == 0)
327                 folderview_check_new(item->folder);
328         else if (action == 1)
329                 folderview_rescan_tree(item->folder, FALSE);
330         else if (action == 2)
331                 folderview_rescan_tree(item->folder, TRUE);
332 }
333
334 static void sync_cb(FolderView *folderview, guint action,
335                            GtkWidget *widget)
336 {
337         FolderItem *item;
338
339         item = folderview_get_selected_item(folderview);
340         g_return_if_fail(item != NULL);
341         folder_synchronise(item->folder);
342 }
343
344 void imap_gtk_synchronise(FolderItem *item)
345 {
346         MainWindow *mainwin = mainwindow_get_mainwindow();
347         FolderView *folderview = mainwin->folderview;
348         
349         g_return_if_fail(item != NULL);
350         g_return_if_fail(item->folder != NULL);
351
352         main_window_cursor_wait(mainwin);
353         inc_lock();
354         main_window_lock(mainwin);
355         gtk_widget_set_sensitive(folderview->ctree, FALSE);
356         main_window_progress_on(mainwin);
357         GTK_EVENTS_FLUSH();
358         if (folder_item_fetch_all_msg(item) < 0) {
359                 gchar *name;
360
361                 name = trim_string(item->name, 32);
362                 alertpanel_error(_("Error occurred while downloading messages in '%s'."), name);
363                 g_free(name);
364         }
365         folder_set_ui_func(item->folder, NULL, NULL);
366         main_window_progress_off(mainwin);
367         gtk_widget_set_sensitive(folderview->ctree, TRUE);
368         main_window_unlock(mainwin);
369         inc_unlock();
370         main_window_cursor_normal(mainwin);
371
372 }
373 static void download_cb(FolderView *folderview, guint action,
374                         GtkWidget *widget)
375 {
376         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
377         FolderItem *item;
378
379         if (!folderview->selected) return;
380
381         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
382         imap_gtk_synchronise(item);
383 }