aboutsummaryrefslogtreecommitdiffstats
path: root/shell/e-corba-storage-registry.c
blob: 07db240d70d158ac4f161e03626e63a3fb004574 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-corba-storage-registry.c
 *
 * Copyright (C) 2000  Ximian, Inc.
 *
 * 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ettore Perazzoli
 */

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

#include "e-local-storage.h"
#include "e-corba-storage.h"
#include "e-corba-storage-registry.h"
#include "e-shell-constants.h"

#include "e-util/e-corba-utils.h"

#include <bonobo/bonobo-exception.h>
#include <gal/util/e-util.h>

#include <string.h>


#define PARENT_TYPE BONOBO_X_OBJECT_TYPE
static BonoboXObjectClass *parent_class = NULL;

struct _ECorbaStorageRegistryPrivate {
    EStorageSet *storage_set;

    GSList *listeners;
};


/* CORBA interface implementation.  */

static void
listener_notify (Bonobo_Listener listener,
         GNOME_Evolution_StorageRegistry_MessageType type,
         const char *name)
{
    CORBA_any any;
    GNOME_Evolution_StorageRegistry_NotifyResult nr;
    CORBA_Environment ev;
    
    nr.type = type;
    nr.name = CORBA_string_dup (name ? name : "");

    any._type = (CORBA_TypeCode) TC_GNOME_Evolution_StorageRegistry_NotifyResult;
    any._value = &nr;

    CORBA_exception_init (&ev);
    Bonobo_Listener_event (listener,
                   "Evolution::StorageRegistry::NotifyResult",
                   &any, &ev);
    if (BONOBO_EX (&ev)) {
        g_warning ("Could not send notify event for %s\n%s", name,
               CORBA_exception_id (&ev));
    }

    CORBA_exception_free (&ev);
}

static GNOME_Evolution_StorageListener
impl_StorageRegistry_addStorage (PortableServer_Servant servant,
                 const GNOME_Evolution_Storage storage_interface,
                 const CORBA_char *name,
                 CORBA_Environment *ev)
{
    BonoboObject *bonobo_object;
    ECorbaStorageRegistry *storage_registry;
    ECorbaStorageRegistryPrivate *priv;
    EStorage *storage;
    GNOME_Evolution_StorageListener listener_interface;
    GSList *iter;
    
    bonobo_object = bonobo_object_from_servant (servant);
    storage_registry = E_CORBA_STORAGE_REGISTRY (bonobo_object);
    priv = storage_registry->priv;

    storage = e_corba_storage_new (storage_interface, name);

    if (! e_storage_set_add_storage (priv->storage_set, storage)) {
        CORBA_exception_set (ev,
                     CORBA_USER_EXCEPTION,
                     ex_GNOME_Evolution_StorageRegistry_Exists,
                     NULL);
        return CORBA_OBJECT_NIL;
    }

    g_object_unref (storage);


       /* FIXME: if we remove a listener while looping through the list we can
        * crash. Yay CORBA reentrancy. */
    for (iter = priv->listeners; iter; iter = iter->next) {
        listener_notify (iter->data,
                 GNOME_Evolution_StorageRegistry_STORAGE_CREATED,
                 name);
    }
    
    listener_interface = CORBA_Object_duplicate (e_corba_storage_get_StorageListener
                             (E_CORBA_STORAGE (storage)), ev);

    return listener_interface;
}

static GNOME_Evolution_StorageRegistry_StorageList *
impl_StorageRegistry_getStorageList (PortableServer_Servant servant,
                     CORBA_Environment *ev)
{
    BonoboObject *bonobo_object;
    ECorbaStorageRegistry *storage_registry;
    ECorbaStorageRegistryPrivate *priv;
    GNOME_Evolution_StorageRegistry_StorageList *storage_list;
    GList *sl, *l;
    
    bonobo_object = bonobo_object_from_servant (servant);
    storage_registry = E_CORBA_STORAGE_REGISTRY (bonobo_object);
    priv = storage_registry->priv;

    sl = e_storage_set_get_storage_list (priv->storage_set);
    
    storage_list = GNOME_Evolution_StorageRegistry_StorageList__alloc ();
    storage_list->_maximum = g_list_length (sl);
    storage_list->_length = 0;
    storage_list->_buffer = CORBA_sequence_GNOME_Evolution_Storage_allocbuf (storage_list->_maximum);
    for (l = sl; l != NULL; l = l->next) {
        EStorage *storage;
        GNOME_Evolution_Storage corba_storage;
        CORBA_Environment ev2;
        
        CORBA_exception_init (&ev2);
        
        storage = l->data;
        if (E_IS_LOCAL_STORAGE (storage)) {
            corba_storage = e_local_storage_get_corba_interface (E_LOCAL_STORAGE (storage));
        } else if (E_IS_CORBA_STORAGE (storage)) {
            corba_storage = e_corba_storage_get_corba_objref (E_CORBA_STORAGE (storage));
        } else {
            continue;           
        }
        
        corba_storage = CORBA_Object_duplicate (corba_storage, &ev2);
        if (BONOBO_EX (&ev2)) {
            CORBA_exception_free (&ev2);            
            continue;
        }       
        storage_list->_buffer[storage_list->_length] = corba_storage;
        storage_list->_length++;        
    }

    CORBA_sequence_set_release (storage_list, TRUE);

    return storage_list;    
}

static GNOME_Evolution_Storage
impl_StorageRegistry_getStorageByName (PortableServer_Servant servant,
                       const CORBA_char *name,
                       CORBA_Environment *ev)
{
    BonoboObject *bonobo_object;
    ECorbaStorageRegistry *storage_registry;
    ECorbaStorageRegistryPrivate *priv;
    GNOME_Evolution_Storage corba_storage;
    EStorage *storage;

    bonobo_object = bonobo_object_from_servant (servant);
    storage_registry = E_CORBA_STORAGE_REGISTRY (bonobo_object);
    priv = storage_registry->priv;

    storage = e_storage_set_get_storage (priv->storage_set, name);
    if (storage == NULL) {
        CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
                     ex_GNOME_Evolution_StorageRegistry_NotFound,
                     NULL);
        return CORBA_OBJECT_NIL;
    }

    corba_storage = CORBA_Object_duplicate (e_corba_storage_get_corba_objref
                        (E_CORBA_STORAGE (storage)), ev);

    return corba_storage;
}

static void
impl_StorageRegistry_removeStorageByName (PortableServer_Servant servant,
                      const CORBA_char *name,
                      CORBA_Environment *ev)
{
    BonoboObject *bonobo_object;
    ECorbaStorageRegistry *storage_registry;
    ECorbaStorageRegistryPrivate *priv;
    EStorage *storage;

    bonobo_object = bonobo_object_from_servant (servant);
    storage_registry = E_CORBA_STORAGE_REGISTRY (bonobo_object);
    priv = storage_registry->priv;

    storage = e_storage_set_get_storage (priv->storage_set, name);
    if (storage == NULL) {
        CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
                     ex_GNOME_Evolution_StorageRegistry_NotFound,
                     NULL);
        return;
    }

    /* FIXME: Yucky to get the storage by name and then remove it.  */
    /* FIXME: Check failure.  */
    e_storage_set_remove_storage (priv->storage_set, storage);
}

static void
storage_set_foreach (EStorageSet *set,
             Bonobo_Listener listener)
{
    GList *storage_list, *p;

    storage_list = e_storage_set_get_storage_list (set);
    for (p = storage_list; p; p = p->next) {
        const char *name;

        name = e_storage_get_name (E_STORAGE (p->data));

        listener_notify (listener, GNOME_Evolution_StorageRegistry_STORAGE_CREATED, name);
        g_object_unref (p->data);
    }
    
    g_list_free (storage_list);
}

static GSList *
find_listener (GSList *p,
           Bonobo_Listener listener)
{
    CORBA_Environment ev;

    CORBA_exception_init (&ev);
    for (; p; p = p->next) {
        if (CORBA_Object_is_equivalent (p->data, listener, &ev) == TRUE) {
            CORBA_exception_free (&ev);
            return p;
        }
    }

    CORBA_exception_free (&ev);
    return NULL;
}

static void
impl_StorageRegistry_addListener (PortableServer_Servant servant,
                  Bonobo_Listener listener,
                  CORBA_Environment *ev)
{
    BonoboObject *bonobo_object;
    ECorbaStorageRegistry *storage_registry;
    ECorbaStorageRegistryPrivate *priv;
    Bonobo_Listener listener_copy;
    CORBA_Environment ev2;
    
    bonobo_object = bonobo_object_from_servant (servant);
    storage_registry = E_CORBA_STORAGE_REGISTRY (bonobo_object);
    priv = storage_registry->priv;

    if (find_listener (priv->listeners, listener) != NULL) {
        CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
                     ex_GNOME_Evolution_StorageRegistry_AlreadyListening,
                     NULL);
        return;
    }

    CORBA_exception_init (&ev2);
    listener_copy = CORBA_Object_duplicate ((CORBA_Object) listener, &ev2);
    if (BONOBO_EX (&ev2)) {
        g_warning ("EvolutionStorageRegistry -- Cannot duplicate listener.");
        CORBA_exception_free (&ev2);
        return;
    }

    CORBA_exception_free (&ev2);
    priv->listeners = g_slist_prepend (priv->listeners, listener_copy);
    storage_set_foreach (priv->storage_set, listener_copy);
}

static void
impl_StorageRegistry_removeListener (PortableServer_Servant servant,
                     Bonobo_Listener listener,
                     CORBA_Environment *ev)
{
    BonoboObject *bonobo_object;
    ECorbaStorageRegistry *storage_registry;
    ECorbaStorageRegistryPrivate *priv;
    CORBA_Environment ev2;
    GSList *p;

    bonobo_object = bonobo_object_from_servant (servant);
    storage_registry = E_CORBA_STORAGE_REGISTRY (bonobo_object);
    priv = storage_registry->priv;

    p = find_listener (priv->listeners, listener);
    if (p == NULL) {
        CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
                     ex_GNOME_Evolution_StorageRegistry_NotFound,
                     NULL);
        return;
    }

    CORBA_exception_init (&ev2);
    CORBA_Object_release ((CORBA_Object) p->data, &ev2);
    CORBA_exception_free (&ev2);
    
    priv->listeners = g_slist_remove_link (priv->listeners, p);
    g_slist_free (p);
}

static GNOME_Evolution_Folder *
impl_StorageRegistry_getFolderByUri (PortableServer_Servant servant,
                     const CORBA_char *uri,
                     CORBA_Environment *ev)
{
    BonoboObject *bonobo_object;
    ECorbaStorageRegistry *storage_registry;
    GNOME_Evolution_Folder *corba_folder;
    EStorageSet *storage_set;
    EFolder *folder;
    char *path;
    CORBA_char *corba_evolution_uri;

    bonobo_object = bonobo_object_from_servant (servant);
    storage_registry = E_CORBA_STORAGE_REGISTRY (bonobo_object);
    storage_set = storage_registry->priv->storage_set;

    if (!strncmp (uri, E_SHELL_URI_PREFIX, E_SHELL_URI_PREFIX_LEN)) {
        corba_evolution_uri = CORBA_string_dup (uri);
        path = (char *)uri + E_SHELL_URI_PREFIX_LEN;
        folder = e_storage_set_get_folder (storage_set, path);
    } else {
        path = e_storage_set_get_path_for_physical_uri (storage_set, uri);
        if (path) {
            corba_evolution_uri = CORBA_string_alloc (strlen (path) + E_SHELL_URI_PREFIX_LEN + 1);
            strcpy (corba_evolution_uri, E_SHELL_URI_PREFIX);
            strcat (corba_evolution_uri, path);
            folder = e_storage_set_get_folder (storage_set, path);
            g_free (path);
        } else {
            corba_evolution_uri = NULL;
            folder = NULL;
        }
    }

    if (!folder) {
        CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
                     ex_GNOME_Evolution_StorageRegistry_NotFound,
                     NULL);
        return CORBA_OBJECT_NIL;
    }

    corba_folder = GNOME_Evolution_Folder__alloc ();

    corba_folder->displayName = CORBA_string_dup (e_folder_get_name (folder));

    corba_folder->description     = CORBA_string_dup (e_safe_corba_string (e_folder_get_description (folder)));
    corba_folder->type            = CORBA_string_dup (e_folder_get_type_string (folder));
    corba_folder->physicalUri     = CORBA_string_dup (e_safe_corba_string (e_folder_get_physical_uri (folder)));
    corba_folder->customIconName  = CORBA_string_dup (e_safe_corba_string (e_folder_get_custom_icon_name (folder)));
    corba_folder->evolutionUri    = corba_evolution_uri;
    corba_folder->unreadCount     = e_folder_get_unread_count (folder);
    corba_folder->sortingPriority = e_folder_get_sorting_priority (folder);

    return corba_folder;
}


/* GObject methods.  */

static void
impl_dispose (GObject *object)
{
    ECorbaStorageRegistry *corba_storage_registry;
    ECorbaStorageRegistryPrivate *priv;

    corba_storage_registry = E_CORBA_STORAGE_REGISTRY (object);
    priv = corba_storage_registry->priv;

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

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

static void
impl_finalize (GObject *object)
{
    ECorbaStorageRegistry *corba_storage_registry;
    ECorbaStorageRegistryPrivate *priv;

    corba_storage_registry = E_CORBA_STORAGE_REGISTRY (object);
    priv = corba_storage_registry->priv;

    g_free (priv);

    (* G_OBJECT_CLASS (parent_class)->finalize) (object);
}


/* Initialization.  */

static void
class_init (ECorbaStorageRegistryClass *klass)
{
    GObjectClass *object_class;
    POA_GNOME_Evolution_StorageRegistry__epv *epv;

    object_class = G_OBJECT_CLASS (klass);
    object_class->dispose  = impl_dispose;
    object_class->finalize = impl_finalize;

    epv = & klass->epv;
    epv->addStorage          = impl_StorageRegistry_addStorage;
    epv->getStorageList      = impl_StorageRegistry_getStorageList;
    epv->getStorageByName    = impl_StorageRegistry_getStorageByName;
    epv->removeStorageByName = impl_StorageRegistry_removeStorageByName;
    epv->addListener         = impl_StorageRegistry_addListener;
    epv->removeListener      = impl_StorageRegistry_removeListener;
    epv->getFolderByUri      = impl_StorageRegistry_getFolderByUri;

    parent_class = gtk_type_class (PARENT_TYPE);
}

static void
init (ECorbaStorageRegistry *corba_storage_registry)
{
    ECorbaStorageRegistryPrivate *priv;

    priv = g_new (ECorbaStorageRegistryPrivate, 1);
    priv->storage_set = NULL;
    priv->listeners = NULL;
    
    corba_storage_registry->priv = priv;
}


void
e_corba_storage_registry_construct (ECorbaStorageRegistry *corba_storage_registry,
                    EStorageSet *storage_set)
{
    ECorbaStorageRegistryPrivate *priv;

    g_return_if_fail (E_IS_CORBA_STORAGE_REGISTRY (corba_storage_registry));

    priv = corba_storage_registry->priv;

    g_object_ref (storage_set);
    priv->storage_set = storage_set;
}

ECorbaStorageRegistry *
e_corba_storage_registry_new (EStorageSet *storage_set)
{
    ECorbaStorageRegistry *corba_storage_registry;

    g_return_val_if_fail (storage_set != NULL, NULL);
    g_return_val_if_fail (E_IS_STORAGE_SET (storage_set), NULL);

    corba_storage_registry = g_object_new (e_corba_storage_registry_get_type (), NULL);

    e_corba_storage_registry_construct (corba_storage_registry, storage_set);

    return corba_storage_registry;
}


E_MAKE_X_TYPE (e_corba_storage_registry, "ECorbaStorageRegistry", ECorbaStorageRegistry,
           class_init, init, PARENT_TYPE,
           POA_GNOME_Evolution_StorageRegistry__init,
           GTK_STRUCT_OFFSET (ECorbaStorageRegistryClass, epv))