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