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., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Martyn Russell <martyn@imendio.com>
26 #include <sys/types.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"
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
,
53 static void contact_group_free (ContactGroup
*group
);
55 static GList
*groups
= NULL
;
58 empathy_contact_groups_get_all (void)
61 gchar
*file_with_path
;
63 /* If already set up clean up first */
65 g_list_foreach (groups
, (GFunc
)contact_group_free
, NULL
);
70 dir
= g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME
, NULL
);
71 file_with_path
= g_build_filename (dir
, CONTACT_GROUPS_XML_FILENAME
, NULL
);
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
);
82 contact_groups_file_parse (const gchar
*filename
)
84 xmlParserCtxtPtr ctxt
;
90 DEBUG ("Attempting to parse file:'%s'...", filename
);
92 ctxt
= xmlNewParserCtxt ();
94 /* Parse and validate the file. */
95 doc
= xmlCtxtReadFile (ctxt
, filename
, NULL
, 0);
97 g_warning ("Failed to parse file:'%s'", filename
);
98 xmlFreeParserCtxt (ctxt
);
102 if (!empathy_xml_validate (doc
, CONTACT_GROUPS_DTD_FILENAME
)) {
103 g_warning ("Failed to validate file:'%s'", filename
);
105 xmlFreeParserCtxt (ctxt
);
109 /* The root node, contacts. */
110 contacts
= xmlDocGetRootElement (doc
);
113 node
= contacts
->children
;
115 if (strcmp ((gchar
*) node
->name
, "account") == 0) {
124 node
= account
->children
;
128 if (strcmp ((gchar
*) node
->name
, "group") == 0) {
132 ContactGroup
*contact_group
;
134 name
= (gchar
*) xmlGetProp (node
, "name");
135 expanded_str
= (gchar
*) xmlGetProp (node
, "expanded");
137 if (expanded_str
&& strcmp (expanded_str
, "yes") == 0) {
143 contact_group
= contact_group_new (name
, expanded
);
144 groups
= g_list_append (groups
, contact_group
);
147 xmlFree (expanded_str
);
153 DEBUG ("Parsed %d contact groups", g_list_length (groups
));
156 xmlFreeParserCtxt (ctxt
);
159 static ContactGroup
*
160 contact_group_new (const gchar
*name
,
165 group
= g_new0 (ContactGroup
, 1);
167 group
->name
= g_strdup (name
);
168 group
->expanded
= expanded
;
174 contact_group_free (ContactGroup
*group
)
176 g_return_if_fail (group
!= NULL
);
178 g_free (group
->name
);
184 contact_groups_file_save (void)
193 dir
= g_build_filename (g_get_home_dir (), ".gnome2", 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
);
198 doc
= xmlNewDoc ("1.0");
199 root
= xmlNewNode (NULL
, "contacts");
200 xmlDocSetRootElement (doc
, root
);
202 node
= xmlNewChild (root
, NULL
, "account", NULL
);
203 xmlNewProp (node
, "name", "Default");
205 for (l
= groups
; l
; l
= l
->next
) {
211 subnode
= xmlNewChild (node
, NULL
, "group", NULL
);
212 xmlNewProp (subnode
, "expanded", cg
->expanded
? "yes" : "no");
213 xmlNewProp (subnode
, "name", cg
->name
);
216 /* Make sure the XML is indented properly */
217 xmlIndentTreeOutput
= 1;
219 DEBUG ("Saving file:'%s'", file
);
220 xmlSaveFormatFileEnc (file
, doc
, "utf-8", 1);
232 empathy_contact_group_get_expanded (const gchar
*group
)
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
) {
246 if (strcmp (cg
->name
, group
) == 0) {
255 empathy_contact_group_set_expanded (const gchar
*group
,
260 gboolean changed
= FALSE
;
262 g_return_if_fail (group
!= NULL
);
264 for (l
= groups
; l
; l
= l
->next
) {
265 ContactGroup
*cg
= l
->data
;
267 if (!cg
|| !cg
->name
) {
271 if (strcmp (cg
->name
, group
) == 0) {
272 cg
->expanded
= expanded
;
278 /* if here... we don't have a ContactGroup for the group. */
280 cg
= contact_group_new (group
, expanded
);
281 groups
= g_list_append (groups
, cg
);
284 contact_groups_file_save ();