Don't assume a connected TpAccount always have a TpConnection
[empathy-mirror.git] / libempathy / empathy-contact-groups.c
blob726824ea43ad03adfbc9a9ad49d5d91a505214cb
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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301 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-lib.h>
32 #include <libxml/parser.h>
33 #include <libxml/tree.h>
35 #include "empathy-utils.h"
36 #include "empathy-contact-groups.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
39 #include "empathy-debug.h"
41 #define CONTACT_GROUPS_XML_FILENAME "contact-groups.xml"
42 #define CONTACT_GROUPS_DTD_FILENAME "empathy-contact-groups.dtd"
44 typedef struct {
45 gchar *name;
46 gboolean expanded;
47 } ContactGroup;
49 static void contact_groups_file_parse (const gchar *filename);
50 static gboolean contact_groups_file_save (void);
51 static ContactGroup *contact_group_new (const gchar *name,
52 gboolean expanded);
53 static void contact_group_free (ContactGroup *group);
55 static GList *groups = NULL;
57 void
58 empathy_contact_groups_get_all (void)
60 gchar *dir;
61 gchar *file_with_path;
63 /* If already set up clean up first */
64 if (groups) {
65 g_list_foreach (groups, (GFunc)contact_group_free, NULL);
66 g_list_free (groups);
67 groups = NULL;
70 dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
71 file_with_path = g_build_filename (dir, CONTACT_GROUPS_XML_FILENAME, NULL);
72 g_free (dir);
74 if (g_file_test (file_with_path, G_FILE_TEST_EXISTS)) {
75 contact_groups_file_parse (file_with_path);
78 g_free (file_with_path);
81 static void
82 contact_groups_file_parse (const gchar *filename)
84 xmlParserCtxtPtr ctxt;
85 xmlDocPtr doc;
86 xmlNodePtr contacts;
87 xmlNodePtr account;
88 xmlNodePtr node;
90 DEBUG ("Attempting to parse file:'%s'...", filename);
92 ctxt = xmlNewParserCtxt ();
94 /* Parse and validate the file. */
95 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
96 if (!doc) {
97 g_warning ("Failed to parse file:'%s'", filename);
98 xmlFreeParserCtxt (ctxt);
99 return;
102 if (!empathy_xml_validate (doc, CONTACT_GROUPS_DTD_FILENAME)) {
103 g_warning ("Failed to validate file:'%s'", filename);
104 xmlFreeDoc (doc);
105 xmlFreeParserCtxt (ctxt);
106 return;
109 /* The root node, contacts. */
110 contacts = xmlDocGetRootElement (doc);
112 account = NULL;
113 node = contacts->children;
114 while (node) {
115 if (strcmp ((gchar *) node->name, "account") == 0) {
116 account = node;
117 break;
119 node = node->next;
122 node = NULL;
123 if (account) {
124 node = account->children;
127 while (node) {
128 if (strcmp ((gchar *) node->name, "group") == 0) {
129 gchar *name;
130 gchar *expanded_str;
131 gboolean expanded;
132 ContactGroup *contact_group;
134 name = (gchar *) xmlGetProp (node, (const xmlChar *) "name");
135 expanded_str = (gchar *) xmlGetProp (node, (const xmlChar *) "expanded");
137 if (expanded_str && strcmp (expanded_str, "yes") == 0) {
138 expanded = TRUE;
139 } else {
140 expanded = FALSE;
143 contact_group = contact_group_new (name, expanded);
144 groups = g_list_append (groups, contact_group);
146 xmlFree (name);
147 xmlFree (expanded_str);
150 node = node->next;
153 DEBUG ("Parsed %d contact groups", g_list_length (groups));
155 xmlFreeDoc (doc);
156 xmlFreeParserCtxt (ctxt);
159 static ContactGroup *
160 contact_group_new (const gchar *name,
161 gboolean expanded)
163 ContactGroup *group;
165 group = g_new0 (ContactGroup, 1);
167 group->name = g_strdup (name);
168 group->expanded = expanded;
170 return group;
173 static void
174 contact_group_free (ContactGroup *group)
176 g_return_if_fail (group != NULL);
178 g_free (group->name);
180 g_free (group);
183 static gboolean
184 contact_groups_file_save (void)
186 xmlDocPtr doc;
187 xmlNodePtr root;
188 xmlNodePtr node;
189 GList *l;
190 gchar *dir;
191 gchar *file;
193 dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
194 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
195 file = g_build_filename (dir, CONTACT_GROUPS_XML_FILENAME, NULL);
196 g_free (dir);
198 doc = xmlNewDoc ((const xmlChar *) "1.0");
199 root = xmlNewNode (NULL, (const xmlChar *) "contacts");
200 xmlDocSetRootElement (doc, root);
202 node = xmlNewChild (root, NULL, (const xmlChar *) "account", NULL);
203 xmlNewProp (node, (const xmlChar *) "name", (const xmlChar *) "Default");
205 for (l = groups; l; l = l->next) {
206 ContactGroup *cg;
207 xmlNodePtr subnode;
209 cg = l->data;
211 subnode = xmlNewChild (node, NULL, (const xmlChar *) "group", NULL);
212 xmlNewProp (subnode, (const xmlChar *) "expanded", cg->expanded ?
213 (const xmlChar *) "yes" : (const xmlChar *) "no");
214 xmlNewProp (subnode, (const xmlChar *) "name", (const xmlChar *) cg->name);
217 /* Make sure the XML is indented properly */
218 xmlIndentTreeOutput = 1;
220 DEBUG ("Saving file:'%s'", file);
221 xmlSaveFormatFileEnc (file, doc, "utf-8", 1);
222 xmlFreeDoc (doc);
224 xmlMemoryDump ();
226 g_free (file);
228 return TRUE;
231 gboolean
232 empathy_contact_group_get_expanded (const gchar *group)
234 GList *l;
235 gboolean default_val = TRUE;
237 g_return_val_if_fail (group != NULL, default_val);
239 for (l = groups; l; l = l->next) {
240 ContactGroup *cg = l->data;
242 if (!cg || !cg->name) {
243 continue;
246 if (strcmp (cg->name, group) == 0) {
247 return cg->expanded;
251 return default_val;
254 void
255 empathy_contact_group_set_expanded (const gchar *group,
256 gboolean expanded)
258 GList *l;
259 ContactGroup *cg;
260 gboolean changed = FALSE;
262 g_return_if_fail (group != NULL);
264 for (l = groups; l; l = l->next) {
265 cg = l->data;
267 if (!cg || !cg->name) {
268 continue;
271 if (strcmp (cg->name, group) == 0) {
272 cg->expanded = expanded;
273 changed = TRUE;
274 break;
278 /* if here... we don't have a ContactGroup for the group. */
279 if (!changed) {
280 cg = contact_group_new (group, expanded);
281 groups = g_list_append (groups, cg);
284 contact_groups_file_save ();