1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto
4  * This file (C) 2005 Andrej Kacian <andrej@kacian.sk>
5  *
6  * - GUI handling functions
7  *
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 2 of the License, or
11  * (at your option) any later version.
12  *
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.
17  *
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.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26 
27 /* Global includes */
28 #include <gtk/gtk.h>
29 #include <glib/gi18n.h>
30 
31 /* Claws Mail includes */
32 #include <gtk/menu.h>
33 #include <mainwindow.h>
34 #include <inputdialog.h>
35 #include <folderview.h>
36 #include <alertpanel.h>
37 #include <main.h>
38 #include <summaryview.h>
39 
40 /* Local includes */
41 #include "rssyl.h"
42 #include "rssyl_prefs.h"
43 #include "rssyl_gtk.h"
44 #include "rssyl_cb_menu.h"
45 
46 static char *rssyl_popup_menu_labels[] =
47 {
48 	N_("_Refresh feed"),
49 	N_("Feed pr_operties"),
50 	N_("Rena_me..."),
51 	N_("R_efresh recursively"),
52 	N_("Subscribe _new feed..."),
53 	N_("Create new _folder..."),
54 	N_("Import feed list..."),
55 	N_("_Delete folder..."),
56 	N_("Remove tree"),
57 	NULL
58 };
59 
60 static GtkActionEntry rssyl_popup_entries[] =
61 {
62 	{"FolderViewPopup/RefreshFeed", NULL, NULL, NULL, NULL, G_CALLBACK(rssyl_refresh_feed_cb) },
63 	{"FolderViewPopup/FeedProperties", NULL, NULL, NULL, NULL, G_CALLBACK(rssyl_prop_cb) },
64 	{"FolderViewPopup/RenameFolder", NULL, NULL, NULL, NULL, G_CALLBACK(rssyl_rename_cb) },
65 	{"FolderViewPopup/RefreshAllFeeds", NULL, NULL, NULL, NULL, G_CALLBACK(rssyl_update_all_cb) },
66 	{"FolderViewPopup/NewFeed", NULL, NULL, NULL, NULL, G_CALLBACK(rssyl_new_feed_cb) },
67 	{"FolderViewPopup/NewFolder", NULL, NULL, NULL, NULL, G_CALLBACK(rssyl_new_folder_cb) },
68 	{"FolderViewPopup/ImportFeedList", NULL, NULL, NULL, NULL, G_CALLBACK(rssyl_import_feed_list_cb) },
69 	{"FolderViewPopup/RemoveFolder", NULL, NULL, NULL, NULL, G_CALLBACK(rssyl_remove_folder_cb) },
70 	{"FolderViewPopup/RemoveMailbox", NULL, NULL, NULL, NULL, G_CALLBACK(rssyl_remove_mailbox_cb) }
71 };
72 
rssyl_add_menuitems(GtkUIManager * ui_manager,FolderItem * item)73 static void rssyl_add_menuitems(GtkUIManager *ui_manager, FolderItem *item)
74 {
75 	MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RefreshFeed", "FolderViewPopup/RefreshFeed", GTK_UI_MANAGER_MENUITEM)
76 	MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "FeedProperties", "FolderViewPopup/FeedProperties", GTK_UI_MANAGER_MENUITEM)
77 	MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RenameFolder", "FolderViewPopup/RenameFolder", GTK_UI_MANAGER_MENUITEM)
78 	MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorRSS1", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
79 	MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RefreshAllFeeds", "FolderViewPopup/RefreshAllFeeds", GTK_UI_MANAGER_MENUITEM)
80 	MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorRSS2", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
81 	MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "NewFeed", "FolderViewPopup/NewFeed", GTK_UI_MANAGER_MENUITEM)
82 	MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "NewFolder", "FolderViewPopup/NewFolder", GTK_UI_MANAGER_MENUITEM)
83 	MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "ImportFeedList", "FolderViewPopup/ImportFeedList", GTK_UI_MANAGER_MENUITEM)
84 	MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorRSS3", "FolderViewPopup/---", GTK_UI_MANAGER_SEPARATOR)
85 	MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RemoveFolder", "FolderViewPopup/RemoveFolder", GTK_UI_MANAGER_MENUITEM)
86 	MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorRSS4", "FolderView/Popup/---", GTK_UI_MANAGER_SEPARATOR)
87 	MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "RemoveMailbox", "FolderViewPopup/RemoveMailbox", GTK_UI_MANAGER_MENUITEM)
88 	MENUITEM_ADDUI_MANAGER(ui_manager, "/Popup/FolderViewPopup", "SeparatorRSS5", "FolderView/Popup/---", GTK_UI_MANAGER_SEPARATOR)
89 }
90 
rssyl_set_sensitivity(GtkUIManager * ui_manager,FolderItem * item)91 static void rssyl_set_sensitivity(GtkUIManager *ui_manager, FolderItem *item)
92 {
93 #define SET_SENS(name, sens) \
94 	cm_menu_set_sensitive_full(ui_manager, "Popup/"name, sens)
95 	RFolderItem *ritem = (RFolderItem *)item;
96 	SET_SENS("FolderViewPopup/RefreshFeed", folder_item_parent(item) != NULL && ritem->url );
97 	SET_SENS("FolderViewPopup/FeedProperties", folder_item_parent(item) != NULL && ritem->url );
98 	SET_SENS("FolderViewPopup/RenameFolder", folder_item_parent(item) != NULL );
99 	SET_SENS("FolderViewPopup/RefreshAllFeeds", TRUE );
100 	SET_SENS("FolderViewPopup/NewFeed", TRUE);
101 	SET_SENS("FolderViewPopup/NewFolder", TRUE );
102 	SET_SENS("FolderViewPopup/RemoveFolder", folder_item_parent(item) != NULL);
103 	SET_SENS("FolderViewPopup/RemoveMailbox", folder_item_parent(item) == NULL);
104 
105 #undef SET_SENS
106 }
107 
108 static FolderViewPopup rssyl_popup =
109 {
110 	"rssyl",
111 	"<rssyl>",
112 	rssyl_popup_entries,
113 	G_N_ELEMENTS(rssyl_popup_entries),
114 	NULL, 0,
115 	NULL, 0, 0, NULL,
116 	rssyl_add_menuitems,
117 	rssyl_set_sensitivity
118 };
119 
rssyl_add_mailbox(GtkAction * action,gpointer callback_data)120 static void rssyl_add_mailbox(GtkAction *action, gpointer callback_data)
121 {
122 	MainWindow *mainwin = (MainWindow *) callback_data;
123 	gchar *path = NULL, *tmp = NULL;
124 	Folder *folder;
125 
126 	path = input_dialog(_("Add RSS folder tree"),
127 			_("Enter name for a new RSS folder tree."),
128 			RSSYL_DEFAULT_MAILBOX);
129 	if( !path ) return;
130 
131 	if( folder_find_from_path(path) ) {
132 		alertpanel_error(_("The mailbox '%s' already exists."), path);
133 		g_free(path);
134 		return;
135 	}
136 
137 	tmp = g_path_get_basename(path);
138 	folder = folder_new(folder_get_class_from_string("rssyl"), tmp, path);
139 	g_free(tmp);
140 	g_free(path);
141 
142 	if( folder->klass->create_tree(folder) < 0 ) {
143 		alertpanel_error(_("Creation of folder tree failed.\n"
144 				"Maybe some files already exist, or you don't have the permission "
145 				"to write there?"));
146 		folder_destroy(folder);
147 		return;
148 	}
149 
150 	folder_add(folder);
151 	folder_scan_tree(folder, TRUE);
152 
153 	folderview_set(mainwin->folderview);
154 }
155 
156 
157 static GtkActionEntry mainwindow_add_mailbox[] = {{
158 	"File/AddMailbox/RSSyl",
159 	NULL, "RSSyl...", NULL, NULL, G_CALLBACK(rssyl_add_mailbox)
160 }};
161 
rssyl_fill_popup_menu_labels(void)162 static void rssyl_fill_popup_menu_labels(void) {
163 	gint i;
164 
165 	for (i = 0; rssyl_popup_menu_labels[i] != NULL; i++) {
166 		(rssyl_popup_entries[i].label = _(rssyl_popup_menu_labels[i]));
167 	}
168 }
169 
170 static guint main_menu_id = 0;
171 
rssyl_gtk_init(void)172 void rssyl_gtk_init(void)
173 {
174 	MainWindow *mainwin = mainwindow_get_mainwindow();
175 	gtk_action_group_add_actions(mainwin->action_group, mainwindow_add_mailbox,
176 			1, (gpointer)mainwin);
177 	MENUITEM_ADDUI_ID_MANAGER(mainwin->ui_manager, "/Menu/File/AddMailbox", PLUGIN_NAME,
178 			  "File/AddMailbox/"PLUGIN_NAME, GTK_UI_MANAGER_MENUITEM,
179 			  main_menu_id);
180 	rssyl_fill_popup_menu_labels();
181 	folderview_register_popup(&rssyl_popup);
182 }
183 
rssyl_gtk_done(void)184 void rssyl_gtk_done(void)
185 {
186 	MainWindow *mainwin = mainwindow_get_mainwindow();
187 	FolderView *folderview = NULL;
188 	FolderItem *fitem = NULL;
189 
190 	if (mainwin == NULL || claws_is_exiting())
191 		return;
192 
193 	folderview = mainwin->folderview;
194 	fitem = folderview->summaryview->folder_item;
195 
196 	if( fitem && IS_RSSYL_FOLDER_ITEM(fitem) ) {
197 		folderview_unselect(folderview);
198 		summary_clear_all(folderview->summaryview);
199 	}
200 
201 	folderview_unregister_popup(&rssyl_popup);
202 
203 	MENUITEM_REMUI_MANAGER(mainwin->ui_manager,mainwin->action_group, "File/AddMailbox/RSSyl", main_menu_id);
204 	main_menu_id = 0;
205 
206 }
207