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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Authors:
* Chris Lahey <clahey@ximian.com>
* Jon Trowbridge <trow@ximian.com.
*
* Copyright (C) 2000, 2001 Ximian, Inc.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gal/e-text/e-entry.h>
#include <libgnome/gnome-i18n.h>
#include "e-select-names-manager.h"
#include "e-select-names-marshal.h"
#include "e-select-names-model.h"
#include "e-select-names-text-model.h"
#include "e-select-names.h"
#include "e-select-names-completion.h"
#include "e-select-names-popup.h"
#include "e-folder-list.h"
#include <addressbook/backend/ebook/e-book-util.h>
#include <addressbook/backend/ebook/e-destination.h>
#include "addressbook/gui/component/addressbook.h"
#include <bonobo/bonobo-object.h>
enum {
CHANGED,
OK,
CANCEL,
LAST_SIGNAL
};
static guint e_select_names_manager_signals[LAST_SIGNAL] = { 0 };
static GObjectClass *parent_class = NULL;
typedef struct {
char *id;
char *title;
ESelectNamesModel *model;
ESelectNamesModel *original_model;
ESelectNamesManager *manager;
guint changed_tag;
} ESelectNamesManagerSection;
typedef struct {
char *id;
EEntry *entry;
ESelectNamesManager *manager;
ESelectNamesModel *model;
ECompletion *comp;
guint cleaning_tag;
} ESelectNamesManagerEntry;
static void e_select_names_manager_init (ESelectNamesManager *manager);
static void e_select_names_manager_class_init (ESelectNamesManagerClass *klass);
static void e_select_names_manager_dispose (GObject *object);
/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
/* ESelectNamesManagerSection routines */
static void
section_model_changed_cb (ESelectNamesModel *model, gpointer closure)
{
ESelectNamesManagerSection *section = closure;
g_signal_emit (section->manager,
e_select_names_manager_signals[CHANGED], 0,
section->id,
FALSE);
}
static ESelectNamesManagerSection *
e_select_names_manager_section_new (ESelectNamesManager *manager,
const gchar *id,
const gchar *title,
ESelectNamesModel *model)
{
ESelectNamesManagerSection *section;
g_return_val_if_fail (E_IS_SELECT_NAMES_MANAGER (manager), NULL);
g_return_val_if_fail (E_IS_SELECT_NAMES_MODEL (model), NULL);
section = g_new0 (ESelectNamesManagerSection, 1);
section->id = g_strdup (id);
section->title = g_strdup (title);
section->manager = manager;
section->model = model;
g_object_ref (section->model);
section->changed_tag =
g_signal_connect (section->model,
"changed",
G_CALLBACK (section_model_changed_cb),
section);
return section;
}
static void
e_select_names_manager_section_free (ESelectNamesManagerSection *section)
{
if (section == NULL)
return;
g_free (section->id);
g_free (section->title);
if (section->model) {
g_signal_handler_disconnect (section->model, section->changed_tag);
g_object_unref (section->model);
}
if (section->original_model) {
g_object_unref (section->original_model);
}
g_free (section);
}
/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
/* ESelectNamesManagerEntry routines */
static ESelectNamesManagerEntry *
get_entry_info (EEntry *entry)
{
g_return_val_if_fail (E_IS_ENTRY (entry), NULL);
return (ESelectNamesManagerEntry *) g_object_get_data (G_OBJECT (entry), "entry_info");
}
static void
popup_cb (EEntry *eentry, GdkEventButton *ev, gint pos, gpointer user_data)
{
ESelectNamesTextModel *text_model;
g_object_get (eentry,
"model", &text_model,
NULL);
g_assert (E_IS_SELECT_NAMES_TEXT_MODEL (text_model));
e_select_names_popup (text_model, ev, pos, GTK_WIDGET (eentry));
}
#if 0
static gboolean
clean_cb (gpointer ptr)
{
ESelectNamesManagerEntry *entry = ptr;
e_select_names_model_clean (entry->model, TRUE);
entry->cleaning_tag = 0;
return FALSE;
}
#endif
static gint
focus_in_cb (GtkWidget *w, GdkEventFocus *ev, gpointer user_data)
{
ESelectNamesManagerEntry *entry = user_data;
if (entry->cleaning_tag) {
gtk_timeout_remove (entry->cleaning_tag);
entry->cleaning_tag = 0;
}
e_select_names_model_cancel_cardify_all (entry->model);
return FALSE;
}
static gint
focus_out_cb (GtkWidget *w, GdkEventFocus *ev, gpointer user_data)
{
#if 0
/* XXX fix me */
ESelectNamesManagerEntry *entry = user_data;
gboolean visible = e_entry_completion_popup_is_visible (entry->entry);
if (! visible) {
e_select_names_model_cardify_all (entry->model, entry->manager->completion_book, 100);
if (entry->cleaning_tag == 0)
entry->cleaning_tag = gtk_timeout_add (100, clean_cb, entry);
}
#endif
return FALSE;
}
static void
completion_popup_cb (EEntry *w, gint visible, gpointer user_data)
{
#if 0
/* XXX fix me */
ESelectNamesManagerEntry *entry = user_data;
if (!visible && !GTK_WIDGET_HAS_FOCUS (GTK_WIDGET (entry->entry->canvas)))
e_select_names_model_cardify_all (entry->model, entry->manager->completion_book, 0);
#endif
}
static void
completion_handler (EEntry *entry, ECompletionMatch *match)
{
ESelectNamesManagerEntry *mgr_entry;
ESelectNamesTextModel *text_model;
EDestination *dest;
gint i, pos, start_pos, len;
if (match == NULL || match->user_data == NULL)
return;
mgr_entry = get_entry_info (entry);
dest = E_DESTINATION (match->user_data);
/* Sometimes I really long for garbage collection. Reference
counting makes you feel 31337, but sometimes it is just a
bitch. */
g_object_ref (dest);
g_object_get (entry,
"model", &text_model,
NULL);
g_assert (E_IS_SELECT_NAMES_TEXT_MODEL (text_model));
pos = e_entry_get_position (entry);
e_select_names_model_text_pos (mgr_entry->model, text_model->seplen, pos, &i, NULL, NULL);
e_select_names_model_replace (mgr_entry->model, i, dest);
e_select_names_model_name_pos (mgr_entry->model, text_model->seplen, i, &start_pos, &len);
e_entry_set_position (entry, start_pos+len);
}
static ESelectNamesManagerEntry *
e_select_names_manager_entry_new (ESelectNamesManager *manager, ESelectNamesModel *model, const gchar *id)
{
ESelectNamesManagerEntry *entry;
ETextModel *text_model;
GList *l;
g_return_val_if_fail (E_IS_SELECT_NAMES_MANAGER (manager), NULL);
g_return_val_if_fail (E_IS_SELECT_NAMES_MODEL (model), NULL);
entry = g_new0 (ESelectNamesManagerEntry, 1);
entry->id = g_strdup (id);
entry->entry = E_ENTRY (e_entry_new ());
text_model = e_select_names_text_model_new (model);
g_object_set(entry->entry,
"model", text_model, /* The entry takes ownership of the text model */
"editable", TRUE,
"use_ellipsis", TRUE,
"allow_newlines", FALSE,
NULL);
g_object_ref (entry->entry);
entry->comp = e_select_names_completion_new (E_SELECT_NAMES_TEXT_MODEL (text_model));
for (l = manager->completion_books; l; l = l->next) {
EBook *book = l->data;
e_select_names_completion_add_book (E_SELECT_NAMES_COMPLETION(entry->comp), book);
}
e_entry_enable_completion_full (entry->entry, entry->comp, 100, completion_handler);
entry->manager = manager;
entry->model = model;
g_object_ref (model);
g_signal_connect (entry->entry,
"popup",
G_CALLBACK (popup_cb),
entry);
g_signal_connect (entry->entry->canvas,
"focus_in_event",
G_CALLBACK (focus_in_cb),
entry);
g_signal_connect (entry->entry->canvas,
"focus_out_event",
G_CALLBACK (focus_out_cb),
entry);
g_signal_connect (entry->entry,
"completion_popup",
G_CALLBACK (completion_popup_cb),
entry);
g_object_set_data (G_OBJECT (entry->entry), "entry_info", entry);
g_object_set_data (G_OBJECT (entry->entry), "select_names_model", model);
g_object_set_data (G_OBJECT (entry->entry), "select_names_text_model", text_model);
g_object_set_data (G_OBJECT (entry->entry), "completion_handler", entry->comp);
return entry;
}
static void
e_select_names_manager_entry_free (ESelectNamesManagerEntry *entry)
{
if (entry == NULL)
return;
g_free (entry->id);
g_object_unref (entry->model);
g_object_unref (entry->entry);
if (entry->cleaning_tag)
gtk_timeout_remove (entry->cleaning_tag);
g_free (entry);
}
/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
static void
e_select_names_manager_save_models (ESelectNamesManager *manager)
{
GList *iter;
for (iter = manager->sections; iter != NULL; iter = g_list_next (iter)) {
ESelectNamesManagerSection *section = iter->data;
if (section->original_model == NULL && section->model != NULL)
section->original_model = e_select_names_model_duplicate (section->model);
}
}
static void
e_select_names_manager_revert_to_saved_models (ESelectNamesManager *manager)
{
GList *iter;
for (iter = manager->sections; iter != NULL; iter = g_list_next (iter)) {
ESelectNamesManagerSection *section = iter->data;
if (section->model && section->original_model) {
e_select_names_model_overwrite_copy (section->model, section->original_model);
g_object_unref (section->original_model);
section->original_model = NULL;
}
}
}
static void
e_select_names_manager_discard_saved_models (ESelectNamesManager *manager)
{
GList *iter;
for (iter = manager->sections; iter != NULL; iter = g_list_next (iter)) {
ESelectNamesManagerSection *section = iter->data;
if (section->original_model) {
g_object_unref (section->original_model);
section->original_model = NULL;
}
}
}
/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
static void
open_book_cb (EBook *book, EBookStatus status, ESelectNamesManager *manager)
{
if (status == E_BOOK_STATUS_SUCCESS) {
GList *l;
for (l = manager->entries; l; l = l->next) {
ESelectNamesManagerEntry *entry = l->data;
e_select_names_completion_add_book (E_SELECT_NAMES_COMPLETION(entry->comp), book);
}
manager->completion_books = g_list_append (manager->completion_books, book);
g_object_ref (book);
}
g_object_unref (manager); /* unref ourself (matches ref before the load_uri call below) */
}
static void
load_completion_books (ESelectNamesManager *manager)
{
EFolderListItem *folders = e_folder_list_parse_xml (manager->cached_folder_list);
EFolderListItem *f;
for (f = folders; f && f->physical_uri; f++) {
char *uri;
EBook *book = e_book_new ();
g_object_ref (manager); /* ref ourself before our async call */
uri = e_book_expand_uri (f->physical_uri);
addressbook_load_uri (book, uri, (EBookCallback)open_book_cb, manager);
g_free (uri);
}
e_folder_list_free_items (folders);
}
static void
read_completion_books_from_db (ESelectNamesManager *manager, EConfigListener *db)
{
char *val;
val = e_config_listener_get_string (db, "/apps/Evolution/Addressbook/Completion/uris");
if (val) {
g_free (manager->cached_folder_list);
manager->cached_folder_list = val;
load_completion_books(manager);
}
}
static void
uris_listener (EConfigListener *db, const char *key,
ESelectNamesManager *manager)
{
GList *l;
char *val;
/* return if it's not the key we're interested in */
if (!strcmp (key, "/apps/Evolution/Addressbook/Completion/uris"))
return;
val = e_config_listener_get_string (db, "/apps/Evolution/Addressbook/Completion/uris");
if (val) {
if (!manager->cached_folder_list || strcmp (val, manager->cached_folder_list)) {
for (l = manager->entries; l; l = l->next) {
ESelectNamesManagerEntry *entry = l->data;
e_select_names_completion_clear_books (E_SELECT_NAMES_COMPLETION (entry->comp));
}
g_list_foreach (manager->completion_books, (GFunc)g_object_unref, NULL);
g_list_free (manager->completion_books);
manager->completion_books = NULL;
g_free (manager->cached_folder_list);
manager->cached_folder_list = val;
load_completion_books (manager);
}
}
}
/**
* e_select_names_manager_new:
* @VCard: a string in vCard format
*
* Returns: a new #ESelectNamesManager that wraps the @VCard.
*/
ESelectNamesManager *
e_select_names_manager_new (void)
{
ESelectNamesManager *manager = g_object_new (E_TYPE_SELECT_NAMES_MANAGER, NULL);
EConfigListener *db;
db = e_book_get_config_database();
manager->listener_id = g_signal_connect (db,
"key_changed",
G_CALLBACK (uris_listener), manager);
read_completion_books_from_db (manager, db);
return manager;
}
/*
* ESelectNamesManager lifecycle management and vcard loading/saving.
*/
void
e_select_names_manager_add_section (ESelectNamesManager *manager,
const char *id,
const char *title)
{
g_return_if_fail (E_IS_SELECT_NAMES_MANAGER (manager));
g_return_if_fail (id != NULL);
g_return_if_fail (title != NULL);
e_select_names_manager_add_section_with_limit (manager, id, title, -1);
}
void
e_select_names_manager_add_section_with_limit (ESelectNamesManager *manager,
const char *id,
const char *title,
gint limit)
{
ESelectNamesManagerSection *section;
ESelectNamesModel *model;
g_return_if_fail (E_IS_SELECT_NAMES_MANAGER (manager));
g_return_if_fail (id != NULL);
g_return_if_fail (title != NULL);
model = e_select_names_model_new ();
e_select_names_model_set_limit (model, limit);
section = e_select_names_manager_section_new (manager, id, title, model);
manager->sections = g_list_append (manager->sections, section);
g_object_unref (model);
}
ESelectNamesModel *
e_select_names_manager_get_source (ESelectNamesManager *manager,
const char *id)
{
GList *iter;
g_return_val_if_fail (E_IS_SELECT_NAMES_MANAGER (manager), NULL);
g_return_val_if_fail (id != NULL, NULL);
for (iter = manager->sections; iter != NULL; iter = g_list_next (iter)) {
ESelectNamesManagerSection *section = iter->data;
if (!strcmp (section->id, id))
return section->model;
}
return NULL;
}
GtkWidget *
e_select_names_manager_create_entry (ESelectNamesManager *manager, const char *id)
{
GList *iter;
g_return_val_if_fail (E_IS_SELECT_NAMES_MANAGER (manager), NULL);
g_return_val_if_fail (id != NULL, NULL);
for (iter = manager->sections; iter != NULL; iter = g_list_next (iter)) {
ESelectNamesManagerSection *section = iter->data;
if (!strcmp(section->id, id)) {
ESelectNamesManagerEntry *entry;
entry = e_select_names_manager_entry_new (manager, section->model, section->id);
manager->entries = g_list_append (manager->entries, entry);
return GTK_WIDGET(entry->entry);
}
}
return NULL;
}
static void
e_select_names_clicked(ESelectNames *dialog, gint button, ESelectNamesManager *manager)
{
gnome_dialog_close(GNOME_DIALOG(dialog));
switch(button) {
case 0:
e_select_names_manager_discard_saved_models (manager);
g_signal_emit (manager, e_select_names_manager_signals[OK], 0);
break;
case 1:
e_select_names_manager_revert_to_saved_models (manager);
g_signal_emit (manager, e_select_names_manager_signals[CANCEL], 0);
break;
}
}
void
e_select_names_manager_activate_dialog (ESelectNamesManager *manager,
const char *id)
{
g_return_if_fail (E_IS_SELECT_NAMES_MANAGER (manager));
g_return_if_fail (id != NULL);
if (manager->names) {
g_assert (GTK_WIDGET_REALIZED (GTK_WIDGET (manager->names)));
e_select_names_set_default (manager->names, id);
gdk_window_show (GTK_WIDGET (manager->names)->window);
gdk_window_raise (GTK_WIDGET (manager->names)->window);
} else {
GList *iter;
manager->names = E_SELECT_NAMES (e_select_names_new ());
for (iter = manager->sections; iter != NULL; iter = g_list_next (iter)) {
ESelectNamesManagerSection *section = iter->data;
e_select_names_add_section (manager->names, section->id, section->title, section->model);
}
e_select_names_set_default (manager->names, id);
g_signal_connect(manager->names,
"clicked",
G_CALLBACK(e_select_names_clicked),
manager);
g_signal_connect(manager->names,
"destroy",
G_CALLBACK(gtk_widget_destroyed),
&manager->names);
gtk_widget_show(GTK_WIDGET(manager->names));
}
e_select_names_manager_save_models (manager);
}
/* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** */
static void
e_select_names_manager_init (ESelectNamesManager *manager)
{
manager->sections = NULL;
manager->entries = NULL;
manager->completion_books = NULL;
manager->cached_folder_list = NULL;
}
static void
e_select_names_manager_dispose (GObject *object)
{
ESelectNamesManager *manager;
manager = E_SELECT_NAMES_MANAGER (object);
if (manager->names) {
gtk_widget_destroy (GTK_WIDGET (manager->names));
manager->names = NULL;
}
if (manager->sections) {
g_list_foreach (manager->sections, (GFunc) e_select_names_manager_section_free, NULL);
g_list_free (manager->sections);
manager->sections = NULL;
}
if (manager->entries) {
g_list_foreach (manager->entries, (GFunc) e_select_names_manager_entry_free, NULL);
g_list_free (manager->entries);
manager->entries = NULL;
}
if (manager->completion_books) {
g_list_foreach (manager->completion_books, (GFunc) g_object_unref, NULL);
g_list_free (manager->completion_books);
manager->completion_books = NULL;
}
if (manager->listener_id) {
g_signal_handler_disconnect (e_book_get_config_database(), manager->listener_id);
manager->listener_id = 0;
}
if (manager->cached_folder_list) {
g_free (manager->cached_folder_list);
manager->cached_folder_list = NULL;
}
if (G_OBJECT_CLASS (parent_class)->dispose)
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void
e_select_names_manager_class_init (ESelectNamesManagerClass *klass)
{
GObjectClass *object_class;
object_class = G_OBJECT_CLASS(klass);
parent_class = g_type_class_peek_parent (klass);
object_class->dispose = e_select_names_manager_dispose;
e_select_names_manager_signals[CHANGED] =
g_signal_new ("changed",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ESelectNamesManagerClass, changed),
NULL, NULL,
e_select_names_marshal_NONE__POINTER_INT,
G_TYPE_NONE, 2,
G_TYPE_POINTER,
G_TYPE_INT);
e_select_names_manager_signals[OK] =
g_signal_new ("ok",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ESelectNamesManagerClass, ok),
NULL, NULL,
e_select_names_marshal_NONE__NONE,
G_TYPE_NONE, 0);
e_select_names_manager_signals[CANCEL] =
g_signal_new ("cancel",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ESelectNamesManagerClass, cancel),
NULL, NULL,
e_select_names_marshal_NONE__NONE,
G_TYPE_NONE, 0);
}
/**
* e_select_names_manager_get_type:
* @void:
*
* Registers the &ESelectNamesManager class if necessary, and returns the type ID
* associated to it.
*
* Return value: The type ID of the &ESelectNamesManager class.
**/
GType
e_select_names_manager_get_type (void)
{
static GType manager_type = 0;
if (!manager_type) {
static const GTypeInfo manager_info = {
sizeof (ESelectNamesManagerClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) e_select_names_manager_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (ESelectNamesManager),
0, /* n_preallocs */
(GInstanceInitFunc) e_select_names_manager_init,
};
manager_type = g_type_register_static (G_TYPE_OBJECT, "ESelectNamesManager", &manager_info, 0);
}
return manager_type;
}
|