2004-11-13 [paul] 0.9.12cvs146.7
[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         gchar *base;
167
168         item = folderview_get_selected_item(folderview);
169         g_return_if_fail(item != NULL);
170         g_return_if_fail(item->path != NULL);
171         g_return_if_fail(item->folder != NULL);
172
173         name = trim_string(item->name, 32);
174         message = g_strdup_printf(_("Input new name for `%s':"), name);
175         base = g_path_get_basename(item->path);
176         new_folder = input_dialog(_("Rename folder"), message, base);
177         g_free(base);
178         g_free(message);
179         g_free(name);
180         if (!new_folder) return;
181         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
182
183 /*
184         TODO: check new name for IMAP namespace separator
185         if (strchr(new_folder, G_DIR_SEPARATOR) != NULL) {
186                 alertpanel_error(_("`%c' can't be included in folder name."),
187                                  G_DIR_SEPARATOR);
188                 return;
189         }
190 */
191         if (folder_find_child_item_by_name(folder_item_parent(item), new_folder)) {
192                 name = trim_string(new_folder, 32);
193                 alertpanel_error(_("The folder `%s' already exists."), name);
194                 g_free(name);
195                 return;
196         }
197
198         Xstrdup_a(old_path, item->path, {g_free(new_folder); return;});
199
200         old_id = folder_item_get_identifier(item);
201         
202         if (folder_item_rename(item, new_folder) < 0) {
203                 alertpanel_error(_("The folder could not be renamed.\n"
204                                    "The new folder name is not allowed."));
205                 g_free(old_id);
206                 return;
207         }
208
209         /* if (FOLDER_TYPE(item->folder) == F_MH)
210                 prefs_filtering_rename_path(old_path, item->path); */
211         new_id = folder_item_get_identifier(item);
212         prefs_filtering_rename_path(old_id, new_id);
213
214         g_free(old_id);
215         g_free(new_id);
216
217         folder_item_prefs_save_config(item);
218         folder_write_list();
219 }
220
221 static void move_folder_cb(FolderView *folderview, guint action, GtkWidget *widget)
222 {
223         FolderItem *from_folder = NULL, *to_folder = NULL;
224
225         from_folder = folderview_get_selected_item(folderview);
226         if (!from_folder || from_folder->folder->klass != imap_get_class())
227                 return;
228
229         to_folder = foldersel_folder_sel(from_folder->folder, FOLDER_SEL_MOVE, NULL);
230         if (!to_folder)
231                 return;
232         
233         folderview_move_folder(folderview, from_folder, to_folder);
234 }
235
236 static void remove_server_cb(FolderView *folderview, guint action, GtkWidget *widget)
237 {
238         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
239         FolderItem *item;
240         PrefsAccount *account;
241         gchar *name;
242         gchar *message;
243         AlertValue avalue;
244
245         if (!folderview->selected) return;
246
247         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
248         g_return_if_fail(item != NULL);
249         g_return_if_fail(item->folder != NULL);
250         g_return_if_fail(item->folder->account != NULL);
251
252         name = trim_string(item->folder->name, 32);
253         message = g_strdup_printf(_("Really delete IMAP4 account `%s'?"), name);
254         avalue = alertpanel(_("Delete IMAP4 account"), message,
255                             _("Yes"), _("+No"), NULL);
256         g_free(message);
257         g_free(name);
258
259         if (avalue != G_ALERTDEFAULT) return;
260
261         if (folderview->opened == folderview->selected ||
262             gtk_ctree_is_ancestor(ctree,
263                                   folderview->selected,
264                                   folderview->opened)) {
265                 summary_clear_all(folderview->summaryview);
266                 folderview->opened = NULL;
267         }
268
269         account = item->folder->account;
270         folderview_unselect(folderview);
271         summary_clear_all(folderview->summaryview);
272         folder_destroy(item->folder);
273         account_destroy(account);
274         account_set_menu();
275         main_window_reflect_prefs_all();
276         folder_write_list();
277 }
278
279 static void delete_folder_cb(FolderView *folderview, guint action,
280                              GtkWidget *widget)
281 {
282         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
283         FolderItem *item;
284         gchar *message, *name;
285         AlertValue avalue;
286         gchar *old_path;
287         gchar *old_id;
288
289         if (!folderview->selected) return;
290
291         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
292         g_return_if_fail(item != NULL);
293         g_return_if_fail(item->path != NULL);
294         g_return_if_fail(item->folder != NULL);
295
296         name = trim_string(item->name, 32);
297         AUTORELEASE_STR(name, {g_free(name); return;});
298         message = g_strdup_printf
299                 (_("All folder(s) and message(s) under `%s' will be deleted.\n"
300                    "Do you really want to delete?"), name);
301         avalue = alertpanel(_("Delete folder"), message,
302                             _("Yes"), _("+No"), NULL);
303         g_free(message);
304         if (avalue != G_ALERTDEFAULT) return;
305
306         Xstrdup_a(old_path, item->path, return);
307         old_id = folder_item_get_identifier(item);
308
309         if (folderview->opened == folderview->selected ||
310             gtk_ctree_is_ancestor(ctree,
311                                   folderview->selected,
312                                   folderview->opened)) {
313                 summary_clear_all(folderview->summaryview);
314                 folderview->opened = NULL;
315         }
316
317         if (item->folder->klass->remove_folder(item->folder, item) < 0) {
318                 folder_item_scan(item);
319                 alertpanel_error(_("Can't remove the folder `%s'."), name);
320                 g_free(old_id);
321                 return;
322         }
323
324         folder_write_list();
325
326         prefs_filtering_delete_path(old_id);
327         g_free(old_id);
328
329 }
330
331 static void update_tree_cb(FolderView *folderview, guint action,
332                            GtkWidget *widget)
333 {
334         FolderItem *item;
335
336         item = folderview_get_selected_item(folderview);
337         g_return_if_fail(item != NULL);
338
339         summary_show(folderview->summaryview, NULL);
340
341         g_return_if_fail(item->folder != NULL);
342
343         if (action == 0)
344                 folderview_check_new(item->folder);
345         else
346                 folderview_rescan_tree(item->folder);
347 }
348
349 static void download_cb(FolderView *folderview, guint action,
350                         GtkWidget *widget)
351 {
352         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
353         MainWindow *mainwin = folderview->mainwin;
354         FolderItem *item;
355
356         if (!folderview->selected) return;
357
358         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
359         g_return_if_fail(item != NULL);
360         g_return_if_fail(item->folder != NULL);
361 #if 0
362         if (!prefs_common.online_mode) {
363                 if (alertpanel(_("Offline"),
364                                _("You are offline. Go online?"),
365                                _("Yes"), _("No"), NULL) == G_ALERTDEFAULT)
366                         main_window_toggle_online(folderview->mainwin, TRUE);
367                 else
368                         return;
369         }
370 #endif
371         main_window_cursor_wait(mainwin);
372         inc_lock();
373         main_window_lock(mainwin);
374         gtk_widget_set_sensitive(folderview->ctree, FALSE);
375         main_window_progress_on(mainwin);
376         GTK_EVENTS_FLUSH();
377         if (folder_item_fetch_all_msg(item) < 0) {
378                 gchar *name;
379
380                 name = trim_string(item->name, 32);
381                 alertpanel_error(_("Error occurred while downloading messages in `%s'."), name);
382                 g_free(name);
383         }
384         folder_set_ui_func(item->folder, NULL, NULL);
385         main_window_progress_off(mainwin);
386         gtk_widget_set_sensitive(folderview->ctree, TRUE);
387         main_window_unlock(mainwin);
388         inc_unlock();
389         main_window_cursor_normal(mainwin);
390 }