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