2 * Copyright (C) 2004-2007 Imendio AB
3 * Copyright (C) 2007-2009 Collabora Ltd.
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: Xavier Claessens <xclaesse@gmail.com>
21 * Martyn Russell <martyn@imendio.com>
27 #include <sys/types.h>
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
33 #include <telepathy-glib/account-manager.h>
34 #include <telepathy-glib/interfaces.h>
36 #include "empathy-tp-chat.h"
37 #include "empathy-chatroom-manager.h"
38 #include "empathy-utils.h"
40 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
41 #include "empathy-debug.h"
43 #define CHATROOMS_XML_FILENAME "chatrooms.xml"
44 #define CHATROOMS_DTD_FILENAME "empathy-chatroom-manager.dtd"
47 static EmpathyChatroomManager *chatroom_manager_singleton = NULL;
49 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatroomManager)
54 TpAccountManager *account_manager;
56 /* source id of the autosave timer */
59 } EmpathyChatroomManagerPriv;
67 static guint signals[LAST_SIGNAL];
77 G_DEFINE_TYPE (EmpathyChatroomManager, empathy_chatroom_manager, G_TYPE_OBJECT);
80 * API to save/load and parse the chatrooms file.
84 chatroom_manager_file_save (EmpathyChatroomManager *manager)
86 EmpathyChatroomManagerPriv *priv;
91 priv = GET_PRIV (manager);
93 doc = xmlNewDoc ((const xmlChar *) "1.0");
94 root = xmlNewNode (NULL, (const xmlChar *) "chatrooms");
95 xmlDocSetRootElement (doc, root);
97 for (l = priv->chatrooms; l; l = l->next) {
98 EmpathyChatroom *chatroom;
100 const gchar *account_id;
104 if (!empathy_chatroom_is_favorite (chatroom)) {
108 account_id = tp_proxy_get_object_path (
109 empathy_chatroom_get_account (chatroom));
111 node = xmlNewChild (root, NULL, (const xmlChar *) "chatroom", NULL);
112 xmlNewTextChild (node, NULL, (const xmlChar *) "name",
113 (const xmlChar *) empathy_chatroom_get_name (chatroom));
114 xmlNewTextChild (node, NULL, (const xmlChar *) "room",
115 (const xmlChar *) empathy_chatroom_get_name (chatroom));
116 xmlNewTextChild (node, NULL, (const xmlChar *) "account",
117 (const xmlChar *) account_id);
118 xmlNewTextChild (node, NULL, (const xmlChar *) "auto_connect",
119 empathy_chatroom_get_auto_connect (chatroom) ?
120 (const xmlChar *) "yes" : (const xmlChar *) "no");
123 /* Make sure the XML is indented properly */
124 xmlIndentTreeOutput = 1;
126 DEBUG ("Saving file:'%s'", priv->file);
127 xmlSaveFormatFileEnc (priv->file, doc, "utf-8", 1);
137 save_timeout (EmpathyChatroomManager *self)
139 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
141 priv->save_timer_id = 0;
142 chatroom_manager_file_save (self);
148 reset_save_timeout (EmpathyChatroomManager *self)
150 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
152 if (priv->save_timer_id > 0)
154 g_source_remove (priv->save_timer_id);
157 priv->save_timer_id = g_timeout_add_seconds (SAVE_TIMER,
158 (GSourceFunc) save_timeout, self);
162 chatroom_changed_cb (EmpathyChatroom *chatroom,
164 EmpathyChatroomManager *self)
166 reset_save_timeout (self);
170 add_chatroom (EmpathyChatroomManager *self,
171 EmpathyChatroom *chatroom)
173 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
175 priv->chatrooms = g_list_prepend (priv->chatrooms, g_object_ref (chatroom));
177 g_signal_connect (chatroom, "notify",
178 G_CALLBACK (chatroom_changed_cb), self);
182 chatroom_manager_parse_chatroom (EmpathyChatroomManager *manager,
185 EmpathyChatroomManagerPriv *priv;
186 EmpathyChatroom *chatroom;
193 gboolean auto_connect;
195 priv = GET_PRIV (manager);
197 /* default values. */
203 for (child = node->children; child; child = child->next) {
206 if (xmlNodeIsText (child)) {
210 tag = (gchar *) child->name;
211 str = (gchar *) xmlNodeGetContent (child);
213 if (strcmp (tag, "name") == 0) {
214 name = g_strdup (str);
216 else if (strcmp (tag, "room") == 0) {
217 room = g_strdup (str);
219 else if (strcmp (tag, "auto_connect") == 0) {
220 if (strcmp (str, "yes") == 0) {
223 auto_connect = FALSE;
226 else if (strcmp (tag, "account") == 0) {
227 account_id = g_strdup (str);
233 account = tp_account_manager_ensure_account (priv->account_manager,
242 chatroom = empathy_chatroom_new_full (account, room, name, auto_connect);
243 empathy_chatroom_set_favorite (chatroom, TRUE);
244 add_chatroom (manager, chatroom);
245 g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
253 chatroom_manager_file_parse (EmpathyChatroomManager *manager,
254 const gchar *filename)
256 EmpathyChatroomManagerPriv *priv;
257 xmlParserCtxtPtr ctxt;
259 xmlNodePtr chatrooms;
262 priv = GET_PRIV (manager);
264 DEBUG ("Attempting to parse file:'%s'...", filename);
266 ctxt = xmlNewParserCtxt ();
268 /* Parse and validate the file. */
269 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
271 g_warning ("Failed to parse file:'%s'", filename);
272 xmlFreeParserCtxt (ctxt);
276 if (!empathy_xml_validate (doc, CHATROOMS_DTD_FILENAME)) {
277 g_warning ("Failed to validate file:'%s'", filename);
279 xmlFreeParserCtxt (ctxt);
283 /* The root node, chatrooms. */
284 chatrooms = xmlDocGetRootElement (doc);
286 for (node = chatrooms->children; node; node = node->next) {
287 if (strcmp ((gchar *) node->name, "chatroom") == 0) {
288 chatroom_manager_parse_chatroom (manager, node);
292 DEBUG ("Parsed %d chatrooms", g_list_length (priv->chatrooms));
295 xmlFreeParserCtxt (ctxt);
301 chatroom_manager_get_all (EmpathyChatroomManager *manager)
303 EmpathyChatroomManagerPriv *priv;
305 priv = GET_PRIV (manager);
308 if (g_file_test (priv->file, G_FILE_TEST_EXISTS) &&
309 !chatroom_manager_file_parse (manager, priv->file)) {
314 g_object_notify (G_OBJECT (manager), "ready");
320 empathy_chatroom_manager_get_property (GObject *object,
325 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
326 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
331 g_value_set_string (value, priv->file);
334 g_value_set_boolean (value, priv->ready);
337 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
343 empathy_chatroom_manager_set_property (GObject *object,
348 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
349 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
355 priv->file = g_value_dup_string (value);
358 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
364 chatroom_manager_finalize (GObject *object)
366 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
367 EmpathyChatroomManagerPriv *priv;
370 priv = GET_PRIV (object);
372 g_object_unref (priv->account_manager);
374 if (priv->save_timer_id > 0)
376 /* have to save before destroy the object */
377 g_source_remove (priv->save_timer_id);
378 priv->save_timer_id = 0;
379 chatroom_manager_file_save (self);
382 for (l = priv->chatrooms; l != NULL; l = g_list_next (l))
384 EmpathyChatroom *chatroom = l->data;
386 g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb,
389 g_object_unref (chatroom);
392 g_list_free (priv->chatrooms);
395 (G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->finalize) (object);
399 account_manager_ready_cb (GObject *source_object,
400 GAsyncResult *result,
403 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (user_data);
404 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
405 GError *error = NULL;
407 if (!tp_account_manager_prepare_finish (manager, result, &error))
409 DEBUG ("Failed to prepare account manager: %s", error->message);
410 g_error_free (error);
414 chatroom_manager_get_all (self);
418 empathy_chatroom_manager_constructor (GType type,
420 GObjectConstructParam *props)
423 EmpathyChatroomManager *self;
424 EmpathyChatroomManagerPriv *priv;
426 if (chatroom_manager_singleton != NULL)
427 return g_object_ref (chatroom_manager_singleton);
429 /* Parent constructor chain */
430 obj = G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->
431 constructor (type, n_props, props);
433 self = EMPATHY_CHATROOM_MANAGER (obj);
434 priv = GET_PRIV (self);
438 chatroom_manager_singleton = self;
439 g_object_add_weak_pointer (obj, (gpointer) &chatroom_manager_singleton);
441 priv->account_manager = tp_account_manager_dup ();
443 tp_account_manager_prepare_async (priv->account_manager, NULL,
444 account_manager_ready_cb, self);
446 if (priv->file == NULL)
448 /* Set the default file path */
451 dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
452 if (!g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
453 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
455 priv->file = g_build_filename (dir, CHATROOMS_XML_FILENAME, NULL);
463 empathy_chatroom_manager_class_init (EmpathyChatroomManagerClass *klass)
465 GObjectClass *object_class = G_OBJECT_CLASS (klass);
466 GParamSpec *param_spec;
468 object_class->constructor = empathy_chatroom_manager_constructor;
469 object_class->get_property = empathy_chatroom_manager_get_property;
470 object_class->set_property = empathy_chatroom_manager_set_property;
471 object_class->finalize = chatroom_manager_finalize;
473 param_spec = g_param_spec_string (
475 "path of the favorite file",
476 "The path of the XML file containing user's favorites",
478 G_PARAM_CONSTRUCT_ONLY |
480 G_PARAM_STATIC_NAME |
481 G_PARAM_STATIC_NICK |
482 G_PARAM_STATIC_BLURB);
483 g_object_class_install_property (object_class, PROP_FILE, param_spec);
485 param_spec = g_param_spec_boolean (
487 "whether the manager is ready yet",
488 "whether the manager is ready yet",
491 g_object_class_install_property (object_class, PROP_READY, param_spec);
493 signals[CHATROOM_ADDED] = g_signal_new ("chatroom-added",
494 G_TYPE_FROM_CLASS (klass),
497 g_cclosure_marshal_VOID__OBJECT,
499 1, EMPATHY_TYPE_CHATROOM);
501 signals[CHATROOM_REMOVED] = g_signal_new ("chatroom-removed",
502 G_TYPE_FROM_CLASS (klass),
505 g_cclosure_marshal_VOID__OBJECT,
507 1, EMPATHY_TYPE_CHATROOM);
509 g_type_class_add_private (object_class, sizeof (EmpathyChatroomManagerPriv));
513 empathy_chatroom_manager_init (EmpathyChatroomManager *manager)
515 EmpathyChatroomManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
516 EMPATHY_TYPE_CHATROOM_MANAGER, EmpathyChatroomManagerPriv);
518 manager->priv = priv;
521 EmpathyChatroomManager *
522 empathy_chatroom_manager_dup_singleton (const gchar *file)
524 return EMPATHY_CHATROOM_MANAGER (g_object_new (EMPATHY_TYPE_CHATROOM_MANAGER,
525 "file", file, NULL));
529 empathy_chatroom_manager_add (EmpathyChatroomManager *manager,
530 EmpathyChatroom *chatroom)
532 EmpathyChatroomManagerPriv *priv;
534 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), FALSE);
535 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), FALSE);
537 priv = GET_PRIV (manager);
539 /* don't add more than once */
540 if (!empathy_chatroom_manager_find (manager,
541 empathy_chatroom_get_account (chatroom),
542 empathy_chatroom_get_room (chatroom)))
544 add_chatroom (manager, chatroom);
546 if (empathy_chatroom_is_favorite (chatroom))
547 reset_save_timeout (manager);
549 g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
557 chatroom_manager_remove_link (EmpathyChatroomManager *manager,
560 EmpathyChatroomManagerPriv *priv;
561 EmpathyChatroom *chatroom;
563 priv = GET_PRIV (manager);
567 if (empathy_chatroom_is_favorite (chatroom))
568 reset_save_timeout (manager);
570 priv->chatrooms = g_list_delete_link (priv->chatrooms, l);
572 g_signal_emit (manager, signals[CHATROOM_REMOVED], 0, chatroom);
573 g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb, manager);
575 g_object_unref (chatroom);
579 empathy_chatroom_manager_remove (EmpathyChatroomManager *manager,
580 EmpathyChatroom *chatroom)
582 EmpathyChatroomManagerPriv *priv;
585 g_return_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager));
586 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
588 priv = GET_PRIV (manager);
590 for (l = priv->chatrooms; l; l = l->next)
592 EmpathyChatroom *this_chatroom;
594 this_chatroom = l->data;
596 if (this_chatroom == chatroom ||
597 empathy_chatroom_equal (chatroom, this_chatroom))
599 chatroom_manager_remove_link (manager, l);
606 empathy_chatroom_manager_find (EmpathyChatroomManager *manager,
610 EmpathyChatroomManagerPriv *priv;
613 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
614 g_return_val_if_fail (room != NULL, NULL);
616 priv = GET_PRIV (manager);
618 for (l = priv->chatrooms; l; l = l->next) {
619 EmpathyChatroom *chatroom;
620 TpAccount *this_account;
621 const gchar *this_room;
624 this_account = empathy_chatroom_get_account (chatroom);
625 this_room = empathy_chatroom_get_room (chatroom);
627 if (this_account && this_room && account == this_account
628 && strcmp (this_room, room) == 0) {
637 empathy_chatroom_manager_get_chatrooms (EmpathyChatroomManager *manager,
640 EmpathyChatroomManagerPriv *priv;
641 GList *chatrooms, *l;
643 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
645 priv = GET_PRIV (manager);
648 return g_list_copy (priv->chatrooms);
652 for (l = priv->chatrooms; l; l = l->next) {
653 EmpathyChatroom *chatroom;
657 if (account == empathy_chatroom_get_account (chatroom)) {
658 chatrooms = g_list_append (chatrooms, chatroom);
666 empathy_chatroom_manager_get_count (EmpathyChatroomManager *manager,
669 EmpathyChatroomManagerPriv *priv;
673 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), 0);
675 priv = GET_PRIV (manager);
678 return g_list_length (priv->chatrooms);
681 for (l = priv->chatrooms; l; l = l->next) {
682 EmpathyChatroom *chatroom;
686 if (account == empathy_chatroom_get_account (chatroom)) {
695 chatroom_manager_chat_destroyed_cb (EmpathyTpChat *chat,
698 EmpathyChatroomManagerPriv *priv = GET_PRIV (manager);
701 for (l = priv->chatrooms; l; l = l->next)
703 EmpathyChatroom *chatroom = l->data;
705 if (empathy_chatroom_get_tp_chat (chatroom) != chat)
708 empathy_chatroom_set_tp_chat (chatroom, NULL);
710 if (!empathy_chatroom_is_favorite (chatroom))
712 /* Remove the chatroom from the list, unless it's in the list of
714 * FIXME this policy should probably not be in libempathy */
715 chatroom_manager_remove_link (manager, l);
723 chatroom_manager_observe_channel_cb (EmpathyDispatcher *dispatcher,
724 EmpathyDispatchOperation *operation, gpointer manager)
726 EmpathyChatroom *chatroom;
729 const gchar *roomname;
731 TpHandleType handle_type;
733 TpConnection *connection;
735 channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
737 /* Observe Text channels to rooms only */
738 if (channel_type != TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
741 channel = empathy_dispatch_operation_get_channel (operation);
742 tp_channel_get_handle (channel, &handle_type);
744 if (handle_type != TP_HANDLE_TYPE_ROOM)
747 chat = EMPATHY_TP_CHAT (
748 empathy_dispatch_operation_get_channel_wrapper (operation));
749 connection = empathy_tp_chat_get_connection (chat);
750 account = empathy_get_account_for_connection (connection);
752 roomname = empathy_tp_chat_get_id (chat);
754 chatroom = empathy_chatroom_manager_find (manager, account, roomname);
756 if (chatroom == NULL)
758 chatroom = empathy_chatroom_new_full (account, roomname, roomname,
760 empathy_chatroom_set_tp_chat (chatroom, chat);
761 empathy_chatroom_manager_add (manager, chatroom);
762 g_object_unref (chatroom);
766 empathy_chatroom_set_tp_chat (chatroom, chat);
769 /* A TpChat is always destroyed as it only gets unreffed after the channel
770 * has been invalidated in the dispatcher.. */
771 g_signal_connect (chat, "destroy",
772 G_CALLBACK (chatroom_manager_chat_destroyed_cb),
777 empathy_chatroom_manager_observe (EmpathyChatroomManager *manager,
778 EmpathyDispatcher *dispatcher)
780 g_signal_connect (dispatcher, "observe",
781 G_CALLBACK (chatroom_manager_observe_channel_cb), manager);