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