6765d9e48301b372b58ef80a80be794c389c0f76
[empathy.git] / libempathy / empathy-chatroom-manager.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2004-2007 Imendio AB
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * Authors: Xavier Claessens <xclaesse@gmail.com>
22  *          Martyn Russell <martyn@imendio.com>
23  */
24
25 #include "config.h"
26
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30
31 #include <libxml/parser.h>
32 #include <libxml/tree.h>
33
34 #include "empathy-tp-chat.h"
35 #include "empathy-chatroom-manager.h"
36 #include "empathy-utils.h"
37
38 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
39 #include "empathy-debug.h"
40
41 #define CHATROOMS_XML_FILENAME "chatrooms.xml"
42 #define CHATROOMS_DTD_FILENAME "empathy-chatroom-manager.dtd"
43 #define SAVE_TIMER 4
44
45 static EmpathyChatroomManager *chatroom_manager_singleton = NULL;
46
47 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChatroomManager)
48 typedef struct {
49         GList      *chatrooms;
50   gchar *file;
51   /* source id of the autosave timer */
52   gint save_timer_id;
53 } EmpathyChatroomManagerPriv;
54
55 static void     chatroom_manager_finalize          (GObject                    *object);
56 static gboolean chatroom_manager_get_all           (EmpathyChatroomManager      *manager);
57 static gboolean chatroom_manager_file_parse        (EmpathyChatroomManager      *manager,
58                                                     const gchar                *filename);
59 static void     chatroom_manager_parse_chatroom    (EmpathyChatroomManager      *manager,
60                                                     xmlNodePtr                  node);
61 static gboolean chatroom_manager_file_save         (EmpathyChatroomManager      *manager);
62 static void reset_save_timeout (EmpathyChatroomManager *self);
63
64 enum {
65         CHATROOM_ADDED,
66         CHATROOM_REMOVED,
67         LAST_SIGNAL
68 };
69
70 static guint signals[LAST_SIGNAL];
71
72 /* properties */
73 enum
74 {
75   PROP_FILE = 1,
76   LAST_PROPERTY
77 };
78
79 G_DEFINE_TYPE (EmpathyChatroomManager, empathy_chatroom_manager, G_TYPE_OBJECT);
80
81 static void
82 empathy_chatroom_manager_get_property (GObject *object,
83                                        guint property_id,
84                                        GValue *value,
85                                        GParamSpec *pspec)
86 {
87   EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
88   EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
89
90   switch (property_id)
91     {
92       case PROP_FILE:
93         g_value_set_string (value, priv->file);
94         break;
95       default:
96         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
97         break;
98     }
99 }
100
101 static void
102 empathy_chatroom_manager_set_property (GObject *object,
103                                        guint property_id,
104                                        const GValue *value,
105                                        GParamSpec *pspec)
106 {
107   EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
108   EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
109
110   switch (property_id)
111     {
112       case PROP_FILE:
113         g_free (priv->file);
114         priv->file = g_value_dup_string (value);
115         break;
116       default:
117         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
118         break;
119     }
120 }
121
122 static GObject *
123 empathy_chatroom_manager_constructor (GType type,
124                                       guint n_props,
125                                       GObjectConstructParam *props)
126 {
127   GObject *obj;
128   EmpathyChatroomManager *self;
129   EmpathyChatroomManagerPriv *priv;
130
131   if (chatroom_manager_singleton != NULL)
132     return g_object_ref (chatroom_manager_singleton);
133
134   /* Parent constructor chain */
135   obj = G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->
136         constructor (type, n_props, props);
137
138   self = EMPATHY_CHATROOM_MANAGER (obj);
139   priv = GET_PRIV (self);
140
141   chatroom_manager_singleton = self;
142   g_object_add_weak_pointer (obj, (gpointer) &chatroom_manager_singleton);
143
144   if (priv->file == NULL)
145     {
146       /* Set the default file path */
147       gchar *dir;
148
149       dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
150       if (!g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
151         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
152
153       priv->file = g_build_filename (dir, CHATROOMS_XML_FILENAME, NULL);
154       g_free (dir);
155     }
156
157   chatroom_manager_get_all (self);
158   return obj;
159 }
160
161 static void
162 empathy_chatroom_manager_class_init (EmpathyChatroomManagerClass *klass)
163 {
164         GObjectClass *object_class = G_OBJECT_CLASS (klass);
165   GParamSpec *param_spec;
166
167   object_class->constructor = empathy_chatroom_manager_constructor;
168   object_class->get_property = empathy_chatroom_manager_get_property;
169   object_class->set_property = empathy_chatroom_manager_set_property;
170         object_class->finalize = chatroom_manager_finalize;
171
172   param_spec = g_param_spec_string (
173       "file",
174       "path of the favorite file",
175       "The path of the XML file containing user's favorites",
176       NULL,
177       G_PARAM_CONSTRUCT_ONLY |
178       G_PARAM_READWRITE |
179       G_PARAM_STATIC_NAME |
180       G_PARAM_STATIC_NICK |
181       G_PARAM_STATIC_BLURB);
182   g_object_class_install_property (object_class, PROP_FILE, param_spec);
183
184         signals[CHATROOM_ADDED] =
185                 g_signal_new ("chatroom-added",
186                               G_TYPE_FROM_CLASS (klass),
187                               G_SIGNAL_RUN_LAST,
188                               0,
189                               NULL, NULL,
190                               g_cclosure_marshal_VOID__OBJECT,
191                               G_TYPE_NONE,
192                               1, EMPATHY_TYPE_CHATROOM);
193         signals[CHATROOM_REMOVED] =
194                 g_signal_new ("chatroom-removed",
195                               G_TYPE_FROM_CLASS (klass),
196                               G_SIGNAL_RUN_LAST,
197                               0,
198                               NULL, NULL,
199                               g_cclosure_marshal_VOID__OBJECT,
200                               G_TYPE_NONE,
201                               1, EMPATHY_TYPE_CHATROOM);
202
203         g_type_class_add_private (object_class,
204                                   sizeof (EmpathyChatroomManagerPriv));
205 }
206
207 static void
208 empathy_chatroom_manager_init (EmpathyChatroomManager *manager)
209 {
210         EmpathyChatroomManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
211                 EMPATHY_TYPE_CHATROOM_MANAGER, EmpathyChatroomManagerPriv);
212
213         manager->priv = priv;
214 }
215
216 static void
217 chatroom_changed_cb (EmpathyChatroom *chatroom,
218                      GParamSpec *spec,
219                      EmpathyChatroomManager *self)
220 {
221   reset_save_timeout (self);
222 }
223
224 static void
225 chatroom_manager_finalize (GObject *object)
226 {
227   EmpathyChatroomManager *self = EMPATHY_CHATROOM_MANAGER (object);
228         EmpathyChatroomManagerPriv *priv;
229   GList *l;
230
231         priv = GET_PRIV (object);
232
233   if (priv->save_timer_id > 0)
234     {
235       /* have to save before destroy the object */
236       g_source_remove (priv->save_timer_id);
237       priv->save_timer_id = 0;
238       chatroom_manager_file_save (self);
239     }
240
241   for (l = priv->chatrooms; l != NULL; l = g_list_next (l))
242     {
243       EmpathyChatroom *chatroom = l->data;
244
245       g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb,
246           self);
247
248       g_object_unref (chatroom);
249     }
250
251         g_list_free (priv->chatrooms);
252   g_free (priv->file);
253
254         (G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->finalize) (object);
255 }
256
257 EmpathyChatroomManager *
258 empathy_chatroom_manager_dup_singleton (const gchar *file)
259 {
260         return EMPATHY_CHATROOM_MANAGER (g_object_new (EMPATHY_TYPE_CHATROOM_MANAGER,
261                 "file", file, NULL));
262 }
263
264 static gboolean
265 save_timeout (EmpathyChatroomManager *self)
266 {
267   EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
268
269   priv->save_timer_id = 0;
270   chatroom_manager_file_save (self);
271
272   return FALSE;
273 }
274
275 static void
276 reset_save_timeout (EmpathyChatroomManager *self)
277 {
278   EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
279
280   if (priv->save_timer_id > 0)
281     {
282       g_source_remove (priv->save_timer_id);
283     }
284
285   priv->save_timer_id = g_timeout_add_seconds (SAVE_TIMER,
286       (GSourceFunc) save_timeout, self);
287 }
288
289 static void
290 add_chatroom (EmpathyChatroomManager *self,
291               EmpathyChatroom *chatroom)
292 {
293   EmpathyChatroomManagerPriv *priv = GET_PRIV (self);
294
295   priv->chatrooms = g_list_prepend (priv->chatrooms, g_object_ref (chatroom));
296
297   g_signal_connect (chatroom, "notify",
298       G_CALLBACK (chatroom_changed_cb), self);
299 }
300
301 gboolean
302 empathy_chatroom_manager_add (EmpathyChatroomManager *manager,
303                              EmpathyChatroom        *chatroom)
304 {
305         EmpathyChatroomManagerPriv *priv;
306
307         g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), FALSE);
308         g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), FALSE);
309
310         priv = GET_PRIV (manager);
311
312         /* don't add more than once */
313         if (!empathy_chatroom_manager_find (manager,
314                                            empathy_chatroom_get_account (chatroom),
315                                            empathy_chatroom_get_room (chatroom))) {
316       gboolean favorite;
317
318       g_object_get (chatroom, "favorite", &favorite, NULL);
319
320     add_chatroom (manager, chatroom);
321
322     if (favorite)
323       {
324         reset_save_timeout (manager);
325       }
326
327                 g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
328
329                 return TRUE;
330         }
331
332         return FALSE;
333 }
334
335 void
336 empathy_chatroom_manager_remove (EmpathyChatroomManager *manager,
337                                 EmpathyChatroom        *chatroom)
338 {
339         EmpathyChatroomManagerPriv *priv;
340         GList                     *l;
341
342         g_return_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager));
343         g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
344
345         priv = GET_PRIV (manager);
346
347         for (l = priv->chatrooms; l; l = l->next) {
348                 EmpathyChatroom *this_chatroom;
349
350                 this_chatroom = l->data;
351
352                 if (this_chatroom == chatroom ||
353         empathy_chatroom_equal (chatroom, this_chatroom)) {
354         gboolean favorite;
355                         priv->chatrooms = g_list_delete_link (priv->chatrooms, l);
356
357       g_object_get (chatroom, "favorite", &favorite, NULL);
358
359       if (favorite)
360         {
361           reset_save_timeout (manager);
362         }
363
364                         g_signal_emit (manager, signals[CHATROOM_REMOVED], 0, this_chatroom);
365
366       g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb,
367           manager);
368
369                         g_object_unref (this_chatroom);
370                         break;
371                 }
372         }
373 }
374
375 EmpathyChatroom *
376 empathy_chatroom_manager_find (EmpathyChatroomManager *manager,
377                               McAccount             *account,
378                               const gchar           *room)
379 {
380         EmpathyChatroomManagerPriv *priv;
381         GList                     *l;
382
383         g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
384         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
385         g_return_val_if_fail (room != NULL, NULL);
386
387         priv = GET_PRIV (manager);
388
389         for (l = priv->chatrooms; l; l = l->next) {
390                 EmpathyChatroom *chatroom;
391                 McAccount      *this_account;
392                 const gchar    *this_room;
393
394                 chatroom = l->data;
395                 this_account = empathy_chatroom_get_account (chatroom);
396                 this_room = empathy_chatroom_get_room (chatroom);
397
398                 if (this_account && this_room &&
399                     empathy_account_equal (account, this_account) &&
400                     strcmp (this_room, room) == 0) {
401                         return chatroom;
402                 }
403         }
404
405         return NULL;
406 }
407
408 GList *
409 empathy_chatroom_manager_get_chatrooms (EmpathyChatroomManager *manager,
410                                        McAccount             *account)
411 {
412         EmpathyChatroomManagerPriv *priv;
413         GList                     *chatrooms, *l;
414
415         g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
416
417         priv = GET_PRIV (manager);
418
419         if (!account) {
420                 return g_list_copy (priv->chatrooms);
421         }
422
423         chatrooms = NULL;
424         for (l = priv->chatrooms; l; l = l->next) {
425                 EmpathyChatroom *chatroom;
426
427                 chatroom = l->data;
428
429                 if (empathy_account_equal (account,
430                                           empathy_chatroom_get_account (chatroom))) {
431                         chatrooms = g_list_append (chatrooms, chatroom);
432                 }
433         }
434
435         return chatrooms;
436 }
437
438 guint
439 empathy_chatroom_manager_get_count (EmpathyChatroomManager *manager,
440                                    McAccount             *account)
441 {
442         EmpathyChatroomManagerPriv *priv;
443         GList                     *l;
444         guint                      count = 0;
445
446         g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), 0);
447
448         priv = GET_PRIV (manager);
449
450         if (!account) {
451                 return g_list_length (priv->chatrooms);
452         }
453
454         for (l = priv->chatrooms; l; l = l->next) {
455                 EmpathyChatroom *chatroom;
456
457                 chatroom = l->data;
458
459                 if (empathy_account_equal (account,
460                                           empathy_chatroom_get_account (chatroom))) {
461                         count++;
462                 }
463         }
464
465         return count;
466 }
467
468 /*
469  * API to save/load and parse the chatrooms file.
470  */
471
472 static gboolean
473 chatroom_manager_get_all (EmpathyChatroomManager *manager)
474 {
475         EmpathyChatroomManagerPriv *priv;
476
477         priv = GET_PRIV (manager);
478
479         /* read file in */
480         if (g_file_test (priv->file, G_FILE_TEST_EXISTS) &&
481             !chatroom_manager_file_parse (manager, priv->file))
482     return FALSE;
483
484         return TRUE;
485 }
486
487 static gboolean
488 chatroom_manager_file_parse (EmpathyChatroomManager *manager,
489                              const gchar           *filename)
490 {
491         EmpathyChatroomManagerPriv *priv;
492         xmlParserCtxtPtr           ctxt;
493         xmlDocPtr                  doc;
494         xmlNodePtr                 chatrooms;
495         xmlNodePtr                 node;
496
497         priv = GET_PRIV (manager);
498
499         DEBUG ("Attempting to parse file:'%s'...", filename);
500
501         ctxt = xmlNewParserCtxt ();
502
503         /* Parse and validate the file. */
504         doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
505         if (!doc) {
506                 g_warning ("Failed to parse file:'%s'", filename);
507                 xmlFreeParserCtxt (ctxt);
508                 return FALSE;
509         }
510
511         if (!empathy_xml_validate (doc, CHATROOMS_DTD_FILENAME)) {
512                 g_warning ("Failed to validate file:'%s'", filename);
513                 xmlFreeDoc(doc);
514                 xmlFreeParserCtxt (ctxt);
515                 return FALSE;
516         }
517
518         /* The root node, chatrooms. */
519         chatrooms = xmlDocGetRootElement (doc);
520
521         for (node = chatrooms->children; node; node = node->next) {
522                 if (strcmp ((gchar *) node->name, "chatroom") == 0) {
523                         chatroom_manager_parse_chatroom (manager, node);
524                 }
525         }
526
527         DEBUG ("Parsed %d chatrooms", g_list_length (priv->chatrooms));
528
529         xmlFreeDoc(doc);
530         xmlFreeParserCtxt (ctxt);
531
532         return TRUE;
533 }
534
535 static void
536 chatroom_manager_parse_chatroom (EmpathyChatroomManager *manager,
537                                  xmlNodePtr             node)
538 {
539         EmpathyChatroomManagerPriv *priv;
540         EmpathyChatroom            *chatroom;
541         McAccount                 *account;
542         xmlNodePtr                 child;
543         gchar                     *str;
544         gchar                     *name;
545         gchar                     *room;
546         gchar                     *account_id;
547         gboolean                   auto_connect;
548
549         priv = GET_PRIV (manager);
550
551         /* default values. */
552         name = NULL;
553         room = NULL;
554         auto_connect = TRUE;
555         account_id = NULL;
556
557         for (child = node->children; child; child = child->next) {
558                 gchar *tag;
559
560                 if (xmlNodeIsText (child)) {
561                         continue;
562                 }
563
564                 tag = (gchar *) child->name;
565                 str = (gchar *) xmlNodeGetContent (child);
566
567                 if (strcmp (tag, "name") == 0) {
568                         name = g_strdup (str);
569                 }
570                 else if (strcmp (tag, "room") == 0) {
571                         room = g_strdup (str);
572                 }
573                 else if (strcmp (tag, "auto_connect") == 0) {
574                         if (strcmp (str, "yes") == 0) {
575                                 auto_connect = TRUE;
576                         } else {
577                                 auto_connect = FALSE;
578                         }
579                 }
580                 else if (strcmp (tag, "account") == 0) {
581                         account_id = g_strdup (str);
582                 }
583
584                 xmlFree (str);
585         }
586
587         account = mc_account_lookup (account_id);
588         if (!account) {
589                 g_free (name);
590                 g_free (room);
591                 g_free (account_id);
592                 return;
593         }
594
595         chatroom = empathy_chatroom_new_full (account, room, name, auto_connect);
596   g_object_set (chatroom, "favorite", TRUE, NULL);
597   add_chatroom (manager, chatroom);
598         g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
599
600         g_object_unref (account);
601         g_free (name);
602         g_free (room);
603         g_free (account_id);
604 }
605
606 static gboolean
607 chatroom_manager_file_save (EmpathyChatroomManager *manager)
608 {
609         EmpathyChatroomManagerPriv *priv;
610         xmlDocPtr                  doc;
611         xmlNodePtr                 root;
612         GList                     *l;
613
614         priv = GET_PRIV (manager);
615
616         doc = xmlNewDoc ("1.0");
617         root = xmlNewNode (NULL, "chatrooms");
618         xmlDocSetRootElement (doc, root);
619
620         for (l = priv->chatrooms; l; l = l->next) {
621                 EmpathyChatroom *chatroom;
622                 xmlNodePtr      node;
623                 const gchar    *account_id;
624     gboolean favorite;
625
626                 chatroom = l->data;
627
628     g_object_get (chatroom, "favorite", &favorite, NULL);
629     if (!favorite)
630       continue;
631
632                 account_id = mc_account_get_unique_name (empathy_chatroom_get_account (chatroom));
633
634                 node = xmlNewChild (root, NULL, "chatroom", NULL);
635                 xmlNewTextChild (node, NULL, "name", empathy_chatroom_get_name (chatroom));
636                 xmlNewTextChild (node, NULL, "room", empathy_chatroom_get_room (chatroom));
637                 xmlNewTextChild (node, NULL, "account", account_id);
638                 xmlNewTextChild (node, NULL, "auto_connect", empathy_chatroom_get_auto_connect (chatroom) ? "yes" : "no");
639         }
640
641         /* Make sure the XML is indented properly */
642         xmlIndentTreeOutput = 1;
643
644         DEBUG ("Saving file:'%s'", priv->file);
645         xmlSaveFormatFileEnc (priv->file, doc, "utf-8", 1);
646         xmlFreeDoc (doc);
647
648         xmlCleanupParser ();
649         xmlMemoryDump ();
650
651         return TRUE;
652 }
653
654 static void
655 chatroom_manager_chat_destroyed_cb (EmpathyTpChat *chat,
656   gpointer user_data)
657 {
658   EmpathyChatroomManager *manager = EMPATHY_CHATROOM_MANAGER (user_data);
659   McAccount *account = empathy_tp_chat_get_account (chat);
660   EmpathyChatroom *chatroom;
661   const gchar *roomname;
662   gboolean favorite;
663
664   roomname = empathy_tp_chat_get_id (chat);
665   chatroom = empathy_chatroom_manager_find (manager, account, roomname);
666
667   if (chatroom == NULL)
668     return;
669
670   g_object_set (chatroom, "tp-chat", NULL, NULL);
671   g_object_get (chatroom, "favorite", &favorite, NULL);
672
673   if (!favorite)
674     {
675       /* Remove the chatroom from the list, unless it's in the list of
676        * favourites..
677        * FIXME this policy should probably not be in libempathy */
678       empathy_chatroom_manager_remove (manager, chatroom);
679     }
680 }
681
682 static void
683 chatroom_manager_observe_channel_cb (EmpathyDispatcher *dispatcher,
684   EmpathyDispatchOperation *operation, gpointer user_data)
685 {
686   EmpathyChatroomManager *manager = EMPATHY_CHATROOM_MANAGER (user_data);
687   EmpathyChatroom *chatroom;
688   TpChannel *channel;
689   EmpathyTpChat *chat;
690   const gchar *roomname;
691   GQuark channel_type;
692   TpHandleType handle_type;
693   McAccount *account;
694
695   channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
696
697   /* Observe Text channels to rooms only */
698   if (channel_type != TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
699     return;
700
701   channel = empathy_dispatch_operation_get_channel (operation);
702   tp_channel_get_handle (channel, &handle_type);
703
704   if (handle_type != TP_HANDLE_TYPE_ROOM)
705     return;
706
707   chat = EMPATHY_TP_CHAT (
708     empathy_dispatch_operation_get_channel_wrapper (operation));
709   account = empathy_tp_chat_get_account (chat);
710
711   roomname = empathy_tp_chat_get_id (chat);
712
713   chatroom = empathy_chatroom_manager_find (manager, account, roomname);
714
715   if (chatroom == NULL)
716     {
717       chatroom = empathy_chatroom_new_full (account, roomname, roomname,
718         FALSE);
719       g_object_set (G_OBJECT (chatroom), "tp-chat", chat, NULL);
720       empathy_chatroom_manager_add (manager, chatroom);
721       g_object_unref (chatroom);
722     }
723   else
724     {
725       g_object_set (G_OBJECT (chatroom), "tp-chat", chat, NULL);
726     }
727
728   /* A TpChat is always destroyed as it only gets unreffed after the channel
729    * has been invalidated in the dispatcher..  */
730   g_signal_connect (chat, "destroy",
731     G_CALLBACK (chatroom_manager_chat_destroyed_cb),
732     manager);
733 }
734
735 void
736 empathy_chatroom_manager_observe (EmpathyChatroomManager *manager,
737   EmpathyDispatcher *dispatcher)
738 {
739   g_signal_connect (dispatcher, "observe",
740     G_CALLBACK (chatroom_manager_observe_channel_cb), manager);
741 }