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