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 "empathy-client-factory.h"
34 #include "empathy-tp-chat.h"
35 #include "empathy-chatroom-manager.h"
36 #include "empathy-utils.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
39 #include "empathy-debug.h"
41 #define CHATROOMS_XML_FILENAME "chatrooms.xml"
42 #define CHATROOMS_DTD_FILENAME "empathy-chatroom-manager.dtd"
45 static EmpathyChatroomManager *chatroom_manager_singleton = NULL;
47 static void observe_channels_cb (TpSimpleObserver *observer,
49 TpConnection *connection,
51 TpChannelDispatchOperation *dispatch_operation,
53 TpObserveChannelsContext *context,
56 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatroomManager)
61 TpAccountManager *account_manager;
63 /* source id of the autosave timer */
66 GFileMonitor *monitor;
69 TpBaseClient *observer;
70 } EmpathyChatroomManagerPriv;
78 static guint signals[LAST_SIGNAL];
88 G_DEFINE_TYPE (EmpathyChatroomManager, empathy_chatroom_manager, G_TYPE_OBJECT);
91 * API to save/load and parse the chatrooms file.
95 chatroom_manager_file_save (EmpathyChatroomManager *manager)
97 EmpathyChatroomManagerPriv *priv;
102 priv = GET_PRIV (manager);
104 priv->writing = TRUE;
106 doc = xmlNewDoc ((const xmlChar *) "1.0");
107 root = xmlNewNode (NULL, (const xmlChar *) "chatrooms");
108 xmlDocSetRootElement (doc, root);
110 for (l = priv->chatrooms; l; l = l->next)
112 EmpathyChatroom *chatroom;
114 const gchar *account_id;
118 if (!empathy_chatroom_is_favorite (chatroom))
121 account_id = tp_proxy_get_object_path (empathy_chatroom_get_account (
124 node = xmlNewChild (root, NULL, (const xmlChar *) "chatroom", NULL);
125 xmlNewTextChild (node, NULL, (const xmlChar *) "name",
126 (const xmlChar *) empathy_chatroom_get_name (chatroom));
127 xmlNewTextChild (node, NULL, (const xmlChar *) "room",
128 (const xmlChar *) empathy_chatroom_get_room (chatroom));
129 xmlNewTextChild (node, NULL, (const xmlChar *) "account",
130 (const xmlChar *) account_id);
131 xmlNewTextChild (node, NULL, (const xmlChar *) "auto_connect",
132 empathy_chatroom_get_auto_connect (chatroom) ?
133 (const xmlChar *) "yes" : (const xmlChar *) "no");
134 xmlNewTextChild (node, NULL, (const xmlChar *) "always_urgent",
135 empathy_chatroom_is_always_urgent (chatroom) ?
136 (const xmlChar *) "yes" : (const xmlChar *) "no");
139 /* Make sure the XML is indented properly */
140 xmlIndentTreeOutput = 1;
142 DEBUG ("Saving file:'%s'", priv->file);
143 xmlSaveFormatFileEnc (priv->file, doc, "utf-8", 1);
148 priv->writing = FALSE;
153 save_timeout (EmpathyChatroomManager *self)
155 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
157 priv->save_timer_id = 0;
158 chatroom_manager_file_save (self);
164 reset_save_timeout (EmpathyChatroomManager *self)
166 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
168 if (priv->save_timer_id > 0)
169 g_source_remove (priv->save_timer_id);
171 priv->save_timer_id = g_timeout_add_seconds (SAVE_TIMER,
172 (GSourceFunc) save_timeout, self);
176 chatroom_changed_cb (EmpathyChatroom *chatroom,
178 EmpathyChatroomManager *self)
180 reset_save_timeout (self);
184 add_chatroom (EmpathyChatroomManager *self,
185 EmpathyChatroom *chatroom)
187 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
189 priv->chatrooms = g_list_prepend (priv->chatrooms, g_object_ref (chatroom));
191 /* Watch only those properties which are exported in the save file */
192 g_signal_connect (chatroom, "notify::name",
193 G_CALLBACK (chatroom_changed_cb), self);
194 g_signal_connect (chatroom, "notify::room",
195 G_CALLBACK (chatroom_changed_cb), self);
196 g_signal_connect (chatroom, "notify::account",
197 G_CALLBACK (chatroom_changed_cb), self);
198 g_signal_connect (chatroom, "notify::auto-connect",
199 G_CALLBACK (chatroom_changed_cb), self);
200 g_signal_connect (chatroom, "notify::always_urgent",
201 G_CALLBACK (chatroom_changed_cb), self);
202 g_signal_connect (chatroom, "notify::favorite",
203 G_CALLBACK (chatroom_changed_cb), self);
207 chatroom_manager_parse_chatroom (EmpathyChatroomManager *manager,
210 EmpathyChatroom *chatroom = NULL;
217 gboolean auto_connect;
218 gboolean always_urgent;
219 EmpathyClientFactory *factory;
220 GError *error = NULL;
222 /* default values. */
226 always_urgent = FALSE;
229 for (child = node->children; child; child = child->next)
233 if (xmlNodeIsText (child))
236 tag = (gchar *) child->name;
237 str = (gchar *) xmlNodeGetContent (child);
239 if (strcmp (tag, "name") == 0)
241 name = g_strdup (str);
243 else if (strcmp (tag, "room") == 0)
245 room = g_strdup (str);
247 else if (strcmp (tag, "auto_connect") == 0)
249 if (strcmp (str, "yes") == 0)
252 auto_connect = FALSE;
254 else if (!tp_strdiff (tag, "always_urgent"))
256 if (strcmp (str, "yes") == 0)
257 always_urgent = TRUE;
259 always_urgent = FALSE;
261 else if (strcmp (tag, "account") == 0)
263 account_id = g_strdup (str);
269 /* account has to be a valid Account object path */
270 if (!tp_dbus_check_valid_object_path (account_id, NULL) ||
271 !g_str_has_prefix (account_id, TP_ACCOUNT_OBJECT_PATH_BASE))
274 factory = empathy_client_factory_dup ();
276 account = tp_simple_client_factory_ensure_account (
277 TP_SIMPLE_CLIENT_FACTORY (factory), account_id, NULL, &error);
278 g_object_unref (factory);
282 DEBUG ("Failed to create account: %s", error->message);
283 g_error_free (error);
291 chatroom = empathy_chatroom_new_full (account, room, name, auto_connect);
292 empathy_chatroom_set_favorite (chatroom, TRUE);
293 empathy_chatroom_set_always_urgent (chatroom, always_urgent);
294 add_chatroom (manager, chatroom);
295 g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
301 tp_clear_object (&chatroom);
305 chatroom_manager_file_parse (EmpathyChatroomManager *manager,
306 const gchar *filename)
308 EmpathyChatroomManagerPriv *priv;
309 xmlParserCtxtPtr ctxt;
311 xmlNodePtr chatrooms;
314 priv = GET_PRIV (manager);
316 DEBUG ("Attempting to parse file:'%s'...", filename);
318 ctxt = xmlNewParserCtxt ();
320 /* Parse and validate the file. */
321 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
324 g_warning ("Failed to parse file:'%s'", filename);
325 xmlFreeParserCtxt (ctxt);
329 if (!empathy_xml_validate (doc, CHATROOMS_DTD_FILENAME))
331 g_warning ("Failed to validate file:'%s'", filename);
333 xmlFreeParserCtxt (ctxt);
337 /* The root node, chatrooms. */
338 chatrooms = xmlDocGetRootElement (doc);
340 for (node = chatrooms->children; node; node = node->next)
342 if (strcmp ((gchar *) node->name, "chatroom") == 0)
343 chatroom_manager_parse_chatroom (manager, node);
346 DEBUG ("Parsed %d chatrooms", g_list_length (priv->chatrooms));
349 xmlFreeParserCtxt (ctxt);
355 chatroom_manager_get_all (EmpathyChatroomManager *manager)
357 EmpathyChatroomManagerPriv *priv;
359 priv = GET_PRIV (manager);
362 if (g_file_test (priv->file, G_FILE_TEST_EXISTS) &&
363 !chatroom_manager_file_parse (manager, priv->file))
369 g_object_notify (G_OBJECT (manager), "ready");
376 empathy_chatroom_manager_get_property (GObject *object,
381 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
382 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
387 g_value_set_string (value, priv->file);
390 g_value_set_boolean (value, priv->ready);
393 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
399 empathy_chatroom_manager_set_property (GObject *object,
404 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
405 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
411 priv->file = g_value_dup_string (value);
414 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
420 chatroom_manager_dispose (GObject *object)
422 EmpathyChatroomManagerPriv *priv;
424 priv = GET_PRIV (object);
426 tp_clear_object (&priv->observer);
427 tp_clear_object (&priv->monitor);
429 (G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->dispose) (object);
433 clear_chatrooms (EmpathyChatroomManager *self)
435 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
438 tmp = priv->chatrooms;
440 /* Unreffing the chatroom may result in destroying the underlying
441 * EmpathyTpChat which will fire the invalidated signal and so make us
442 * re-call this function. We already set priv->chatrooms to NULL so we won't
443 * try to destroy twice the same objects. */
444 priv->chatrooms = NULL;
446 for (l = tmp; l != NULL; l = g_list_next (l))
448 EmpathyChatroom *chatroom = l->data;
450 g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb,
452 g_signal_emit (self, signals[CHATROOM_REMOVED], 0, chatroom);
454 g_object_unref (chatroom);
461 chatroom_manager_finalize (GObject *object)
463 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
464 EmpathyChatroomManagerPriv *priv;
466 priv = GET_PRIV (object);
468 g_object_unref (priv->account_manager);
470 if (priv->save_timer_id > 0)
472 /* have to save before destroy the object */
473 g_source_remove (priv->save_timer_id);
474 priv->save_timer_id = 0;
475 chatroom_manager_file_save (self);
478 clear_chatrooms (self);
482 (G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->finalize) (object);
486 file_changed_cb (GFileMonitor *monitor,
489 GFileMonitorEvent event_type,
492 EmpathyChatroomManager *self = user_data;
493 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
495 if (event_type != G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
501 DEBUG ("chatrooms file changed; reloading list");
503 clear_chatrooms (self);
504 chatroom_manager_get_all (self);
508 account_manager_ready_cb (GObject *source_object,
509 GAsyncResult *result,
512 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (user_data);
513 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
514 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
515 GError *error = NULL;
518 if (!tp_proxy_prepare_finish (manager, result, &error))
520 DEBUG ("Failed to prepare account manager: %s", error->message);
521 g_error_free (error);
525 chatroom_manager_get_all (self);
527 /* Set up file monitor */
528 file = g_file_new_for_path (priv->file);
530 priv->monitor = g_file_monitor (file, 0, NULL, &error);
531 if (priv->monitor == NULL)
533 DEBUG ("Failed to create file monitor on %s: %s", priv->file,
536 g_error_free (error);
540 g_signal_connect (priv->monitor, "changed", G_CALLBACK (file_changed_cb),
544 tp_clear_object (&file);
545 g_object_unref (self);
549 empathy_chatroom_manager_constructor (GType type,
551 GObjectConstructParam *props)
554 EmpathyChatroomManager *self;
555 EmpathyChatroomManagerPriv *priv;
556 GError *error = NULL;
558 if (chatroom_manager_singleton != NULL)
559 return g_object_ref (chatroom_manager_singleton);
561 /* Parent constructor chain */
562 obj = G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->
563 constructor (type, n_props, props);
565 self = EMPATHY_CHATROOM_MANAGER (obj);
566 priv = GET_PRIV (self);
570 chatroom_manager_singleton = self;
571 g_object_add_weak_pointer (obj, (gpointer) &chatroom_manager_singleton);
573 priv->account_manager = tp_account_manager_dup ();
575 tp_proxy_prepare_async (priv->account_manager, NULL,
576 account_manager_ready_cb, g_object_ref (self));
578 if (priv->file == NULL)
580 /* Set the default file path */
583 dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
584 if (!g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
585 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
587 priv->file = g_build_filename (dir, CHATROOMS_XML_FILENAME, NULL);
591 /* Setup a room observer */
592 priv->observer = tp_simple_observer_new_with_am (priv->account_manager, TRUE,
593 "Empathy.ChatroomManager", TRUE, observe_channels_cb, self, NULL);
595 tp_base_client_take_observer_filter (priv->observer, tp_asv_new (
596 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
597 TP_IFACE_CHANNEL_TYPE_TEXT,
598 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
602 if (!tp_base_client_register (priv->observer, &error))
604 g_critical ("Failed to register Observer: %s", error->message);
606 g_error_free (error);
613 empathy_chatroom_manager_class_init (EmpathyChatroomManagerClass *klass)
615 GObjectClass *object_class = G_OBJECT_CLASS (klass);
616 GParamSpec *param_spec;
618 object_class->constructor = empathy_chatroom_manager_constructor;
619 object_class->get_property = empathy_chatroom_manager_get_property;
620 object_class->set_property = empathy_chatroom_manager_set_property;
621 object_class->dispose = chatroom_manager_dispose;
622 object_class->finalize = chatroom_manager_finalize;
624 param_spec = g_param_spec_string (
626 "path of the favorite file",
627 "The path of the XML file containing user's favorites",
629 G_PARAM_CONSTRUCT_ONLY |
631 G_PARAM_STATIC_NAME |
632 G_PARAM_STATIC_NICK |
633 G_PARAM_STATIC_BLURB);
634 g_object_class_install_property (object_class, PROP_FILE, param_spec);
636 param_spec = g_param_spec_boolean (
638 "whether the manager is ready yet",
639 "whether the manager is ready yet",
642 g_object_class_install_property (object_class, PROP_READY, param_spec);
644 signals[CHATROOM_ADDED] = g_signal_new ("chatroom-added",
645 G_TYPE_FROM_CLASS (klass),
648 g_cclosure_marshal_generic,
650 1, EMPATHY_TYPE_CHATROOM);
652 signals[CHATROOM_REMOVED] = g_signal_new ("chatroom-removed",
653 G_TYPE_FROM_CLASS (klass),
656 g_cclosure_marshal_generic,
658 1, EMPATHY_TYPE_CHATROOM);
660 g_type_class_add_private (object_class, sizeof (EmpathyChatroomManagerPriv));
664 empathy_chatroom_manager_init (EmpathyChatroomManager *manager)
666 EmpathyChatroomManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
667 EMPATHY_TYPE_CHATROOM_MANAGER, EmpathyChatroomManagerPriv);
669 manager->priv = priv;
672 EmpathyChatroomManager *
673 empathy_chatroom_manager_dup_singleton (const gchar *file)
675 return EMPATHY_CHATROOM_MANAGER (g_object_new (EMPATHY_TYPE_CHATROOM_MANAGER,
676 "file", file, NULL));
680 empathy_chatroom_manager_add (EmpathyChatroomManager *manager,
681 EmpathyChatroom *chatroom)
683 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), FALSE);
684 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), FALSE);
686 /* don't add more than once */
687 if (!empathy_chatroom_manager_find (manager,
688 empathy_chatroom_get_account (chatroom),
689 empathy_chatroom_get_room (chatroom)))
691 add_chatroom (manager, chatroom);
693 if (empathy_chatroom_is_favorite (chatroom))
694 reset_save_timeout (manager);
696 g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
704 chatroom_manager_remove_link (EmpathyChatroomManager *manager,
707 EmpathyChatroomManagerPriv *priv;
708 EmpathyChatroom *chatroom;
710 priv = GET_PRIV (manager);
714 if (empathy_chatroom_is_favorite (chatroom))
715 reset_save_timeout (manager);
717 priv->chatrooms = g_list_delete_link (priv->chatrooms, l);
719 g_signal_emit (manager, signals[CHATROOM_REMOVED], 0, chatroom);
720 g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb, manager);
722 g_object_unref (chatroom);
726 empathy_chatroom_manager_remove (EmpathyChatroomManager *manager,
727 EmpathyChatroom *chatroom)
729 EmpathyChatroomManagerPriv *priv;
732 g_return_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager));
733 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
735 priv = GET_PRIV (manager);
737 for (l = priv->chatrooms; l; l = l->next)
739 EmpathyChatroom *this_chatroom;
741 this_chatroom = l->data;
743 if (this_chatroom == chatroom ||
744 empathy_chatroom_equal (chatroom, this_chatroom))
746 chatroom_manager_remove_link (manager, l);
753 empathy_chatroom_manager_find (EmpathyChatroomManager *manager,
757 EmpathyChatroomManagerPriv *priv;
760 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
761 g_return_val_if_fail (room != NULL, NULL);
763 priv = GET_PRIV (manager);
765 for (l = priv->chatrooms; l; l = l->next)
767 EmpathyChatroom *chatroom;
768 TpAccount *this_account;
769 const gchar *this_room;
772 this_account = empathy_chatroom_get_account (chatroom);
773 this_room = empathy_chatroom_get_room (chatroom);
775 if (this_account && this_room && account == this_account
776 && strcmp (this_room, room) == 0)
784 empathy_chatroom_manager_ensure_chatroom (EmpathyChatroomManager *manager,
789 EmpathyChatroom *chatroom;
791 chatroom = empathy_chatroom_manager_find (manager, account, room);
795 return g_object_ref (chatroom);
799 chatroom = empathy_chatroom_new_full (account,
803 empathy_chatroom_manager_add (manager, chatroom);
809 empathy_chatroom_manager_get_chatrooms (EmpathyChatroomManager *manager,
812 EmpathyChatroomManagerPriv *priv;
813 GList *chatrooms, *l;
815 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
817 priv = GET_PRIV (manager);
820 return g_list_copy (priv->chatrooms);
823 for (l = priv->chatrooms; l; l = l->next)
825 EmpathyChatroom *chatroom;
829 if (account == empathy_chatroom_get_account (chatroom))
830 chatrooms = g_list_append (chatrooms, chatroom);
837 chatroom_manager_chat_invalidated_cb (EmpathyTpChat *chat,
843 EmpathyChatroomManagerPriv *priv = GET_PRIV (manager);
846 for (l = priv->chatrooms; l; l = l->next)
848 EmpathyChatroom *chatroom = l->data;
850 if (empathy_chatroom_get_tp_chat (chatroom) != chat)
853 empathy_chatroom_set_tp_chat (chatroom, NULL);
855 if (!empathy_chatroom_is_favorite (chatroom))
857 /* Remove the chatroom from the list, unless it's in the list of
859 * FIXME this policy should probably not be in libempathy */
860 chatroom_manager_remove_link (manager, l);
868 observe_channels_cb (TpSimpleObserver *observer,
870 TpConnection *connection,
872 TpChannelDispatchOperation *dispatch_operation,
874 TpObserveChannelsContext *context,
877 EmpathyChatroomManager *self = user_data;
880 for (l = channels; l != NULL; l = g_list_next (l))
882 EmpathyTpChat *tp_chat = l->data;
883 const gchar *roomname;
884 EmpathyChatroom *chatroom;
886 if (tp_proxy_get_invalidated ((TpChannel *) tp_chat) != NULL)
889 if (!EMPATHY_IS_TP_CHAT (tp_chat))
892 roomname = empathy_tp_chat_get_id (tp_chat);
893 chatroom = empathy_chatroom_manager_find (self, account, roomname);
895 if (chatroom == NULL)
897 chatroom = empathy_chatroom_new_full (account, roomname, roomname,
899 empathy_chatroom_manager_add (self, chatroom);
900 g_object_unref (chatroom);
903 empathy_chatroom_set_tp_chat (chatroom, tp_chat);
905 g_signal_connect (tp_chat, "invalidated",
906 G_CALLBACK (chatroom_manager_chat_invalidated_cb),
910 tp_observe_channels_context_accept (context);