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

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>

#include "e-select-names-model.h"
#include "e-util/e-util.h"
#include "addressbook/backend/ebook/e-card-simple.h"

enum {
    E_SELECT_NAMES_MODEL_CHANGED,
    E_SELECT_NAMES_MODEL_LAST_SIGNAL
};

static guint e_select_names_model_signals[E_SELECT_NAMES_MODEL_LAST_SIGNAL] = { 0 };

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

static void e_select_names_model_init (ESelectNamesModel *model);
static void e_select_names_model_class_init (ESelectNamesModelClass *klass);

static void e_select_names_model_destroy (GtkObject *object);
static void e_select_names_model_set_arg (GtkObject *object, GtkArg *arg, guint arg_id);
static void e_select_names_model_get_arg (GtkObject *object, GtkArg *arg, guint arg_id);

/**
 * e_select_names_model_get_type:
 * @void: 
 * 
 * Registers the &ESelectNamesModel class if necessary, and returns the type ID
 * associated to it.
 * 
 * Return value: The type ID of the &ESelectNamesModel class.
 **/
GtkType
e_select_names_model_get_type (void)
{
    static GtkType model_type = 0;

    if (!model_type) {
        GtkTypeInfo model_info = {
            "ESelectNamesModel",
            sizeof (ESelectNamesModel),
            sizeof (ESelectNamesModelClass),
            (GtkClassInitFunc) e_select_names_model_class_init,
            (GtkObjectInitFunc) e_select_names_model_init,
            NULL, /* reserved_1 */
            NULL, /* reserved_2 */
            (GtkClassInitFunc) NULL
        };

        model_type = gtk_type_unique (gtk_object_get_type (), &model_info);
    }

    return model_type;
}

/**
 * e_select_names_model_new:
 * @VCard: a string in vCard format
 *
 * Returns: a new #ESelectNamesModel that wraps the @VCard.
 */
ESelectNamesModel *
e_select_names_model_new (void)
{
    ESelectNamesModel *model = E_SELECT_NAMES_MODEL(gtk_type_new(e_select_names_model_get_type()));
    return model;
}

/**
 * e_select_names_model_new:
 * @VCard: a string in vCard format
 *
 * Returns: a new #ESelectNamesModel that wraps the @VCard.
 */
ESelectNamesModel *
e_select_names_model_duplicate (ESelectNamesModel *old)
{
    ESelectNamesModel *model = E_SELECT_NAMES_MODEL(gtk_type_new(e_select_names_model_get_type()));
    model->data = e_list_duplicate(old->data);
    model->id = g_strdup(old->id);
    model->title = g_strdup(old->title);
    return model;
}

static void
e_select_names_model_class_init (ESelectNamesModelClass *klass)
{
    GtkObjectClass *object_class;

    object_class = GTK_OBJECT_CLASS(klass);

    e_select_names_model_signals[E_SELECT_NAMES_MODEL_CHANGED] =
        gtk_signal_new ("changed",
                GTK_RUN_LAST,
                object_class->type,
                GTK_SIGNAL_OFFSET (ESelectNamesModelClass, changed),
                gtk_marshal_NONE__NONE,
                GTK_TYPE_NONE, 0);

    gtk_object_class_add_signals (object_class, e_select_names_model_signals, E_SELECT_NAMES_MODEL_LAST_SIGNAL);

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

    klass->changed = NULL;

    object_class->destroy = e_select_names_model_destroy;
    object_class->get_arg = e_select_names_model_get_arg;
    object_class->set_arg = e_select_names_model_set_arg;
}

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

static void
e_select_names_model_destroy (GtkObject *object)
{
    ESelectNamesModel *model;
    
    model = E_SELECT_NAMES_MODEL (object);

    gtk_object_unref(GTK_OBJECT(model->data));
}


/* Set_arg handler for the model */
static void
e_select_names_model_set_arg (GtkObject *object, GtkArg *arg, guint arg_id)
{
    ESelectNamesModel *model;
    
    model = E_SELECT_NAMES_MODEL (object);

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

/* Get_arg handler for the model */
static void
e_select_names_model_get_arg (GtkObject *object, GtkArg *arg, guint arg_id)
{
    ESelectNamesModel *model;

    model = E_SELECT_NAMES_MODEL (object);

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

static void *
data_copy(const void *sec, void *data)
{
    const ESelectNamesModelData *section = sec;
    ESelectNamesModelData *newsec;
    
    newsec = g_new(ESelectNamesModelData, 1);
    newsec->type = section->type;
    newsec->card = section->card;
    if (newsec->card)
        gtk_object_ref(GTK_OBJECT(newsec->card));
    newsec->string = g_strdup(section->string);
    return newsec;
}

static void
data_free(void *sec, void *data)
{
    ESelectNamesModelData *section = sec;
    if (section->card)
        gtk_object_unref(GTK_OBJECT(section->card));
    g_free(section->string);
    g_free(section);
}

/**
 * e_select_names_model_init:
 */
static void
e_select_names_model_init (ESelectNamesModel *model)
{
    model->data = e_list_new(data_copy, data_free, model);
}

static void *
copy_func(const void *data, void *user_data)
{
    GtkObject *object = (void *) data;
    if (object)
        gtk_object_ref(object);
    return object;
}

static void
free_func(void *data, void *user_data)
{
    GtkObject *object = data;
    if (object)
        gtk_object_unref(object);
}

/* Of type ECard */
EList                    *e_select_names_model_get_cards                 (ESelectNamesModel *model)
{
    EList *list = e_list_new(copy_func, free_func, NULL);
    EIterator *iterator = e_list_get_iterator(model->data);
    EIterator *new_iterator = e_list_get_iterator(list);

    for (e_iterator_reset(iterator); e_iterator_is_valid(iterator); e_iterator_next(iterator)) {
        ESelectNamesModelData *node = (void *) e_iterator_get(iterator);
        ECard *card;
        ECardSimple *simple;
        if (node->card) {
            card = node->card;
            gtk_object_ref(GTK_OBJECT(card));
        } else {
            card = e_card_new("");
        }
        simple = e_card_simple_new(card);
        e_card_simple_set_arbitrary(simple, "text_version", "string", node->string);
        e_iterator_insert(new_iterator, card, FALSE);
        gtk_object_unref(GTK_OBJECT(card));
        gtk_object_unref(GTK_OBJECT(simple));
    }
    return list;
}

EList                    *e_select_names_model_get_data                 (ESelectNamesModel *model)
{
    return model->data;
}

static void
e_select_names_model_changed           (ESelectNamesModel *model)
{
    gtk_signal_emit(GTK_OBJECT(model),
            e_select_names_model_signals[E_SELECT_NAMES_MODEL_CHANGED]);
}

void
e_select_names_model_insert            (ESelectNamesModel *model,
                    EIterator *iterator, /* Must be one of the iterators in the model, or NULL if the list is empty. */
                    int index,
                    char *data)
{
    gchar **strings = e_strsplit(data, ",", -1);
    int i;
    if (iterator == NULL) {
        ESelectNamesModelData new = {E_SELECT_NAMES_MODEL_DATA_TYPE_STRING_ADDRESS, NULL, ""};

        e_list_append(model->data, &new);
        iterator = e_list_get_iterator(model->data);

        index = 0;
    } else {
        gtk_object_ref(GTK_OBJECT(iterator));
    }
    if (strings[0]) {
        ESelectNamesModelData *node = (void *) e_iterator_get(iterator);
        gchar *temp = g_strdup_printf("%.*s%s%s", index, node->string, strings[0], node->string + index);
        g_free(node->string);
        node->string = temp;
    }
    for (i = 1; strings[i]; i++) {
        ESelectNamesModelData *node = (void *) e_iterator_get(iterator);
        gchar *temp = g_strdup_printf("%.*s", index, node->string);
        gchar *temp2 = g_strdup_printf("%s%s", strings[0], node->string + index);

        g_free(node->string);
        node->type = E_SELECT_NAMES_MODEL_DATA_TYPE_STRING_ADDRESS;
        node->string = temp;
        if (node->card)
            gtk_object_unref(GTK_OBJECT(node->card));
        node->card = NULL;

        node = g_new(ESelectNamesModelData, 1);
        node->type = E_SELECT_NAMES_MODEL_DATA_TYPE_STRING_ADDRESS;
        node->card = NULL;
        node->string = temp2;
        e_iterator_insert(iterator, node, 0);
        g_free(node->string);
        g_free(node);
    }
    e_select_names_model_changed(model);
    gtk_object_unref(GTK_OBJECT(iterator));
}

void
e_select_names_model_insert_length     (ESelectNamesModel *model,
                    EIterator *iterator, /* Must be one of the iterators in the model. */
                    int index,
                    char *data,
                    int length)
{
    gchar *string = g_new(char, length + 1);
    strncpy(string, data, length);
    string[length] = 0;
    e_select_names_model_insert(model, iterator, index, string);
    g_free(string);
}

void
e_select_names_model_delete            (ESelectNamesModel *model,
                    EIterator *iterator, /* Must be one of the iterators in the model. */
                    int index,
                    int length)
{
    while (length > 0 && e_iterator_is_valid(iterator)) {
        ESelectNamesModelData *node = (void *) e_iterator_get(iterator);
        int this_length = strlen(node->string);
        if (this_length <= index + length) {
            gchar *temp = g_strdup_printf("%.*s", index, node->string);
            g_free(node->string);
            node->string = temp;
            length -= this_length - index;
        } else {
            gchar *temp = g_strdup_printf("%.*s%s", index, node->string, node->string + index + length);
            g_free(node->string);
            node->string = temp;
            break;
        }
        
        if (length > 0) {
            e_iterator_next(iterator);
            if (e_iterator_is_valid(iterator)) {
                ESelectNamesModelData *node2 = (void *) e_iterator_get(iterator);
                gchar *temp = g_strdup_printf("%s%s", node->string, node2->string);
                g_free(node2->string);
                node2->string = temp;
                e_iterator_prev(iterator);
                e_iterator_delete(iterator);
                length --;
            }
        }
    }
    e_select_names_model_changed(model);
}

void
e_select_names_model_replace           (ESelectNamesModel *model,
                    EIterator *iterator, /* Must be one of the iterators in the model. */
                    int index,
                    int length,
                    char *data)
{
    if (iterator == NULL) {
        ESelectNamesModelData new = {E_SELECT_NAMES_MODEL_DATA_TYPE_STRING_ADDRESS, NULL, ""};

        e_list_append(model->data, &new);
        iterator = e_list_get_iterator(model->data);

        index = 0;
    } else {
        gtk_object_ref(GTK_OBJECT(iterator));
    }
    while (length > 0 && e_iterator_is_valid(iterator)) {
        ESelectNamesModelData *node = (void *) e_iterator_get(iterator);
        int this_length = strlen(node->string);
        if (this_length <= index + length) {
            gchar *temp = g_strdup_printf("%.*s", index, node->string);
            g_free(node->string);
            node->string = temp;
            length -= this_length - index;
        } else {
            gchar *temp = g_strdup_printf("%.*s%s", index, node->string, node->string + index + length);
            g_free(node->string);
            node->string = temp;
            length = 0;
        }

        if (length > 0) {
            e_iterator_next(iterator);
            if (e_iterator_is_valid(iterator)) {
                ESelectNamesModelData *node2 = (void *) e_iterator_get(iterator);
                gchar *temp = g_strdup_printf("%s%s", node->string, node2->string);
                g_free(node2->string);
                node2->string = temp;
                e_iterator_prev(iterator);
                e_iterator_delete(iterator);
            }
        }
    }
    if (!e_iterator_is_valid(iterator)) {
        ESelectNamesModelData *node;
        e_iterator_last(iterator);
        if (e_iterator_is_valid(iterator)) {
            node = (void *) e_iterator_get(iterator);
            index = strlen(node->string);
        } else
            index = 0;
    }
    e_select_names_model_insert (model, iterator, index, data);
    gtk_object_unref(GTK_OBJECT(iterator));
}


void
e_select_names_model_add_item          (ESelectNamesModel *model,
                    EIterator *iterator, /* NULL for at the beginning. */
                    ESelectNamesModelData *data)
{
    e_iterator_insert(iterator, data, FALSE);
    e_select_names_model_changed(model);
}

void
e_select_names_model_remove_item       (ESelectNamesModel *model,
                    EIterator *iterator)
{
    e_iterator_delete(iterator);
    e_select_names_model_changed(model);
}