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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* e-address-widget.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 version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* 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 <ctype.h>
#include <gtk/gtklabel.h>
#include <libgnomeui/gnome-popup-menu.h>
#include <bonobo/bonobo-control.h>
#include <bonobo/bonobo-property-bag.h>
#include <bonobo/bonobo-generic-factory.h>
#include <addressbook/gui/contact-editor/e-contact-quick-add.h>
#include "e-address-widget.h"
static void e_address_widget_class_init (EAddressWidgetClass *klass);
static void e_address_widget_init (EAddressWidget *obj);
static void e_address_widget_destroy (GtkObject *obj);
static gint e_address_widget_button_press_handler (GtkWidget *w, GdkEventButton *ev);
static void e_address_widget_popup (EAddressWidget *, GdkEventButton *ev);
static void e_address_widget_schedule_query (EAddressWidget *);
static GtkObjectClass *parent_class;
static EBook *common_book = NULL; /* sort of lame */
static gboolean doing_queries = FALSE;
static void
e_address_widget_class_init (EAddressWidgetClass *klass)
{
GtkObjectClass *object_class = (GtkObjectClass *) klass;
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->destroy = e_address_widget_destroy;
widget_class->button_press_event = e_address_widget_button_press_handler;
}
static void
e_address_widget_init (EAddressWidget *addr)
{
}
static void
e_address_widget_destroy (GtkObject *obj)
{
EAddressWidget *addr = E_ADDRESS_WIDGET (obj);
g_free (addr->name);
g_free (addr->email);
if (addr->query_tag)
e_book_simple_query_cancel (common_book, addr->query_tag);
if (addr->query_idle_tag)
gtk_idle_remove (addr->query_idle_tag);
}
static gint
e_address_widget_button_press_handler (GtkWidget *w, GdkEventButton *ev)
{
EAddressWidget *addr = E_ADDRESS_WIDGET (w);
if (ev->button == 3 && ev->state == 0) {
e_address_widget_popup (addr, ev);
return TRUE;
}
return FALSE;
}
GType
e_address_widget_get_type (void)
{
static GType aw_type = 0;
if (!aw_type) {
static const GTypeInfo aw_info = {
sizeof (EAddressWidgetClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) e_address_widget_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (EAddressWidget),
0, /* n_preallocs */
(GInstanceInitFunc) e_address_widget_init,
};
aw_type = g_type_register_static (gtk_event_box_get_type (), "EAddressWidget", &aw_info, 0);
}
return aw_type;
}
static void
gtk_widget_visible (GtkWidget *w, gboolean x)
{
if (x)
gtk_widget_show (w);
else
gtk_widget_hide (w);
}
static void
e_address_widget_refresh (EAddressWidget *addr)
{
gchar *str;
gboolean have_name, have_email;
g_return_if_fail (addr && E_IS_ADDRESS_WIDGET (addr));
have_name = addr->name && *addr->name;
have_email = addr->email && *addr->email && (addr->card == NULL || !addr->known_email);
gtk_label_set_text (GTK_LABEL (addr->name_widget), have_name ? addr->name : "");
gtk_widget_visible (addr->name_widget, have_name);
if (addr->card) {
gint i, N = strlen (addr->name);
gchar *pattern = g_malloc (N+1);
for (i=0; i<N; ++i)
pattern[i] = '_';
pattern[i] = '\0';
gtk_label_set_pattern (GTK_LABEL (addr->name_widget), pattern);
g_free (pattern);
} else {
gtk_label_set_pattern (GTK_LABEL (addr->name_widget), "");
}
if (have_email) {
str = g_strdup_printf (have_name ? "<%s>" : "%s", addr->email);
gtk_label_set_text (GTK_LABEL (addr->email_widget), str);
g_free (str);
} else {
gtk_label_set_text (GTK_LABEL (addr->email_widget), "");
}
gtk_widget_visible (addr->email_widget, have_email);
gtk_widget_visible (addr->spacer, have_name && have_email);
/* Launch a query to find the appropriate card, if necessary. */
if (addr->card == NULL)
e_address_widget_schedule_query (addr);
}
void
e_address_widget_set_name (EAddressWidget *addr, const gchar *name)
{
g_return_if_fail (addr && E_IS_ADDRESS_WIDGET (addr));
g_free (addr->name);
addr->name = g_strdup (name);
e_address_widget_refresh (addr);
}
void
e_address_widget_set_email (EAddressWidget *addr, const gchar *email)
{
g_return_if_fail (addr && E_IS_ADDRESS_WIDGET (addr));
g_free (addr->email);
addr->email = g_strdup (email);
e_address_widget_refresh (addr);
}
void
e_address_widget_set_text (EAddressWidget *addr, const gchar *text)
{
g_return_if_fail (addr && E_IS_ADDRESS_WIDGET (addr));
e_address_widget_set_email (addr, text); /* CRAP */
}
void
e_address_widget_construct (EAddressWidget *addr)
{
GtkWidget *box;
g_return_if_fail (addr && E_IS_ADDRESS_WIDGET (addr));
box = gtk_hbox_new (FALSE, 2);
addr->name_widget = gtk_label_new ("");
addr->spacer = gtk_label_new (" ");
addr->email_widget = gtk_label_new ("");
gtk_box_pack_start (GTK_BOX (box), addr->name_widget, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (box), addr->spacer, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (box), addr->email_widget, FALSE, FALSE, 0);
gtk_container_add (GTK_CONTAINER (addr), box);
gtk_widget_show (box);
gtk_widget_show (addr->name_widget);
gtk_widget_show (addr->email_widget);
}
GtkWidget *
e_address_widget_new (void)
{
EAddressWidget *addr = g_object_new (E_TYPE_ADDRESS_WIDGET, NULL);
e_address_widget_construct (addr);
return GTK_WIDGET (addr);
}
/*
*
* Cardification
*
*/
static void
e_address_widget_cardify (EAddressWidget *addr, ECard *card, gboolean known_email)
{
if (addr->card != card || addr->known_email != known_email) {
if (addr->card != card) {
if (addr->card)
g_object_unref (addr->card);
addr->card = card;
g_object_ref (addr->card);
}
addr->known_email = known_email;
if (!(addr->name && *addr->name)) {
gchar *s = e_card_name_to_string (card->name);
e_address_widget_set_name (addr, s);
g_free (s);
}
e_address_widget_refresh (addr);
}
}
static void
query_results_cb (EBook *book, EBookSimpleQueryStatus status, const GList *cards, gpointer user_data)
{
EAddressWidget *addr = user_data;
if (g_list_length ((GList *) cards) == 1) {
ECard *card = E_CARD (cards->data);
e_address_widget_cardify (addr, card, TRUE);
}
addr->query_tag = 0;
}
static void
e_address_widget_do_query (EAddressWidget *addr)
{
e_book_name_and_email_query (common_book, addr->name, addr->email, query_results_cb, addr);
}
static void
book_ready_cb (EBook *book, EBookStatus status, gpointer user_data)
{
EAddressWidget *addr = E_ADDRESS_WIDGET (user_data);
if (common_book == NULL) {
common_book = book;
g_object_ref (common_book);
} else
g_object_unref (book);
e_address_widget_do_query (addr);
}
static gint
query_idle_fn (gpointer ptr)
{
EAddressWidget *addr = E_ADDRESS_WIDGET (ptr);
if (common_book) {
e_address_widget_do_query (addr);
} else {
e_book_load_default_book (e_book_new (), book_ready_cb, addr);
}
addr->query_idle_tag = 0;
return FALSE;
}
static void
e_address_widget_schedule_query (EAddressWidget *addr)
{
if (addr->query_idle_tag || !doing_queries)
return;
addr->query_idle_tag = gtk_idle_add (query_idle_fn, addr);
}
/*
*
* Popup Menu
*
*/
#define ARBITRARY_UIINFO_LIMIT 64
static gint
popup_add_name_and_address (EAddressWidget *addr, GnomeUIInfo *uiinfo, gint i)
{
gboolean flag = FALSE;
if (addr->name && *addr->name) {
uiinfo[i].type = GNOME_APP_UI_ITEM;
uiinfo[i].label = addr->name;
++i;
flag = TRUE;
}
if (addr->email && *addr->email) {
uiinfo[i].type = GNOME_APP_UI_ITEM;
uiinfo[i].label = addr->email;
++i;
flag = TRUE;
}
if (flag) {
uiinfo[i].type = GNOME_APP_UI_SEPARATOR;
++i;
}
return i;
}
static void
flip_queries_flag_cb (GtkWidget *w, gpointer user_data)
{
doing_queries = !doing_queries;
}
static gint
popup_add_query_change (EAddressWidget *addr, GnomeUIInfo *uiinfo, gint i)
{
uiinfo[i].type = GNOME_APP_UI_SEPARATOR;
++i;
uiinfo[i].type = GNOME_APP_UI_ITEM;
uiinfo[i].label = doing_queries ? _("Disable Queries") : _("Enable Queries (Dangerous!)");
uiinfo[i].moreinfo = flip_queries_flag_cb;
++i;
return i;
}
static GtkWidget *
popup_menu_card (EAddressWidget *addr)
{
GnomeUIInfo uiinfo[ARBITRARY_UIINFO_LIMIT];
GtkWidget *pop;
gint i=0;
ECard *card = E_CARD (addr->card);
g_return_val_if_fail (card != NULL, NULL);
memset (uiinfo, 0, sizeof (uiinfo));
i = popup_add_name_and_address (addr, uiinfo, i);
uiinfo[i].type = GNOME_APP_UI_ITEM;
uiinfo[i].label = _("Edit Contact Info");
++i;
i = popup_add_query_change (addr, uiinfo, i);
uiinfo[i].type = GNOME_APP_UI_ENDOFINFO;
pop = gnome_popup_menu_new (uiinfo);
return pop;
}
static void
post_quick_add_cb (ECard *card, gpointer user_data)
{
e_address_widget_cardify (E_ADDRESS_WIDGET (user_data), card, TRUE);
}
static void
add_contacts_cb (GtkWidget *w, gpointer user_data)
{
EAddressWidget *addr = E_ADDRESS_WIDGET (user_data);
e_contact_quick_add (addr->name, addr->email, post_quick_add_cb, addr);
}
static GtkWidget *
popup_menu_nocard (EAddressWidget *addr)
{
GnomeUIInfo uiinfo[ARBITRARY_UIINFO_LIMIT];
GtkWidget *pop;
gint i=0;
memset (uiinfo, 0, sizeof (uiinfo));
i = popup_add_name_and_address (addr, uiinfo, i);
uiinfo[i].type = GNOME_APP_UI_ITEM;
uiinfo[i].label = _("Add to Contacts");
uiinfo[i].moreinfo = add_contacts_cb;
++i;
i = popup_add_query_change (addr, uiinfo, i);
uiinfo[i].type = GNOME_APP_UI_ENDOFINFO;
pop = gnome_popup_menu_new (uiinfo);
return pop;
}
static void
e_address_widget_popup (EAddressWidget *addr, GdkEventButton *ev)
{
GtkWidget *pop;
g_return_if_fail (addr && E_IS_ADDRESS_WIDGET (addr));
pop = addr->card ? popup_menu_card (addr) : popup_menu_nocard (addr);
if (pop)
gnome_popup_menu_do_popup (pop, NULL, NULL, ev, addr, GTK_WIDGET (addr));
}
/*
*
* Bonobo Control Magic
*
*/
enum {
ADDRESS_PROPERTY_NAME,
ADDRESS_PROPERTY_EMAIL,
ADDRESS_PROPERTY_TEXT,
ADDRESS_PROPERTY_BACKGROUND_RGB
};
static void
get_prop (BonoboPropertyBag *bag, BonoboArg *arg, guint arg_id, CORBA_Environment *ev, gpointer user_data)
{
EAddressWidget *addr = E_ADDRESS_WIDGET (user_data);
switch (arg_id) {
case ADDRESS_PROPERTY_NAME:
BONOBO_ARG_SET_STRING (arg, addr->name ? addr->name :"");
break;
case ADDRESS_PROPERTY_EMAIL:
BONOBO_ARG_SET_STRING (arg, addr->email ? addr->email : "");
break;
case ADDRESS_PROPERTY_TEXT:
BONOBO_ARG_SET_STRING (arg, "?");
break;
}
}
static void
set_prop (BonoboPropertyBag *bag, const BonoboArg *arg, guint arg_id, CORBA_Environment *ev, gpointer user_data)
{
EAddressWidget *addr = E_ADDRESS_WIDGET (user_data);
switch (arg_id) {
case ADDRESS_PROPERTY_NAME:
e_address_widget_set_name (addr, BONOBO_ARG_GET_STRING (arg));
break;
case ADDRESS_PROPERTY_EMAIL:
e_address_widget_set_email (addr, BONOBO_ARG_GET_STRING (arg));
break;
case ADDRESS_PROPERTY_TEXT:
e_address_widget_set_text (addr, BONOBO_ARG_GET_STRING (arg));
break;
case ADDRESS_PROPERTY_BACKGROUND_RGB:
{
gint bg = BONOBO_ARG_GET_INT (arg);
GdkColor color;
color.red = (bg & 0xff0000) >> 8;
color.green = (bg & 0x00ff00);
color.blue = (bg & 0x0000ff) << 8;
if (gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (addr)), &color, FALSE, TRUE)) {
GtkStyle *style = gtk_style_copy (gtk_widget_get_style (GTK_WIDGET (addr)));
style->bg[0] = color;
gtk_widget_set_style (GTK_WIDGET (addr), style);
}
}
break;
}
}
BonoboControl *
e_address_widget_factory_new_control (void)
{
BonoboControl *control;
BonoboPropertyBag *bag;
GtkWidget *w;
w = e_address_widget_new ();
gtk_widget_show (w);
control = bonobo_control_new (w);
bag = bonobo_property_bag_new (get_prop, set_prop, w);
bonobo_property_bag_add (bag, "name", ADDRESS_PROPERTY_NAME,
BONOBO_ARG_STRING, NULL, NULL,
BONOBO_PROPERTY_READABLE | BONOBO_PROPERTY_WRITEABLE);
bonobo_property_bag_add (bag, "email", ADDRESS_PROPERTY_EMAIL,
BONOBO_ARG_STRING, NULL, NULL,
BONOBO_PROPERTY_READABLE | BONOBO_PROPERTY_WRITEABLE);
bonobo_property_bag_add (bag, "text", ADDRESS_PROPERTY_TEXT,
BONOBO_ARG_STRING, NULL, NULL,
BONOBO_PROPERTY_READABLE | BONOBO_PROPERTY_WRITEABLE);
bonobo_property_bag_add (bag, "background_rgb", ADDRESS_PROPERTY_BACKGROUND_RGB,
BONOBO_ARG_INT, NULL, NULL,
BONOBO_PROPERTY_WRITEABLE);
bonobo_control_set_properties (control, bonobo_object_corba_objref (BONOBO_OBJECT (bag)), NULL);
bonobo_object_unref (BONOBO_OBJECT (bag));
return control;
}
|