aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/contact-list-editor/e-contact-list-model.c
blob: b94e8e79c345229006c955601f24afc197200065 (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
/*
 * 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; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

#include <string.h>

#include "e-contact-list-model.h"
#include "e-util/e-alert-dialog.h"
#include "shell/e-shell.h"

static gpointer parent_class;

G_DEFINE_TYPE (EContactListModel, e_contact_list_model, GTK_TYPE_TREE_STORE);

struct _EContactListModelPrivate {

    GHashTable *uids_table;
    GHashTable *emails_table;

};

static gboolean
contact_list_get_iter (EContactListModel *model,
                       GtkTreeIter *iter,
                       gint row)
{
    GtkTreePath *path;
    gboolean iter_valid;

    path = gtk_tree_path_new_from_indices (row, -1);
    iter_valid = gtk_tree_model_get_iter (
        GTK_TREE_MODEL (model), iter, path);
    gtk_tree_path_free (path);

    return iter_valid;
}

static GObject *
contact_list_model_constructor (GType type,
                                guint n_construct_properties,
                                GObjectConstructParam *construct_properties)
{
    GObject *object;
    GType types[1];

    types[0] = E_TYPE_DESTINATION;

    /* Chain up to parent's constructor() method. */
    object = G_OBJECT_CLASS (parent_class)->constructor (
        type, n_construct_properties, construct_properties);

    gtk_tree_store_set_column_types (
        GTK_TREE_STORE (object), G_N_ELEMENTS (types), types);

    return object;
}

static void
contact_list_model_dispose (GObject *object)
{
    EContactListModelPrivate *priv = E_CONTACT_LIST_MODEL (object)->priv;

    if (priv->uids_table) {
        g_hash_table_destroy (priv->uids_table);
        priv->uids_table = NULL;
    }

    if (priv->emails_table) {
        g_hash_table_destroy (priv->emails_table);
        priv->emails_table = NULL;
    }

    G_OBJECT_CLASS (e_contact_list_model_parent_class)->dispose (object);
}

static void
e_contact_list_model_class_init (EContactListModelClass *class)
{
    GObjectClass *object_class;

    g_type_class_add_private (class, sizeof (EContactListModelPrivate));

    parent_class = g_type_class_peek_parent (class);

    object_class = G_OBJECT_CLASS (class);
    object_class->constructor = contact_list_model_constructor;
    object_class->dispose = contact_list_model_dispose;
}

static void
e_contact_list_model_init (EContactListModel *model)
{
    model->priv = G_TYPE_INSTANCE_GET_PRIVATE (model, E_TYPE_CONTACT_LIST_MODEL, EContactListModelPrivate);

    model->priv->uids_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
    model->priv->emails_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
}

GtkTreeModel *
e_contact_list_model_new (void)
{
    return g_object_new (E_TYPE_CONTACT_LIST_MODEL, NULL);
}

gboolean
e_contact_list_model_has_email (EContactListModel *model,
                                const gchar *email)
{
    return (g_hash_table_lookup (model->priv->emails_table, email) != NULL);
}

gboolean
e_contact_list_model_has_uid (EContactListModel *model,
                  const gchar* uid)
{
    return (g_hash_table_lookup (model->priv->uids_table, uid) != NULL);
}

GtkTreePath*
e_contact_list_model_add_destination (EContactListModel *model,
                                      EDestination *destination,
                                      GtkTreeIter *parent,
                                      gboolean ignore_conflicts)
{
    GtkTreeIter iter;
    GtkTreePath *path;

    g_return_val_if_fail (E_IS_CONTACT_LIST_MODEL (model), NULL);
    g_return_val_if_fail (E_IS_DESTINATION (destination), NULL);

    if (e_destination_is_evolution_list (destination)) {
        const GList *dest, *dests = e_destination_list_get_root_dests (destination);
        /* Get number of instances of this list in the model */
        gint list_refs = GPOINTER_TO_INT (g_hash_table_lookup (model->priv->uids_table,
                    e_destination_get_contact_uid (destination)));

        gtk_tree_store_append (GTK_TREE_STORE (model), &iter, parent);
        gtk_tree_store_set (GTK_TREE_STORE (model), &iter, 0, destination, -1);

        for (dest = dests; dest; dest = dest->next) {
            path = e_contact_list_model_add_destination (model, dest->data, &iter, ignore_conflicts);
            if (dest->next && path)
                gtk_tree_path_free (path);
        }

        /* When the list has no children the remove it. We don't want empty sublists displayed. */
        if (!gtk_tree_model_iter_has_child (GTK_TREE_MODEL (model), &iter)) {
            gtk_tree_store_remove (GTK_TREE_STORE (model), &iter);
        } else {
            g_hash_table_insert (model->priv->uids_table,
                g_strdup (e_destination_get_contact_uid (destination)),
                GINT_TO_POINTER (list_refs + 1));
        }
    } else {
        gint dest_refs;

        if (e_contact_list_model_has_email (model, e_destination_get_email (destination)) &&
            ignore_conflicts == FALSE) {
            return NULL;
        }

        dest_refs = GPOINTER_TO_INT (g_hash_table_lookup (model->priv->emails_table,
                e_destination_get_email (destination)));

        g_hash_table_insert (model->priv->emails_table,
            g_strdup (e_destination_get_email (destination)),
            GINT_TO_POINTER (dest_refs + 1));

        gtk_tree_store_append (GTK_TREE_STORE (model), &iter, parent);
        gtk_tree_store_set (GTK_TREE_STORE (model), &iter, 0, destination, -1);

        path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &iter);
    }

    return path;
}

void
e_contact_list_model_add_contact (EContactListModel *model,
                                  EContact *contact,
                                  gint email_num)
{
    EDestination *destination;

    g_return_if_fail (E_IS_CONTACT_LIST_MODEL (model));
    g_return_if_fail (E_IS_CONTACT (contact));

    destination = e_destination_new ();
    e_destination_set_contact (destination, contact, email_num);
    e_contact_list_model_add_destination (model, destination, NULL, TRUE);
}

static void
contact_list_model_unref_row_dest (EContactListModel *model,
                   GtkTreeIter *iter)
{
    EDestination *dest;
    GtkTreeModel *tree_model;

    tree_model = GTK_TREE_MODEL (model);
    gtk_tree_model_get (tree_model, iter, 0, &dest, -1);

    if (gtk_tree_model_iter_has_child (tree_model, iter)) {
        GtkTreeIter child_iter;
        gint list_refs = GPOINTER_TO_INT (g_hash_table_lookup (model->priv->uids_table,
            e_destination_get_contact_uid (dest)));

        /* If the list is only once in the model, then remove it from the hash table,
           otherwise decrease the counter by one */
        if (list_refs <= 1) {
            g_hash_table_remove (model->priv->uids_table,
                e_destination_get_contact_uid (dest));
        } else {
            g_hash_table_insert (model->priv->uids_table,
                g_strdup (e_destination_get_contact_uid (dest)),
                GINT_TO_POINTER (list_refs - 1));
        }

        if (gtk_tree_model_iter_children (tree_model, &child_iter, iter)) {
            do {
                contact_list_model_unref_row_dest (model, &child_iter);
            } while (gtk_tree_model_iter_next (tree_model, &child_iter));
        }

    } else {
        gint dest_refs = GPOINTER_TO_INT (g_hash_table_lookup (model->priv->emails_table,
            e_destination_get_email (dest)));

        if (dest_refs <= 1) {
            g_hash_table_remove (model->priv->emails_table,
                e_destination_get_email (dest));
        } else {
            g_hash_table_insert (model->priv->emails_table,
                g_strdup (e_destination_get_email (dest)),
                GINT_TO_POINTER (dest_refs - 1));
        }
    }

    g_object_unref (dest);
}

void
e_contact_list_model_remove_row (EContactListModel *model,
                                 GtkTreeIter *iter)
{
    GtkTreeIter parent_iter;

    g_return_if_fail (E_IS_CONTACT_LIST_MODEL (model));
    g_return_if_fail (iter);

    /* Use helper function to update our reference counters in
       hash tables but don't remove any row. */
    contact_list_model_unref_row_dest (model, iter);

    /* Get iter of parent of the row to be removed. After the row is removed, check if there are
       any more children left for the parent_iter, an eventually remove the parent_iter as well */
    if (gtk_tree_model_iter_parent (GTK_TREE_MODEL (model), &parent_iter, iter)) {
        gtk_tree_store_remove (GTK_TREE_STORE (model), iter);
        if (!gtk_tree_model_iter_has_child (GTK_TREE_MODEL (model), &parent_iter)) {
            contact_list_model_unref_row_dest (model, &parent_iter);
            gtk_tree_store_remove (GTK_TREE_STORE (model), &parent_iter);
        }
    } else {
        gtk_tree_store_remove (GTK_TREE_STORE (model), iter);
    }
}

void
e_contact_list_model_remove_all (EContactListModel *model)
{
    g_return_if_fail (E_IS_CONTACT_LIST_MODEL (model));

    g_hash_table_remove_all (model->priv->uids_table);
    g_hash_table_remove_all (model->priv->emails_table);

    gtk_tree_store_clear (GTK_TREE_STORE (model));
}

EDestination *
e_contact_list_model_get_destination (EContactListModel *model,
                                      gint row)
{
    EDestination *destination;
    GtkTreeIter iter;
    gboolean iter_valid;

    g_return_val_if_fail (E_IS_CONTACT_LIST_MODEL (model), NULL);

    iter_valid = contact_list_get_iter (model, &iter, row);
    g_return_val_if_fail (iter_valid, NULL);

    gtk_tree_model_get (
        GTK_TREE_MODEL (model), &iter, 0, &destination, -1);

    return destination;
}