aboutsummaryrefslogtreecommitdiffstats
path: root/shell/e-shell-content.c
blob: 586b7068595f6c2ef0637f9413abc3e4ffdaae9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/*
 * e-shell-content.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

/**
 * SECTION: e-shell-content
 * @short_description: the right side of the main window
 * @include: shell/e-shell-content.h
 **/

#include "e-shell-content.h"

#include <glib/gi18n.h>

#include "e-util/e-alert-dialog.h"
#include "e-util/e-alert-sink.h"
#include "e-util/e-extensible.h"
#include "e-util/e-util.h"
#include "e-util/e-alert-dialog.h"
#include "filter/e-rule-editor.h"
#include "widgets/misc/e-action-combo-box.h"
#include "widgets/misc/e-alert-bar.h"
#include "widgets/misc/e-hinted-entry.h"

#include "e-shell-backend.h"
#include "e-shell-searchbar.h"
#include "e-shell-view.h"
#include "e-shell-window-actions.h"

#define E_SHELL_CONTENT_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_SHELL_CONTENT, EShellContentPrivate))

struct _EShellContentPrivate {

    gpointer shell_view;    /* weak pointer */

    GtkWidget *alert_bar;
    GtkWidget *searchbar;   /* not referenced */

    /* Custom search rules. */
    gchar *user_filename;
};

enum {
    PROP_0,
    PROP_ALERT_BAR,
    PROP_SHELL_VIEW
};

/* Forward Declarations */
static void e_shell_content_alert_sink_init
                    (EAlertSinkInterface *interface);

G_DEFINE_TYPE_WITH_CODE (
    EShellContent,
    e_shell_content,
    GTK_TYPE_BIN,
    G_IMPLEMENT_INTERFACE (
        E_TYPE_ALERT_SINK, e_shell_content_alert_sink_init)
    G_IMPLEMENT_INTERFACE (
        E_TYPE_EXTENSIBLE, NULL));

static void
shell_content_dialog_rule_changed (GtkWidget *dialog,
                                   EFilterRule *rule)
{
    gboolean sensitive;

    sensitive = (rule != NULL) && (rule->parts != NULL);

    gtk_dialog_set_response_sensitive (
        GTK_DIALOG (dialog), GTK_RESPONSE_OK, sensitive);
    gtk_dialog_set_response_sensitive (
        GTK_DIALOG (dialog), GTK_RESPONSE_APPLY, sensitive);
}

static void
shell_content_set_shell_view (EShellContent *shell_content,
                              EShellView *shell_view)
{
    g_return_if_fail (shell_content->priv->shell_view == NULL);

    shell_content->priv->shell_view = shell_view;

    g_object_add_weak_pointer (
        G_OBJECT (shell_view),
        &shell_content->priv->shell_view);
}

static void
shell_content_set_property (GObject *object,
                            guint property_id,
                            const GValue *value,
                            GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_SHELL_VIEW:
            shell_content_set_shell_view (
                E_SHELL_CONTENT (object),
                g_value_get_object (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
shell_content_get_property (GObject *object,
                            guint property_id,
                            GValue *value,
                            GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_ALERT_BAR:
            g_value_set_object (
                value, e_shell_content_get_alert_bar (
                E_SHELL_CONTENT (object)));
            return;

        case PROP_SHELL_VIEW:
            g_value_set_object (
                value, e_shell_content_get_shell_view (
                E_SHELL_CONTENT (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
shell_content_dispose (GObject *object)
{
    EShellContentPrivate *priv;

    priv = E_SHELL_CONTENT_GET_PRIVATE (object);

    if (priv->shell_view != NULL) {
        g_object_remove_weak_pointer (
            G_OBJECT (priv->shell_view), &priv->shell_view);
        priv->shell_view = NULL;
    }

    if (priv->alert_bar != NULL) {
        g_object_unref (priv->alert_bar);
        priv->alert_bar = NULL;
    }

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (e_shell_content_parent_class)->dispose (object);
}

static void
shell_content_finalize (GObject *object)
{
    EShellContentPrivate *priv;

    priv = E_SHELL_CONTENT_GET_PRIVATE (object);

    g_free (priv->user_filename);

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (e_shell_content_parent_class)->finalize (object);
}

static void
shell_content_constructed (GObject *object)
{
    EShellContent *shell_content;
    EShellBackend *shell_backend;
    EShellView *shell_view;
    GtkWidget *widget;
    const gchar *config_dir;

    shell_content = E_SHELL_CONTENT (object);
    shell_view = e_shell_content_get_shell_view (shell_content);
    shell_backend = e_shell_view_get_shell_backend (shell_view);

    widget = e_alert_bar_new ();
    gtk_widget_set_parent (widget, GTK_WIDGET (shell_content));
    shell_content->priv->alert_bar = g_object_ref_sink (widget);
    /* EAlertBar controls its own visibility. */

    /* XXX Regenerate the filename for custom saved search as done
     *     in shell_view_init_search_context().  ERuleContext ought
     *     to remember the filename when loading rules so you don't
     *     have to keep passing it in when saving rules. */
    config_dir = e_shell_backend_get_config_dir (shell_backend);
    shell_content->priv->user_filename =
        g_build_filename (config_dir, "searches.xml", NULL);

    e_extensible_load_extensions (E_EXTENSIBLE (object));

    if (G_OBJECT_CLASS (e_shell_content_parent_class)->constructed)
        G_OBJECT_CLASS (e_shell_content_parent_class)->constructed (object);
}

static void
shell_content_get_preferred_width (GtkWidget *widget,
                                   gint      *minimum,
                                   gint      *natural)
{
    EShellContentPrivate *priv;
        gint min, nat;
        gint child_min, child_nat;
    GtkWidget *child;

    priv = E_SHELL_CONTENT_GET_PRIVATE (widget);

        *minimum = *natural = 0;

    child = gtk_bin_get_child (GTK_BIN (widget));
        gtk_widget_get_preferred_width (child, &child_min, &child_nat);
        gtk_widget_get_preferred_width (priv->alert_bar, &min, &nat);

        *minimum = MAX (min, child_min);
        *natural = MAX (nat, child_nat);

    if (priv->searchbar == NULL)
        return;

        gtk_widget_get_preferred_width (priv->searchbar, &min, &nat);
        *minimum = MAX (*minimum, min);
        *natural = MAX (*natural, nat);
}

static void
shell_content_get_preferred_height (GtkWidget *widget,
                                    gint      *minimum,
                                    gint      *natural)
{
    EShellContentPrivate *priv;
        gint min, nat;
    GtkWidget *child;

    priv = E_SHELL_CONTENT_GET_PRIVATE (widget);

    child = gtk_bin_get_child (GTK_BIN (widget));
    gtk_widget_get_preferred_height (child, minimum, natural);

    if (priv->searchbar == NULL)
        return;

    gtk_widget_get_preferred_height (priv->searchbar, &min, &nat);
        *minimum += min;
        *natural += nat;
}

static void
shell_content_size_allocate (GtkWidget *widget,
                             GtkAllocation *allocation)
{
    EShellContentPrivate *priv;
    GtkAllocation child_allocation;
    GtkRequisition child_requisition;
    GtkWidget *child;
    gint remaining_height;

    priv = E_SHELL_CONTENT_GET_PRIVATE (widget);

    remaining_height = allocation->height;
    gtk_widget_set_allocation (widget, allocation);

    child_allocation.x = allocation->x;
    child_allocation.y = allocation->y;
    child_allocation.width = allocation->width;

    child_requisition.height = 0;

    /* Alert bar gets to be as tall as it wants (if visible). */

    child = priv->alert_bar;
    child_allocation.y += child_requisition.height;

    if (gtk_widget_get_visible (child))
        gtk_widget_get_preferred_size (child, &child_requisition, NULL);
    else
        child_requisition.height = 0;

    remaining_height -= child_requisition.height;
    child_allocation.height = child_requisition.height;

    gtk_widget_size_allocate (child, &child_allocation);

    /* Search bar gets to be as tall as it wants (if we have one). */

    child = priv->searchbar;
    child_allocation.y += child_requisition.height;

    if (child != NULL)
        gtk_widget_get_preferred_size (child, &child_requisition, NULL);
    else
        child_requisition.height = 0;

    remaining_height -= child_requisition.height;
    child_allocation.height = child_requisition.height;

    if (child != NULL)
        gtk_widget_size_allocate (child, &child_allocation);

    /* The GtkBin child gets whatever vertical space is left. */

    child_allocation.y += child_requisition.height;
    child_allocation.height = remaining_height;

    child = gtk_bin_get_child (GTK_BIN (widget));
    if (child != NULL)
        gtk_widget_size_allocate (child, &child_allocation);
}

static void
shell_content_remove (GtkContainer *container,
                      GtkWidget *widget)
{
    GtkContainerClass *container_class;
    EShellContentPrivate *priv;

    priv = E_SHELL_CONTENT_GET_PRIVATE (container);

    if (widget == priv->alert_bar) {
        gtk_widget_unparent (priv->alert_bar);
        priv->alert_bar = NULL;
        return;
    }

    if (widget == priv->searchbar) {
        gtk_widget_unparent (priv->searchbar);
        priv->searchbar = NULL;
        return;
    }

    /* Chain up to parent's remove() method. */
    container_class = GTK_CONTAINER_CLASS (e_shell_content_parent_class);
    container_class->remove (container, widget);
}

static void
shell_content_forall (GtkContainer *container,
                      gboolean include_internals,
                      GtkCallback callback,
                      gpointer callback_data)
{
    EShellContentPrivate *priv;

    priv = E_SHELL_CONTENT_GET_PRIVATE (container);

    if (priv->alert_bar != NULL)
        callback (priv->alert_bar, callback_data);

    if (priv->searchbar != NULL)
        callback (priv->searchbar, callback_data);

    /* Chain up to parent's forall() method. */
    GTK_CONTAINER_CLASS (e_shell_content_parent_class)->forall (
        container, include_internals, callback, callback_data);
}

static void
shell_content_submit_alert (EAlertSink *alert_sink,
                            EAlert *alert)
{
    EShellView *shell_view;
    EShellWindow *shell_window;
    EShellContent *shell_content;
    GtkWidget *alert_bar;
    GtkWidget *dialog;

    shell_content = E_SHELL_CONTENT (alert_sink);
    shell_view = e_shell_content_get_shell_view (shell_content);
    shell_window = e_shell_view_get_shell_window (shell_view);
    alert_bar = e_shell_content_get_alert_bar (shell_content);

    switch (e_alert_get_message_type (alert)) {
        case GTK_MESSAGE_INFO:
        case GTK_MESSAGE_WARNING:
        case GTK_MESSAGE_ERROR:
            e_alert_bar_add_alert (
                E_ALERT_BAR (alert_bar), alert);
            break;

        default:
            dialog = e_alert_dialog_new (
                GTK_WINDOW (shell_window), alert);
            gtk_dialog_run (GTK_DIALOG (dialog));
            gtk_widget_destroy (dialog);
            break;
    }
}

static void
e_shell_content_class_init (EShellContentClass *class)
{
    GObjectClass *object_class;
    GtkWidgetClass *widget_class;
    GtkContainerClass *container_class;

    g_type_class_add_private (class, sizeof (EShellContentPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = shell_content_set_property;
    object_class->get_property = shell_content_get_property;
    object_class->dispose = shell_content_dispose;
    object_class->finalize = shell_content_finalize;
    object_class->constructed = shell_content_constructed;

    widget_class = GTK_WIDGET_CLASS (class);
        widget_class->get_preferred_width = shell_content_get_preferred_width;
        widget_class->get_preferred_height = shell_content_get_preferred_height;
    widget_class->size_allocate = shell_content_size_allocate;

    container_class = GTK_CONTAINER_CLASS (class);
    container_class->remove = shell_content_remove;
    container_class->forall = shell_content_forall;

    /**
     * EShellContent:alert-bar
     *
     * Displays informational and error messages.
     **/
    g_object_class_install_property (
        object_class,
        PROP_ALERT_BAR,
        g_param_spec_object (
            "alert-bar",
            "Alert Bar",
            "Displays informational and error messages",
            E_TYPE_ALERT_BAR,
            G_PARAM_READABLE));

    /**
     * EShellContent:shell-view
     *
     * The #EShellView to which the content widget belongs.
     **/
    g_object_class_install_property (
        object_class,
        PROP_SHELL_VIEW,
        g_param_spec_object (
            "shell-view",
            NULL,
            NULL,
            E_TYPE_SHELL_VIEW,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT_ONLY));
}

static void
e_shell_content_alert_sink_init (EAlertSinkInterface *interface)
{
    interface->submit_alert = shell_content_submit_alert;
}

static void
e_shell_content_init (EShellContent *shell_content)
{
    shell_content->priv = E_SHELL_CONTENT_GET_PRIVATE (shell_content);

    gtk_widget_set_has_window (GTK_WIDGET (shell_content), FALSE);
}

/**
 * e_shell_content_new:
 * @shell_view: an #EShellView
 *
 * Creates a new #EShellContent instance belonging to @shell_view.
 *
 * Returns: a new #EShellContent instance
 **/
GtkWidget *
e_shell_content_new (EShellView *shell_view)
{
    g_return_val_if_fail (E_IS_SHELL_VIEW (shell_view), NULL);

    return g_object_new (
        E_TYPE_SHELL_CONTENT, "shell-view", shell_view, NULL);
}

/**
 * e_shell_content_set_searchbar:
 * @shell_content: an #EShellContent
 * @searchbar: a #GtkWidget, or %NULL
 *
 * Packs @searchbar at the top of @shell_content.
 **/
void
e_shell_content_set_searchbar (EShellContent *shell_content,
                               GtkWidget *searchbar)
{
    g_return_if_fail (E_IS_SHELL_CONTENT (shell_content));

    if (searchbar != NULL)
        g_return_if_fail (GTK_IS_WIDGET (searchbar));

    if (shell_content->priv->searchbar != NULL)
        gtk_container_remove (
            GTK_CONTAINER (shell_content),
            shell_content->priv->searchbar);

    shell_content->priv->searchbar = searchbar;

    if (searchbar != NULL)
        gtk_widget_set_parent (searchbar, GTK_WIDGET (shell_content));

    gtk_widget_queue_resize (GTK_WIDGET (shell_content));
}

/**
 * e_shell_content_check_state:
 * @shell_content: an #EShellContent
 *
 * #EShellContent subclasses should implement the
 * <structfield>check_state</structfield> method in #EShellContentClass
 * to return a set of flags describing the current content selection.
 * Subclasses are responsible for defining their own flags.  This is
 * primarily used to assist shell views with updating actions (see
 * e_shell_view_update_actions()).
 *
 * Returns: a set of flags describing the current @shell_content selection
 **/
guint32
e_shell_content_check_state (EShellContent *shell_content)
{
    EShellContentClass *shell_content_class;

    g_return_val_if_fail (E_IS_SHELL_CONTENT (shell_content), 0);

    shell_content_class = E_SHELL_CONTENT_GET_CLASS (shell_content);
    g_return_val_if_fail (shell_content_class->check_state != NULL, 0);

    return shell_content_class->check_state (shell_content);
}

/**
 * e_shell_content_focus_search_results:
 * @shell_content: an #EShellContent
 *
 * #EShellContent subclasses should implement the
 * <structfield>focus_search_results</structfield> method in
 * #EShellContentClass to direct input focus to the widget
 * displaying search results.  This is usually called during
 * e_shell_view_execute_search().
 **/
void
e_shell_content_focus_search_results (EShellContent *shell_content)
{
    EShellContentClass *shell_content_class;

    g_return_if_fail (E_IS_SHELL_CONTENT (shell_content));

    shell_content_class = E_SHELL_CONTENT_GET_CLASS (shell_content);

    if (shell_content_class->focus_search_results != NULL)
        shell_content_class->focus_search_results (shell_content);
}

/**
 * e_shell_content_get_alert_bar:
 * @shell_content: an #EShellContent
 *
 * Returns the #EAlertBar used to display informational and error messages.
 *
 * Returns: the #EAlertBar for @shell_content
 **/
GtkWidget *
e_shell_content_get_alert_bar (EShellContent *shell_content)
{
    g_return_val_if_fail (E_IS_SHELL_CONTENT (shell_content), NULL);

    return shell_content->priv->alert_bar;
}

/**
 * e_shell_content_get_shell_view:
 * @shell_content: an #EShellContent
 *
 * Returns the #EShellView that was passed to e_shell_content_new().
 *
 * Returns: the #EShellView to which @shell_content belongs
 **/
EShellView *
e_shell_content_get_shell_view (EShellContent *shell_content)
{
    g_return_val_if_fail (E_IS_SHELL_CONTENT (shell_content), NULL);

    return E_SHELL_VIEW (shell_content->priv->shell_view);
}

void
e_shell_content_run_advanced_search_dialog (EShellContent *shell_content)
{
    EShellView *shell_view;
    EShellWindow *shell_window;
    GtkWidget *content_area;
    GtkWidget *dialog;
    GtkWidget *widget;
    EFilterRule *rule;
    ERuleContext *context;
    const gchar *user_filename;
    gint response;
    EAlert *alert = NULL;

    g_return_if_fail (E_IS_SHELL_CONTENT (shell_content));

    shell_view = e_shell_content_get_shell_view (shell_content);
    shell_window = e_shell_view_get_shell_window (shell_view);
    user_filename = shell_content->priv->user_filename;

    rule = e_shell_view_get_search_rule (shell_view);

    if (rule == NULL)
        rule = e_filter_rule_new ();
    else
        rule = e_filter_rule_clone (rule);

    context = E_SHELL_VIEW_GET_CLASS (shell_view)->search_context;
    widget = e_filter_rule_get_widget (rule, context);
    e_filter_rule_set_source (rule, E_FILTER_SOURCE_INCOMING);

    dialog = gtk_dialog_new_with_buttons (
        _("Advanced Search"), GTK_WINDOW (shell_window),
        GTK_DIALOG_DESTROY_WITH_PARENT,
        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
        GTK_STOCK_SAVE, GTK_RESPONSE_APPLY,
        GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);

    gtk_container_set_border_width (GTK_CONTAINER (dialog), 7);
    gtk_container_set_border_width (GTK_CONTAINER (widget), 3);
    gtk_window_set_default_size (GTK_WINDOW (dialog), 600, 300);

    content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
    gtk_box_pack_start (GTK_BOX (content_area), widget, TRUE, TRUE, 0);

    g_signal_connect_swapped (
        rule, "changed", G_CALLBACK (
        shell_content_dialog_rule_changed), dialog);

    shell_content_dialog_rule_changed (dialog, rule);

run:
    response = gtk_dialog_run (GTK_DIALOG (dialog));

    if (response != GTK_RESPONSE_OK && response != GTK_RESPONSE_APPLY)
        goto exit;

    if (!e_filter_rule_validate (rule, &alert)) {
        e_alert_run_dialog (GTK_WINDOW (dialog), alert);
        g_object_unref (alert);
        alert = NULL;
        goto run;
    }

    e_shell_view_custom_search (shell_view, rule);

    if (response == GTK_RESPONSE_APPLY) {
        if (!e_rule_context_find_rule (context, rule->name, rule->source))
            e_rule_context_add_rule (context, rule);
        e_rule_context_save (context, user_filename);
        goto run;
    }

exit:
    g_object_unref (rule);
    gtk_widget_destroy (dialog);
}

void
e_shell_content_run_edit_searches_dialog (EShellContent *shell_content)
{
    EShellView *shell_view;
    ERuleContext *context;
    ERuleEditor *editor;
    const gchar *user_filename;

    g_return_if_fail (E_IS_SHELL_CONTENT (shell_content));

    shell_view = e_shell_content_get_shell_view (shell_content);
    context = E_SHELL_VIEW_GET_CLASS (shell_view)->search_context;
    user_filename = shell_content->priv->user_filename;

    editor = e_rule_editor_new (
        context, E_FILTER_SOURCE_INCOMING, _("Searches"));
    gtk_window_set_title (GTK_WINDOW (editor), _("Searches"));

    if (gtk_dialog_run (GTK_DIALOG (editor)) == GTK_RESPONSE_OK)
        e_rule_context_save (context, user_filename);

    gtk_widget_destroy (GTK_WIDGET (editor));
}

void
e_shell_content_run_save_search_dialog (EShellContent *shell_content)
{
    EShellView *shell_view;
    EShellWindow *shell_window;
    GtkWidget *content_area;
    GtkWidget *dialog;
    GtkWidget *widget;
    EFilterRule *rule;
    ERuleContext *context;
    const gchar *user_filename;
    gchar *search_name;
    gint response;
    EAlert *alert = NULL;

    g_return_if_fail (E_IS_SHELL_CONTENT (shell_content));

    shell_view = e_shell_content_get_shell_view (shell_content);
    shell_window = e_shell_view_get_shell_window (shell_view);
    user_filename = shell_content->priv->user_filename;

    rule = e_shell_view_get_search_rule (shell_view);
    g_return_if_fail (E_IS_FILTER_RULE (rule));
    rule = e_filter_rule_clone (rule);

    search_name = e_shell_view_get_search_name (shell_view);
    e_filter_rule_set_name (rule, search_name);
    g_free (search_name);

    context = E_SHELL_VIEW_GET_CLASS (shell_view)->search_context;
    widget = e_filter_rule_get_widget (rule, context);
    e_filter_rule_set_source (rule, E_FILTER_SOURCE_INCOMING);

    dialog = gtk_dialog_new_with_buttons (
        _("Save Search"), GTK_WINDOW (shell_window),
        GTK_DIALOG_DESTROY_WITH_PARENT,
        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
        GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);

    gtk_container_set_border_width (GTK_CONTAINER (dialog), 7);
    gtk_container_set_border_width (GTK_CONTAINER (widget), 3);
    gtk_window_set_default_size (GTK_WINDOW (dialog), 500, 300);

    content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
    gtk_box_pack_start (GTK_BOX (content_area), widget, TRUE, TRUE, 0);

    g_signal_connect_swapped (
        rule, "changed", G_CALLBACK (
        shell_content_dialog_rule_changed), dialog);

    shell_content_dialog_rule_changed (dialog, rule);

run:
    response = gtk_dialog_run (GTK_DIALOG (dialog));

    if (response != GTK_RESPONSE_OK)
        goto exit;

    if (!e_filter_rule_validate (rule, &alert)) {
        e_alert_run_dialog (GTK_WINDOW (dialog), alert);
        g_object_unref (alert);
        alert = NULL;
        goto run;
    }

    /* XXX This function steals the rule reference, so
     *     counteract that by referencing it again. */
    e_rule_context_add_rule (context, g_object_ref (rule));

    e_rule_context_save (context, user_filename);

exit:
    g_object_unref (rule);
    gtk_widget_destroy (dialog);
}