aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-pilot-map.c
blob: 3a1b660c600ad188b2a8024886185942a713c148 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Evolution Conduits - Pilot Map routines
 *
 * Copyright (C) 2000 Helix Code, Inc.
 *
 * Authors: JP Rosevear <jpr@helixcode.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 Place, Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <time.h>
#include <gnome.h>
#include <gnome-xml/parser.h>
#include <e-pilot-map.h>

static void
map_set_node_timet (xmlNodePtr node, const char *name, time_t t)
{
    char *tstring;
    
    tstring = g_strdup_printf ("%ld", t);
    xmlSetProp (node, name, tstring);
}

static void
map_sax_start_element (void *data, const xmlChar *name, 
               const xmlChar **attrs)
{
    EPilotMap *map = (EPilotMap *)data;

    if (!strcmp (name, "PilotMap")) {
        while (attrs && *attrs != NULL) {
            const xmlChar **val = attrs;
            
            val++;
            if (!strcmp (*attrs, "timestamp")) 
                map->since = (time_t)strtoul (*val, NULL, 0);

            attrs = ++val;
        }
    }
     
    if (!strcmp (name, "map")) {
        char *uid = NULL;
        guint32 *pid = g_new (guint32, 1);

        *pid = 0;

        while (attrs && *attrs != NULL) {
            const xmlChar **val = attrs;
            
            val++;
            if (!strcmp (*attrs, "uid")) 
                uid = g_strdup (*val);
            
            if (!strcmp (*attrs, "pilot_id"))
                *pid = strtoul (*val, NULL, 0);
                
            attrs = ++val;
        }
            
        if (uid && *pid != 0) {
            g_hash_table_insert (map->pid_map, pid, uid);
            g_hash_table_insert (map->uid_map, uid, pid);
        } else {
            g_free (pid);
        }
    }
}

static void
map_write_foreach (gpointer key, gpointer value, gpointer data)
{
    xmlNodePtr root = data;
    xmlNodePtr mnode;
    unsigned long *pid = key;
    const char *uid = value;
    char *pidstr;
    
    mnode = xmlNewChild (root, NULL, "map", NULL);
    xmlSetProp (mnode, "uid", uid);
    pidstr = g_strdup_printf ("%lu", *pid);
    xmlSetProp (mnode, "pilot_id", pidstr);
    g_free (pidstr);
}

gboolean 
e_pilot_map_pid_is_archived (EPilotMap *map, guint32 pid)
{
    return FALSE;
}

gboolean 
e_pilot_map_uid_is_archived (EPilotMap *map, const char *uid)
{
    return FALSE;
}

void 
e_pilot_map_insert (EPilotMap *map, guint32 pid, const char *uid, gboolean archived)
{
    char *new_uid;
    guint32 *new_pid = g_new (guint32, 1);
    
    *new_pid = pid;
    new_uid = g_strdup (uid);

    g_hash_table_insert (map->pid_map, new_pid, new_uid);
    g_hash_table_insert (map->uid_map, new_uid, new_pid);
}

guint32 
e_pilot_map_lookup_pid (EPilotMap *map, const char *uid) 
{
    guint32 *pid;
    
    pid = g_hash_table_lookup (map->uid_map, uid);

    if (pid == NULL)
        return 0;
    
    return *pid;
}

const char *
e_pilot_map_lookup_uid (EPilotMap *map, guint32 pid)
{
    const char *uid;
    
    uid = g_hash_table_lookup (map->pid_map, &pid);

    return uid;
}

int 
e_pilot_map_read (const char *filename, EPilotMap **map)
{
    xmlSAXHandler handler;
    EPilotMap *new_map = g_new0 (EPilotMap, 1);

    *map = NULL;
    
    memset (&handler, 0, sizeof (xmlSAXHandler));
    handler.startElement = map_sax_start_element;

    new_map->pid_map = g_hash_table_new (g_int_hash, g_int_equal);
    new_map->uid_map = g_hash_table_new (g_str_hash, g_str_equal);

    if (g_file_exists (filename)) {
        if (xmlSAXUserParseFile (&handler, new_map, filename) < 0) {
            g_free (new_map);
            return -1;
        }
    }
    
    *map = new_map;
    
    return 0;
}
        
int
e_pilot_map_write (const char *filename, EPilotMap *map)
{
    xmlDocPtr doc;
    int ret;
    
    g_return_val_if_fail (map != NULL, -1);
    
    doc = xmlNewDoc ("1.0");
    if (doc == NULL) {
        g_warning ("Pilot map file could not be created\n");
        return -1;
    }
    doc->root = xmlNewDocNode(doc, NULL, "PilotMap", NULL);
    map->since = time (NULL);
    map_set_node_timet (doc->root, "timestamp", map->since);

    g_hash_table_foreach (map->pid_map, map_write_foreach, doc->root);
    
    /* Write the file */
    xmlSetDocCompressMode (doc, 0);
    ret = xmlSaveFile (filename, doc);
    if (ret < 0) {
        g_warning ("Pilot map file '%s' could not be saved\n", filename);
        return -1;
    }
    
    xmlFreeDoc (doc);

    return 0;
}

void 
e_pilot_map_destroy (EPilotMap *map)
{
    g_hash_table_destroy (map->pid_map);
    g_hash_table_destroy (map->uid_map);
    g_free (map);
}