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>
25 #include "empathy-chatroom-manager.h"
29 #include "empathy-client-factory.h"
30 #include "empathy-utils.h"
32 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
33 #include "empathy-debug.h"
35 #define CHATROOMS_XML_FILENAME "chatrooms.xml"
36 #define CHATROOMS_DTD_RESOURCENAME "/org/gnome/Empathy/empathy-chatroom-manager.dtd"
39 static EmpathyChatroomManager *chatroom_manager_singleton = NULL;
41 static void observe_channels_cb (TpSimpleObserver *observer,
43 TpConnection *connection,
45 TpChannelDispatchOperation *dispatch_operation,
47 TpObserveChannelsContext *context,
50 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatroomManager)
55 TpAccountManager *account_manager;
57 /* source id of the autosave timer */
60 GFileMonitor *monitor;
63 TpBaseClient *observer;
64 } EmpathyChatroomManagerPriv;
72 static guint signals[LAST_SIGNAL];
82 G_DEFINE_TYPE (EmpathyChatroomManager, empathy_chatroom_manager, G_TYPE_OBJECT);
85 * API to save/load and parse the chatrooms file.
89 chatroom_manager_file_save (EmpathyChatroomManager *manager)
91 EmpathyChatroomManagerPriv *priv;
96 priv = GET_PRIV (manager);
100 doc = xmlNewDoc ((const xmlChar *) "1.0");
101 root = xmlNewNode (NULL, (const xmlChar *) "chatrooms");
102 xmlDocSetRootElement (doc, root);
104 for (l = priv->chatrooms; l; l = l->next)
106 EmpathyChatroom *chatroom;
108 const gchar *account_id;
112 if (!empathy_chatroom_is_favorite (chatroom))
115 account_id = tp_proxy_get_object_path (empathy_chatroom_get_account (
118 node = xmlNewChild (root, NULL, (const xmlChar *) "chatroom", NULL);
119 xmlNewTextChild (node, NULL, (const xmlChar *) "name",
120 (const xmlChar *) empathy_chatroom_get_name (chatroom));
121 xmlNewTextChild (node, NULL, (const xmlChar *) "room",
122 (const xmlChar *) empathy_chatroom_get_room (chatroom));
123 xmlNewTextChild (node, NULL, (const xmlChar *) "account",
124 (const xmlChar *) account_id);
125 xmlNewTextChild (node, NULL, (const xmlChar *) "auto_connect",
126 empathy_chatroom_get_auto_connect (chatroom) ?
127 (const xmlChar *) "yes" : (const xmlChar *) "no");
128 xmlNewTextChild (node, NULL, (const xmlChar *) "always_urgent",
129 empathy_chatroom_is_always_urgent (chatroom) ?
130 (const xmlChar *) "yes" : (const xmlChar *) "no");
133 /* Make sure the XML is indented properly */
134 xmlIndentTreeOutput = 1;
136 DEBUG ("Saving file:'%s'", priv->file);
137 xmlSaveFormatFileEnc (priv->file, doc, "utf-8", 1);
142 priv->writing = FALSE;
147 save_timeout (EmpathyChatroomManager *self)
149 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
151 priv->save_timer_id = 0;
152 chatroom_manager_file_save (self);
158 reset_save_timeout (EmpathyChatroomManager *self)
160 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
162 if (priv->save_timer_id > 0)
163 g_source_remove (priv->save_timer_id);
165 priv->save_timer_id = g_timeout_add_seconds (SAVE_TIMER,
166 (GSourceFunc) save_timeout, self);
170 chatroom_changed_cb (EmpathyChatroom *chatroom,
172 EmpathyChatroomManager *self)
174 reset_save_timeout (self);
178 add_chatroom (EmpathyChatroomManager *self,
179 EmpathyChatroom *chatroom)
181 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
183 priv->chatrooms = g_list_prepend (priv->chatrooms, g_object_ref (chatroom));
185 /* Watch only those properties which are exported in the save file */
186 g_signal_connect (chatroom, "notify::name",
187 G_CALLBACK (chatroom_changed_cb), self);
188 g_signal_connect (chatroom, "notify::room",
189 G_CALLBACK (chatroom_changed_cb), self);
190 g_signal_connect (chatroom, "notify::account",
191 G_CALLBACK (chatroom_changed_cb), self);
192 g_signal_connect (chatroom, "notify::auto-connect",
193 G_CALLBACK (chatroom_changed_cb), self);
194 g_signal_connect (chatroom, "notify::always_urgent",
195 G_CALLBACK (chatroom_changed_cb), self);
196 g_signal_connect (chatroom, "notify::favorite",
197 G_CALLBACK (chatroom_changed_cb), self);
201 chatroom_manager_parse_chatroom (EmpathyChatroomManager *manager,
204 EmpathyChatroom *chatroom = NULL;
211 gboolean auto_connect;
212 gboolean always_urgent;
213 EmpathyClientFactory *factory;
214 GError *error = NULL;
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 has to be a valid Account object path */
264 if (!tp_dbus_check_valid_object_path (account_id, NULL) ||
265 !g_str_has_prefix (account_id, TP_ACCOUNT_OBJECT_PATH_BASE))
268 factory = empathy_client_factory_dup ();
270 account = tp_simple_client_factory_ensure_account (
271 TP_SIMPLE_CLIENT_FACTORY (factory), account_id, NULL, &error);
272 g_object_unref (factory);
276 DEBUG ("Failed to create account: %s", error->message);
277 g_error_free (error);
285 chatroom = empathy_chatroom_new_full (account, room, name, auto_connect);
286 empathy_chatroom_set_favorite (chatroom, TRUE);
287 empathy_chatroom_set_always_urgent (chatroom, always_urgent);
288 add_chatroom (manager, chatroom);
289 g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
295 tp_clear_object (&chatroom);
299 chatroom_manager_file_parse (EmpathyChatroomManager *manager,
300 const gchar *filename)
302 EmpathyChatroomManagerPriv *priv;
303 xmlParserCtxtPtr ctxt;
305 xmlNodePtr chatrooms;
308 priv = GET_PRIV (manager);
310 DEBUG ("Attempting to parse file:'%s'...", filename);
312 ctxt = xmlNewParserCtxt ();
314 /* Parse and validate the file. */
315 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
318 g_warning ("Failed to parse file:'%s'", filename);
319 xmlFreeParserCtxt (ctxt);
323 if (!empathy_xml_validate_from_resource (doc, CHATROOMS_DTD_RESOURCENAME))
325 g_warning ("Failed to validate file:'%s'", filename);
327 xmlFreeParserCtxt (ctxt);
331 /* The root node, chatrooms. */
332 chatrooms = xmlDocGetRootElement (doc);
334 for (node = chatrooms->children; node; node = node->next)
336 if (strcmp ((gchar *) node->name, "chatroom") == 0)
337 chatroom_manager_parse_chatroom (manager, node);
340 DEBUG ("Parsed %d chatrooms", g_list_length (priv->chatrooms));
343 xmlFreeParserCtxt (ctxt);
349 chatroom_manager_get_all (EmpathyChatroomManager *manager)
351 EmpathyChatroomManagerPriv *priv;
353 priv = GET_PRIV (manager);
356 if (g_file_test (priv->file, G_FILE_TEST_EXISTS) &&
357 !chatroom_manager_file_parse (manager, priv->file))
363 g_object_notify (G_OBJECT (manager), "ready");
370 empathy_chatroom_manager_get_property (GObject *object,
375 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
376 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
381 g_value_set_string (value, priv->file);
384 g_value_set_boolean (value, priv->ready);
387 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
393 empathy_chatroom_manager_set_property (GObject *object,
398 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
399 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
405 priv->file = g_value_dup_string (value);
408 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
414 chatroom_manager_dispose (GObject *object)
416 EmpathyChatroomManagerPriv *priv;
418 priv = GET_PRIV (object);
420 tp_clear_object (&priv->observer);
421 tp_clear_object (&priv->monitor);
423 (G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->dispose) (object);
427 clear_chatrooms (EmpathyChatroomManager *self)
429 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
432 tmp = priv->chatrooms;
434 /* Unreffing the chatroom may result in destroying the underlying
435 * EmpathyTpChat which will fire the invalidated signal and so make us
436 * re-call this function. We already set priv->chatrooms to NULL so we won't
437 * try to destroy twice the same objects. */
438 priv->chatrooms = NULL;
440 for (l = tmp; l != NULL; l = g_list_next (l))
442 EmpathyChatroom *chatroom = l->data;
444 g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb,
446 g_signal_emit (self, signals[CHATROOM_REMOVED], 0, chatroom);
448 g_object_unref (chatroom);
455 chatroom_manager_finalize (GObject *object)
457 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
458 EmpathyChatroomManagerPriv *priv;
460 priv = GET_PRIV (object);
462 g_object_unref (priv->account_manager);
464 if (priv->save_timer_id > 0)
466 /* have to save before destroy the object */
467 g_source_remove (priv->save_timer_id);
468 priv->save_timer_id = 0;
469 chatroom_manager_file_save (self);
472 clear_chatrooms (self);
476 (G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->finalize) (object);
480 file_changed_cb (GFileMonitor *monitor,
483 GFileMonitorEvent event_type,
486 EmpathyChatroomManager *self = user_data;
487 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
489 if (event_type != G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT)
495 DEBUG ("chatrooms file changed; reloading list");
497 clear_chatrooms (self);
498 chatroom_manager_get_all (self);
502 account_manager_ready_cb (GObject *source_object,
503 GAsyncResult *result,
506 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (user_data);
507 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
508 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
509 GError *error = NULL;
512 if (!tp_proxy_prepare_finish (manager, result, &error))
514 DEBUG ("Failed to prepare account manager: %s", error->message);
515 g_error_free (error);
519 chatroom_manager_get_all (self);
521 /* Set up file monitor */
522 file = g_file_new_for_path (priv->file);
524 priv->monitor = g_file_monitor (file, 0, NULL, &error);
525 if (priv->monitor == NULL)
527 DEBUG ("Failed to create file monitor on %s: %s", priv->file,
530 g_error_free (error);
534 g_signal_connect (priv->monitor, "changed", G_CALLBACK (file_changed_cb),
538 tp_clear_object (&file);
539 g_object_unref (self);
543 empathy_chatroom_manager_constructor (GType type,
545 GObjectConstructParam *props)
548 EmpathyChatroomManager *self;
549 EmpathyChatroomManagerPriv *priv;
550 GError *error = NULL;
552 if (chatroom_manager_singleton != NULL)
553 return g_object_ref (chatroom_manager_singleton);
555 /* Parent constructor chain */
556 obj = G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->
557 constructor (type, n_props, props);
559 self = EMPATHY_CHATROOM_MANAGER (obj);
560 priv = GET_PRIV (self);
564 chatroom_manager_singleton = self;
565 g_object_add_weak_pointer (obj, (gpointer) &chatroom_manager_singleton);
567 priv->account_manager = tp_account_manager_dup ();
569 tp_proxy_prepare_async (priv->account_manager, NULL,
570 account_manager_ready_cb, g_object_ref (self));
572 if (priv->file == NULL)
574 /* Set the default file path */
577 dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
578 if (!g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
579 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
581 priv->file = g_build_filename (dir, CHATROOMS_XML_FILENAME, NULL);
585 /* Setup a room observer */
586 priv->observer = tp_simple_observer_new_with_am (priv->account_manager, TRUE,
587 "Empathy.ChatroomManager", TRUE, observe_channels_cb, self, NULL);
589 tp_base_client_take_observer_filter (priv->observer, tp_asv_new (
590 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
591 TP_IFACE_CHANNEL_TYPE_TEXT,
592 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
596 if (!tp_base_client_register (priv->observer, &error))
598 g_critical ("Failed to register Observer: %s", error->message);
600 g_error_free (error);
607 empathy_chatroom_manager_class_init (EmpathyChatroomManagerClass *klass)
609 GObjectClass *object_class = G_OBJECT_CLASS (klass);
610 GParamSpec *param_spec;
612 object_class->constructor = empathy_chatroom_manager_constructor;
613 object_class->get_property = empathy_chatroom_manager_get_property;
614 object_class->set_property = empathy_chatroom_manager_set_property;
615 object_class->dispose = chatroom_manager_dispose;
616 object_class->finalize = chatroom_manager_finalize;
618 param_spec = g_param_spec_string (
620 "path of the favorite file",
621 "The path of the XML file containing user's favorites",
623 G_PARAM_CONSTRUCT_ONLY |
625 G_PARAM_STATIC_NAME |
626 G_PARAM_STATIC_NICK |
627 G_PARAM_STATIC_BLURB);
628 g_object_class_install_property (object_class, PROP_FILE, param_spec);
630 param_spec = g_param_spec_boolean (
632 "whether the manager is ready yet",
633 "whether the manager is ready yet",
636 g_object_class_install_property (object_class, PROP_READY, param_spec);
638 signals[CHATROOM_ADDED] = g_signal_new ("chatroom-added",
639 G_TYPE_FROM_CLASS (klass),
642 g_cclosure_marshal_generic,
644 1, EMPATHY_TYPE_CHATROOM);
646 signals[CHATROOM_REMOVED] = g_signal_new ("chatroom-removed",
647 G_TYPE_FROM_CLASS (klass),
650 g_cclosure_marshal_generic,
652 1, EMPATHY_TYPE_CHATROOM);
654 g_type_class_add_private (object_class, sizeof (EmpathyChatroomManagerPriv));
658 empathy_chatroom_manager_init (EmpathyChatroomManager *manager)
660 EmpathyChatroomManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
661 EMPATHY_TYPE_CHATROOM_MANAGER, EmpathyChatroomManagerPriv);
663 manager->priv = priv;
666 EmpathyChatroomManager *
667 empathy_chatroom_manager_dup_singleton (const gchar *file)
669 return EMPATHY_CHATROOM_MANAGER (g_object_new (EMPATHY_TYPE_CHATROOM_MANAGER,
670 "file", file, NULL));
674 empathy_chatroom_manager_add (EmpathyChatroomManager *manager,
675 EmpathyChatroom *chatroom)
677 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), FALSE);
678 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), FALSE);
680 /* don't add more than once */
681 if (!empathy_chatroom_manager_find (manager,
682 empathy_chatroom_get_account (chatroom),
683 empathy_chatroom_get_room (chatroom)))
685 add_chatroom (manager, chatroom);
687 if (empathy_chatroom_is_favorite (chatroom))
688 reset_save_timeout (manager);
690 g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
698 chatroom_manager_remove_link (EmpathyChatroomManager *manager,
701 EmpathyChatroomManagerPriv *priv;
702 EmpathyChatroom *chatroom;
704 priv = GET_PRIV (manager);
708 if (empathy_chatroom_is_favorite (chatroom))
709 reset_save_timeout (manager);
711 priv->chatrooms = g_list_delete_link (priv->chatrooms, l);
713 g_signal_emit (manager, signals[CHATROOM_REMOVED], 0, chatroom);
714 g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb, manager);
716 g_object_unref (chatroom);
720 empathy_chatroom_manager_remove (EmpathyChatroomManager *manager,
721 EmpathyChatroom *chatroom)
723 EmpathyChatroomManagerPriv *priv;
726 g_return_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager));
727 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
729 priv = GET_PRIV (manager);
731 for (l = priv->chatrooms; l; l = l->next)
733 EmpathyChatroom *this_chatroom;
735 this_chatroom = l->data;
737 if (this_chatroom == chatroom ||
738 empathy_chatroom_equal (chatroom, this_chatroom))
740 chatroom_manager_remove_link (manager, l);
747 empathy_chatroom_manager_find (EmpathyChatroomManager *manager,
751 EmpathyChatroomManagerPriv *priv;
754 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
755 g_return_val_if_fail (room != NULL, NULL);
757 priv = GET_PRIV (manager);
759 for (l = priv->chatrooms; l; l = l->next)
761 EmpathyChatroom *chatroom;
762 TpAccount *this_account;
763 const gchar *this_room;
766 this_account = empathy_chatroom_get_account (chatroom);
767 this_room = empathy_chatroom_get_room (chatroom);
769 if (this_account && this_room && account == this_account
770 && strcmp (this_room, room) == 0)
778 empathy_chatroom_manager_ensure_chatroom (EmpathyChatroomManager *manager,
783 EmpathyChatroom *chatroom;
785 chatroom = empathy_chatroom_manager_find (manager, account, room);
789 return g_object_ref (chatroom);
793 chatroom = empathy_chatroom_new_full (account,
797 empathy_chatroom_manager_add (manager, chatroom);
803 empathy_chatroom_manager_get_chatrooms (EmpathyChatroomManager *manager,
806 EmpathyChatroomManagerPriv *priv;
807 GList *chatrooms, *l;
809 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
811 priv = GET_PRIV (manager);
814 return g_list_copy (priv->chatrooms);
817 for (l = priv->chatrooms; l; l = l->next)
819 EmpathyChatroom *chatroom;
823 if (account == empathy_chatroom_get_account (chatroom))
824 chatrooms = g_list_append (chatrooms, chatroom);
831 chatroom_manager_chat_invalidated_cb (EmpathyTpChat *chat,
837 EmpathyChatroomManagerPriv *priv = GET_PRIV (manager);
840 for (l = priv->chatrooms; l; l = l->next)
842 EmpathyChatroom *chatroom = l->data;
844 if (empathy_chatroom_get_tp_chat (chatroom) != chat)
847 empathy_chatroom_set_tp_chat (chatroom, NULL);
849 if (!empathy_chatroom_is_favorite (chatroom))
851 /* Remove the chatroom from the list, unless it's in the list of
853 * FIXME this policy should probably not be in libempathy */
854 chatroom_manager_remove_link (manager, l);
862 observe_channels_cb (TpSimpleObserver *observer,
864 TpConnection *connection,
866 TpChannelDispatchOperation *dispatch_operation,
868 TpObserveChannelsContext *context,
871 EmpathyChatroomManager *self = user_data;
874 for (l = channels; l != NULL; l = g_list_next (l))
876 EmpathyTpChat *tp_chat = l->data;
877 const gchar *roomname;
878 EmpathyChatroom *chatroom;
880 if (tp_proxy_get_invalidated ((TpChannel *) tp_chat) != NULL)
883 if (!EMPATHY_IS_TP_CHAT (tp_chat))
886 roomname = empathy_tp_chat_get_id (tp_chat);
887 chatroom = empathy_chatroom_manager_find (self, account, roomname);
889 if (chatroom == NULL)
891 chatroom = empathy_chatroom_new_full (account, roomname, roomname,
893 empathy_chatroom_manager_add (self, chatroom);
894 g_object_unref (chatroom);
897 empathy_chatroom_set_tp_chat (chatroom, tp_chat);
899 g_signal_connect (tp_chat, "invalidated",
900 G_CALLBACK (chatroom_manager_chat_invalidated_cb),
904 tp_observe_channels_context_accept (context);