Updated for release
[empathy-mirror.git] / libempathy-gtk / empathy-contact-groups.c
blob1e5fabe290fa2fac0103013fb80691e97e36eb8b
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2005-2007 Imendio AB
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.
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.
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.
20 * Authors: Martyn Russell <martyn@imendio.com>
23 #include "config.h"
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
29 #include <glib.h>
30 #include <glib/gi18n.h>
32 #include <libxml/parser.h>
33 #include <libxml/tree.h>
35 #include <libempathy/empathy-debug.h>
36 #include <libempathy/empathy-utils.h>
38 #include "empathy-contact-groups.h"
40 #define DEBUG_DOMAIN "ContactGroups"
42 #define CONTACT_GROUPS_XML_FILENAME "contact-groups.xml"
43 #define CONTACT_GROUPS_DTD_FILENAME "empathy-contact-groups.dtd"
45 typedef struct {
46 gchar *name;
47 gboolean expanded;
48 } ContactGroup;
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);
56 static GList *groups = NULL;
58 void
59 empathy_contact_groups_get_all (void)
61 gchar *dir;
62 gchar *file_with_path;
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;
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);
75 if (g_file_test (file_with_path, G_FILE_TEST_EXISTS)) {
76 contact_groups_file_parse (file_with_path);
79 g_free (file_with_path);
82 static void
83 contact_groups_file_parse (const gchar *filename)
85 xmlParserCtxtPtr ctxt;
86 xmlDocPtr doc;
87 xmlNodePtr contacts;
88 xmlNodePtr account;
89 xmlNodePtr node;
91 empathy_debug (DEBUG_DOMAIN, "Attempting to parse file:'%s'...", filename);
93 ctxt = xmlNewParserCtxt ();
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;
103 if (!empathy_xml_validate (doc, CONTACT_GROUPS_DTD_FILENAME)) {
104 g_warning ("Failed to validate file:'%s'", filename);
105 xmlFreeDoc(doc);
106 xmlFreeParserCtxt (ctxt);
107 return;
110 /* The root node, contacts. */
111 contacts = xmlDocGetRootElement (doc);
113 account = NULL;
114 node = contacts->children;
115 while (node) {
116 if (strcmp ((gchar *) node->name, "account") == 0) {
117 account = node;
118 break;
120 node = node->next;
123 node = NULL;
124 if (account) {
125 node = account->children;
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;
135 name = (gchar *) xmlGetProp (node, "name");
136 expanded_str = (gchar *) xmlGetProp (node, "expanded");
138 if (expanded_str && strcmp (expanded_str, "yes") == 0) {
139 expanded = TRUE;
140 } else {
141 expanded = FALSE;
144 contact_group = contact_group_new (name, expanded);
145 groups = g_list_append (groups, contact_group);
147 xmlFree (name);
148 xmlFree (expanded_str);
151 node = node->next;
154 empathy_debug (DEBUG_DOMAIN, "Parsed %d contact groups", g_list_length (groups));
156 xmlFreeDoc(doc);
157 xmlFreeParserCtxt (ctxt);
160 static ContactGroup *
161 contact_group_new (const gchar *name,
162 gboolean expanded)
164 ContactGroup *group;
166 group = g_new0 (ContactGroup, 1);
168 group->name = g_strdup (name);
169 group->expanded = expanded;
171 return group;
174 static void
175 contact_group_free (ContactGroup *group)
177 g_return_if_fail (group != NULL);
179 g_free (group->name);
181 g_free (group);
184 static gboolean
185 contact_groups_file_save (void)
187 xmlDocPtr doc;
188 xmlNodePtr root;
189 xmlNodePtr node;
190 GList *l;
191 gchar *dir;
192 gchar *file;
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);
199 doc = xmlNewDoc ("1.0");
200 root = xmlNewNode (NULL, "contacts");
201 xmlDocSetRootElement (doc, root);
203 node = xmlNewChild (root, NULL, "account", NULL);
204 xmlNewProp (node, "name", "Default");
206 for (l = groups; l; l = l->next) {
207 ContactGroup *cg;
208 xmlNodePtr subnode;
210 cg = l->data;
212 subnode = xmlNewChild (node, NULL, "group", NULL);
213 xmlNewProp (subnode, "expanded", cg->expanded ? "yes" : "no");
214 xmlNewProp (subnode, "name", cg->name);
217 /* Make sure the XML is indented properly */
218 xmlIndentTreeOutput = 1;
220 empathy_debug (DEBUG_DOMAIN, "Saving file:'%s'", file);
221 xmlSaveFormatFileEnc (file, doc, "utf-8", 1);
222 xmlFreeDoc (doc);
224 xmlCleanupParser ();
225 xmlMemoryDump ();
227 g_free (file);
229 return TRUE;
232 gboolean
233 empathy_contact_group_get_expanded (const gchar *group)
235 GList *l;
236 gboolean default_val = TRUE;
238 g_return_val_if_fail (group != NULL, default_val);
240 for (l = groups; l; l = l->next) {
241 ContactGroup *cg = l->data;
243 if (!cg || !cg->name) {
244 continue;
247 if (strcmp (cg->name, group) == 0) {
248 return cg->expanded;
252 return default_val;
255 void
256 empathy_contact_group_set_expanded (const gchar *group,
257 gboolean expanded)
259 GList *l;
260 ContactGroup *cg;
261 gboolean changed = FALSE;
263 g_return_if_fail (group != NULL);
265 for (l = groups; l; l = l->next) {
266 ContactGroup *cg = l->data;
268 if (!cg || !cg->name) {
269 continue;
272 if (strcmp (cg->name, group) == 0) {
273 cg->expanded = expanded;
274 changed = TRUE;
275 break;
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);
285 contact_groups_file_save ();