aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/contact-list-editor/e-contact-list-model.c
blob: f381e6754e7d72552adaf30ad9c331e81ae46613 (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
/*
 * 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)
 *
 */

#include <config.h>
#include <string.h>

#include "e-contact-list-model.h"
#include "e-util/e-error.h"

static gpointer parent_class;

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_list_store_set_column_types (
        GTK_LIST_STORE (object), G_N_ELEMENTS (types), types);

    return object;
}

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

    parent_class = g_type_class_peek_parent (class);

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

GType
e_contact_list_model_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY (type == 0))
        type = g_type_register_static_simple (
            GTK_TYPE_LIST_STORE,
            "EContactListModel",
            sizeof (EContactListModelClass),
            (GClassInitFunc) contact_list_model_class_init,
            sizeof (EContactListModel),
            (GInstanceInitFunc) NULL, 0);

    return type;
}

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)
{
    GtkTreeIter iter;
    gboolean iter_valid;
    gboolean has_email = FALSE;

    g_return_val_if_fail (E_IS_CONTACT_LIST_MODEL (model), FALSE);
    g_return_val_if_fail (email != NULL, FALSE);

    iter_valid = gtk_tree_model_get_iter_first (
        GTK_TREE_MODEL (model), &iter);

    while (!has_email && iter_valid) {
        EDestination *destination;
        const gchar *textrep;

        gtk_tree_model_get (
            GTK_TREE_MODEL (model), &iter, 0, &destination, -1);
        textrep = e_destination_get_textrep (destination, TRUE);
        has_email = (strcmp (email, textrep) == 0);
        g_object_unref (destination);

        iter_valid = gtk_tree_model_iter_next (
            GTK_TREE_MODEL (model), &iter);
    }

    return has_email;
}

void
e_contact_list_model_add_destination (EContactListModel *model,
                                      EDestination *destination)
{
    GtkTreeIter iter;

    g_return_if_fail (E_IS_CONTACT_LIST_MODEL (model));
    g_return_if_fail (E_IS_DESTINATION (destination));

    gtk_list_store_append (GTK_LIST_STORE (model), &iter);
    gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, destination, -1);
}

void
e_contact_list_model_add_email (EContactListModel *model,
                                const gchar *email)
{
    const gchar *tag = "addressbook:ask-list-add-exists";
    EDestination *destination;

    g_return_if_fail (E_IS_CONTACT_LIST_MODEL (model));
    g_return_if_fail (email != NULL);

    if (e_contact_list_model_has_email (model, email))
        if (e_error_run (NULL, tag, email, NULL) != GTK_RESPONSE_YES)
            return;

    destination = e_destination_new ();
    e_destination_set_email (destination, email);
    e_contact_list_model_add_destination (model, destination);
}

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);
}

void
e_contact_list_model_remove_row (EContactListModel *model,
                                 gint row)
{
    GtkTreeIter iter;
    gboolean iter_valid;

    g_return_if_fail (E_IS_CONTACT_LIST_MODEL (model));

    iter_valid = contact_list_get_iter (model, &iter, row);
    g_return_if_fail (iter_valid);

    gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
}

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

    gtk_list_store_clear (GTK_LIST_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;
}