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);
205 chatroom_manager_parse_chatroom (EmpathyChatroomManager *manager,
208 EmpathyChatroom *chatroom = NULL;
215 gboolean auto_connect;
216 gboolean always_urgent;
217 EmpathyClientFactory *factory;
218 GError *error = NULL;
220 /* default values. */
224 always_urgent = FALSE;
227 for (child = node->children; child; child = child->next)
231 if (xmlNodeIsText (child))
234 tag = (gchar *) child->name;
235 str = (gchar *) xmlNodeGetContent (child);
237 if (strcmp (tag, "name") == 0)
239 name = g_strdup (str);
241 else if (strcmp (tag, "room") == 0)
243 room = g_strdup (str);
245 else if (strcmp (tag, "auto_connect") == 0)
247 if (strcmp (str, "yes") == 0)
250 auto_connect = FALSE;
252 else if (!tp_strdiff (tag, "always_urgent"))
254 if (strcmp (str, "yes") == 0)
255 always_urgent = TRUE;
257 always_urgent = FALSE;
259 else if (strcmp (tag, "account") == 0)
261 account_id = g_strdup (str);
267 /* account has to be a valid Account object path */
268 if (!tp_dbus_check_valid_object_path (account_id, NULL) ||
269 !g_str_has_prefix (account_id, TP_ACCOUNT_OBJECT_PATH_BASE))
272 factory = empathy_client_factory_dup ();
274 account = tp_simple_client_factory_ensure_account (
275 TP_SIMPLE_CLIENT_FACTORY (factory), account_id, NULL, &error);
276 g_object_unref (factory);
280 DEBUG ("Failed to create account: %s", error->message);
281 g_error_free (error);
289 chatroom = empathy_chatroom_new_full (account, room, name, auto_connect);
290 empathy_chatroom_set_favorite (chatroom, TRUE);
291 empathy_chatroom_set_always_urgent (chatroom, always_urgent);
292 add_chatroom (manager, chatroom);
293 g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
299 tp_clear_object (&chatroom);
303 chatroom_manager_file_parse (EmpathyChatroomManager *manager,
304 const gchar *filename)
306 EmpathyChatroomManagerPriv *priv;
307 xmlParserCtxtPtr ctxt;
309 xmlNodePtr chatrooms;
312 priv = GET_PRIV (manager);
314 DEBUG ("Attempting to parse file:'%s'...", filename);
316 ctxt = xmlNewParserCtxt ();
318 /* Parse and validate the file. */
319 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
322 g_warning ("Failed to parse file:'%s'", filename);
323 xmlFreeParserCtxt (ctxt);
327 if (!empathy_xml_validate (doc, CHATROOMS_DTD_FILENAME))
329 g_warning ("Failed to validate file:'%s'", filename);
331 xmlFreeParserCtxt (ctxt);
335 /* The root node, chatrooms. */
336 chatrooms = xmlDocGetRootElement (doc);
338 for (node = chatrooms->children; node; node = node->next)
340 if (strcmp ((gchar *) node->name, "chatroom") == 0)
341 chatroom_manager_parse_chatroom (manager, node);
344 DEBUG ("Parsed %d chatrooms", g_list_length (priv->chatrooms));
347 xmlFreeParserCtxt (ctxt);
353 chatroom_manager_get_all (EmpathyChatroomManager *manager)
355 EmpathyChatroomManagerPriv *priv;
357 priv = GET_PRIV (manager);
360 if (g_file_test (priv->file, G_FILE_TEST_EXISTS) &&
361 !chatroom_manager_file_parse (manager, priv->file))
367 g_object_notify (G_OBJECT (manager), "ready");
374 empathy_chatroom_manager_get_property (GObject *object,
379 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
380 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
385 g_value_set_string (value, priv->file);
388 g_value_set_boolean (value, priv->ready);
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
397 empathy_chatroom_manager_set_property (GObject *object,
402 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
403 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
409 priv->file = g_value_dup_string (value);
412 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
418 chatroom_manager_dispose (GObject *object)
420 EmpathyChatroomManagerPriv *priv;
422 priv = GET_PRIV (object);
424 tp_clear_object (&priv->observer);
425 tp_clear_object (&priv->monitor);
427 (G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->dispose) (object);
431 clear_chatrooms (EmpathyChatroomManager *self)
433 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
436 tmp = priv->chatrooms;
438 /* Unreffing the chatroom may result in destroying the underlying
439 * EmpathyTpChat which will fire the invalidated signal and so make us
440 * re-call this function. We already set priv->chatrooms to NULL so we won't
441 * try to destroy twice the same objects. */
442 priv->chatrooms = NULL;
444 for (l = tmp; l != NULL; l = g_list_next (l))
446 EmpathyChatroom *chatroom = l->data;
448 g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb,
450 g_signal_emit (self, signals[CHATROOM_REMOVED], 0, chatroom);
452 g_object_unref (chatroom);
459 chatroom_manager_finalize (GObject *object)
461 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
462 EmpathyChatroomManagerPriv *priv;
464 priv = GET_PRIV (object);
466 g_object_unref (priv->account_manager);
468 if (priv->save_timer_id > 0)
470 /* have to save before destroy the object */
471 g_source_remove (priv->save_timer_id);
472 priv->save_timer_id = 0;
473 chatroom_manager_file_save (self);
476 clear_chatrooms (self);
480 (G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->finalize) (object);
484 file_changed_cb (GFileMonitor *monitor,
487 GFileMonitorEvent event_type,
490 EmpathyChatroomManager *self = user_data;
491 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
493 if (event_type != G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
499 DEBUG ("chatrooms file changed; reloading list");
501 clear_chatrooms (self);
502 chatroom_manager_get_all (self);
506 account_manager_ready_cb (GObject *source_object,
507 GAsyncResult *result,
510 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (user_data);
511 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
512 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
513 GError *error = NULL;
516 if (!tp_proxy_prepare_finish (manager, result, &error))
518 DEBUG ("Failed to prepare account manager: %s", error->message);
519 g_error_free (error);
523 chatroom_manager_get_all (self);
525 /* Set up file monitor */
526 file = g_file_new_for_path (priv->file);
528 priv->monitor = g_file_monitor (file, 0, NULL, &error);
529 if (priv->monitor == NULL)
531 DEBUG ("Failed to create file monitor on %s: %s", priv->file,
534 g_error_free (error);
538 g_signal_connect (priv->monitor, "changed", G_CALLBACK (file_changed_cb),
542 tp_clear_object (&file);
543 g_object_unref (self);
547 empathy_chatroom_manager_constructor (GType type,
549 GObjectConstructParam *props)
552 EmpathyChatroomManager *self;
553 EmpathyChatroomManagerPriv *priv;
554 GError *error = NULL;
556 if (chatroom_manager_singleton != NULL)
557 return g_object_ref (chatroom_manager_singleton);
559 /* Parent constructor chain */
560 obj = G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->
561 constructor (type, n_props, props);
563 self = EMPATHY_CHATROOM_MANAGER (obj);
564 priv = GET_PRIV (self);
568 chatroom_manager_singleton = self;
569 g_object_add_weak_pointer (obj, (gpointer) &chatroom_manager_singleton);
571 priv->account_manager = tp_account_manager_dup ();
573 tp_proxy_prepare_async (priv->account_manager, NULL,
574 account_manager_ready_cb, g_object_ref (self));
576 if (priv->file == NULL)
578 /* Set the default file path */
581 dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
582 if (!g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
583 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
585 priv->file = g_build_filename (dir, CHATROOMS_XML_FILENAME, NULL);
589 /* Setup a room observer */
590 priv->observer = tp_simple_observer_new_with_am (priv->account_manager, TRUE,
591 "Empathy.ChatroomManager", TRUE, observe_channels_cb, self, NULL);
593 tp_base_client_take_observer_filter (priv->observer, tp_asv_new (
594 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
595 TP_IFACE_CHANNEL_TYPE_TEXT,
596 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
600 if (!tp_base_client_register (priv->observer, &error))
602 g_critical ("Failed to register Observer: %s", error->message);
604 g_error_free (error);
611 empathy_chatroom_manager_class_init (EmpathyChatroomManagerClass *klass)
613 GObjectClass *object_class = G_OBJECT_CLASS (klass);
614 GParamSpec *param_spec;
616 object_class->constructor = empathy_chatroom_manager_constructor;
617 object_class->get_property = empathy_chatroom_manager_get_property;
618 object_class->set_property = empathy_chatroom_manager_set_property;
619 object_class->dispose = chatroom_manager_dispose;
620 object_class->finalize = chatroom_manager_finalize;
622 param_spec = g_param_spec_string (
624 "path of the favorite file",
625 "The path of the XML file containing user's favorites",
627 G_PARAM_CONSTRUCT_ONLY |
629 G_PARAM_STATIC_NAME |
630 G_PARAM_STATIC_NICK |
631 G_PARAM_STATIC_BLURB);
632 g_object_class_install_property (object_class, PROP_FILE, param_spec);
634 param_spec = g_param_spec_boolean (
636 "whether the manager is ready yet",
637 "whether the manager is ready yet",
640 g_object_class_install_property (object_class, PROP_READY, param_spec);
642 signals[CHATROOM_ADDED] = g_signal_new ("chatroom-added",
643 G_TYPE_FROM_CLASS (klass),
646 g_cclosure_marshal_generic,
648 1, EMPATHY_TYPE_CHATROOM);
650 signals[CHATROOM_REMOVED] = g_signal_new ("chatroom-removed",
651 G_TYPE_FROM_CLASS (klass),
654 g_cclosure_marshal_generic,
656 1, EMPATHY_TYPE_CHATROOM);
658 g_type_class_add_private (object_class, sizeof (EmpathyChatroomManagerPriv));
662 empathy_chatroom_manager_init (EmpathyChatroomManager *manager)
664 EmpathyChatroomManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
665 EMPATHY_TYPE_CHATROOM_MANAGER, EmpathyChatroomManagerPriv);
667 manager->priv = priv;
670 EmpathyChatroomManager *
671 empathy_chatroom_manager_dup_singleton (const gchar *file)
673 return EMPATHY_CHATROOM_MANAGER (g_object_new (EMPATHY_TYPE_CHATROOM_MANAGER,
674 "file", file, NULL));
678 empathy_chatroom_manager_add (EmpathyChatroomManager *manager,
679 EmpathyChatroom *chatroom)
681 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), FALSE);
682 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), FALSE);
684 /* don't add more than once */
685 if (!empathy_chatroom_manager_find (manager,
686 empathy_chatroom_get_account (chatroom),
687 empathy_chatroom_get_room (chatroom)))
689 add_chatroom (manager, chatroom);
691 if (empathy_chatroom_is_favorite (chatroom))
692 reset_save_timeout (manager);
694 g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
702 chatroom_manager_remove_link (EmpathyChatroomManager *manager,
705 EmpathyChatroomManagerPriv *priv;
706 EmpathyChatroom *chatroom;
708 priv = GET_PRIV (manager);
712 if (empathy_chatroom_is_favorite (chatroom))
713 reset_save_timeout (manager);
715 priv->chatrooms = g_list_delete_link (priv->chatrooms, l);
717 g_signal_emit (manager, signals[CHATROOM_REMOVED], 0, chatroom);
718 g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb, manager);
720 g_object_unref (chatroom);
724 empathy_chatroom_manager_remove (EmpathyChatroomManager *manager,
725 EmpathyChatroom *chatroom)
727 EmpathyChatroomManagerPriv *priv;
730 g_return_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager));
731 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
733 priv = GET_PRIV (manager);
735 for (l = priv->chatrooms; l; l = l->next)
737 EmpathyChatroom *this_chatroom;
739 this_chatroom = l->data;
741 if (this_chatroom == chatroom ||
742 empathy_chatroom_equal (chatroom, this_chatroom))
744 chatroom_manager_remove_link (manager, l);
751 empathy_chatroom_manager_find (EmpathyChatroomManager *manager,
755 EmpathyChatroomManagerPriv *priv;
758 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
759 g_return_val_if_fail (room != NULL, NULL);
761 priv = GET_PRIV (manager);
763 for (l = priv->chatrooms; l; l = l->next)
765 EmpathyChatroom *chatroom;
766 TpAccount *this_account;
767 const gchar *this_room;
770 this_account = empathy_chatroom_get_account (chatroom);
771 this_room = empathy_chatroom_get_room (chatroom);
773 if (this_account && this_room && account == this_account
774 && strcmp (this_room, room) == 0)
782 empathy_chatroom_manager_ensure_chatroom (EmpathyChatroomManager *manager,
787 EmpathyChatroom *chatroom;
789 chatroom = empathy_chatroom_manager_find (manager, account, room);
793 return g_object_ref (chatroom);
797 chatroom = empathy_chatroom_new_full (account,
801 empathy_chatroom_manager_add (manager, chatroom);
807 empathy_chatroom_manager_get_chatrooms (EmpathyChatroomManager *manager,
810 EmpathyChatroomManagerPriv *priv;
811 GList *chatrooms, *l;
813 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
815 priv = GET_PRIV (manager);
818 return g_list_copy (priv->chatrooms);
821 for (l = priv->chatrooms; l; l = l->next)
823 EmpathyChatroom *chatroom;
827 if (account == empathy_chatroom_get_account (chatroom))
828 chatrooms = g_list_append (chatrooms, chatroom);
835 chatroom_manager_chat_invalidated_cb (EmpathyTpChat *chat,
841 EmpathyChatroomManagerPriv *priv = GET_PRIV (manager);
844 for (l = priv->chatrooms; l; l = l->next)
846 EmpathyChatroom *chatroom = l->data;
848 if (empathy_chatroom_get_tp_chat (chatroom) != chat)
851 empathy_chatroom_set_tp_chat (chatroom, NULL);
853 if (!empathy_chatroom_is_favorite (chatroom))
855 /* Remove the chatroom from the list, unless it's in the list of
857 * FIXME this policy should probably not be in libempathy */
858 chatroom_manager_remove_link (manager, l);
866 observe_channels_cb (TpSimpleObserver *observer,
868 TpConnection *connection,
870 TpChannelDispatchOperation *dispatch_operation,
872 TpObserveChannelsContext *context,
875 EmpathyChatroomManager *self = user_data;
878 for (l = channels; l != NULL; l = g_list_next (l))
880 EmpathyTpChat *tp_chat = l->data;
881 const gchar *roomname;
882 EmpathyChatroom *chatroom;
884 if (tp_proxy_get_invalidated ((TpChannel *) tp_chat) != NULL)
887 if (!EMPATHY_IS_TP_CHAT (tp_chat))
890 roomname = empathy_tp_chat_get_id (tp_chat);
891 chatroom = empathy_chatroom_manager_find (self, account, roomname);
893 if (chatroom == NULL)
895 chatroom = empathy_chatroom_new_full (account, roomname, roomname,
897 empathy_chatroom_manager_add (self, chatroom);
898 g_object_unref (chatroom);
901 empathy_chatroom_set_tp_chat (chatroom, tp_chat);
903 g_signal_connect (tp_chat, "invalidated",
904 G_CALLBACK (chatroom_manager_chat_invalidated_cb),
908 tp_observe_channels_context_accept (context);