aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-name-selector-model.c
blob: 5b3690d6e4aebde6b6722b318e0fd74f519f8069 (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
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/* e-name-selector-model.c - Model for contact selection.
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser 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 Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Hans Petter Jansson <hpj@novell.com>
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <string.h>
#include <glib/gi18n-lib.h>
#include "e-name-selector-model.h"

#define E_NAME_SELECTOR_MODEL_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_NAME_SELECTOR_MODEL, ENameSelectorModelPrivate))

typedef struct {
    gchar              *name;
    gchar              *pretty_name;

    EDestinationStore  *destination_store;
}
Section;

struct _ENameSelectorModelPrivate {
    GArray *sections;
    EContactStore *contact_store;
    ETreeModelGenerator *contact_filter;
    GHashTable *destination_uid_hash;
};

static gint generate_contact_rows  (EContactStore *contact_store, GtkTreeIter *iter,
                    ENameSelectorModel *name_selector_model);
static void override_email_address (EContactStore *contact_store, GtkTreeIter *iter,
                    gint permutation_n, gint column, GValue *value,
                    ENameSelectorModel *name_selector_model);
static void free_section           (ENameSelectorModel *name_selector_model, gint n);

/* ------------------ *
 * Class/object setup *
 * ------------------ */

/* Signals */

enum {
    SECTION_ADDED,
    SECTION_REMOVED,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE (ENameSelectorModel, e_name_selector_model, G_TYPE_OBJECT)

static void
e_name_selector_model_init (ENameSelectorModel *name_selector_model)
{
    name_selector_model->priv =
        E_NAME_SELECTOR_MODEL_GET_PRIVATE (name_selector_model);

    name_selector_model->priv->sections = g_array_new (FALSE, FALSE, sizeof (Section));
    name_selector_model->priv->contact_store = e_contact_store_new ();

    name_selector_model->priv->contact_filter =
        e_tree_model_generator_new (GTK_TREE_MODEL (name_selector_model->priv->contact_store));
    e_tree_model_generator_set_generate_func (
        name_selector_model->priv->contact_filter,
        (ETreeModelGeneratorGenerateFunc) generate_contact_rows,
        name_selector_model, NULL);
    e_tree_model_generator_set_modify_func (name_selector_model->priv->contact_filter,
                        (ETreeModelGeneratorModifyFunc) override_email_address,
                        name_selector_model, NULL);

    g_object_unref (name_selector_model->priv->contact_store);

    name_selector_model->priv->destination_uid_hash = NULL;
}

static void
name_selector_model_finalize (GObject *object)
{
    ENameSelectorModelPrivate *priv;
    gint i;

    priv = E_NAME_SELECTOR_MODEL_GET_PRIVATE (object);

    for (i = 0; i < priv->sections->len; i++)
        free_section (E_NAME_SELECTOR_MODEL (object), i);

    g_array_free (priv->sections, TRUE);
    g_object_unref (priv->contact_filter);

    if (priv->destination_uid_hash)
        g_hash_table_destroy (priv->destination_uid_hash);

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (e_name_selector_model_parent_class)->finalize (object);
}

static void
e_name_selector_model_class_init (ENameSelectorModelClass *class)
{
    GObjectClass *object_class;

    g_type_class_add_private (class, sizeof (ENameSelectorModelPrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->finalize = name_selector_model_finalize;

    signals[SECTION_ADDED] = g_signal_new (
        "section-added",
        G_OBJECT_CLASS_TYPE (object_class),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (ENameSelectorModelClass, section_added),
        NULL, NULL,
        g_cclosure_marshal_VOID__STRING,
        G_TYPE_NONE, 1,
        G_TYPE_STRING);

    signals[SECTION_REMOVED] = g_signal_new (
        "section-removed",
        G_OBJECT_CLASS_TYPE (object_class),
        G_SIGNAL_RUN_LAST,
        G_STRUCT_OFFSET (ENameSelectorModelClass, section_removed),
        NULL, NULL,
        g_cclosure_marshal_VOID__STRING,
        G_TYPE_NONE, 1,
        G_TYPE_STRING);
}

/**
 * e_name_selector_model_new:
 *
 * Creates a new #ENameSelectorModel.
 *
 * Returns: A new #ENameSelectorModel.
 **/
ENameSelectorModel *
e_name_selector_model_new (void)
{
    return E_NAME_SELECTOR_MODEL (g_object_new (E_TYPE_NAME_SELECTOR_MODEL, NULL));
}

/* ---------------------------- *
 * GtkTreeModelFilter filtering *
 * ---------------------------- */

static void
deep_free_list (GList *list)
{
    GList *l;

    for (l = list; l; l = g_list_next (l))
        g_free (l->data);

    g_list_free (list);
}

static gint
generate_contact_rows (EContactStore *contact_store,
                       GtkTreeIter *iter,
                       ENameSelectorModel *name_selector_model)
{
    EContact    *contact;
    const gchar *contact_uid;
    gint         n_rows, used_rows = 0;
    gint         i;

    contact = e_contact_store_get_contact (contact_store, iter);
    g_assert (contact != NULL);

    contact_uid = e_contact_get_const (contact, E_CONTACT_UID);
    if (!contact_uid)
        return 0;  /* Can happen with broken databases */

    for (i = 0; i < name_selector_model->priv->sections->len; i++) {
        Section *section;
        GList   *destinations;
        GList   *l;

        section = &g_array_index (name_selector_model->priv->sections, Section, i);
        destinations = e_destination_store_list_destinations (section->destination_store);

        for (l = destinations; l; l = g_list_next (l)) {
            EDestination *destination = l->data;
            const gchar  *destination_uid;

            destination_uid = e_destination_get_contact_uid (destination);
            if (destination_uid && !strcmp (contact_uid, destination_uid)) {
                used_rows++;
            }
        }

        g_list_free (destinations);
    }

    if (e_contact_get (contact, E_CONTACT_IS_LIST)) {
        n_rows = 1 - used_rows;
    } else {
        GList    *email_list;

        email_list = e_contact_get (contact, E_CONTACT_EMAIL);
        n_rows = g_list_length (email_list) - used_rows;
        deep_free_list (email_list);
    }

    g_return_val_if_fail (n_rows >= 0, 0);

    return n_rows;
}

static void
override_email_address (EContactStore *contact_store,
                        GtkTreeIter *iter,
                        gint permutation_n,
                        gint column,
                        GValue *value,
                        ENameSelectorModel *name_selector_model)
{
    if (column == E_CONTACT_EMAIL_1) {
        EContact *contact;
        GList    *email_list;
        gchar    *email;

        contact = e_contact_store_get_contact (contact_store, iter);
        email_list = e_name_selector_model_get_contact_emails_without_used (name_selector_model, contact, TRUE);
        g_return_if_fail (g_list_length (email_list) <= permutation_n);
        email = g_strdup (g_list_nth_data (email_list, permutation_n));
        g_value_set_string (value, email);
        e_name_selector_model_free_emails_list (email_list);
    } else {
        gtk_tree_model_get_value (GTK_TREE_MODEL (contact_store), iter, column, value);
    }
}

/* --------------- *
 * Section helpers *
 * --------------- */

typedef struct
{
    ENameSelectorModel *name_selector_model;
    GHashTable         *other_hash;
}
HashCompare;

static void
emit_destination_uid_changes_cb (gchar *uid_num,
                                 gpointer value,
                                 HashCompare *hash_compare)
{
    EContactStore *contact_store = hash_compare->name_selector_model->priv->contact_store;

    if (!hash_compare->other_hash || !g_hash_table_lookup (hash_compare->other_hash, uid_num)) {
        GtkTreeIter  iter;
        GtkTreePath *path;
        gchar *sep;

        sep = strrchr (uid_num, ':');
        g_return_if_fail (sep != NULL);

        *sep = '\0';
        if (e_contact_store_find_contact (contact_store, uid_num, &iter)) {
            *sep = ':';

            path = gtk_tree_model_get_path (GTK_TREE_MODEL (contact_store), &iter);
            gtk_tree_model_row_changed (GTK_TREE_MODEL (contact_store), path, &iter);
            gtk_tree_path_free (path);
        } else {
            *sep = ':';
        }
    }
}

static void
destinations_changed (ENameSelectorModel *name_selector_model)
{
    GHashTable  *destination_uid_hash_new;
    GHashTable  *destination_uid_hash_old;
    HashCompare  hash_compare;
    gint         i;

    destination_uid_hash_new = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);

    for (i = 0; i < name_selector_model->priv->sections->len; i++) {
        Section *section = &g_array_index (name_selector_model->priv->sections, Section, i);
        GList   *destinations;
        GList   *l;

        destinations = e_destination_store_list_destinations (section->destination_store);

        for (l = destinations; l; l = g_list_next (l)) {
            EDestination *destination = l->data;
            const gchar  *destination_uid;

            destination_uid = e_destination_get_contact_uid (destination);
            if (destination_uid)
                g_hash_table_insert (
                    destination_uid_hash_new,
                    g_strdup_printf (
                        "%s:%d", destination_uid,
                        e_destination_get_email_num (destination)),
                    GINT_TO_POINTER (TRUE));
        }

        g_list_free (destinations);
    }

    destination_uid_hash_old = name_selector_model->priv->destination_uid_hash;
    name_selector_model->priv->destination_uid_hash = destination_uid_hash_new;

    hash_compare.name_selector_model = name_selector_model;

    hash_compare.other_hash = destination_uid_hash_old;
    g_hash_table_foreach (
        destination_uid_hash_new,
        (GHFunc) emit_destination_uid_changes_cb,
        &hash_compare);

    if (destination_uid_hash_old) {
        hash_compare.other_hash = destination_uid_hash_new;
        g_hash_table_foreach (
            destination_uid_hash_old,
            (GHFunc) emit_destination_uid_changes_cb,
            &hash_compare);

        g_hash_table_destroy (destination_uid_hash_old);
    }
}

static void
free_section (ENameSelectorModel *name_selector_model,
              gint n)
{
    Section *section;

    g_assert (n >= 0);
    g_assert (n < name_selector_model->priv->sections->len);

    section = &g_array_index (name_selector_model->priv->sections, Section, n);

    g_signal_handlers_disconnect_matched (
        section->destination_store, G_SIGNAL_MATCH_DATA,
        0, 0, NULL, NULL, name_selector_model);

    g_free (section->name);
    g_free (section->pretty_name);
    g_object_unref (section->destination_store);
}

static gint
find_section_by_name (ENameSelectorModel *name_selector_model,
                      const gchar *name)
{
    gint i;

    g_assert (name != NULL);

    for (i = 0; i < name_selector_model->priv->sections->len; i++) {
        Section *section = &g_array_index (name_selector_model->priv->sections, Section, i);

        if (!strcmp (name, section->name))
            return i;
    }

    return -1;
}

/* ---------------------- *
 * ENameSelectorModel API *
 * ---------------------- */

/**
 * e_name_selector_model_peek_contact_store:
 * @name_selector_model: an #ENameSelectorModel
 *
 * Gets the #EContactStore associated with @name_selector_model.
 *
 * Returns: An #EContactStore.
 **/
EContactStore *
e_name_selector_model_peek_contact_store (ENameSelectorModel *name_selector_model)
{
    g_return_val_if_fail (E_IS_NAME_SELECTOR_MODEL (name_selector_model), NULL);

    return name_selector_model->priv->contact_store;
}

/**
 * e_name_selector_model_peek_contact_filter:
 * @name_selector_model: an #ENameSelectorModel
 *
 * Gets the #ETreeModelGenerator being used to filter and/or extend the
 * list of contacts in @name_selector_model's #EContactStore.
 *
 * Returns: An #ETreeModelGenerator.
 **/
ETreeModelGenerator *
e_name_selector_model_peek_contact_filter (ENameSelectorModel *name_selector_model)
{
    g_return_val_if_fail (E_IS_NAME_SELECTOR_MODEL (name_selector_model), NULL);

    return name_selector_model->priv->contact_filter;
}

/**
 * e_name_selector_model_list_sections:
 * @name_selector_model: an #ENameSelectorModel
 *
 * Gets a list of the destination sections in @name_selector_model.
 *
 * Returns: A #GList of pointers to strings. The #GList and the
 * strings belong to the caller, and must be freed when no longer needed.
 **/
GList *
e_name_selector_model_list_sections (ENameSelectorModel *name_selector_model)
{
    GList *section_names = NULL;
    gint   i;

    g_return_val_if_fail (E_IS_NAME_SELECTOR_MODEL (name_selector_model), NULL);

    /* Do this backwards so we can use g_list_prepend () and get correct order */
    for (i = name_selector_model->priv->sections->len - 1; i >= 0; i--) {
        Section *section = &g_array_index (name_selector_model->priv->sections, Section, i);
        gchar   *name;

        name = g_strdup (section->name);
        section_names = g_list_prepend (section_names, name);
    }

    return section_names;
}

/**
 * e_name_selector_model_add_section:
 * @name_selector_model: an #ENameSelectorModel
 * @name: internal name of this section
 * @pretty_name: user-visible name of this section
 * @destination_store: the #EDestinationStore to use to store the destinations for this
 * section, or %NULL if @name_selector_model should create its own.
 *
 * Adds a destination section to @name_selector_model.
 **/
void
e_name_selector_model_add_section (ENameSelectorModel *name_selector_model,
                                   const gchar *name,
                                   const gchar *pretty_name,
                                   EDestinationStore *destination_store)
{
    Section section;

    g_return_if_fail (E_IS_NAME_SELECTOR_MODEL (name_selector_model));
    g_return_if_fail (name != NULL);
    g_return_if_fail (pretty_name != NULL);

    if (find_section_by_name (name_selector_model, name) >= 0) {
        g_warning ("ENameSelectorModel already has a section called '%s'!", name);
        return;
    }

    memset (&section, 0, sizeof (Section));

    section.name = g_strdup (name);
    section.pretty_name = g_strdup (pretty_name);

    if (destination_store)
        section.destination_store = g_object_ref (destination_store);
    else
        section.destination_store = e_destination_store_new ();

    g_signal_connect_swapped (
        section.destination_store, "row-changed",
        G_CALLBACK (destinations_changed), name_selector_model);
    g_signal_connect_swapped (
        section.destination_store, "row-deleted",
        G_CALLBACK (destinations_changed), name_selector_model);
    g_signal_connect_swapped (
        section.destination_store, "row-inserted",
        G_CALLBACK (destinations_changed), name_selector_model);

    g_array_append_val (name_selector_model->priv->sections, section);

    destinations_changed (name_selector_model);
    g_signal_emit (name_selector_model, signals[SECTION_ADDED], 0, name);
}

/**
 * e_name_selector_model_remove_section:
 * @name_selector_model: an #ENameSelectorModel
 * @name: internal name of the section to remove
 *
 * Removes a destination section from @name_selector_model.
 **/
void
e_name_selector_model_remove_section (ENameSelectorModel *name_selector_model,
                                      const gchar *name)
{
    gint     n;

    g_return_if_fail (E_IS_NAME_SELECTOR_MODEL (name_selector_model));
    g_return_if_fail (name != NULL);

    n = find_section_by_name (name_selector_model, name);
    if (n < 0) {
        g_warning ("ENameSelectorModel does not have a section called '%s'!", name);
        return;
    }

    free_section (name_selector_model, n);
    g_array_remove_index_fast (name_selector_model->priv->sections, n);  /* Order doesn't matter */

    destinations_changed (name_selector_model);
    g_signal_emit (name_selector_model, signals[SECTION_REMOVED], 0, name);
}

/**
 * e_name_selector_model_peek_section:
 * @name_selector_model: an #ENameSelectorModel
 * @name: internal name of the section to peek
 * @pretty_name: location in which to store a pointer to the user-visible name of the section,
 * or %NULL if undesired.
 * @destination_store: location in which to store a pointer to the #EDestinationStore being used
 * by the section, or %NULL if undesired
 *
 * Gets the parameters for a destination section.
 **/
gboolean
e_name_selector_model_peek_section (ENameSelectorModel *name_selector_model,
                                    const gchar *name,
                                    gchar **pretty_name,
                                    EDestinationStore **destination_store)
{
    Section *section;
    gint     n;

    g_return_val_if_fail (E_IS_NAME_SELECTOR_MODEL (name_selector_model), FALSE);
    g_return_val_if_fail (name != NULL, FALSE);

    n = find_section_by_name (name_selector_model, name);
    if (n < 0) {
        g_warning ("ENameSelectorModel does not have a section called '%s'!", name);
        return FALSE;
    }

    section = &g_array_index (name_selector_model->priv->sections, Section, n);

    if (pretty_name)
        *pretty_name = g_strdup (section->pretty_name);
    if (destination_store)
        *destination_store = section->destination_store;

    return TRUE;
}

/**
 * e_name_selector_model_get_contact_emails_without_used:
 * @name_selector_model: an #ENameSelectorModel
 * @contact: to get emails from
 * @remove_used: set to %TRUE to remove used from a list; or set to %FALSE to
 * set used indexes to %NULL and keep them in the returned list
 *
 * Returns list of all email from @contact, without all used
 * in any section. Each item is a string, an email address.
 * Returned list should be freed with @e_name_selector_model_free_emails_list.
 *
 * Since: 2.30
 **/
GList *
e_name_selector_model_get_contact_emails_without_used (ENameSelectorModel *name_selector_model,
                                                       EContact *contact,
                                                       gboolean remove_used)
{
    GList *email_list;
    gint emails;
    gint i;
    const gchar *contact_uid;

    g_return_val_if_fail (name_selector_model != NULL, NULL);
    g_return_val_if_fail (E_IS_NAME_SELECTOR_MODEL (name_selector_model), NULL);
    g_return_val_if_fail (contact != NULL, NULL);
    g_return_val_if_fail (E_IS_CONTACT (contact), NULL);

    contact_uid = e_contact_get_const (contact, E_CONTACT_UID);
    g_return_val_if_fail (contact_uid != NULL, NULL);

    email_list = e_contact_get (contact, E_CONTACT_EMAIL);
    emails = g_list_length (email_list);

    for (i = 0; i < name_selector_model->priv->sections->len; i++) {
        Section *section;
        GList   *destinations;
        GList   *l;

        section = &g_array_index (name_selector_model->priv->sections, Section, i);
        destinations = e_destination_store_list_destinations (section->destination_store);

        for (l = destinations; l; l = g_list_next (l)) {
            EDestination *destination = l->data;
            const gchar  *destination_uid;

            destination_uid = e_destination_get_contact_uid (destination);
            if (destination_uid && !strcmp (contact_uid, destination_uid)) {
                gint email_num = e_destination_get_email_num (destination);

                if (email_num < 0 || email_num >= emails) {
                    g_warning ("%s: Destination's email_num %d out of bounds 0..%d", G_STRFUNC, email_num, emails - 1);
                } else {
                    GList *nth = g_list_nth (email_list, email_num);

                    g_return_val_if_fail (nth != NULL, NULL);

                    g_free (nth->data);
                    nth->data = NULL;
                }
            }
        }

        g_list_free (destinations);
    }

    if (remove_used) {
        /* remove all with data NULL, which are those used already */
        do {
            emails = g_list_length (email_list);
            email_list = g_list_remove (email_list, NULL);
        } while (g_list_length (email_list) != emails);
    }

    return email_list;
}

/**
 * e_name_selector_model_free_emails_list:
 * @email_list: list of emails returned from @e_name_selector_model_get_contact_emails_without_used
 *
 * Frees a list of emails returned from @e_name_selector_model_get_contact_emails_without_used.
 *
 * Since: 2.30
 **/
void
e_name_selector_model_free_emails_list (GList *email_list)
{
    deep_free_list (email_list);
}