aboutsummaryrefslogtreecommitdiffstats
path: root/shell/importer/evolution-importer-client.c
blob: 1687679cc496b6f92d8ea1323532674c9981139b (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* evolution-importer-listener.c
 *
 * Copyright (C) 2000  Helix Code, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * 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: Iain Holmes  <iain@helixcode.com>
 * Based on evolution-shell-component-client.c by Ettore Perazzoli
 */

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

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

#include "GNOME_Evolution_Importer.h"
#include "evolution-importer-client.h"


#define PARENT_TYPE BONOBO_OBJECT_CLIENT_TYPE
static BonoboObjectClass *parent_class = NULL;

struct _EvolutionImporterClientPrivate {
    EvolutionImporterClientCallback callback;
    void *closure;

    GNOME_Evolution_ImporterListener listener_interface;
    PortableServer_Servant listener_servant;
};


static PortableServer_ServantBase__epv Listener_base_epv;
static POA_GNOME_Evolution_ImporterListener__epv Listener_epv;
static POA_GNOME_Evolution_ImporterListener__vepv Listener_vepv;
static gboolean Listener_vepv_initialized = FALSE;

struct _ImporterListenerServant {
    POA_GNOME_Evolution_ImporterListener servant;
    EvolutionImporterClient *component_client;
};
typedef struct _ImporterListenerServant ImporterListenerServant;



static void
dispatch_callback (EvolutionImporterClient *client,
           EvolutionImporterResult result,
           gboolean more_items)
{
    EvolutionImporterClientPrivate *priv;
    EvolutionImporterClientCallback callback;
    PortableServer_ObjectId *oid;
    void *closure;
    CORBA_Environment ev;

    priv = client->private;

    g_return_if_fail (priv->callback != NULL);
    g_return_if_fail (priv->listener_servant != NULL);

    CORBA_exception_init (&ev);

    oid = PortableServer_POA_servant_to_id (bonobo_poa (), priv->listener_servant, &ev);
    PortableServer_POA_deactivate_object (bonobo_poa (), oid, &ev);
    POA_GNOME_Evolution_ImporterListener__fini (priv->listener_servant, &ev);
    CORBA_free (oid);

    CORBA_Object_release (priv->listener_interface, &ev);
    CORBA_exception_free (&ev);

    priv->listener_servant = NULL;
    priv->listener_interface = CORBA_OBJECT_NIL;

    callback = priv->callback;
    closure = priv->closure;

    priv->callback = NULL;
    priv->closure = NULL;

    (* callback) (client, result, more_items, closure);
}

static EvolutionImporterClient *
component_client_from_ImporterListener_servant (PortableServer_Servant servant)
{
    ImporterListenerServant *listener_servant;

    listener_servant = (ImporterListenerServant *) servant;
    return listener_servant->component_client;
}

static EvolutionImporterResult
result_from_async_corba_result (GNOME_Evolution_ImporterListener_ImporterResult corba_result)
{
    switch (corba_result) {
    case GNOME_Evolution_ImporterListener_OK:
        return EVOLUTION_IMPORTER_OK;
    case GNOME_Evolution_ImporterListener_UNSUPPORTED_OPERATION:
        return EVOLUTION_IMPORTER_UNSUPPORTED_OPERATION;
    case GNOME_Evolution_ImporterListener_UNKNOWN_DATA:
        return EVOLUTION_IMPORTER_UNKNOWN_DATA;
    case GNOME_Evolution_ImporterListener_BAD_DATA:
        return EVOLUTION_IMPORTER_BAD_DATA;
    case GNOME_Evolution_ImporterListener_BAD_FILE:
        return EVOLUTION_IMPORTER_BAD_FILE;
    case GNOME_Evolution_ImporterListener_NOT_READY:
        return EVOLUTION_IMPORTER_NOT_READY;
    default:
        return EVOLUTION_IMPORTER_UNKNOWN_ERROR;
    }
}

static void
impl_ImporterListener_notifyResult (PortableServer_Servant servant,
                    const GNOME_Evolution_ImporterListener_ImporterResult result,
                    const CORBA_boolean more_items,
                    CORBA_Environment *ev)
{
    EvolutionImporterClient *client;

    client = component_client_from_ImporterListener_servant (servant);
    dispatch_callback (client, result_from_async_corba_result (result), more_items);
}

static void
ImporterListener_vepv_initialize (void)
{
    Listener_base_epv._private = NULL;
    Listener_base_epv.finalize = NULL;
    Listener_base_epv.default_POA = NULL;

    Listener_epv.notifyResult = impl_ImporterListener_notifyResult;
    
    Listener_vepv._base_epv = &Listener_base_epv;
    Listener_vepv.GNOME_Evolution_ImporterListener_epv = &Listener_epv;
    
    Listener_vepv_initialized = TRUE;
}

static PortableServer_Servant *
create_listener_servant (EvolutionImporterClient *client)
{
    ImporterListenerServant *servant;

    if (!Listener_vepv_initialized)
        ImporterListener_vepv_initialize ();

    servant = g_new0 (ImporterListenerServant, 1);
    servant->servant.vepv = &Listener_vepv;
    servant->component_client = client;

    return (PortableServer_Servant) servant;
}

static void
free_listener_servant (PortableServer_Servant servant)
{
    g_free (servant);
}

static void
create_listener_interface (EvolutionImporterClient *client)
{
    EvolutionImporterClientPrivate *priv;
    PortableServer_Servant listener_servant;
    GNOME_Evolution_ImporterListener corba_interface;
    CORBA_Environment ev;

    priv = client->private;

    listener_servant = create_listener_servant (client);
    CORBA_exception_init (&ev);

    POA_GNOME_Evolution_ImporterListener__init (listener_servant, &ev);

    if (ev._major != CORBA_NO_EXCEPTION) {
        free_listener_servant (listener_servant);
        return;
    }

    CORBA_free (PortableServer_POA_activate_object (bonobo_poa (), 
                            listener_servant, &ev));
    corba_interface = PortableServer_POA_servant_to_reference (bonobo_poa (),
                                   listener_servant, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        corba_interface = CORBA_OBJECT_NIL;
            free_listener_servant (listener_servant);
    }

    CORBA_exception_free (&ev);

    priv->listener_servant = listener_servant;
    priv->listener_interface = corba_interface;
}



static void
destroy (GtkObject *object)
{
    EvolutionImporterClient *client;
    EvolutionImporterClientPrivate *priv;

    client = EVOLUTION_IMPORTER_CLIENT (object);
    priv = client->private;

    if (priv->callback != NULL)
        dispatch_callback (client, EVOLUTION_IMPORTER_INTERRUPTED, FALSE);

    g_free (priv);

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

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

    object_class = GTK_OBJECT_CLASS (klass);
    parent_class = gtk_type_class (PARENT_TYPE);

    object_class->destroy = destroy;
}

static void
init (EvolutionImporterClient *client)
{
    EvolutionImporterClientPrivate *priv;

    priv = g_new (EvolutionImporterClientPrivate, 1);
    priv->listener_interface = CORBA_OBJECT_NIL;
    priv->listener_servant = NULL;
    priv->callback = NULL;
    priv->closure = NULL;

    client->private = priv;
}

void
evolution_importer_client_construct (EvolutionImporterClient *client,
                     CORBA_Object corba_object)
{
    g_return_if_fail (client != NULL);
    g_return_if_fail (EVOLUTION_IS_IMPORTER_CLIENT (client));
    g_return_if_fail (corba_object != CORBA_OBJECT_NIL);

    bonobo_object_client_construct (BONOBO_OBJECT_CLIENT (client), corba_object);
}

EvolutionImporterClient *
evolution_importer_client_new (const CORBA_Object objref)
{
    EvolutionImporterClient *client;

    g_return_val_if_fail (objref != CORBA_OBJECT_NIL, NULL);

    client = gtk_type_new (evolution_importer_client_get_type ());
    evolution_importer_client_construct (client, objref);

    return client;
}

/* API */
void
evolution_importer_client_process_item (EvolutionImporterClient *client,
                    EvolutionImporterClientCallback callback,
                    void *closure)
{
    EvolutionImporterClientPrivate *priv;
    GNOME_Evolution_Importer corba_importer;
    CORBA_Environment ev;

    g_return_if_fail (client != NULL);
    g_return_if_fail (EVOLUTION_IS_IMPORTER_CLIENT (client));
    g_return_if_fail (callback != NULL);

    priv = client->private;

    if (priv->callback != NULL) {
        (* callback) (client, EVOLUTION_IMPORTER_BUSY, FALSE, closure);
        return;
    }
    
    create_listener_interface (client);

    CORBA_exception_init (&ev);

    corba_importer = bonobo_object_corba_objref (BONOBO_OBJECT (client));
    priv->callback = callback;
    priv->closure = closure;

    GNOME_Evolution_Importer_processItem (corba_importer,
                          priv->listener_interface, &ev);
    CORBA_exception_free (&ev);
}

const char *
evolution_importer_client_get_error (EvolutionImporterClient *client)
{
    EvolutionImporterClientPrivate *priv;
    GNOME_Evolution_Importer corba_importer;
    CORBA_char *str;
    CORBA_Environment ev;

    g_return_val_if_fail (client != NULL, NULL);
    g_return_val_if_fail (EVOLUTION_IS_IMPORTER_CLIENT (client), NULL);

    priv = client->private;
    corba_importer = bonobo_object_corba_objref (BONOBO_OBJECT (client));

    CORBA_exception_init (&ev);
    str = GNOME_Evolution_Importer_getError (corba_importer, &ev);
    
    return str;
}

E_MAKE_TYPE (evolution_importer_client, "EvolutionImporterClient",
         EvolutionImporterClient, class_init, init, PARENT_TYPE)