evolution-3.6.4/modules/calendar/e-cal-shell-view.c

No issues found

  1 /*
  2  * e-cal-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-cal-shell-view-private.h"
 27 
 28 static gpointer parent_class;
 29 static GType cal_shell_view_type;
 30 
 31 static void
 32 cal_shell_view_dispose (GObject *object)
 33 {
 34 	e_cal_shell_view_private_dispose (E_CAL_SHELL_VIEW (object));
 35 
 36 	/* Chain up to parent's dispose() method. */
 37 	G_OBJECT_CLASS (parent_class)->dispose (object);
 38 }
 39 
 40 static void
 41 cal_shell_view_finalize (GObject *object)
 42 {
 43 	e_cal_shell_view_private_finalize (E_CAL_SHELL_VIEW (object));
 44 
 45 	/* Chain up to parent's finalize() method. */
 46 	G_OBJECT_CLASS (parent_class)->finalize (object);
 47 }
 48 
 49 static void
 50 cal_shell_view_add_action_button (GtkBox *box,
 51                                   GtkAction *action)
 52 {
 53 	GtkWidget *button, *icon;
 54 
 55 	g_return_if_fail (box != NULL);
 56 	g_return_if_fail (action != NULL);
 57 
 58 	icon = gtk_action_create_icon (action, GTK_ICON_SIZE_BUTTON);
 59 	button = gtk_button_new ();
 60 	gtk_button_set_image (GTK_BUTTON (button), icon);
 61 	gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
 62 
 63 	g_object_bind_property (
 64 		action, "visible",
 65 		button, "visible",
 66 		G_BINDING_SYNC_CREATE);
 67 
 68 	g_object_bind_property (
 69 		action, "sensitive",
 70 		button, "sensitive",
 71 		G_BINDING_SYNC_CREATE);
 72 
 73 	g_object_bind_property (
 74 		action, "tooltip",
 75 		button, "tooltip-text",
 76 		G_BINDING_SYNC_CREATE);
 77 
 78 	g_signal_connect_swapped (
 79 		button, "clicked",
 80 		G_CALLBACK (gtk_action_activate), action);
 81 }
 82 
 83 static void
 84 cal_shell_view_constructed (GObject *object)
 85 {
 86 	EShellContent *shell_content;
 87 	EShellWindow *shell_window;
 88 	EShellSearchbar *searchbar;
 89 	GtkWidget *box;
 90 
 91 	/* Chain up to parent's constructed() method. */
 92 	G_OBJECT_CLASS (parent_class)->constructed (object);
 93 
 94 	e_cal_shell_view_private_constructed (E_CAL_SHELL_VIEW (object));
 95 
 96 	/* no search bar in express mode */
 97 	if (e_shell_get_express_mode (e_shell_get_default ()))
 98 		return;
 99 
100 	shell_window = e_shell_view_get_shell_window (E_SHELL_VIEW (object));
101 	shell_content = e_shell_view_get_shell_content (E_SHELL_VIEW (object));
102 	searchbar = e_cal_shell_content_get_searchbar (E_CAL_SHELL_CONTENT (shell_content));
103 
104 	box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
105 
106 	cal_shell_view_add_action_button (GTK_BOX (box), ACTION (CALENDAR_SEARCH_PREV));
107 	cal_shell_view_add_action_button (GTK_BOX (box), ACTION (CALENDAR_SEARCH_NEXT));
108 	cal_shell_view_add_action_button (GTK_BOX (box), ACTION (CALENDAR_SEARCH_STOP));
109 
110 	gtk_widget_show_all (box);
111 
112 	gtk_box_pack_start (GTK_BOX (e_shell_searchbar_get_search_box (searchbar)), box, FALSE, FALSE, 0);
113 }
114 
115 static void
116 cal_shell_view_execute_search (EShellView *shell_view)
117 {
118 	ECalShellContent *cal_shell_content;
119 	ECalShellSidebar *cal_shell_sidebar;
120 	EShellWindow *shell_window;
121 	EShellContent *shell_content;
122 	EShellSidebar *shell_sidebar;
123 	EShellSearchbar *searchbar;
124 	EActionComboBox *combo_box;
125 	GnomeCalendar *calendar;
126 	ECalendar *date_navigator;
127 	ECalModel *model;
128 	GtkRadioAction *action;
129 	icaltimezone *timezone;
130 	const gchar *default_tzloc = NULL;
131 	struct icaltimetype current_time;
132 	time_t start_range;
133 	time_t end_range;
134 	time_t now_time;
135 	gboolean range_search;
136 	gchar *start, *end;
137 	gchar *query;
138 	gchar *temp;
139 	gint value;
140 
141 	e_cal_shell_view_search_stop (E_CAL_SHELL_VIEW (shell_view));
142 
143 	shell_window = e_shell_view_get_shell_window (shell_view);
144 	shell_content = e_shell_view_get_shell_content (shell_view);
145 	shell_sidebar = e_shell_view_get_shell_sidebar (shell_view);
146 
147 	cal_shell_content = E_CAL_SHELL_CONTENT (shell_content);
148 	cal_shell_sidebar = E_CAL_SHELL_SIDEBAR (shell_sidebar);
149 
150 	searchbar = e_cal_shell_content_get_searchbar (cal_shell_content);
151 
152 	calendar = e_cal_shell_content_get_calendar (cal_shell_content);
153 	model = gnome_calendar_get_model (calendar);
154 	timezone = e_cal_model_get_timezone (model);
155 	current_time = icaltime_current_time_with_zone (timezone);
156 	now_time = time_day_begin (icaltime_as_timet (current_time));
157 
158 	if (timezone && timezone != icaltimezone_get_utc_timezone ())
159 		default_tzloc = icaltimezone_get_location (timezone);
160 	if (!default_tzloc)
161 		default_tzloc = "";
162 
163 	action = GTK_RADIO_ACTION (ACTION (CALENDAR_SEARCH_ANY_FIELD_CONTAINS));
164 	value = gtk_radio_action_get_current_value (action);
165 
166 	if (value == CALENDAR_SEARCH_ADVANCED) {
167 		query = e_shell_view_get_search_query (shell_view);
168 
169 		if (!query)
170 			query = g_strdup ("");
171 	} else {
172 		const gchar *format;
173 		const gchar *text;
174 		GString *string;
175 
176 		text = e_shell_searchbar_get_search_text (searchbar);
177 
178 		if (text == NULL || *text == '\0') {
179 			text = "";
180 			value = CALENDAR_SEARCH_SUMMARY_CONTAINS;
181 		}
182 
183 		switch (value) {
184 			default:
185 				text = "";
186 				/* fall through */
187 
188 			case CALENDAR_SEARCH_SUMMARY_CONTAINS:
189 				format = "(contains? \"summary\" %s)";
190 				break;
191 
192 			case CALENDAR_SEARCH_DESCRIPTION_CONTAINS:
193 				format = "(contains? \"description\" %s)";
194 				break;
195 
196 			case CALENDAR_SEARCH_ANY_FIELD_CONTAINS:
197 				format = "(contains? \"any\" %s)";
198 				break;
199 		}
200 
201 		/* Build the query. */
202 		string = g_string_new ("");
203 		e_sexp_encode_string (string, text);
204 		query = g_strdup_printf (format, string->str);
205 		g_string_free (string, TRUE);
206 	}
207 
208 	range_search = FALSE;
209 	start_range = end_range = 0;
210 
211 	/* Apply selected filter. */
212 	combo_box = e_shell_searchbar_get_filter_combo_box (searchbar);
213 	value = e_action_combo_box_get_current_value (combo_box);
214 	switch (value) {
215 		case CALENDAR_FILTER_ANY_CATEGORY:
216 			break;
217 
218 		case CALENDAR_FILTER_UNMATCHED:
219 			temp = g_strdup_printf (
220 				"(and (has-categories? #f) %s)", query);
221 			g_free (query);
222 			query = temp;
223 			break;
224 
225 		case CALENDAR_FILTER_ACTIVE_APPOINTMENTS:
226 			/* Show a year's worth of appointments. */
227 			start_range = now_time;
228 			end_range = time_day_end (time_add_day (start_range, 365));
229 			start = isodate_from_time_t (start_range);
230 			end = isodate_from_time_t (end_range);
231 
232 			temp = g_strdup_printf (
233 				"(and %s (occur-in-time-range? "
234 				"(make-time \"%s\") "
235 				"(make-time \"%s\") \"%s\"))",
236 				query, start, end, default_tzloc);
237 			g_free (query);
238 			query = temp;
239 
240 			range_search = TRUE;
241 			break;
242 
243 		case CALENDAR_FILTER_NEXT_7_DAYS_APPOINTMENTS:
244 			start_range = now_time;
245 			end_range = time_day_end (time_add_day (start_range, 7));
246 			start = isodate_from_time_t (start_range);
247 			end = isodate_from_time_t (end_range);
248 
249 			temp = g_strdup_printf (
250 				"(and %s (occur-in-time-range? "
251 				"(make-time \"%s\") "
252 				"(make-time \"%s\") \"%s\"))",
253 				query, start, end, default_tzloc);
254 			g_free (query);
255 			query = temp;
256 
257 			range_search = TRUE;
258 			break;
259 
260 		case CALENDAR_FILTER_OCCURS_LESS_THAN_5_TIMES:
261 			temp = g_strdup_printf (
262 				"(and %s (< (occurrences-count?) 5))", query);
263 			g_free (query);
264 			query = temp;
265 			break;
266 
267 		default:
268 		{
269 			GList *categories;
270 			const gchar *category_name;
271 
272 			categories = e_util_get_searchable_categories ();
273 			category_name = g_list_nth_data (categories, value);
274 			g_list_free (categories);
275 
276 			temp = g_strdup_printf (
277 				"(and (has-categories? \"%s\") %s)",
278 				category_name, query);
279 			g_free (query);
280 			query = temp;
281 			break;
282 		}
283 	}
284 
285 	date_navigator = e_cal_shell_sidebar_get_date_navigator (cal_shell_sidebar);
286 
287 	if (range_search) {
288 		/* Switch to list view and hide the date navigator. */
289 		action = GTK_RADIO_ACTION (ACTION (CALENDAR_VIEW_LIST));
290 		gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE);
291 		gtk_widget_hide (GTK_WIDGET (date_navigator));
292 	} else {
293 		/* Ensure the date navigator is visible. */
294 		gtk_widget_show (GTK_WIDGET (date_navigator));
295 	}
296 
297 	/* Submit the query. */
298 	gnome_calendar_set_search_query (
299 		calendar, query, range_search, start_range, end_range);
300 	g_free (query);
301 
302 	/* update also actions, thus Find Prev/Next/Stop will be sensitive as expected */
303 	e_shell_view_update_actions (shell_view);
304 }
305 
306 static void
307 cal_shell_view_update_actions (EShellView *shell_view)
308 {
309 	ECalShellViewPrivate *priv;
310 	ECalShellContent *cal_shell_content;
311 	EShellContent *shell_content;
312 	EShellSidebar *shell_sidebar;
313 	EShellWindow *shell_window;
314 	EShell *shell;
315 	ESource *source;
316 	ESourceRegistry *registry;
317 	GnomeCalendar *calendar;
318 	ECalModel *model;
319 	GtkAction *action;
320 	const gchar *model_sexp;
321 	gboolean is_searching;
322 	gboolean sensitive;
323 	guint32 state;
324 
325 	/* Be descriptive. */
326 	gboolean any_events_selected;
327 	gboolean has_mail_identity = FALSE;
328 	gboolean has_primary_source;
329 	gboolean primary_source_is_writable;
330 	gboolean primary_source_is_removable;
331 	gboolean primary_source_is_remote_deletable;
332 	gboolean primary_source_in_collection;
333 	gboolean multiple_events_selected;
334 	gboolean selection_is_editable;
335 	gboolean selection_is_instance;
336 	gboolean selection_is_meeting;
337 	gboolean selection_is_recurring;
338 	gboolean selection_can_delegate;
339 	gboolean single_event_selected;
340 	gboolean refresh_supported;
341 
342 	/* Chain up to parent's update_actions() method. */
343 	E_SHELL_VIEW_CLASS (parent_class)->update_actions (shell_view);
344 
345 	priv = E_CAL_SHELL_VIEW_GET_PRIVATE (shell_view);
346 
347 	shell_window = e_shell_view_get_shell_window (shell_view);
348 	shell = e_shell_window_get_shell (shell_window);
349 
350 	if (e_shell_get_express_mode (shell)) {
351 		GtkWidget *widget;
352 
353 		/* Hide the New button on the toolbar. */
354 		widget = e_shell_window_get_managed_widget (
355 			shell_window, "/main-toolbar");
356 		widget = (GtkWidget *) gtk_toolbar_get_nth_item (
357 			GTK_TOOLBAR (widget), 0);
358 		gtk_widget_hide (widget);
359 
360 		/* Hide the main menu. */
361 		widget = e_shell_window_get_managed_widget (
362 			shell_window, "/main-menu");
363 		gtk_widget_hide (widget);
364 	}
365 
366 	registry = e_shell_get_registry (shell);
367 	source = e_source_registry_ref_default_mail_identity (registry);
368 	has_mail_identity = (source != NULL);
369 	if (source != NULL) {
370 		has_mail_identity = TRUE;
371 		g_object_unref (source);
372 	}
373 
374 	cal_shell_content = priv->cal_shell_content;
375 	calendar = e_cal_shell_content_get_calendar (cal_shell_content);
376 	model = gnome_calendar_get_model (calendar);
377 	model_sexp = e_cal_model_get_search_query (model);
378 	is_searching = model_sexp && *model_sexp &&
379 		g_strcmp0 (model_sexp, "#t") != 0 &&
380 		g_strcmp0 (model_sexp, "(contains? \"summary\"  \"\")") != 0;
381 
382 	shell_content = e_shell_view_get_shell_content (shell_view);
383 	state = e_shell_content_check_state (shell_content);
384 
385 	single_event_selected =
386 		(state & E_CAL_SHELL_CONTENT_SELECTION_SINGLE);
387 	multiple_events_selected =
388 		(state & E_CAL_SHELL_CONTENT_SELECTION_MULTIPLE);
389 	selection_is_editable =
390 		(state & E_CAL_SHELL_CONTENT_SELECTION_IS_EDITABLE);
391 	selection_is_instance =
392 		(state & E_CAL_SHELL_CONTENT_SELECTION_IS_INSTANCE);
393 	selection_is_meeting =
394 		(state & E_CAL_SHELL_CONTENT_SELECTION_IS_MEETING);
395 	selection_is_recurring =
396 		(state & E_CAL_SHELL_CONTENT_SELECTION_IS_RECURRING);
397 	selection_can_delegate =
398 		(state & E_CAL_SHELL_CONTENT_SELECTION_CAN_DELEGATE);
399 
400 	shell_sidebar = e_shell_view_get_shell_sidebar (shell_view);
401 	state = e_shell_sidebar_check_state (shell_sidebar);
402 
403 	has_primary_source =
404 		(state & E_CAL_SHELL_SIDEBAR_HAS_PRIMARY_SOURCE);
405 	primary_source_is_writable =
406 		(state & E_CAL_SHELL_SIDEBAR_PRIMARY_SOURCE_IS_WRITABLE);
407 	primary_source_is_removable =
408 		(state & E_CAL_SHELL_SIDEBAR_PRIMARY_SOURCE_IS_REMOVABLE);
409 	primary_source_is_remote_deletable =
410 		(state & E_CAL_SHELL_SIDEBAR_PRIMARY_SOURCE_IS_REMOTE_DELETABLE);
411 	primary_source_in_collection =
412 		(state & E_CAL_SHELL_SIDEBAR_PRIMARY_SOURCE_IN_COLLECTION);
413 	refresh_supported =
414 		(state & E_CAL_SHELL_SIDEBAR_SOURCE_SUPPORTS_REFRESH);
415 
416 	any_events_selected =
417 		(single_event_selected || multiple_events_selected);
418 
419 	action = ACTION (CALENDAR_COPY);
420 	sensitive = has_primary_source;
421 	gtk_action_set_sensitive (action, sensitive);
422 
423 	action = ACTION (CALENDAR_DELETE);
424 	sensitive =
425 		primary_source_is_removable ||
426 		primary_source_is_remote_deletable;
427 	gtk_action_set_sensitive (action, sensitive);
428 
429 	action = ACTION (CALENDAR_PROPERTIES);
430 	sensitive = primary_source_is_writable;
431 	gtk_action_set_sensitive (action, sensitive);
432 
433 	action = ACTION (CALENDAR_REFRESH);
434 	sensitive = refresh_supported;
435 	gtk_action_set_sensitive (action, sensitive);
436 
437 	action = ACTION (CALENDAR_RENAME);
438 	sensitive =
439 		primary_source_is_writable &&
440 		!primary_source_in_collection;
441 	gtk_action_set_sensitive (action, sensitive);
442 
443 	action = ACTION (CALENDAR_SEARCH_PREV);
444 	gtk_action_set_sensitive (action, is_searching);
445 
446 	action = ACTION (CALENDAR_SEARCH_NEXT);
447 	gtk_action_set_sensitive (action, is_searching);
448 
449 	action = ACTION (CALENDAR_SEARCH_STOP);
450 	sensitive = is_searching && priv->searching_activity != NULL;
451 	gtk_action_set_sensitive (action, sensitive);
452 
453 	action = ACTION (EVENT_DELEGATE);
454 	sensitive =
455 		single_event_selected &&
456 		selection_is_editable &&
457 		selection_can_delegate &&
458 		selection_is_meeting;
459 	gtk_action_set_sensitive (action, sensitive);
460 
461 	action = ACTION (EVENT_DELETE);
462 	sensitive =
463 		any_events_selected &&
464 		selection_is_editable &&
465 		!selection_is_recurring;
466 	gtk_action_set_sensitive (action, sensitive);
467 
468 	action = ACTION (EVENT_DELETE_OCCURRENCE);
469 	sensitive =
470 		any_events_selected &&
471 		selection_is_editable &&
472 		selection_is_recurring;
473 	gtk_action_set_sensitive (action, sensitive);
474 
475 	action = ACTION (EVENT_DELETE_OCCURRENCE_ALL);
476 	sensitive =
477 		any_events_selected &&
478 		selection_is_editable &&
479 		selection_is_recurring;
480 	gtk_action_set_sensitive (action, sensitive);
481 
482 	action = ACTION (EVENT_FORWARD);
483 	sensitive = single_event_selected;
484 	gtk_action_set_sensitive (action, sensitive);
485 
486 	action = ACTION (EVENT_OCCURRENCE_MOVABLE);
487 	sensitive =
488 		single_event_selected &&
489 		selection_is_editable &&
490 		selection_is_recurring &&
491 		selection_is_instance;
492 	gtk_action_set_sensitive (action, sensitive);
493 
494 	action = ACTION (EVENT_OPEN);
495 	sensitive = single_event_selected;
496 	gtk_action_set_sensitive (action, sensitive);
497 
498 	action = ACTION (EVENT_PRINT);
499 	sensitive = single_event_selected;
500 	gtk_action_set_sensitive (action, sensitive);
501 
502 	action = ACTION (EVENT_SAVE_AS);
503 	sensitive = single_event_selected;
504 	gtk_action_set_sensitive (action, sensitive);
505 
506 	action = ACTION (EVENT_SCHEDULE);
507 	sensitive =
508 		single_event_selected &&
509 		selection_is_editable &&
510 		!selection_is_meeting;
511 	gtk_action_set_sensitive (action, sensitive);
512 
513 	action = ACTION (EVENT_SCHEDULE_APPOINTMENT);
514 	sensitive =
515 		single_event_selected &&
516 		selection_is_editable &&
517 		selection_is_meeting;
518 	gtk_action_set_sensitive (action, sensitive);
519 
520 	action = ACTION (EVENT_REPLY);
521 	sensitive = single_event_selected && selection_is_meeting;
522 	gtk_action_set_sensitive (action, sensitive);
523 
524 	action = ACTION (EVENT_REPLY_ALL);
525 	sensitive = single_event_selected && selection_is_meeting;
526 	gtk_action_set_sensitive (action, sensitive);
527 
528 	action = ACTION (EVENT_MEETING_NEW);
529 	gtk_action_set_visible (action, has_mail_identity);
530 }
531 
532 static void
533 cal_shell_view_class_init (ECalShellViewClass *class,
534                            GTypeModule *type_module)
535 {
536 	GObjectClass *object_class;
537 	EShellViewClass *shell_view_class;
538 
539 	parent_class = g_type_class_peek_parent (class);
540 	g_type_class_add_private (class, sizeof (ECalShellViewPrivate));
541 
542 	object_class = G_OBJECT_CLASS (class);
543 	object_class->dispose = cal_shell_view_dispose;
544 	object_class->finalize = cal_shell_view_finalize;
545 	object_class->constructed = cal_shell_view_constructed;
546 
547 	shell_view_class = E_SHELL_VIEW_CLASS (class);
548 	shell_view_class->label = _("Calendar");
549 	shell_view_class->icon_name = "x-office-calendar";
550 	shell_view_class->ui_definition = "evolution-calendars.ui";
551 	shell_view_class->ui_manager_id = "org.gnome.evolution.calendars";
552 	shell_view_class->search_options = "/calendar-search-options";
553 	shell_view_class->search_rules = "caltypes.xml";
554 	shell_view_class->new_shell_content = e_cal_shell_content_new;
555 	shell_view_class->new_shell_sidebar = e_cal_shell_sidebar_new;
556 	shell_view_class->execute_search = cal_shell_view_execute_search;
557 	shell_view_class->update_actions = cal_shell_view_update_actions;
558 
559 	/* XXX This is an unusual place to need an EShell instance.
560 	 *     Would be cleaner to implement a method that either
561 	 *     chains up or does nothing based on express mode. */
562 	if (e_shell_get_express_mode (e_shell_get_default ()))
563 		shell_view_class->construct_searchbar = NULL;
564 
565 }
566 
567 static void
568 cal_shell_view_init (ECalShellView *cal_shell_view,
569                      EShellViewClass *shell_view_class)
570 {
571 	cal_shell_view->priv =
572 		E_CAL_SHELL_VIEW_GET_PRIVATE (cal_shell_view);
573 
574 	e_cal_shell_view_private_init (cal_shell_view, shell_view_class);
575 }
576 
577 GType
578 e_cal_shell_view_get_type (void)
579 {
580 	return cal_shell_view_type;
581 }
582 
583 void
584 e_cal_shell_view_register_type (GTypeModule *type_module)
585 {
586 	const GTypeInfo type_info = {
587 		sizeof (ECalShellViewClass),
588 		(GBaseInitFunc) NULL,
589 		(GBaseFinalizeFunc) NULL,
590 		(GClassInitFunc) cal_shell_view_class_init,
591 		(GClassFinalizeFunc) NULL,
592 		type_module,
593 		sizeof (ECalShellView),
594 		0,    /* n_preallocs */
595 		(GInstanceInitFunc) cal_shell_view_init,
596 		NULL  /* value_table */
597 	};
598 
599 	cal_shell_view_type = g_type_module_register_type (
600 		type_module, E_TYPE_SHELL_VIEW,
601 		"ECalShellView", &type_info, 0);
602 }