Allow custom window title for the folder selection dialog.
[claws.git] / src / plugins / mailmbox / plugin_gtk.c
1 /*
2  * mailmbox Plugin -- mbox support for Sylpheed
3  * Copyright (C) 2003-2005 Christoph Hohmann, 
4  *                         Hoa v. Dinh, 
5  *                         Alfons Hoogervorst
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #  include "claws-features.h"
26 #endif
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30
31 #include <gtk/gtk.h>
32
33 #include "plugin.h"
34 #include "folder.h"
35 #include "mailmbox_folder.h"
36 #include "mainwindow.h"
37 #include "folderview.h"
38 #include "inputdialog.h"
39 #include "foldersel.h"
40 #include "alertpanel.h"
41 #include "main.h"
42 #include "menu.h"
43 #include "account.h"
44 #include "prefs_actions.h"
45 #include "summaryview.h"
46 #include "folder_item_prefs.h"
47
48 static void add_mailbox(GtkAction *action, gpointer callback_data);
49 static void new_folder_cb(GtkAction *action, gpointer data);
50 static void delete_folder_cb(GtkAction *action, gpointer data);
51 static void rename_folder_cb(GtkAction *action, gpointer data);
52 static void move_folder_cb(GtkAction *action, gpointer data);
53 static void copy_folder_cb(GtkAction *action, gpointer data);
54 static void update_tree_cb(GtkAction *action, gpointer data);
55 static void remove_mailbox_cb(GtkAction *action, gpointer data);
56
57 static GtkActionEntry claws_mailmbox_popup_entries[] = 
58 {
59         {"FolderViewPopup/CreateNewFolder",     NULL, N_("Create _new folder..."), NULL, NULL, G_CALLBACK(new_folder_cb) },
60         {"FolderViewPopup/RenameFolder",        NULL, N_("_Rename folder..."), NULL, NULL, G_CALLBACK(rename_folder_cb) },
61         {"FolderViewPopup/MoveFolder",          NULL, N_("M_ove folder..."), NULL, NULL, G_CALLBACK(move_folder_cb) },
62         {"FolderViewPopup/CopyFolder",          NULL, N_("Cop_y folder..."), NULL, NULL, G_CALLBACK(copy_folder_cb) },
63         {"FolderViewPopup/DeleteFolder",        NULL, N_("_Delete folder..."), NULL, NULL, G_CALLBACK(delete_folder_cb) },
64         {"FolderViewPopup/CheckNewMessages",    NULL, N_("_Check for new messages"), NULL, NULL, G_CALLBACK(update_tree_cb) }, /*0*/
65         {"FolderViewPopup/CheckNewFolders",     NULL, N_("C_heck for new folders"), NULL, NULL, G_CALLBACK(update_tree_cb) }, /*1*/
66         {"FolderViewPopup/RebuildTree",         NULL, N_("R_ebuild folder tree"), NULL, NULL, G_CALLBACK(update_tree_cb) }, /*2*/
67         {"FolderViewPopup/RemoveMailbox",       NULL, N_("Remove _mailbox..."), NULL, NULL, G_CALLBACK(remove_mailbox_cb) },
68 };                      
69 static void set_sensitivity(GtkUIManager *ui_manager, FolderItem *item);
70 static void add_menuitems(GtkUIManager *ui_manager, FolderItem *item);
71
72 static FolderViewPopup claws_mailmbox_popup =
73 {
74         "mailmbox",
75         "<MailmboxFolder>",
76         claws_mailmbox_popup_entries,
77         G_N_ELEMENTS(claws_mailmbox_popup_entries),
78         NULL, 0,
79         NULL, 0, 0, NULL,
80         add_menuitems,
81         set_sensitivity
82 };
83
84 static GtkActionEntry mainwindow_add_mailbox[] = {{
85         "File/AddMailbox/Mbox",
86         NULL, "mbox...", NULL, NULL, G_CALLBACK(add_mailbox)
87 }};
88
89 static guint main_menu_id = 0;
90
91 gint plugin_gtk_init(gchar **error)
92 {
93         MainWindow *mainwin = mainwindow_get_mainwindow();
94
95         folderview_register_popup(&claws_mailmbox_popup);
96
97         gtk_action_group_add_actions(mainwin->action_group, mainwindow_add_mailbox,
98                         1, (gpointer)mainwin);
99         MENUITEM_ADDUI_ID_MANAGER(mainwin->ui_manager, "/Menu/File/AddMailbox", "Mbox", 
100                           "File/AddMailbox/Mbox", GTK_UI_MANAGER_MENUITEM,
101                           main_menu_id)
102
103         return 0;
104 }
105
106 void plugin_gtk_done(void)
107 {
108         MainWindow *mainwin = mainwindow_get_mainwindow();
109         
110         if (mainwin == NULL || claws_is_exiting())
111                 return;
112
113         folderview_unregister_popup(&claws_mailmbox_popup);
114
115         MENUITEM_REMUI_MANAGER(mainwin->ui_manager,mainwin->action_group, "File/AddMailbox/Mbox", main_menu_id);
116         main_menu_id = 0;
117 }
118
119 static void add_menuitems(GtkUIManager *ui_manager, FolderItem *item)
120 {
121         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "CreateNewFolder", "FolderViewPopup/CreateNewFolder", GTK_UI_MANAGER_MENUITEM)
122         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorMbox1", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
123         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RenameFolder", "FolderViewPopup/RenameFolder", GTK_UI_MANAGER_MENUITEM)
124         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "MoveFolder", "FolderViewPopup/MoveFolder", GTK_UI_MANAGER_MENUITEM)
125         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "CopyFolder", "FolderViewPopup/CopyFolder", GTK_UI_MANAGER_MENUITEM)
126         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorMbox2", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
127         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "DeleteFolder", "FolderViewPopup/DeleteFolder", GTK_UI_MANAGER_MENUITEM)
128         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorMbox3", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
129         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "CheckNewMessages", "FolderViewPopup/CheckNewMessages", GTK_UI_MANAGER_MENUITEM)
130         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "CheckNewFolders", "FolderViewPopup/CheckNewFolders", GTK_UI_MANAGER_MENUITEM)
131         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RebuildTree", "FolderViewPopup/RebuildTree", GTK_UI_MANAGER_MENUITEM)
132         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorMbox4", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
133         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RemoveMailbox", "FolderViewPopup/RemoveMailbox", GTK_UI_MANAGER_MENUITEM)
134         MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorMbox5", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
135 }
136
137 static void set_sensitivity(GtkUIManager *ui_manager, FolderItem *item)
138 {
139         gboolean folder_is_normal = 
140                         item != NULL &&
141                         item->stype == F_NORMAL &&
142                         !folder_has_parent_of_type(item, F_OUTBOX) &&
143                         !folder_has_parent_of_type(item, F_DRAFT) &&
144                         !folder_has_parent_of_type(item, F_QUEUE) &&
145                         !folder_has_parent_of_type(item, F_TRASH);
146 #define SET_SENS(name, sens) \
147         cm_menu_set_sensitive_full(ui_manager, "Popup/"name, sens)
148
149         SET_SENS("FolderViewPopup/CreateNewFolder",     item && item->stype != F_INBOX);
150         SET_SENS("FolderViewPopup/RenameFolder",        item && item->stype == F_NORMAL && folder_item_parent(item) != NULL);
151         SET_SENS("FolderViewPopup/MoveFolder",          folder_is_normal && folder_item_parent(item) != NULL);
152         SET_SENS("FolderViewPopup/DeleteFolder",        item && item->stype == F_NORMAL && folder_item_parent(item) != NULL);
153
154         SET_SENS("FolderViewPopup/CheckNewMessages",    folder_item_parent(item) == NULL);
155         SET_SENS("FolderViewPopup/CheckNewFolders",     folder_item_parent(item) == NULL);
156         SET_SENS("FolderViewPopup/RebuildTree",         folder_item_parent(item) == NULL);
157
158         SET_SENS("FolderViewPopup/RemoveMailbox",       folder_item_parent(item) == NULL);
159
160 #undef SET_SENS
161 }
162
163 #define DO_ACTION(name, act)    { if (!strcmp(a_name, name)) act; }
164
165 static void update_tree_cb(GtkAction *action, gpointer data)
166 {
167         FolderView *folderview = (FolderView *)data;
168         FolderItem *item;
169         const gchar *a_name = gtk_action_get_name(action);
170
171         item = folderview_get_selected_item(folderview);
172         g_return_if_fail(item != NULL);
173
174         summary_show(folderview->summaryview, NULL);
175
176         g_return_if_fail(item->folder != NULL);
177
178         DO_ACTION("FolderViewPopup/CheckNewMessages", folderview_check_new(item->folder));
179         DO_ACTION("FolderViewPopup/CheckNewFolders", folderview_rescan_tree(item->folder, FALSE));
180         DO_ACTION("FolderViewPopup/RebuildTree", folderview_rescan_tree(item->folder, TRUE));
181 }
182
183 static void add_mailbox(GtkAction *action, gpointer callback_data)
184 {
185         MainWindow *mainwin = (MainWindow *) callback_data;
186         gchar *path, *basename;
187         Folder *folder;
188
189         path = input_dialog(_("Add mailbox"),
190                             _("Input the location of mailbox.\n"
191                               "If the existing mailbox is specified, it will be\n"
192                               "scanned automatically."),
193                             "Mail");
194         if (!path) return;
195         if (folder_find_from_path(path)) {
196                 alertpanel_error(_("The mailbox '%s' already exists."), path);
197                 g_free(path);
198                 return;
199         }
200         basename = g_path_get_basename(path);
201
202         if (!folder_local_name_ok(basename)) {
203                 g_free(path);
204                 g_free(basename);
205                 return;
206         }
207
208         folder = folder_new(folder_get_class_from_string("mailmbox"), 
209                             !strcmp(path, "Mail") ? _("Mailbox") : basename,
210                             path);
211         g_free(basename);
212         g_free(path);
213
214         if (folder->klass->create_tree(folder) < 0) {
215                 alertpanel_error(_("Creation of the mailbox failed.\n"
216                                    "Maybe some files already exist, or you don't have the permission to write there."));
217                 folder_destroy(folder);
218                 return;
219         }
220
221         folder_add(folder);
222         folder_scan_tree(folder, TRUE);
223
224         folderview_set(mainwin->folderview);
225
226         return;
227 }
228
229 static void new_folder_cb(GtkAction *action, gpointer data)
230 {
231         FolderView *folderview = (FolderView *)data;
232         FolderItem *item;
233         FolderItem *new_item;
234         gchar *new_folder;
235         gchar *name;
236         gchar *p;
237
238         if (!folderview->selected) return;
239
240         item = folderview_get_selected_item(folderview);
241         g_return_if_fail(item != NULL);
242         g_return_if_fail(item->folder != NULL);
243
244         new_folder = input_dialog(_("New folder"),
245                                   _("Input the name of new folder:"),
246                                   _("NewFolder"));
247         if (!new_folder) return;
248         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
249
250         p = strchr(new_folder, G_DIR_SEPARATOR);
251         if (p == NULL)
252                 p = strchr(new_folder, '.');
253         if (p) {
254                 alertpanel_error(_("'%c' can't be included in folder name."),
255                                  p[0]);
256                 return;
257         }
258
259         if (!folder_local_name_ok(new_folder))
260                 return;
261
262         name = trim_string(new_folder, 32);
263         AUTORELEASE_STR(name, {g_free(name); return;});
264
265         /* find whether the directory already exists */
266         p = g_strconcat(item->path ? item->path : "", ".", new_folder, NULL);
267         if (folder_find_child_item_by_name(item, p)) {
268                 g_free(p);
269                 alertpanel_error(_("The folder '%s' already exists."), name);
270                 return;
271         }
272         g_free(p);
273
274         new_item = folder_create_folder(item, new_folder);
275         if (!new_item) {
276                 alertpanel_error(_("Can't create the folder '%s'."), name);
277                 return;
278         }
279
280         folder_write_list();
281 }
282
283 static void remove_mailbox_cb(GtkAction *action, gpointer data)
284 {
285         FolderView *folderview = (FolderView *)data;
286         FolderItem *item;
287         gchar *name;
288         gchar *message;
289         AlertValue avalue;
290
291         item = folderview_get_selected_item(folderview);
292         g_return_if_fail(item != NULL);
293         g_return_if_fail(item->folder != NULL);
294         if (folder_item_parent(item)) return;
295
296         name = trim_string(item->folder->name, 32);
297         message = g_strdup_printf
298                 (_("Really remove the mailbox '%s'?\n"
299                    "(The messages are NOT deleted from the disk)"), name);
300         avalue = alertpanel_full(_("Remove mailbox"), message,
301                                  GTK_STOCK_CANCEL, _("_Remove"), NULL, FALSE,
302                                  NULL, ALERT_WARNING, G_ALERTDEFAULT);
303         g_free(message);
304         g_free(name);
305         if (avalue != G_ALERTALTERNATE) return;
306
307         folderview_unselect(folderview);
308         summary_clear_all(folderview->summaryview);
309
310         folder_destroy(item->folder);
311 }
312
313 static void delete_folder_cb(GtkAction *action, gpointer data)
314 {
315         FolderView *folderview = (FolderView *)data;
316         FolderItem *item, *opened;
317         gchar *message, *name;
318         AlertValue avalue;
319         gchar *old_id;
320
321         item = folderview_get_selected_item(folderview);
322         g_return_if_fail(item != NULL);
323         g_return_if_fail(item->path != NULL);
324         g_return_if_fail(item->folder != NULL);
325         opened = folderview_get_opened_item(folderview);
326
327         name = trim_string(item->name, 32);
328         AUTORELEASE_STR(name, {g_free(name); return;});
329         message = g_strdup_printf
330                 (_("All folders and messages under '%s' will be deleted.\n"
331                    "Do you really want to delete?"), name);
332         avalue = alertpanel_full(_("Delete folder"), message,
333                                  GTK_STOCK_CANCEL, GTK_STOCK_DELETE, NULL, FALSE,
334                                  NULL, ALERT_NOTICE, G_ALERTDEFAULT);
335         g_free(message);
336         if (avalue != G_ALERTALTERNATE) return;
337
338         old_id = folder_item_get_identifier(item);
339
340         if (item == opened ||
341                         folder_is_child_of(item, opened)) {
342                 summary_clear_all(folderview->summaryview);
343                 folderview_close_opened(folderview, TRUE);
344         }
345
346         if (item->folder->klass->remove_folder(item->folder, item) < 0) {
347                 alertpanel_error(_("Can't remove the folder '%s'."), name);
348                 if (item == opened)
349                         summary_show(folderview->summaryview,
350                                      folderview->summaryview->folder_item);
351                 g_free(old_id);
352                 return;
353         }
354
355         folder_write_list();
356
357         prefs_filtering_delete_path(old_id);
358         g_free(old_id);
359
360 }
361
362 static void move_folder_cb(GtkAction *action, gpointer data)
363 {
364         FolderView *folderview = (FolderView *)data;
365         FolderItem *from_folder = NULL, *to_folder = NULL;
366
367         from_folder = folderview_get_selected_item(folderview);
368         if (!from_folder || from_folder->folder->klass != claws_mailmbox_get_class())
369                 return;
370
371         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL, FALSE,
372                         ngettext(
373                                 "Select folder to move selected message to",
374                                 "Select folder to move selected messages to",
375                                 summary_get_selection_count(folderview->summaryview))
376         );
377         if (!to_folder)
378                 return;
379
380         folderview_move_folder(folderview, from_folder, to_folder, 0);
381 }
382
383 static void copy_folder_cb(GtkAction *action, gpointer data)
384 {
385         FolderView *folderview = (FolderView *)data;
386         FolderItem *from_folder = NULL, *to_folder = NULL;
387
388         from_folder = folderview_get_selected_item(folderview);
389         if (!from_folder || from_folder->folder->klass != claws_mailmbox_get_class())
390                 return;
391
392         to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_MOVE, NULL, FALSE,
393                         ngettext(
394                                 "Select folder to copy selected message to",
395                                 "Select folder to copy selected messages to",
396                                 summary_get_selection_count(folderview->summaryview))
397         );
398         if (!to_folder)
399                 return;
400
401         folderview_move_folder(folderview, from_folder, to_folder, 1);
402 }
403
404 static void rename_folder_cb(GtkAction *action, gpointer data)
405 {
406         FolderView *folderview = (FolderView *)data;
407         FolderItem *item, *parent;
408         gchar *new_folder;
409         gchar *name;
410         gchar *message;
411         gchar *old_id;
412         gchar *new_id;
413         gchar *p;
414
415         item = folderview_get_selected_item(folderview);
416         g_return_if_fail(item != NULL);
417         g_return_if_fail(item->path != NULL);
418         g_return_if_fail(item->folder != NULL);
419
420         name = trim_string(item->name, 32);
421         message = g_strdup_printf(_("Input new name for '%s':"), name);
422         new_folder = input_dialog(_("Rename folder"), message, item->name);
423         g_free(message);
424         g_free(name);
425         if (!new_folder) return;
426         AUTORELEASE_STR(new_folder, {g_free(new_folder); return;});
427
428         p = strchr(new_folder, G_DIR_SEPARATOR);
429         if (p == NULL)
430                 p = strchr(new_folder, '.');
431         if (p) {
432                 alertpanel_error(_("'%c' can't be included in folder name."),
433                                  p[0]);
434                 return;
435         }
436
437         if (!folder_local_name_ok(new_folder))
438                 return;
439
440         parent = folder_item_parent(item);
441         p = g_strconcat(parent->path ? parent->path : "", ".", new_folder, NULL);
442         if (folder_find_child_item_by_name(parent, p)) {
443                 name = trim_string(new_folder, 32);
444                 alertpanel_error(_("The folder '%s' already exists."), name);
445                 g_free(name);
446                 return;
447         }
448
449         old_id = folder_item_get_identifier(item);
450
451         if (folder_item_rename(item, new_folder) < 0) {
452                 alertpanel_error(_("The folder could not be renamed.\n"
453                                    "The new folder name is not allowed."));
454                 g_free(old_id);
455                 return;
456         }
457
458         new_id = folder_item_get_identifier(item);
459         prefs_filtering_rename_path(old_id, new_id);
460         account_rename_path(old_id, new_id);
461         prefs_actions_rename_path(old_id, new_id);
462
463         g_free(old_id);
464         g_free(new_id);
465
466         folder_item_prefs_save_config_recursive(item);
467         folder_write_list();
468 }
469