aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/component/select-names/e-select-names-bonobo.c
blob: 25adf13a65cc4ac2cf9d4aff2939166234ff657d (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-select-names-bonobo.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 "e-select-names-bonobo.h"

#include <bonobo/bonobo-property-bag.h>
#include <bonobo/bonobo-control.h>
#include <bonobo/bonobo-event-source.h>

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

#include "Evolution-Addressbook-SelectNames.h"

#include "e-select-names-manager.h"
#include "e-select-names-model.h"
#include "e-select-names-text-model.h"
#include "e-select-names-completion.h"



#define PARENT_TYPE bonobo_object_get_type ()
static BonoboObjectClass *parent_class = NULL;

struct _ESelectNamesBonoboPrivate {
    ESelectNamesManager *manager;
    BonoboEventSource *event_source;
};

enum _EntryPropertyID {
    ENTRY_PROPERTY_ID_TEXT,
    ENTRY_PROPERTY_ID_DESTINATIONS,
    ENTRY_PROPERTY_ID_ALLOW_CONTACT_LISTS,
    ENTRY_PROPERTY_ID_ENTRY_CHANGED
};
typedef enum _EntryPropertyID EntryPropertyID;


/* PropertyBag implementation for the entry widgets.  */

static void
entry_get_property_fn (BonoboPropertyBag *bag,
               BonoboArg *arg,
               unsigned int arg_id,
               CORBA_Environment *ev,
               void *user_data)
{
    GtkWidget *w;

    w = GTK_WIDGET (user_data);

    switch (arg_id) {
    case ENTRY_PROPERTY_ID_TEXT:
        {
            ESelectNamesModel *model;
            model = E_SELECT_NAMES_MODEL (gtk_object_get_data (GTK_OBJECT (w), "select_names_model"));
            g_assert (model != NULL);
            
            BONOBO_ARG_SET_STRING (arg, e_select_names_model_get_textification (model));
        break;
        }

    case ENTRY_PROPERTY_ID_DESTINATIONS:
        {
            ESelectNamesModel *model;
            char *text;

            model = E_SELECT_NAMES_MODEL (gtk_object_get_data (GTK_OBJECT (w), "select_names_model"));
            g_assert (model != NULL);

            text = e_select_names_model_export_destinationv (model);
            BONOBO_ARG_SET_STRING (arg, text);
            g_free (text);
        }
        break;

    case ENTRY_PROPERTY_ID_ALLOW_CONTACT_LISTS:
        {
            ESelectNamesCompletion *comp;
            comp = E_SELECT_NAMES_COMPLETION (gtk_object_get_data (GTK_OBJECT (w), "completion_handler"));
            g_assert (comp != NULL);

            BONOBO_ARG_SET_BOOLEAN (arg, e_select_names_completion_get_match_contact_lists (comp));
            break;
        }

    case ENTRY_PROPERTY_ID_ENTRY_CHANGED:
        /* This is a read-only property. */
        g_assert_not_reached ();
        break;

    default:
        break;
    }
}

static void
entry_set_property_fn (BonoboPropertyBag *bag,
               const BonoboArg *arg,
               guint arg_id,
               CORBA_Environment *ev,
               gpointer user_data)
{
    GtkWidget *w;

    w = GTK_WIDGET (user_data);

    switch (arg_id) {

    case ENTRY_PROPERTY_ID_TEXT:
        e_entry_set_text (E_ENTRY (w), BONOBO_ARG_GET_STRING (arg));
        break;

    case ENTRY_PROPERTY_ID_DESTINATIONS:
        {
            ESelectNamesModel *model;
            model = E_SELECT_NAMES_MODEL (gtk_object_get_data (GTK_OBJECT (w), "select_names_model"));
            g_assert (model != NULL);

            e_select_names_model_import_destinationv (model, BONOBO_ARG_GET_STRING (arg));
            break;
        }

    case ENTRY_PROPERTY_ID_ALLOW_CONTACT_LISTS:
        {
            ESelectNamesCompletion *comp;
            comp = E_SELECT_NAMES_COMPLETION (gtk_object_get_data (GTK_OBJECT (w), "completion_handler"));
            g_assert (comp != NULL);

            e_select_names_completion_set_match_contact_lists (comp, BONOBO_ARG_GET_BOOLEAN (arg));
            break;
        }
        
    case ENTRY_PROPERTY_ID_ENTRY_CHANGED:
        gtk_object_set_data (GTK_OBJECT (w), "entry_property_id_changed", GUINT_TO_POINTER (1));
        break;

    default:
        break;
    }
}


/* CORBA interface implementation.  */

static POA_GNOME_Evolution_Addressbook_SelectNames__vepv SelectNames_vepv;

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

    servant = (POA_GNOME_Evolution_Addressbook_SelectNames *) g_new0 (BonoboObjectServant, 1);
    servant->vepv = &SelectNames_vepv;

    CORBA_exception_init (&ev);

    POA_GNOME_Evolution_Addressbook_SelectNames__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
impl_SelectNames_add_section (PortableServer_Servant servant,
                  const CORBA_char *id,
                  const CORBA_char *title,
                  CORBA_Environment *ev)
{
    BonoboObject *bonobo_object;
    ESelectNamesBonobo *select_names;
    ESelectNamesBonoboPrivate *priv;

    bonobo_object = bonobo_object_from_servant (servant);
    select_names = E_SELECT_NAMES_BONOBO (bonobo_object);
    priv = select_names->priv;

    e_select_names_manager_add_section (priv->manager, id, title);
}

static void
impl_SelectNames_add_section_with_limit (PortableServer_Servant servant,
                     const CORBA_char *id,
                     const CORBA_char *title,
                     CORBA_short limit,
                     CORBA_Environment *ev)
{
    BonoboObject *bonobo_object;
    ESelectNamesBonobo *select_names;
    ESelectNamesBonoboPrivate *priv;

    bonobo_object = bonobo_object_from_servant (servant);
    select_names = E_SELECT_NAMES_BONOBO (bonobo_object);
    priv = select_names->priv;

    e_select_names_manager_add_section_with_limit (priv->manager, id, title, limit);
}

static void
entry_changed (GtkWidget *widget, BonoboControl *control)
{
    gboolean changed = GPOINTER_TO_UINT (gtk_object_get_data (GTK_OBJECT (widget), "entry_property_id_changed"));

    if (!changed)
        bonobo_control_set_property (control, "entry_changed", TRUE, NULL);
}

static Bonobo_Control
impl_SelectNames_get_entry_for_section (PortableServer_Servant servant,
                    const CORBA_char *section_id,
                    CORBA_Environment *ev)
{
    BonoboObject *bonobo_object;
    ESelectNamesBonobo *select_names;
    ESelectNamesBonoboPrivate *priv;
    GtkWidget *entry_widget;
    BonoboControl *control;
    BonoboPropertyBag *property_bag;

    bonobo_object = bonobo_object_from_servant (servant);
    select_names = E_SELECT_NAMES_BONOBO (bonobo_object);
    priv = select_names->priv;

    entry_widget = e_select_names_manager_create_entry (priv->manager, section_id);
    gtk_widget_show (entry_widget);
    
    if (entry_widget == NULL) {
        CORBA_exception_set (ev,
                     CORBA_USER_EXCEPTION,
                     ex_GNOME_Evolution_Addressbook_SelectNames_SectionNotFound,
                     NULL);
        return CORBA_OBJECT_NIL;
    }

    control = bonobo_control_new (entry_widget);

    property_bag = bonobo_property_bag_new (entry_get_property_fn, entry_set_property_fn, entry_widget);
    bonobo_property_bag_add (property_bag, "text", ENTRY_PROPERTY_ID_TEXT,
                 BONOBO_ARG_STRING, NULL, NULL,
                 BONOBO_PROPERTY_READABLE | BONOBO_PROPERTY_WRITEABLE);
    bonobo_property_bag_add (property_bag, "destinations", ENTRY_PROPERTY_ID_DESTINATIONS,
                 BONOBO_ARG_STRING, NULL, NULL,
                 BONOBO_PROPERTY_READABLE | BONOBO_PROPERTY_WRITEABLE);
    bonobo_property_bag_add (property_bag, "allow_contact_lists", ENTRY_PROPERTY_ID_ALLOW_CONTACT_LISTS,
                 BONOBO_ARG_BOOLEAN, NULL, NULL,
                 BONOBO_PROPERTY_READABLE | BONOBO_PROPERTY_WRITEABLE);
    bonobo_property_bag_add (property_bag, "entry_changed", ENTRY_PROPERTY_ID_ENTRY_CHANGED,
                 BONOBO_ARG_BOOLEAN, NULL, NULL,
                 BONOBO_PROPERTY_WRITEABLE);

    bonobo_control_set_properties (control, property_bag);

    gtk_signal_connect (GTK_OBJECT (entry_widget), "changed",
                GTK_SIGNAL_FUNC (entry_changed), control);

    return CORBA_Object_duplicate (bonobo_object_corba_objref (BONOBO_OBJECT (control)), ev);
}

static void
impl_SelectNames_activate_dialog (PortableServer_Servant servant,
                  const CORBA_char *section_id,
                  CORBA_Environment *ev)
{
    BonoboObject *bonobo_object;
    ESelectNamesBonobo *select_names;
    ESelectNamesBonoboPrivate *priv;

    bonobo_object = bonobo_object_from_servant (servant);
    select_names = E_SELECT_NAMES_BONOBO (bonobo_object);
    priv = select_names->priv;

    e_select_names_manager_activate_dialog (priv->manager, section_id);
}


/* GtkObject methods.  */

static void
impl_destroy (GtkObject *object)
{
    ESelectNamesBonobo *select_names;
    ESelectNamesBonoboPrivate *priv;

    select_names = E_SELECT_NAMES_BONOBO (object);
    priv = select_names->priv;

    gtk_object_unref (GTK_OBJECT (priv->manager));

    g_free (priv);
}


static void
corba_class_init ()
{
    POA_GNOME_Evolution_Addressbook_SelectNames__vepv *vepv;
    POA_GNOME_Evolution_Addressbook_SelectNames__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_Addressbook_SelectNames__epv, 1);
    epv->addSection          = impl_SelectNames_add_section;
    epv->addSectionWithLimit = impl_SelectNames_add_section_with_limit;
    epv->getEntryBySection   = impl_SelectNames_get_entry_for_section;
    epv->activateDialog      = impl_SelectNames_activate_dialog;

    vepv = &SelectNames_vepv;
    vepv->Bonobo_Unknown_epv                    = bonobo_object_get_epv ();
    vepv->GNOME_Evolution_Addressbook_SelectNames_epv = epv;
}

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

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

    object_class->destroy = impl_destroy;

    corba_class_init ();
}

static void
manager_changed_cb (ESelectNamesManager *manager, const gchar *section_id, gint changed_working_copy, gpointer closure)
{
    ESelectNamesBonobo *select_names = E_SELECT_NAMES_BONOBO (closure);
    BonoboArg *arg;

    arg = bonobo_arg_new (BONOBO_ARG_STRING);
    BONOBO_ARG_SET_STRING (arg, section_id);

    bonobo_event_source_notify_listeners_full (select_names->priv->event_source,
                           "GNOME/Evolution",
                           "changed",
                           changed_working_copy ? "working_copy" : "model",
                           arg, NULL);

    bonobo_arg_release (arg);
}

static void
init (ESelectNamesBonobo *select_names)
{
    ESelectNamesBonoboPrivate *priv;

    priv = g_new (ESelectNamesBonoboPrivate, 1);

    priv->manager = e_select_names_manager_new ();
    priv->event_source = NULL;

    gtk_signal_connect (GTK_OBJECT (priv->manager),
                "changed",
                GTK_SIGNAL_FUNC (manager_changed_cb),
                select_names);

    select_names->priv = priv;
}


void
e_select_names_bonobo_construct (ESelectNamesBonobo *select_names,
                 GNOME_Evolution_Addressbook_SelectNames corba_object)
{
    g_return_if_fail (select_names != NULL);
    g_return_if_fail (E_IS_SELECT_NAMES_BONOBO (select_names));

    bonobo_object_construct (BONOBO_OBJECT (select_names), corba_object);

    g_assert (select_names->priv->event_source == NULL);
    select_names->priv->event_source = bonobo_event_source_new ();
    bonobo_object_add_interface (BONOBO_OBJECT (select_names), BONOBO_OBJECT (select_names->priv->event_source));
}

ESelectNamesBonobo *
e_select_names_bonobo_new (void)
{
    POA_GNOME_Evolution_Addressbook_SelectNames *servant;
    GNOME_Evolution_Addressbook_SelectNames corba_object;
    ESelectNamesBonobo *select_names;

    servant = create_servant ();
    if (servant == NULL)
        return NULL;
    
    select_names = gtk_type_new (e_select_names_bonobo_get_type ());

    corba_object = bonobo_object_activate_servant (BONOBO_OBJECT (select_names), servant);
    e_select_names_bonobo_construct (select_names, corba_object);

    return select_names;
}


E_MAKE_TYPE (e_select_names_bonobo, "ESelectNamesBonobo", ESelectNamesBonobo, class_init, init, PARENT_TYPE)