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);
136 save_timeout (EmpathyChatroomManager *self)
138 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
140 priv->save_timer_id = 0;
141 chatroom_manager_file_save (self);
147 reset_save_timeout (EmpathyChatroomManager *self)
149 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
151 if (priv->save_timer_id > 0)
153 g_source_remove (priv->save_timer_id);
156 priv->save_timer_id = g_timeout_add_seconds (SAVE_TIMER,
157 (GSourceFunc) save_timeout, self);
161 chatroom_changed_cb (EmpathyChatroom *chatroom,
163 EmpathyChatroomManager *self)
165 reset_save_timeout (self);
169 add_chatroom (EmpathyChatroomManager *self,
170 EmpathyChatroom *chatroom)
172 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
174 priv->chatrooms = g_list_prepend (priv->chatrooms, g_object_ref (chatroom));
176 g_signal_connect (chatroom, "notify",
177 G_CALLBACK (chatroom_changed_cb), self);
181 chatroom_manager_parse_chatroom (EmpathyChatroomManager *manager,
184 EmpathyChatroomManagerPriv *priv;
185 EmpathyChatroom *chatroom;
192 gboolean auto_connect;
194 priv = GET_PRIV (manager);
196 /* default values. */
202 for (child = node->children; child; child = child->next) {
205 if (xmlNodeIsText (child)) {
209 tag = (gchar *) child->name;
210 str = (gchar *) xmlNodeGetContent (child);
212 if (strcmp (tag, "name") == 0) {
213 name = g_strdup (str);
215 else if (strcmp (tag, "room") == 0) {
216 room = g_strdup (str);
218 else if (strcmp (tag, "auto_connect") == 0) {
219 if (strcmp (str, "yes") == 0) {
222 auto_connect = FALSE;
225 else if (strcmp (tag, "account") == 0) {
226 account_id = g_strdup (str);
232 account = tp_account_manager_ensure_account (priv->account_manager,
241 chatroom = empathy_chatroom_new_full (account, room, name, auto_connect);
242 empathy_chatroom_set_favorite (chatroom, TRUE);
243 add_chatroom (manager, chatroom);
244 g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
252 chatroom_manager_file_parse (EmpathyChatroomManager *manager,
253 const gchar *filename)
255 EmpathyChatroomManagerPriv *priv;
256 xmlParserCtxtPtr ctxt;
258 xmlNodePtr chatrooms;
261 priv = GET_PRIV (manager);
263 DEBUG ("Attempting to parse file:'%s'...", filename);
265 ctxt = xmlNewParserCtxt ();
267 /* Parse and validate the file. */
268 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
270 g_warning ("Failed to parse file:'%s'", filename);
271 xmlFreeParserCtxt (ctxt);
275 if (!empathy_xml_validate (doc, CHATROOMS_DTD_FILENAME)) {
276 g_warning ("Failed to validate file:'%s'", filename);
278 xmlFreeParserCtxt (ctxt);
282 /* The root node, chatrooms. */
283 chatrooms = xmlDocGetRootElement (doc);
285 for (node = chatrooms->children; node; node = node->next) {
286 if (strcmp ((gchar *) node->name, "chatroom") == 0) {
287 chatroom_manager_parse_chatroom (manager, node);
291 DEBUG ("Parsed %d chatrooms", g_list_length (priv->chatrooms));
294 xmlFreeParserCtxt (ctxt);
300 chatroom_manager_get_all (EmpathyChatroomManager *manager)
302 EmpathyChatroomManagerPriv *priv;
304 priv = GET_PRIV (manager);
307 if (g_file_test (priv->file, G_FILE_TEST_EXISTS) &&
308 !chatroom_manager_file_parse (manager, priv->file)) {
313 g_object_notify (G_OBJECT (manager), "ready");
319 empathy_chatroom_manager_get_property (GObject *object,
324 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
325 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
330 g_value_set_string (value, priv->file);
333 g_value_set_boolean (value, priv->ready);
336 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
342 empathy_chatroom_manager_set_property (GObject *object,
347 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
348 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
354 priv->file = g_value_dup_string (value);
357 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
363 chatroom_manager_finalize (GObject *object)
365 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
366 EmpathyChatroomManagerPriv *priv;
369 priv = GET_PRIV (object);
371 g_object_unref (priv->account_manager);
373 if (priv->save_timer_id > 0)
375 /* have to save before destroy the object */
376 g_source_remove (priv->save_timer_id);
377 priv->save_timer_id = 0;
378 chatroom_manager_file_save (self);
381 for (l = priv->chatrooms; l != NULL; l = g_list_next (l))
383 EmpathyChatroom *chatroom = l->data;
385 g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb,
388 g_object_unref (chatroom);
391 g_list_free (priv->chatrooms);
394 (G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->finalize) (object);
398 account_manager_ready_cb (GObject *source_object,
399 GAsyncResult *result,
402 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (user_data);
403 TpAccountManager *manager = TP_ACCOUNT_MANAGER (source_object);
404 GError *error = NULL;
406 if (!tp_account_manager_prepare_finish (manager, result, &error))
408 DEBUG ("Failed to prepare account manager: %s", error->message);
409 g_error_free (error);
413 chatroom_manager_get_all (self);
417 empathy_chatroom_manager_constructor (GType type,
419 GObjectConstructParam *props)
422 EmpathyChatroomManager *self;
423 EmpathyChatroomManagerPriv *priv;
425 if (chatroom_manager_singleton != NULL)
426 return g_object_ref (chatroom_manager_singleton);
428 /* Parent constructor chain */
429 obj = G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->
430 constructor (type, n_props, props);
432 self = EMPATHY_CHATROOM_MANAGER (obj);
433 priv = GET_PRIV (self);
437 chatroom_manager_singleton = self;
438 g_object_add_weak_pointer (obj, (gpointer) &chatroom_manager_singleton);
440 priv->account_manager = tp_account_manager_dup ();
442 tp_account_manager_prepare_async (priv->account_manager, NULL,
443 account_manager_ready_cb, self);
445 if (priv->file == NULL)
447 /* Set the default file path */
450 dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
451 if (!g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
452 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
454 priv->file = g_build_filename (dir, CHATROOMS_XML_FILENAME, NULL);
462 empathy_chatroom_manager_class_init (EmpathyChatroomManagerClass *klass)
464 GObjectClass *object_class = G_OBJECT_CLASS (klass);
465 GParamSpec *param_spec;
467 object_class->constructor = empathy_chatroom_manager_constructor;
468 object_class->get_property = empathy_chatroom_manager_get_property;
469 object_class->set_property = empathy_chatroom_manager_set_property;
470 object_class->finalize = chatroom_manager_finalize;
472 param_spec = g_param_spec_string (
474 "path of the favorite file",
475 "The path of the XML file containing user's favorites",
477 G_PARAM_CONSTRUCT_ONLY |
479 G_PARAM_STATIC_NAME |
480 G_PARAM_STATIC_NICK |
481 G_PARAM_STATIC_BLURB);
482 g_object_class_install_property (object_class, PROP_FILE, param_spec);
484 param_spec = g_param_spec_boolean (
486 "whether the manager is ready yet",
487 "whether the manager is ready yet",
490 g_object_class_install_property (object_class, PROP_READY, param_spec);
492 signals[CHATROOM_ADDED] = g_signal_new ("chatroom-added",
493 G_TYPE_FROM_CLASS (klass),
496 g_cclosure_marshal_VOID__OBJECT,
498 1, EMPATHY_TYPE_CHATROOM);
500 signals[CHATROOM_REMOVED] = g_signal_new ("chatroom-removed",
501 G_TYPE_FROM_CLASS (klass),
504 g_cclosure_marshal_VOID__OBJECT,
506 1, EMPATHY_TYPE_CHATROOM);
508 g_type_class_add_private (object_class, sizeof (EmpathyChatroomManagerPriv));
512 empathy_chatroom_manager_init (EmpathyChatroomManager *manager)
514 EmpathyChatroomManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
515 EMPATHY_TYPE_CHATROOM_MANAGER, EmpathyChatroomManagerPriv);
517 manager->priv = priv;
520 EmpathyChatroomManager *
521 empathy_chatroom_manager_dup_singleton (const gchar *file)
523 return EMPATHY_CHATROOM_MANAGER (g_object_new (EMPATHY_TYPE_CHATROOM_MANAGER,
524 "file", file, NULL));
528 empathy_chatroom_manager_add (EmpathyChatroomManager *manager,
529 EmpathyChatroom *chatroom)
531 EmpathyChatroomManagerPriv *priv;
533 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), FALSE);
534 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), FALSE);
536 priv = GET_PRIV (manager);
538 /* don't add more than once */
539 if (!empathy_chatroom_manager_find (manager,
540 empathy_chatroom_get_account (chatroom),
541 empathy_chatroom_get_room (chatroom)))
543 add_chatroom (manager, chatroom);
545 if (empathy_chatroom_is_favorite (chatroom))
546 reset_save_timeout (manager);
548 g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
556 chatroom_manager_remove_link (EmpathyChatroomManager *manager,
559 EmpathyChatroomManagerPriv *priv;
560 EmpathyChatroom *chatroom;
562 priv = GET_PRIV (manager);
566 if (empathy_chatroom_is_favorite (chatroom))
567 reset_save_timeout (manager);
569 priv->chatrooms = g_list_delete_link (priv->chatrooms, l);
571 g_signal_emit (manager, signals[CHATROOM_REMOVED], 0, chatroom);
572 g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb, manager);
574 g_object_unref (chatroom);
578 empathy_chatroom_manager_remove (EmpathyChatroomManager *manager,
579 EmpathyChatroom *chatroom)
581 EmpathyChatroomManagerPriv *priv;
584 g_return_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager));
585 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
587 priv = GET_PRIV (manager);
589 for (l = priv->chatrooms; l; l = l->next)
591 EmpathyChatroom *this_chatroom;
593 this_chatroom = l->data;
595 if (this_chatroom == chatroom ||
596 empathy_chatroom_equal (chatroom, this_chatroom))
598 chatroom_manager_remove_link (manager, l);
605 empathy_chatroom_manager_find (EmpathyChatroomManager *manager,
609 EmpathyChatroomManagerPriv *priv;
612 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
613 g_return_val_if_fail (room != NULL, NULL);
615 priv = GET_PRIV (manager);
617 for (l = priv->chatrooms; l; l = l->next) {
618 EmpathyChatroom *chatroom;
619 TpAccount *this_account;
620 const gchar *this_room;
623 this_account = empathy_chatroom_get_account (chatroom);
624 this_room = empathy_chatroom_get_room (chatroom);
626 if (this_account && this_room && account == this_account
627 && strcmp (this_room, room) == 0) {
636 empathy_chatroom_manager_get_chatrooms (EmpathyChatroomManager *manager,
639 EmpathyChatroomManagerPriv *priv;
640 GList *chatrooms, *l;
642 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
644 priv = GET_PRIV (manager);
647 return g_list_copy (priv->chatrooms);
651 for (l = priv->chatrooms; l; l = l->next) {
652 EmpathyChatroom *chatroom;
656 if (account == empathy_chatroom_get_account (chatroom)) {
657 chatrooms = g_list_append (chatrooms, chatroom);
665 empathy_chatroom_manager_get_count (EmpathyChatroomManager *manager,
668 EmpathyChatroomManagerPriv *priv;
672 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), 0);
674 priv = GET_PRIV (manager);
677 return g_list_length (priv->chatrooms);
680 for (l = priv->chatrooms; l; l = l->next) {
681 EmpathyChatroom *chatroom;
685 if (account == empathy_chatroom_get_account (chatroom)) {
694 chatroom_manager_chat_destroyed_cb (EmpathyTpChat *chat,
697 EmpathyChatroomManagerPriv *priv = GET_PRIV (manager);
700 for (l = priv->chatrooms; l; l = l->next)
702 EmpathyChatroom *chatroom = l->data;
704 if (empathy_chatroom_get_tp_chat (chatroom) != chat)
707 empathy_chatroom_set_tp_chat (chatroom, NULL);
709 if (!empathy_chatroom_is_favorite (chatroom))
711 /* Remove the chatroom from the list, unless it's in the list of
713 * FIXME this policy should probably not be in libempathy */
714 chatroom_manager_remove_link (manager, l);
722 chatroom_manager_observe_channel_cb (EmpathyDispatcher *dispatcher,
723 EmpathyDispatchOperation *operation, gpointer manager)
725 EmpathyChatroom *chatroom;
728 const gchar *roomname;
730 TpHandleType handle_type;
732 TpConnection *connection;
734 channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
736 /* Observe Text channels to rooms only */
737 if (channel_type != TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
740 channel = empathy_dispatch_operation_get_channel (operation);
741 tp_channel_get_handle (channel, &handle_type);
743 if (handle_type != TP_HANDLE_TYPE_ROOM)
746 chat = EMPATHY_TP_CHAT (
747 empathy_dispatch_operation_get_channel_wrapper (operation));
748 connection = empathy_tp_chat_get_connection (chat);
749 account = empathy_get_account_for_connection (connection);
751 roomname = empathy_tp_chat_get_id (chat);
753 chatroom = empathy_chatroom_manager_find (manager, account, roomname);
755 if (chatroom == NULL)
757 chatroom = empathy_chatroom_new_full (account, roomname, roomname,
759 empathy_chatroom_set_tp_chat (chatroom, chat);
760 empathy_chatroom_manager_add (manager, chatroom);
761 g_object_unref (chatroom);
765 empathy_chatroom_set_tp_chat (chatroom, chat);
768 /* A TpChat is always destroyed as it only gets unreffed after the channel
769 * has been invalidated in the dispatcher.. */
770 g_signal_connect (chat, "destroy",
771 G_CALLBACK (chatroom_manager_chat_destroyed_cb),
776 empathy_chatroom_manager_observe (EmpathyChatroomManager *manager,
777 EmpathyDispatcher *dispatcher)
779 g_signal_connect (dispatcher, "observe",
780 G_CALLBACK (chatroom_manager_observe_channel_cb), manager);