aboutsummaryrefslogtreecommitdiffstats
path: root/shell/e-corba-storage-registry.c
blob: c93ac2caa5b7020c3b8a64b52bac12e808a631dc (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
/* -*- 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-corba-storage.h"
#include "e-corba-storage-registry.h"
#include "e-shell-constants.h"

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


#define PARENT_TYPE BONOBO_OBJECT_TYPE
static BonoboObjectClass *parent_class = NULL;

struct _ECorbaStorageRegistryPrivate {
    EStorageSet *storage_set;

    GSList *listeners;
};


/* CORBA interface implementation.  */

static POA_GNOME_Evolution_StorageRegistry__vepv storage_registry_vepv;

static POA_GNOME_Evolution_StorageRegistry *
create_servant (void)
{
    POA_GNOME_Evolution_StorageRegistry *servant;
    CORBA_Environment ev;

    servant = (POA_GNOME_Evolution_StorageRegistry *) g_new0 (BonoboObjectServant, 1);
    servant->vepv = &storage_registry_vepv;

    CORBA_exception_init (&ev);

    POA_GNOME_Evolution_StorageRegistry__init ((PortableServer_Servant) servant, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        g_free (servant);
        CORBA_exception_free (&ev);
        return NULL;
    }

    CORBA_exception_free (&ev);

    return servant;
}

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;

    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;
    }

    gtk_object_unref (GTK_OBJECT (storage));

    listener_interface = CORBA_Object_duplicate (e_corba_storage_get_StorageListener
                             (E_CORBA_STORAGE (storage)), ev);

    return listener_interface;
}

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);
        gtk_object_unref (GTK_OBJECT (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
            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));
    if (e_folder_get_description (folder))
        corba_folder->description = CORBA_string_dup (e_folder_get_description (folder));
    else
        corba_folder->description = CORBA_string_dup ("");
    corba_folder->type = CORBA_string_dup (e_folder_get_type_string (folder));
    if (e_folder_get_physical_uri (folder))
        corba_folder->physicalUri = CORBA_string_dup (e_folder_get_physical_uri (folder));
    else
        corba_folder->physicalUri = CORBA_string_dup ("");
    corba_folder->evolutionUri = corba_evolution_uri;
    corba_folder->unreadCount = e_folder_get_unread_count (folder);

    return corba_folder;
}


/* GtkObject methods.  */

static void
destroy (GtkObject *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)
        gtk_object_unref (GTK_OBJECT (priv->storage_set));
    g_free (priv);

    (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}


/* Initialization.  */

static void
corba_class_init (void)
{
    POA_GNOME_Evolution_StorageRegistry__vepv *vepv;
    POA_GNOME_Evolution_StorageRegistry__epv *epv;
    PortableServer_ServantBase__epv *base_epv;

    base_epv = g_new0 (PortableServer_ServantBase__epv, 1);
    base_epv->_private    = NULL;
    base_epv->finalize    = NULL;
    base_epv->default_POA = NULL;

    epv = g_new0 (POA_GNOME_Evolution_StorageRegistry__epv, 1);
    epv->addStorage          = impl_StorageRegistry_addStorage;
    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;

    vepv = &storage_registry_vepv;
    vepv->_base_epv                     = base_epv;
    vepv->Bonobo_Unknown_epv            = bonobo_object_get_epv ();
    vepv->GNOME_Evolution_StorageRegistry_epv = epv;
}

static void
class_init (ECorbaStorageRegistryClass *klass)
{
    GtkObjectClass *object_class;

    object_class = GTK_OBJECT_CLASS (klass);
    object_class->destroy = destroy;

    parent_class = gtk_type_class (PARENT_TYPE);

    corba_class_init ();
}

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,
                    GNOME_Evolution_StorageRegistry corba_object,
                    EStorageSet *storage_set)
{
    ECorbaStorageRegistryPrivate *priv;

    g_return_if_fail (corba_storage_registry != NULL);
    g_return_if_fail (E_IS_CORBA_STORAGE_REGISTRY (corba_storage_registry));
    g_return_if_fail (corba_object != CORBA_OBJECT_NIL);

    bonobo_object_construct (BONOBO_OBJECT (corba_storage_registry), corba_object);

    priv = corba_storage_registry->priv;

    gtk_object_ref (GTK_OBJECT (storage_set));
    priv->storage_set = storage_set;
}

ECorbaStorageRegistry *
e_corba_storage_registry_new (EStorageSet *storage_set)
{
    ECorbaStorageRegistry *corba_storage_registry;
    POA_GNOME_Evolution_StorageRegistry *servant;
    GNOME_Evolution_StorageRegistry corba_object;

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

    servant = create_servant ();
    if (servant == NULL)
        return NULL;

    corba_storage_registry = gtk_type_new (e_corba_storage_registry_get_type ());

    corba_object = bonobo_object_activate_servant (BONOBO_OBJECT (corba_storage_registry),
                               servant);

    e_corba_storage_registry_construct (corba_storage_registry, corba_object, storage_set);

    return corba_storage_registry;
}


E_MAKE_TYPE (e_corba_storage_registry, "ECorbaStorageRegistry", ECorbaStorageRegistry, class_init, init, PARENT_TYPE)