2005-01-04 [christoph] 0.9.13cvs27
[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 imap_settings_cb(FolderView *folderview, guint action, GtkWidget *widget);
46 static void remove_server_cb(FolderView *folderview, guint action, GtkWidget *widget);
47 static void delete_folder_cb(FolderView *folderview, guint action, GtkWidget *widget);
48 static void update_tree_cb(FolderView *folderview, guint action, GtkWidget *widget);
49 static void download_cb(FolderView *folderview, guint action, GtkWidget *widget);
50
51 static GtkItemFactoryEntry imap_popup_entries[] =
52 {
53         {N_("/Create _new folder..."),   NULL, new_folder_cb,    0, NULL},
54         {N_("/_Rename folder..."),       NULL, rename_folder_cb, 0, NULL},
55         {N_("/M_ove folder..."),         NULL, move_folder_cb,   0, NULL},
56         {N_("/_Delete folder"),          NULL, delete_folder_cb, 0, NULL},
57         {N_("/---"),                     NULL, NULL,             0, "<Separator>"},
58         {N_("/Down_load messages"),      NULL, download_cb,      0, NULL},
59         {N_("/---"),                     NULL, NULL,             0, "<Separator>"},
60         {N_("/_Check for new messages"), NULL, update_tree_cb,   0, NULL},
61         {N_("/R_ebuild folder tree"),    NULL, update_tree_cb,   1, NULL},
62         {N_("/---"),                     NULL, NULL,             0, "<Separator>"},
63         {N_("/IMAP4 _account settings"), NULL, imap_settings_cb, 0, NULL},
64         {N_("/Remove _IMAP4 account"),   NULL, remove_server_cb, 0, NULL},
65         {N_("/---"),                     NULL, NULL,             0, "<Separator>"},
66 };
67
68 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item);
69
70 static FolderViewPopup imap_popup =
71 {
72         "imap",
73         "<IMAPFolder>",
74         NULL,
75         set_sensitivity
76 };
77
78 void imap_gtk_init(void)
79 {
80         guint i, n_entries;
81
82         n_entries = sizeof(imap_popup_entries) /
83                 sizeof(imap_popup_entries[0]);
84         for (i = 0; i < n_entries; i++)
85                 imap_popup.entries = g_slist_append(imap_popup.entries, &imap_popup_entries[i]);
86
87         folderview_register_popup(&imap_popup);
88 }
89
90 static void set_sensitivity(GtkItemFactory *factory, FolderItem *item)
91 {
92 #define SET_SENS(name, sens) \
93         menu_set_sensitive(factory, name, sens)
94
95         SET_SENS("/Create new folder...",   TRUE);
96         SET_SENS("/Rename folder...",       item->stype == F_NORMAL && folder_item_parent(item) != NULL);
97         SET_SENS("/Move folder...",         item->stype == F_NORMAL && folder_item_parent(item) != NULL);
98         SET_SENS("/Delete folder",          item->stype == F_NORMAL && folder_item_parent(item) != NULL);
99
100         SET_SENS("/Check for new messages", folder_item_parent(item) == NULL);
101         SET_SENS("/Rebuild folder tree",    folder_item_parent(item) == NULL);
102
103         SET_SENS("/Remove IMAP4 account",   folder_item_parent(item) == NULL);
104
105 #undef SET_SENS
106 }
107
108 static void new_folder_cb(FolderView *folderview, guint action,
109                           GtkWidget *widget)
110 {
111         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
112         FolderItem *item;
113         FolderItem *new_item;
114         gchar *new_folder;
115         gchar *name;
116         gchar *p;
117
118         if (!folderview->selected) return;
119
120         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
121         g_return_if_fail(item != NULL);
122         g_return_if_fail(item->folder != NULL);
123         g_return_if_fail(item->folder->account != NULL);
124
125         new_folder = input_dialog
126                 (_("New folder"),
127                  _("Input the name of new folder:\n"
128                    "(if you want to create a folder to store subfolders,\n"
129                    " append `/' at the end of the name)"),
130                  _("NewFolder"));
131         if (!new_folder) return;
132         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
133
134         p = strchr(new_folder, G_DIR_SEPARATOR);
135         if (p && *(p + 1) != '\0') {
136                 alertpanel_error(_("`%c' can't be included in folder name."),
137                                  G_DIR_SEPARATOR);
138                 return;
139         }
140
141         name = trim_string(new_folder, 32);
142         AUTORELEASE_STR(name, {g_free(name); return;});
143
144         /* find whether the directory already exists */
145         if (folder_find_child_item_by_name(item, new_folder)) {
146                 alertpanel_error(_("The folder `%s' already exists."), name);
147                 return;
148         }
149
150         new_item = folder_create_folder(item, new_folder);
151         if (!new_item) {
152                 alertpanel_error(_("Can't create the folder `%s'."), name);
153                 return;
154         }
155         folder_write_list();
156 }
157
158 static void rename_folder_cb(FolderView *folderview, guint action,
159                              GtkWidget *widget)
160 {
161         FolderItem *item;
162         gchar *new_folder;
163         gchar *name;
164         gchar *message;
165         gchar *old_path;
166         gchar *old_id;
167         gchar *new_id;
168
169         item = folderview_get_selected_item(folderview);
170         g_return_if_fail(item != NULL);
171         g_return_if_fail(item->path != NULL);
172         g_return_if_fail(item->folder != NULL);
173
174         name = trim_string(item->name, 32);
175         message = g_strdup_printf(_("Input new name for `%s':"), name);
176         new_folder = input_dialog(_("Rename folder"), message,
177                                   g_basename(item->path));
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 imap_settings_cb(FolderView *folderview, guint action, GtkWidget *widget)
237 {
238         FolderItem *item;
239
240         item = folderview_get_selected_item(folderview);
241         if (item == NULL)
242                 return;
243
244         account_open(item->folder->account);
245 }
246
247 static void remove_server_cb(FolderView *folderview, guint action, GtkWidget *widget)
248 {
249         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
250         FolderItem *item;
251         PrefsAccount *account;
252         gchar *name;
253         gchar *message;
254         AlertValue avalue;
255
256         if (!folderview->selected) return;
257
258         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
259         g_return_if_fail(item != NULL);
260         g_return_if_fail(item->folder != NULL);
261         g_return_if_fail(item->folder->account != NULL);
262
263         name = trim_string(item->folder->name, 32);
264         message = g_strdup_printf(_("Really delete IMAP4 account `%s'?"), name);
265         avalue = alertpanel(_("Delete IMAP4 account"), message,
266                             _("Yes"), _("+No"), NULL);
267         g_free(message);
268         g_free(name);
269
270         if (avalue != G_ALERTDEFAULT) return;
271
272         if (folderview->opened == folderview->selected ||
273             gtk_ctree_is_ancestor(ctree,
274                                   folderview->selected,
275                                   folderview->opened)) {
276                 summary_clear_all(folderview->summaryview);
277                 folderview->opened = NULL;
278         }
279
280         account = item->folder->account;
281         folderview_unselect(folderview);
282         summary_clear_all(folderview->summaryview);
283         folder_destroy(item->folder);
284         account_destroy(account);
285         account_set_menu();
286         main_window_reflect_prefs_all();
287         folder_write_list();
288 }
289
290 static void delete_folder_cb(FolderView *folderview, guint action,
291                              GtkWidget *widget)
292 {
293         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
294         FolderItem *item;
295         gchar *message, *name;
296         AlertValue avalue;
297         gchar *old_path;
298         gchar *old_id;
299
300         if (!folderview->selected) return;
301
302         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
303         g_return_if_fail(item != NULL);
304         g_return_if_fail(item->path != NULL);
305         g_return_if_fail(item->folder != NULL);
306
307         name = trim_string(item->name, 32);
308         AUTORELEASE_STR(name, {g_free(name); return;});
309         message = g_strdup_printf
310                 (_("All folder(s) and message(s) under `%s' will be deleted.\n"
311                    "Do you really want to delete?"), name);
312         avalue = alertpanel(_("Delete folder"), message,
313                             _("Yes"), _("+No"), NULL);
314         g_free(message);
315         if (avalue != G_ALERTDEFAULT) return;
316
317         Xstrdup_a(old_path, item->path, return);
318         old_id = folder_item_get_identifier(item);
319
320         if (folderview->opened == folderview->selected ||
321             gtk_ctree_is_ancestor(ctree,
322                                   folderview->selected,
323                                   folderview->opened)) {
324                 summary_clear_all(folderview->summaryview);
325                 folderview->opened = NULL;
326         }
327
328         if (item->folder->klass->remove_folder(item->folder, item) < 0) {
329                 folder_item_scan(item);
330                 alertpanel_error(_("Can't remove the folder `%s'."), name);
331                 g_free(old_id);
332                 return;
333         }
334
335         folder_write_list();
336
337         prefs_filtering_delete_path(old_id);
338         g_free(old_id);
339
340 }
341
342 static void update_tree_cb(FolderView *folderview, guint action,
343                            GtkWidget *widget)
344 {
345         FolderItem *item;
346
347         item = folderview_get_selected_item(folderview);
348         g_return_if_fail(item != NULL);
349
350         summary_show(folderview->summaryview, NULL);
351
352         g_return_if_fail(item->folder != NULL);
353
354         if (action == 0)
355                 folderview_check_new(item->folder);
356         else
357                 folderview_rescan_tree(item->folder);
358 }
359
360 static void download_cb(FolderView *folderview, guint action,
361                         GtkWidget *widget)
362 {
363         GtkCTree *ctree = GTK_CTREE(folderview->ctree);
364         MainWindow *mainwin = folderview->mainwin;
365         FolderItem *item;
366
367         if (!folderview->selected) return;
368
369         item = gtk_ctree_node_get_row_data(ctree, folderview->selected);
370         g_return_if_fail(item != NULL);
371         g_return_if_fail(item->folder != NULL);
372 #if 0
373         if (!prefs_common.online_mode) {
374                 if (alertpanel(_("Offline"),
375                                _("You are offline. Go online?"),
376                                _("Yes"), _("No"), NULL) == G_ALERTDEFAULT)
377                         main_window_toggle_online(folderview->mainwin, TRUE);
378                 else
379                         return;
380         }
381 #endif
382         main_window_cursor_wait(mainwin);
383         inc_lock();
384         main_window_lock(mainwin);
385         gtk_widget_set_sensitive(folderview->ctree, FALSE);
386         main_window_progress_on(mainwin);
387         GTK_EVENTS_FLUSH();
388         if (folder_item_fetch_all_msg(item) < 0) {
389                 gchar *name;
390
391                 name = trim_string(item->name, 32);
392                 alertpanel_error(_("Error occurred while downloading messages in `%s'."), name);
393                 g_free(name);
394         }
395         folder_set_ui_func(item->folder, NULL, NULL);
396         main_window_progress_off(mainwin);
397         gtk_widget_set_sensitive(folderview->ctree, TRUE);
398         main_window_unlock(mainwin);
399         inc_unlock();
400         main_window_cursor_normal(mainwin);
401 }