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