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