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.h>
32
#include <libxml/parser.h>
33
#include <libxml/tree.h>
35
#include <libempathy/gossip-debug.h>
36
#include <libempathy/gossip-utils.h>
38
#include "gossip-contact-groups.h"
40
#define DEBUG_DOMAIN "ContactGroups"
42
#define CONTACT_GROUPS_XML_FILENAME "contact-groups.xml"
43
#define CONTACT_GROUPS_DTD_FILENAME "gossip-contact-groups.dtd"
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,
54
static void contact_group_free (ContactGroup *group);
56
static GList *groups = NULL;
59
gossip_contact_groups_get_all (void)
62
gchar *file_with_path;
64
/* If already set up clean up first */
66
g_list_foreach (groups, (GFunc)contact_group_free, 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);
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);
83
contact_groups_file_parse (const gchar *filename)
85
xmlParserCtxtPtr ctxt;
91
gossip_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);
98
g_warning ("Failed to parse file:'%s'", filename);
99
xmlFreeParserCtxt (ctxt);
103
if (!gossip_xml_validate (doc, CONTACT_GROUPS_DTD_FILENAME)) {
104
g_warning ("Failed to validate file:'%s'", filename);
106
xmlFreeParserCtxt (ctxt);
110
/* The root node, contacts. */
111
contacts = xmlDocGetRootElement (doc);
114
node = contacts->children;
116
if (strcmp ((gchar *) node->name, "account") == 0) {
125
node = account->children;
129
if (strcmp ((gchar *) node->name, "group") == 0) {
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) {
144
contact_group = contact_group_new (name, expanded);
145
groups = g_list_append (groups, contact_group);
148
xmlFree (expanded_str);
154
gossip_debug (DEBUG_DOMAIN, "Parsed %d contact groups", g_list_length (groups));
157
xmlFreeParserCtxt (ctxt);
160
static ContactGroup *
161
contact_group_new (const gchar *name,
166
group = g_new0 (ContactGroup, 1);
168
group->name = g_strdup (name);
169
group->expanded = expanded;
175
contact_group_free (ContactGroup *group)
177
g_return_if_fail (group != NULL);
179
g_free (group->name);
185
contact_groups_file_save (void)
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);
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) {
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
gossip_debug (DEBUG_DOMAIN, "Saving file:'%s'", file);
221
xmlSaveFormatFileEnc (file, doc, "utf-8", 1);
233
gossip_contact_group_get_expanded (const gchar *group)
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) {
247
if (strcmp (cg->name, group) == 0) {
256
gossip_contact_group_set_expanded (const gchar *group,
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) {
272
if (strcmp (cg->name, group) == 0) {
273
cg->expanded = expanded;
279
/* if here... we don't have a ContactGroup for the group. */
281
cg = contact_group_new (group, expanded);
282
groups = g_list_append (groups, cg);
285
contact_groups_file_save ();