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