aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/backend/pas/pas-backend.c
blob: a447d64851022ee4c8d3f6579c9ebe04bfc82df5 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Author:
 *   Nat Friedman (nat@ximian.com)
 *
 * Copyright 2000, Ximian, Inc.
 */

#include <config.h>
#include <pthread.h>
#include "pas-book-view.h"
#include "pas-backend.h"
#include "pas-marshal.h"

struct _PASBackendPrivate {
    GMutex *open_mutex;

    GMutex *clients_mutex;
    GList *clients;

    char *uri;
    gboolean loaded, writable, removed;

    GMutex *views_mutex;
    EList *views;
};

/* Signal IDs */
enum {
    LAST_CLIENT_GONE,
    LAST_SIGNAL
};

static guint pas_backend_signals[LAST_SIGNAL];

static GObjectClass *parent_class;

gboolean
pas_backend_construct (PASBackend *backend)
{
    return TRUE;
}

GNOME_Evolution_Addressbook_CallStatus
pas_backend_load_uri (PASBackend             *backend,
              const char             *uri,
              gboolean                only_if_exists)
{
    GNOME_Evolution_Addressbook_CallStatus status;

    g_return_val_if_fail (backend && PAS_IS_BACKEND (backend), FALSE);
    g_return_val_if_fail (uri, FALSE);
    g_return_val_if_fail (backend->priv->loaded == FALSE, FALSE);

    g_assert (PAS_BACKEND_GET_CLASS (backend)->load_uri);

    status = (* PAS_BACKEND_GET_CLASS (backend)->load_uri) (backend, uri, only_if_exists);

    if (status == GNOME_Evolution_Addressbook_Success)
        backend->priv->uri = g_strdup (uri);

    return status;
}

/**
 * pas_backend_get_uri:
 * @backend: An addressbook backend.
 * 
 * Queries the URI that an addressbook backend is serving.
 * 
 * Return value: URI for the backend.
 **/
const char *
pas_backend_get_uri (PASBackend *backend)
{
    g_return_val_if_fail (backend && PAS_IS_BACKEND (backend), NULL);

    return backend->priv->uri;
}

void
pas_backend_open (PASBackend *backend,
          PASBook    *book,
          gboolean    only_if_exists)
{
    g_return_if_fail (backend && PAS_IS_BACKEND (backend));
    g_return_if_fail (book && PAS_IS_BOOK (book));

    g_mutex_lock (backend->priv->open_mutex);

    if (backend->priv->loaded) {
        pas_book_respond_open (
            book, GNOME_Evolution_Addressbook_Success);

        pas_book_report_writable (book, backend->priv->writable);
    } else {
        GNOME_Evolution_Addressbook_CallStatus status =
            pas_backend_load_uri (backend, pas_book_get_uri (book), only_if_exists);

        pas_book_respond_open (book, status);

        if (status == GNOME_Evolution_Addressbook_Success)
            pas_book_report_writable (book, backend->priv->writable);
    }

    g_mutex_unlock (backend->priv->open_mutex);
}

void
pas_backend_remove (PASBackend *backend,
            PASBook    *book)
{
    g_return_if_fail (backend && PAS_IS_BACKEND (backend));
    g_return_if_fail (book && PAS_IS_BOOK (book));

    g_assert (PAS_BACKEND_GET_CLASS (backend)->remove);

    (* PAS_BACKEND_GET_CLASS (backend)->remove) (backend, book);
}

void
pas_backend_create_contact (PASBackend *backend,
                PASBook    *book,
                const char *vcard)
{
    g_return_if_fail (backend && PAS_IS_BACKEND (backend));
    g_return_if_fail (book && PAS_IS_BOOK (book));
    g_return_if_fail (vcard);

    g_assert (PAS_BACKEND_GET_CLASS (backend)->create_contact);

    (* PAS_BACKEND_GET_CLASS (backend)->create_contact) (backend, book, vcard);
}

void
pas_backend_remove_contacts (PASBackend *backend,
                 PASBook *book,
                 GList *id_list)
{
    g_return_if_fail (backend && PAS_IS_BACKEND (backend));
    g_return_if_fail (book && PAS_IS_BOOK (book));
    g_return_if_fail (id_list);

    g_assert (PAS_BACKEND_GET_CLASS (backend)->remove_contacts);

    (* PAS_BACKEND_GET_CLASS (backend)->remove_contacts) (backend, book, id_list);
}

void
pas_backend_modify_contact (PASBackend *backend,
                PASBook *book,
                const char *vcard)
{
    g_return_if_fail (backend && PAS_IS_BACKEND (backend));
    g_return_if_fail (book && PAS_IS_BOOK (book));
    g_return_if_fail (vcard);

    g_assert (PAS_BACKEND_GET_CLASS (backend)->modify_contact);

    (* PAS_BACKEND_GET_CLASS (backend)->modify_contact) (backend, book, vcard);
}

void
pas_backend_get_contact (PASBackend *backend,
             PASBook *book,
             const char *id)
{
    g_return_if_fail (backend && PAS_IS_BACKEND (backend));
    g_return_if_fail (book && PAS_IS_BOOK (book));
    g_return_if_fail (id);

    g_assert (PAS_BACKEND_GET_CLASS (backend)->get_contact);

    (* PAS_BACKEND_GET_CLASS (backend)->get_contact) (backend, book, id);
}

void
pas_backend_get_contact_list (PASBackend *backend,
                  PASBook *book,
                  const char *query)
{
    g_return_if_fail (backend && PAS_IS_BACKEND (backend));
    g_return_if_fail (book && PAS_IS_BOOK (book));
    g_return_if_fail (query);

    g_assert (PAS_BACKEND_GET_CLASS (backend)->get_contact_list);

    (* PAS_BACKEND_GET_CLASS (backend)->get_contact_list) (backend, book, query);
}

void
pas_backend_start_book_view (PASBackend *backend,
                 PASBookView *book_view)
{
    g_return_if_fail (backend && PAS_IS_BACKEND (backend));
    g_return_if_fail (book_view && PAS_IS_BOOK_VIEW (book_view));

    g_assert (PAS_BACKEND_GET_CLASS (backend)->start_book_view);

    (* PAS_BACKEND_GET_CLASS (backend)->start_book_view) (backend, book_view);
}

void
pas_backend_stop_book_view (PASBackend             *backend,
                PASBookView            *book_view)
{
    g_return_if_fail (backend && PAS_IS_BACKEND (backend));
    g_return_if_fail (book_view && PAS_IS_BOOK_VIEW (book_view));

    g_assert (PAS_BACKEND_GET_CLASS (backend)->stop_book_view);

    (* PAS_BACKEND_GET_CLASS (backend)->stop_book_view) (backend, book_view);
}

void
pas_backend_get_changes (PASBackend *backend,
             PASBook *book,
             const char *change_id)
{
    g_return_if_fail (backend && PAS_IS_BACKEND (backend));
    g_return_if_fail (book && PAS_IS_BOOK (book));
    g_return_if_fail (change_id);

    g_assert (PAS_BACKEND_GET_CLASS (backend)->get_changes);

    (* PAS_BACKEND_GET_CLASS (backend)->get_changes) (backend, book, change_id);
}

void
pas_backend_authenticate_user (PASBackend *backend,
                   PASBook *book,
                   const char *user,
                   const char *passwd,
                   const char *auth_method)
{
    g_return_if_fail (backend && PAS_IS_BACKEND (backend));
    g_return_if_fail (book && PAS_IS_BOOK (book));
    g_return_if_fail (user && passwd && auth_method);

    g_assert (PAS_BACKEND_GET_CLASS (backend)->authenticate_user);

    (* PAS_BACKEND_GET_CLASS (backend)->authenticate_user) (backend, book, user, passwd, auth_method);
}

void
pas_backend_get_supported_fields (PASBackend *backend,
                  PASBook *book)

{
    g_return_if_fail (backend && PAS_IS_BACKEND (backend));
    g_return_if_fail (book && PAS_IS_BOOK (book));

    g_assert (PAS_BACKEND_GET_CLASS (backend)->get_supported_fields);

    (* PAS_BACKEND_GET_CLASS (backend)->get_supported_fields) (backend, book);
}

void
pas_backend_get_supported_auth_methods (PASBackend *backend,
                    PASBook *book)
{
    g_return_if_fail (backend && PAS_IS_BACKEND (backend));
    g_return_if_fail (book && PAS_IS_BOOK (book));

    g_assert (PAS_BACKEND_GET_CLASS (backend)->get_supported_auth_methods);

    (* PAS_BACKEND_GET_CLASS (backend)->get_supported_auth_methods) (backend, book);
}

GNOME_Evolution_Addressbook_CallStatus
pas_backend_cancel_operation (PASBackend *backend,
                  PASBook *book)
{
    g_return_val_if_fail (backend && PAS_IS_BACKEND (backend), GNOME_Evolution_Addressbook_OtherError);
    g_return_val_if_fail (book && PAS_IS_BOOK (book), GNOME_Evolution_Addressbook_OtherError);

    g_assert (PAS_BACKEND_GET_CLASS (backend)->cancel_operation);

    return (* PAS_BACKEND_GET_CLASS (backend)->cancel_operation) (backend, book);
}

static void
book_destroy_cb (gpointer data, GObject *where_book_was)
{
    PASBackend *backend = PAS_BACKEND (data);

    pas_backend_remove_client (backend, (PASBook *)where_book_was);
}

static void
listener_died_cb (gpointer cnx, gpointer user_data)
{
    PASBook *book = PAS_BOOK (user_data);

    pas_backend_remove_client (pas_book_get_backend (book), book);
}

static void
last_client_gone (PASBackend *backend)
{
    g_signal_emit (backend, pas_backend_signals[LAST_CLIENT_GONE], 0);
}

EList*
pas_backend_get_book_views (PASBackend *backend)
{
    g_return_val_if_fail (backend && PAS_IS_BACKEND (backend), FALSE);

    return g_object_ref (backend->priv->views);
}

void
pas_backend_add_book_view (PASBackend *backend,
               PASBookView *view)
{
    g_mutex_lock (backend->priv->views_mutex);

    e_list_append (backend->priv->views, view);

    g_mutex_unlock (backend->priv->views_mutex);
}

void
pas_backend_remove_book_view (PASBackend *backend,
                  PASBookView *view)
{
    g_mutex_lock (backend->priv->views_mutex);

    e_list_remove (backend->priv->views, view);

    g_mutex_unlock (backend->priv->views_mutex);
}

/**
 * pas_backend_add_client:
 * @backend: An addressbook backend.
 * @book: the corba object representing the client connection.
 *
 * Adds a client to an addressbook backend.
 *
 * Return value: TRUE on success, FALSE on failure to add the client.
 */
gboolean
pas_backend_add_client (PASBackend      *backend,
            PASBook         *book)
{
    g_return_val_if_fail (backend && PAS_IS_BACKEND (backend), FALSE);
    g_return_val_if_fail (book && PAS_IS_BOOK (book), FALSE);

    bonobo_object_set_immortal (BONOBO_OBJECT (book), TRUE);

    g_object_weak_ref (G_OBJECT (book), book_destroy_cb, backend);

    ORBit_small_listen_for_broken (pas_book_get_listener (book), G_CALLBACK (listener_died_cb), book);

    g_mutex_lock (backend->priv->clients_mutex);
    backend->priv->clients = g_list_prepend (backend->priv->clients, book);
    g_mutex_unlock (backend->priv->clients_mutex);

    return TRUE;
}

void
pas_backend_remove_client (PASBackend *backend,
               PASBook    *book)
{
    /* XXX this needs a bit more thinking wrt the mutex - we
       should be holding it when we check to see if clients is
       NULL */

    g_return_if_fail (backend && PAS_IS_BACKEND (backend));
    g_return_if_fail (book && PAS_IS_BOOK (book));

    /* Disconnect */
    g_mutex_lock (backend->priv->clients_mutex);
    backend->priv->clients = g_list_remove (backend->priv->clients, book);
    g_mutex_unlock (backend->priv->clients_mutex);

    /* When all clients go away, notify the parent factory about it so that
     * it may decide whether to kill the backend or not.
     */
    if (!backend->priv->clients)
        last_client_gone (backend);
}

char *
pas_backend_get_static_capabilities (PASBackend *backend)
{
    g_return_val_if_fail (backend && PAS_IS_BACKEND (backend), NULL);
    
    g_assert (PAS_BACKEND_GET_CLASS (backend)->get_static_capabilities);

    return PAS_BACKEND_GET_CLASS (backend)->get_static_capabilities (backend);
}

gboolean
pas_backend_is_loaded (PASBackend *backend)
{
    g_return_val_if_fail (backend && PAS_IS_BACKEND (backend), FALSE);

    return backend->priv->loaded;
}

void
pas_backend_set_is_loaded (PASBackend *backend, gboolean is_loaded)
{
    g_return_if_fail (backend && PAS_IS_BACKEND (backend));

    backend->priv->loaded = is_loaded;
}

gboolean
pas_backend_is_writable (PASBackend *backend)
{
    g_return_val_if_fail (backend && PAS_IS_BACKEND (backend), FALSE);
    
    return backend->priv->writable;
}

void
pas_backend_set_is_writable (PASBackend *backend, gboolean is_writable)
{
    g_return_if_fail (backend && PAS_IS_BACKEND (backend));
    
    backend->priv->writable = is_writable;
}

gboolean
pas_backend_is_removed (PASBackend *backend)
{
    g_return_val_if_fail (backend && PAS_IS_BACKEND (backend), FALSE);
    
    return backend->priv->removed;
}

void
pas_backend_set_is_removed (PASBackend *backend, gboolean is_removed)
{
    g_return_if_fail (backend && PAS_IS_BACKEND (backend));
    
    backend->priv->removed = is_removed;
}



GNOME_Evolution_Addressbook_BookChangeItem*
pas_backend_change_add_new     (const char *vcard)
{
    GNOME_Evolution_Addressbook_BookChangeItem* new_change = GNOME_Evolution_Addressbook_BookChangeItem__alloc();

    new_change->changeType= GNOME_Evolution_Addressbook_ContactAdded;
    new_change->vcard = CORBA_string_dup (vcard);

    return new_change;
}

GNOME_Evolution_Addressbook_BookChangeItem*
pas_backend_change_modify_new  (const char *vcard)
{
    GNOME_Evolution_Addressbook_BookChangeItem* new_change = GNOME_Evolution_Addressbook_BookChangeItem__alloc();

    new_change->changeType= GNOME_Evolution_Addressbook_ContactModified;
    new_change->vcard = CORBA_string_dup (vcard);

    return new_change;
}

GNOME_Evolution_Addressbook_BookChangeItem*
pas_backend_change_delete_new  (const char *vcard)
{
    GNOME_Evolution_Addressbook_BookChangeItem* new_change = GNOME_Evolution_Addressbook_BookChangeItem__alloc();

    new_change->changeType= GNOME_Evolution_Addressbook_ContactDeleted;
    new_change->vcard = CORBA_string_dup (vcard);

    return new_change;
}



static void
pas_backend_foreach_view (PASBackend *backend,
              void (*callback) (PASBookView *, gpointer),
              gpointer user_data)
{
    EList *views;
    PASBookView *view;
    EIterator *iter;

    views = pas_backend_get_book_views (backend);
    iter = e_list_get_iterator (views);

    while (e_iterator_is_valid (iter)) {
        view = (PASBookView*)e_iterator_get (iter);

        bonobo_object_ref (view);
        callback (view, user_data);
        bonobo_object_unref (view);

        e_iterator_next (iter);
    }

    g_object_unref (iter);
    g_object_unref (views);
}


static void
view_notify_update (PASBookView *view, gpointer contact)
{
    pas_book_view_notify_update (view, contact);
}

/**
 * pas_backend_notify_update:
 * @backend: an addressbook backend
 * @contact: a new or modified contact
 *
 * Notifies all of @backend's book views about the new or modified
 * contacts @contact.
 *
 * pas_book_respond_create() and pas_book_respond_modify() call this
 * function for you. You only need to call this from your backend if
 * contacts are created or modified by another (non-PAS-using) client.
 **/
void
pas_backend_notify_update (PASBackend *backend, EContact *contact)
{
    pas_backend_foreach_view (backend, view_notify_update, contact);
}


static void
view_notify_remove (PASBookView *view, gpointer id)
{
    pas_book_view_notify_remove (view, id);
}

/**
 * pas_backend_notify_remove:
 * @backend: an addressbook backend
 * @id: a contact id
 *
 * Notifies all of @backend's book views that the contact with UID
 * @id has been removed.
 *
 * pas_book_respond_remove_contacts() calls this function for you. You
 * only need to call this from your backend if contacts are removed by
 * another (non-PAS-using) client.
 **/
void
pas_backend_notify_remove (PASBackend *backend, const char *id)
{
    pas_backend_foreach_view (backend, view_notify_remove, (gpointer)id);
}


static void
view_notify_complete (PASBookView *view, gpointer unused)
{
    pas_book_view_notify_complete (view, GNOME_Evolution_Addressbook_Success);
}

/**
 * pas_backend_notify_complete:
 * @backend: an addressbook backend
 *
 * Notifies all of @backend's book views that the current set of
 * notifications is complete; use this after a series of
 * pas_backend_notify_update() and pas_backend_notify_remove() calls.
 **/
void
pas_backend_notify_complete (PASBackend *backend)
{
    pas_backend_foreach_view (backend, view_notify_complete, NULL);
}



static void
pas_backend_init (PASBackend *backend)
{
    PASBackendPrivate *priv;

    priv          = g_new0 (PASBackendPrivate, 1);
    priv->uri     = NULL;
    priv->clients = NULL;
    priv->views   = e_list_new((EListCopyFunc) g_object_ref, (EListFreeFunc) g_object_unref, NULL);
    priv->open_mutex = g_mutex_new ();
    priv->clients_mutex = g_mutex_new ();
    priv->views_mutex = g_mutex_new ();

    backend->priv = priv;
}

static void
pas_backend_dispose (GObject *object)
{
    PASBackend *backend;

    backend = PAS_BACKEND (object);

    if (backend->priv) {
        g_list_free (backend->priv->clients);

        if (backend->priv->uri)
            g_free (backend->priv->uri);

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

        g_mutex_free (backend->priv->open_mutex);
        g_mutex_free (backend->priv->clients_mutex);
        g_mutex_free (backend->priv->views_mutex);

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

    G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
pas_backend_class_init (PASBackendClass *klass)
{
    GObjectClass *object_class;

    parent_class = g_type_class_peek_parent (klass);

    object_class = (GObjectClass *) klass;

    object_class->dispose = pas_backend_dispose;

    pas_backend_signals[LAST_CLIENT_GONE] =
        g_signal_new ("last_client_gone",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (PASBackendClass, last_client_gone),
                  NULL, NULL,
                  pas_marshal_NONE__NONE,
                  G_TYPE_NONE, 0);
}

/**
 * pas_backend_get_type:
 */
GType
pas_backend_get_type (void)
{
    static GType type = 0;

    if (! type) {
        GTypeInfo info = {
            sizeof (PASBackendClass),
            NULL, /* base_class_init */
            NULL, /* base_class_finalize */
            (GClassInitFunc)  pas_backend_class_init,
            NULL, /* class_finalize */
            NULL, /* class_data */
            sizeof (PASBackend),
            0,    /* n_preallocs */
            (GInstanceInitFunc) pas_backend_init
        };

        type = g_type_register_static (G_TYPE_OBJECT, "PASBackend", &info, 0);
    }

    return type;
}