aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-signature.c
blob: 6f34472c65bef101718e6706b246b89888a20fb5 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2004 Ximian, Inc. (www.ximian.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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 General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 *
 */


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

#include <string.h>

#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>

#include <gconf/gconf-client.h>

#include "e-uid.h"

#include "e-signature.h"


static void e_signature_class_init (ESignatureClass *klass);
static void e_signature_init (ESignature *sig, ESignatureClass *klass);
static void e_signature_finalize (GObject *object);


static GObjectClass *parent_class = NULL;


GType
e_signature_get_type (void)
{
    static GType type = 0;
    
    if (!type) {
        GTypeInfo type_info = {
            sizeof (ESignatureClass),
            NULL, NULL,
            (GClassInitFunc) e_signature_class_init,
            NULL, NULL,
            sizeof (ESignature),
            0,
            (GInstanceInitFunc) e_signature_init,
        };
        
        type = g_type_register_static (G_TYPE_OBJECT, "ESignature", &type_info, 0);
    }
    
    return type;
}

static void
e_signature_class_init (ESignatureClass *klass)
{
    GObjectClass *object_class = (GObjectClass *) klass;
    
    parent_class = g_type_class_ref (G_TYPE_OBJECT);
    
    /* virtual method override */
    object_class->finalize = e_signature_finalize;
}

static void
e_signature_init (ESignature *sig, ESignatureClass *klass)
{
    ;
}

static void
e_signature_finalize (GObject *object)
{
    ESignature *sig = (ESignature *) object;
    
    g_free (sig->uid);
    g_free (sig->name);
    g_free (sig->filename);
    
    G_OBJECT_CLASS (parent_class)->finalize (object);
}


/**
 * e_signature_new:
 *
 * Returns a new signature which can be filled in and
 * added to an #ESignatureList.
 **/
ESignature *
e_signature_new (void)
{
    ESignature *signature;
    
    signature = g_object_new (E_TYPE_SIGNATURE, NULL);
    signature->uid = e_uid_new ();
    
    return signature;
}


/**
 * e_signature_new_from_xml:
 * @xml: an XML signature description
 *
 * Return value: a new #ESignature based on the data in @xml, or %NULL
 * if @xml could not be parsed as valid signature data.
 **/
ESignature *
e_signature_new_from_xml (const char *xml)
{
    ESignature *signature;
    
    signature = g_object_new (E_TYPE_SIGNATURE, NULL);
    if (!e_signature_set_from_xml (signature, xml)) {
        g_object_unref (signature);
        return NULL;
    }
    
    return signature;
}


static gboolean
xml_set_bool (xmlNodePtr node, const char *name, gboolean *val)
{
    gboolean bool;
    char *buf;
    
    if ((buf = xmlGetProp (node, name))) {
        bool = (!strcmp (buf, "true") || !strcmp (buf, "yes"));
        xmlFree (buf);
        
        if (bool != *val) {
            *val = bool;
            return TRUE;
        }
    }
    
    return FALSE;
}

static gboolean
xml_set_prop (xmlNodePtr node, const char *name, char **val)
{
    char *buf, *new_val;
    
    buf = xmlGetProp (node, name);
    new_val = g_strdup (buf);
    xmlFree (buf);
    
    /* We can use strcmp here whether the value is UTF8 or
     * not, since we only care if the bytes changed.
     */
    if (!*val || strcmp (*val, new_val)) {
        g_free (*val);
        *val = new_val;
        return TRUE;
    } else {
        g_free (new_val);
        return FALSE;
    }
}

static gboolean
xml_set_content (xmlNodePtr node, char **val)
{
    char *buf, *new_val;
    
    buf = xmlNodeGetContent (node);
        new_val = g_strdup (buf);
    xmlFree (buf);
    
    /* We can use strcmp here whether the value is UTF8 or
     * not, since we only care if the bytes changed.
     */
    if (!*val || strcmp (*val, new_val)) {
        g_free (*val);
        *val = new_val;
        return TRUE;
    } else {
        g_free (new_val);
        return FALSE;
    }
}


/**
 * e_signature_uid_from_xml:
 * @xml: an XML signature description
 *
 * Return value: the permanent UID of the signature described by @xml
 * (or %NULL if @xml could not be parsed or did not contain a uid).
 * The caller must free this string.
 **/
char *
e_signature_uid_from_xml (const char *xml)
{
    xmlNodePtr node;
    xmlDocPtr doc;
    char *uid = NULL;
    
    if (!(doc = xmlParseDoc ((char *) xml)))
        return NULL;
    
    node = doc->children;
    if (strcmp (node->name, "signature") != 0) {
        xmlFreeDoc (doc);
        return NULL;
    }
    
    xml_set_prop (node, "uid", &uid);
    xmlFreeDoc (doc);
    
    return uid;
}


/**
 * e_signature_set_from_xml:
 * @signature: an #ESignature
 * @xml: an XML signature description.
 *
 * Changes @signature to match @xml.
 *
 * Returns %TRUE if the signature was loaded or %FALSE otherwise.
 **/
gboolean
e_signature_set_from_xml (ESignature *signature, const char *xml)
{
    gboolean changed = FALSE;
    xmlNodePtr node, cur;
    xmlDocPtr doc;
    gboolean bool;
    char *buf;
    
    if (!(doc = xmlParseDoc ((char *) xml)))
        return FALSE;
    
    node = doc->children;
    if (strcmp (node->name, "signature") != 0) {
        xmlFreeDoc (doc);
        return FALSE;
    }
    
    if (!signature->uid)
        xml_set_prop (node, "uid", &signature->uid);
    
    changed |= xml_set_prop (node, "name", &signature->name);
    changed |= xml_set_bool (node, "auto", &signature->autogen);
    
    if (signature->autogen) {
        /* we're done */
        g_free (signature->filename);
        signature->filename = NULL;
        signature->script = FALSE;
        signature->html = FALSE;
        xmlFreeDoc (doc);
        
        return changed;
    }
    
    buf = NULL;
    xml_set_prop (node, "format", &buf);
    if (buf && !strcmp (buf, "text/html"))
        bool = TRUE;
    else
        bool = FALSE;
    g_free (buf);
    
    if (signature->html != bool) {
        signature->html = bool;
        changed = TRUE;
    }
    
    cur = node->children;
    while (cur) {
        if (!strcmp (cur->name, "filename")) {
            changed |= xml_set_content (cur, &signature->filename);
            changed |= xml_set_bool (cur, "script", &signature->script);
            break;
        } else if (!strcmp (cur->name, "script")) {
            /* this is for handling 1.4 signature script definitions */
            changed |= xml_set_content (cur, &signature->filename);
            if (!signature->script) {
                signature->script = TRUE;
                changed = TRUE;
            }
            break;
        }
        
        cur = cur->next;
    }
    
    xmlFreeDoc (doc);
    
    return changed;
}


/**
 * e_signature_to_xml:
 * @signature: an #ESignature
 *
 * Return value: an XML representation of @signature, which the caller
 * must free.
 **/
char *
e_signature_to_xml (ESignature *signature)
{
    char *xmlbuf, *tmp;
    xmlNodePtr root, node;
    xmlDocPtr doc;
    int n;
    
    doc = xmlNewDoc ("1.0");
    
    root = xmlNewDocNode (doc, NULL, "signature", NULL);
    xmlDocSetRootElement (doc, root);
    
    xmlSetProp (root, "name", signature->name);
    xmlSetProp (root, "uid", signature->uid);
    xmlSetProp (root, "auto", signature->autogen ? "true" : "false");
    
    if (!signature->autogen) {
        xmlSetProp (root, "format", signature->html ? "text/html" : "text/plain");
        
        if (signature->filename) {
            node = xmlNewTextChild (root, NULL, "filename", signature->filename);
            if (signature->script)
                xmlSetProp (node, "script", "true");
        }
    }
    
    xmlDocDumpMemory (doc, (xmlChar **) &xmlbuf, &n);
    xmlFreeDoc (doc);
    
    /* remap to glib memory */
    tmp = g_malloc (n + 1);
    memcpy (tmp, xmlbuf, n);
    tmp[n] = '\0';
    xmlFree (xmlbuf);
    
    return tmp;
}