]> git.0d.be Git - empathy.git/blob - libempathy/empathy-contact-groups.c
Merge branch 'gnome-3-8'
[empathy.git] / libempathy / empathy-contact-groups.c
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., 51 Franklin St, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors: Martyn Russell <martyn@imendio.com>
21  */
22
23 #include "config.h"
24 #include "empathy-contact-groups.h"
25
26 #include <sys/stat.h>
27
28 #include "empathy-utils.h"
29
30 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
31 #include "empathy-debug.h"
32
33 #define CONTACT_GROUPS_XML_FILENAME "contact-groups.xml"
34 #define CONTACT_GROUPS_DTD_RESOURCENAME "/org/gnome/Empathy/empathy-contact-groups.dtd"
35
36 typedef struct {
37         gchar    *name;
38         gboolean  expanded;
39 } ContactGroup;
40
41 static void          contact_groups_file_parse (const gchar  *filename);
42 static gboolean      contact_groups_file_save  (void);
43 static ContactGroup *contact_group_new         (const gchar  *name,
44                                                 gboolean      expanded);
45 static void          contact_group_free        (ContactGroup *group);
46
47 static GList *groups = NULL;
48
49 void
50 empathy_contact_groups_get_all (void)
51 {
52         gchar *dir;
53         gchar *file_with_path;
54
55         /* If already set up clean up first */
56         if (groups) {
57                 g_list_foreach (groups, (GFunc)contact_group_free, NULL);
58                 g_list_free (groups);
59                 groups = NULL;
60         }
61
62         dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
63         file_with_path = g_build_filename (dir, CONTACT_GROUPS_XML_FILENAME, NULL);
64         g_free (dir);
65
66         if (g_file_test (file_with_path, G_FILE_TEST_EXISTS)) {
67                 contact_groups_file_parse (file_with_path);
68         }
69
70         g_free (file_with_path);
71 }
72
73 static void
74 contact_groups_file_parse (const gchar *filename)
75 {
76         xmlParserCtxtPtr ctxt;
77         xmlDocPtr        doc;
78         xmlNodePtr       contacts;
79         xmlNodePtr       account;
80         xmlNodePtr       node;
81
82         DEBUG ("Attempting to parse file:'%s'...", filename);
83
84         ctxt = xmlNewParserCtxt ();
85
86         /* Parse and validate the file. */
87         doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
88         if (!doc) {
89                 g_warning ("Failed to parse file:'%s'", filename);
90                 xmlFreeParserCtxt (ctxt);
91                 return;
92         }
93
94         if (!empathy_xml_validate_from_resource (doc, CONTACT_GROUPS_DTD_RESOURCENAME)) {
95                 g_warning ("Failed to validate file:'%s'", filename);
96                 xmlFreeDoc (doc);
97                 xmlFreeParserCtxt (ctxt);
98                 return;
99         }
100
101         /* The root node, contacts. */
102         contacts = xmlDocGetRootElement (doc);
103
104         account = NULL;
105         node = contacts->children;
106         while (node) {
107                 if (strcmp ((gchar *) node->name, "account") == 0) {
108                         account = node;
109                         break;
110                 }
111                 node = node->next;
112         }
113
114         node = NULL;
115         if (account) {
116                 node = account->children;
117         }
118
119         while (node) {
120                 if (strcmp ((gchar *) node->name, "group") == 0) {
121                         gchar        *name;
122                         gchar        *expanded_str;
123                         gboolean      expanded;
124                         ContactGroup *contact_group;
125
126                         name = (gchar *) xmlGetProp (node, (const xmlChar *) "name");
127                         expanded_str = (gchar *) xmlGetProp (node, (const xmlChar *) "expanded");
128
129                         if (expanded_str && strcmp (expanded_str, "yes") == 0) {
130                                 expanded = TRUE;
131                         } else {
132                                 expanded = FALSE;
133                         }
134
135                         contact_group = contact_group_new (name, expanded);
136                         groups = g_list_append (groups, contact_group);
137
138                         xmlFree (name);
139                         xmlFree (expanded_str);
140                 }
141
142                 node = node->next;
143         }
144
145         DEBUG ("Parsed %d contact groups", g_list_length (groups));
146
147         xmlFreeDoc (doc);
148         xmlFreeParserCtxt (ctxt);
149 }
150
151 static ContactGroup *
152 contact_group_new (const gchar *name,
153                    gboolean     expanded)
154 {
155         ContactGroup *group;
156
157         group = g_new0 (ContactGroup, 1);
158
159         group->name = g_strdup (name);
160         group->expanded = expanded;
161
162         return group;
163 }
164
165 static void
166 contact_group_free (ContactGroup *group)
167 {
168         g_return_if_fail (group != NULL);
169
170         g_free (group->name);
171
172         g_free (group);
173 }
174
175 static gboolean
176 contact_groups_file_save (void)
177 {
178         xmlDocPtr   doc;
179         xmlNodePtr  root;
180         xmlNodePtr  node;
181         GList      *l;
182         gchar      *dir;
183         gchar      *file;
184
185         dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
186         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
187         file = g_build_filename (dir, CONTACT_GROUPS_XML_FILENAME, NULL);
188         g_free (dir);
189
190         doc = xmlNewDoc ((const xmlChar *) "1.0");
191         root = xmlNewNode (NULL, (const xmlChar *) "contacts");
192         xmlDocSetRootElement (doc, root);
193
194         node = xmlNewChild (root, NULL, (const xmlChar *) "account", NULL);
195         xmlNewProp (node, (const xmlChar *) "name", (const xmlChar *) "Default");
196
197         for (l = groups; l; l = l->next) {
198                 ContactGroup *cg;
199                 xmlNodePtr    subnode;
200
201                 cg = l->data;
202
203                 subnode = xmlNewChild (node, NULL, (const xmlChar *) "group", NULL);
204                 xmlNewProp (subnode, (const xmlChar *) "expanded", cg->expanded ?
205                                 (const xmlChar *) "yes" : (const xmlChar *) "no");
206                 xmlNewProp (subnode, (const xmlChar *) "name", (const xmlChar *) cg->name);
207         }
208
209         /* Make sure the XML is indented properly */
210         xmlIndentTreeOutput = 1;
211
212         DEBUG ("Saving file:'%s'", file);
213         xmlSaveFormatFileEnc (file, doc, "utf-8", 1);
214         xmlFreeDoc (doc);
215
216         xmlMemoryDump ();
217
218         g_free (file);
219
220         return TRUE;
221 }
222
223 gboolean
224 empathy_contact_group_get_expanded (const gchar *group)
225 {
226         GList    *l;
227         gboolean  default_val = TRUE;
228
229         g_return_val_if_fail (group != NULL, default_val);
230
231         for (l = groups; l; l = l->next) {
232                 ContactGroup *cg = l->data;
233
234                 if (!cg || !cg->name) {
235                         continue;
236                 }
237
238                 if (strcmp (cg->name, group) == 0) {
239                         return cg->expanded;
240                 }
241         }
242
243         return default_val;
244 }
245
246 void
247 empathy_contact_group_set_expanded (const gchar *group,
248                                    gboolean     expanded)
249 {
250         GList        *l;
251         ContactGroup *cg;
252         gboolean      changed = FALSE;
253
254         g_return_if_fail (group != NULL);
255
256         for (l = groups; l; l = l->next) {
257                 cg = l->data;
258
259                 if (!cg || !cg->name) {
260                         continue;
261                 }
262
263                 if (strcmp (cg->name, group) == 0) {
264                         cg->expanded = expanded;
265                         changed = TRUE;
266                         break;
267                 }
268         }
269
270         /* if here... we don't have a ContactGroup for the group. */
271         if (!changed) {
272                 cg = contact_group_new (group, expanded);
273                 groups = g_list_append (groups, cg);
274         }
275
276         contact_groups_file_save ();
277 }