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