aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/component/e-address-popup.c
blob: 134ce1241b1eb8d86e6c08beaa98c3fd89f078ca (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 * e-address-popup.c
 *
 * Copyright (C) 2001 Ximian, Inc.
 *
 * Developed by Jon Trowbridge <trow@ximian.com>
 */

/*
 * 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.
 */

#include <config.h>
#include "e-address-popup.h"
#include <bonobo/bonobo-control.h>
#include <bonobo/bonobo-property-bag.h>
#include <bonobo/bonobo-generic-factory.h>
#include <gal/widgets/e-popup-menu.h>
#include <gal/widgets/e-unicode.h>
#include <addressbook/backend/ebook/e-book.h>
#include <addressbook/backend/ebook/e-book-util.h>
#include <addressbook/contact-editor/e-contact-editor.h>
#include <addressbook/contact-editor/e-contact-quick-add.h>
#include <addressbook/gui/widgets/e-minicard-widget.h>

static GtkObjectClass *parent_class;
static EBook *common_book = NULL; /* still sort of lame */

static void e_address_popup_destroy             (GtkObject *);
static void e_address_popup_query              (EAddressPopup *);


static void
e_address_popup_class_init (EAddressPopupClass *klass)
{
    GtkObjectClass *object_class = (GtkObjectClass *) klass;

    parent_class = GTK_OBJECT_CLASS (gtk_type_class (gtk_event_box_get_type ()));

    object_class->destroy = e_address_popup_destroy;
}

static void
e_address_popup_init (EAddressPopup *pop)
{
    
}

static void
e_address_popup_destroy (GtkObject *obj)
{
    EAddressPopup *pop = E_ADDRESS_POPUP (obj);

    if (pop->card)
        gtk_object_unref (GTK_OBJECT (pop->card));

    if (pop->leave_timeout_tag)
        gtk_timeout_remove (pop->leave_timeout_tag);

    g_free (pop->name);
    g_free (pop->email);

    if (GTK_OBJECT_CLASS (parent_class)->destroy)
        GTK_OBJECT_CLASS (parent_class)->destroy (obj);
}

GtkType
e_address_popup_get_type (void)
{
    static GtkType pop_type = 0;

    if (!pop_type) {
        GtkTypeInfo pop_info = {
            "EAddressPopup",
            sizeof (EAddressPopup),
            sizeof (EAddressPopupClass),
            (GtkClassInitFunc) e_address_popup_class_init,
            (GtkObjectInitFunc) e_address_popup_init,
            NULL, NULL,
            (GtkClassInitFunc) NULL
        };

        pop_type = gtk_type_unique (gtk_event_box_get_type (), &pop_info);
    }

    return pop_type;
}

static void
e_address_popup_refresh_names (EAddressPopup *pop)
{
    if (pop->name_widget) {
        if (pop->name && *pop->name) {
            gchar *s = e_utf8_to_gtk_string (pop->name_widget, pop->name);
            gtk_label_set_text (GTK_LABEL (pop->name_widget), s);
            g_free (s);
            gtk_widget_show (pop->name_widget);
        } else {
            gtk_widget_hide (pop->name_widget);
        }
    }

    if (pop->email_widget) {
        if (pop->email && *pop->email) {
            gchar *s = e_utf8_to_gtk_string (pop->email_widget, pop->email);
            gtk_label_set_text (GTK_LABEL (pop->email_widget), s);
            g_free (s);
            gtk_widget_show (pop->email_widget);
        } else {
            gtk_widget_hide (pop->email_widget);
        }
    }

    e_address_popup_query (pop);
}

void
e_address_popup_set_name (EAddressPopup *pop, const gchar *name)
{
    g_return_if_fail (pop && E_IS_ADDRESS_POPUP (pop));

    g_free (pop->name);
    pop->name = g_strdup (name);
    g_strstrip (pop->name);

    if (pop->name && pop->email)
        e_address_popup_refresh_names (pop);
}

void
e_address_popup_set_email (EAddressPopup *pop, const gchar *email)
{
    g_return_if_fail (pop && E_IS_ADDRESS_POPUP (pop));

    g_free (pop->email);
    pop->email = g_strdup (email);
    g_strstrip (pop->email);

    if (pop->name && pop->email)
        e_address_popup_refresh_names (pop);
}

void
e_address_popup_construct (EAddressPopup *pop)
{
    GtkWidget *vbox, *name_holder;
    GdkColor color = { 0x0, 0xffff, 0xffff, 0xffff };

    g_return_if_fail (pop && E_IS_ADDRESS_POPUP (pop));

    pop->main_vbox = gtk_vbox_new (FALSE, 0);

    /* Build Generic View */

    name_holder = gtk_event_box_new ();
    vbox = gtk_vbox_new (FALSE, 2);
    pop->name_widget = gtk_label_new ("");
    pop->email_widget = gtk_label_new ("");

    gtk_box_pack_start (GTK_BOX (vbox), pop->name_widget, TRUE, TRUE, 2);
    gtk_box_pack_start (GTK_BOX (vbox), pop->email_widget, TRUE, TRUE, 2);
    gtk_container_add (GTK_CONTAINER (name_holder), GTK_WIDGET (vbox));

    if (gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (name_holder)), &color, FALSE, TRUE)) {
        GtkStyle *style = gtk_style_copy (gtk_widget_get_style (GTK_WIDGET (name_holder)));
        style->bg[0] = color;
        gtk_widget_set_style (GTK_WIDGET (name_holder), style);
    }

    pop->generic_view = gtk_frame_new (NULL);
    gtk_container_add (GTK_CONTAINER (pop->generic_view), name_holder);
    gtk_box_pack_start (GTK_BOX (pop->main_vbox), pop->generic_view, TRUE, TRUE, 0);
    gtk_widget_show_all (pop->generic_view);

    pop->query_msg = gtk_label_new ("Querying Addressbook...");
    gtk_box_pack_start (GTK_BOX (pop->main_vbox), pop->query_msg, TRUE, TRUE, 0);
    gtk_widget_show (pop->query_msg);

    /* Build Minicard View */
    pop->minicard_view = e_minicard_widget_new ();
    gtk_box_pack_start (GTK_BOX (pop->main_vbox), pop->minicard_view, TRUE, TRUE, 0);


    /* Final assembly */

    gtk_container_add (GTK_CONTAINER (pop), pop->main_vbox);
    gtk_widget_show (pop->main_vbox);

    gtk_container_set_border_width (GTK_CONTAINER (vbox), 3);
    gtk_container_set_border_width (GTK_CONTAINER (pop), 2);
}

GtkWidget *
e_address_popup_new (void)
{
    EAddressPopup *pop = gtk_type_new (E_ADDRESS_POPUP_TYPE);
    e_address_popup_construct (pop);
    return GTK_WIDGET (pop);
}

static void
found_fields_cb (EBook *book, EBookStatus status, EList *writable_fields, gpointer closure)
{
    EAddressPopup *pop = E_ADDRESS_POPUP (closure);
    EContactEditor *ce = e_contact_editor_new (pop->card, FALSE, writable_fields, FALSE);
    e_contact_editor_raise (ce);
    gtk_widget_destroy (GTK_WIDGET (pop));
}

static void
edit_contact_info_cb (EAddressPopup *pop)
{
    e_book_get_supported_fields (common_book, found_fields_cb, pop);
}

static void
e_address_popup_cardify (EAddressPopup *pop, ECard *card)
{
    GtkWidget *b;

    g_return_if_fail (pop && E_IS_ADDRESS_POPUP (pop));
    g_return_if_fail (card && E_IS_CARD (card));
    g_return_if_fail (pop->card == NULL);

    pop->card = card;
    gtk_object_ref (GTK_OBJECT (pop->card));

    e_minicard_widget_set_card (E_MINICARD_WIDGET (pop->minicard_view), card);
    gtk_widget_show (pop->minicard_view);
    gtk_widget_hide (pop->generic_view);

    b = gtk_button_new_with_label ("Edit Contact Info");
    gtk_box_pack_start (GTK_BOX (pop->main_vbox), b, TRUE, TRUE, 0);
    gtk_signal_connect_object (GTK_OBJECT (b),
                   "clicked",
                   GTK_SIGNAL_FUNC (edit_contact_info_cb),
                   GTK_OBJECT (pop));
    gtk_widget_show (b);
}

static void
add_contacts_cb (EAddressPopup *pop)
{
    if (pop->email && *pop->email) {

        if (pop->name && *pop->name)
            e_contact_quick_add (pop->name, pop->email, NULL, NULL);
        else
            e_contact_quick_add_free_form (pop->email, NULL, NULL);

    }

    gtk_widget_destroy (GTK_WIDGET (pop));
}

static void
e_address_popup_no_matches (EAddressPopup *pop)
{
    GtkWidget *b;

    g_return_if_fail (pop && E_IS_ADDRESS_POPUP (pop));

    b = gtk_button_new_with_label ("Add to Contacts");
    gtk_box_pack_start (GTK_BOX (pop->main_vbox), b, TRUE, TRUE, 0);
    gtk_signal_connect_object (GTK_OBJECT (b),
                   "clicked",
                   GTK_SIGNAL_FUNC (add_contacts_cb),
                   GTK_OBJECT (pop));
    gtk_widget_show (b);
}

static void
e_address_popup_multiple_matches (EAddressPopup *pop)
{
    
}


/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/

/*
 *  Addressbook Query Fun
 */

static void
query_cb (EBook *book, EBookSimpleQueryStatus status, const GList *cards, gpointer closure)
{
    EAddressPopup *pop;

    if (status != E_BOOK_SIMPLE_QUERY_STATUS_SUCCESS)
        return;

    pop = E_ADDRESS_POPUP (closure);

    pop->have_queried = TRUE;
    gtk_widget_hide (pop->query_msg);

    if (cards == NULL) {

        e_address_popup_no_matches (pop);

    } else {
        if (g_list_length ((GList *) cards) == 1)
            e_address_popup_cardify (pop, E_CARD (cards->data));
        else
            e_address_popup_multiple_matches (pop);
    }

    pop->query_tag = 0;
}

static void
start_query (EAddressPopup *pop)
{
    g_assert (common_book != NULL);
    g_return_if_fail (pop && E_IS_ADDRESS_POPUP (pop));

    if (pop->query_tag)
        e_book_simple_query_cancel (common_book, pop->query_tag);

    pop->query_tag = e_book_name_and_email_query (common_book, pop->name, pop->email, query_cb, pop);
}

static void
loaded_book_cb (EBook *book, EBookStatus status, gpointer closure)
{
    g_return_if_fail (status == E_BOOK_STATUS_SUCCESS);
    g_return_if_fail (book != NULL);

    if (common_book == NULL) {
        common_book = book;
        gtk_object_ref (GTK_OBJECT (common_book));
    }
    
    start_query (E_ADDRESS_POPUP (closure));
}

static void
e_address_popup_query (EAddressPopup *pop)
{
    g_return_if_fail (pop && E_IS_ADDRESS_POPUP (pop));

    if (common_book == NULL) {
        EBook *book = e_book_new ();
        e_book_load_local_address_book (book, loaded_book_cb, pop);
    } else {
        start_query (pop);
    }
}

/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/

enum {
    PROPERTY_NAME,
    PROPERTY_EMAIL
};

static void
set_prop (BonoboPropertyBag *bag, const BonoboArg *arg, guint arg_id, CORBA_Environment *ev, gpointer user_data)
{
    EAddressPopup *pop = E_ADDRESS_POPUP (user_data);

    switch (arg_id) {

    case PROPERTY_NAME:
        e_address_popup_set_name (pop, BONOBO_ARG_GET_STRING (arg));
        break;

    case PROPERTY_EMAIL:
        e_address_popup_set_email (pop, BONOBO_ARG_GET_STRING (arg));
        break;

    default:
        g_assert_not_reached ();
    }
            
}

static BonoboControl *
e_address_popup_factory_new_control (void)
{
        BonoboControl *control;
        BonoboPropertyBag *bag;
    GtkWidget *w;

    w = e_address_popup_new ();
    control = bonobo_control_new (w);
    gtk_widget_show (w);

    gtk_signal_connect_object (GTK_OBJECT (w),
                   "destroy",
                   GTK_SIGNAL_FUNC (bonobo_object_unref),
                   GTK_OBJECT (control));

        bag = bonobo_property_bag_new (NULL, set_prop, w);
        bonobo_property_bag_add (bag, "name", PROPERTY_NAME,
                                 BONOBO_ARG_STRING, NULL, NULL,
                                 BONOBO_PROPERTY_WRITEABLE);

        bonobo_property_bag_add (bag, "email", PROPERTY_EMAIL,
                                 BONOBO_ARG_STRING, NULL, NULL,
                                 BONOBO_PROPERTY_WRITEABLE);

        bonobo_control_set_properties (control, bag);
        bonobo_object_unref (BONOBO_OBJECT (bag));

        return control;
}

static BonoboObject *
e_address_popup_factory (BonoboGenericFactory *factory, gpointer user_data)
{
    return BONOBO_OBJECT (e_address_popup_factory_new_control ());
}

void
e_address_popup_factory_init (void)
{
    static BonoboGenericFactory *factory = NULL;

    if (factory != NULL)
        return;

    factory = bonobo_generic_factory_new ("OAFIID:GNOME_Evolution_Addressbook_AddressPopupFactory",
                          e_address_popup_factory, NULL);
    
    if (factory == NULL)
        g_error ("I could not register an AddressPopup factory.");
}