Break after deleting current item in chatroom list
[empathy-mirror.git] / libempathy / empathy-chatroom-manager.c
blob44416670e4c948d4aeddd1e93377a2e132fdc6a4
1 /*
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>
24 #include "config.h"
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
33 #include "empathy-tp-chat.h"
34 #include "empathy-chatroom-manager.h"
35 #include "empathy-account-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"
43 #define SAVE_TIMER 4
45 static EmpathyChatroomManager *chatroom_manager_singleton = NULL;
47 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatroomManager)
48 typedef struct
50 GList *chatrooms;
51 gchar *file;
52 EmpathyAccountManager *account_manager;
53 /* source id of the autosave timer */
54 gint save_timer_id;
55 } EmpathyChatroomManagerPriv;
57 enum {
58 CHATROOM_ADDED,
59 CHATROOM_REMOVED,
60 LAST_SIGNAL
63 static guint signals[LAST_SIGNAL];
65 /* properties */
66 enum
68 PROP_FILE = 1,
69 LAST_PROPERTY
72 G_DEFINE_TYPE (EmpathyChatroomManager, empathy_chatroom_manager, G_TYPE_OBJECT);
75 * API to save/load and parse the chatrooms file.
78 static gboolean
79 chatroom_manager_file_save (EmpathyChatroomManager *manager)
81 EmpathyChatroomManagerPriv *priv;
82 xmlDocPtr doc;
83 xmlNodePtr root;
84 GList *l;
86 priv = GET_PRIV (manager);
88 doc = xmlNewDoc ("1.0");
89 root = xmlNewNode (NULL, "chatrooms");
90 xmlDocSetRootElement (doc, root);
92 for (l = priv->chatrooms; l; l = l->next) {
93 EmpathyChatroom *chatroom;
94 xmlNodePtr node;
95 const gchar *account_id;
97 chatroom = l->data;
99 if (!empathy_chatroom_is_favorite (chatroom)) {
100 continue;
103 account_id = mc_account_get_unique_name (empathy_chatroom_get_account (chatroom));
105 node = xmlNewChild (root, NULL, "chatroom", NULL);
106 xmlNewTextChild (node, NULL, "name", empathy_chatroom_get_name (chatroom));
107 xmlNewTextChild (node, NULL, "room", empathy_chatroom_get_room (chatroom));
108 xmlNewTextChild (node, NULL, "account", account_id);
109 xmlNewTextChild (node, NULL, "auto_connect",
110 empathy_chatroom_get_auto_connect (chatroom) ? "yes" : "no");
113 /* Make sure the XML is indented properly */
114 xmlIndentTreeOutput = 1;
116 DEBUG ("Saving file:'%s'", priv->file);
117 xmlSaveFormatFileEnc (priv->file, doc, "utf-8", 1);
118 xmlFreeDoc (doc);
120 xmlCleanupParser ();
121 xmlMemoryDump ();
123 return TRUE;
126 static gboolean
127 save_timeout (EmpathyChatroomManager *self)
129 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
131 priv->save_timer_id = 0;
132 chatroom_manager_file_save (self);
134 return FALSE;
137 static void
138 reset_save_timeout (EmpathyChatroomManager *self)
140 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
142 if (priv->save_timer_id > 0)
144 g_source_remove (priv->save_timer_id);
147 priv->save_timer_id = g_timeout_add_seconds (SAVE_TIMER,
148 (GSourceFunc) save_timeout, self);
151 static void
152 chatroom_changed_cb (EmpathyChatroom *chatroom,
153 GParamSpec *spec,
154 EmpathyChatroomManager *self)
156 reset_save_timeout (self);
159 static void
160 add_chatroom (EmpathyChatroomManager *self,
161 EmpathyChatroom *chatroom)
163 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
165 priv->chatrooms = g_list_prepend (priv->chatrooms, g_object_ref (chatroom));
167 g_signal_connect (chatroom, "notify",
168 G_CALLBACK (chatroom_changed_cb), self);
171 static void
172 chatroom_manager_parse_chatroom (EmpathyChatroomManager *manager,
173 xmlNodePtr node)
175 EmpathyChatroomManagerPriv *priv;
176 EmpathyChatroom *chatroom;
177 McAccount *account;
178 xmlNodePtr child;
179 gchar *str;
180 gchar *name;
181 gchar *room;
182 gchar *account_id;
183 gboolean auto_connect;
185 priv = GET_PRIV (manager);
187 /* default values. */
188 name = NULL;
189 room = NULL;
190 auto_connect = TRUE;
191 account_id = NULL;
193 for (child = node->children; child; child = child->next) {
194 gchar *tag;
196 if (xmlNodeIsText (child)) {
197 continue;
200 tag = (gchar *) child->name;
201 str = (gchar *) xmlNodeGetContent (child);
203 if (strcmp (tag, "name") == 0) {
204 name = g_strdup (str);
206 else if (strcmp (tag, "room") == 0) {
207 room = g_strdup (str);
209 else if (strcmp (tag, "auto_connect") == 0) {
210 if (strcmp (str, "yes") == 0) {
211 auto_connect = TRUE;
212 } else {
213 auto_connect = FALSE;
216 else if (strcmp (tag, "account") == 0) {
217 account_id = g_strdup (str);
220 xmlFree (str);
223 account = mc_account_lookup (account_id);
224 if (!account) {
225 g_free (name);
226 g_free (room);
227 g_free (account_id);
228 return;
231 chatroom = empathy_chatroom_new_full (account, room, name, auto_connect);
232 empathy_chatroom_set_favorite (chatroom, TRUE);
233 add_chatroom (manager, chatroom);
234 g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
236 g_object_unref (account);
237 g_free (name);
238 g_free (room);
239 g_free (account_id);
242 static gboolean
243 chatroom_manager_file_parse (EmpathyChatroomManager *manager,
244 const gchar *filename)
246 EmpathyChatroomManagerPriv *priv;
247 xmlParserCtxtPtr ctxt;
248 xmlDocPtr doc;
249 xmlNodePtr chatrooms;
250 xmlNodePtr node;
252 priv = GET_PRIV (manager);
254 DEBUG ("Attempting to parse file:'%s'...", filename);
256 ctxt = xmlNewParserCtxt ();
258 /* Parse and validate the file. */
259 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
260 if (!doc) {
261 g_warning ("Failed to parse file:'%s'", filename);
262 xmlFreeParserCtxt (ctxt);
263 return FALSE;
266 if (!empathy_xml_validate (doc, CHATROOMS_DTD_FILENAME)) {
267 g_warning ("Failed to validate file:'%s'", filename);
268 xmlFreeDoc (doc);
269 xmlFreeParserCtxt (ctxt);
270 return FALSE;
273 /* The root node, chatrooms. */
274 chatrooms = xmlDocGetRootElement (doc);
276 for (node = chatrooms->children; node; node = node->next) {
277 if (strcmp ((gchar *) node->name, "chatroom") == 0) {
278 chatroom_manager_parse_chatroom (manager, node);
282 DEBUG ("Parsed %d chatrooms", g_list_length (priv->chatrooms));
284 xmlFreeDoc (doc);
285 xmlFreeParserCtxt (ctxt);
287 return TRUE;
290 static gboolean
291 chatroom_manager_get_all (EmpathyChatroomManager *manager)
293 EmpathyChatroomManagerPriv *priv;
295 priv = GET_PRIV (manager);
297 /* read file in */
298 if (g_file_test (priv->file, G_FILE_TEST_EXISTS) &&
299 !chatroom_manager_file_parse (manager, priv->file)) {
300 return FALSE;
303 return TRUE;
306 static void
307 empathy_chatroom_manager_get_property (GObject *object,
308 guint property_id,
309 GValue *value,
310 GParamSpec *pspec)
312 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
313 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
315 switch (property_id)
317 case PROP_FILE:
318 g_value_set_string (value, priv->file);
319 break;
320 default:
321 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
322 break;
326 static void
327 empathy_chatroom_manager_set_property (GObject *object,
328 guint property_id,
329 const GValue *value,
330 GParamSpec *pspec)
332 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
333 EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
335 switch (property_id)
337 case PROP_FILE:
338 g_free (priv->file);
339 priv->file = g_value_dup_string (value);
340 break;
341 default:
342 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
343 break;
347 static void
348 chatroom_manager_finalize (GObject *object)
350 EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
351 EmpathyChatroomManagerPriv *priv;
352 GList *l;
354 priv = GET_PRIV (object);
356 g_object_unref (priv->account_manager);
358 if (priv->save_timer_id > 0)
360 /* have to save before destroy the object */
361 g_source_remove (priv->save_timer_id);
362 priv->save_timer_id = 0;
363 chatroom_manager_file_save (self);
366 for (l = priv->chatrooms; l != NULL; l = g_list_next (l))
368 EmpathyChatroom *chatroom = l->data;
370 g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb,
371 self);
373 g_object_unref (chatroom);
376 g_list_free (priv->chatrooms);
377 g_free (priv->file);
379 (G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->finalize) (object);
382 static GObject *
383 empathy_chatroom_manager_constructor (GType type,
384 guint n_props,
385 GObjectConstructParam *props)
387 GObject *obj;
388 EmpathyChatroomManager *self;
389 EmpathyChatroomManagerPriv *priv;
391 if (chatroom_manager_singleton != NULL)
392 return g_object_ref (chatroom_manager_singleton);
394 /* Parent constructor chain */
395 obj = G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->
396 constructor (type, n_props, props);
398 self = EMPATHY_CHATROOM_MANAGER (obj);
399 priv = GET_PRIV (self);
401 chatroom_manager_singleton = self;
402 g_object_add_weak_pointer (obj, (gpointer) &chatroom_manager_singleton);
404 priv->account_manager = empathy_account_manager_dup_singleton ();
406 if (priv->file == NULL)
408 /* Set the default file path */
409 gchar *dir;
411 dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
412 if (!g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
413 g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
415 priv->file = g_build_filename (dir, CHATROOMS_XML_FILENAME, NULL);
416 g_free (dir);
419 chatroom_manager_get_all (self);
420 return obj;
423 static void
424 empathy_chatroom_manager_class_init (EmpathyChatroomManagerClass *klass)
426 GObjectClass *object_class = G_OBJECT_CLASS (klass);
427 GParamSpec *param_spec;
429 object_class->constructor = empathy_chatroom_manager_constructor;
430 object_class->get_property = empathy_chatroom_manager_get_property;
431 object_class->set_property = empathy_chatroom_manager_set_property;
432 object_class->finalize = chatroom_manager_finalize;
434 param_spec = g_param_spec_string (
435 "file",
436 "path of the favorite file",
437 "The path of the XML file containing user's favorites",
438 NULL,
439 G_PARAM_CONSTRUCT_ONLY |
440 G_PARAM_READWRITE |
441 G_PARAM_STATIC_NAME |
442 G_PARAM_STATIC_NICK |
443 G_PARAM_STATIC_BLURB);
444 g_object_class_install_property (object_class, PROP_FILE, param_spec);
446 signals[CHATROOM_ADDED] = g_signal_new ("chatroom-added",
447 G_TYPE_FROM_CLASS (klass),
448 G_SIGNAL_RUN_LAST,
449 0, NULL, NULL,
450 g_cclosure_marshal_VOID__OBJECT,
451 G_TYPE_NONE,
452 1, EMPATHY_TYPE_CHATROOM);
454 signals[CHATROOM_REMOVED] = g_signal_new ("chatroom-removed",
455 G_TYPE_FROM_CLASS (klass),
456 G_SIGNAL_RUN_LAST,
457 0, NULL, NULL,
458 g_cclosure_marshal_VOID__OBJECT,
459 G_TYPE_NONE,
460 1, EMPATHY_TYPE_CHATROOM);
462 g_type_class_add_private (object_class, sizeof (EmpathyChatroomManagerPriv));
465 static void
466 empathy_chatroom_manager_init (EmpathyChatroomManager *manager)
468 EmpathyChatroomManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
469 EMPATHY_TYPE_CHATROOM_MANAGER, EmpathyChatroomManagerPriv);
471 manager->priv = priv;
474 EmpathyChatroomManager *
475 empathy_chatroom_manager_dup_singleton (const gchar *file)
477 return EMPATHY_CHATROOM_MANAGER (g_object_new (EMPATHY_TYPE_CHATROOM_MANAGER,
478 "file", file, NULL));
481 gboolean
482 empathy_chatroom_manager_add (EmpathyChatroomManager *manager,
483 EmpathyChatroom *chatroom)
485 EmpathyChatroomManagerPriv *priv;
487 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), FALSE);
488 g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), FALSE);
490 priv = GET_PRIV (manager);
492 /* don't add more than once */
493 if (!empathy_chatroom_manager_find (manager,
494 empathy_chatroom_get_account (chatroom),
495 empathy_chatroom_get_room (chatroom)))
497 add_chatroom (manager, chatroom);
499 if (empathy_chatroom_is_favorite (chatroom))
500 reset_save_timeout (manager);
502 g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
503 return TRUE;
506 return FALSE;
509 static void
510 chatroom_manager_remove_link (EmpathyChatroomManager *manager,
511 GList *l)
513 EmpathyChatroomManagerPriv *priv;
514 EmpathyChatroom *chatroom;
516 priv = GET_PRIV (manager);
518 chatroom = l->data;
520 if (empathy_chatroom_is_favorite (chatroom))
521 reset_save_timeout (manager);
523 g_signal_emit (manager, signals[CHATROOM_REMOVED], 0, chatroom);
524 g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb, manager);
526 priv->chatrooms = g_list_delete_link (priv->chatrooms, l);
527 g_object_unref (chatroom);
530 void
531 empathy_chatroom_manager_remove (EmpathyChatroomManager *manager,
532 EmpathyChatroom *chatroom)
534 EmpathyChatroomManagerPriv *priv;
535 GList *l;
537 g_return_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager));
538 g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
540 priv = GET_PRIV (manager);
542 for (l = priv->chatrooms; l; l = l->next)
544 EmpathyChatroom *this_chatroom;
546 this_chatroom = l->data;
548 if (this_chatroom == chatroom ||
549 empathy_chatroom_equal (chatroom, this_chatroom))
551 chatroom_manager_remove_link (manager, l);
552 break;
557 EmpathyChatroom *
558 empathy_chatroom_manager_find (EmpathyChatroomManager *manager,
559 McAccount *account,
560 const gchar *room)
562 EmpathyChatroomManagerPriv *priv;
563 GList *l;
565 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
566 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
567 g_return_val_if_fail (room != NULL, NULL);
569 priv = GET_PRIV (manager);
571 for (l = priv->chatrooms; l; l = l->next) {
572 EmpathyChatroom *chatroom;
573 McAccount *this_account;
574 const gchar *this_room;
576 chatroom = l->data;
577 this_account = empathy_chatroom_get_account (chatroom);
578 this_room = empathy_chatroom_get_room (chatroom);
580 if (this_account && this_room &&
581 empathy_account_equal (account, this_account) &&
582 strcmp (this_room, room) == 0) {
583 return chatroom;
587 return NULL;
590 GList *
591 empathy_chatroom_manager_get_chatrooms (EmpathyChatroomManager *manager,
592 McAccount *account)
594 EmpathyChatroomManagerPriv *priv;
595 GList *chatrooms, *l;
597 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
599 priv = GET_PRIV (manager);
601 if (!account) {
602 return g_list_copy (priv->chatrooms);
605 chatrooms = NULL;
606 for (l = priv->chatrooms; l; l = l->next) {
607 EmpathyChatroom *chatroom;
609 chatroom = l->data;
611 if (empathy_account_equal (account,
612 empathy_chatroom_get_account (chatroom))) {
613 chatrooms = g_list_append (chatrooms, chatroom);
617 return chatrooms;
620 guint
621 empathy_chatroom_manager_get_count (EmpathyChatroomManager *manager,
622 McAccount *account)
624 EmpathyChatroomManagerPriv *priv;
625 GList *l;
626 guint count = 0;
628 g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), 0);
630 priv = GET_PRIV (manager);
632 if (!account) {
633 return g_list_length (priv->chatrooms);
636 for (l = priv->chatrooms; l; l = l->next) {
637 EmpathyChatroom *chatroom;
639 chatroom = l->data;
641 if (empathy_account_equal (account,
642 empathy_chatroom_get_account (chatroom))) {
643 count++;
647 return count;
650 static void
651 chatroom_manager_chat_destroyed_cb (EmpathyTpChat *chat,
652 gpointer manager)
654 EmpathyChatroomManagerPriv *priv = GET_PRIV (manager);
655 GList *l;
657 for (l = priv->chatrooms; l; l = l->next)
659 EmpathyChatroom *chatroom = l->data;
661 if (empathy_chatroom_get_tp_chat (chatroom) != chat)
662 continue;
664 empathy_chatroom_set_tp_chat (chatroom, NULL);
666 if (!empathy_chatroom_is_favorite (chatroom))
668 /* Remove the chatroom from the list, unless it's in the list of
669 * favourites..
670 * FIXME this policy should probably not be in libempathy */
671 chatroom_manager_remove_link (manager, l);
674 break;
678 static void
679 chatroom_manager_observe_channel_cb (EmpathyDispatcher *dispatcher,
680 EmpathyDispatchOperation *operation, gpointer manager)
682 EmpathyChatroomManagerPriv *priv = GET_PRIV (manager);
683 EmpathyChatroom *chatroom;
684 TpChannel *channel;
685 EmpathyTpChat *chat;
686 const gchar *roomname;
687 GQuark channel_type;
688 TpHandleType handle_type;
689 McAccount *account;
690 TpConnection *connection;
692 channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
694 /* Observe Text channels to rooms only */
695 if (channel_type != TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
696 return;
698 channel = empathy_dispatch_operation_get_channel (operation);
699 tp_channel_get_handle (channel, &handle_type);
701 if (handle_type != TP_HANDLE_TYPE_ROOM)
702 return;
704 chat = EMPATHY_TP_CHAT (
705 empathy_dispatch_operation_get_channel_wrapper (operation));
706 connection = empathy_tp_chat_get_connection (chat);
707 account = empathy_account_manager_get_account (priv->account_manager,
708 connection);
710 roomname = empathy_tp_chat_get_id (chat);
712 chatroom = empathy_chatroom_manager_find (manager, account, roomname);
714 if (chatroom == NULL)
716 chatroom = empathy_chatroom_new_full (account, roomname, roomname,
717 FALSE);
718 empathy_chatroom_set_tp_chat (chatroom, chat);
719 empathy_chatroom_manager_add (manager, chatroom);
720 g_object_unref (chatroom);
722 else
724 empathy_chatroom_set_tp_chat (chatroom, chat);
727 /* A TpChat is always destroyed as it only gets unreffed after the channel
728 * has been invalidated in the dispatcher.. */
729 g_signal_connect (chat, "destroy",
730 G_CALLBACK (chatroom_manager_chat_destroyed_cb),
731 manager);
734 void
735 empathy_chatroom_manager_observe (EmpathyChatroomManager *manager,
736 EmpathyDispatcher *dispatcher)
738 g_signal_connect (dispatcher, "observe",
739 G_CALLBACK (chatroom_manager_observe_channel_cb), manager);