~bcurtiswx/ubuntu/precise/empathy/3.4.2.1-0ubuntu1

« back to all changes in this revision

Viewing changes to libempathy-gtk/gossip-contact-groups.c

  • Committer: Bazaar Package Importer
  • Author(s): Sjoerd Simons
  • Date: 2007-05-20 15:31:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070520153142-r3auwguxdgxhktqb
Tags: upstream-0.4
ImportĀ upstreamĀ versionĀ 0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 
2
/*
 
3
 * Copyright (C) 2005-2007 Imendio AB
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License as
 
7
 * published by the Free Software Foundation; either version 2 of the
 
8
 * License, or (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public
 
16
 * License along with this program; if not, write to the
 
17
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
 * Boston, MA 02111-1307, USA.
 
19
 *
 
20
 * Authors: Martyn Russell <martyn@imendio.com>
 
21
 */
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include <string.h>
 
26
#include <sys/types.h>
 
27
#include <sys/stat.h>
 
28
 
 
29
#include <glib.h>
 
30
#include <glib/gi18n.h>
 
31
 
 
32
#include <libxml/parser.h>
 
33
#include <libxml/tree.h>
 
34
 
 
35
#include <libempathy/gossip-debug.h>
 
36
#include <libempathy/gossip-utils.h>
 
37
 
 
38
#include "gossip-contact-groups.h"
 
39
 
 
40
#define DEBUG_DOMAIN "ContactGroups"
 
41
 
 
42
#define CONTACT_GROUPS_XML_FILENAME "contact-groups.xml"
 
43
#define CONTACT_GROUPS_DTD_FILENAME "gossip-contact-groups.dtd"
 
44
 
 
45
typedef struct {
 
46
        gchar    *name;
 
47
        gboolean  expanded;
 
48
} ContactGroup;
 
49
 
 
50
static void          contact_groups_file_parse (const gchar  *filename);
 
51
static gboolean      contact_groups_file_save  (void);
 
52
static ContactGroup *contact_group_new         (const gchar  *name,
 
53
                                                gboolean      expanded);
 
54
static void          contact_group_free        (ContactGroup *group);
 
55
 
 
56
static GList *groups = NULL;
 
57
 
 
58
void
 
59
gossip_contact_groups_get_all (void)
 
60
{
 
61
        gchar *dir;
 
62
        gchar *file_with_path;
 
63
 
 
64
        /* If already set up clean up first */
 
65
        if (groups) {
 
66
                g_list_foreach (groups, (GFunc)contact_group_free, NULL);
 
67
                g_list_free (groups);
 
68
                groups = NULL;
 
69
        }
 
70
 
 
71
        dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
 
72
        file_with_path = g_build_filename (dir, CONTACT_GROUPS_XML_FILENAME, NULL);
 
73
        g_free (dir);
 
74
 
 
75
        if (g_file_test (file_with_path, G_FILE_TEST_EXISTS)) {
 
76
                contact_groups_file_parse (file_with_path);
 
77
        }
 
78
 
 
79
        g_free (file_with_path);
 
80
}
 
81
 
 
82
static void
 
83
contact_groups_file_parse (const gchar *filename)
 
84
{
 
85
        xmlParserCtxtPtr ctxt;
 
86
        xmlDocPtr        doc;
 
87
        xmlNodePtr       contacts;
 
88
        xmlNodePtr       account;
 
89
        xmlNodePtr       node;
 
90
 
 
91
        gossip_debug (DEBUG_DOMAIN, "Attempting to parse file:'%s'...", filename);
 
92
 
 
93
        ctxt = xmlNewParserCtxt ();
 
94
 
 
95
        /* Parse and validate the file. */
 
96
        doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
 
97
        if (!doc) {
 
98
                g_warning ("Failed to parse file:'%s'", filename);
 
99
                xmlFreeParserCtxt (ctxt);
 
100
                return;
 
101
        }
 
102
 
 
103
        if (!gossip_xml_validate (doc, CONTACT_GROUPS_DTD_FILENAME)) {
 
104
                g_warning ("Failed to validate file:'%s'", filename);
 
105
                xmlFreeDoc(doc);
 
106
                xmlFreeParserCtxt (ctxt);
 
107
                return;
 
108
        }
 
109
 
 
110
        /* The root node, contacts. */
 
111
        contacts = xmlDocGetRootElement (doc);
 
112
 
 
113
        account = NULL;
 
114
        node = contacts->children;
 
115
        while (node) {
 
116
                if (strcmp ((gchar *) node->name, "account") == 0) {
 
117
                        account = node;
 
118
                        break;
 
119
                }
 
120
                node = node->next;
 
121
        }
 
122
 
 
123
        node = NULL;
 
124
        if (account) {
 
125
                node = account->children;
 
126
        }
 
127
 
 
128
        while (node) {
 
129
                if (strcmp ((gchar *) node->name, "group") == 0) {
 
130
                        gchar        *name;
 
131
                        gchar        *expanded_str;
 
132
                        gboolean      expanded;
 
133
                        ContactGroup *contact_group;
 
134
 
 
135
                        name = (gchar *) xmlGetProp (node, "name");
 
136
                        expanded_str = (gchar *) xmlGetProp (node, "expanded");
 
137
 
 
138
                        if (expanded_str && strcmp (expanded_str, "yes") == 0) {
 
139
                                expanded = TRUE;
 
140
                        } else {
 
141
                                expanded = FALSE;
 
142
                        }
 
143
 
 
144
                        contact_group = contact_group_new (name, expanded);
 
145
                        groups = g_list_append (groups, contact_group);
 
146
 
 
147
                        xmlFree (name);
 
148
                        xmlFree (expanded_str);
 
149
                }
 
150
 
 
151
                node = node->next;
 
152
        }
 
153
 
 
154
        gossip_debug (DEBUG_DOMAIN, "Parsed %d contact groups", g_list_length (groups));
 
155
 
 
156
        xmlFreeDoc(doc);
 
157
        xmlFreeParserCtxt (ctxt);
 
158
}
 
159
 
 
160
static ContactGroup *
 
161
contact_group_new (const gchar *name,
 
162
                   gboolean     expanded)
 
163
{
 
164
        ContactGroup *group;
 
165
 
 
166
        group = g_new0 (ContactGroup, 1);
 
167
 
 
168
        group->name = g_strdup (name);
 
169
        group->expanded = expanded;
 
170
 
 
171
        return group;
 
172
}
 
173
 
 
174
static void
 
175
contact_group_free (ContactGroup *group)
 
176
{
 
177
        g_return_if_fail (group != NULL);
 
178
 
 
179
        g_free (group->name);
 
180
 
 
181
        g_free (group);
 
182
}
 
183
 
 
184
static gboolean
 
185
contact_groups_file_save (void)
 
186
{
 
187
        xmlDocPtr   doc;
 
188
        xmlNodePtr  root;
 
189
        xmlNodePtr  node;
 
190
        GList      *l;
 
191
        gchar      *dir;
 
192
        gchar      *file;
 
193
 
 
194
        dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
 
195
        g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
 
196
        file = g_build_filename (dir, CONTACT_GROUPS_XML_FILENAME, NULL);
 
197
        g_free (dir);
 
198
 
 
199
        doc = xmlNewDoc ("1.0");
 
200
        root = xmlNewNode (NULL, "contacts");
 
201
        xmlDocSetRootElement (doc, root);
 
202
 
 
203
        node = xmlNewChild (root, NULL, "account", NULL);
 
204
        xmlNewProp (node, "name", "Default");
 
205
 
 
206
        for (l = groups; l; l = l->next) {
 
207
                ContactGroup *cg;
 
208
                xmlNodePtr    subnode;
 
209
 
 
210
                cg = l->data;
 
211
 
 
212
                subnode = xmlNewChild (node, NULL, "group", NULL);
 
213
                xmlNewProp (subnode, "expanded", cg->expanded ? "yes" : "no");
 
214
                xmlNewProp (subnode, "name", cg->name);
 
215
        }
 
216
 
 
217
        /* Make sure the XML is indented properly */
 
218
        xmlIndentTreeOutput = 1;
 
219
 
 
220
        gossip_debug (DEBUG_DOMAIN, "Saving file:'%s'", file);
 
221
        xmlSaveFormatFileEnc (file, doc, "utf-8", 1);
 
222
        xmlFreeDoc (doc);
 
223
 
 
224
        xmlCleanupParser ();
 
225
        xmlMemoryDump ();
 
226
 
 
227
        g_free (file);
 
228
 
 
229
        return TRUE;
 
230
}
 
231
 
 
232
gboolean
 
233
gossip_contact_group_get_expanded (const gchar *group)
 
234
{
 
235
        GList    *l;
 
236
        gboolean  default_val = TRUE;
 
237
 
 
238
        g_return_val_if_fail (group != NULL, default_val);
 
239
 
 
240
        for (l = groups; l; l = l->next) {
 
241
                ContactGroup *cg = l->data;
 
242
 
 
243
                if (!cg || !cg->name) {
 
244
                        continue;
 
245
                }
 
246
 
 
247
                if (strcmp (cg->name, group) == 0) {
 
248
                        return cg->expanded;
 
249
                }
 
250
        }
 
251
 
 
252
        return default_val;
 
253
}
 
254
 
 
255
void
 
256
gossip_contact_group_set_expanded (const gchar *group,
 
257
                                   gboolean     expanded)
 
258
{
 
259
        GList        *l;
 
260
        ContactGroup *cg;
 
261
        gboolean      changed = FALSE;
 
262
 
 
263
        g_return_if_fail (group != NULL);
 
264
 
 
265
        for (l = groups; l; l = l->next) {
 
266
                ContactGroup *cg = l->data;
 
267
 
 
268
                if (!cg || !cg->name) {
 
269
                        continue;
 
270
                }
 
271
 
 
272
                if (strcmp (cg->name, group) == 0) {
 
273
                        cg->expanded = expanded;
 
274
                        changed = TRUE;
 
275
                        break;
 
276
                }
 
277
        }
 
278
 
 
279
        /* if here... we don't have a ContactGroup for the group. */
 
280
        if (!changed) {
 
281
                cg = contact_group_new (group, expanded);
 
282
                groups = g_list_append (groups, cg);
 
283
        }
 
284
 
 
285
        contact_groups_file_save ();
 
286
}