1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301 USA
20 * Authors: Martyn Russell <martyn@imendio.com>
24 #include "empathy-contact-groups.h"
28 #include "empathy-utils.h"
30 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
31 #include "empathy-debug.h"
33 #define CONTACT_GROUPS_XML_FILENAME "contact-groups.xml"
34 #define CONTACT_GROUPS_DTD_RESOURCENAME "/org/gnome/Empathy/empathy-contact-groups.dtd"
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,
45 static void contact_group_free (ContactGroup *group);
47 static GList *groups = NULL;
50 empathy_contact_groups_get_all (void)
53 gchar *file_with_path;
55 /* If already set up clean up first */
57 g_list_foreach (groups, (GFunc)contact_group_free, NULL);
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);
66 if (g_file_test (file_with_path, G_FILE_TEST_EXISTS)) {
67 contact_groups_file_parse (file_with_path);
70 g_free (file_with_path);
74 contact_groups_file_parse (const gchar *filename)
76 xmlParserCtxtPtr ctxt;
82 DEBUG ("Attempting to parse file:'%s'...", filename);
84 ctxt = xmlNewParserCtxt ();
86 /* Parse and validate the file. */
87 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
89 g_warning ("Failed to parse file:'%s'", filename);
90 xmlFreeParserCtxt (ctxt);
94 if (!empathy_xml_validate_from_resource (doc, CONTACT_GROUPS_DTD_RESOURCENAME)) {
95 g_warning ("Failed to validate file:'%s'", filename);
97 xmlFreeParserCtxt (ctxt);
101 /* The root node, contacts. */
102 contacts = xmlDocGetRootElement (doc);
105 node = contacts->children;
107 if (strcmp ((gchar *) node->name, "account") == 0) {
116 node = account->children;
120 if (strcmp ((gchar *) node->name, "group") == 0) {
124 ContactGroup *contact_group;
126 name = (gchar *) xmlGetProp (node, (const xmlChar *) "name");
127 expanded_str = (gchar *) xmlGetProp (node, (const xmlChar *) "expanded");
129 if (expanded_str && strcmp (expanded_str, "yes") == 0) {
135 contact_group = contact_group_new (name, expanded);
136 groups = g_list_append (groups, contact_group);
139 xmlFree (expanded_str);
145 DEBUG ("Parsed %d contact groups", g_list_length (groups));
148 xmlFreeParserCtxt (ctxt);
151 static ContactGroup *
152 contact_group_new (const gchar *name,
157 group = g_new0 (ContactGroup, 1);
159 group->name = g_strdup (name);
160 group->expanded = expanded;
166 contact_group_free (ContactGroup *group)
168 g_return_if_fail (group != NULL);
170 g_free (group->name);
176 contact_groups_file_save (void)
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);
190 doc = xmlNewDoc ((const xmlChar *) "1.0");
191 root = xmlNewNode (NULL, (const xmlChar *) "contacts");
192 xmlDocSetRootElement (doc, root);
194 node = xmlNewChild (root, NULL, (const xmlChar *) "account", NULL);
195 xmlNewProp (node, (const xmlChar *) "name", (const xmlChar *) "Default");
197 for (l = groups; l; l = l->next) {
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);
209 /* Make sure the XML is indented properly */
210 xmlIndentTreeOutput = 1;
212 DEBUG ("Saving file:'%s'", file);
213 xmlSaveFormatFileEnc (file, doc, "utf-8", 1);
224 empathy_contact_group_get_expanded (const gchar *group)
227 gboolean default_val = TRUE;
229 g_return_val_if_fail (group != NULL, default_val);
231 for (l = groups; l; l = l->next) {
232 ContactGroup *cg = l->data;
234 if (!cg || !cg->name) {
238 if (strcmp (cg->name, group) == 0) {
247 empathy_contact_group_set_expanded (const gchar *group,
252 gboolean changed = FALSE;
254 g_return_if_fail (group != NULL);
256 for (l = groups; l; l = l->next) {
259 if (!cg || !cg->name) {
263 if (strcmp (cg->name, group) == 0) {
264 cg->expanded = expanded;
270 /* if here... we don't have a ContactGroup for the group. */
272 cg = contact_group_new (group, expanded);
273 groups = g_list_append (groups, cg);
276 contact_groups_file_save ();