evolution-3.6.4/modules/addressbook/e-book-shell-view.c

No issues found

  1 /*
  2  * e-book-shell-view.c
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) version 3.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 16  *
 17  *
 18  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 19  *
 20  */
 21 
 22 #ifdef HAVE_CONFIG_H
 23 #include <config.h>
 24 #endif
 25 
 26 #include "e-book-shell-view-private.h"
 27 
 28 static gpointer parent_class;
 29 static GType book_shell_view_type;
 30 
 31 static void
 32 book_shell_view_dispose (GObject *object)
 33 {
 34 	EBookShellView *book_shell_view;
 35 
 36 	book_shell_view = E_BOOK_SHELL_VIEW (object);
 37 	e_book_shell_view_private_dispose (book_shell_view);
 38 
 39 	/* Chain up to parent's dispose() method. */
 40 	G_OBJECT_CLASS (parent_class)->dispose (object);
 41 }
 42 
 43 static void
 44 book_shell_view_finalize (GObject *object)
 45 {
 46 	EBookShellView *book_shell_view;
 47 
 48 	book_shell_view = E_BOOK_SHELL_VIEW (object);
 49 	e_book_shell_view_private_finalize (book_shell_view);
 50 
 51 	/* Chain up to parent's finalize() method. */
 52 	G_OBJECT_CLASS (parent_class)->finalize (object);
 53 }
 54 
 55 static void
 56 book_shell_view_constructed (GObject *object)
 57 {
 58 	EBookShellView *book_shell_view;
 59 
 60 	/* Chain up to parent's constructed() method. */
 61 	G_OBJECT_CLASS (parent_class)->constructed (object);
 62 
 63 	book_shell_view = E_BOOK_SHELL_VIEW (object);
 64 	e_book_shell_view_private_constructed (book_shell_view);
 65 }
 66 
 67 static void
 68 book_shell_view_execute_search (EShellView *shell_view)
 69 {
 70 	EBookShellViewPrivate *priv;
 71 	EBookShellContent *book_shell_content;
 72 	EShellWindow *shell_window;
 73 	EShellContent *shell_content;
 74 	EShellSearchbar *searchbar;
 75 	EActionComboBox *combo_box;
 76 	GtkRadioAction *action;
 77 	EAddressbookView *view;
 78 	EAddressbookModel *model;
 79 	gchar *query;
 80 	gchar *temp;
 81 	gint filter_id, search_id;
 82 	gchar *search_text = NULL;
 83 	EFilterRule *advanced_search = NULL;
 84 
 85 	priv = E_BOOK_SHELL_VIEW_GET_PRIVATE (shell_view);
 86 
 87 	if (priv->search_locked)
 88 		return;
 89 
 90 	shell_window = e_shell_view_get_shell_window (shell_view);
 91 	shell_content = e_shell_view_get_shell_content (shell_view);
 92 
 93 	book_shell_content = E_BOOK_SHELL_CONTENT (shell_content);
 94 	searchbar = e_book_shell_content_get_searchbar (book_shell_content);
 95 
 96 	action = GTK_RADIO_ACTION (ACTION (CONTACT_SEARCH_ANY_FIELD_CONTAINS));
 97 	search_id = gtk_radio_action_get_current_value (action);
 98 
 99 	if (search_id == CONTACT_SEARCH_ADVANCED) {
100 		query = e_shell_view_get_search_query (shell_view);
101 
102 		if (query == NULL)
103 			query = g_strdup ("");
104 
105 		/* internal pointer, no need to free it */
106 		advanced_search = e_shell_view_get_search_rule (shell_view);
107 	} else {
108 		const gchar *text;
109 		const gchar *format;
110 		GString *string;
111 
112 		text = e_shell_searchbar_get_search_text (searchbar);
113 
114 		if (text == NULL || *text == '\0') {
115 			text = "";
116 			search_id = CONTACT_SEARCH_ANY_FIELD_CONTAINS;
117 		}
118 
119 		search_text = text && *text ? g_strdup (text) : NULL;
120 
121 		switch (search_id) {
122 			case CONTACT_SEARCH_NAME_CONTAINS:
123 				format = "(contains \"full_name\" %s)";
124 				break;
125 
126 			case CONTACT_SEARCH_EMAIL_BEGINS_WITH:
127 				format = "(beginswith \"email\" %s)";
128 				break;
129 
130 			default:
131 				text = "";
132 				/* fall through */
133 
134 			case CONTACT_SEARCH_ANY_FIELD_CONTAINS:
135 				format = "(contains \"x-evolution-any-field\" %s)";
136 				break;
137 		}
138 
139 		/* Build the query. */
140 		string = g_string_new ("");
141 		e_sexp_encode_string (string, text);
142 		query = g_strdup_printf (format, string->str);
143 		g_string_free (string, TRUE);
144 	}
145 
146 	/* Apply selected filter. */
147 	combo_box = e_shell_searchbar_get_filter_combo_box (searchbar);
148 	filter_id = e_action_combo_box_get_current_value (combo_box);
149 	switch (filter_id) {
150 		case CONTACT_FILTER_ANY_CATEGORY:
151 			break;
152 
153 		case CONTACT_FILTER_UNMATCHED:
154 			temp = g_strdup_printf (
155 				"(and (not (and (exists \"CATEGORIES\") "
156 				"(not (is \"CATEGORIES\" \"\")))) %s)",
157 				query);
158 			g_free (query);
159 			query = temp;
160 			break;
161 
162 		default:
163 		{
164 			GList *categories;
165 			const gchar *category_name;
166 
167 			categories = e_util_get_searchable_categories ();
168 			category_name = g_list_nth_data (categories, filter_id);
169 			g_list_free (categories);
170 
171 			temp = g_strdup_printf (
172 				"(and (is \"category_list\" \"%s\") %s)",
173 				category_name, query);
174 			g_free (query);
175 			query = temp;
176 		}
177 	}
178 
179 	/* Submit the query. */
180 	view = e_book_shell_content_get_current_view (book_shell_content);
181 	model = e_addressbook_view_get_model (view);
182 	e_addressbook_model_set_query (model, query);
183 	e_addressbook_view_set_search (
184 		view, filter_id, search_id, search_text, advanced_search);
185 	g_free (query);
186 	g_free (search_text);
187 }
188 
189 static void
190 book_shell_view_update_actions (EShellView *shell_view)
191 {
192 	EShellContent *shell_content;
193 	EShellSidebar *shell_sidebar;
194 	EShellWindow *shell_window;
195 	GtkAction *action;
196 	const gchar *label;
197 	gboolean sensitive;
198 	guint32 state;
199 
200 	/* Be descriptive. */
201 	gboolean any_contacts_selected;
202 	gboolean has_primary_source;
203 	gboolean multiple_contacts_selected;
204 	gboolean primary_source_is_writable;
205 	gboolean primary_source_is_removable;
206 	gboolean primary_source_is_remote_deletable;
207 	gboolean primary_source_in_collection;
208 	gboolean single_contact_selected;
209 	gboolean selection_is_contact_list;
210 	gboolean selection_has_email;
211 	gboolean source_is_busy;
212 	gboolean source_is_editable;
213 
214 	/* Chain up to parent's update_actions() method. */
215 	E_SHELL_VIEW_CLASS (parent_class)->update_actions (shell_view);
216 
217 	shell_window = e_shell_view_get_shell_window (shell_view);
218 
219 	shell_content = e_shell_view_get_shell_content (shell_view);
220 	state = e_shell_content_check_state (shell_content);
221 
222 	single_contact_selected =
223 		(state & E_BOOK_SHELL_CONTENT_SELECTION_SINGLE);
224 	multiple_contacts_selected =
225 		(state & E_BOOK_SHELL_CONTENT_SELECTION_MULTIPLE);
226 	selection_has_email =
227 		(state & E_BOOK_SHELL_CONTENT_SELECTION_HAS_EMAIL);
228 	selection_is_contact_list =
229 		(state & E_BOOK_SHELL_CONTENT_SELECTION_IS_CONTACT_LIST);
230 	source_is_busy =
231 		(state & E_BOOK_SHELL_CONTENT_SOURCE_IS_BUSY);
232 	source_is_editable =
233 		(state & E_BOOK_SHELL_CONTENT_SOURCE_IS_EDITABLE);
234 
235 	shell_sidebar = e_shell_view_get_shell_sidebar (shell_view);
236 	state = e_shell_sidebar_check_state (shell_sidebar);
237 
238 	has_primary_source =
239 		(state & E_BOOK_SHELL_SIDEBAR_HAS_PRIMARY_SOURCE);
240 	primary_source_is_writable =
241 		(state & E_BOOK_SHELL_SIDEBAR_PRIMARY_SOURCE_IS_WRITABLE);
242 	primary_source_is_removable =
243 		(state & E_BOOK_SHELL_SIDEBAR_PRIMARY_SOURCE_IS_REMOVABLE);
244 	primary_source_is_remote_deletable =
245 		(state & E_BOOK_SHELL_SIDEBAR_PRIMARY_SOURCE_IS_REMOTE_DELETABLE);
246 	primary_source_in_collection =
247 		(state & E_BOOK_SHELL_SIDEBAR_PRIMARY_SOURCE_IN_COLLECTION);
248 
249 	any_contacts_selected =
250 		(single_contact_selected || multiple_contacts_selected);
251 
252 	action = ACTION (ADDRESS_BOOK_MOVE);
253 	sensitive = source_is_editable;
254 	gtk_action_set_sensitive (action, sensitive);
255 
256 	action = ACTION (ADDRESS_BOOK_DELETE);
257 	sensitive =
258 		primary_source_is_removable ||
259 		primary_source_is_remote_deletable;
260 	gtk_action_set_sensitive (action, sensitive);
261 
262 	action = ACTION (ADDRESS_BOOK_PRINT);
263 	sensitive = has_primary_source;
264 	gtk_action_set_sensitive (action, sensitive);
265 
266 	action = ACTION (ADDRESS_BOOK_PRINT_PREVIEW);
267 	sensitive = has_primary_source;
268 	gtk_action_set_sensitive (action, sensitive);
269 
270 	action = ACTION (ADDRESS_BOOK_PROPERTIES);
271 	sensitive = primary_source_is_writable;
272 	gtk_action_set_sensitive (action, sensitive);
273 
274 	action = ACTION (ADDRESS_BOOK_RENAME);
275 	sensitive =
276 		primary_source_is_writable &&
277 		!primary_source_in_collection;
278 	gtk_action_set_sensitive (action, sensitive);
279 
280 	action = ACTION (ADDRESS_BOOK_STOP);
281 	sensitive = source_is_busy;
282 	gtk_action_set_sensitive (action, sensitive);
283 
284 	action = ACTION (CONTACT_COPY);
285 	sensitive = any_contacts_selected;
286 	gtk_action_set_sensitive (action, sensitive);
287 
288 	action = ACTION (CONTACT_DELETE);
289 	sensitive = source_is_editable && any_contacts_selected;
290 	gtk_action_set_sensitive (action, sensitive);
291 
292 	action = ACTION (CONTACT_FIND);
293 	sensitive = single_contact_selected;
294 	gtk_action_set_sensitive (action, sensitive);
295 
296 	action = ACTION (CONTACT_FORWARD);
297 	sensitive = any_contacts_selected;
298 	gtk_action_set_sensitive (action, sensitive);
299 	if (multiple_contacts_selected)
300 		label = _("_Forward Contacts");
301 	else
302 		label = _("_Forward Contact");
303 	gtk_action_set_label (action, label);
304 
305 	action = ACTION (CONTACT_MOVE);
306 	sensitive = source_is_editable && any_contacts_selected;
307 	gtk_action_set_sensitive (action, sensitive);
308 
309 	action = ACTION (CONTACT_NEW);
310 	sensitive = source_is_editable;
311 	gtk_action_set_sensitive (action, sensitive);
312 
313 	action = ACTION (CONTACT_NEW_LIST);
314 	sensitive = source_is_editable;
315 	gtk_action_set_sensitive (action, sensitive);
316 
317 	action = ACTION (CONTACT_OPEN);
318 	sensitive = any_contacts_selected;
319 	gtk_action_set_sensitive (action, sensitive);
320 
321 	action = ACTION (CONTACT_PRINT);
322 	sensitive = any_contacts_selected;
323 	gtk_action_set_sensitive (action, sensitive);
324 
325 	action = ACTION (CONTACT_SAVE_AS);
326 	sensitive = any_contacts_selected;
327 	gtk_action_set_sensitive (action, sensitive);
328 
329 	action = ACTION (CONTACT_SEND_MESSAGE);
330 	sensitive = any_contacts_selected && selection_has_email;
331 	gtk_action_set_sensitive (action, sensitive);
332 	if (multiple_contacts_selected)
333 		label = _("_Send Message to Contacts");
334 	else if (selection_is_contact_list)
335 		label = _("_Send Message to List");
336 	else
337 		label = _("_Send Message to Contact");
338 	gtk_action_set_label (action, label);
339 }
340 
341 static void
342 book_shell_view_class_init (EBookShellViewClass *class)
343 {
344 	GObjectClass *object_class;
345 	EShellViewClass *shell_view_class;
346 
347 	parent_class = g_type_class_peek_parent (class);
348 	g_type_class_add_private (class, sizeof (EBookShellViewPrivate));
349 
350 	object_class = G_OBJECT_CLASS (class);
351 	object_class->dispose = book_shell_view_dispose;
352 	object_class->finalize = book_shell_view_finalize;
353 	object_class->constructed = book_shell_view_constructed;
354 
355 	shell_view_class = E_SHELL_VIEW_CLASS (class);
356 	shell_view_class->label = _("Contacts");
357 	shell_view_class->icon_name = "x-office-address-book";
358 	shell_view_class->ui_definition = "evolution-contacts.ui";
359 	shell_view_class->ui_manager_id = "org.gnome.evolution.contacts";
360 	shell_view_class->search_options = "/contact-search-options";
361 	shell_view_class->search_rules = "addresstypes.xml";
362 	shell_view_class->new_shell_content = e_book_shell_content_new;
363 	shell_view_class->new_shell_sidebar = e_book_shell_sidebar_new;
364 	shell_view_class->execute_search = book_shell_view_execute_search;
365 	shell_view_class->update_actions = book_shell_view_update_actions;
366 }
367 
368 static void
369 book_shell_view_init (EBookShellView *book_shell_view,
370                       EShellViewClass *shell_view_class)
371 {
372 	book_shell_view->priv =
373 		E_BOOK_SHELL_VIEW_GET_PRIVATE (book_shell_view);
374 
375 	e_book_shell_view_private_init (book_shell_view, shell_view_class);
376 }
377 
378 GType
379 e_book_shell_view_get_type (void)
380 {
381 	return book_shell_view_type;
382 }
383 
384 void
385 e_book_shell_view_register_type (GTypeModule *type_module)
386 {
387 	const GTypeInfo type_info = {
388 		sizeof (EBookShellViewClass),
389 		(GBaseInitFunc) NULL,
390 		(GBaseFinalizeFunc) NULL,
391 		(GClassInitFunc) book_shell_view_class_init,
392 		(GClassFinalizeFunc) NULL,
393 		NULL,  /* class_data */
394 		sizeof (EBookShellView),
395 		0,     /* n_preallocs */
396 		(GInstanceInitFunc) book_shell_view_init,
397 		NULL   /* value_table */
398 	};
399 
400 	book_shell_view_type = g_type_module_register_type (
401 		type_module, E_TYPE_SHELL_VIEW,
402 		"EBookShellView", &type_info, 0);
403 }
404 
405 void
406 e_book_shell_view_disable_searching (EBookShellView *book_shell_view)
407 {
408 	EBookShellViewPrivate *priv;
409 
410 	g_return_if_fail (book_shell_view != NULL);
411 	g_return_if_fail (E_IS_BOOK_SHELL_VIEW (book_shell_view));
412 
413 	priv = book_shell_view->priv;
414 	priv->search_locked++;
415 }
416 
417 void
418 e_book_shell_view_enable_searching (EBookShellView *book_shell_view)
419 {
420 	EBookShellViewPrivate *priv;
421 
422 	g_return_if_fail (book_shell_view != NULL);
423 	g_return_if_fail (E_IS_BOOK_SHELL_VIEW (book_shell_view));
424 
425 	priv = book_shell_view->priv;
426 	g_return_if_fail (priv->search_locked > 0);
427 
428 	priv->search_locked--;
429 }