aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/backend/pas/pas-book.c
blob: b1d2688b3fc68093eb9ffb821c38336a420d1f4b (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * pas-book.c
 *
 * Copyright 2000, Helix Code, Inc.
 */

#include <config.h>
#include <gtk/gtksignal.h>
#include "pas-book.h"

static BonoboObjectClass *pas_book_parent_class;
POA_Evolution_Book__vepv pas_book_vepv;

enum {
    REQUESTS_QUEUED,
    LAST_SIGNAL
};

static guint pas_book_signals [LAST_SIGNAL];

struct _PASBookPrivate {
    PASBackend             *backend;
    Evolution_BookListener  listener;
    PASBookGetVCardFn       get_vcard;
    PASBookCanWriteFn       can_write;
    PASBookCanWriteCardFn   can_write_card;

    GList                  *request_queue;
    gint                    idle_id;
};

static gboolean
pas_book_check_queue (PASBook *book)
{
    if (book->priv->request_queue != NULL) {
        gtk_signal_emit (GTK_OBJECT (book),
                 pas_book_signals [REQUESTS_QUEUED]);
    }

    if (book->priv->request_queue == NULL) {
        book->priv->idle_id = 0;
        return FALSE;
    }

    return TRUE;
}

static void
pas_book_queue_request (PASBook *book, PASRequest *req)
{
    book->priv->request_queue =
        g_list_append (book->priv->request_queue, req);

    if (book->priv->idle_id == 0) {
        book->priv->idle_id = g_idle_add ((GSourceFunc) pas_book_check_queue, book);
    }
}

static void
pas_book_queue_create_card (PASBook *book, const char *vcard)
{
    PASRequest *req;

    req        = g_new0 (PASRequest, 1);
    req->op    = CreateCard;
    req->vcard = g_strdup (vcard);

    pas_book_queue_request (book, req);
}

static void
pas_book_queue_remove_card (PASBook *book, const char *id)
{
    PASRequest *req;

    req     = g_new0 (PASRequest, 1);
    req->op = RemoveCard;
    req->id = g_strdup (id);

    pas_book_queue_request (book, req);
}

static void
pas_book_queue_modify_card (PASBook *book, const char *vcard)
{
    PASRequest *req;

    req        = g_new0 (PASRequest, 1);
    req->op    = ModifyCard;
    req->vcard = g_strdup (vcard);

    pas_book_queue_request (book, req);
}

static void
pas_book_queue_get_cursor (PASBook *book, const char *search)
{
    PASRequest *req;

    req         = g_new0 (PASRequest, 1);
    req->op     = GetCursor;
    req->search = g_strdup(search);

    pas_book_queue_request (book, req);
}

static void
pas_book_queue_get_book_view (PASBook *book, const Evolution_BookViewListener listener, const char *search)
{
    PASRequest *req;
    CORBA_Environment ev;

    req           = g_new0 (PASRequest, 1);
    req->op       = GetBookView;
    req->search   = g_strdup(search);
    
    CORBA_exception_init (&ev);

    req->listener = CORBA_Object_duplicate(listener, &ev);

    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("pas_book_queue_get_book_view: Exception "
               "duplicating BookViewListener!\n");
    }

    CORBA_exception_free (&ev);

    pas_book_queue_request (book, req);
}

static void
pas_book_queue_get_changes (PASBook *book, const Evolution_BookViewListener listener, const char *change_id)
{
    PASRequest *req;
    CORBA_Environment ev;

    req           = g_new0 (PASRequest, 1);
    req->op       = GetChanges;
    req->change_id= g_strdup(change_id);
    
    CORBA_exception_init (&ev);

    req->listener = CORBA_Object_duplicate(listener, &ev);

    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("pas_book_queue_get_changes: Exception "
               "duplicating BookViewListener!\n");
    }

    CORBA_exception_free (&ev);

    pas_book_queue_request (book, req);
}

static void
pas_book_queue_check_connection (PASBook *book)
{
    PASRequest *req;

    req        = g_new0 (PASRequest, 1);
    req->op    = CheckConnection;

    pas_book_queue_request (book, req);
}

static CORBA_char *
impl_Evolution_Book_get_vcard (PortableServer_Servant servant,
                   const Evolution_CardId id,
                   CORBA_Environment *ev)
{
    PASBook    *book = PAS_BOOK (bonobo_object_from_servant (servant));
    char       *vcard;
    CORBA_char *retval;

    vcard = (book->priv->get_vcard) (book, (const char *) id);
    retval = CORBA_string_dup (vcard);
    g_free (vcard);

    return retval;
}

static CORBA_boolean
impl_Evolution_Book_can_write (PortableServer_Servant servant,
                   CORBA_Environment *ev)
{
    PASBook    *book = PAS_BOOK (bonobo_object_from_servant (servant));
    CORBA_boolean retval;

    retval = (book->priv->can_write) (book);

    return retval;
}

static CORBA_boolean
impl_Evolution_Book_can_write_card (PortableServer_Servant servant,
                    const Evolution_CardId id,
                    CORBA_Environment *ev)
{
    PASBook    *book = PAS_BOOK (bonobo_object_from_servant (servant));
    CORBA_boolean retval;

    retval = (book->priv->can_write_card) (book, (const char *) id);

    return retval;
}

static void
impl_Evolution_Book_create_card (PortableServer_Servant servant,
                 const Evolution_VCard vcard,
                 CORBA_Environment *ev)
{
    PASBook *book = PAS_BOOK (bonobo_object_from_servant (servant));

    pas_book_queue_create_card (book, (const char *) vcard);
}

static void
impl_Evolution_Book_remove_card (PortableServer_Servant servant,
                 const Evolution_CardId id,
                 CORBA_Environment *ev)
{
    PASBook *book = PAS_BOOK (bonobo_object_from_servant (servant));

    pas_book_queue_remove_card (book, (const char *) id);
}

static void
impl_Evolution_Book_modify_card (PortableServer_Servant servant,
                 const Evolution_VCard vcard,
                 CORBA_Environment *ev)
{
    PASBook *book = PAS_BOOK (bonobo_object_from_servant (servant));

    pas_book_queue_modify_card (book, (const char *) vcard);
}

static void
impl_Evolution_Book_get_cursor (PortableServer_Servant servant,
                const CORBA_char *search,
                CORBA_Environment *ev)
{
    PASBook *book = PAS_BOOK (bonobo_object_from_servant (servant));

    pas_book_queue_get_cursor (book, search);
}

static void
impl_Evolution_Book_get_book_view (PortableServer_Servant servant,
                   const Evolution_BookViewListener listener,
                   const CORBA_char *search,
                   CORBA_Environment *ev)
{
    PASBook *book = PAS_BOOK (bonobo_object_from_servant (servant));

    pas_book_queue_get_book_view (book, listener, search);
}

static void
impl_Evolution_Book_get_changes (PortableServer_Servant servant,
                 const Evolution_BookViewListener listener,
                 const CORBA_char *change_id,
                 CORBA_Environment *ev)
{
    PASBook *book = PAS_BOOK (bonobo_object_from_servant (servant));

    pas_book_queue_get_changes (book, listener, change_id);
}

static void
impl_Evolution_Book_check_connection (PortableServer_Servant servant,
                      CORBA_Environment *ev)
{
    PASBook *book = PAS_BOOK (bonobo_object_from_servant (servant));

    pas_book_queue_check_connection (book);
}

static char *
impl_Evolution_Book_get_static_capabilities (PortableServer_Servant servant,
                         CORBA_Environment *ev)
{
    PASBook *book = PAS_BOOK (bonobo_object_from_servant (servant));
    char *temp;
    char *ret_val;

    temp = pas_backend_get_static_capabilities (book->priv->backend);
    ret_val = CORBA_string_dup(temp);
    g_free(temp);
    return ret_val;
}

/**
 * pas_book_get_backend:
 */
PASBackend *
pas_book_get_backend (PASBook *book)
{
    g_return_val_if_fail (book != NULL,       NULL);
    g_return_val_if_fail (PAS_IS_BOOK (book), NULL);

    return book->priv->backend;
}

/**
 * pas_book_get_listener:
 */
Evolution_BookListener
pas_book_get_listener (PASBook *book)
{
    g_return_val_if_fail (book != NULL,       CORBA_OBJECT_NIL);
    g_return_val_if_fail (PAS_IS_BOOK (book), CORBA_OBJECT_NIL);

    return book->priv->listener;
}

/**
 * pas_book_check_pending
 */
gint
pas_book_check_pending (PASBook *book)
{
    g_return_val_if_fail (book != NULL,       -1);
    g_return_val_if_fail (PAS_IS_BOOK (book), -1);

    return g_list_length (book->priv->request_queue);
}

/**
 * pas_book_pop_request:
 */
PASRequest *
pas_book_pop_request (PASBook *book)
{
    GList      *popped;
    PASRequest *req;

    g_return_val_if_fail (book != NULL,       NULL);
    g_return_val_if_fail (PAS_IS_BOOK (book), NULL);

    if (book->priv->request_queue == NULL)
        return NULL;

    req = book->priv->request_queue->data;

    popped = book->priv->request_queue;
    book->priv->request_queue =
        g_list_remove_link (book->priv->request_queue, popped);

    g_list_free_1 (popped);

    return req;
}

/**
 * pas_book_respond_open:
 */
void
pas_book_respond_open (PASBook                           *book,
               Evolution_BookListener_CallStatus  status)
{
    CORBA_Environment ev;

    CORBA_exception_init (&ev);

    if (status == Evolution_BookListener_Success) {
        Evolution_BookListener_respond_open_book (
            book->priv->listener, status,
            bonobo_object_corba_objref (BONOBO_OBJECT (book)),
            &ev);
    } else {
        Evolution_BookListener_respond_open_book (
            book->priv->listener, status,
            CORBA_OBJECT_NIL, &ev);
    }
    

    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("pas_book_respond_open: Exception "
               "responding to BookListener!\n");
    }

    CORBA_exception_free (&ev);
}

/**
 * pas_book_respond_create:
 */
void
pas_book_respond_create (PASBook                           *book,
             Evolution_BookListener_CallStatus  status,
             const char                        *id)
{
    CORBA_Environment ev;

    CORBA_exception_init (&ev);

    Evolution_BookListener_respond_create_card (
        book->priv->listener, status, (char *)id, &ev);

    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("pas_book_respond_create: Exception "
               "responding to BookListener!\n");
    }

    CORBA_exception_free (&ev);
}

/**
 * pas_book_respond_remove:
 */
void
pas_book_respond_remove (PASBook                           *book,
             Evolution_BookListener_CallStatus  status)
{
    CORBA_Environment ev;

    CORBA_exception_init (&ev);

    Evolution_BookListener_respond_remove_card (
        book->priv->listener, status, &ev);

    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("pas_book_respond_remove: Exception "
               "responding to BookListener!\n");
    }

    CORBA_exception_free (&ev);
}

/**
 * pas_book_respond_modify:
 */
void
pas_book_respond_modify (PASBook                           *book,
             Evolution_BookListener_CallStatus  status)
{
    CORBA_Environment ev;

    CORBA_exception_init (&ev);

    Evolution_BookListener_respond_modify_card (
        book->priv->listener, status, &ev);

    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("pas_book_respond_modify: Exception "
               "responding to BookListener!\n");
    }

    CORBA_exception_free (&ev);
}

/**
 * pas_book_respond_get_cursor:
 */
void
pas_book_respond_get_cursor (PASBook                           *book,
                 Evolution_BookListener_CallStatus  status,
                 PASCardCursor                     *cursor)
{
    CORBA_Environment ev;
    CORBA_Object      object;

    CORBA_exception_init (&ev);
    
    object = bonobo_object_corba_objref(BONOBO_OBJECT(cursor));

    Evolution_BookListener_respond_get_cursor (
        book->priv->listener, status, object, &ev);

    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("pas_book_respond_get_cursor: Exception "
               "responding to BookListener!\n");
    }

    CORBA_exception_free (&ev);
}

/**
 * pas_book_respond_get_book_view:
 */
void
pas_book_respond_get_book_view (PASBook                           *book,
                Evolution_BookListener_CallStatus  status,
                PASBookView                       *book_view)
{
    CORBA_Environment ev;
    CORBA_Object      object;

    CORBA_exception_init (&ev);
    
    object = bonobo_object_corba_objref(BONOBO_OBJECT(book_view));

    Evolution_BookListener_respond_get_view (
        book->priv->listener, status, object, &ev);

    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("pas_book_respond_get_book_view: Exception "
               "responding to BookListener!\n");
    }

    CORBA_exception_free (&ev);
}

/**
 * pas_book_respond_get_changes:
 */
void
pas_book_respond_get_changes (PASBook                           *book,
                  Evolution_BookListener_CallStatus  status,
                  PASBookView                       *book_view)
{
    CORBA_Environment ev;
    CORBA_Object      object;

    CORBA_exception_init (&ev);
    
    object = bonobo_object_corba_objref(BONOBO_OBJECT(book_view));

    Evolution_BookListener_respond_get_changes (
        book->priv->listener, status, object, &ev);

    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("pas_book_respond_get_changes: Exception "
               "responding to BookListener!\n");
    }

    CORBA_exception_free (&ev);
}

/**
 * pas_book_report_connection:
 */
void
pas_book_report_connection (PASBook  *book,
                gboolean  connected)
{
    CORBA_Environment ev;

    CORBA_exception_init (&ev);

    Evolution_BookListener_report_connection_status (
        book->priv->listener, (CORBA_boolean) connected, &ev);

    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("pas_book_report_connection: Exception "
               "responding to BookListener!\n");
    }

    CORBA_exception_free (&ev);
}

static gboolean
pas_book_construct (PASBook                *book,
            PASBackend             *backend,
            Evolution_BookListener  listener,
            PASBookGetVCardFn       get_vcard,
            PASBookCanWriteFn       can_write,
            PASBookCanWriteCardFn   can_write_card)
{
    POA_Evolution_Book *servant;
    CORBA_Environment   ev;
    CORBA_Object        obj;

    g_assert (book      != NULL);
    g_assert (PAS_IS_BOOK (book));
    g_assert (listener  != CORBA_OBJECT_NIL);
    g_assert (get_vcard != NULL);
    g_assert (can_write != NULL);
    g_assert (can_write_card != NULL);

    servant = (POA_Evolution_Book *) g_new0 (BonoboObjectServant, 1);
    servant->vepv = &pas_book_vepv;

    CORBA_exception_init (&ev);

    POA_Evolution_Book__init ((PortableServer_Servant) servant, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        g_free (servant);
        CORBA_exception_free (&ev);

        return FALSE;
    }

    CORBA_exception_free (&ev);

    obj = bonobo_object_activate_servant (BONOBO_OBJECT (book), servant);
    if (obj == CORBA_OBJECT_NIL) {
        g_free (servant);

        return FALSE;
    }

    bonobo_object_construct (BONOBO_OBJECT (book), obj);

    CORBA_exception_init (&ev);
    book->priv->listener = CORBA_Object_duplicate (listener, &ev);

    if (ev._major != CORBA_NO_EXCEPTION)
        g_message ("pas_book_construct(): could not duplicate the listener");

    CORBA_exception_free (&ev);

    book->priv->listener  = listener;
    book->priv->get_vcard = get_vcard;
    book->priv->can_write = can_write;
    book->priv->can_write_card = can_write_card;
    book->priv->backend   = backend;

    return TRUE;
}

/**
 * pas_book_new:
 */
PASBook *
pas_book_new (PASBackend             *backend,
          Evolution_BookListener  listener,
          PASBookGetVCardFn       get_vcard,
          PASBookCanWriteFn       can_write,
          PASBookCanWriteCardFn   can_write_card)
{
    PASBook *book;

    g_return_val_if_fail (listener  != CORBA_OBJECT_NIL, NULL);
    g_return_val_if_fail (get_vcard != NULL,             NULL);

    book = gtk_type_new (pas_book_get_type ());

    if (! pas_book_construct (book, backend, listener,
                  get_vcard, can_write, can_write_card)) {
        gtk_object_unref (GTK_OBJECT (book));

        return NULL;
    }

    return book;
}

static void
pas_book_destroy (GtkObject *object)
{
    PASBook *book = PAS_BOOK (object);
    GList   *l;
    CORBA_Environment ev;

    for (l = book->priv->request_queue; l != NULL; l = l->next) {
        PASRequest *req = l->data;

        g_free (req->id);
        g_free (req->vcard);
        g_free (req);
    }
    g_list_free (book->priv->request_queue);

    CORBA_exception_init (&ev);
    CORBA_Object_release (book->priv->listener, &ev);

    if (ev._major != CORBA_NO_EXCEPTION)
        g_message ("pas_book_construct(): could not release the listener");

    CORBA_exception_free (&ev);

    g_free (book->priv);

    GTK_OBJECT_CLASS (pas_book_parent_class)->destroy (object); 
}

static POA_Evolution_Book__epv *
pas_book_get_epv (void)
{
    POA_Evolution_Book__epv *epv;

    epv = g_new0 (POA_Evolution_Book__epv, 1);

    epv->get_vcard               = impl_Evolution_Book_get_vcard;
    epv->can_write               = impl_Evolution_Book_can_write;
    epv->can_write_card          = impl_Evolution_Book_can_write_card;
    epv->create_card             = impl_Evolution_Book_create_card;
    epv->remove_card             = impl_Evolution_Book_remove_card;
    epv->modify_card             = impl_Evolution_Book_modify_card;
    epv->check_connection        = impl_Evolution_Book_check_connection;
    epv->get_static_capabilities = impl_Evolution_Book_get_static_capabilities;
    epv->get_cursor              = impl_Evolution_Book_get_cursor;
    epv->get_book_view           = impl_Evolution_Book_get_book_view;
    epv->get_changes             = impl_Evolution_Book_get_changes;

    return epv;
    
}

static void
pas_book_corba_class_init (void)
{
    pas_book_vepv.Bonobo_Unknown_epv  = bonobo_object_get_epv ();
    pas_book_vepv.Evolution_Book_epv = pas_book_get_epv ();
}

static void
pas_book_class_init (PASBookClass *klass)
{
    GtkObjectClass *object_class = (GtkObjectClass *) klass;

    pas_book_parent_class = gtk_type_class (bonobo_object_get_type ());

    pas_book_signals [REQUESTS_QUEUED] =
        gtk_signal_new ("requests_queued",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (PASBookClass, requests_queued),
                gtk_marshal_NONE__NONE,
                GTK_TYPE_NONE, 0);

    gtk_object_class_add_signals (object_class, pas_book_signals, LAST_SIGNAL);

    object_class->destroy = pas_book_destroy;

    pas_book_corba_class_init ();
}

static void
pas_book_init (PASBook *book)
{
    book->priv                = g_new0 (PASBookPrivate, 1);
    book->priv->idle_id       = 0;
    book->priv->request_queue = NULL;
}

/**
 * pas_book_get_type:
 */
GtkType
pas_book_get_type (void)
{
    static GtkType type = 0;

    if (! type) {
        GtkTypeInfo info = {
            "PASBook",
            sizeof (PASBook),
            sizeof (PASBookClass),
            (GtkClassInitFunc)  pas_book_class_init,
            (GtkObjectInitFunc) pas_book_init,
            NULL, /* reserved 1 */
            NULL, /* reserved 2 */
            (GtkClassInitFunc) NULL
        };

        type = gtk_type_unique (bonobo_object_get_type (), &info);
    }

    return type;
}