aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/cal-search-bar.c
blob: d82d0e7ce6de52ff55c2e534a665bb507dce25b9 (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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
/* Evolution calendar - Search bar widget for calendar views
 *
 * Copyright (C) 2001 Ximian, Inc.
 *
 * Author: Federico Mena-Quintero <federico@ximian.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gtk/gtkmenu.h>
#include <gtk/gtkmenuitem.h>
#include <gtk/gtkoptionmenu.h>
#include <gtk/gtksignal.h>
#include <glib/gi18n.h>
#include <libedataserver/e-categories.h>
#include <libecal/e-cal-time-util.h>
#include <e-util/e-icon-factory.h>
#include <libedataserver/e-categories.h>
#include <filter/rule-editor.h>

#include "cal-search-bar.h"

#include "e-util/e-util.h"
#include "e-util/e-error.h"
#include "e-util/e-util-private.h"

typedef struct CALSearchBarItem {
     ESearchBarItem search;
     const char *image;
} CALSearchBarItem;

static ESearchBarItem calendar_search_items[] = {
    E_FILTERBAR_ADVANCED,
    {NULL, 0, 0},
    E_FILTERBAR_SAVE,
    E_FILTERBAR_EDIT,
    {NULL, -1, 0}
};


/* IDs and option items for the ESearchBar */
enum {
    SEARCH_SUMMARY_CONTAINS,
    SEARCH_DESCRIPTION_CONTAINS,
    SEARCH_ANY_FIELD_CONTAINS,
    SEARCH_CATEGORY_IS,
    SEARCH_COMMENT_CONTAINS,
    SEARCH_LOCATION_CONTAINS,
    SEARCH_ATTENDEE_CONTAINS

};

/* Comments are disabled because they are kind of useless right now, see bug 33247 */
static ESearchBarItem search_option_items[] = {
    { N_("Summary contains"), SEARCH_SUMMARY_CONTAINS, ESB_ITEMTYPE_RADIO },
    { N_("Description contains"), SEARCH_DESCRIPTION_CONTAINS, ESB_ITEMTYPE_RADIO },
    { N_("Category is"), SEARCH_CATEGORY_IS, ESB_ITEMTYPE_RADIO },
    { N_("Comment contains"), SEARCH_COMMENT_CONTAINS, ESB_ITEMTYPE_RADIO },
    { N_("Location contains"), SEARCH_LOCATION_CONTAINS, ESB_ITEMTYPE_RADIO },
    { N_("Any field contains"), SEARCH_ANY_FIELD_CONTAINS, ESB_ITEMTYPE_RADIO },
};

/* IDs for the categories suboptions */

typedef enum {
    CATEGORIES_ALL,
    CATEGORIES_UNMATCHED,
    LAST_FIELD
} common_search_options;

typedef enum {
    N_DAY_TASK = LAST_FIELD,
    ACTIVE_TASK,
    OVERDUE_TASK,
    COMPLETED_TASK,
    TASK_WITH_ATTACHMENT,
    TASK_LAST_FIELD
} task_search_options;

typedef enum  {
    ACTIVE_APPONTMENT = LAST_FIELD,
    N_DAY_APPOINTMENT,
    CAL_LAST_FIELD
} cal_search_options;

/* We add 2 to the offset to include the separators used to differenciate the quick search queries. */
#define CATEGORIES_TASKS_OFFSET (TASK_LAST_FIELD + 2)
#define CATEGORIES_MEMOS_OFFSET (LAST_FIELD + 1)
#define CATEGORIES_CALENDAR_OFFSET (CAL_LAST_FIELD + 2)

/* Private part of the CalSearchBar structure */
struct CalSearchBarPrivate {
    /* Array of categories */
    GPtrArray *categories;

    RuleContext *search_context;
    FilterRule *search_rule;
    guint32 view_flag;

    time_t start;
    time_t end;
};

static void cal_search_bar_destroy (GtkObject *object);

static void cal_search_bar_search_activated (ESearchBar *search);

/* Signal IDs */
enum {
    SEXP_CHANGED,
    CATEGORY_CHANGED,
    LAST_SIGNAL
};

static guint cal_search_bar_signals[LAST_SIGNAL] = { 0 };


G_DEFINE_TYPE (CalSearchBar, cal_search_bar, E_FILTER_BAR_TYPE)

/* Class initialization function for the calendar search bar */
static void
cal_search_bar_class_init (CalSearchBarClass *klass)
{

    GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
    ESearchBarClass *search_bar_class = E_SEARCH_BAR_CLASS (klass);

    cal_search_bar_signals[SEXP_CHANGED] =
        gtk_signal_new ("sexp_changed",
                GTK_RUN_FIRST,
                G_TYPE_FROM_CLASS (object_class),
                GTK_SIGNAL_OFFSET (CalSearchBarClass, sexp_changed),
                gtk_marshal_NONE__STRING,
                GTK_TYPE_NONE, 1,
                GTK_TYPE_STRING);

    cal_search_bar_signals[CATEGORY_CHANGED] =
        gtk_signal_new ("category_changed",
                GTK_RUN_FIRST,
                G_TYPE_FROM_CLASS (object_class),
                GTK_SIGNAL_OFFSET (CalSearchBarClass, category_changed),
                gtk_marshal_NONE__STRING,
                GTK_TYPE_NONE, 1,
                GTK_TYPE_STRING);

    klass->sexp_changed = NULL;
    klass->category_changed = NULL;

    search_bar_class->search_activated = cal_search_bar_search_activated;
    object_class->destroy = cal_search_bar_destroy;
}

/* Object initialization function for the calendar search bar */
static void
cal_search_bar_init (CalSearchBar *cal_search)
{
    CalSearchBarPrivate *priv;

    priv = g_new (CalSearchBarPrivate, 1);
    cal_search->priv = priv;

    priv->categories = g_ptr_array_new ();
    g_ptr_array_set_size (priv->categories, 0);

    priv->start = -1;
    priv->end = -1;
}

/* Frees an array of categories */
static void
free_categories (GPtrArray *categories)
{
    int i;

    for (i = 0; i < categories->len; i++) {
        if (categories->pdata[i] == NULL)
            continue;
        g_free (categories->pdata[i]);
    }

    g_ptr_array_free (categories, TRUE);
}

/* Destroy handler for the calendar search bar */
static void
cal_search_bar_destroy (GtkObject *object)
{
    CalSearchBar *cal_search;
    CalSearchBarPrivate *priv;

    g_return_if_fail (object != NULL);
    g_return_if_fail (IS_CAL_SEARCH_BAR (object));

    cal_search = CAL_SEARCH_BAR (object);
    priv = cal_search->priv;

    if (priv) {
        if (priv->categories) {
            free_categories (priv->categories);
            priv->categories = NULL;
        }

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

        /* FIXME
        if (priv->search_context) {
            g_object_unref (priv->search_context);
            priv->search_context = NULL;
        }*/

        g_free (priv);
        cal_search->priv = NULL;
    }

    if (GTK_OBJECT_CLASS (cal_search_bar_parent_class)->destroy)
        (* GTK_OBJECT_CLASS (cal_search_bar_parent_class)->destroy) (object);
}



/* Emits the "sexp_changed" signal for the calendar search bar */
static void
notify_sexp_changed (CalSearchBar *cal_search, const char *sexp)
{
    gtk_signal_emit (GTK_OBJECT (cal_search), cal_search_bar_signals[SEXP_CHANGED],
             sexp);
}

/* Returns the string of the currently selected category, NULL for "Unmatched" and "All
*/
static const char *
get_current_category (CalSearchBar *cal_search)
{
    CalSearchBarPrivate *priv;
    gint viewid, i = -1;

    priv = cal_search->priv;

    g_return_val_if_fail (priv->categories != NULL, NULL);

    viewid = e_search_bar_get_viewitem_id (E_SEARCH_BAR (cal_search));

    if (viewid == CATEGORIES_ALL || viewid == CATEGORIES_UNMATCHED)
        return NULL;

    if (priv->view_flag == CAL_SEARCH_TASKS_DEFAULT)
        i = viewid - CATEGORIES_TASKS_OFFSET;
    else if (priv->view_flag == CAL_SEARCH_MEMOS_DEFAULT)
        i = viewid - CATEGORIES_MEMOS_OFFSET;
    else if (priv->view_flag == CAL_SEARCH_CALENDAR_DEFAULT)
        i = viewid - CATEGORIES_CALENDAR_OFFSET;

    if (i >= 0 && i < priv->categories->len)
        return priv->categories->pdata[i];
    else
        return NULL;
}


/* Returns a sexp for the selected category in the drop-down menu.  The "All"
 * option is returned as (const char *) 1, and the "Unfiled" option is returned
 * as NULL.
 */
static char *
get_show_option_sexp (CalSearchBar *cal_search)
{
    CalSearchBarPrivate *priv ;
    gint viewid ;
    char *start, *end, *due, *ret = NULL;
    const char *category = NULL ;
    time_t start_range, end_range;

    priv = cal_search->priv;
    viewid = e_search_bar_get_viewitem_id (E_SEARCH_BAR (cal_search));

    if (viewid == CATEGORIES_UNMATCHED)
        return g_strdup ("(has-categories? #f)"); /* Unfiled items */
    else if (viewid == CATEGORIES_ALL)
        return NULL; /* All items */

    switch (priv->view_flag) {
    case CAL_SEARCH_TASKS_DEFAULT:
        if (viewid == N_DAY_TASK) {
            start_range = time(NULL);
            end_range = time_add_day(start_range, 7);
            start = isodate_from_time_t (start_range);
            due = isodate_from_time_t (end_range);

            ret =  g_strdup_printf ("(due-in-time-range? (make-time \"%s\")"
                    "                      (make-time \"%s\"))",
                    start, due);

            g_free (start);
            g_free (due);

            return ret;
        } else if (viewid == ACTIVE_TASK) {
            /* Shows the tasks due for an year from now which are not completed yet*/
            start_range = time(NULL);
            end_range = time_add_day(start_range, 365);
            start = isodate_from_time_t (start_range);
            due = isodate_from_time_t (end_range);

            ret =  g_strdup_printf ("(and (due-in-time-range? (make-time \"%s\")"
                    "                      (make-time \"%s\")) (not (is-completed?)))",
                    start, due);

            g_free (start);
            g_free (due);

            return ret;
        } else if (viewid == OVERDUE_TASK) {
            /* Shows the tasks which are overdue from lower limit 1970 to the current time */
            start_range = 0;
            end_range = time (NULL);
            start = isodate_from_time_t (start_range);
            due = isodate_from_time_t (end_range);

            ret =  g_strdup_printf ("(and (due-in-time-range? (make-time \"%s\")"
                    "                      (make-time \"%s\")) (not (is-completed?)))",
                    start, due);

            g_free (start);
            g_free (due);

            return ret;
        } else if (viewid == COMPLETED_TASK)
            return g_strdup ("(is-completed?)");
        else if (viewid == TASK_WITH_ATTACHMENT)
            return g_strdup ("(has-attachments?)");
        break;
    case CAL_SEARCH_CALENDAR_DEFAULT:
        if (viewid == ACTIVE_APPONTMENT) {
            /* Shows next one year's Appointments */
            start_range = time (NULL);
            end_range = time_add_day (start_range, 365);
            start = isodate_from_time_t (start_range);
            end = isodate_from_time_t (end_range);

            ret = g_strdup_printf ("(occur-in-time-range? (make-time \"%s\")"
                    "                      (make-time \"%s\"))",
                    start, end);

            cal_search->priv->start = start_range;
            cal_search->priv->end = end_range;

            g_free (start);
            g_free (end);

            return ret;
        } else if (viewid == N_DAY_APPOINTMENT) {
            start_range = time (NULL);
            end_range = time_add_day (start_range, 7);
            start = isodate_from_time_t (start_range);
            end = isodate_from_time_t (end_range);

            ret = g_strdup_printf ("(occur-in-time-range? (make-time \"%s\")"
                    "                      (make-time \"%s\"))",
                    start, end);

            cal_search->priv->start = start_range;
            cal_search->priv->end = end_range;

            g_free (start);
            g_free (end);

            return ret;
        }
        break;
    default:
        break;
    }

    category = get_current_category (cal_search);

    if (category != NULL)
        return g_strdup_printf ("(has-categories? \"%s\")", category);
    else
        return NULL;
}

/* Sets the query string to be (contains? "field" "text") */
static void
notify_e_cal_view_contains (CalSearchBar *cal_search, const char *field, const char *view)
{
    char *text = NULL;
    char *sexp = " ";

    text = e_search_bar_get_text (E_SEARCH_BAR (cal_search));

    if (!text)
        return; /* This is an error in the UTF8 conversion, not an empty string! */

    if (text && *text) {
        sexp = g_strdup_printf ("(contains? \"%s\" \"%s\")", field, text);
        g_free (text);
    } else
        sexp = g_strdup ("(contains? \"summary\" \"\")"); /* Show all */


    /* Apply the selected view on search */
    if (view && *view){
        sexp = g_strconcat ("(and ",sexp, view, ")", NULL);
    }

    notify_sexp_changed (cal_search, sexp);

    g_free (sexp);
}

#if 0
/* Sets the query string to the appropriate match for categories */
static void
notify_category_is (CalSearchBar *cal_search)
{
    char *sexp;

    sexp = get_show_option_sexp (cal_search);

    if (!sexp)
        notify_sexp_changed (cal_search, "#t"); /* Match all */
    else
        notify_sexp_changed (cal_search, sexp);

    if (sexp)
        g_free (sexp);
}
#endif

/* Creates a new query from the values in the widgets and notifies upstream */
static void
regen_query (CalSearchBar *cal_search)
{
    int id;
    char *show_option_sexp = NULL;
    char *sexp = NULL;
    GString *out = NULL;
    EFilterBar *efb = (EFilterBar *) cal_search;

    /* Fetch the data from the ESearchBar's entry widgets */
    id = e_search_bar_get_item_id (E_SEARCH_BAR (cal_search));

    cal_search->priv->start = -1;
    cal_search->priv->end = -1;

    /* Get the selected view */
    show_option_sexp = get_show_option_sexp (cal_search);

    /* Generate the different types of queries */
    switch (id) {
    case SEARCH_ANY_FIELD_CONTAINS:
        notify_e_cal_view_contains (cal_search, "any", show_option_sexp);
        break;

    case SEARCH_SUMMARY_CONTAINS:
        notify_e_cal_view_contains (cal_search, "summary", show_option_sexp);
        break;

    case SEARCH_DESCRIPTION_CONTAINS:
        notify_e_cal_view_contains (cal_search, "description", show_option_sexp);
        break;

    case SEARCH_COMMENT_CONTAINS:
        notify_e_cal_view_contains (cal_search, "comment", show_option_sexp);
        break;

    case SEARCH_LOCATION_CONTAINS:
        notify_e_cal_view_contains (cal_search, "location", show_option_sexp);
        break;
    case SEARCH_ATTENDEE_CONTAINS:
        notify_e_cal_view_contains (cal_search, "attendee", show_option_sexp);
        break;
    case  E_FILTERBAR_ADVANCED_ID:
        out = g_string_new ("");
        filter_rule_build_code (efb->current_query, out);

        if (show_option_sexp && *show_option_sexp)
            sexp = g_strconcat ("(and ", out->str, show_option_sexp, ")", NULL);

        notify_sexp_changed (cal_search, sexp ? sexp : out->str);

        g_string_free (out, TRUE);
        g_free(sexp);
        break;

    default:
        g_return_if_reached ();
    }

    g_free (show_option_sexp);
}

#if 0
static void
regen_view_query (CalSearchBar *cal_search)
{
        const char *category;
    notify_category_is (cal_search);

    category = cal_search_bar_get_category (cal_search);
    gtk_signal_emit (GTK_OBJECT (cal_search), cal_search_bar_signals[CATEGORY_CHANGED],
             category);
}
#endif

/* search_activated handler for the calendar search bar */
static void
cal_search_bar_search_activated (ESearchBar *search)
{
    CalSearchBar *cal_search;

    cal_search = CAL_SEARCH_BAR (search);
    regen_query (cal_search);
}

static GtkWidget *
generate_viewoption_menu (CALSearchBarItem *subitems)
{
    GtkWidget *menu, *menu_item;
    gint i = 0;

    menu = gtk_menu_new ();

    for (i = 0; subitems[i].search.id != -1; ++i) {
        if (subitems[i].search.text) {
            char *str = NULL;
            str = e_str_without_underscores (subitems[i].search.text);
            menu_item = gtk_image_menu_item_new_with_label (str);
/*          if (subitems[i].image)
                gtk_image_menu_item_set_image (menu_item, e_icon_factory_get_image (subitems[i].image, E_ICON_SIZE_MENU));*/
            g_free (str);
        } else {
            menu_item = gtk_menu_item_new ();
            gtk_widget_set_sensitive (menu_item, FALSE);
        }

        g_object_set_data (G_OBJECT (menu_item), "EsbItemId",
                   GINT_TO_POINTER (subitems[i].search.id));

        gtk_widget_show (menu_item);
        gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);
    }

    return menu;
}

static void
setup_category_options (CalSearchBar *cal_search, CALSearchBarItem *subitems, gint index, gint offset)
{
    CalSearchBarPrivate *priv;
    gint i;

    priv = cal_search->priv;

    if (priv->categories->len > 0) {
        subitems[index].search.text = NULL; /* separator */
        subitems[index].search.id = 0;
        subitems[index].image = NULL;

        for (i = 0; i < priv->categories->len; i++) {
            const char *category;

            category = priv->categories->pdata[i] ? priv->categories->pdata [i] : "";

            /* The search.text field should not be free'd */
            subitems[i + offset].search.text = (char *) category;
            subitems[i + offset].search.id = i + offset;
            subitems[i + offset].image = e_categories_get_icon_file_for (category);
        }
        index = i + offset;
    }

    subitems[index].search.id = -1; /* terminator */
    subitems[index].search.text = NULL;
    subitems[index].image = NULL;
}


/* Creates the suboptions menu for the ESearchBar with the list of categories */
static void
make_suboptions (CalSearchBar *cal_search)
{
    CalSearchBarPrivate *priv;
    CALSearchBarItem *subitems = NULL;
    GtkWidget *menu;

    priv = cal_search->priv;

    g_return_if_fail (priv->categories != NULL);

    /* Categories plus "all", "unmatched", separator, terminator */

    /* All, unmatched, separator */

    if (priv->view_flag == CAL_SEARCH_TASKS_DEFAULT) {
        subitems = g_new (CALSearchBarItem, priv->categories->len + CATEGORIES_TASKS_OFFSET + 1);

        subitems[0].search.text = _("Any Category");
        subitems[0].search.id = CATEGORIES_ALL;
        subitems[0].image = NULL;

        subitems[1].search.text = _("Unmatched");
        subitems[1].search.id = CATEGORIES_UNMATCHED;
        subitems[1].image = NULL;

        subitems[2].search.text = NULL;
        subitems[2].search.id = 0;
        subitems[2].image = NULL;

        subitems[3].search.text = _("Next 7 Days' Tasks");
        subitems[3].search.id = N_DAY_TASK;
        subitems[3].image = NULL;

        subitems[4].search.text = _("Active Tasks");
        subitems[4].search.id = ACTIVE_TASK;
        subitems[4].image = NULL;

        subitems[5].search.text = _("Overdue Tasks");
        subitems[5].search.id = OVERDUE_TASK;
        subitems[5].image = NULL;

        subitems[6].search.text = _("Completed Tasks");
        subitems[6].search.id = COMPLETED_TASK;
        subitems[6].image = NULL;

        subitems[7].search.text = _("Tasks with Attachments");
        subitems[7].search.id = TASK_WITH_ATTACHMENT;
        subitems[7].image = NULL;

        /* All the other items */
        setup_category_options (cal_search, subitems, 8, CATEGORIES_TASKS_OFFSET);

        menu = generate_viewoption_menu (subitems);
        e_search_bar_set_viewoption_menu ((ESearchBar *)cal_search, menu);

    } else if (priv->view_flag == CAL_SEARCH_MEMOS_DEFAULT) {
        subitems = g_new (CALSearchBarItem, priv->categories->len + CATEGORIES_MEMOS_OFFSET + 1);

        /* All, unmatched, separator */

        subitems[0].search.text = _("Any Category");
        subitems[0].search.id = CATEGORIES_ALL;
        subitems[0].image = NULL;

        subitems[1].search.text = _("Unmatched");
        subitems[1].search.id = CATEGORIES_UNMATCHED;
        subitems[1].image = NULL;

        /* All the other items */
        setup_category_options (cal_search, subitems, 2, CATEGORIES_MEMOS_OFFSET);

        menu = generate_viewoption_menu (subitems);
        e_search_bar_set_viewoption_menu ((ESearchBar *)cal_search, menu);

    } else if (priv->view_flag == CAL_SEARCH_CALENDAR_DEFAULT) {
        subitems = g_new (CALSearchBarItem, priv->categories->len + CATEGORIES_CALENDAR_OFFSET + 1);

        /* All, unmatched, separator */

        subitems[0].search.text = _("Any Category");
        subitems[0].search.id = CATEGORIES_ALL;
        subitems[0].image = NULL;

        subitems[1].search.text = _("Unmatched");
        subitems[1].search.id = CATEGORIES_UNMATCHED;
        subitems[1].image = NULL;

        subitems[2].search.text = NULL;
        subitems[2].search.id = 0;
        subitems[2].image = NULL;

        subitems[3].search.text = _("Active Appointments");
        subitems[3].search.id = ACTIVE_APPONTMENT;
        subitems[3].image = NULL;

        subitems[4].search.text = _("Next 7 Days' Appointments");
        subitems[4].search.id = N_DAY_APPOINTMENT;
        subitems[4].image = NULL;

        /* All the other items */
        setup_category_options (cal_search, subitems, 5, CATEGORIES_CALENDAR_OFFSET);

        menu = generate_viewoption_menu (subitems);
        e_search_bar_set_viewoption_menu ((ESearchBar *)cal_search, menu);
    }

    if(subitems != NULL)
        g_free (subitems);
}

static void
search_menu_activated (ESearchBar *esb, int id)
{
    if (id == E_FILTERBAR_ADVANCED_ID)
        e_search_bar_set_item_id (esb, id);
}

/**
 * cal_search_bar_construct:
 * @cal_search: A calendar search bar.
 * @flags: bitfield of items to appear in the search menu
 *
 * Constructs a calendar search bar by binding its menu and option items.
 *
 * Return value: The same value as @cal_search.
 **/
CalSearchBar *
cal_search_bar_construct (CalSearchBar *cal_search, guint32 flags)
{
    ESearchBarItem *items;
    guint32 bit = 0x1;
    int i, j;
    char *xmlfile = NULL;
    char *userfile = NULL;
    FilterPart *part;
    RuleContext *search_context;
    FilterRule  *search_rule;

    g_return_val_if_fail (IS_CAL_SEARCH_BAR (cal_search), NULL);

    items = g_alloca ((G_N_ELEMENTS (search_option_items) + 1) * sizeof (ESearchBarItem));
    for (i = 0, j = 0; i < G_N_ELEMENTS (search_option_items); i++, bit <<= 1) {
        if ((flags & bit) != 0) {
            items[j].text = search_option_items[i].text;
            items[j].id = search_option_items[i].id;
            items[j].type = search_option_items[i].type;
            j++;
        }
    }

    items[j].text = NULL;
    items[j].id = -1;
    search_context = rule_context_new ();
    cal_search->priv->view_flag = flags;

    rule_context_add_part_set (search_context, "partset", filter_part_get_type (),
            rule_context_add_part, rule_context_next_part);
    rule_context_add_rule_set (search_context, "ruleset", filter_rule_get_type (),
            rule_context_add_rule, rule_context_next_rule);

    if (flags == CAL_SEARCH_MEMOS_DEFAULT) {
        userfile = g_build_filename (g_get_home_dir (), ".evolution", "memos", "searches.xml", NULL);
        xmlfile = g_build_filename (SEARCH_RULE_DIR, "memotypes.xml", NULL);
    } else if (flags == CAL_SEARCH_TASKS_DEFAULT) {
        userfile = g_build_filename (g_get_home_dir (), ".evolution", "tasks", "searches.xml", NULL);
        xmlfile = g_build_filename (SEARCH_RULE_DIR, "tasktypes.xml", NULL);
    } else {
        userfile = g_build_filename (g_get_home_dir (), ".evolution", "calendar", "searches.xml", NULL);
        xmlfile = g_build_filename (SEARCH_RULE_DIR, "caltypes.xml", NULL);
    }

    g_object_set_data_full (G_OBJECT (search_context), "user", userfile, g_free);
    g_object_set_data_full (G_OBJECT (search_context), "system", xmlfile, g_free);

    rule_context_load (search_context, xmlfile, userfile);
    search_rule = filter_rule_new ();
    part = rule_context_next_part (search_context, NULL);

    if (part == NULL)
        g_warning ("Could not load calendar search; no parts.");
    else
        filter_rule_add_part (search_rule, filter_part_clone (part));

    e_filter_bar_new_construct (search_context, xmlfile, userfile, NULL, cal_search,
            (EFilterBar*) cal_search );
    e_search_bar_set_menu ((ESearchBar *) cal_search, calendar_search_items);

    g_signal_connect ((ESearchBar *) cal_search, "menu_activated", G_CALLBACK (search_menu_activated), cal_search);

    make_suboptions (cal_search);

    cal_search->priv->search_rule = search_rule;
    cal_search->priv->search_context = search_context;

    g_free (xmlfile);
    g_free (userfile);

    return cal_search;
}

/**
 * cal_search_bar_new:
 * flags: bitfield of items to appear in the search menu
 *
 * creates a new calendar search bar.
 *
 * return value: a newly-created calendar search bar.  you should connect to the
 * "sexp_changed" signal to monitor changes in the generated sexps.
 **/
GtkWidget *
cal_search_bar_new (guint32 flags)
{
    CalSearchBar *cal_search;

    cal_search = g_object_new (TYPE_CAL_SEARCH_BAR, NULL);
    return GTK_WIDGET (cal_search_bar_construct (cal_search, flags));
}

/* Used from qsort() */
static int
compare_categories_cb (const void *a, const void *b)
{
    const char **ca, **cb;

    ca = (const char **) a;
    cb = (const char **) b;

    /* FIXME: should use some utf8 strcoll() thingy */
    return strcmp (*ca, *cb);
}

/* Creates a sorted array of categories based on the original one; copies the
 * string values.
 */
static GPtrArray *
sort_categories (GPtrArray *categories)
{
    GPtrArray *c;
    int i;

    c = g_ptr_array_new ();
    g_ptr_array_set_size (c, categories->len);

    for (i = 0; i < categories->len; i++)
        c->pdata[i] = g_strdup (categories->pdata[i]);

    qsort (c->pdata, c->len, sizeof (gpointer), compare_categories_cb);

    return c;
}

/**
 * cal_search_bar_set_categories:
 * @cal_search: A calendar search bar.
 * @categories: Array of pointers to strings for the category names.
 *
 * Sets the list of categories that are to be shown in the drop-down list
 * of a calendar search bar.  The search bar will automatically add an item
 * for "unfiled" components, that is, those that have no categories assigned
 * to them.
 **/
void
cal_search_bar_set_categories (CalSearchBar *cal_search, GPtrArray *categories)
{
    CalSearchBarPrivate *priv;

    g_return_if_fail (IS_CAL_SEARCH_BAR (cal_search));
    g_return_if_fail (categories != NULL);

    priv = cal_search->priv;

    g_return_if_fail (priv->categories != NULL);
    free_categories (priv->categories);

    priv->categories = sort_categories (categories);
    make_suboptions (cal_search);
}

/**
 * cal_search_bar_get_category:
 * @cal_search: A calendar search bar.
 *
 * Queries the currently selected category name in a calendar search bar.
 * If "All" or "Unfiled" are selected, this function will return NULL.
 *
 * Return value: Name of the selected category, or NULL if there is no
 * selected category.
 **/
const char *
cal_search_bar_get_category (CalSearchBar *cal_search)
{
    const char *category;

    category = get_current_category (cal_search);

    return category;
}

void
cal_search_bar_get_time_range (CalSearchBar *cal_search, time_t *start, time_t *end)
{
    CalSearchBarPrivate *priv;

    g_return_if_fail (IS_CAL_SEARCH_BAR (cal_search));

    priv = cal_search->priv;

    *start = priv->start;
    *end = priv->end;
}