aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/e-memos.c
blob: 5e162f8a287397b42e85d853e286912010287b95 (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
/*
 * 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/>  
 *
 *
 * Authors:
 *      Federico Mena Quintero <federico@ximian.com>
 *      Damon Chaplin <damon@ximian.com>
 *      Rodrigo Moya <rodrigo@ximian.com>
 *      Nathan Owens <pianocomp81@yahoo.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

#include <string.h>
#include <glib/gi18n.h>
#include <libedataserver/e-time-utils.h>
#include <table/e-table-scrolled.h>
#include <widgets/menus/gal-view-instance.h>
#include <widgets/menus/gal-view-factory-etable.h>
#include <widgets/menus/gal-view-etable.h>

#include "e-util/e-error.h"
#include "e-util/e-categories-config.h"
#include "e-util/e-util-private.h"
#include <libecal/e-cal-time-util.h>
#include <libedataserver/e-url.h>
#include <libedataserver/e-categories.h>
#include "widgets/menus/gal-view-menus.h"
#include "dialogs/delete-error.h"
#include "calendar-config.h"
#include "cal-search-bar.h"
#include "comp-util.h"
#include "e-memo-table-config.h"
#include "misc.h"
#include "e-cal-component-memo-preview.h"
#include "e-memos.h"
#include "common/authentication.h"


/* Private part of the GnomeCalendar structure */
struct _EMemosPrivate {
    /* The memo lists for display */
    GHashTable *clients;
    GList *clients_list;
    ECal *default_client;

    ECalView *query;

    /* The EMemoTable showing the memos. */
    GtkWidget   *memos_view;
    EMemoTableConfig *memos_view_config;

    /* Calendar search bar for memos */
    GtkWidget *search_bar;

    /* The preview */
    GtkWidget *preview;

    gchar *current_uid;
    char *sexp;

    /* View instance and the view menus handler */
    GalViewInstance *view_instance;
    GalViewMenus *view_menus;

    GList *notifications;
};

static void setup_widgets (EMemos *memos);
static void e_memos_destroy (GtkObject *object);
static void update_view (EMemos *memos);

static void categories_changed_cb (gpointer object, gpointer user_data);

/* Signal IDs */
enum {
    SELECTION_CHANGED,
    SOURCE_ADDED,
    SOURCE_REMOVED,
    LAST_SIGNAL
};

static guint e_memos_signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE (EMemos, e_memos, GTK_TYPE_TABLE)

/* Callback used when the sexp in the search bar changes */
static void
search_bar_sexp_changed_cb (CalSearchBar *cal_search, const char *sexp, gpointer data)
{
    EMemos *memos;
    EMemosPrivate *priv;

    memos = E_MEMOS (data);
    priv = memos->priv;

    if (priv->sexp)
        g_free (priv->sexp);

    priv->sexp = g_strdup (sexp);

    update_view (memos);
}

/* Callback used when the selected category in the search bar changes */
static void
search_bar_category_changed_cb (CalSearchBar *cal_search, const char *category, gpointer data)
{
    EMemos *memos;
    EMemosPrivate *priv;
    ECalModel *model;

    memos = E_MEMOS (data);
    priv = memos->priv;

    model = e_memo_table_get_model (E_MEMO_TABLE (priv->memos_view));
    e_cal_model_set_default_category (model, category);
}

static void
set_timezone (EMemos *memos)
{
    EMemosPrivate *priv;
    icaltimezone *zone;
    GList *l;

    priv = memos->priv;

    zone = calendar_config_get_icaltimezone ();
    for (l = priv->clients_list; l != NULL; l = l->next) {
        ECal *client = l->data;

        if (e_cal_get_load_state (client) == E_CAL_LOAD_LOADED)
            /* FIXME Error checking */
            e_cal_set_default_timezone (client, zone, NULL);
    }

    if (priv->default_client && e_cal_get_load_state (priv->default_client) == E_CAL_LOAD_LOADED)
        /* FIXME Error checking */
        e_cal_set_default_timezone (priv->default_client, zone, NULL);

    if (priv->preview)
        e_cal_component_memo_preview_set_default_timezone (E_CAL_COMPONENT_MEMO_PREVIEW (priv->preview), zone);
}

static void
timezone_changed_cb (GConfClient *client, guint id, GConfEntry *entry, gpointer data)
{
    EMemos *memos = data;

    set_timezone (memos);
}

static void
update_view (EMemos *memos)
{
    EMemosPrivate *priv;
    ECalModel *model;

    priv = memos->priv;

    model = e_memo_table_get_model (E_MEMO_TABLE (priv->memos_view));

    e_cal_model_set_search_query (model, priv->sexp);

    e_cal_component_memo_preview_clear (E_CAL_COMPONENT_MEMO_PREVIEW (priv->preview));
}

static void
setup_config (EMemos *memos)
{
    EMemosPrivate *priv;
    guint not;

    priv = memos->priv;

    /* Timezone */
    set_timezone (memos);

    not = calendar_config_add_notification_timezone (timezone_changed_cb, memos);
    priv->notifications = g_list_prepend (priv->notifications, GUINT_TO_POINTER (not));
}

struct AffectedComponents {
    EMemoTable *memo_table;
    GSList *components; /* contains pointers to ECalModelComponent */
};

/**
 * get_selected_components_cb
 * Helper function to fill list of selected components in EMemoTable.
 * This function is called from e_table_selected_row_foreach.
 **/
static void
get_selected_components_cb (int model_row, gpointer data)
{
    struct AffectedComponents *ac = (struct AffectedComponents *) data;

    if (!ac || !ac->memo_table)
        return;

    ac->components = g_slist_prepend (ac->components, e_cal_model_get_component_at (E_CAL_MODEL (e_memo_table_get_model (ac->memo_table)), model_row));
}

/**
 * do_for_selected_components
 * Calls function func for all selected components in memo_table.
 *
 * @param memo_table Table with selected components of our interest.
 * @param func Function to be called on each selected component from cal_table.
 *        The first parameter of this function is a pointer to ECalModelComponent and
 *        the second parameter of this function is pointer to cal_table
 * @param user_data User data, will be passed to func.
 **/
static void
do_for_selected_components (EMemoTable *memo_table, GFunc func, gpointer user_data)
{
    ETable *etable;
    struct AffectedComponents ac;

    g_return_if_fail (memo_table != NULL);

    ac.memo_table = memo_table;
    ac.components = NULL;

    etable = e_table_scrolled_get_table (E_TABLE_SCROLLED (memo_table->etable));
    e_table_selected_row_foreach (etable, get_selected_components_cb, &ac);

    g_slist_foreach (ac.components, func, user_data);
    g_slist_free (ac.components);
}

/**
 * obtain_list_of_components
 * As a callback function to convert each ECalModelComponent to string
 * of format "source_uid\ncomponent_str" and add this newly allocated
 * string to the list of components. Strings should be freed with g_free.
 *
 * @param data ECalModelComponent object.
 * @param user_data Pointer to GSList list, where to put new strings.
 **/
static void
obtain_list_of_components (gpointer data, gpointer user_data)
{
    GSList **list;
    ECalModelComponent *comp_data;

    list = (GSList **) user_data;
    comp_data = (ECalModelComponent *) data;

    if (list && comp_data) {
        char *comp_str;
        icalcomponent *vcal;

        vcal = e_cal_util_new_top_level ();
        e_cal_util_add_timezones_from_component (vcal, comp_data->icalcomp);
        icalcomponent_add_component (vcal, icalcomponent_new_clone (comp_data->icalcomp));

        comp_str = icalcomponent_as_ical_string (vcal);
        if (comp_str) {
            ESource *source = e_cal_get_source (comp_data->client);
            const char *source_uid = e_source_peek_uid (source);

            *list = g_slist_prepend (*list, g_strdup_printf ("%s\n%s", source_uid, comp_str));

            /* do not free this pointer, it owns libical */
            /* g_free (comp_str); */
        }

        icalcomponent_free (vcal);
        g_free (comp_str);
    }
}

static void
table_drag_data_get (ETable             *table,
             int                 row,
             int                 col,
             GdkDragContext     *context,
             GtkSelectionData   *selection_data,
             guint               info,
             guint               time,
             EMemos             *memos)
{
    EMemosPrivate *priv;

    priv = memos->priv;

    if (info == TARGET_VCALENDAR) {
        /* we will pass an icalcalendar component for both types */
        GSList *components = NULL;

        do_for_selected_components (E_MEMO_TABLE (priv->memos_view), obtain_list_of_components, &components);

        if (components) {
            cal_comp_selection_set_string_list (selection_data, components);

            g_slist_foreach (components, (GFunc)g_free, NULL);
            g_slist_free (components);
        }
    }
}

static void
setup_widgets (EMemos *memos)
{
    EMemosPrivate *priv;
    ETable *etable;
    ECalModel *model;

    priv = memos->priv;

    priv->search_bar = cal_search_bar_new (CAL_SEARCH_MEMOS_DEFAULT);
    g_signal_connect (priv->search_bar, "sexp_changed",
              G_CALLBACK (search_bar_sexp_changed_cb), memos);
    g_signal_connect (priv->search_bar, "category_changed",
              G_CALLBACK (search_bar_category_changed_cb), memos);

    categories_changed_cb (NULL, memos);

    gtk_table_attach (GTK_TABLE (memos), priv->search_bar, 0, 1, 0, 1,
              GTK_EXPAND | GTK_FILL | GTK_SHRINK, 0, 0, 0);
    gtk_widget_show (priv->search_bar);

    /* create the memo list */
    priv->memos_view = e_memo_table_new ();
    priv->memos_view_config = e_memo_table_config_new (E_MEMO_TABLE (priv->memos_view));

    g_signal_connect (etable, "table_drag_data_get",
              G_CALLBACK(table_drag_data_get), memos);
}

/* Class initialization function for the gnome calendar */
static void
e_memos_class_init (EMemosClass *klass)
{
    GtkObjectClass *object_class;

    object_class = (GtkObjectClass *) klass;

    e_memos_signals[SELECTION_CHANGED] =
        g_signal_new ("selection_changed",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (EMemosClass, selection_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__INT,
                  G_TYPE_NONE, 1,
                  G_TYPE_INT);

    e_memos_signals[SOURCE_ADDED] =
        g_signal_new ("source_added",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (EMemosClass, source_added),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
                  G_TYPE_NONE,
                  1,
                  G_TYPE_OBJECT);

    e_memos_signals[SOURCE_REMOVED] =
        g_signal_new ("source_removed",
                  G_TYPE_FROM_CLASS (object_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (EMemosClass, source_removed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
                  G_TYPE_NONE,
                  1,
                  G_TYPE_OBJECT);

    object_class->destroy = e_memos_destroy;

    klass->selection_changed = NULL;
    klass->source_added = NULL;
    klass->source_removed = NULL;
}


static void
categories_changed_cb (gpointer object, gpointer user_data)
{
    GList *cat_list;
    GPtrArray *cat_array;
    EMemosPrivate *priv;
    EMemos *memos = user_data;

    priv = memos->priv;

    cat_array = g_ptr_array_new ();
    cat_list = e_categories_get_list ();
    while (cat_list != NULL) {
        if (e_categories_is_searchable ((const char *) cat_list->data))
            g_ptr_array_add (cat_array, cat_list->data);
        cat_list = g_list_remove (cat_list, cat_list->data);
    }

    cal_search_bar_set_categories (CAL_SEARCH_BAR(priv->search_bar), cat_array);

    g_ptr_array_free (cat_array, TRUE);
}

/* Object initialization function for the gnome calendar */
static void
e_memos_init (EMemos *memos)
{
    EMemosPrivate *priv;

    priv = g_new0 (EMemosPrivate, 1);
    memos->priv = priv;

    setup_config (memos);
    setup_widgets (memos);

    e_categories_register_change_listener (G_CALLBACK (categories_changed_cb), memos);

    priv->clients = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
    priv->query = NULL;
    priv->view_instance = NULL;
    priv->view_menus = NULL;
    priv->current_uid = NULL;
    priv->sexp = g_strdup ("#t");
    priv->default_client = NULL;
    update_view (memos);
}

GtkWidget *
e_memos_new (void)
{
    EMemos *memos;

    memos = g_object_new (e_memos_get_type (), NULL);

    return GTK_WIDGET (memos);
}


static void
e_memos_destroy (GtkObject *object)
{
    EMemos *memos;
    EMemosPrivate *priv;

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

    memos = E_MEMOS (object);
    priv = memos->priv;

    if (priv) {
        GList *l;

        e_categories_unregister_change_listener (G_CALLBACK (categories_changed_cb), memos);

        /* disconnect from signals on all the clients */
        for (l = priv->clients_list; l != NULL; l = l->next) {
            g_signal_handlers_disconnect_matched (l->data, G_SIGNAL_MATCH_DATA,
                                  0, 0, NULL, NULL, memos);
        }

        g_hash_table_destroy (priv->clients);
        g_list_free (priv->clients_list);

        if (priv->current_uid) {
            g_free (priv->current_uid);
            priv->current_uid = NULL;
        }

        if (priv->sexp) {
            g_free (priv->sexp);
            priv->sexp = NULL;
        }

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

        for (l = priv->notifications; l; l = l->next)
            calendar_config_remove_notification (GPOINTER_TO_UINT (l->data));
        priv->notifications = NULL;

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

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

static void
set_status_message (EMemos *memos, const char *message, ...)
{
    EMemosPrivate *priv;
    va_list args;
    char sz[2048], *msg_string = NULL;

    if (message) {
        va_start (args, message);
        vsnprintf (sz, sizeof sz, message, args);
        va_end (args);
        msg_string = sz;
    }

    priv = memos->priv;

    e_memo_table_set_status_message (E_MEMO_TABLE (priv->memos_view), msg_string);
}

/* Callback from the calendar client when the calendar is opened */
static void
client_cal_opened_cb (ECal *ecal, ECalendarStatus status, EMemos *memos)
{
    ECalModel *model;
    ESource *source;
    EMemosPrivate *priv;

    priv = memos->priv;

    source = e_cal_get_source (ecal);

    switch (status) {
    case E_CALENDAR_STATUS_OK :
        g_signal_handlers_disconnect_matched (ecal, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, client_cal_opened_cb, NULL);

        set_status_message (memos, _("Loading memos"));
        model = e_memo_table_get_model (E_MEMO_TABLE (priv->memos_view));
        e_cal_model_add_client (model, ecal);

        set_timezone (memos);
        set_status_message (memos, NULL);
        break;
    case E_CALENDAR_STATUS_BUSY :
        break;
    case E_CALENDAR_STATUS_REPOSITORY_OFFLINE:
        e_error_run (GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (memos))), "calendar:prompt-no-contents-offline-memos", NULL);
        break;
    default :
        /* Make sure the source doesn't disappear on us */
        g_object_ref (source);

        priv->clients_list = g_list_remove (priv->clients_list, ecal);
        g_signal_handlers_disconnect_matched (ecal, G_SIGNAL_MATCH_DATA,
                              0, 0, NULL, NULL, memos);

        /* Do this last because it unrefs the client */
        g_hash_table_remove (priv->clients,  e_source_peek_uid (source));

        g_signal_emit (memos, e_memos_signals[SOURCE_REMOVED], 0, source);

        set_status_message (memos, NULL);
        g_object_unref (source);

        break;
    }
}

static void
default_client_cal_opened_cb (ECal *ecal, ECalendarStatus status, EMemos *memos)
{
    ECalModel *model;
    ESource *source;
    EMemosPrivate *priv;

    priv = memos->priv;

    source = e_cal_get_source (ecal);

    switch (status) {
    case E_CALENDAR_STATUS_OK :
        g_signal_handlers_disconnect_matched (ecal, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, default_client_cal_opened_cb, NULL);
        model = e_memo_table_get_model (E_MEMO_TABLE (priv->memos_view));

        set_timezone (memos);
        e_cal_model_set_default_client (model, ecal);
        set_status_message (memos, NULL);
        break;
    case E_CALENDAR_STATUS_BUSY:
        break;
    default :
        /* Make sure the source doesn't disappear on us */
        g_object_ref (source);

        priv->clients_list = g_list_remove (priv->clients_list, ecal);
        g_signal_handlers_disconnect_matched (ecal, G_SIGNAL_MATCH_DATA,
                              0, 0, NULL, NULL, memos);

        /* Do this last because it unrefs the client */
        g_hash_table_remove (priv->clients,  e_source_peek_uid (source));

        g_signal_emit (memos, e_memos_signals[SOURCE_REMOVED], 0, source);

        set_status_message (memos, NULL);
        g_object_unref (priv->default_client);
        priv->default_client = NULL;
        g_object_unref (source);

        break;
    }
}

typedef void (*open_func) (ECal *, ECalendarStatus, EMemos *);

static gboolean
open_ecal (EMemos *memos, ECal *cal, gboolean only_if_exists, open_func of)
{
    set_status_message (memos, _("Opening memos at %s"), e_cal_get_uri (cal));

    g_signal_connect (G_OBJECT (cal), "cal_opened", G_CALLBACK (of), memos);
    e_cal_open_async (cal, only_if_exists);

    return TRUE;
}

gboolean
e_memos_add_memo_source (EMemos *memos, ESource *source)
{
    EMemosPrivate *priv;
    ECal *client;
    const char *uid;

    g_return_val_if_fail (memos != NULL, FALSE);
    g_return_val_if_fail (E_IS_MEMOS (memos), FALSE);
    g_return_val_if_fail (E_IS_SOURCE (source), FALSE);

    priv = memos->priv;

    uid = e_source_peek_uid (source);
    client = g_hash_table_lookup (priv->clients, uid);
    if (client) {
        /* We already have it */

        return TRUE;
    } else {
        ESource *default_source;

        if (priv->default_client) {
            default_source = e_cal_get_source (priv->default_client);

            /* We don't have it but the default client is it */
            if (!strcmp (e_source_peek_uid (default_source), uid))
                client = g_object_ref (priv->default_client);
        }

        /* Create a new one */
        if (!client) {
            client = auth_new_cal_from_source (source, E_CAL_SOURCE_TYPE_JOURNAL);
            if (!client)
                return FALSE;
        }
    }

    g_signal_connect (G_OBJECT (client), "backend_error", G_CALLBACK (backend_error_cb), memos);
    g_signal_connect (G_OBJECT (client), "backend_died", G_CALLBACK (backend_died_cb), memos);

    /* add the client to internal structure */
    g_hash_table_insert (priv->clients, g_strdup (uid) , client);
    priv->clients_list = g_list_prepend (priv->clients_list, client);

    g_signal_emit (memos, e_memos_signals[SOURCE_ADDED], 0, source);

    open_ecal (memos, client, FALSE, client_cal_opened_cb);

    return TRUE;
}

gboolean
e_memos_remove_memo_source (EMemos *memos, ESource *source)
{
    EMemosPrivate *priv;
    ECal *client;
    ECalModel *model;
    const char *uid;

    g_return_val_if_fail (memos != NULL, FALSE);
    g_return_val_if_fail (E_IS_MEMOS (memos), FALSE);
    g_return_val_if_fail (E_IS_SOURCE (source), FALSE);

    priv = memos->priv;

    uid = e_source_peek_uid (source);
    client = g_hash_table_lookup (priv->clients, uid);
    if (!client)
        return TRUE;


    priv->clients_list = g_list_remove (priv->clients_list, client);
    g_signal_handlers_disconnect_matched (client, G_SIGNAL_MATCH_DATA,
                          0, 0, NULL, NULL, memos);

    model = e_memo_table_get_model (E_MEMO_TABLE (priv->memos_view));
    e_cal_model_remove_client (model, client);

    g_hash_table_remove (priv->clients, uid);


    g_signal_emit (memos, e_memos_signals[SOURCE_REMOVED], 0, source);

    return TRUE;
}

gboolean
e_memos_set_default_source (EMemos *memos, ESource *source)
{
    EMemosPrivate *priv;
    ECal *ecal;

    g_return_val_if_fail (memos != NULL, FALSE);
    g_return_val_if_fail (E_IS_MEMOS (memos), FALSE);
    g_return_val_if_fail (E_IS_SOURCE (source), FALSE);

    priv = memos->priv;

    ecal = g_hash_table_lookup (priv->clients, e_source_peek_uid (source));

    if (priv->default_client)
        g_object_unref (priv->default_client);

    if (ecal) {
        priv->default_client = g_object_ref (ecal);
    } else {
        priv->default_client = auth_new_cal_from_source (source, E_CAL_SOURCE_TYPE_JOURNAL);
        if (!priv->default_client)
            return FALSE;
    }

    open_ecal (memos, priv->default_client, FALSE, default_client_cal_opened_cb);

    return TRUE;
}

ECal *
e_memos_get_default_client (EMemos *memos)
{
    EMemosPrivate *priv;

    g_return_val_if_fail (memos != NULL, NULL);
    g_return_val_if_fail (E_IS_MEMOS (memos), NULL);

    priv = memos->priv;

    return e_cal_model_get_default_client (e_memo_table_get_model (E_MEMO_TABLE (priv->memos_view)));
}