aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-signature-list.c
blob: 34b45be5ba8b1d89bcc4d1f3349a60f3ccf73069 (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
/*
 *
 * 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:
 *      Jeffrey Stedfast <fejj@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

#include <string.h>

#include <libedataserver/e-uid.h>

#include "e-signature-list.h"

struct _ESignatureListPrivate {
    GConfClient *gconf;
    guint notify_id;
    gboolean resave;
};

enum {
    SIGNATURE_ADDED,
    SIGNATURE_CHANGED,
    SIGNATURE_REMOVED,
    LAST_SIGNAL
};

static guint signals [LAST_SIGNAL] = { 0 };

static void e_signature_list_class_init (ESignatureListClass *klass);
static void e_signature_list_init (ESignatureList *list, ESignatureListClass *klass);
static void e_signature_list_finalize (GObject *object);
static void e_signature_list_dispose (GObject *object);

static EListClass *parent_class = NULL;

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

    if (!type) {
        GTypeInfo type_info = {
            sizeof (ESignatureListClass),
            NULL, NULL,
            (GClassInitFunc) e_signature_list_class_init,
            NULL, NULL,
            sizeof (ESignatureList),
            0,
            (GInstanceInitFunc) e_signature_list_init,
        };

        type = g_type_register_static (E_TYPE_LIST, "ESignatureList", &type_info, 0);
    }

    return type;
}

static void
e_signature_list_class_init (ESignatureListClass *klass)
{
    GObjectClass *object_class = (GObjectClass *) klass;

    parent_class = g_type_class_ref (E_TYPE_LIST);

    /* virtual method override */
    object_class->dispose = e_signature_list_dispose;
    object_class->finalize = e_signature_list_finalize;

    /* signals */
    signals[SIGNATURE_ADDED] =
        g_signal_new ("signature-added",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (ESignatureListClass, signature_added),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
                  G_TYPE_NONE, 1,
                  E_TYPE_SIGNATURE);
    signals[SIGNATURE_CHANGED] =
        g_signal_new ("signature-changed",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (ESignatureListClass, signature_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
                  G_TYPE_NONE, 1,
                  E_TYPE_SIGNATURE);
    signals[SIGNATURE_REMOVED] =
        g_signal_new ("signature-removed",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (ESignatureListClass, signature_removed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__OBJECT,
                  G_TYPE_NONE, 1,
                  E_TYPE_SIGNATURE);
}

static void
e_signature_list_init (ESignatureList *list, ESignatureListClass *klass)
{
    list->priv = g_new0 (struct _ESignatureListPrivate, 1);
}

static void
e_signature_list_dispose (GObject *object)
{
    ESignatureList *list = (ESignatureList *) object;

    if (list->priv->gconf) {
        if (list->priv->notify_id != 0)
            gconf_client_notify_remove (list->priv->gconf, list->priv->notify_id);
        g_object_unref (list->priv->gconf);
        list->priv->gconf = NULL;
    }

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

static void
e_signature_list_finalize (GObject *object)
{
    ESignatureList *list = (ESignatureList *) object;

    g_free (list->priv);

    G_OBJECT_CLASS (parent_class)->finalize (object);
}

static GSList *
add_autogen (ESignatureList *list, GSList *new_sigs)
{
    ESignature *autogen;

    autogen = e_signature_new ();
    e_signature_set_autogenerated (autogen, TRUE);

    e_list_append (E_LIST (list), autogen);

    return g_slist_prepend (new_sigs, autogen);
}

static void
gconf_signatures_changed (GConfClient *client, guint cnxn_id, GConfEntry *entry, gpointer user_data)
{
    ESignatureList *signature_list = user_data;
    GSList *list, *l, *n, *new_sigs = NULL;
    gboolean have_autogen = FALSE;
    gboolean resave = FALSE;
    ESignature *signature;
    EList *old_sigs;
    EIterator *iter;
    gboolean found;
    gchar *uid;

    old_sigs = e_list_duplicate (E_LIST (signature_list));

    list = gconf_client_get_list (client, "/apps/evolution/mail/signatures", GCONF_VALUE_STRING, NULL);
    for (l = list; l; l = l->next) {
        found = FALSE;
        if ((uid = e_signature_uid_from_xml (l->data))) {
            /* See if this is an existing signature */
            for (iter = e_list_get_iterator (old_sigs); e_iterator_is_valid (iter); e_iterator_next (iter)) {
                const gchar *signature_uid;

                signature = (ESignature *) e_iterator_get (iter);
                signature_uid = e_signature_get_uid (signature);
                if (!strcmp (signature_uid, uid)) {
                    /* The signature still exists, so remove
                     * it from "old_sigs" and update it.
                     */
                    found = TRUE;
                    e_iterator_delete (iter);
                    if (e_signature_set_from_xml (signature, l->data))
                        g_signal_emit (signature_list, signals[SIGNATURE_CHANGED], 0, signature);

                    have_autogen |= e_signature_get_autogenerated (signature);

                    break;
                }
            }

            g_object_unref (iter);
        }

        if (!found) {
            resave = TRUE;

            /* Must be a new signature */
            signature = e_signature_new_from_xml (l->data);
            if (signature) {
                have_autogen |= e_signature_get_autogenerated (signature);

                e_list_append (E_LIST (signature_list), signature);
                new_sigs = g_slist_prepend (new_sigs, signature);
            }
        }

        g_free (uid);
    }

    if (!have_autogen) {
        new_sigs = add_autogen (signature_list, new_sigs);
        have_autogen = TRUE;
        resave = TRUE;
    }

    if (new_sigs != NULL) {
        /* Now emit signals for each added signature. */
        l = g_slist_reverse (new_sigs);
        while (l != NULL) {
            n = l->next;
            signature = l->data;
            g_signal_emit (signature_list, signals[SIGNATURE_ADDED], 0, signature);
            g_object_unref (signature);
            g_slist_free_1 (l);
            l = n;
        }
    }

    /* Anything left in old_sigs must have been deleted */
    for (iter = e_list_get_iterator (old_sigs); e_iterator_is_valid (iter); e_iterator_next (iter)) {
        signature = (ESignature *) e_iterator_get (iter);
        e_list_remove (E_LIST (signature_list), signature);
        g_signal_emit (signature_list, signals[SIGNATURE_REMOVED], 0, signature);
    }

    g_object_unref (iter);
    g_object_unref (old_sigs);

    signature_list->priv->resave = resave;
}

static gpointer
copy_func (gconstpointer data, gpointer closure)
{
    GObject *object = (GObject *)data;

    g_object_ref (object);

    return object;
}

static void
free_func (gpointer data, gpointer closure)
{
    g_object_unref (data);
}

/**
 * e_signature_list_new:
 * @gconf: a #GConfClient
 *
 * Reads the list of signaturess from @gconf and listens for changes.
 * Will emit #signature_added, #signature_changed, and #signature_removed
 * signals according to notifications from GConf.
 *
 * You can modify the list using e_list_append(), e_list_remove(), and
 * e_iterator_delete(). After adding, removing, or changing accounts,
 * you must call e_signature_list_save() to push the changes back to
 * GConf.
 *
 * Return value: the list of signatures
 **/
ESignatureList *
e_signature_list_new (GConfClient *gconf)
{
    ESignatureList *signature_list;

    g_return_val_if_fail (GCONF_IS_CLIENT (gconf), NULL);

    signature_list = g_object_new (E_TYPE_SIGNATURE_LIST, NULL);
    e_signature_list_construct (signature_list, gconf);

    return signature_list;
}

void
e_signature_list_construct (ESignatureList *signature_list, GConfClient *gconf)
{
    g_return_if_fail (GCONF_IS_CLIENT (gconf));

    e_list_construct (E_LIST (signature_list), copy_func, free_func, NULL);
    signature_list->priv->gconf = gconf;
    g_object_ref (gconf);

    gconf_client_add_dir (signature_list->priv->gconf,
                  "/apps/evolution/mail/signatures",
                  GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);

    signature_list->priv->notify_id =
        gconf_client_notify_add (signature_list->priv->gconf,
                     "/apps/evolution/mail/signatures",
                     gconf_signatures_changed, signature_list,
                     NULL, NULL);

    gconf_signatures_changed (signature_list->priv->gconf,
                  signature_list->priv->notify_id,
                  NULL, signature_list);

    if (signature_list->priv->resave) {
        e_signature_list_save (signature_list);
        signature_list->priv->resave = FALSE;
    }
}

/**
 * e_signature_list_save:
 * @signature_list: an #ESignatureList
 *
 * Saves @signature_list to GConf. Signals will be emitted for changes.
 **/
void
e_signature_list_save (ESignatureList *signature_list)
{
    GSList *list = NULL;
    ESignature *signature;
    EIterator *iter;
    gchar *xmlbuf;

    for (iter = e_list_get_iterator (E_LIST (signature_list));
         e_iterator_is_valid (iter);
         e_iterator_next (iter)) {
        signature = (ESignature *) e_iterator_get (iter);

        if ((xmlbuf = e_signature_to_xml (signature)))
            list = g_slist_append (list, xmlbuf);
    }

    g_object_unref (iter);

    gconf_client_set_list (signature_list->priv->gconf,
                   "/apps/evolution/mail/signatures",
                   GCONF_VALUE_STRING, list, NULL);

    while (list) {
        g_free (list->data);
        list = g_slist_remove (list, list->data);
    }

    gconf_client_suggest_sync (signature_list->priv->gconf, NULL);
}

/**
 * e_signature_list_add:
 * @signatures: signature list
 * @signature: signature to add
 *
 * Add an signature to the signature list.  Will emit the signature-changed
 * event.
 **/
void
e_signature_list_add (ESignatureList *signatures, ESignature *signature)
{
    e_list_append ((EList *) signatures, signature);
    g_signal_emit (signatures, signals[SIGNATURE_ADDED], 0, signature);
}

/**
 * e_signature_list_change:
 * @signatures: signature list
 * @signature: signature to change
 *
 * Signal that the details of an signature have changed.
 **/
void
e_signature_list_change (ESignatureList *signatures, ESignature *signature)
{
    /* maybe the signature should do this itself ... */
    g_signal_emit (signatures, signals[SIGNATURE_CHANGED], 0, signature);
}

/**
 * e_signature_list_remove:
 * @signatures: signature list
 * @signature: signature
 *
 * Remove an signature from the signature list, and emit the
 * signature-removed signal.  If the signature was the default signature,
 * then reset the default to the first signature.
 **/
void
e_signature_list_remove (ESignatureList *signatures, ESignature *signature)
{
    /* not sure if need to ref but no harm */
    g_object_ref (signature);
    e_list_remove ((EList *) signatures, signature);
    g_signal_emit (signatures, signals[SIGNATURE_REMOVED], 0, signature);
    g_object_unref (signature);
}

/**
 * e_signature_list_find:
 * @signatures: signature list
 * @type: Type of search.
 * @key: Search key.
 *
 * Perform a search of the signature list on a single key.
 *
 * @type must be set from one of the following search types:
 * E_SIGNATURE_FIND_NAME - Find a signature by signature name.
 * E_SIGNATURE_FIND_UID - Find a signature based on UID
 *
 * Return value: The signature or NULL if it doesn't exist.
 **/
const ESignature *
e_signature_list_find (ESignatureList *signatures,
                       e_signature_find_t type,
                       const gchar *key)
{
    ESignature *signature = NULL;
    EIterator *it;

    /* this could use a callback for more flexibility ...
       ... but this makes the common cases easier */

    if (!key)
        return NULL;

    for (it = e_list_get_iterator ((EList *) signatures);
         e_iterator_is_valid (it);
         e_iterator_next (it)) {
        const gchar *value;

        /* XXX EIterator misuses const. */
        signature = (ESignature *) e_iterator_get (it);

        switch (type) {
            case E_SIGNATURE_FIND_NAME:
                value = e_signature_get_name (signature);
                break;
            case E_SIGNATURE_FIND_UID:
                value = e_signature_get_uid (signature);
                break;
        }

        if (g_strcmp0 (value, key) == 0)
            break;

        signature = NULL;
    }

    g_object_unref (it);

    return signature;
}