2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 Hiroyuki Yamamoto & the Claws Mail Team
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
28 #include <glib/gi18n.h>
34 #include "folder_item_prefs.h"
35 #include "folderview.h"
38 #include "alertpanel.h"
39 #include "inputdialog.h"
41 #include "foldersel.h"
42 #include "prefs_common.h"
43 #include "prefs_actions.h"
45 static void new_folder_cb(GtkAction
*action
, gpointer data
);
46 static void delete_folder_cb(GtkAction
*action
, gpointer data
);
47 static void rename_folder_cb(GtkAction
*action
, gpointer data
);
48 static void move_folder_cb(GtkAction
*action
, gpointer data
);
49 static void copy_folder_cb(GtkAction
*action
, gpointer data
);
50 static void update_tree_cb(GtkAction
*action
, gpointer data
);
51 static void remove_mailbox_cb(GtkAction
*action
, gpointer data
);
53 static GtkActionEntry mh_popup_entries
[] =
55 {"FolderViewPopup/CreateNewFolder", NULL
, N_("Create _new folder..."), NULL
, NULL
, G_CALLBACK(new_folder_cb
) },
56 {"FolderViewPopup/RenameFolder", NULL
, N_("_Rename folder..."), NULL
, NULL
, G_CALLBACK(rename_folder_cb
) },
57 {"FolderViewPopup/MoveFolder", NULL
, N_("M_ove folder..."), NULL
, NULL
, G_CALLBACK(move_folder_cb
) },
58 {"FolderViewPopup/CopyFolder", NULL
, N_("Cop_y folder..."), NULL
, NULL
, G_CALLBACK(copy_folder_cb
) },
59 {"FolderViewPopup/DeleteFolder", NULL
, N_("_Delete folder..."), NULL
, NULL
, G_CALLBACK(delete_folder_cb
) },
60 {"FolderViewPopup/CheckNewMessages", NULL
, N_("_Check for new messages"), NULL
, NULL
, G_CALLBACK(update_tree_cb
) }, /*0*/
61 {"FolderViewPopup/CheckNewFolders", NULL
, N_("C_heck for new folders"), NULL
, NULL
, G_CALLBACK(update_tree_cb
) }, /*1*/
62 {"FolderViewPopup/RebuildTree", NULL
, N_("R_ebuild folder tree"), NULL
, NULL
, G_CALLBACK(update_tree_cb
) }, /*2*/
63 {"FolderViewPopup/RemoveMailbox", NULL
, N_("Remove _mailbox..."), NULL
, NULL
, G_CALLBACK(remove_mailbox_cb
) },
65 static void set_sensitivity(GtkUIManager
*ui_manager
, FolderItem
*item
);
66 static void add_menuitems(GtkUIManager
*ui_manager
, FolderItem
*item
);
68 static FolderViewPopup mh_popup
=
73 G_N_ELEMENTS(mh_popup_entries
),
80 void mh_gtk_init(void)
82 folderview_register_popup(&mh_popup
);
85 static void add_menuitems(GtkUIManager
*ui_manager
, FolderItem
*item
)
87 MENUITEM_ADDUI_MANAGER(ui_manager
, "/Popup/FolderViewPopup", "CreateNewFolder", "FolderViewPopup/CreateNewFolder", GTK_UI_MANAGER_MENUITEM
)
88 MENUITEM_ADDUI_MANAGER(ui_manager
, "/Popup/FolderViewPopup", "SeparatorMH1", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR
)
89 MENUITEM_ADDUI_MANAGER(ui_manager
, "/Popup/FolderViewPopup", "RenameFolder", "FolderViewPopup/RenameFolder", GTK_UI_MANAGER_MENUITEM
)
90 MENUITEM_ADDUI_MANAGER(ui_manager
, "/Popup/FolderViewPopup", "MoveFolder", "FolderViewPopup/MoveFolder", GTK_UI_MANAGER_MENUITEM
)
91 MENUITEM_ADDUI_MANAGER(ui_manager
, "/Popup/FolderViewPopup", "CopyFolder", "FolderViewPopup/CopyFolder", GTK_UI_MANAGER_MENUITEM
)
92 MENUITEM_ADDUI_MANAGER(ui_manager
, "/Popup/FolderViewPopup", "SeparatorMH2", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR
)
93 MENUITEM_ADDUI_MANAGER(ui_manager
, "/Popup/FolderViewPopup", "DeleteFolder", "FolderViewPopup/DeleteFolder", GTK_UI_MANAGER_MENUITEM
)
94 MENUITEM_ADDUI_MANAGER(ui_manager
, "/Popup/FolderViewPopup", "SeparatorMH3", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR
)
95 MENUITEM_ADDUI_MANAGER(ui_manager
, "/Popup/FolderViewPopup", "CheckNewMessages", "FolderViewPopup/CheckNewMessages", GTK_UI_MANAGER_MENUITEM
)
96 MENUITEM_ADDUI_MANAGER(ui_manager
, "/Popup/FolderViewPopup", "CheckNewFolders", "FolderViewPopup/CheckNewFolders", GTK_UI_MANAGER_MENUITEM
)
97 MENUITEM_ADDUI_MANAGER(ui_manager
, "/Popup/FolderViewPopup", "RebuildTree", "FolderViewPopup/RebuildTree", GTK_UI_MANAGER_MENUITEM
)
98 MENUITEM_ADDUI_MANAGER(ui_manager
, "/Popup/FolderViewPopup", "SeparatorMH4", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR
)
99 MENUITEM_ADDUI_MANAGER(ui_manager
, "/Popup/FolderViewPopup", "RemoveMailbox", "FolderViewPopup/RemoveMailbox", GTK_UI_MANAGER_MENUITEM
)
100 MENUITEM_ADDUI_MANAGER(ui_manager
, "/Popup/FolderViewPopup", "SeparatorMH5", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR
)
103 static void set_sensitivity(GtkUIManager
*ui_manager
, FolderItem
*item
)
105 gboolean folder_is_normal
=
107 item
->stype
== F_NORMAL
&&
108 !folder_has_parent_of_type(item
, F_OUTBOX
) &&
109 !folder_has_parent_of_type(item
, F_DRAFT
) &&
110 !folder_has_parent_of_type(item
, F_QUEUE
) &&
111 !folder_has_parent_of_type(item
, F_TRASH
);
112 #define SET_SENS(name, sens) \
113 cm_menu_set_sensitive_full(ui_manager, "Popup/"name, sens)
115 SET_SENS("FolderViewPopup/CreateNewFolder", TRUE
);
116 SET_SENS("FolderViewPopup/RenameFolder", item
&& item
->stype
== F_NORMAL
&& folder_item_parent(item
) != NULL
);
117 SET_SENS("FolderViewPopup/MoveFolder", folder_is_normal
&& folder_item_parent(item
) != NULL
);
118 SET_SENS("FolderViewPopup/DeleteFolder", item
&& item
->stype
== F_NORMAL
&& folder_item_parent(item
) != NULL
);
120 SET_SENS("FolderViewPopup/CheckNewMessages", folder_item_parent(item
) == NULL
);
121 SET_SENS("FolderViewPopup/CheckNewFolders", folder_item_parent(item
) == NULL
);
122 SET_SENS("FolderViewPopup/RebuildTree", folder_item_parent(item
) == NULL
);
124 SET_SENS("FolderViewPopup/RemoveMailbox", folder_item_parent(item
) == NULL
);
129 static void new_folder_cb(GtkAction
*action
, gpointer data
)
131 FolderView
*folderview
= (FolderView
*)data
;
133 FolderItem
*new_item
;
138 if (!folderview_get_selected_item(folderview
)) return;
140 item
= folderview_get_selected_item(folderview
);
141 cm_return_if_fail(item
!= NULL
);
142 cm_return_if_fail(item
->folder
!= NULL
);
144 new_folder
= input_dialog_with_checkbtn(_("New folder"),
145 _("Input the name of new folder:"),
147 _("Inherit properties from parent folder"),
148 &(prefs_common
.inherit_folder_props
));
149 if (!new_folder
) return;
150 AUTORELEASE_STR(new_folder
, {g_free(new_folder
); return;});
152 p
= strchr(new_folder
, G_DIR_SEPARATOR
);
154 alertpanel_error(_("'%c' can't be included in folder name."),
159 if (!folder_local_name_ok(new_folder
))
162 name
= trim_string(new_folder
, 32);
163 AUTORELEASE_STR(name
, {g_free(name
); return;});
165 /* find whether the directory already exists */
166 if (folder_find_child_item_by_name(item
, new_folder
)) {
167 alertpanel_error(_("The folder '%s' already exists."), name
);
171 new_item
= folder_create_folder(item
, new_folder
);
173 alertpanel_error(_("Can't create the folder '%s'."), name
);
177 if (prefs_common
.inherit_folder_props
) {
178 folder_item_prefs_copy_prefs(item
, new_item
);
184 static void delete_folder_cb(GtkAction
*action
, gpointer data
)
186 FolderView
*folderview
= (FolderView
*)data
;
187 FolderItem
*item
, *opened
;
188 gchar
*message
, *name
;
193 item
= folderview_get_selected_item(folderview
);
194 opened
= folderview_get_opened_item(folderview
);
196 cm_return_if_fail(item
!= NULL
);
197 cm_return_if_fail(item
->path
!= NULL
);
198 cm_return_if_fail(item
->folder
!= NULL
);
200 name
= trim_string(item
->name
, 32);
201 AUTORELEASE_STR(name
, {g_free(name
); return;});
202 message
= g_markup_printf_escaped
203 (_("All folders and messages under '%s' will be permanently deleted. "
204 "Recovery will not be possible.\n\n"
205 "Do you really want to delete?"), name
);
206 avalue
= alertpanel_full(_("Delete folder"), message
,
207 GTK_STOCK_CANCEL
, GTK_STOCK_DELETE
, NULL
, FALSE
,
208 NULL
, ALERT_WARNING
, G_ALERTDEFAULT
);
210 if (avalue
!= G_ALERTALTERNATE
) return;
212 old_id
= folder_item_get_identifier(item
);
214 if (item
== opened
||
215 folder_is_child_of(item
, opened
)) {
216 summary_clear_all(folderview
->summaryview
);
217 folderview_close_opened(folderview
, TRUE
);
220 if ((ret
= item
->folder
->klass
->remove_folder(item
->folder
, item
)) < 0) {
221 folder_item_scan(item
);
222 alertpanel_error(_("Can't remove the folder '%s'\n\n%s."),
223 name
, g_strerror(-ret
));
230 prefs_filtering_delete_path(old_id
);
235 static void rename_folder_cb(GtkAction
*action
, gpointer data
)
237 FolderView
*folderview
= (FolderView
*)data
;
246 item
= folderview_get_selected_item(folderview
);
247 cm_return_if_fail(item
!= NULL
);
248 cm_return_if_fail(item
->path
!= NULL
);
249 cm_return_if_fail(item
->folder
!= NULL
);
251 name
= trim_string(item
->name
, 32);
252 message
= g_strdup_printf(_("Input new name for '%s':"), name
);
253 base
= g_path_get_basename(item
->path
);
254 new_folder
= input_dialog(_("Rename folder"), message
, base
);
258 if (!new_folder
) return;
259 AUTORELEASE_STR(new_folder
, {g_free(new_folder
); return;});
261 if (strchr(new_folder
, G_DIR_SEPARATOR
) != NULL
) {
262 alertpanel_error(_("'%c' can't be included in folder name."),
267 if (!folder_local_name_ok(new_folder
))
270 if (folder_find_child_item_by_name(folder_item_parent(item
), new_folder
)) {
271 name
= trim_string(new_folder
, 32);
272 alertpanel_error(_("The folder '%s' already exists."), name
);
277 old_id
= folder_item_get_identifier(item
);
279 if (folder_item_rename(item
, new_folder
) < 0) {
280 alertpanel_error(_("The folder could not be renamed.\n"
281 "The new folder name is not allowed."));
286 new_id
= folder_item_get_identifier(item
);
287 prefs_filtering_rename_path(old_id
, new_id
);
288 account_rename_path(old_id
, new_id
);
289 prefs_actions_rename_path(old_id
, new_id
);
293 folder_item_prefs_save_config_recursive(item
);
297 static void move_folder_cb(GtkAction
*action
, gpointer data
)
299 FolderView
*folderview
= (FolderView
*)data
;
300 FolderItem
*from_folder
= NULL
, *to_folder
= NULL
;
302 from_folder
= folderview_get_selected_item(folderview
);
303 if (!from_folder
|| from_folder
->folder
->klass
!= mh_get_class())
306 to_folder
= foldersel_folder_sel(NULL
, FOLDER_SEL_MOVE
, NULL
, TRUE
,
308 "Select folder to move selected message to",
309 "Select folder to move selected messages to",
310 summary_get_selection_count(folderview
->summaryview
))
315 folderview_move_folder(folderview
, from_folder
, to_folder
, 0);
318 static void copy_folder_cb(GtkAction
*action
, gpointer data
)
320 FolderView
*folderview
= (FolderView
*)data
;
321 FolderItem
*from_folder
= NULL
, *to_folder
= NULL
;
323 from_folder
= folderview_get_selected_item(folderview
);
324 if (!from_folder
|| from_folder
->folder
->klass
!= mh_get_class())
327 to_folder
= foldersel_folder_sel(NULL
, FOLDER_SEL_MOVE
, NULL
, TRUE
,
329 "Select folder to copy selected message to",
330 "Select folder to copy selected messages to",
331 summary_get_selection_count(folderview
->summaryview
))
336 folderview_move_folder(folderview
, from_folder
, to_folder
, 1);
339 #define DO_ACTION(name, act) { if (!strcmp(a_name, name)) act; }
341 static void update_tree_cb(GtkAction
*action
, gpointer data
)
343 FolderView
*folderview
= (FolderView
*)data
;
345 const gchar
*a_name
= gtk_action_get_name(action
);
347 item
= folderview_get_selected_item(folderview
);
348 cm_return_if_fail(item
!= NULL
);
350 summary_show(folderview
->summaryview
, NULL
);
352 cm_return_if_fail(item
->folder
!= NULL
);
354 DO_ACTION("FolderViewPopup/CheckNewMessages", folderview_check_new(item
->folder
));
355 DO_ACTION("FolderViewPopup/CheckNewFolders", folderview_rescan_tree(item
->folder
, FALSE
));
356 DO_ACTION("FolderViewPopup/RebuildTree", folderview_rescan_tree(item
->folder
, TRUE
));
359 static void remove_mailbox_cb(GtkAction
*action
, gpointer data
)
361 FolderView
*folderview
= (FolderView
*)data
;
367 item
= folderview_get_selected_item(folderview
);
368 cm_return_if_fail(item
!= NULL
);
369 cm_return_if_fail(item
->folder
!= NULL
);
370 if (folder_item_parent(item
)) return;
372 name
= trim_string(item
->folder
->name
, 32);
373 message
= g_markup_printf_escaped
374 (_("Really remove the mailbox '%s'?\n"
375 "(The messages are NOT deleted from the disk)"), name
);
376 avalue
= alertpanel_full(_("Remove mailbox"), message
,
377 GTK_STOCK_CANCEL
, _("_Remove"), NULL
, FALSE
,
378 NULL
, ALERT_WARNING
, G_ALERTDEFAULT
);
382 if (avalue
!= G_ALERTALTERNATE
) return;
384 folderview_unselect(folderview
);
385 summary_clear_all(folderview
->summaryview
);
387 folder_destroy(item
->folder
);