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"
27 #include <tp-account-widgets/tpaw-utils.h>
29 #include "empathy-utils.h"
31 #define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
32 #include "empathy-debug.h"
34 #define CONTACT_GROUPS_XML_FILENAME "contact-groups.xml"
35 #define CONTACT_GROUPS_DTD_RESOURCENAME "/org/gnome/Empathy/empathy-contact-groups.dtd"
42 static void contact_groups_file_parse (const gchar
*filename
);
43 static gboolean
contact_groups_file_save (void);
44 static ContactGroup
*contact_group_new (const gchar
*name
,
46 static void contact_group_free (ContactGroup
*group
);
48 static GList
*groups
= NULL
;
51 empathy_contact_groups_get_all (void)
54 gchar
*file_with_path
;
56 /* If already set up clean up first */
58 g_list_foreach (groups
, (GFunc
)contact_group_free
, NULL
);
63 dir
= g_build_filename (g_get_user_config_dir (), PACKAGE_NAME
, NULL
);
64 file_with_path
= g_build_filename (dir
, CONTACT_GROUPS_XML_FILENAME
, NULL
);
67 if (g_file_test (file_with_path
, G_FILE_TEST_EXISTS
)) {
68 contact_groups_file_parse (file_with_path
);
71 g_free (file_with_path
);
75 contact_groups_file_parse (const gchar
*filename
)
77 xmlParserCtxtPtr ctxt
;
83 DEBUG ("Attempting to parse file:'%s'...", filename
);
85 ctxt
= xmlNewParserCtxt ();
87 /* Parse and validate the file. */
88 doc
= xmlCtxtReadFile (ctxt
, filename
, NULL
, 0);
90 g_warning ("Failed to parse file:'%s'", filename
);
91 xmlFreeParserCtxt (ctxt
);
95 if (!tpaw_xml_validate_from_resource (doc
, CONTACT_GROUPS_DTD_RESOURCENAME
)) {
96 g_warning ("Failed to validate file:'%s'", filename
);
98 xmlFreeParserCtxt (ctxt
);
102 /* The root node, contacts. */
103 contacts
= xmlDocGetRootElement (doc
);
106 node
= contacts
->children
;
108 if (strcmp ((gchar
*) node
->name
, "account") == 0) {
117 node
= account
->children
;
121 if (strcmp ((gchar
*) node
->name
, "group") == 0) {
125 ContactGroup
*contact_group
;
127 name
= (gchar
*) xmlGetProp (node
, (const xmlChar
*) "name");
128 expanded_str
= (gchar
*) xmlGetProp (node
, (const xmlChar
*) "expanded");
130 if (expanded_str
&& strcmp (expanded_str
, "yes") == 0) {
136 contact_group
= contact_group_new (name
, expanded
);
137 groups
= g_list_append (groups
, contact_group
);
140 xmlFree (expanded_str
);
146 DEBUG ("Parsed %d contact groups", g_list_length (groups
));
149 xmlFreeParserCtxt (ctxt
);
152 static ContactGroup
*
153 contact_group_new (const gchar
*name
,
158 group
= g_new0 (ContactGroup
, 1);
160 group
->name
= g_strdup (name
);
161 group
->expanded
= expanded
;
167 contact_group_free (ContactGroup
*group
)
169 g_return_if_fail (group
!= NULL
);
171 g_free (group
->name
);
177 contact_groups_file_save (void)
186 dir
= g_build_filename (g_get_user_config_dir (), PACKAGE_NAME
, NULL
);
187 g_mkdir_with_parents (dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
);
188 file
= g_build_filename (dir
, CONTACT_GROUPS_XML_FILENAME
, NULL
);
191 doc
= xmlNewDoc ((const xmlChar
*) "1.0");
192 root
= xmlNewNode (NULL
, (const xmlChar
*) "contacts");
193 xmlDocSetRootElement (doc
, root
);
195 node
= xmlNewChild (root
, NULL
, (const xmlChar
*) "account", NULL
);
196 xmlNewProp (node
, (const xmlChar
*) "name", (const xmlChar
*) "Default");
198 for (l
= groups
; l
; l
= l
->next
) {
204 subnode
= xmlNewChild (node
, NULL
, (const xmlChar
*) "group", NULL
);
205 xmlNewProp (subnode
, (const xmlChar
*) "expanded", cg
->expanded
?
206 (const xmlChar
*) "yes" : (const xmlChar
*) "no");
207 xmlNewProp (subnode
, (const xmlChar
*) "name", (const xmlChar
*) cg
->name
);
210 /* Make sure the XML is indented properly */
211 xmlIndentTreeOutput
= 1;
213 DEBUG ("Saving file:'%s'", file
);
214 xmlSaveFormatFileEnc (file
, doc
, "utf-8", 1);
225 empathy_contact_group_get_expanded (const gchar
*group
)
228 gboolean default_val
= TRUE
;
230 g_return_val_if_fail (group
!= NULL
, default_val
);
232 for (l
= groups
; l
; l
= l
->next
) {
233 ContactGroup
*cg
= l
->data
;
235 if (!cg
|| !cg
->name
) {
239 if (strcmp (cg
->name
, group
) == 0) {
248 empathy_contact_group_set_expanded (const gchar
*group
,
253 gboolean changed
= FALSE
;
255 g_return_if_fail (group
!= NULL
);
257 for (l
= groups
; l
; l
= l
->next
) {
260 if (!cg
|| !cg
->name
) {
264 if (strcmp (cg
->name
, group
) == 0) {
265 cg
->expanded
= expanded
;
271 /* if here... we don't have a ContactGroup for the group. */
273 cg
= contact_group_new (group
, expanded
);
274 groups
= g_list_append (groups
, cg
);
277 contact_groups_file_save ();