aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/component/select-names/e-select-names-manager.c
blob: b255942d084691c4a04fcea1786aec4b78c38367 (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
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Authors: 
 *   Chris Lahey     <clahey@ximian.com>
 *
 * Copyright (C) 2000 Ximian, Inc.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

#include "e-select-names-manager.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 <addressbook/backend/ebook/e-destination.h>

/* Object argument IDs */
enum {
    ARG_0,
    ARG_CARD,
};

enum {
    CHANGED,
    LAST_SIGNAL
};

static guint e_select_names_manager_signals[LAST_SIGNAL] = { 0 };


typedef struct {
    char *id;
    char *title;
    ESelectNamesModel *model;
    ESelectNamesManager *manager;
    guint changed_handler;
} ESelectNamesManagerSection;

typedef struct {
    char *id;
    EEntry *entry;
} 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_destroy (GtkObject *object);
static void e_select_names_manager_set_arg (GtkObject *object, GtkArg *arg, guint arg_id);
static void e_select_names_manager_get_arg (GtkObject *object, GtkArg *arg, guint arg_id);

/**
 * 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.
 **/
GtkType
e_select_names_manager_get_type (void)
{
    static GtkType manager_type = 0;

    if (!manager_type) {
        GtkTypeInfo manager_info = {
            "ESelectNamesManager",
            sizeof (ESelectNamesManager),
            sizeof (ESelectNamesManagerClass),
            (GtkClassInitFunc) e_select_names_manager_class_init,
            (GtkObjectInitFunc) e_select_names_manager_init,
            NULL, /* reserved_1 */
            NULL, /* reserved_2 */
            (GtkClassInitFunc) NULL
        };

        manager_type = gtk_type_unique (gtk_object_get_type (), &manager_info);
    }

    return manager_type;
}

/**
 * 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 = E_SELECT_NAMES_MANAGER(gtk_type_new(e_select_names_manager_get_type()));
    return manager;
}

static void
e_select_names_manager_class_init (ESelectNamesManagerClass *klass)
{
    GtkObjectClass *object_class;

    object_class = GTK_OBJECT_CLASS(klass);

    gtk_object_add_arg_type ("ESelectNamesManager::card",
                 GTK_TYPE_OBJECT, GTK_ARG_READWRITE, ARG_CARD);

    object_class->destroy = e_select_names_manager_destroy;
    object_class->get_arg = e_select_names_manager_get_arg;
    object_class->set_arg = e_select_names_manager_set_arg;

    e_select_names_manager_signals[CHANGED] = 
        gtk_signal_new ("changed",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (ESelectNamesManagerClass, changed),
                gtk_marshal_NONE__POINTER_INT,
                GTK_TYPE_NONE, 2,
                GTK_TYPE_POINTER,
                GTK_TYPE_INT);
    gtk_object_class_add_signals (object_class, e_select_names_manager_signals, LAST_SIGNAL);
}

/*
 * ESelectNamesManager lifecycle management and vcard loading/saving.
 */

static void
e_select_names_manager_destroy (GtkObject *object)
{
    ESelectNamesManager *manager;
    
    manager = E_SELECT_NAMES_MANAGER (object);

    gtk_object_unref(GTK_OBJECT(manager->sections));
    gtk_object_unref(GTK_OBJECT(manager->entries));
}


/* Set_arg handler for the manager */
static void
e_select_names_manager_set_arg (GtkObject *object, GtkArg *arg, guint arg_id)
{
    ESelectNamesManager *manager;
    
    manager = E_SELECT_NAMES_MANAGER (object);

    switch (arg_id) {
    case ARG_CARD:
        break;
    default:
        return;
    }
}

/* Get_arg handler for the manager */
static void
e_select_names_manager_get_arg (GtkObject *object, GtkArg *arg, guint arg_id)
{
    ESelectNamesManager *manager;

    manager = E_SELECT_NAMES_MANAGER (object);

    switch (arg_id) {
    case ARG_CARD:
        break;
    default:
        arg->type = GTK_TYPE_INVALID;
        break;
    }
}

static void *
section_copy(const void *sec, void *data)
{
    const ESelectNamesManagerSection *section = sec;
    ESelectNamesManagerSection *newsec;

    static void section_model_changed_cb (ESelectNamesModel *, gpointer);

    newsec = g_new(ESelectNamesManagerSection, 1);
    newsec->id = g_strdup(section->id);
    newsec->title = g_strdup(section->title);
    newsec->model = section->model;
    newsec->manager = section->manager;
    newsec->changed_handler = gtk_signal_connect (GTK_OBJECT (newsec->model),
                              "changed",
                              GTK_SIGNAL_FUNC (section_model_changed_cb),
                              newsec);

    if (newsec->model)
        gtk_object_ref(GTK_OBJECT(newsec->model));
    return newsec;
}

static void
section_free(void *sec, void *data)
{
    ESelectNamesManagerSection *section = sec;
    if (section->manager && section->changed_handler) {
        gtk_signal_disconnect (GTK_OBJECT (section->model), section->changed_handler);
    }
    g_free(section->id);
    g_free(section->title);
    if (section->model)
        gtk_object_unref(GTK_OBJECT(section->model));
    g_free(section);
}

static void *
entry_copy(const void *ent, void *data)
{
    const ESelectNamesManagerEntry *entry = ent;
    ESelectNamesManagerEntry *newent;
    
    newent = g_new(ESelectNamesManagerEntry, 1);
    newent->id = g_strdup(entry->id);
    newent->entry = entry->entry;
    if (newent->entry)
        gtk_object_ref(GTK_OBJECT(newent->entry));
    return newent;
}

static void
entry_free(void *ent, void *data)
{
    ESelectNamesManagerEntry *entry = ent;
    g_free(entry->id);
    if (entry->entry)
        gtk_object_unref(GTK_OBJECT(entry->entry));
    g_free(entry);
}

/**
 * e_select_names_manager_init:
 */
static void
e_select_names_manager_init (ESelectNamesManager *manager)
{
    manager->sections = e_list_new(section_copy, section_free, manager);
    manager->entries  = e_list_new(entry_copy, entry_free, manager);
}

static void
section_model_changed_cb (ESelectNamesModel *model, gpointer closure)
{
    ESelectNamesManagerSection *section = closure;
    gtk_signal_emit (GTK_OBJECT (section->manager),
             e_select_names_manager_signals[CHANGED],
             section->id,
             FALSE);
}

static void
section_model_working_copy_changed_cb (ESelectNamesModel *model, gpointer closure)
{
    ESelectNamesManagerSection *section = closure;
    gtk_signal_emit (GTK_OBJECT (section->manager),
             e_select_names_manager_signals[CHANGED],
             section->id,
             TRUE);
}

void
e_select_names_manager_add_section (ESelectNamesManager *manager,
                    const char *id,
                    const char *title)
{
    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;
    
    section = g_new(ESelectNamesManagerSection, 1);
    section->id = g_strdup(id);
    section->title = g_strdup(title);

    section->model = e_select_names_model_new();
    e_select_names_model_set_limit (section->model, limit);

    section->manager = manager;

    section->changed_handler = gtk_signal_connect (GTK_OBJECT (section->model),
                               "changed",
                               GTK_SIGNAL_FUNC (section_model_changed_cb),
                               section);

    e_list_append(manager->sections, section);
    section_free(section, manager);
}

ESelectNamesModel *
e_select_names_manager_get_source (ESelectNamesManager *manager, const char *id)
{
    EIterator *iterator;

    g_return_val_if_fail (manager && E_IS_SELECT_NAMES_MANAGER (manager), NULL);
    g_return_val_if_fail (id, NULL);

    iterator = e_list_get_iterator (manager->sections);
    for (e_iterator_reset (iterator); e_iterator_is_valid (iterator); e_iterator_next (iterator)) {
        const ESelectNamesManagerSection *section = e_iterator_get (iterator);
        if (!strcmp (section->id, id)) {
            return section->model;
        }
    }

    return NULL;
}

static void
entry_destroyed(EEntry *entry, ESelectNamesManager *manager)
{
    if(!GTK_OBJECT_DESTROYED(manager)) {
        EIterator *iterator = e_list_get_iterator(manager->entries);
        for (e_iterator_reset(iterator); e_iterator_is_valid(iterator); e_iterator_next(iterator)) {
            const ESelectNamesManagerEntry *this_entry = e_iterator_get(iterator);
            if(entry == this_entry->entry) {
                e_iterator_delete(iterator);
                break;
            }
        }
    }
    gtk_object_unref(GTK_OBJECT(manager));
}

static void
completion_handler (EEntry *entry, ECompletionMatch *match)
{
    ESelectNamesModel *snm;
    EDestination *dest;
    gint i, pos, start_pos, len;

    if (match == NULL || match->user_data == NULL)
        return;


    snm = E_SELECT_NAMES_MODEL (gtk_object_get_data (GTK_OBJECT (entry), "select_names_model"));
    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. */
    gtk_object_ref (GTK_OBJECT (dest));

    pos = e_entry_get_position (entry);
    e_select_names_model_text_pos (snm, pos, &i, NULL, NULL);
    e_select_names_model_replace (snm, i, dest);
    e_select_names_model_name_pos (snm, i, &start_pos, &len);
    e_entry_set_position (entry, start_pos+len);
}

static void
popup_cb (EEntry *entry, GdkEventButton *ev, gint pos, ESelectNamesModel *model)
{
    e_select_names_popup (model, ev, pos);
}

GtkWidget *
e_select_names_manager_create_entry (ESelectNamesManager *manager, const char *id)
{
    ETextModel *model;
    EIterator *iterator;
    iterator = e_list_get_iterator(manager->sections);
    for (e_iterator_reset(iterator); e_iterator_is_valid(iterator); e_iterator_next(iterator)) {
        const ESelectNamesManagerSection *section = e_iterator_get(iterator);
        if (!strcmp(section->id, id)) {
            ESelectNamesManagerEntry *entry;
            EEntry *eentry;
            ECompletion *comp;

            eentry = E_ENTRY (e_entry_new ());
            gtk_object_set_data (GTK_OBJECT (eentry), "select_names_model", section->model);

            gtk_signal_connect (GTK_OBJECT (eentry),
                        "popup",
                        GTK_SIGNAL_FUNC (popup_cb),
                        section->model);

            entry = g_new (ESelectNamesManagerEntry, 1);
            entry->entry = eentry;
            entry->id = (char *)id;

            model = e_select_names_text_model_new (section->model);
            e_list_append (manager->entries, entry);
            g_free(entry);

            comp = e_select_names_completion_new (NULL, section->model); /* NULL == use local addressbook */
            e_entry_enable_completion_full (eentry, comp, 50, completion_handler);

            gtk_object_set(GTK_OBJECT(eentry),
                       "model", model,
                       "editable", TRUE,
                       "use_ellipsis", TRUE,
                       "allow_newlines", FALSE,
                       NULL);
            gtk_signal_connect(GTK_OBJECT(eentry), "destroy",
                       GTK_SIGNAL_FUNC(entry_destroyed), manager);
            gtk_object_ref(GTK_OBJECT(manager));
            return GTK_WIDGET(eentry);
        }
    }
    return NULL;
}

static void
e_select_names_clicked(ESelectNames *dialog, gint button, ESelectNamesManager *manager)
{
    switch(button) {
    case 0: {
        EList *list = manager->sections;
        EIterator *iterator = e_list_get_iterator(list);
        for (e_iterator_reset(iterator); e_iterator_is_valid(iterator); e_iterator_next(iterator)) {
            ESelectNamesManagerSection *section = (void *) e_iterator_get(iterator);
            ESelectNamesModel *source = e_select_names_get_source(dialog, section->id);
            e_select_names_model_overwrite_copy (section->model, source);
            gtk_object_unref (GTK_OBJECT (source));
            
        }
        gtk_object_unref(GTK_OBJECT(iterator));

#if 0
        list = manager->entries;
        iterator = e_list_get_iterator(list);
        for (e_iterator_reset(iterator); e_iterator_is_valid(iterator); e_iterator_next(iterator)) {
            ESelectNamesManagerEntry *entry = (void *) e_iterator_get(iterator);
            ESelectNamesModel *source = e_select_names_get_source(dialog, entry->id);
            if (source) {
                ETextModel *model = e_select_names_text_model_new(source);
                if (model) {
                    gtk_object_set(GTK_OBJECT(entry->entry),
                               "model", model,
                               NULL);
                    gtk_object_unref(GTK_OBJECT(source));
                }
                gtk_object_unref(GTK_OBJECT(model));
            }
        }
        gtk_object_unref(GTK_OBJECT(iterator));
#endif
        break;
    }
    }
    gnome_dialog_close(GNOME_DIALOG(dialog));
}

void
e_select_names_manager_activate_dialog (ESelectNamesManager *manager,
                    const char *id)
{
    EIterator *iterator;
    
    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 {
        manager->names = E_SELECT_NAMES (e_select_names_new ());

        iterator = e_list_get_iterator(manager->sections);
        for (e_iterator_reset(iterator); e_iterator_is_valid(iterator); e_iterator_next(iterator)) {
            const ESelectNamesManagerSection *section = e_iterator_get(iterator);
            ESelectNamesModel *newmodel = e_select_names_model_duplicate(section->model);
            e_select_names_add_section(manager->names, section->id, section->title, newmodel);
            gtk_signal_connect (GTK_OBJECT (newmodel),
                        "changed",
                        GTK_SIGNAL_FUNC (section_model_working_copy_changed_cb),
                        (gpointer)section); /* casting out const to avoid compiler warning */
            gtk_object_unref(GTK_OBJECT(newmodel));
        }
        e_select_names_set_default(manager->names, id);
        gtk_signal_connect(GTK_OBJECT(manager->names), "clicked",
                   GTK_SIGNAL_FUNC(e_select_names_clicked), manager);
        gtk_signal_connect(GTK_OBJECT(manager->names), "destroy",
                   GTK_SIGNAL_FUNC(gtk_widget_destroyed), 
                   &manager->names);
        gtk_widget_show(GTK_WIDGET(manager->names));
    }
}

#if 0
/* Of type ECard */
EList *
e_select_names_manager_get_cards (ESelectNamesManager *manager,
                  const char *id)
{
    EIterator *iterator;
    iterator = e_list_get_iterator(manager->sections);
    for (e_iterator_reset(iterator); e_iterator_is_valid(iterator); e_iterator_next(iterator)) {
        const ESelectNamesManagerSection *section = e_iterator_get(iterator);
        if (!strcmp(section->id, id)) {
            return e_select_names_model_get_cards(section->model);
        }
    }
    return NULL;
}
#endif