aboutsummaryrefslogtreecommitdiffstats
path: root/shell/evolution-shell-client.c
blob: ff737de4c531d5f847be9306a74b76a12f4a409c (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* evolution-shell-client.c
 *
 * Copyright (C) 2000  Ximian, 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: Ettore Perazzoli
 */

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

#include <gtk/gtkmain.h>
#include <bonobo/bonobo-main.h>
#include <bonobo/bonobo-object.h>

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

#include "evolution-shell-client.h"


struct _EvolutionShellClientPrivate {
    GNOME_Evolution_Activity activity_interface;
};

#define PARENT_TYPE bonobo_object_client_get_type ()
static BonoboObjectClientClass *parent_class = NULL;


/* Easy-to-use wrapper for Evolution::user_select_folder.  */

static PortableServer_ServantBase__epv FolderSelectionListener_base_epv;
static POA_GNOME_Evolution_FolderSelectionListener__epv FolderSelectionListener_epv;
static POA_GNOME_Evolution_FolderSelectionListener__vepv FolderSelectionListener_vepv;
static gboolean FolderSelectionListener_vtables_initialized = FALSE;

struct _FolderSelectionListenerServant {
    POA_GNOME_Evolution_FolderSelectionListener servant;
    char **uri_return;
    char **physical_uri_return;
};
typedef struct _FolderSelectionListenerServant FolderSelectionListenerServant;

static void
impl_FolderSelectionListener_selected (PortableServer_Servant servant,
                       const CORBA_char *uri,
                       const CORBA_char *physical_uri,
                       CORBA_Environment *ev)
{
    FolderSelectionListenerServant *listener_servant;

    listener_servant = (FolderSelectionListenerServant *) servant;

    if (listener_servant->uri_return != NULL)
        * (listener_servant->uri_return) = g_strdup (uri);

    if (listener_servant->physical_uri_return != NULL)
        * (listener_servant->physical_uri_return) = g_strdup (physical_uri);

    gtk_main_quit ();
}

static void
impl_FolderSelectionListener_cancel (PortableServer_Servant servant,
                     CORBA_Environment *ev)
{
    FolderSelectionListenerServant *listener_servant;

    listener_servant = (FolderSelectionListenerServant *) servant;

    if (listener_servant->uri_return != NULL)
        * (listener_servant->uri_return) = NULL;

    if (listener_servant->physical_uri_return != NULL)
        * (listener_servant->physical_uri_return) = NULL;

    gtk_main_quit ();
}   

static void
init_FolderSelectionListener_vtables (void)
{
    FolderSelectionListener_base_epv._private    = NULL;
    FolderSelectionListener_base_epv.finalize    = NULL;
    FolderSelectionListener_base_epv.default_POA = NULL;

    FolderSelectionListener_epv.notifySelected = impl_FolderSelectionListener_selected;
    FolderSelectionListener_epv.notifyCanceled = impl_FolderSelectionListener_cancel;

    FolderSelectionListener_vepv._base_epv                             = &FolderSelectionListener_base_epv;
    FolderSelectionListener_vepv.GNOME_Evolution_FolderSelectionListener_epv = &FolderSelectionListener_epv;
        
    FolderSelectionListener_vtables_initialized = TRUE;
}

static GNOME_Evolution_FolderSelectionListener
create_folder_selection_listener_interface (char **result,
                        char **uri_return,
                        char **physical_uri_return)
{
    GNOME_Evolution_FolderSelectionListener corba_interface;
    CORBA_Environment ev;
    FolderSelectionListenerServant *servant;
    PortableServer_Servant listener_servant;

    if (! FolderSelectionListener_vtables_initialized)
        init_FolderSelectionListener_vtables ();

    servant = g_new0 (FolderSelectionListenerServant, 1);
    servant->servant.vepv        = &FolderSelectionListener_vepv;
    servant->uri_return          = uri_return;
    servant->physical_uri_return = physical_uri_return;

    listener_servant = (PortableServer_Servant) servant;

    CORBA_exception_init (&ev);

    POA_GNOME_Evolution_FolderSelectionListener__init (listener_servant, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        g_free(servant);
        return CORBA_OBJECT_NIL;
    }

    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;

    CORBA_exception_free (&ev);

    return corba_interface;
}

static int
count_string_items (const char *list[])
{
    int i;

    if (list == NULL)
        return 0;

    for (i = 0; list[i] != NULL; i++)
        ;

    return i;
}

static void
user_select_folder (EvolutionShellClient *shell_client,
            const char *title,
            const char *default_folder,
            const char *possible_types[],
            char **uri_return,
            char **physical_uri_return)
{
    GNOME_Evolution_FolderSelectionListener listener_interface;
    GNOME_Evolution_Shell corba_shell;
    CORBA_Environment ev;
    GNOME_Evolution_Shell_FolderTypeNameList corba_type_name_list;
    int num_possible_types;
    char *result;

    result = NULL;

    if (uri_return != NULL)
        *uri_return = NULL;
    if (physical_uri_return != NULL)
        *physical_uri_return = NULL;

    listener_interface = create_folder_selection_listener_interface (&result, uri_return, 
                                     physical_uri_return);
    if (listener_interface == CORBA_OBJECT_NIL)
        return;

    CORBA_exception_init (&ev);

    corba_shell = bonobo_object_corba_objref (BONOBO_OBJECT (shell_client));

    num_possible_types = count_string_items (possible_types);

    corba_type_name_list._length  = num_possible_types;
    corba_type_name_list._maximum = num_possible_types;
    corba_type_name_list._buffer  = (CORBA_char **) possible_types;

    GNOME_Evolution_Shell_selectUserFolder (corba_shell, listener_interface,
                        title, default_folder, &corba_type_name_list,
                        &ev);

    if (ev._major != CORBA_NO_EXCEPTION) {
        CORBA_exception_free (&ev);
        return;
    }

    gtk_main();

    CORBA_Object_release (listener_interface, &ev);

    CORBA_exception_free (&ev);
}


/* GtkObject methods.  */

static void
destroy (GtkObject *object)
{
    EvolutionShellClient *shell_client;
    EvolutionShellClientPrivate *priv;
    CORBA_Environment ev;

    shell_client = EVOLUTION_SHELL_CLIENT (object);
    priv = shell_client->priv;

    CORBA_exception_init (&ev);

    if (priv->activity_interface != CORBA_OBJECT_NIL) {
        Bonobo_Unknown_unref (priv->activity_interface, &ev);
        if (ev._major != CORBA_NO_EXCEPTION)
            g_warning ("EvolutionShellClient::destroy: "
                   "Error unreffing the ::Activity interface -- %s\n",
                   ev._repo_id);
        CORBA_Object_release (priv->activity_interface, &ev);
    }

    CORBA_exception_free (&ev);

    g_free (priv);

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

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

    parent_class = gtk_type_class (bonobo_object_get_type ());

    object_class = GTK_OBJECT_CLASS (klass);

    object_class->destroy = destroy;
}

static void
init (EvolutionShellClient *shell_client)
{
    EvolutionShellClientPrivate *priv;

    priv = g_new (EvolutionShellClientPrivate, 1);
    priv->activity_interface = CORBA_OBJECT_NIL;

    shell_client->priv = priv;
}


/**
 * evolution_shell_client_construct:
 * @shell_client: 
 * @corba_shell: 
 * 
 * Construct @shell_client associating it to @corba_shell.
 **/
void
evolution_shell_client_construct (EvolutionShellClient *shell_client,
                  GNOME_Evolution_Shell corba_shell)
{
    CORBA_Environment ev;
    EvolutionShellClientPrivate *priv;

    g_return_if_fail (shell_client != NULL);
    g_return_if_fail (EVOLUTION_IS_SHELL_CLIENT (shell_client));
    g_return_if_fail (corba_shell != CORBA_OBJECT_NIL);

    bonobo_object_construct (BONOBO_OBJECT (shell_client), (CORBA_Object) corba_shell);

    CORBA_exception_init (&ev);

    priv = shell_client->priv;
    g_return_if_fail (priv->activity_interface == CORBA_OBJECT_NIL);

    priv->activity_interface = Bonobo_Unknown_queryInterface (bonobo_object_corba_objref (BONOBO_OBJECT (shell_client)),
                                  "IDL:GNOME/Evolution/Activity:1.0",
                                  &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        g_warning ("EvolutionShellClient: Error querying interface ::Activity -- %s", ev._repo_id);
        priv->activity_interface = CORBA_OBJECT_NIL;
    } else if (CORBA_Object_is_nil (priv->activity_interface, &ev)) {
        g_warning ("No ::Activity interface for ShellClient %p", shell_client);
    }

    CORBA_exception_free (&ev);
}

/**
 * evolution_shell_client_new:
 * @corba_shell: A pointer to the CORBA Evolution::Shell interface.
 * 
 * Create a new client object for @corba_shell.
 * 
 * Return value: A pointer to the Evolution::Shell client BonoboObject.
 **/
EvolutionShellClient *
evolution_shell_client_new (GNOME_Evolution_Shell corba_shell)
{
    EvolutionShellClient *shell_client;

    shell_client = gtk_type_new (evolution_shell_client_get_type ());

    evolution_shell_client_construct (shell_client, corba_shell);

    if (bonobo_object_corba_objref (BONOBO_OBJECT (shell_client)) == CORBA_OBJECT_NIL) {
        bonobo_object_unref (BONOBO_OBJECT (shell_client));
        return NULL;
    }

    return shell_client;
}


/**
 * evolution_shell_client_user_select_folder:
 * @shell_client: A EvolutionShellClient object
 * @title: The title for the folder selection dialog
 * @default_folder: URI (physical or evolution:) of the folder initially selected on the dialog
 * @uri_return: 
 * @physical_uri_return: 
 * 
 * Pop up the shell's folder selection dialog with the specified @title and
 * @default_folder as the initially selected folder.  On return, set *@uri and
 * *@physical_uri to the evolution: URI and the physical URI of the selected
 * folder (or %NULL if the user cancelled the dialog).  (The dialog is modal.)
 **/
void
evolution_shell_client_user_select_folder (EvolutionShellClient *shell_client,
                       const char *title,
                       const char *default_folder,
                       const char *possible_types[],
                       char **uri_return,
                       char **physical_uri_return)
{
    g_return_if_fail (shell_client != NULL);
    g_return_if_fail (EVOLUTION_IS_SHELL_CLIENT (shell_client));
    g_return_if_fail (title != NULL);
    g_return_if_fail (default_folder != NULL);

    user_select_folder (shell_client, title, default_folder, possible_types,
                uri_return, physical_uri_return);
}


/**
 * evolution_shell_client_get_activity_interface:
 * @shell_client: An EvolutionShellClient object
 * 
 * Get the GNOME::Evolution::Activity for the shell associated to
 * @shell_client.
 * 
 * Return value: A CORBA Object represeting the GNOME::Evolution::Activity
 * interface.
 **/
GNOME_Evolution_Activity
evolution_shell_client_get_activity_interface (EvolutionShellClient *shell_client)
{
    g_return_val_if_fail (shell_client != NULL, CORBA_OBJECT_NIL);
    g_return_val_if_fail (EVOLUTION_IS_SHELL_CLIENT (shell_client), CORBA_OBJECT_NIL);

    return shell_client->priv->activity_interface;
}


/**
 * evolution_shell_client_get_local_storage:
 * @shell_client: An EvolutionShellClient object
 * 
 * Retrieve the local storage interface for this shell.
 * 
 * Return value: a pointer to the CORBA object implementing the local storage
 * in the shell associated with @shell_client.
 **/
GNOME_Evolution_Storage
evolution_shell_client_get_local_storage (EvolutionShellClient *shell_client)
{
    GNOME_Evolution_Shell corba_shell;
    GNOME_Evolution_Storage corba_local_storage;
    CORBA_Environment ev;

    g_return_val_if_fail (shell_client != NULL, CORBA_OBJECT_NIL);
    g_return_val_if_fail (EVOLUTION_IS_SHELL_CLIENT (shell_client), CORBA_OBJECT_NIL);

    CORBA_exception_init (&ev);

    corba_shell = bonobo_object_corba_objref (BONOBO_OBJECT (shell_client));
    if (corba_shell == CORBA_OBJECT_NIL)
        return CORBA_OBJECT_NIL;

    corba_local_storage = GNOME_Evolution_Shell_getLocalStorage (corba_shell, &ev);
    if (ev._major != CORBA_NO_EXCEPTION) {
        CORBA_exception_free (&ev);
        return CORBA_OBJECT_NIL;
    }

    CORBA_exception_free (&ev);

    return corba_local_storage;
}


E_MAKE_TYPE (evolution_shell_client, "EvolutionShellClient", EvolutionShellClient, class_init, init, PARENT_TYPE)