1 /*
2  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
3  * Copyright (C) 2014 Red Hat, Inc. (www.redhat.com)
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "evolution-config.h"
19 
20 #include <gtk/gtk.h>
21 #include <glib/gi18n-lib.h>
22 
23 #include "e-memo-shell-view-private.h"
24 #include "e-memo-shell-view.h"
25 
G_DEFINE_DYNAMIC_TYPE(EMemoShellView,e_memo_shell_view,E_TYPE_CAL_BASE_SHELL_VIEW)26 G_DEFINE_DYNAMIC_TYPE (EMemoShellView, e_memo_shell_view, E_TYPE_CAL_BASE_SHELL_VIEW)
27 
28 static void
29 memo_shell_view_execute_search (EShellView *shell_view)
30 {
31 	EMemoShellContent *memo_shell_content;
32 	EShellWindow *shell_window;
33 	EShellContent *shell_content;
34 	EShellSearchbar *searchbar;
35 	EActionComboBox *combo_box;
36 	GtkRadioAction *action;
37 	ECalComponentPreview *memo_preview;
38 	EPreviewPane *preview_pane;
39 	EMemoTable *memo_table;
40 	EWebView *web_view;
41 	ECalModel *model;
42 	ECalDataModel *data_model;
43 	gchar *query;
44 	gchar *temp;
45 	gint value;
46 
47 	shell_content = e_shell_view_get_shell_content (shell_view);
48 	shell_window = e_shell_view_get_shell_window (shell_view);
49 
50 	memo_shell_content = E_MEMO_SHELL_CONTENT (shell_content);
51 	searchbar = e_memo_shell_content_get_searchbar (memo_shell_content);
52 
53 	action = GTK_RADIO_ACTION (ACTION (MEMO_SEARCH_ANY_FIELD_CONTAINS));
54 	value = gtk_radio_action_get_current_value (action);
55 
56 	if (value == MEMO_SEARCH_ADVANCED) {
57 		query = e_shell_view_get_search_query (shell_view);
58 
59 		if (!query)
60 			query = g_strdup ("");
61 	} else {
62 		const gchar *format;
63 		const gchar *text;
64 		GString *string;
65 
66 		text = e_shell_searchbar_get_search_text (searchbar);
67 
68 		if (text == NULL || *text == '\0') {
69 			text = "";
70 			value = MEMO_SEARCH_SUMMARY_CONTAINS;
71 		}
72 
73 		switch (value) {
74 			default:
75 				text = "";
76 				/* fall through */
77 
78 			case MEMO_SEARCH_SUMMARY_CONTAINS:
79 				format = "(contains? \"summary\" %s)";
80 				break;
81 
82 			case MEMO_SEARCH_DESCRIPTION_CONTAINS:
83 				format = "(contains? \"description\" %s)";
84 				break;
85 
86 			case MEMO_SEARCH_ANY_FIELD_CONTAINS:
87 				format = "(contains? \"any\" %s)";
88 				break;
89 		}
90 
91 		/* Build the query. */
92 		string = g_string_new ("");
93 		e_sexp_encode_string (string, text);
94 		query = g_strdup_printf (format, string->str);
95 		g_string_free (string, TRUE);
96 	}
97 
98 	/* Apply selected filter. */
99 	combo_box = e_shell_searchbar_get_filter_combo_box (searchbar);
100 	value = e_action_combo_box_get_current_value (combo_box);
101 	switch (value) {
102 		case MEMO_FILTER_ANY_CATEGORY:
103 			break;
104 
105 		case MEMO_FILTER_UNMATCHED:
106 			temp = g_strdup_printf (
107 				"(and (has-categories? #f) %s)", query);
108 			g_free (query);
109 			query = temp;
110 			break;
111 
112 		default:
113 		{
114 			GList *categories;
115 			const gchar *category_name;
116 
117 			categories = e_util_dup_searchable_categories ();
118 			category_name = g_list_nth_data (categories, value);
119 
120 			temp = g_strdup_printf (
121 				"(and (has-categories? \"%s\") %s)",
122 				category_name, query);
123 			g_free (query);
124 			query = temp;
125 
126 			g_list_free_full (categories, g_free);
127 			break;
128 		}
129 	}
130 
131 	/* Submit the query. */
132 	memo_table = e_memo_shell_content_get_memo_table (memo_shell_content);
133 	model = e_memo_table_get_model (memo_table);
134 	data_model = e_cal_model_get_data_model (model);
135 	e_cal_data_model_set_filter (data_model, query);
136 	g_free (query);
137 
138 	preview_pane =
139 		e_memo_shell_content_get_preview_pane (memo_shell_content);
140 
141 	web_view = e_preview_pane_get_web_view (preview_pane);
142 	memo_preview = E_CAL_COMPONENT_PREVIEW (web_view);
143 	e_cal_component_preview_clear (memo_preview);
144 }
145 
146 static void
memo_shell_view_update_actions(EShellView * shell_view)147 memo_shell_view_update_actions (EShellView *shell_view)
148 {
149 	EShellContent *shell_content;
150 	EShellSidebar *shell_sidebar;
151 	EShellWindow *shell_window;
152 	GtkAction *action;
153 	const gchar *label;
154 	gboolean sensitive;
155 	guint32 state;
156 
157 	/* Be descriptive. */
158 	gboolean any_memos_selected;
159 	gboolean has_primary_source;
160 	gboolean multiple_memos_selected;
161 	gboolean primary_source_is_writable;
162 	gboolean primary_source_is_removable;
163 	gboolean primary_source_is_remote_deletable;
164 	gboolean primary_source_in_collection;
165 	gboolean selection_has_url;
166 	gboolean single_memo_selected;
167 	gboolean sources_are_editable;
168 	gboolean refresh_supported;
169 	gboolean all_sources_selected;
170 	gboolean clicked_source_is_primary;
171 	gboolean clicked_source_is_collection;
172 
173 	/* Chain up to parent's update_actions() method. */
174 	E_SHELL_VIEW_CLASS (e_memo_shell_view_parent_class)->
175 		update_actions (shell_view);
176 
177 	shell_window = e_shell_view_get_shell_window (shell_view);
178 
179 	shell_content = e_shell_view_get_shell_content (shell_view);
180 	state = e_shell_content_check_state (shell_content);
181 
182 	single_memo_selected =
183 		(state & E_CAL_BASE_SHELL_CONTENT_SELECTION_SINGLE);
184 	multiple_memos_selected =
185 		(state & E_CAL_BASE_SHELL_CONTENT_SELECTION_MULTIPLE);
186 	sources_are_editable =
187 		(state & E_CAL_BASE_SHELL_CONTENT_SELECTION_IS_EDITABLE);
188 	selection_has_url =
189 		(state & E_CAL_BASE_SHELL_CONTENT_SELECTION_HAS_URL);
190 
191 	shell_sidebar = e_shell_view_get_shell_sidebar (shell_view);
192 	state = e_shell_sidebar_check_state (shell_sidebar);
193 
194 	has_primary_source =
195 		(state & E_CAL_BASE_SHELL_SIDEBAR_HAS_PRIMARY_SOURCE);
196 	primary_source_is_writable =
197 		(state & E_CAL_BASE_SHELL_SIDEBAR_PRIMARY_SOURCE_IS_WRITABLE);
198 	primary_source_is_removable =
199 		(state & E_CAL_BASE_SHELL_SIDEBAR_PRIMARY_SOURCE_IS_REMOVABLE);
200 	primary_source_is_remote_deletable =
201 		(state & E_CAL_BASE_SHELL_SIDEBAR_PRIMARY_SOURCE_IS_REMOTE_DELETABLE);
202 	primary_source_in_collection =
203 		(state & E_CAL_BASE_SHELL_SIDEBAR_PRIMARY_SOURCE_IN_COLLECTION);
204 	refresh_supported =
205 		(state & E_CAL_BASE_SHELL_SIDEBAR_SOURCE_SUPPORTS_REFRESH);
206 	all_sources_selected =
207 		(state & E_CAL_BASE_SHELL_SIDEBAR_ALL_SOURCES_SELECTED) != 0;
208 	clicked_source_is_primary =
209 		(state & E_CAL_BASE_SHELL_SIDEBAR_CLICKED_SOURCE_IS_PRIMARY) != 0;
210 	clicked_source_is_collection =
211 		(state & E_CAL_BASE_SHELL_SIDEBAR_CLICKED_SOURCE_IS_COLLECTION) != 0;
212 
213 	any_memos_selected = (single_memo_selected || multiple_memos_selected);
214 
215 	action = ACTION (MEMO_LIST_SELECT_ALL);
216 	sensitive = clicked_source_is_primary && !all_sources_selected;
217 	gtk_action_set_sensitive (action, sensitive);
218 
219 	action = ACTION (MEMO_LIST_SELECT_ONE);
220 	sensitive = clicked_source_is_primary;
221 	gtk_action_set_sensitive (action, sensitive);
222 
223 	action = ACTION (MEMO_DELETE);
224 	sensitive = any_memos_selected && sources_are_editable;
225 	gtk_action_set_sensitive (action, sensitive);
226 	if (multiple_memos_selected)
227 		label = _("Delete Memos");
228 	else
229 		label = _("Delete Memo");
230 	gtk_action_set_label (action, label);
231 
232 	action = ACTION (MEMO_FIND);
233 	sensitive = single_memo_selected;
234 	gtk_action_set_sensitive (action, sensitive);
235 
236 	action = ACTION (MEMO_FORWARD);
237 	sensitive = single_memo_selected;
238 	gtk_action_set_sensitive (action, sensitive);
239 
240 	action = ACTION (MEMO_LIST_COPY);
241 	sensitive = has_primary_source;
242 	gtk_action_set_sensitive (action, sensitive);
243 
244 	action = ACTION (MEMO_LIST_DELETE);
245 	sensitive =
246 		primary_source_is_removable ||
247 		primary_source_is_remote_deletable;
248 	gtk_action_set_sensitive (action, sensitive);
249 
250 	action = ACTION (MEMO_LIST_PRINT);
251 	sensitive = has_primary_source;
252 	gtk_action_set_sensitive (action, sensitive);
253 
254 	action = ACTION (MEMO_LIST_PRINT_PREVIEW);
255 	sensitive = has_primary_source;
256 	gtk_action_set_sensitive (action, sensitive);
257 
258 	action = ACTION (MEMO_LIST_PROPERTIES);
259 	sensitive = clicked_source_is_primary && primary_source_is_writable;
260 	gtk_action_set_sensitive (action, sensitive);
261 
262 	action = ACTION (MEMO_LIST_REFRESH);
263 	sensitive = clicked_source_is_primary && refresh_supported;
264 	gtk_action_set_sensitive (action, sensitive);
265 
266 	action = ACTION (MEMO_LIST_REFRESH_BACKEND);
267 	sensitive = clicked_source_is_collection;
268 	gtk_action_set_sensitive (action, sensitive);
269 
270 	action = ACTION (MEMO_LIST_RENAME);
271 	sensitive = clicked_source_is_primary && (
272 		primary_source_is_writable &&
273 		!primary_source_in_collection);
274 	gtk_action_set_sensitive (action, sensitive);
275 
276 	action = ACTION (MEMO_OPEN);
277 	sensitive = single_memo_selected;
278 	gtk_action_set_sensitive (action, sensitive);
279 
280 	action = ACTION (MEMO_OPEN_URL);
281 	sensitive = single_memo_selected && selection_has_url;
282 	gtk_action_set_sensitive (action, sensitive);
283 
284 	action = ACTION (MEMO_PRINT);
285 	sensitive = single_memo_selected;
286 	gtk_action_set_sensitive (action, sensitive);
287 
288 	action = ACTION (MEMO_SAVE_AS);
289 	sensitive = single_memo_selected;
290 	gtk_action_set_sensitive (action, sensitive);
291 }
292 
293 static void
memo_shell_view_dispose(GObject * object)294 memo_shell_view_dispose (GObject *object)
295 {
296 	e_memo_shell_view_private_dispose (E_MEMO_SHELL_VIEW (object));
297 
298 	/* Chain up to parent's dispose() method. */
299 	G_OBJECT_CLASS (e_memo_shell_view_parent_class)->dispose (object);
300 }
301 
302 static void
memo_shell_view_finalize(GObject * object)303 memo_shell_view_finalize (GObject *object)
304 {
305 	e_memo_shell_view_private_finalize (E_MEMO_SHELL_VIEW (object));
306 
307 	/* Chain up to parent's finalize() method. */
308 	G_OBJECT_CLASS (e_memo_shell_view_parent_class)->finalize (object);
309 }
310 
311 static void
memo_shell_view_constructed(GObject * object)312 memo_shell_view_constructed (GObject *object)
313 {
314 	/* Chain up to parent's constructed() method. */
315 	G_OBJECT_CLASS (e_memo_shell_view_parent_class)->constructed (object);
316 
317 	e_memo_shell_view_private_constructed (E_MEMO_SHELL_VIEW (object));
318 }
319 
320 static void
e_memo_shell_view_class_init(EMemoShellViewClass * class)321 e_memo_shell_view_class_init (EMemoShellViewClass *class)
322 {
323 	GObjectClass *object_class;
324 	EShellViewClass *shell_view_class;
325 	ECalBaseShellViewClass *cal_base_shell_view_class;
326 
327 	g_type_class_add_private (class, sizeof (EMemoShellViewPrivate));
328 
329 	object_class = G_OBJECT_CLASS (class);
330 	object_class->dispose = memo_shell_view_dispose;
331 	object_class->finalize = memo_shell_view_finalize;
332 	object_class->constructed = memo_shell_view_constructed;
333 
334 	shell_view_class = E_SHELL_VIEW_CLASS (class);
335 	shell_view_class->label = _("Memos");
336 	shell_view_class->icon_name = "evolution-memos";
337 	shell_view_class->ui_definition = "evolution-memos.ui";
338 	shell_view_class->ui_manager_id = "org.gnome.evolution.memos";
339 	shell_view_class->search_options = "/memo-search-options";
340 	shell_view_class->search_rules = "memotypes.xml";
341 	shell_view_class->new_shell_content = e_memo_shell_content_new;
342 	shell_view_class->new_shell_sidebar = e_cal_base_shell_sidebar_new;
343 	shell_view_class->execute_search = memo_shell_view_execute_search;
344 	shell_view_class->update_actions = memo_shell_view_update_actions;
345 
346 	cal_base_shell_view_class = E_CAL_BASE_SHELL_VIEW_CLASS (class);
347 	cal_base_shell_view_class->source_type = E_CAL_CLIENT_SOURCE_TYPE_MEMOS;
348 
349 	/* Ensure the GalView types we need are registered. */
350 	g_type_ensure (GAL_TYPE_VIEW_ETABLE);
351 }
352 
353 static void
e_memo_shell_view_class_finalize(EMemoShellViewClass * class)354 e_memo_shell_view_class_finalize (EMemoShellViewClass *class)
355 {
356 }
357 
358 static void
e_memo_shell_view_init(EMemoShellView * memo_shell_view)359 e_memo_shell_view_init (EMemoShellView *memo_shell_view)
360 {
361 	memo_shell_view->priv = E_MEMO_SHELL_VIEW_GET_PRIVATE (memo_shell_view);
362 }
363 
364 void
e_memo_shell_view_type_register(GTypeModule * type_module)365 e_memo_shell_view_type_register (GTypeModule *type_module)
366 {
367 	/* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
368 	 *     function, so we have to wrap it with a public function in
369 	 *     order to register types from a separate compilation unit. */
370 	e_memo_shell_view_register_type (type_module);
371 }
372