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

#include <config.h>
#include <ctype.h>
#include <gnome.h>
#include <liboaf/liboaf.h>

#include "addressbook.h"
#include "pas-book-factory.h"

#define PAS_BOOK_FACTORY_OAF_ID "OAFIID:GNOME_Evolution_Wombat_ServerFactory"

static BonoboObjectClass          *pas_book_factory_parent_class;
POA_GNOME_Evolution_Addressbook_BookFactory__vepv   pas_book_factory_vepv;

typedef struct {
    char                              *uri;
    GNOME_Evolution_Addressbook_BookListener             listener;
} PASBookFactoryQueuedRequest;

struct _PASBookFactoryPrivate {
    gint        idle_id;
    GHashTable *backends;
    GHashTable *active_server_map;
    GList      *queued_requests;
};

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

static guint factory_signals[LAST_SIGNAL];

static char *
pas_book_factory_canonicalize_uri (const char *uri)
{
    /* FIXME: What do I do here? */

    return g_strdup (uri);
}

static char *
pas_book_factory_extract_proto_from_uri (const char *uri)
{
    char *proto;
    char *p;

    p = strchr (uri, ':');

    if (p == NULL)
        return NULL;

    proto = g_malloc0 (p - uri + 1);

    strncpy (proto, uri, p - uri);

    return proto;
}

/**
 * pas_book_factory_register_backend:
 * @factory:
 * @proto:
 * @backend:
 */
void
pas_book_factory_register_backend (PASBookFactory      *factory,
                   const char          *proto,
                   PASBackendFactoryFn  backend)
{
    g_return_if_fail (factory != NULL);
    g_return_if_fail (PAS_IS_BOOK_FACTORY (factory));
    g_return_if_fail (proto != NULL);
    g_return_if_fail (backend != NULL);

    if (g_hash_table_lookup (factory->priv->backends, proto) != NULL) {
        g_warning ("pas_book_factory_register_backend: "
               "Proto \"%s\" already registered!\n", proto);
    }

    g_hash_table_insert (factory->priv->backends,
                 g_strdup (proto), backend);
}

/**
 * pas_book_factory_get_n_backends:
 * @factory: An addressbook factory.
 * 
 * Queries the number of running addressbook backends in an addressbook factory.
 * 
 * Return value: Number of running backends.
 **/
int
pas_book_factory_get_n_backends (PASBookFactory *factory)
{
    g_return_val_if_fail (factory != NULL, -1);
    g_return_val_if_fail (PAS_IS_BOOK_FACTORY (factory), -1);

    return g_hash_table_size (factory->priv->active_server_map);
}

/* Callback used when a backend loses its last connected client */
static void
backend_last_client_gone_cb (PASBackend *backend, gpointer data)
{
    PASBookFactory *factory;
    const char *uri;
    gpointer orig_key;
    gboolean result;
    char *orig_uri;

    factory = PAS_BOOK_FACTORY (data);

    /* Remove the backend from the active server map */

    uri = pas_backend_get_uri (backend);
    g_assert (uri != NULL);

    result = g_hash_table_lookup_extended (factory->priv->active_server_map, uri,
                           &orig_key, NULL);
    g_assert (result != FALSE);

    orig_uri = orig_key;

    g_hash_table_remove (factory->priv->active_server_map, orig_uri);
    g_free (orig_uri);

    gtk_object_unref (GTK_OBJECT (backend));

    /* Notify upstream if there are no more backends */

    if (g_hash_table_size (factory->priv->active_server_map) == 0)
        gtk_signal_emit (GTK_OBJECT (factory), factory_signals[LAST_BOOK_GONE]);
}

static PASBackendFactoryFn
pas_book_factory_lookup_backend_factory (PASBookFactory *factory,
                     const char     *uri)
{
    PASBackendFactoryFn  backend_fn;
    char                *proto;
    char                *canonical_uri;

    g_assert (factory != NULL);
    g_assert (PAS_IS_BOOK_FACTORY (factory));
    g_assert (uri != NULL);

    canonical_uri = pas_book_factory_canonicalize_uri (uri);
    if (canonical_uri == NULL)
        return NULL;

    proto = pas_book_factory_extract_proto_from_uri (canonical_uri);
    if (proto == NULL) {
        g_free (canonical_uri);
        return NULL;
    }

    backend_fn = g_hash_table_lookup (factory->priv->backends, proto);

    g_free (proto); 
    g_free (canonical_uri);

    return backend_fn;
}

static PASBackend *
pas_book_factory_launch_backend (PASBookFactory              *factory,
                 GNOME_Evolution_Addressbook_BookListener       listener,
                 const char                  *uri)
{
    PASBackendFactoryFn  backend_factory;
    PASBackend          *backend;

    backend_factory = pas_book_factory_lookup_backend_factory (
        factory, uri);

    if (!backend_factory) {
        CORBA_Environment ev;

        CORBA_exception_init (&ev);
        GNOME_Evolution_Addressbook_BookListener_notifyBookOpened (
            listener,
            GNOME_Evolution_Addressbook_BookListener_ProtocolNotSupported,
            CORBA_OBJECT_NIL,
            &ev);

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

        CORBA_exception_free (&ev);
        return NULL;
    }

    backend = (* backend_factory) ();
    if (!backend) {
        CORBA_Environment ev;

        CORBA_exception_init (&ev);
        GNOME_Evolution_Addressbook_BookListener_notifyBookOpened (
            listener,
            GNOME_Evolution_Addressbook_BookListener_OtherError,
            CORBA_OBJECT_NIL,
            &ev);

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

        CORBA_exception_free (&ev);
        return NULL;
    }

    g_hash_table_insert (factory->priv->active_server_map,
                 g_strdup (uri),
                 backend);

    gtk_signal_connect (GTK_OBJECT (backend), "last_client_gone",
                backend_last_client_gone_cb,
                factory);

    return backend;
}

static void
pas_book_factory_process_request (PASBookFactory              *factory,
                  PASBookFactoryQueuedRequest *request)
{
    PASBackend *backend;
    char *uri;
    GNOME_Evolution_Addressbook_BookListener listener;
    CORBA_Environment ev;

    uri = request->uri;
    listener = request->listener;
    g_free (request);

    /* Look up the backend and create one if needed */

    backend = g_hash_table_lookup (factory->priv->active_server_map, uri);

    if (!backend) {
        backend = pas_book_factory_launch_backend (factory, listener, uri);
        if (!backend)
            goto out;

        if (!pas_backend_add_client (backend, listener))
            goto out;

        pas_backend_load_uri (backend, uri);

        goto out;
    }

    pas_backend_add_client (backend, listener);

 out:
    g_free (uri);

    CORBA_exception_init (&ev);
    CORBA_Object_release (listener, &ev);

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

    CORBA_exception_free (&ev);
}

static gboolean
pas_book_factory_process_queue (PASBookFactory *factory)
{
    /* Process pending Book-creation requests. */
    if (factory->priv->queued_requests != NULL) {
        PASBookFactoryQueuedRequest  *request;
        GList *l;

        l = factory->priv->queued_requests;
        request = l->data;

        pas_book_factory_process_request (factory, request);

        factory->priv->queued_requests = g_list_remove_link (
            factory->priv->queued_requests, l);
        g_list_free_1 (l);
    }

    if (factory->priv->queued_requests == NULL) {

        factory->priv->idle_id = 0;
        return FALSE;
    }

    return TRUE;
}

static void
pas_book_factory_queue_request (PASBookFactory               *factory,
                const char                   *uri,
                const GNOME_Evolution_Addressbook_BookListener  listener)
{
    PASBookFactoryQueuedRequest *request;
    GNOME_Evolution_Addressbook_BookListener       listener_copy;
    CORBA_Environment            ev;

    CORBA_exception_init (&ev);

    listener_copy = CORBA_Object_duplicate (listener, &ev);

    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("PASBookFactory: Could not duplicate BookListener!\n");
        CORBA_exception_free (&ev);
        return;
    }

    CORBA_exception_free (&ev);

    request           = g_new0 (PASBookFactoryQueuedRequest, 1);
    request->listener = listener_copy;
    request->uri      = g_strdup (uri);

    factory->priv->queued_requests =
        g_list_prepend (factory->priv->queued_requests, request);

    if (! factory->priv->idle_id) {
        factory->priv->idle_id =
            g_idle_add ((GSourceFunc) pas_book_factory_process_queue, factory);
    }
}


static void
impl_GNOME_Evolution_Addressbook_BookFactory_openBook (PortableServer_Servant        servant,
                      const CORBA_char             *uri,
                      const GNOME_Evolution_Addressbook_BookListener  listener,
                      CORBA_Environment            *ev)
{
    PASBookFactory      *factory =
        PAS_BOOK_FACTORY (bonobo_object_from_servant (servant));
    PASBackendFactoryFn  backend_factory;

    backend_factory = pas_book_factory_lookup_backend_factory (factory, uri);

    if (backend_factory == NULL) {
        GNOME_Evolution_Addressbook_BookListener_notifyBookOpened (
            listener,
            GNOME_Evolution_Addressbook_BookListener_ProtocolNotSupported,
            CORBA_OBJECT_NIL,
            ev);

        return;
    }

    pas_book_factory_queue_request (factory, uri, listener);
}

static gboolean
pas_book_factory_construct (PASBookFactory *factory)
{
    POA_GNOME_Evolution_Addressbook_BookFactory  *servant;
    CORBA_Environment           ev;
    CORBA_Object                obj;

    g_assert (factory != NULL);
    g_assert (PAS_IS_BOOK_FACTORY (factory));

    servant = (POA_GNOME_Evolution_Addressbook_BookFactory *) g_new0 (BonoboObjectServant, 1);
    servant->vepv = &pas_book_factory_vepv;

    CORBA_exception_init (&ev);

    POA_GNOME_Evolution_Addressbook_BookFactory__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 (factory), servant);
    if (obj == CORBA_OBJECT_NIL) {
        g_free (servant);

        return FALSE;
    }

    bonobo_object_construct (BONOBO_OBJECT (factory), obj);

    return TRUE;
}

/**
 * pas_book_factory_new:
 */
PASBookFactory *
pas_book_factory_new (void)
{
    PASBookFactory *factory;

    factory = gtk_type_new (pas_book_factory_get_type ());

    if (! pas_book_factory_construct (factory)) {
        g_warning ("pas_book_factory_new: Could not construct PASBookFactory!\n");
        gtk_object_unref (GTK_OBJECT (factory));

        return NULL;
    }

    return factory;
}

static gboolean
register_factory (CORBA_Object obj)
{
    OAF_RegistrationResult result;

    puts ("about to register addressbook");

    result = oaf_active_server_register (PAS_BOOK_FACTORY_OAF_ID, obj);

    switch (result) {
    case OAF_REG_SUCCESS:
        return TRUE;
    case OAF_REG_NOT_LISTED:
        g_message ("Error registering the PAS factory: not listed");
        return FALSE;
    case OAF_REG_ALREADY_ACTIVE:
        g_message ("Error registering the PAS factory: already active");
        return FALSE;
    case OAF_REG_ERROR:
    default:
        g_message ("Error registering the PAS factory: generic error");
        return FALSE;
    }
}

/**
 * pas_book_factory_activate:
 */
void
pas_book_factory_activate (PASBookFactory *factory)
{
    g_return_if_fail (factory != NULL);
    g_return_if_fail (PAS_IS_BOOK_FACTORY (factory));

    register_factory (bonobo_object_corba_objref (BONOBO_OBJECT (factory)));
}

static void
pas_book_factory_init (PASBookFactory *factory)
{
    factory->priv = g_new0 (PASBookFactoryPrivate, 1);

    factory->priv->active_server_map = g_hash_table_new (g_str_hash, g_str_equal);
    factory->priv->backends          = g_hash_table_new (g_str_hash, g_str_equal);
    factory->priv->queued_requests   = NULL;
}

static void
free_active_server_map_entry (gpointer key, gpointer value, gpointer data)
{
    char *uri;
    PASBackend *backend;

    uri = key;
    g_free (uri);

    backend = PAS_BACKEND (value);
    gtk_object_unref (GTK_OBJECT (backend));
}

static void
remove_backends_entry (gpointer key, gpointer value, gpointer data)
{
    char *uri;

    uri = key;
    g_free (uri);
}

static void
pas_book_factory_destroy (GtkObject *object)
{
    PASBookFactory *factory = PAS_BOOK_FACTORY (object);
    GList          *l;

    for (l = factory->priv->queued_requests; l != NULL; l = l->next) {
        PASBookFactoryQueuedRequest *request = l->data;
        CORBA_Environment ev;

        g_free (request->uri);

        CORBA_exception_init (&ev);
        CORBA_Object_release (request->listener, &ev);
        CORBA_exception_free (&ev);

        g_free (request);
    }
    g_list_free (factory->priv->queued_requests);
    factory->priv->queued_requests = NULL;

    g_hash_table_foreach (factory->priv->active_server_map,
                  free_active_server_map_entry,
                  NULL);
    g_hash_table_destroy (factory->priv->active_server_map);
    factory->priv->active_server_map = NULL;

    g_hash_table_foreach (factory->priv->backends,
                  remove_backends_entry,
                  NULL);
    g_hash_table_destroy (factory->priv->backends);
    factory->priv->backends = NULL;
    
    g_free (factory->priv);

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

static POA_GNOME_Evolution_Addressbook_BookFactory__epv *
pas_book_factory_get_epv (void)
{
    POA_GNOME_Evolution_Addressbook_BookFactory__epv *epv;

    epv = g_new0 (POA_GNOME_Evolution_Addressbook_BookFactory__epv, 1);

    epv->openBook = impl_GNOME_Evolution_Addressbook_BookFactory_openBook;

    return epv;
    
}

static void
pas_book_factory_corba_class_init (void)
{
    pas_book_factory_vepv.Bonobo_Unknown_epv         = bonobo_object_get_epv ();
    pas_book_factory_vepv.GNOME_Evolution_Addressbook_BookFactory_epv = pas_book_factory_get_epv ();
}

static void
pas_book_factory_class_init (PASBookFactoryClass *klass)
{
    GtkObjectClass *object_class = (GtkObjectClass *) klass;

    pas_book_factory_parent_class = gtk_type_class (bonobo_object_get_type ());

    factory_signals[LAST_BOOK_GONE] =
        gtk_signal_new ("last_book_gone",
                GTK_RUN_FIRST,
                object_class->type,
                GTK_SIGNAL_OFFSET (PASBookFactoryClass, last_book_gone),
                gtk_marshal_NONE__NONE,
                GTK_TYPE_NONE, 0);

    gtk_object_class_add_signals (object_class, factory_signals, LAST_SIGNAL);

    object_class->destroy = pas_book_factory_destroy;

    pas_book_factory_corba_class_init ();
}

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

    if (! type) {
        GtkTypeInfo info = {
            "PASBookFactory",
            sizeof (PASBookFactory),
            sizeof (PASBookFactoryClass),
            (GtkClassInitFunc)  pas_book_factory_class_init,
            (GtkObjectInitFunc) pas_book_factory_init,
            NULL, /* reserved 1 */
            NULL, /* reserved 2 */
            (GtkClassInitFunc) NULL
        };

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

    return type;
}