]> git.0d.be Git - empathy.git/blob - libempathy/empathy-chatroom-manager.c
contact-manager, dispatcher: renamed _new_connection_cb to _status_changed_cb
[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
406   if (!tp_account_manager_prepare_finish (manager, result, NULL))
407     return;
408
409   chatroom_manager_get_all (self);
410 }
411
412 static GObject *
413 empathy_chatroom_manager_constructor (GType type,
414                                       guint n_props,
415                                       GObjectConstructParam *props)
416 {
417   GObject *obj;
418   EmpathyChatroomManager *self;
419   EmpathyChatroomManagerPriv *priv;
420
421   if (chatroom_manager_singleton != NULL)
422     return g_object_ref (chatroom_manager_singleton);
423
424   /* Parent constructor chain */
425   obj = G_OBJECT_CLASS (empathy_chatroom_manager_parent_class)->
426         constructor (type, n_props, props);
427
428   self = EMPATHY_CHATROOM_MANAGER (obj);
429   priv = GET_PRIV (self);
430
431   priv->ready = FALSE;
432
433   chatroom_manager_singleton = self;
434   g_object_add_weak_pointer (obj, (gpointer) &chatroom_manager_singleton);
435
436   priv->account_manager = tp_account_manager_dup ();
437
438   tp_account_manager_prepare_async (priv->account_manager, NULL,
439       account_manager_ready_cb, self);
440
441   if (priv->file == NULL)
442     {
443       /* Set the default file path */
444       gchar *dir;
445
446       dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
447       if (!g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
448         g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
449
450       priv->file = g_build_filename (dir, CHATROOMS_XML_FILENAME, NULL);
451       g_free (dir);
452     }
453
454   return obj;
455 }
456
457 static void
458 empathy_chatroom_manager_class_init (EmpathyChatroomManagerClass *klass)
459 {
460   GObjectClass *object_class = G_OBJECT_CLASS (klass);
461   GParamSpec *param_spec;
462
463   object_class->constructor = empathy_chatroom_manager_constructor;
464   object_class->get_property = empathy_chatroom_manager_get_property;
465   object_class->set_property = empathy_chatroom_manager_set_property;
466         object_class->finalize = chatroom_manager_finalize;
467
468   param_spec = g_param_spec_string (
469       "file",
470       "path of the favorite file",
471       "The path of the XML file containing user's favorites",
472       NULL,
473       G_PARAM_CONSTRUCT_ONLY |
474       G_PARAM_READWRITE |
475       G_PARAM_STATIC_NAME |
476       G_PARAM_STATIC_NICK |
477       G_PARAM_STATIC_BLURB);
478   g_object_class_install_property (object_class, PROP_FILE, param_spec);
479
480   param_spec = g_param_spec_boolean (
481       "ready",
482       "whether the manager is ready yet",
483       "whether the manager is ready yet",
484       FALSE,
485       G_PARAM_READABLE);
486   g_object_class_install_property (object_class, PROP_READY, param_spec);
487
488   signals[CHATROOM_ADDED] = g_signal_new ("chatroom-added",
489       G_TYPE_FROM_CLASS (klass),
490       G_SIGNAL_RUN_LAST,
491       0, NULL, NULL,
492       g_cclosure_marshal_VOID__OBJECT,
493       G_TYPE_NONE,
494       1, EMPATHY_TYPE_CHATROOM);
495
496   signals[CHATROOM_REMOVED] = g_signal_new ("chatroom-removed",
497       G_TYPE_FROM_CLASS (klass),
498       G_SIGNAL_RUN_LAST,
499       0, NULL, NULL,
500       g_cclosure_marshal_VOID__OBJECT,
501       G_TYPE_NONE,
502       1, EMPATHY_TYPE_CHATROOM);
503
504   g_type_class_add_private (object_class, sizeof (EmpathyChatroomManagerPriv));
505 }
506
507 static void
508 empathy_chatroom_manager_init (EmpathyChatroomManager *manager)
509 {
510   EmpathyChatroomManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
511       EMPATHY_TYPE_CHATROOM_MANAGER, EmpathyChatroomManagerPriv);
512
513   manager->priv = priv;
514 }
515
516 EmpathyChatroomManager *
517 empathy_chatroom_manager_dup_singleton (const gchar *file)
518 {
519   return EMPATHY_CHATROOM_MANAGER (g_object_new (EMPATHY_TYPE_CHATROOM_MANAGER,
520       "file", file, NULL));
521 }
522
523 gboolean
524 empathy_chatroom_manager_add (EmpathyChatroomManager *manager,
525                              EmpathyChatroom        *chatroom)
526 {
527   EmpathyChatroomManagerPriv *priv;
528
529   g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), FALSE);
530   g_return_val_if_fail (EMPATHY_IS_CHATROOM (chatroom), FALSE);
531
532   priv = GET_PRIV (manager);
533
534   /* don't add more than once */
535   if (!empathy_chatroom_manager_find (manager,
536       empathy_chatroom_get_account (chatroom),
537       empathy_chatroom_get_room (chatroom)))
538     {
539       add_chatroom (manager, chatroom);
540
541       if (empathy_chatroom_is_favorite (chatroom))
542         reset_save_timeout (manager);
543
544       g_signal_emit (manager, signals[CHATROOM_ADDED], 0, chatroom);
545       return TRUE;
546     }
547
548   return FALSE;
549 }
550
551 static void
552 chatroom_manager_remove_link (EmpathyChatroomManager *manager,
553                               GList *l)
554 {
555   EmpathyChatroomManagerPriv *priv;
556   EmpathyChatroom *chatroom;
557
558   priv = GET_PRIV (manager);
559
560   chatroom = l->data;
561
562   if (empathy_chatroom_is_favorite (chatroom))
563     reset_save_timeout (manager);
564
565   priv->chatrooms = g_list_delete_link (priv->chatrooms, l);
566
567   g_signal_emit (manager, signals[CHATROOM_REMOVED], 0, chatroom);
568   g_signal_handlers_disconnect_by_func (chatroom, chatroom_changed_cb, manager);
569
570   g_object_unref (chatroom);
571 }
572
573 void
574 empathy_chatroom_manager_remove (EmpathyChatroomManager *manager,
575                                  EmpathyChatroom        *chatroom)
576 {
577   EmpathyChatroomManagerPriv *priv;
578   GList *l;
579
580   g_return_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager));
581   g_return_if_fail (EMPATHY_IS_CHATROOM (chatroom));
582
583   priv = GET_PRIV (manager);
584
585   for (l = priv->chatrooms; l; l = l->next)
586     {
587       EmpathyChatroom *this_chatroom;
588
589       this_chatroom = l->data;
590
591       if (this_chatroom == chatroom ||
592           empathy_chatroom_equal (chatroom, this_chatroom))
593         {
594           chatroom_manager_remove_link (manager, l);
595           break;
596         }
597     }
598 }
599
600 EmpathyChatroom *
601 empathy_chatroom_manager_find (EmpathyChatroomManager *manager,
602                                TpAccount *account,
603                                const gchar *room)
604 {
605         EmpathyChatroomManagerPriv *priv;
606         GList                     *l;
607
608         g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
609         g_return_val_if_fail (room != NULL, NULL);
610
611         priv = GET_PRIV (manager);
612
613         for (l = priv->chatrooms; l; l = l->next) {
614                 EmpathyChatroom *chatroom;
615                 TpAccount *this_account;
616                 const gchar    *this_room;
617
618                 chatroom = l->data;
619                 this_account = empathy_chatroom_get_account (chatroom);
620                 this_room = empathy_chatroom_get_room (chatroom);
621
622                 if (this_account && this_room && account == this_account
623                                 && strcmp (this_room, room) == 0) {
624                         return chatroom;
625                 }
626         }
627
628         return NULL;
629 }
630
631 GList *
632 empathy_chatroom_manager_get_chatrooms (EmpathyChatroomManager *manager,
633                                        TpAccount *account)
634 {
635         EmpathyChatroomManagerPriv *priv;
636         GList                     *chatrooms, *l;
637
638         g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), NULL);
639
640         priv = GET_PRIV (manager);
641
642         if (!account) {
643                 return g_list_copy (priv->chatrooms);
644         }
645
646         chatrooms = NULL;
647         for (l = priv->chatrooms; l; l = l->next) {
648                 EmpathyChatroom *chatroom;
649
650                 chatroom = l->data;
651
652                 if (account == empathy_chatroom_get_account (chatroom)) {
653                         chatrooms = g_list_append (chatrooms, chatroom);
654                 }
655         }
656
657         return chatrooms;
658 }
659
660 guint
661 empathy_chatroom_manager_get_count (EmpathyChatroomManager *manager,
662                                    TpAccount *account)
663 {
664         EmpathyChatroomManagerPriv *priv;
665         GList                     *l;
666         guint                      count = 0;
667
668         g_return_val_if_fail (EMPATHY_IS_CHATROOM_MANAGER (manager), 0);
669
670         priv = GET_PRIV (manager);
671
672         if (!account) {
673                 return g_list_length (priv->chatrooms);
674         }
675
676         for (l = priv->chatrooms; l; l = l->next) {
677                 EmpathyChatroom *chatroom;
678
679                 chatroom = l->data;
680
681                 if (account == empathy_chatroom_get_account (chatroom)) {
682                         count++;
683                 }
684         }
685
686         return count;
687 }
688
689 static void
690 chatroom_manager_chat_destroyed_cb (EmpathyTpChat *chat,
691   gpointer manager)
692 {
693   EmpathyChatroomManagerPriv *priv = GET_PRIV (manager);
694   GList *l;
695
696   for (l = priv->chatrooms; l; l = l->next)
697     {
698       EmpathyChatroom *chatroom = l->data;
699
700       if (empathy_chatroom_get_tp_chat (chatroom) != chat)
701         continue;
702
703       empathy_chatroom_set_tp_chat (chatroom, NULL);
704
705       if (!empathy_chatroom_is_favorite (chatroom))
706         {
707           /* Remove the chatroom from the list, unless it's in the list of
708            * favourites..
709            * FIXME this policy should probably not be in libempathy */
710           chatroom_manager_remove_link (manager, l);
711         }
712
713       break;
714     }
715 }
716
717 static void
718 chatroom_manager_observe_channel_cb (EmpathyDispatcher *dispatcher,
719   EmpathyDispatchOperation *operation, gpointer manager)
720 {
721   EmpathyChatroom *chatroom;
722   TpChannel *channel;
723   EmpathyTpChat *chat;
724   const gchar *roomname;
725   GQuark channel_type;
726   TpHandleType handle_type;
727   TpAccount *account;
728   TpConnection *connection;
729
730   channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
731
732   /* Observe Text channels to rooms only */
733   if (channel_type != TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
734     return;
735
736   channel = empathy_dispatch_operation_get_channel (operation);
737   tp_channel_get_handle (channel, &handle_type);
738
739   if (handle_type != TP_HANDLE_TYPE_ROOM)
740     return;
741
742   chat = EMPATHY_TP_CHAT (
743     empathy_dispatch_operation_get_channel_wrapper (operation));
744   connection = empathy_tp_chat_get_connection (chat);
745   account = empathy_get_account_for_connection (connection);
746
747   roomname = empathy_tp_chat_get_id (chat);
748
749   chatroom = empathy_chatroom_manager_find (manager, account, roomname);
750
751   if (chatroom == NULL)
752     {
753       chatroom = empathy_chatroom_new_full (account, roomname, roomname,
754         FALSE);
755       empathy_chatroom_set_tp_chat (chatroom, chat);
756       empathy_chatroom_manager_add (manager, chatroom);
757       g_object_unref (chatroom);
758     }
759   else
760     {
761         empathy_chatroom_set_tp_chat (chatroom, chat);
762     }
763
764   /* A TpChat is always destroyed as it only gets unreffed after the channel
765    * has been invalidated in the dispatcher..  */
766   g_signal_connect (chat, "destroy",
767     G_CALLBACK (chatroom_manager_chat_destroyed_cb),
768     manager);
769 }
770
771 void
772 empathy_chatroom_manager_observe (EmpathyChatroomManager *manager,
773   EmpathyDispatcher *dispatcher)
774 {
775   g_signal_connect (dispatcher, "observe",
776     G_CALLBACK (chatroom_manager_observe_channel_cb), manager);
777 }