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