2 * Copyright (C) 2004-2007 Imendio AB
3 * Copyright (C) 2007-2010 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>
35 #include <telepathy-glib/simple-observer.h>
36 #include <telepathy-glib/util.h>
38 #include "empathy-tp-chat.h"
39 #include "empathy-chatroom-manager.h"
40 #include "empathy-utils.h"
42 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
43 #include "empathy-debug.h"
45 #define CHATROOMS_XML_FILENAME "chatrooms.xml"
46 #define CHATROOMS_DTD_FILENAME "empathy-chatroom-manager.dtd"
49 static EmpathyChatroomManager *chatroom_manager_singleton = NULL;
51 static void observe_channels_cb (TpSimpleObserver *observer,
53 TpConnection *connection,
55 TpChannelDispatchOperation *dispatch_operation,
57 TpObserveChannelsContext *context,
60 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatroomManager)
65 TpAccountManager *account_manager;
67 /* source id of the autosave timer */
70 GFileMonitor *monitor;
73 TpBaseClient *observer;
74 } EmpathyChatroomManagerPriv;
82 static guint signals[LAST_SIGNAL];
92 G_DEFINE_TYPE (EmpathyChatroomManager, empathy_chatroom_manager, G_TYPE_OBJECT);
95 * API to save/load and parse the chatrooms file.
99 chatroom_manager_file_save (EmpathyChatroomManager *manager)
101 EmpathyChatroomManagerPriv *priv;
106 priv = GET_PRIV (manager);
108 priv->writing = TRUE;
110 doc = xmlNewDoc ((const xmlChar *) "1.0");
111 root = xmlNewNode (NULL, (const xmlChar *) "chatrooms");
112 xmlDocSetRootElement (doc, root);
114 for (l = priv->chatrooms; l; l = l->next)
116 EmpathyChatroom *chatroom;
118 const gchar *account_id;
122 if (!empathy_chatroom_is_favorite (chatroom))
125 account_id = tp_proxy_get_object_path (empathy_chatroom_get_account (
128 node = xmlNewChild (root, NULL, (const xmlChar *) "chatroom", NULL);
129 xmlNewTextChild (node, NULL, (const xmlChar *) "name",
130 (const xmlChar *) empathy_chatroom_get_name (chatroom));
131 xmlNewTextChild (node, NULL, (const xmlChar *) "room",
132 (const xmlChar *) empathy_chatroom_get_room (chatroom));
133 xmlNewTextChild (node, NULL, (const xmlChar *) "account",
134 (const xmlChar *) account_id);
135 xmlNewTextChild (node, NULL, (const xmlChar *) "auto_connect",
136 empathy_chatroom_get_auto_connect (chatroom) ?
137 (const xmlChar *) "yes" : (const xmlChar *) "no");
138 xmlNewTextChild (node, NULL, (const xmlChar *) "always_urgent",
139 empathy_chatroom_is_always_urgent (chatroom) ?
140 (const xmlChar *) "yes" : (const xmlChar *) "no");
143 /* Make sure the XML is indented properly */
144 xmlIndentTreeOutput = 1;
146 DEBUG ("Saving file:'%s'", priv->file);
147 xmlSaveFormatFileEnc (priv->file, doc, "utf-8", 1);
152 priv->writing = FALSE;
157 save_timeout (EmpathyChatroomManager *self)
159 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
161 priv->save_timer_id = 0;
162 chatroom_manager_file_save (self);
168 reset_save_timeout (EmpathyChatroomManager *self)
170 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
172 if (priv->save_timer_id > 0)
173 g_source_remove (priv->save_timer_id);
175 priv->save_timer_id = g_timeout_add_seconds (SAVE_TIMER,
176 (GSourceFunc) save_timeout, self);
180 chatroom_changed_cb (EmpathyChatroom *chatroom,
182 EmpathyChatroomManager *self)
184 reset_save_timeout (self);
188 add_chatroom (EmpathyChatroomManager *self,
189 EmpathyChatroom *chatroom)
191 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
193 priv->chatrooms = g_list_prepend (priv->chatrooms, g_object_ref (chatroom));
195 g_signal_connect (chatroom, "notify",
196 G_CALLBACK (chatroom_changed_cb), self);
200 chatroom_manager_parse_chatroom (EmpathyChatroomManager *manager,
203 EmpathyChatroomManagerPriv *priv;
204 EmpathyChatroom *chatroom;
211 gboolean auto_connect;
212 gboolean always_urgent;
214 priv = GET_PRIV (manager);
216 /* default values. */
220 always_urgent = FALSE;
223 for (child = node->children; child; child = child->next)
227 if (xmlNodeIsText (child))
230 tag = (gchar *) child->name;
231 str = (gchar *) xmlNodeGetContent (child);
233 if (strcmp (tag, "name") == 0)
235 name = g_strdup (str);
237 else if (strcmp (tag, "room") == 0)
239 room = g_strdup (str);
241 else if (strcmp (tag, "auto_connect") == 0)
243 if (strcmp (str, "yes") == 0)
246 auto_connect = FALSE;
248 else if (!tp_strdiff (tag, "always_urgent"))
250 if (strcmp (str, "yes") == 0)
251 always_urgent = TRUE;
253 always_urgent = FALSE;
255 else if (strcmp (tag, "account") == 0)
257 account_id = g_strdup (str);
263 account = tp_account_manager_ensure_account (priv->account_manager,
273 chatroom = empathy_chatroom_new_full (account, room, name, auto_connect);
274 empathy_chatroom_set_favorite (chatroom, TRUE);
275 empathy_chatroom_set_always_urgent (chatroom, always_urgent);
276 add_chatroom (manager, chatroom);
277 g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
285 chatroom_manager_file_parse (EmpathyChatroomManager *manager,
286 const gchar *filename)
288 EmpathyChatroomManagerPriv *priv;
289 xmlParserCtxtPtr ctxt;
291 xmlNodePtr chatrooms;
294 priv = GET_PRIV (manager);
296 DEBUG ("Attempting to parse file:'%s'...", filename);
298 ctxt = xmlNewParserCtxt ();
300 /* Parse and validate the file. */
301 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
304 g_warning ("Failed to parse file:'%s'", filename);
305 xmlFreeParserCtxt (ctxt);
309 if (!empathy_xml_validate (doc, CHATROOMS_DTD_FILENAME))
311 g_warning ("Failed to validate file:'%s'", filename);
313 xmlFreeParserCtxt (ctxt);
317 /* The root node, chatrooms. */
318 chatrooms = xmlDocGetRootElement (doc);
320 for (node = chatrooms->children; node; node = node->next)
322 if (strcmp ((gchar *) node->name, "chatroom") == 0)
323 chatroom_manager_parse_chatroom (manager, node);
326 DEBUG ("Parsed %d chatrooms", g_list_length (priv->chatrooms));
329 xmlFreeParserCtxt (ctxt);
335 chatroom_manager_get_all (EmpathyChatroomManager *manager)
337 EmpathyChatroomManagerPriv *priv;
339 priv = GET_PRIV (manager);
342 if (g_file_test (priv->file, G_FILE_TEST_EXISTS) &&
343 !chatroom_manager_file_parse (manager, priv->file))
349 g_object_notify (G_OBJECT (manager), "ready");
356 empathy_chatroom_manager_get_property (GObject *object,
361 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
362 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
367 g_value_set_string (value, priv->file);
370 g_value_set_boolean (value, priv->ready);
373 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
379 empathy_chatroom_manager_set_property (GObject *object,
384 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
385 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
391 priv->file = g_value_dup_string (value);
394 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
400 chatroom_manager_dispose (GObject *object)
402 EmpathyChatroomManagerPriv *priv;
404 priv = GET_PRIV (object);
406 tp_clear_object (&priv->observer);
407 tp_clear_object (&priv->monitor);
409 (G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->dispose) (object);
413 clear_chatrooms (EmpathyChatroomManager *self)
415 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
418 for (l = priv->chatrooms; l != NULL; l = g_list_next (l))
420 EmpathyChatroom *chatroom = l->data;
422 g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb,
424 g_signal_emit (self, signals[CHATROOM_REMOVED], 0, chatroom);
426 g_object_unref (chatroom);
429 g_list_free (priv->chatrooms);
430 priv->chatrooms = NULL;
434 chatroom_manager_finalize (GObject *object)
436 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
437 EmpathyChatroomManagerPriv *priv;
439 priv = GET_PRIV (object);
441 g_object_unref (priv->account_manager);
443 if (priv->save_timer_id > 0)
445 /* have to save before destroy the object */
446 g_source_remove (priv->save_timer_id);
447 priv->save_timer_id = 0;
448 chatroom_manager_file_save (self);
451 clear_chatrooms (self);
455 (G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->finalize) (object);
459 file_changed_cb (GFileMonitor *monitor,
462 GFileMonitorEvent event_type,
465 EmpathyChatroomManager *self = user_data;
466 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
468 if (event_type != G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
474 DEBUG ("chatrooms file changed; reloading list");
476 clear_chatrooms (self);
477 chatroom_manager_get_all (self);
481 account_manager_ready_cb (GObject *source_object,
482 GAsyncResult *result,
485 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (user_data);
486 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
487 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
488 GError *error = NULL;
491 if (!tp_account_manager_prepare_finish (manager, result, &error))
493 DEBUG ("Failed to prepare account manager: %s", error->message);
494 g_error_free (error);
498 chatroom_manager_get_all (self);
500 /* Set up file monitor */
501 file = g_file_new_for_path (priv->file);
503 priv->monitor = g_file_monitor (file, 0, NULL, &error);
504 if (priv->monitor == NULL)
506 DEBUG ("Failed to create file monitor on %s: %s", priv->file,
509 g_error_free (error);
513 g_signal_connect (priv->monitor, "changed", G_CALLBACK (file_changed_cb),
517 tp_clear_object (&file);
518 g_object_unref (self);
522 empathy_chatroom_manager_constructor (GType type,
524 GObjectConstructParam *props)
527 EmpathyChatroomManager *self;
528 EmpathyChatroomManagerPriv *priv;
529 GError *error = NULL;
532 if (chatroom_manager_singleton != NULL)
533 return g_object_ref (chatroom_manager_singleton);
535 /* Parent constructor chain */
536 obj = G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->
537 constructor (type, n_props, props);
539 self = EMPATHY_CHATROOM_MANAGER (obj);
540 priv = GET_PRIV (self);
544 chatroom_manager_singleton = self;
545 g_object_add_weak_pointer (obj, (gpointer) &chatroom_manager_singleton);
547 priv->account_manager = tp_account_manager_dup ();
549 tp_account_manager_prepare_async (priv->account_manager, NULL,
550 account_manager_ready_cb, g_object_ref (self));
552 if (priv->file == NULL)
554 /* Set the default file path */
557 dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
558 if (!g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
559 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
561 priv->file = g_build_filename (dir, CHATROOMS_XML_FILENAME, NULL);
565 dbus = tp_dbus_daemon_dup (&error);
568 g_warning ("Failed to get TpDBusDaemon: %s", error->message);
570 g_error_free (error);
574 /* Setup a room observer */
575 priv->observer = tp_simple_observer_new (dbus, TRUE,
576 "Empathy.ChatroomManager", TRUE, observe_channels_cb, self, NULL);
578 g_object_unref (dbus);
580 tp_base_client_take_observer_filter (priv->observer, tp_asv_new (
581 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
582 TP_IFACE_CHANNEL_TYPE_TEXT,
583 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
587 tp_base_client_add_connection_features_varargs (priv->observer,
588 TP_CONNECTION_FEATURE_CAPABILITIES, NULL);
590 if (!tp_base_client_register (priv->observer, &error))
592 g_critical ("Failed to register Observer: %s", error->message);
594 g_error_free (error);
601 empathy_chatroom_manager_class_init (EmpathyChatroomManagerClass *klass)
603 GObjectClass *object_class = G_OBJECT_CLASS (klass);
604 GParamSpec *param_spec;
606 object_class->constructor = empathy_chatroom_manager_constructor;
607 object_class->get_property = empathy_chatroom_manager_get_property;
608 object_class->set_property = empathy_chatroom_manager_set_property;
609 object_class->dispose = chatroom_manager_dispose;
610 object_class->finalize = chatroom_manager_finalize;
612 param_spec = g_param_spec_string (
614 "path of the favorite file",
615 "The path of the XML file containing user's favorites",
617 G_PARAM_CONSTRUCT_ONLY |
619 G_PARAM_STATIC_NAME |
620 G_PARAM_STATIC_NICK |
621 G_PARAM_STATIC_BLURB);
622 g_object_class_install_property (object_class, PROP_FILE, param_spec);
624 param_spec = g_param_spec_boolean (
626 "whether the manager is ready yet",
627 "whether the manager is ready yet",
630 g_object_class_install_property (object_class, PROP_READY, param_spec);
632 signals[CHATROOM_ADDED] = g_signal_new ("chatroom-added",
633 G_TYPE_FROM_CLASS (klass),
636 g_cclosure_marshal_VOID__OBJECT,
638 1, EMPATHY_TYPE_CHATROOM);
640 signals[CHATROOM_REMOVED] = g_signal_new ("chatroom-removed",
641 G_TYPE_FROM_CLASS (klass),
644 g_cclosure_marshal_VOID__OBJECT,
646 1, EMPATHY_TYPE_CHATROOM);
648 g_type_class_add_private (object_class, sizeof (EmpathyChatroomManagerPriv));
652 empathy_chatroom_manager_init (EmpathyChatroomManager *manager)
654 EmpathyChatroomManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
655 EMPATHY_TYPE_CHATROOM_MANAGER, EmpathyChatroomManagerPriv);
657 manager->priv = priv;
660 EmpathyChatroomManager *
661 empathy_chatroom_manager_dup_singleton (const gchar *file)
663 return EMPATHY_CHATROOM_MANAGER (g_object_new (EMPATHY_TYPE_CHATROOM_MANAGER,
664 "file", file, NULL));
668 empathy_chatroom_manager_add (EmpathyChatroomManager *manager,
669 EmpathyChatroom *chatroom)
671 EmpathyChatroomManagerPriv *priv;
673 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), FALSE);
674 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), FALSE);
676 priv = GET_PRIV (manager);
678 /* don't add more than once */
679 if (!empathy_chatroom_manager_find (manager,
680 empathy_chatroom_get_account (chatroom),
681 empathy_chatroom_get_room (chatroom)))
683 add_chatroom (manager, chatroom);
685 if (empathy_chatroom_is_favorite (chatroom))
686 reset_save_timeout (manager);
688 g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
696 chatroom_manager_remove_link (EmpathyChatroomManager *manager,
699 EmpathyChatroomManagerPriv *priv;
700 EmpathyChatroom *chatroom;
702 priv = GET_PRIV (manager);
706 if (empathy_chatroom_is_favorite (chatroom))
707 reset_save_timeout (manager);
709 priv->chatrooms = g_list_delete_link (priv->chatrooms, l);
711 g_signal_emit (manager, signals[CHATROOM_REMOVED], 0, chatroom);
712 g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb, manager);
714 g_object_unref (chatroom);
718 empathy_chatroom_manager_remove (EmpathyChatroomManager *manager,
719 EmpathyChatroom *chatroom)
721 EmpathyChatroomManagerPriv *priv;
724 g_return_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager));
725 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
727 priv = GET_PRIV (manager);
729 for (l = priv->chatrooms; l; l = l->next)
731 EmpathyChatroom *this_chatroom;
733 this_chatroom = l->data;
735 if (this_chatroom == chatroom ||
736 empathy_chatroom_equal (chatroom, this_chatroom))
738 chatroom_manager_remove_link (manager, l);
745 empathy_chatroom_manager_find (EmpathyChatroomManager *manager,
749 EmpathyChatroomManagerPriv *priv;
752 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
753 g_return_val_if_fail (room != NULL, NULL);
755 priv = GET_PRIV (manager);
757 for (l = priv->chatrooms; l; l = l->next)
759 EmpathyChatroom *chatroom;
760 TpAccount *this_account;
761 const gchar *this_room;
764 this_account = empathy_chatroom_get_account (chatroom);
765 this_room = empathy_chatroom_get_room (chatroom);
767 if (this_account && this_room && account == this_account
768 && strcmp (this_room, room) == 0)
776 empathy_chatroom_manager_ensure_chatroom (EmpathyChatroomManager *manager,
781 EmpathyChatroom *chatroom;
783 chatroom = empathy_chatroom_manager_find (manager, account, room);
787 return g_object_ref (chatroom);
791 chatroom = empathy_chatroom_new_full (account,
795 empathy_chatroom_manager_add (manager, chatroom);
801 empathy_chatroom_manager_get_chatrooms (EmpathyChatroomManager *manager,
804 EmpathyChatroomManagerPriv *priv;
805 GList *chatrooms, *l;
807 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
809 priv = GET_PRIV (manager);
812 return g_list_copy (priv->chatrooms);
815 for (l = priv->chatrooms; l; l = l->next)
817 EmpathyChatroom *chatroom;
821 if (account == empathy_chatroom_get_account (chatroom))
822 chatrooms = g_list_append (chatrooms, chatroom);
829 chatroom_manager_chat_destroyed_cb (EmpathyTpChat *chat,
832 EmpathyChatroomManagerPriv *priv = GET_PRIV (manager);
835 for (l = priv->chatrooms; l; l = l->next)
837 EmpathyChatroom *chatroom = l->data;
839 if (empathy_chatroom_get_tp_chat (chatroom) != chat)
842 empathy_chatroom_set_tp_chat (chatroom, NULL);
844 if (!empathy_chatroom_is_favorite (chatroom))
846 /* Remove the chatroom from the list, unless it's in the list of
848 * FIXME this policy should probably not be in libempathy */
849 chatroom_manager_remove_link (manager, l);
857 observe_channels_cb (TpSimpleObserver *observer,
859 TpConnection *connection,
861 TpChannelDispatchOperation *dispatch_operation,
863 TpObserveChannelsContext *context,
866 EmpathyChatroomManager *self = user_data;
869 for (l = channels; l != NULL; l = g_list_next (l))
871 TpChannel *channel = l->data;
872 EmpathyTpChat *tp_chat;
873 const gchar *roomname;
874 EmpathyChatroom *chatroom;
876 if (tp_proxy_get_invalidated (channel) != NULL)
879 tp_chat = empathy_tp_chat_new (account, channel);
880 roomname = empathy_tp_chat_get_id (tp_chat);
881 chatroom = empathy_chatroom_manager_find (self, account, roomname);
883 if (chatroom == NULL)
885 chatroom = empathy_chatroom_new_full (account, roomname, roomname,
887 empathy_chatroom_manager_add (self, chatroom);
888 g_object_unref (chatroom);
891 empathy_chatroom_set_tp_chat (chatroom, tp_chat);
892 g_object_unref (tp_chat);
894 /* A TpChat is always destroyed as it only gets unreffed after the channel
895 * has been invalidated in the dispatcher.. */
896 g_signal_connect (tp_chat, "destroy",
897 G_CALLBACK (chatroom_manager_chat_destroyed_cb),
901 tp_observe_channels_context_accept (context);