evolution-3.6.4/shell/e-shell-content.c

No issues found

  1 /*
  2  * e-shell-content.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 /**
 23  * SECTION: e-shell-content
 24  * @short_description: the right side of the main window
 25  * @include: shell/e-shell-content.h
 26  **/
 27 
 28 #ifdef HAVE_CONFIG_H
 29 #include <config.h>
 30 #endif
 31 
 32 #include "e-shell-content.h"
 33 
 34 #include <glib/gi18n.h>
 35 #include <libebackend/libebackend.h>
 36 
 37 #include "e-util/e-util.h"
 38 #include "libevolution-utils/e-alert-dialog.h"
 39 #include "libevolution-utils/e-alert-sink.h"
 40 #include "filter/e-rule-editor.h"
 41 #include "widgets/misc/e-action-combo-box.h"
 42 #include "widgets/misc/e-alert-bar.h"
 43 
 44 #include "e-shell-backend.h"
 45 #include "e-shell-searchbar.h"
 46 #include "e-shell-view.h"
 47 #include "e-shell-window-actions.h"
 48 
 49 #define E_SHELL_CONTENT_GET_PRIVATE(obj) \
 50 	(G_TYPE_INSTANCE_GET_PRIVATE \
 51 	((obj), E_TYPE_SHELL_CONTENT, EShellContentPrivate))
 52 
 53 struct _EShellContentPrivate {
 54 
 55 	gpointer shell_view;	/* weak pointer */
 56 
 57 	GtkWidget *alert_bar;
 58 	GtkWidget *searchbar;	/* not referenced */
 59 
 60 	/* Custom search rules. */
 61 	gchar *user_filename;
 62 };
 63 
 64 enum {
 65 	PROP_0,
 66 	PROP_ALERT_BAR,
 67 	PROP_SHELL_VIEW
 68 };
 69 
 70 /* Forward Declarations */
 71 static void	e_shell_content_alert_sink_init
 72 					(EAlertSinkInterface *interface);
 73 
 74 G_DEFINE_TYPE_WITH_CODE (
 75 	EShellContent,
 76 	e_shell_content,
 77 	GTK_TYPE_BIN,
 78 	G_IMPLEMENT_INTERFACE (
 79 		E_TYPE_ALERT_SINK, e_shell_content_alert_sink_init)
 80 	G_IMPLEMENT_INTERFACE (
 81 		E_TYPE_EXTENSIBLE, NULL));
 82 
 83 static void
 84 shell_content_dialog_rule_changed (GtkWidget *dialog,
 85                                    EFilterRule *rule)
 86 {
 87 	gboolean sensitive;
 88 
 89 	sensitive = (rule != NULL) && (rule->parts != NULL);
 90 
 91 	gtk_dialog_set_response_sensitive (
 92 		GTK_DIALOG (dialog), GTK_RESPONSE_OK, sensitive);
 93 	gtk_dialog_set_response_sensitive (
 94 		GTK_DIALOG (dialog), GTK_RESPONSE_APPLY, sensitive);
 95 }
 96 
 97 static void
 98 shell_content_set_shell_view (EShellContent *shell_content,
 99                               EShellView *shell_view)
100 {
101 	g_return_if_fail (shell_content->priv->shell_view == NULL);
102 
103 	shell_content->priv->shell_view = shell_view;
104 
105 	g_object_add_weak_pointer (
106 		G_OBJECT (shell_view),
107 		&shell_content->priv->shell_view);
108 }
109 
110 static void
111 shell_content_set_property (GObject *object,
112                             guint property_id,
113                             const GValue *value,
114                             GParamSpec *pspec)
115 {
116 	switch (property_id) {
117 		case PROP_SHELL_VIEW:
118 			shell_content_set_shell_view (
119 				E_SHELL_CONTENT (object),
120 				g_value_get_object (value));
121 			return;
122 	}
123 
124 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
125 }
126 
127 static void
128 shell_content_get_property (GObject *object,
129                             guint property_id,
130                             GValue *value,
131                             GParamSpec *pspec)
132 {
133 	switch (property_id) {
134 		case PROP_ALERT_BAR:
135 			g_value_set_object (
136 				value, e_shell_content_get_alert_bar (
137 				E_SHELL_CONTENT (object)));
138 			return;
139 
140 		case PROP_SHELL_VIEW:
141 			g_value_set_object (
142 				value, e_shell_content_get_shell_view (
143 				E_SHELL_CONTENT (object)));
144 			return;
145 	}
146 
147 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
148 }
149 
150 static void
151 shell_content_dispose (GObject *object)
152 {
153 	EShellContentPrivate *priv;
154 
155 	priv = E_SHELL_CONTENT_GET_PRIVATE (object);
156 
157 	if (priv->shell_view != NULL) {
158 		g_object_remove_weak_pointer (
159 			G_OBJECT (priv->shell_view), &priv->shell_view);
160 		priv->shell_view = NULL;
161 	}
162 
163 	if (priv->alert_bar != NULL) {
164 		g_object_unref (priv->alert_bar);
165 		priv->alert_bar = NULL;
166 	}
167 
168 	/* Chain up to parent's dispose() method. */
169 	G_OBJECT_CLASS (e_shell_content_parent_class)->dispose (object);
170 }
171 
172 static void
173 shell_content_finalize (GObject *object)
174 {
175 	EShellContentPrivate *priv;
176 
177 	priv = E_SHELL_CONTENT_GET_PRIVATE (object);
178 
179 	g_free (priv->user_filename);
180 
181 	/* Chain up to parent's finalize() method. */
182 	G_OBJECT_CLASS (e_shell_content_parent_class)->finalize (object);
183 }
184 
185 static void
186 shell_content_constructed (GObject *object)
187 {
188 	EShellContent *shell_content;
189 	EShellBackend *shell_backend;
190 	EShellView *shell_view;
191 	GtkWidget *widget;
192 	const gchar *config_dir;
193 
194 	shell_content = E_SHELL_CONTENT (object);
195 	shell_view = e_shell_content_get_shell_view (shell_content);
196 	shell_backend = e_shell_view_get_shell_backend (shell_view);
197 
198 	widget = e_alert_bar_new ();
199 	gtk_widget_set_parent (widget, GTK_WIDGET (shell_content));
200 	shell_content->priv->alert_bar = g_object_ref_sink (widget);
201 	/* EAlertBar controls its own visibility. */
202 
203 	/* XXX Regenerate the filename for custom saved search as done
204 	 *     in shell_view_init_search_context().  ERuleContext ought
205 	 *     to remember the filename when loading rules so you don't
206 	 *     have to keep passing it in when saving rules. */
207 	config_dir = e_shell_backend_get_config_dir (shell_backend);
208 	shell_content->priv->user_filename =
209 		g_build_filename (config_dir, "searches.xml", NULL);
210 
211 	e_extensible_load_extensions (E_EXTENSIBLE (object));
212 
213 	/* Chain up to parent's constructed() method. */
214 	G_OBJECT_CLASS (e_shell_content_parent_class)->constructed (object);
215 }
216 
217 static void
218 shell_content_get_preferred_width (GtkWidget *widget,
219                                    gint *minimum,
220                                    gint *natural)
221 {
222 	EShellContentPrivate *priv;
223 	GtkWidget *child;
224 
225 	priv = E_SHELL_CONTENT_GET_PRIVATE (widget);
226 
227 	*minimum = *natural = 0;
228 
229 	child = gtk_bin_get_child (GTK_BIN (widget));
230 	gtk_widget_get_preferred_width (child, minimum, natural);
231 
232 	if (gtk_widget_get_visible (priv->alert_bar)) {
233 		gint child_minimum;
234 		gint child_natural;
235 
236 		gtk_widget_get_preferred_width (
237 			priv->alert_bar, &child_minimum, &child_natural);
238 
239 		*minimum = MAX (*minimum, child_minimum);
240 		*natural = MAX (*natural, child_natural);
241 	}
242 
243 	if (priv->searchbar != NULL) {
244 		gint child_minimum;
245 		gint child_natural;
246 
247 		gtk_widget_get_preferred_width (
248 			priv->searchbar, &child_minimum, &child_natural);
249 
250 		*minimum = MAX (*minimum, child_minimum);
251 		*natural = MAX (*natural, child_natural);
252 	}
253 }
254 
255 static void
256 shell_content_get_preferred_height (GtkWidget *widget,
257                                     gint *minimum,
258                                     gint *natural)
259 {
260 	EShellContentPrivate *priv;
261 	GtkWidget *child;
262 
263 	priv = E_SHELL_CONTENT_GET_PRIVATE (widget);
264 
265 	child = gtk_bin_get_child (GTK_BIN (widget));
266 	gtk_widget_get_preferred_height (child, minimum, natural);
267 
268 	if (gtk_widget_get_visible (priv->alert_bar)) {
269 		gint child_minimum;
270 		gint child_natural;
271 
272 		gtk_widget_get_preferred_height (
273 			priv->alert_bar, &child_minimum, &child_natural);
274 
275 		*minimum += child_minimum;
276 		*natural += child_natural;
277 	}
278 
279 	if (priv->searchbar != NULL) {
280 		gint child_minimum;
281 		gint child_natural;
282 
283 		gtk_widget_get_preferred_height (
284 			priv->searchbar, &child_minimum, &child_natural);
285 
286 		*minimum += child_minimum;
287 		*natural += child_natural;
288 	}
289 }
290 
291 static void
292 shell_content_size_allocate (GtkWidget *widget,
293                              GtkAllocation *allocation)
294 {
295 	EShellContentPrivate *priv;
296 	GtkAllocation child_allocation;
297 	GtkRequisition child_requisition;
298 	GtkWidget *child;
299 	gint remaining_height;
300 
301 	priv = E_SHELL_CONTENT_GET_PRIVATE (widget);
302 
303 	remaining_height = allocation->height;
304 	gtk_widget_set_allocation (widget, allocation);
305 
306 	child_allocation.x = allocation->x;
307 	child_allocation.y = allocation->y;
308 	child_allocation.width = allocation->width;
309 
310 	child_requisition.height = 0;
311 
312 	/* Alert bar gets to be as tall as it wants (if visible). */
313 
314 	child = priv->alert_bar;
315 	child_allocation.y += child_requisition.height;
316 
317 	if (gtk_widget_get_visible (child))
318 		gtk_widget_get_preferred_height_for_width (child, child_allocation.width, &child_requisition.height, NULL);
319 	else
320 		child_requisition.height = 0;
321 
322 	remaining_height -= child_requisition.height;
323 	child_allocation.height = child_requisition.height;
324 
325 	if (child_allocation.height > 0)
326 		gtk_widget_size_allocate (child, &child_allocation);
327 
328 	/* Search bar gets to be as tall as it wants (if we have one). */
329 
330 	child = priv->searchbar;
331 	child_allocation.y += child_requisition.height;
332 
333 	if (child != NULL)
334 		gtk_widget_get_preferred_size (child, &child_requisition, NULL);
335 	else
336 		child_requisition.height = 0;
337 
338 	remaining_height -= child_requisition.height;
339 	child_allocation.height = child_requisition.height;
340 
341 	if (child != NULL)
342 		gtk_widget_size_allocate (child, &child_allocation);
343 
344 	/* The GtkBin child gets whatever vertical space is left. */
345 
346 	child_allocation.y += child_requisition.height;
347 	child_allocation.height = remaining_height;
348 
349 	child = gtk_bin_get_child (GTK_BIN (widget));
350 	if (child != NULL)
351 		gtk_widget_size_allocate (child, &child_allocation);
352 }
353 
354 static void
355 shell_content_remove (GtkContainer *container,
356                       GtkWidget *widget)
357 {
358 	GtkContainerClass *container_class;
359 	EShellContentPrivate *priv;
360 
361 	priv = E_SHELL_CONTENT_GET_PRIVATE (container);
362 
363 	if (widget == priv->alert_bar) {
364 		gtk_widget_unparent (priv->alert_bar);
365 		priv->alert_bar = NULL;
366 		return;
367 	}
368 
369 	if (widget == priv->searchbar) {
370 		gtk_widget_unparent (priv->searchbar);
371 		priv->searchbar = NULL;
372 		return;
373 	}
374 
375 	/* Chain up to parent's remove() method. */
376 	container_class = GTK_CONTAINER_CLASS (e_shell_content_parent_class);
377 	container_class->remove (container, widget);
378 }
379 
380 static void
381 shell_content_forall (GtkContainer *container,
382                       gboolean include_internals,
383                       GtkCallback callback,
384                       gpointer callback_data)
385 {
386 	EShellContentPrivate *priv;
387 
388 	priv = E_SHELL_CONTENT_GET_PRIVATE (container);
389 
390 	if (priv->alert_bar != NULL)
391 		callback (priv->alert_bar, callback_data);
392 
393 	if (priv->searchbar != NULL)
394 		callback (priv->searchbar, callback_data);
395 
396 	/* Chain up to parent's forall() method. */
397 	GTK_CONTAINER_CLASS (e_shell_content_parent_class)->forall (
398 		container, include_internals, callback, callback_data);
399 }
400 
401 static void
402 shell_content_submit_alert (EAlertSink *alert_sink,
403                             EAlert *alert)
404 {
405 	EShellView *shell_view;
406 	EShellWindow *shell_window;
407 	EShellContent *shell_content;
408 	GtkWidget *alert_bar;
409 	GtkWidget *dialog;
410 
411 	shell_content = E_SHELL_CONTENT (alert_sink);
412 	shell_view = e_shell_content_get_shell_view (shell_content);
413 	shell_window = e_shell_view_get_shell_window (shell_view);
414 	alert_bar = e_shell_content_get_alert_bar (shell_content);
415 
416 	switch (e_alert_get_message_type (alert)) {
417 		case GTK_MESSAGE_INFO:
418 		case GTK_MESSAGE_WARNING:
419 		case GTK_MESSAGE_ERROR:
420 			e_alert_bar_add_alert (
421 				E_ALERT_BAR (alert_bar), alert);
422 			break;
423 
424 		default:
425 			dialog = e_alert_dialog_new (
426 				GTK_WINDOW (shell_window), alert);
427 			gtk_dialog_run (GTK_DIALOG (dialog));
428 			gtk_widget_destroy (dialog);
429 			break;
430 	}
431 }
432 
433 static void
434 e_shell_content_class_init (EShellContentClass *class)
435 {
436 	GObjectClass *object_class;
437 	GtkWidgetClass *widget_class;
438 	GtkContainerClass *container_class;
439 
440 	g_type_class_add_private (class, sizeof (EShellContentPrivate));
441 
442 	object_class = G_OBJECT_CLASS (class);
443 	object_class->set_property = shell_content_set_property;
444 	object_class->get_property = shell_content_get_property;
445 	object_class->dispose = shell_content_dispose;
446 	object_class->finalize = shell_content_finalize;
447 	object_class->constructed = shell_content_constructed;
448 
449 	widget_class = GTK_WIDGET_CLASS (class);
450 	widget_class->get_preferred_width = shell_content_get_preferred_width;
451 	widget_class->get_preferred_height = shell_content_get_preferred_height;
452 	widget_class->size_allocate = shell_content_size_allocate;
453 
454 	container_class = GTK_CONTAINER_CLASS (class);
455 	container_class->remove = shell_content_remove;
456 	container_class->forall = shell_content_forall;
457 
458 	/**
459 	 * EShellContent:alert-bar
460 	 *
461 	 * Displays informational and error messages.
462 	 **/
463 	g_object_class_install_property (
464 		object_class,
465 		PROP_ALERT_BAR,
466 		g_param_spec_object (
467 			"alert-bar",
468 			"Alert Bar",
469 			"Displays informational and error messages",
470 			E_TYPE_ALERT_BAR,
471 			G_PARAM_READABLE));
472 
473 	/**
474 	 * EShellContent:shell-view
475 	 *
476 	 * The #EShellView to which the content widget belongs.
477 	 **/
478 	g_object_class_install_property (
479 		object_class,
480 		PROP_SHELL_VIEW,
481 		g_param_spec_object (
482 			"shell-view",
483 			NULL,
484 			NULL,
485 			E_TYPE_SHELL_VIEW,
486 			G_PARAM_READWRITE |
487 			G_PARAM_CONSTRUCT_ONLY));
488 }
489 
490 static void
491 e_shell_content_alert_sink_init (EAlertSinkInterface *interface)
492 {
493 	interface->submit_alert = shell_content_submit_alert;
494 }
495 
496 static void
497 e_shell_content_init (EShellContent *shell_content)
498 {
499 	shell_content->priv = E_SHELL_CONTENT_GET_PRIVATE (shell_content);
500 
501 	gtk_widget_set_has_window (GTK_WIDGET (shell_content), FALSE);
502 }
503 
504 /**
505  * e_shell_content_new:
506  * @shell_view: an #EShellView
507  *
508  * Creates a new #EShellContent instance belonging to @shell_view.
509  *
510  * Returns: a new #EShellContent instance
511  **/
512 GtkWidget *
513 e_shell_content_new (EShellView *shell_view)
514 {
515 	g_return_val_if_fail (E_IS_SHELL_VIEW (shell_view), NULL);
516 
517 	return g_object_new (
518 		E_TYPE_SHELL_CONTENT, "shell-view", shell_view, NULL);
519 }
520 
521 /**
522  * e_shell_content_set_searchbar:
523  * @shell_content: an #EShellContent
524  * @searchbar: a #GtkWidget, or %NULL
525  *
526  * Packs @searchbar at the top of @shell_content.
527  **/
528 void
529 e_shell_content_set_searchbar (EShellContent *shell_content,
530                                GtkWidget *searchbar)
531 {
532 	g_return_if_fail (E_IS_SHELL_CONTENT (shell_content));
533 
534 	if (searchbar != NULL)
535 		g_return_if_fail (GTK_IS_WIDGET (searchbar));
536 
537 	if (shell_content->priv->searchbar != NULL)
538 		gtk_container_remove (
539 			GTK_CONTAINER (shell_content),
540 			shell_content->priv->searchbar);
541 
542 	shell_content->priv->searchbar = searchbar;
543 
544 	if (searchbar != NULL)
545 		gtk_widget_set_parent (searchbar, GTK_WIDGET (shell_content));
546 
547 	gtk_widget_queue_resize (GTK_WIDGET (shell_content));
548 }
549 
550 /**
551  * e_shell_content_check_state:
552  * @shell_content: an #EShellContent
553  *
554  * #EShellContent subclasses should implement the
555  * <structfield>check_state</structfield> method in #EShellContentClass
556  * to return a set of flags describing the current content selection.
557  * Subclasses are responsible for defining their own flags.  This is
558  * primarily used to assist shell views with updating actions (see
559  * e_shell_view_update_actions()).
560  *
561  * Returns: a set of flags describing the current @shell_content selection
562  **/
563 guint32
564 e_shell_content_check_state (EShellContent *shell_content)
565 {
566 	EShellContentClass *shell_content_class;
567 
568 	g_return_val_if_fail (E_IS_SHELL_CONTENT (shell_content), 0);
569 
570 	shell_content_class = E_SHELL_CONTENT_GET_CLASS (shell_content);
571 	g_return_val_if_fail (shell_content_class->check_state != NULL, 0);
572 
573 	return shell_content_class->check_state (shell_content);
574 }
575 
576 /**
577  * e_shell_content_focus_search_results:
578  * @shell_content: an #EShellContent
579  *
580  * #EShellContent subclasses should implement the
581  * <structfield>focus_search_results</structfield> method in
582  * #EShellContentClass to direct input focus to the widget
583  * displaying search results.  This is usually called during
584  * e_shell_view_execute_search().
585  **/
586 void
587 e_shell_content_focus_search_results (EShellContent *shell_content)
588 {
589 	EShellContentClass *shell_content_class;
590 
591 	g_return_if_fail (E_IS_SHELL_CONTENT (shell_content));
592 
593 	shell_content_class = E_SHELL_CONTENT_GET_CLASS (shell_content);
594 
595 	if (shell_content_class->focus_search_results != NULL)
596 		shell_content_class->focus_search_results (shell_content);
597 }
598 
599 /**
600  * e_shell_content_get_alert_bar:
601  * @shell_content: an #EShellContent
602  *
603  * Returns the #EAlertBar used to display informational and error messages.
604  *
605  * Returns: the #EAlertBar for @shell_content
606  **/
607 GtkWidget *
608 e_shell_content_get_alert_bar (EShellContent *shell_content)
609 {
610 	g_return_val_if_fail (E_IS_SHELL_CONTENT (shell_content), NULL);
611 
612 	return shell_content->priv->alert_bar;
613 }
614 
615 /**
616  * e_shell_content_get_shell_view:
617  * @shell_content: an #EShellContent
618  *
619  * Returns the #EShellView that was passed to e_shell_content_new().
620  *
621  * Returns: the #EShellView to which @shell_content belongs
622  **/
623 EShellView *
624 e_shell_content_get_shell_view (EShellContent *shell_content)
625 {
626 	g_return_val_if_fail (E_IS_SHELL_CONTENT (shell_content), NULL);
627 
628 	return E_SHELL_VIEW (shell_content->priv->shell_view);
629 }
630 
631 void
632 e_shell_content_run_advanced_search_dialog (EShellContent *shell_content)
633 {
634 	EShellView *shell_view;
635 	EShellWindow *shell_window;
636 	GtkWidget *content_area;
637 	GtkWidget *dialog;
638 	GtkWidget *widget;
639 	EFilterRule *rule;
640 	ERuleContext *context;
641 	const gchar *user_filename;
642 	gulong handler_id;
643 	gint response;
644 	EAlert *alert = NULL;
645 
646 	g_return_if_fail (E_IS_SHELL_CONTENT (shell_content));
647 
648 	shell_view = e_shell_content_get_shell_view (shell_content);
649 	shell_window = e_shell_view_get_shell_window (shell_view);
650 	user_filename = shell_content->priv->user_filename;
651 
652 	rule = e_shell_view_get_search_rule (shell_view);
653 
654 	if (rule == NULL)
655 		rule = e_filter_rule_new ();
656 	else
657 		rule = e_filter_rule_clone (rule);
658 
659 	context = E_SHELL_VIEW_GET_CLASS (shell_view)->search_context;
660 	widget = e_filter_rule_get_widget (rule, context);
661 	e_filter_rule_set_source (rule, E_FILTER_SOURCE_INCOMING);
662 
663 	dialog = gtk_dialog_new_with_buttons (
664 		_("Advanced Search"), GTK_WINDOW (shell_window),
665 		GTK_DIALOG_DESTROY_WITH_PARENT,
666 		GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
667 		GTK_STOCK_SAVE, GTK_RESPONSE_APPLY,
668 		GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
669 
670 	gtk_container_set_border_width (GTK_CONTAINER (dialog), 7);
671 	gtk_container_set_border_width (GTK_CONTAINER (widget), 3);
672 	gtk_window_set_default_size (GTK_WINDOW (dialog), 600, 300);
673 
674 	content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
675 	gtk_box_pack_start (GTK_BOX (content_area), widget, TRUE, TRUE, 0);
676 
677 	handler_id = g_signal_connect_swapped (
678 		rule, "changed", G_CALLBACK (
679 		shell_content_dialog_rule_changed), dialog);
680 
681 	shell_content_dialog_rule_changed (dialog, rule);
682 
683 run:
684 	response = gtk_dialog_run (GTK_DIALOG (dialog));
685 
686 	if (response != GTK_RESPONSE_OK && response != GTK_RESPONSE_APPLY)
687 		goto exit;
688 
689 	if (!e_filter_rule_validate (rule, &alert)) {
690 		e_alert_run_dialog (GTK_WINDOW (dialog), alert);
691 		g_object_unref (alert);
692 		alert = NULL;
693 		goto run;
694 	}
695 
696 	e_shell_view_custom_search (shell_view, rule);
697 
698 	if (response == GTK_RESPONSE_APPLY) {
699 		if (!e_rule_context_find_rule (context, rule->name, rule->source))
700 			e_rule_context_add_rule (context, rule);
701 		e_rule_context_save (context, user_filename);
702 		goto run;
703 	}
704 
705 exit:
706 	g_signal_handler_disconnect (rule, handler_id);
707 
708 	g_object_unref (rule);
709 	gtk_widget_destroy (dialog);
710 }
711 
712 void
713 e_shell_content_run_edit_searches_dialog (EShellContent *shell_content)
714 {
715 	EShellView *shell_view;
716 	ERuleContext *context;
717 	ERuleEditor *editor;
718 	const gchar *user_filename;
719 
720 	g_return_if_fail (E_IS_SHELL_CONTENT (shell_content));
721 
722 	shell_view = e_shell_content_get_shell_view (shell_content);
723 	context = E_SHELL_VIEW_GET_CLASS (shell_view)->search_context;
724 	user_filename = shell_content->priv->user_filename;
725 
726 	editor = e_rule_editor_new (
727 		context, E_FILTER_SOURCE_INCOMING, _("Searches"));
728 	gtk_window_set_title (GTK_WINDOW (editor), _("Searches"));
729 
730 	if (gtk_dialog_run (GTK_DIALOG (editor)) == GTK_RESPONSE_OK)
731 		e_rule_context_save (context, user_filename);
732 
733 	gtk_widget_destroy (GTK_WIDGET (editor));
734 }
735 
736 void
737 e_shell_content_run_save_search_dialog (EShellContent *shell_content)
738 {
739 	EShellView *shell_view;
740 	EShellWindow *shell_window;
741 	GtkWidget *content_area;
742 	GtkWidget *dialog;
743 	GtkWidget *widget;
744 	EFilterRule *rule;
745 	ERuleContext *context;
746 	const gchar *user_filename;
747 	gchar *search_name;
748 	gulong handler_id;
749 	gint response;
750 	EAlert *alert = NULL;
751 
752 	g_return_if_fail (E_IS_SHELL_CONTENT (shell_content));
753 
754 	shell_view = e_shell_content_get_shell_view (shell_content);
755 	shell_window = e_shell_view_get_shell_window (shell_view);
756 	user_filename = shell_content->priv->user_filename;
757 
758 	rule = e_shell_view_get_search_rule (shell_view);
759 	g_return_if_fail (E_IS_FILTER_RULE (rule));
760 	rule = e_filter_rule_clone (rule);
761 
762 	search_name = e_shell_view_get_search_name (shell_view);
763 	e_filter_rule_set_name (rule, search_name);
764 	g_free (search_name);
765 
766 	context = E_SHELL_VIEW_GET_CLASS (shell_view)->search_context;
767 	widget = e_filter_rule_get_widget (rule, context);
768 	e_filter_rule_set_source (rule, E_FILTER_SOURCE_INCOMING);
769 
770 	dialog = gtk_dialog_new_with_buttons (
771 		_("Save Search"), GTK_WINDOW (shell_window),
772 		GTK_DIALOG_DESTROY_WITH_PARENT,
773 		GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
774 		GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
775 
776 	gtk_container_set_border_width (GTK_CONTAINER (dialog), 7);
777 	gtk_container_set_border_width (GTK_CONTAINER (widget), 3);
778 	gtk_window_set_default_size (GTK_WINDOW (dialog), 500, 300);
779 
780 	content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
781 	gtk_box_pack_start (GTK_BOX (content_area), widget, TRUE, TRUE, 0);
782 
783 	handler_id = g_signal_connect_swapped (
784 		rule, "changed", G_CALLBACK (
785 		shell_content_dialog_rule_changed), dialog);
786 
787 	shell_content_dialog_rule_changed (dialog, rule);
788 
789 run:
790 	response = gtk_dialog_run (GTK_DIALOG (dialog));
791 
792 	if (response != GTK_RESPONSE_OK)
793 		goto exit;
794 
795 	if (!e_filter_rule_validate (rule, &alert)) {
796 		e_alert_run_dialog (GTK_WINDOW (dialog), alert);
797 		g_object_unref (alert);
798 		alert = NULL;
799 		goto run;
800 	}
801 
802 	/* XXX This function steals the rule reference, so
803 	 *     counteract that by referencing it again. */
804 	e_rule_context_add_rule (context, g_object_ref (rule));
805 
806 	e_rule_context_save (context, user_filename);
807 
808 exit:
809 	g_signal_handler_disconnect (rule, handler_id);
810 
811 	g_object_unref (rule);
812 	gtk_widget_destroy (dialog);
813 }