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