aboutsummaryrefslogtreecommitdiffstats
path: root/shell/importer/evolution-importer-client.c
blob: 06d30d573326d816a19d1d3f50e28fc4aa3f3311 (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
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Iain Holmes  <iain@ximian.com>
 *
 * Based on evolution-shell-component-client.c by Ettore Perazzoli
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

#include "evolution-importer-client.h"

#include <glib.h>
#include <bonobo/bonobo-object.h>
#include <bonobo/bonobo-main.h>
#include <bonobo/bonobo-widget.h>
#include <bonobo/bonobo-exception.h>

#include "GNOME_Evolution_Importer.h"

G_DEFINE_TYPE (EvolutionImporterClient, evolution_importer_client, G_TYPE_OBJECT)


static void
finalise (GObject *object)
{
    /* FIXME: should this unref the client->objref?? */

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

static void
evolution_importer_client_class_init (EvolutionImporterClientClass *klass)
{
    GObjectClass *object_class;

    object_class = G_OBJECT_CLASS (klass);

    object_class->finalize = finalise;
}

static void
evolution_importer_client_init (EvolutionImporterClient *client)
{
}

/**
 * evolution_importer_client_new:
 * @objref: The CORBA_Object to make a client for.
 *
 * Makes a client for @objref. @objref should be an Evolution_Importer.
 *
 * Returns: A newly created EvolutionImporterClient.
 */
EvolutionImporterClient *
evolution_importer_client_new (const CORBA_Object objref)
{
    EvolutionImporterClient *client;

    g_return_val_if_fail (objref != CORBA_OBJECT_NIL, NULL);

    client = g_object_new (evolution_importer_client_get_type (), NULL);
    client->objref = objref;

    return client;
}

/**
 * evolution_importer_client_new_from_id:
 * @id: The oafiid of the component to make a client for.
 *
 * Makes a client for the object returned by activating @id.
 *
 * Returns: A newly created EvolutionImporterClient.
 */
EvolutionImporterClient *
evolution_importer_client_new_from_id (const char *id)
{
    CORBA_Environment ev;
    CORBA_Object objref;

    g_return_val_if_fail (id != NULL, NULL);

    CORBA_exception_init (&ev);
    objref = bonobo_activation_activate_from_id ((char *) id, 0, NULL, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        CORBA_exception_free (&ev);
        g_warning ("Could not start %s.", id);
        return NULL;
    }

    CORBA_exception_free (&ev);
    if (objref == CORBA_OBJECT_NIL) {
        g_warning ("Could not activate component %s", id);
        return NULL;
    }

    return evolution_importer_client_new (objref);
}

/* API */
GtkWidget *
evolution_importer_client_create_control (EvolutionImporterClient *client)
{
    GNOME_Evolution_Importer corba_importer;
    GtkWidget *widget = NULL;
    Bonobo_Control control;
    CORBA_Environment ev;

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

    CORBA_exception_init (&ev);
    corba_importer = client->objref;
    GNOME_Evolution_Importer_createControl (corba_importer, &control, &ev);

    if (!BONOBO_EX (&ev)) {
        /* FIXME Pass in container? */
        widget = bonobo_widget_new_control_from_objref (control, NULL);
        gtk_widget_show (widget);
    }

    CORBA_exception_free (&ev);

    return widget;
}

/**
 * evolution_importer_client_support_format:
 * @client: The EvolutionImporterClient.
 * @filename: Name of the file to check.
 *
 * Checks whether @client is able to import @filename.
 *
 * Returns: TRUE if @client can import @filename, FALSE otherwise.
 */
gboolean
evolution_importer_client_support_format (EvolutionImporterClient *client,
                      const char *filename)
{
    GNOME_Evolution_Importer corba_importer;
    gboolean result;
    CORBA_Environment ev;

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

    CORBA_exception_init (&ev);
    corba_importer = client->objref;
    result = GNOME_Evolution_Importer_supportFormat (corba_importer,
                             filename, &ev);
    CORBA_exception_free (&ev);

    return result;
}

/**
 * evolution_importer_client_load_file:
 * @client: The EvolutionImporterClient.
 * @filename: The file to load.
 * @physical_uri: The physical URI of the folder to import data into.
 * @folder_type: The type of the folder represented by @physical_uri.
 *
 * Loads and initialises the importer.
 *
 * Returns: TRUE on sucess, FALSE on failure.
 */
gboolean
evolution_importer_client_load_file (EvolutionImporterClient *client, const char *filename)
{
    GNOME_Evolution_Importer corba_importer;
    gboolean result;
    CORBA_Environment ev;

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

    CORBA_exception_init (&ev);
    corba_importer = client->objref;
    result = GNOME_Evolution_Importer_loadFile (corba_importer, filename, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("Oh there *WAS* an exception.\nIt was %s",
               CORBA_exception_id (&ev));
        CORBA_exception_free (&ev);
        return FALSE;
    }
    CORBA_exception_free (&ev);

    return result;
}

/**
 * evolution_importer_client_process_item:
 * @client: The EvolutionImporterClient.
 * @listener: The EvolutionImporterListener.
 *
 * Starts importing the next item in the file. @listener will be notified
 * when the item has finished.
 */
void
evolution_importer_client_process_item (EvolutionImporterClient *client,
                    EvolutionImporterListener *listener)
{
    GNOME_Evolution_Importer corba_importer;
    GNOME_Evolution_ImporterListener corba_listener;
    CORBA_Environment ev;

    g_return_if_fail (client != NULL);
    g_return_if_fail (EVOLUTION_IS_IMPORTER_CLIENT (client));
    g_return_if_fail (listener != NULL);
    g_return_if_fail (EVOLUTION_IS_IMPORTER_LISTENER (listener));

    CORBA_exception_init (&ev);

    corba_importer = client->objref;
    corba_listener = bonobo_object_corba_objref (BONOBO_OBJECT (listener));
    GNOME_Evolution_Importer_processItem (corba_importer,
                          corba_listener, &ev);
    CORBA_exception_free (&ev);
}

/**
 * evolution_importer_client_get_error:
 * @client: The EvolutionImporterClient.
 *
 * Gets the error as a string.
 *
 * Returns: The error as a string. If there is no error NULL is returned.
 * Importers need not support this method and if so, NULL is also returned.
 */
const char *
evolution_importer_client_get_error (EvolutionImporterClient *client)
{
    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);

    corba_importer = client->objref;

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

    return str;
}