1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2004-2007 Imendio AB
4 * Copyright (C) 2007 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Xavier Claessens <xclaesse@gmail.com>
22 * Martyn Russell <martyn@imendio.com>
28 #include <sys/types.h>
31 #include <libxml/parser.h>
32 #include <libxml/tree.h>
34 #include "gossip-debug.h"
35 #include "gossip-chatroom-manager.h"
36 #include "gossip-utils.h"
38 #define DEBUG_DOMAIN "ChatroomManager"
40 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_CHATROOM_MANAGER, GossipChatroomManagerPriv))
42 #define CHATROOMS_XML_FILENAME "chatrooms.xml"
43 #define CHATROOMS_DTD_FILENAME "gossip-chatroom-manager.dtd"
45 struct _GossipChatroomManagerPriv {
49 static void gossip_chatroom_manager_class_init (GossipChatroomManagerClass *klass);
50 static void gossip_chatroom_manager_init (GossipChatroomManager *manager);
51 static void chatroom_manager_finalize (GObject *object);
52 static gboolean chatroom_manager_get_all (GossipChatroomManager *manager);
53 static gboolean chatroom_manager_file_parse (GossipChatroomManager *manager,
54 const gchar *filename);
55 static void chatroom_manager_parse_chatroom (GossipChatroomManager *manager,
57 static gboolean chatroom_manager_file_save (GossipChatroomManager *manager);
65 static guint signals[LAST_SIGNAL];
67 G_DEFINE_TYPE (GossipChatroomManager, gossip_chatroom_manager, G_TYPE_OBJECT);
70 gossip_chatroom_manager_class_init (GossipChatroomManagerClass *klass)
72 GObjectClass *object_class = G_OBJECT_CLASS (klass);
74 object_class->finalize = chatroom_manager_finalize;
76 signals[CHATROOM_ADDED] =
77 g_signal_new ("chatroom-added",
78 G_TYPE_FROM_CLASS (klass),
82 g_cclosure_marshal_VOID__OBJECT,
84 1, GOSSIP_TYPE_CHATROOM);
85 signals[CHATROOM_REMOVED] =
86 g_signal_new ("chatroom-removed",
87 G_TYPE_FROM_CLASS (klass),
91 g_cclosure_marshal_VOID__OBJECT,
93 1, GOSSIP_TYPE_CHATROOM);
95 g_type_class_add_private (object_class,
96 sizeof (GossipChatroomManagerPriv));
100 gossip_chatroom_manager_init (GossipChatroomManager *manager)
105 chatroom_manager_finalize (GObject *object)
107 GossipChatroomManagerPriv *priv;
109 priv = GET_PRIV (object);
111 g_list_foreach (priv->chatrooms, (GFunc) g_object_unref, NULL);
112 g_list_free (priv->chatrooms);
114 (G_OBJECT_CLASS (gossip_chatroom_manager_parent_class)->finalize) (object);
117 GossipChatroomManager *
118 gossip_chatroom_manager_new (void)
120 static GossipChatroomManager *manager = NULL;
123 GossipChatroomManagerPriv *priv;
125 manager = g_object_new (GOSSIP_TYPE_CHATROOM_MANAGER, NULL);
126 priv = GET_PRIV (manager);
127 chatroom_manager_get_all (manager);
129 g_object_add_weak_pointer (G_OBJECT (manager), (gpointer) &manager);
131 g_object_ref (manager);
138 gossip_chatroom_manager_add (GossipChatroomManager *manager,
139 GossipChatroom *chatroom)
141 GossipChatroomManagerPriv *priv;
143 g_return_val_if_fail (GOSSIP_IS_CHATROOM_MANAGER (manager), FALSE);
144 g_return_val_if_fail (GOSSIP_IS_CHATROOM (chatroom), FALSE);
146 priv = GET_PRIV (manager);
148 /* don't add more than once */
149 if (!gossip_chatroom_manager_find (manager,
150 gossip_chatroom_get_account (chatroom),
151 gossip_chatroom_get_room (chatroom))) {
152 priv->chatrooms = g_list_append (priv->chatrooms, g_object_ref (chatroom));
153 chatroom_manager_file_save (manager);
155 g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
164 gossip_chatroom_manager_remove (GossipChatroomManager *manager,
165 GossipChatroom *chatroom)
167 GossipChatroomManagerPriv *priv;
170 g_return_if_fail (GOSSIP_IS_CHATROOM_MANAGER (manager));
171 g_return_if_fail (GOSSIP_IS_CHATROOM (chatroom));
173 priv = GET_PRIV (manager);
175 for (l = priv->chatrooms; l; l = l->next) {
176 GossipChatroom *this_chatroom;
178 this_chatroom = l->data;
180 if (gossip_chatroom_equal (chatroom, this_chatroom)) {
181 priv->chatrooms = g_list_delete_link (priv->chatrooms, l);
183 chatroom_manager_file_save (manager);
185 g_signal_emit (manager, signals[CHATROOM_REMOVED], 0, this_chatroom);
186 g_object_unref (this_chatroom);
193 gossip_chatroom_manager_find (GossipChatroomManager *manager,
197 GossipChatroomManagerPriv *priv;
200 g_return_val_if_fail (GOSSIP_IS_CHATROOM_MANAGER (manager), NULL);
201 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
202 g_return_val_if_fail (room != NULL, NULL);
204 priv = GET_PRIV (manager);
206 for (l = priv->chatrooms; l; l = l->next) {
207 GossipChatroom *chatroom;
208 McAccount *this_account;
209 const gchar *this_room;
212 this_account = gossip_chatroom_get_account (chatroom);
213 this_room = gossip_chatroom_get_room (chatroom);
215 if (gossip_account_equal (account, this_account) &&
216 strcmp (this_room, room) == 0) {
225 gossip_chatroom_manager_get_chatrooms (GossipChatroomManager *manager,
228 GossipChatroomManagerPriv *priv;
229 GList *chatrooms, *l;
231 g_return_val_if_fail (GOSSIP_IS_CHATROOM_MANAGER (manager), NULL);
233 priv = GET_PRIV (manager);
236 return g_list_copy (priv->chatrooms);
240 for (l = priv->chatrooms; l; l = l->next) {
241 GossipChatroom *chatroom;
245 if (gossip_account_equal (account,
246 gossip_chatroom_get_account (chatroom))) {
247 chatrooms = g_list_append (chatrooms, chatroom);
255 gossip_chatroom_manager_get_count (GossipChatroomManager *manager,
258 GossipChatroomManagerPriv *priv;
262 g_return_val_if_fail (GOSSIP_IS_CHATROOM_MANAGER (manager), 0);
264 priv = GET_PRIV (manager);
267 return g_list_length (priv->chatrooms);
270 for (l = priv->chatrooms; l; l = l->next) {
271 GossipChatroom *chatroom;
275 if (gossip_account_equal (account,
276 gossip_chatroom_get_account (chatroom))) {
285 gossip_chatroom_manager_store (GossipChatroomManager *manager)
287 g_return_if_fail (GOSSIP_IS_CHATROOM_MANAGER (manager));
289 chatroom_manager_file_save (manager);
293 * API to save/load and parse the chatrooms file.
297 chatroom_manager_get_all (GossipChatroomManager *manager)
299 GossipChatroomManagerPriv *priv;
301 gchar *file_with_path = NULL;
303 priv = GET_PRIV (manager);
305 dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
306 if (!g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
307 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
310 file_with_path = g_build_filename (dir, CHATROOMS_XML_FILENAME, NULL);
314 if (g_file_test (file_with_path, G_FILE_TEST_EXISTS) &&
315 !chatroom_manager_file_parse (manager, file_with_path)) {
316 g_free (file_with_path);
320 g_free (file_with_path);
326 chatroom_manager_file_parse (GossipChatroomManager *manager,
327 const gchar *filename)
329 GossipChatroomManagerPriv *priv;
330 xmlParserCtxtPtr ctxt;
332 xmlNodePtr chatrooms;
335 priv = GET_PRIV (manager);
337 gossip_debug (DEBUG_DOMAIN, "Attempting to parse file:'%s'...", filename);
339 ctxt = xmlNewParserCtxt ();
341 /* Parse and validate the file. */
342 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
344 g_warning ("Failed to parse file:'%s'", filename);
345 xmlFreeParserCtxt (ctxt);
349 if (!gossip_xml_validate (doc, CHATROOMS_DTD_FILENAME)) {
350 g_warning ("Failed to validate file:'%s'", filename);
352 xmlFreeParserCtxt (ctxt);
356 /* The root node, chatrooms. */
357 chatrooms = xmlDocGetRootElement (doc);
359 for (node = chatrooms->children; node; node = node->next) {
360 if (strcmp ((gchar *) node->name, "chatroom") == 0) {
361 chatroom_manager_parse_chatroom (manager, node);
365 gossip_debug (DEBUG_DOMAIN,
366 "Parsed %d chatrooms",
367 g_list_length (priv->chatrooms));
370 xmlFreeParserCtxt (ctxt);
376 chatroom_manager_parse_chatroom (GossipChatroomManager *manager,
379 GossipChatroomManagerPriv *priv;
380 GossipChatroom *chatroom;
387 gboolean auto_connect;
389 priv = GET_PRIV (manager);
391 /* default values. */
397 for (child = node->children; child; child = child->next) {
400 if (xmlNodeIsText (child)) {
404 tag = (gchar *) child->name;
405 str = (gchar *) xmlNodeGetContent (child);
407 if (strcmp (tag, "name") == 0) {
408 name = g_strdup (str);
410 else if (strcmp (tag, "room") == 0) {
411 room = g_strdup (str);
413 else if (strcmp (tag, "auto_connect") == 0) {
414 if (strcmp (str, "yes") == 0) {
417 auto_connect = FALSE;
420 else if (strcmp (tag, "account") == 0) {
421 account_id = g_strdup (str);
427 account = mc_account_lookup (account_id);
435 chatroom = gossip_chatroom_new_full (account,
440 priv->chatrooms = g_list_prepend (priv->chatrooms, chatroom);
442 g_object_unref (account);
449 chatroom_manager_file_save (GossipChatroomManager *manager)
451 GossipChatroomManagerPriv *priv;
458 priv = GET_PRIV (manager);
460 dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
461 if (!g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
462 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
465 file = g_build_filename (dir, CHATROOMS_XML_FILENAME, NULL);
468 doc = xmlNewDoc ("1.0");
469 root = xmlNewNode (NULL, "chatrooms");
470 xmlDocSetRootElement (doc, root);
472 for (l = priv->chatrooms; l; l = l->next) {
473 GossipChatroom *chatroom;
475 const gchar *account_id;
478 account_id = mc_account_get_unique_name (gossip_chatroom_get_account (chatroom));
480 node = xmlNewChild (root, NULL, "chatroom", NULL);
481 xmlNewTextChild (node, NULL, "name", gossip_chatroom_get_name (chatroom));
482 xmlNewTextChild (node, NULL, "room", gossip_chatroom_get_room (chatroom));
483 xmlNewTextChild (node, NULL, "account", account_id);
484 xmlNewTextChild (node, NULL, "auto_connect", gossip_chatroom_get_auto_connect (chatroom) ? "yes" : "no");
487 /* Make sure the XML is indented properly */
488 xmlIndentTreeOutput = 1;
490 gossip_debug (DEBUG_DOMAIN, "Saving file:'%s'", file);
491 xmlSaveFormatFileEnc (file, doc, "utf-8", 1);