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