]> git.0d.be Git - empathy.git/blob - libempathy/empathy-dispatcher.c
dispatcher_connection_new_requested_channel: fix a use-after-free crash
[empathy.git] / libempathy / empathy-dispatcher.c
1 /* * Copyright (C) 2007-2009 Collabora Ltd.
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16  *
17  * Authors: Xavier Claessens <xclaesse@gmail.com>
18  *          Sjoerd Simons <sjoerd.simons@collabora.co.uk>
19  *          Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
20  */
21
22 #define DISPATCHER_BUS_NAME TP_CLIENT_BUS_NAME_BASE "Empathy"
23 #define DISPATCHER_OBJECT_PATH TP_CLIENT_OBJECT_PATH_BASE "Empathy"
24
25 #include <config.h>
26
27 #include <string.h>
28
29 #include <glib/gi18n-lib.h>
30
31 #include <telepathy-glib/account-manager.h>
32 #include <telepathy-glib/enums.h>
33 #include <telepathy-glib/connection.h>
34 #include <telepathy-glib/util.h>
35 #include <telepathy-glib/dbus.h>
36 #include <telepathy-glib/proxy-subclass.h>
37 #include <telepathy-glib/gtypes.h>
38 #include <telepathy-glib/defs.h>
39 #include <telepathy-glib/svc-client.h>
40 #include <telepathy-glib/svc-generic.h>
41 #include <telepathy-glib/interfaces.h>
42
43 #include <extensions/extensions.h>
44
45 #include "empathy-dispatcher.h"
46 #include "empathy-handler.h"
47 #include "empathy-utils.h"
48 #include "empathy-tp-contact-factory.h"
49 #include "empathy-chatroom-manager.h"
50 #include "empathy-utils.h"
51
52 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
53 #include <libempathy/empathy-debug.h>
54
55 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
56 typedef struct
57 {
58   gboolean dispose_has_run;
59
60   TpAccountManager *account_manager;
61   /* connection to connection data mapping */
62   GHashTable *connections;
63   GHashTable *outstanding_classes_requests;
64   gpointer token;
65
66   /* channels which the dispatcher is listening "invalidated" */
67   GList *channels;
68   GPtrArray *array;
69
70   /* main handler */
71   EmpathyHandler *handler;
72
73   /* extra handlers */
74   GList *handlers;
75
76   GHashTable *request_channel_class_async_ids;
77   /* (TpAccount *) => gulong
78    * Signal handler ID of the "status-changed" signal */
79   GHashTable *status_changed_handlers;
80 } EmpathyDispatcherPriv;
81
82 static GList *
83 empathy_dispatcher_get_channels (EmpathyHandler *handler,
84     gpointer user_data);
85
86 static gboolean
87 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
88     const gchar *account_path,
89     const gchar *connection_path,
90     const GPtrArray *channels,
91     const GPtrArray *requests_satisfied,
92     guint64 timestamp,
93     GHashTable *handler_info,
94     gpointer user_data,
95     GError **error);
96
97 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
98
99 enum
100 {
101   PROP_INTERFACES = 1,
102   PROP_HANDLER,
103 };
104
105 enum
106 {
107   OBSERVE,
108   APPROVE,
109   DISPATCH,
110   LAST_SIGNAL
111 };
112
113 static guint signals[LAST_SIGNAL];
114 static EmpathyDispatcher *dispatcher = NULL;
115
116 static void dispatcher_init_connection_if_needed (
117     EmpathyDispatcher *dispatcher,
118     TpConnection *connection);
119
120 static GList * empathy_dispatcher_find_channel_classes
121   (EmpathyDispatcher *dispatcher, TpConnection *connection,
122    const gchar *channel_type, guint handle_type, GArray *fixed_properties);
123
124
125 typedef struct
126 {
127   EmpathyDispatcher *dispatcher;
128   EmpathyDispatchOperation *operation;
129   TpConnection *connection;
130   gboolean should_ensure;
131   gchar *channel_type;
132   guint handle_type;
133   guint handle;
134   EmpathyContact *contact;
135   TpProxyPendingCall *pending_call;
136
137   /* Properties to pass to the channel when requesting it */
138   GHashTable *request;
139   EmpathyDispatcherRequestCb *cb;
140   gpointer user_data;
141   gpointer *request_data;
142 } DispatcherRequestData;
143
144 typedef struct
145 {
146   TpChannel *channel;
147   /* Channel type specific wrapper object */
148   GObject *channel_wrapper;
149 } DispatchData;
150
151 typedef struct
152 {
153   /* ObjectPath => DispatchData.. */
154   GHashTable *dispatched_channels;
155   /* ObjectPath -> EmpathyDispatchOperations */
156   GHashTable *dispatching_channels;
157
158   /* ObjectPath -> EmpathyDispatchOperations
159    *
160    * This holds channels which were announced with NewChannel while we have an
161    * outstanding channel request for a channel of this type. On the Requests
162    * interface, CreateChannel and EnsureChannel are guaranteed by the spec to
163    * return before NewChannels is emitted, but there was no guarantee of the
164    * ordering of RequestChannel vs. NewChannel. So if necessary, channels are
165    * held in limbo here until we know whether they were requested.
166    */
167   GHashTable *outstanding_channels;
168   /* List of DispatcherRequestData */
169   GList *outstanding_requests;
170   /* List of requestable channel classes */
171   GPtrArray *requestable_channels;
172 } ConnectionData;
173
174 typedef struct
175 {
176   EmpathyDispatcher *dispatcher;
177   TpConnection *connection;
178   char *channel_type;
179   guint handle_type;
180   GArray *properties;
181   EmpathyDispatcherFindChannelClassCb *callback;
182   gpointer user_data;
183 } FindChannelRequest;
184
185 static void
186 empathy_dispatcher_call_create_or_ensure_channel (
187     EmpathyDispatcher *dispatcher,
188     DispatcherRequestData *request_data);
189
190 static DispatchData *
191 new_dispatch_data (TpChannel *channel,
192                    GObject *channel_wrapper)
193 {
194   DispatchData *d = g_slice_new0 (DispatchData);
195   d->channel = g_object_ref (channel);
196   if (channel_wrapper != NULL)
197     d->channel_wrapper = g_object_ref (channel_wrapper);
198
199   return d;
200 }
201
202 static void
203 free_dispatch_data (DispatchData *data)
204 {
205   g_object_unref (data->channel);
206   if (data->channel_wrapper != NULL)
207     g_object_unref (data->channel_wrapper);
208
209   g_slice_free (DispatchData, data);
210 }
211
212 static DispatcherRequestData *
213 new_dispatcher_request_data (EmpathyDispatcher *self,
214                              TpConnection *connection,
215                              const gchar *channel_type,
216                              guint handle_type,
217                              guint handle,
218                              GHashTable *request,
219                              EmpathyContact *contact,
220                              EmpathyDispatcherRequestCb *cb,
221                              gpointer user_data)
222 {
223   DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
224
225   result->dispatcher = g_object_ref (self);
226   result->connection = connection;
227
228   result->should_ensure = FALSE;
229
230   result->channel_type = g_strdup (channel_type);
231   result->handle_type = handle_type;
232   result->handle = handle;
233   result->request = request;
234
235   if (contact != NULL)
236     result->contact = g_object_ref (contact);
237
238   result->cb = cb;
239   result->user_data = user_data;
240
241   return result;
242 }
243
244 static void
245 free_dispatcher_request_data (DispatcherRequestData *r)
246 {
247   g_free (r->channel_type);
248
249   if (r->dispatcher != NULL)
250     g_object_unref (r->dispatcher);
251
252   if (r->contact != NULL)
253     g_object_unref (r->contact);
254
255   if (r->request != NULL)
256     g_hash_table_unref (r->request);
257
258   if (r->pending_call != NULL)
259     tp_proxy_pending_call_cancel (r->pending_call);
260
261   g_slice_free (DispatcherRequestData, r);
262 }
263
264 static ConnectionData *
265 new_connection_data (void)
266 {
267   ConnectionData *cd = g_slice_new0 (ConnectionData);
268
269   cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
270       g_free, (GDestroyNotify) free_dispatch_data);
271
272   cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
273       g_free, g_object_unref);
274
275   cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
276       g_free, NULL);
277
278   return cd;
279 }
280
281 static void
282 free_connection_data (ConnectionData *cd)
283 {
284   GList *l;
285   guint i;
286
287   g_hash_table_destroy (cd->dispatched_channels);
288   g_hash_table_destroy (cd->dispatching_channels);
289
290   for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
291     {
292       free_dispatcher_request_data (l->data);
293     }
294
295   if (cd->requestable_channels  != NULL)
296     {
297       for (i = 0 ; i < cd->requestable_channels->len ; i++)
298           g_value_array_free (
299             g_ptr_array_index (cd->requestable_channels, i));
300       g_ptr_array_free (cd->requestable_channels, TRUE);
301     }
302 }
303
304 static void
305 free_find_channel_request (FindChannelRequest *r)
306 {
307   guint idx;
308   char *str;
309
310   g_object_unref (r->dispatcher);
311   g_free (r->channel_type);
312
313   if (r->properties != NULL)
314     {
315       for (idx = 0; idx < r->properties->len ; idx++)
316         {
317           str = g_array_index (r->properties, char *, idx);
318           g_free (str);
319         }
320
321       g_array_free (r->properties, TRUE);
322     }
323
324   g_slice_free (FindChannelRequest, r);
325 }
326
327 static void
328 dispatcher_connection_invalidated_cb (TpConnection *connection,
329                                       guint domain,
330                                       gint code,
331                                       gchar *message,
332                                       EmpathyDispatcher *self)
333 {
334   EmpathyDispatcherPriv *priv = GET_PRIV (self);
335
336   DEBUG ("Error: %s", message);
337   g_hash_table_remove (priv->connections, connection);
338 }
339
340 static gboolean
341 dispatcher_operation_can_start (EmpathyDispatcher *self,
342                                 EmpathyDispatchOperation *operation,
343                                 ConnectionData *cd)
344 {
345   GList *l;
346   const gchar *channel_type =
347     empathy_dispatch_operation_get_channel_type (operation);
348
349   for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
350     {
351       DispatcherRequestData *d = (DispatcherRequestData *) l->data;
352
353       if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
354         {
355           return FALSE;
356         }
357     }
358
359   return TRUE;
360 }
361
362 static void
363 dispatch_operation_flush_requests (EmpathyDispatcher *self,
364                                    EmpathyDispatchOperation *operation,
365                                    GError *error,
366                                    ConnectionData *cd)
367 {
368   GList *l;
369
370   l = cd->outstanding_requests;
371   while (l != NULL)
372     {
373       DispatcherRequestData *d = (DispatcherRequestData *) l->data;
374       GList *lt = l;
375
376       l = g_list_next (l);
377
378       if (d->operation == operation)
379         {
380           if (d->cb != NULL)
381             {
382               if (error != NULL)
383                 d->cb (NULL, error, d->user_data);
384               else
385                 d->cb (operation, NULL, d->user_data);
386             }
387
388           cd->outstanding_requests = g_list_delete_link
389             (cd->outstanding_requests, lt);
390
391           free_dispatcher_request_data (d);
392         }
393     }
394 }
395
396 static void
397 dispatcher_channel_invalidated_cb (TpProxy *proxy,
398                                    guint domain,
399                                    gint code,
400                                    gchar *message,
401                                    EmpathyDispatcher *self)
402 {
403   /* Channel went away... */
404   EmpathyDispatcherPriv *priv = GET_PRIV (self);
405   TpConnection *connection;
406   EmpathyDispatchOperation *operation;
407   ConnectionData *cd;
408   const gchar *object_path;
409
410   connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
411
412   cd = g_hash_table_lookup (priv->connections, connection);
413   /* Connection itself invalidated? */
414   if (cd == NULL)
415     return;
416
417   object_path = tp_proxy_get_object_path (proxy);
418
419   DEBUG ("Channel %s invalidated", object_path);
420
421   g_hash_table_remove (cd->dispatched_channels, object_path);
422   g_hash_table_remove (cd->dispatching_channels, object_path);
423
424   priv->channels = g_list_remove (priv->channels, proxy);
425
426   operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
427   if (operation != NULL)
428     {
429       GError error = { domain, code, message };
430       dispatch_operation_flush_requests (self, operation, &error, cd);
431       g_hash_table_remove (cd->outstanding_channels, object_path);
432       g_object_unref (operation);
433     }
434 }
435
436 static void
437 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
438                                 EmpathyDispatcher *self)
439 {
440   g_assert (empathy_dispatch_operation_is_incoming (operation));
441   DEBUG ("Send of for dispatching: %s",
442     empathy_dispatch_operation_get_object_path (operation));
443   g_signal_emit (self, signals[DISPATCH], 0, operation);
444 }
445
446 static void
447 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
448                                EmpathyDispatcher *self)
449 {
450   /* Our job is done, remove the dispatch operation and mark the channel as
451    * dispatched */
452   EmpathyDispatcherPriv *priv = GET_PRIV (self);
453   TpConnection *connection;
454   ConnectionData *cd;
455   const gchar *object_path;
456
457   connection = empathy_dispatch_operation_get_tp_connection (operation);
458   cd = g_hash_table_lookup (priv->connections, connection);
459   g_assert (cd != NULL);
460
461   object_path = empathy_dispatch_operation_get_object_path (operation);
462
463   if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
464     {
465       DispatchData *d;
466       d = new_dispatch_data (
467         empathy_dispatch_operation_get_channel (operation),
468         empathy_dispatch_operation_get_channel_wrapper (operation));
469       g_hash_table_insert (cd->dispatched_channels,
470         g_strdup (object_path), d);
471     }
472   g_hash_table_remove (cd->dispatching_channels, object_path);
473
474   DEBUG ("Channel claimed: %s", object_path);
475 }
476
477 static void
478 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
479                              EmpathyDispatcher *self)
480 {
481   EmpathyDispatcherPriv *priv = GET_PRIV (self);
482   TpConnection *connection;
483   ConnectionData *cd;
484   EmpathyDispatchOperationState status;
485
486   g_signal_connect (operation, "approved",
487     G_CALLBACK (dispatch_operation_approved_cb), self);
488
489   g_signal_connect (operation, "claimed",
490     G_CALLBACK (dispatch_operation_claimed_cb), self);
491
492   /* Signal the observers */
493   DEBUG ("Send to observers: %s",
494     empathy_dispatch_operation_get_object_path (operation));
495   g_signal_emit (self, signals[OBSERVE], 0, operation);
496
497   empathy_dispatch_operation_start (operation);
498
499   /* Signal potential requestors */
500   connection =  empathy_dispatch_operation_get_tp_connection (operation);
501   cd = g_hash_table_lookup (priv->connections, connection);
502   g_assert (cd != NULL);
503
504   g_object_ref (operation);
505   g_object_ref (self);
506
507   dispatch_operation_flush_requests (self, operation, NULL, cd);
508   status = empathy_dispatch_operation_get_status (operation);
509   g_object_unref (operation);
510
511   if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
512     return;
513
514   if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
515     {
516       DEBUG ("Send to approvers: %s",
517         empathy_dispatch_operation_get_object_path (operation));
518       g_signal_emit (self, signals[APPROVE], 0, operation);
519     }
520   else
521     {
522       g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
523       DEBUG ("Send of for dispatching: %s",
524         empathy_dispatch_operation_get_object_path (operation));
525       g_signal_emit (self, signals[DISPATCH], 0, operation);
526     }
527
528   g_object_unref (self);
529 }
530
531 static void
532 dispatcher_start_dispatching (EmpathyDispatcher *self,
533                               EmpathyDispatchOperation *operation,
534                               ConnectionData *cd)
535 {
536   const gchar *object_path =
537     empathy_dispatch_operation_get_object_path (operation);
538
539   DEBUG ("Dispatching process started for %s", object_path);
540
541   if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
542     {
543       g_assert (g_hash_table_lookup (cd->outstanding_channels,
544         object_path) == NULL);
545
546       g_hash_table_insert (cd->dispatching_channels,
547         g_strdup (object_path), operation);
548
549       switch (empathy_dispatch_operation_get_status (operation))
550         {
551           case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
552             g_signal_connect (operation, "ready",
553               G_CALLBACK (dispatch_operation_ready_cb), self);
554             break;
555           case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
556             dispatch_operation_ready_cb (operation, self);
557             break;
558           default:
559             g_assert_not_reached ();
560         }
561
562     }
563   else if (empathy_dispatch_operation_get_status (operation) >=
564       EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
565     {
566       /* Already dispatching and the operation is pending, thus the observers
567        * have seen it (if applicable), so we can flush the request right away.
568        */
569       dispatch_operation_flush_requests (self, operation, NULL, cd);
570     }
571 }
572
573 static void
574 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
575                                          ConnectionData *cd)
576 {
577   GHashTableIter iter;
578   gpointer value;
579
580   g_hash_table_iter_init (&iter, cd->outstanding_channels);
581   while (g_hash_table_iter_next (&iter, NULL, &value))
582     {
583       EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
584
585       if (dispatcher_operation_can_start (self, operation, cd))
586         {
587           g_hash_table_iter_remove (&iter);
588           dispatcher_start_dispatching (self, operation, cd);
589         }
590     }
591 }
592
593 static void
594 dispatcher_connection_new_channel (EmpathyDispatcher *self,
595                                    TpConnection *connection,
596                                    const gchar *object_path,
597                                    const gchar *channel_type,
598                                    guint handle_type,
599                                    guint handle,
600                                    GHashTable *properties,
601                                    gboolean incoming)
602 {
603   EmpathyDispatcherPriv *priv = GET_PRIV (self);
604   TpChannel         *channel;
605   ConnectionData *cd;
606   EmpathyDispatchOperation *operation;
607   int i;
608   /* Channel types we never want to dispatch because they're either deprecated
609    * or can't sensibly be dispatch (e.g. channels that should always be
610    * requested) */
611   const char *blacklist[] = {
612     TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
613     TP_IFACE_CHANNEL_TYPE_TUBES,
614     TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
615     NULL
616   };
617
618   dispatcher_init_connection_if_needed (self, connection);
619
620   cd = g_hash_table_lookup (priv->connections, connection);
621
622   /* Don't bother with channels we have already dispatched or are dispatching
623    * currently. This can happen when NewChannel(s) is fired after
624    * RequestChannel/CreateChannel/EnsureChannel */
625   if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
626     return;
627
628   if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
629     return;
630
631   /* Should never occur, but just in case a CM fires spurious NewChannel(s)
632    * signals */
633   if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
634     return;
635
636   /* Only pick up non-requested text and file channels. For all other it
637    * doesn't make sense to handle it if we didn't request it. The same goes
638    * for channels we discovered by the Channels property or ListChannels */
639   if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
640         && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
641     {
642       DEBUG ("Ignoring incoming channel of type %s on %s",
643         channel_type, object_path);
644       return;
645     }
646
647   for (i = 0 ; blacklist[i] != NULL; i++)
648     {
649       if (!tp_strdiff (channel_type, blacklist[i]))
650         {
651           DEBUG ("Ignoring blacklisted channel type %s on %s",
652             channel_type, object_path);
653           return;
654         }
655     }
656
657   DEBUG ("New channel of type %s on %s", channel_type, object_path);
658
659   if (properties == NULL)
660     channel = tp_channel_new (connection, object_path, channel_type,
661       handle_type, handle, NULL);
662   else
663     channel = tp_channel_new_from_properties (connection, object_path,
664       properties, NULL);
665
666   g_signal_connect (channel, "invalidated",
667     G_CALLBACK (dispatcher_channel_invalidated_cb),
668     self);
669
670   priv->channels = g_list_prepend (priv->channels, channel);
671
672   operation = empathy_dispatch_operation_new (connection, channel, NULL,
673     incoming);
674
675   g_object_unref (channel);
676
677   if (incoming)
678     {
679       /* Request could either be by us or by a remote party. If there are no
680        * outstanding requests for this channel type we can assume it's remote.
681        * Otherwise we wait untill they are all satisfied */
682       if (dispatcher_operation_can_start (self, operation, cd))
683         dispatcher_start_dispatching (self, operation, cd);
684       else
685         g_hash_table_insert (cd->outstanding_channels,
686           g_strdup (object_path), operation);
687     }
688   else
689     {
690       dispatcher_start_dispatching (self, operation, cd);
691     }
692 }
693
694 static void
695 dispatcher_connection_new_channel_with_properties (
696     EmpathyDispatcher *self,
697     TpConnection *connection,
698     const gchar *object_path,
699     GHashTable *properties)
700 {
701   const gchar *channel_type;
702   guint handle_type;
703   guint handle;
704   gboolean requested;
705   gboolean valid;
706
707
708   channel_type = tp_asv_get_string (properties,
709     TP_IFACE_CHANNEL ".ChannelType");
710   if (channel_type == NULL)
711     {
712       g_message ("%s had an invalid ChannelType property", object_path);
713       return;
714     }
715
716   handle_type = tp_asv_get_uint32 (properties,
717     TP_IFACE_CHANNEL ".TargetHandleType", &valid);
718   if (!valid)
719     {
720       g_message ("%s had an invalid TargetHandleType property", object_path);
721       return;
722     }
723
724   handle = tp_asv_get_uint32 (properties,
725     TP_IFACE_CHANNEL ".TargetHandle", &valid);
726   if (!valid)
727     {
728       g_message ("%s had an invalid TargetHandle property", object_path);
729       return;
730     }
731
732   /* We assume there is no channel dispather, so we're the only one dispatching
733    * it. Which means that a requested channel it is outgoing one */
734   requested = tp_asv_get_boolean (properties,
735     TP_IFACE_CHANNEL ".Requested", &valid);
736   if (!valid)
737     {
738       g_message ("%s had an invalid Requested property", object_path);
739       requested = FALSE;
740     }
741
742   dispatcher_connection_new_channel (self, connection,
743     object_path, channel_type, handle_type, handle, properties, !requested);
744 }
745
746 static void
747 dispatcher_connection_got_all (TpProxy *proxy,
748                                GHashTable *properties,
749                                const GError *error,
750                                gpointer user_data,
751                                GObject *object)
752 {
753   EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
754   EmpathyDispatcherPriv *priv = GET_PRIV (self);
755   GPtrArray *requestable_channels;
756   GPtrArray *existing_channels;
757
758   if (error) {
759     DEBUG ("Error: %s", error->message);
760     return;
761   }
762
763   requestable_channels = tp_asv_get_boxed (properties,
764     "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
765
766   if (requestable_channels == NULL)
767     DEBUG ("No RequestableChannelClasses property !?! on connection");
768   else
769     {
770       ConnectionData *cd;
771       GList *requests, *l;
772       FindChannelRequest *request;
773       GList *retval;
774
775       cd = g_hash_table_lookup (priv->connections, proxy);
776       g_assert (cd != NULL);
777
778       cd->requestable_channels = g_boxed_copy (
779         TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
780
781       requests = g_hash_table_lookup (priv->outstanding_classes_requests,
782           proxy);
783
784       for (l = requests; l != NULL; l = l->next)
785         {
786           request = l->data;
787
788           retval = empathy_dispatcher_find_channel_classes (self,
789               TP_CONNECTION (proxy), request->channel_type,
790               request->handle_type, request->properties);
791           request->callback (retval, request->user_data);
792
793           free_find_channel_request (request);
794           g_list_free (retval);
795         }
796
797       g_list_free (requests);
798
799       g_hash_table_remove (priv->outstanding_classes_requests, proxy);
800     }
801
802   existing_channels = tp_asv_get_boxed (properties,
803       "Channels", TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST);
804
805   if (existing_channels != NULL)
806     {
807       guint idx;
808
809       for (idx = 0; idx < existing_channels->len; idx++)
810         {
811           GValueArray *values = g_ptr_array_index (existing_channels, idx);
812           const gchar *object_path;
813           GHashTable *props;
814
815           object_path = g_value_get_boxed (g_value_array_get_nth (values, 0));
816           props = g_value_get_boxed (g_value_array_get_nth (values, 1));
817
818           if (tp_strdiff (tp_asv_get_string (props,
819                       TP_IFACE_CHANNEL ".ChannelType"),
820                   TP_IFACE_CHANNEL_TYPE_TEXT))
821             continue;
822
823           dispatcher_connection_new_channel_with_properties (self,
824               TP_CONNECTION (proxy), object_path, props);
825         }
826     }
827 }
828
829 static void
830 dispatcher_connection_advertise_capabilities_cb (TpConnection    *connection,
831                                                  const GPtrArray *capabilities,
832                                                  const GError    *error,
833                                                  gpointer         user_data,
834                                                  GObject         *self)
835 {
836   if (error)
837     DEBUG ("Error: %s", error->message);
838 }
839
840 static void
841 connection_ready_cb (TpConnection *connection,
842     const GError *error,
843     gpointer user_data)
844 {
845   EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
846   GPtrArray   *capabilities;
847   GType        cap_type;
848   GValue       cap = {0, };
849   const gchar *remove_ = NULL;
850
851   if (error != NULL)
852     {
853       DEBUG ("Error: %s", error->message);
854       goto out;
855     }
856
857   if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
858       TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
859     {
860       tp_cli_dbus_properties_call_get_all (connection, -1,
861         TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
862         dispatcher_connection_got_all,
863         NULL, NULL, G_OBJECT (self));
864     }
865
866   /* Advertise VoIP capabilities */
867   capabilities = g_ptr_array_sized_new (1);
868   cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
869     G_TYPE_UINT, G_TYPE_INVALID);
870   g_value_init (&cap, cap_type);
871   g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
872   dbus_g_type_struct_set (&cap,
873         0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
874         1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
875            TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
876            TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN  |
877            TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P |
878            TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_ICE_UDP,
879            G_MAXUINT);
880   g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
881
882   tp_cli_connection_interface_capabilities_call_advertise_capabilities (
883     connection, -1, capabilities, &remove_,
884     dispatcher_connection_advertise_capabilities_cb,
885     NULL, NULL, G_OBJECT (self));
886
887   g_value_unset (&cap);
888   g_ptr_array_free (capabilities, TRUE);
889 out:
890   g_object_unref (self);
891 }
892
893 static void
894 dispatcher_init_connection_if_needed (EmpathyDispatcher *self,
895     TpConnection *connection)
896 {
897   EmpathyDispatcherPriv *priv = GET_PRIV (self);
898
899   if (g_hash_table_lookup (priv->connections, connection) != NULL)
900     return;
901
902   g_hash_table_insert (priv->connections, g_object_ref (connection),
903     new_connection_data ());
904
905   g_signal_connect (connection, "invalidated",
906     G_CALLBACK (dispatcher_connection_invalidated_cb), self);
907
908   /* Ensure to keep the self object alive while the call_when_ready is
909    * running */
910   g_object_ref (self);
911   tp_connection_call_when_ready (connection, connection_ready_cb, self);
912 }
913
914 static void
915 dispatcher_status_changed_cb (TpAccount *account,
916                               guint old_status,
917                               guint new_status,
918                               guint reason,
919                               gchar *dbus_error_name,
920                               GHashTable *details,
921                               EmpathyDispatcher *self)
922 {
923   TpConnection *conn = tp_account_get_connection (account);
924
925   if (conn != NULL)
926     dispatcher_init_connection_if_needed (self, conn);
927 }
928
929 static void
930 remove_idle_handlers (gpointer key,
931                       gpointer value,
932                       gpointer user_data)
933 {
934   guint source_id;
935
936   source_id = GPOINTER_TO_UINT (value);
937   g_source_remove (source_id);
938 }
939
940 static GObject *
941 dispatcher_constructor (GType type,
942                         guint n_construct_params,
943                         GObjectConstructParam *construct_params)
944 {
945   GObject *retval;
946   EmpathyDispatcherPriv *priv;
947
948   if (dispatcher != NULL)
949     return g_object_ref (dispatcher);
950
951   retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
952     (type, n_construct_params, construct_params);
953
954   dispatcher = EMPATHY_DISPATCHER (retval);
955   g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
956
957   priv = GET_PRIV (dispatcher);
958
959   if (priv->handler == NULL)
960     priv->handler = empathy_handler_new (NULL, NULL, NULL);
961
962   empathy_handler_set_handle_channels_func (priv->handler,
963     empathy_dispatcher_handle_channels,
964     dispatcher);
965
966   empathy_handler_set_channels_func (priv->handler,
967     empathy_dispatcher_get_channels,
968     dispatcher);
969
970   return retval;
971 }
972
973 static void
974 dispatcher_dispose (GObject *object)
975 {
976   EmpathyDispatcherPriv *priv = GET_PRIV (object);
977   GHashTableIter iter;
978   gpointer connection;
979   GList *l;
980
981   if (priv->dispose_has_run)
982     return;
983
984   priv->dispose_has_run = TRUE;
985
986   for (l = priv->handlers ; l != NULL; l = g_list_next (l))
987     g_object_unref (G_OBJECT (l->data));
988
989   g_list_free (priv->handlers);
990   priv->handlers = NULL;
991
992   if (priv->handler != NULL)
993     g_object_unref (priv->handler);
994   priv->handler = NULL;
995
996   g_hash_table_iter_init (&iter, priv->connections);
997   while (g_hash_table_iter_next (&iter, &connection, NULL))
998     {
999       g_signal_handlers_disconnect_by_func (connection,
1000           dispatcher_connection_invalidated_cb, object);
1001     }
1002
1003   g_hash_table_destroy (priv->connections);
1004   priv->connections = NULL;
1005
1006   G_OBJECT_CLASS (empathy_dispatcher_parent_class)->dispose (object);
1007 }
1008
1009 static void
1010 dispatcher_finalize (GObject *object)
1011 {
1012   EmpathyDispatcherPriv *priv = GET_PRIV (object);
1013   GList *l;
1014   GHashTableIter iter;
1015   gpointer connection;
1016   GList *list;
1017   gpointer account, id;
1018
1019   if (priv->request_channel_class_async_ids != NULL)
1020     {
1021       g_hash_table_foreach (priv->request_channel_class_async_ids,
1022         remove_idle_handlers, NULL);
1023       g_hash_table_destroy (priv->request_channel_class_async_ids);
1024     }
1025
1026   for (l = priv->channels; l; l = l->next)
1027     {
1028       g_signal_handlers_disconnect_by_func (l->data,
1029           dispatcher_channel_invalidated_cb, object);
1030     }
1031
1032   g_list_free (priv->channels);
1033
1034   g_hash_table_iter_init (&iter, priv->outstanding_classes_requests);
1035   while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list))
1036     {
1037       g_list_foreach (list, (GFunc) free_find_channel_request, NULL);
1038       g_list_free (list);
1039     }
1040
1041   g_hash_table_iter_init (&iter, priv->status_changed_handlers);
1042   while (g_hash_table_iter_next (&iter, &account, &id))
1043     {
1044       g_signal_handler_disconnect (account, GPOINTER_TO_UINT (id));
1045     }
1046   g_hash_table_destroy (priv->status_changed_handlers);
1047
1048   g_object_unref (priv->account_manager);
1049
1050   g_hash_table_destroy (priv->outstanding_classes_requests);
1051 }
1052
1053 static void
1054 dispatcher_set_property (GObject *object,
1055   guint property_id,
1056   const GValue *value,
1057   GParamSpec *pspec)
1058 {
1059   EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
1060   EmpathyDispatcherPriv *priv = GET_PRIV (self);
1061
1062   switch (property_id)
1063     {
1064       case PROP_HANDLER:
1065         priv->handler = g_value_dup_object (value);
1066         break;
1067       default:
1068         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1069         break;
1070     }
1071 }
1072
1073 static void
1074 dispatcher_get_property (GObject *object,
1075   guint property_id,
1076   GValue *value,
1077   GParamSpec *pspec)
1078 {
1079   EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
1080   EmpathyDispatcherPriv *priv = GET_PRIV (self);
1081
1082   switch (property_id)
1083     {
1084       case PROP_HANDLER:
1085         g_value_set_object (value, priv->handler);
1086         break;
1087       default:
1088         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1089         break;
1090     }
1091 }
1092
1093 static void
1094 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
1095 {
1096   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1097   GParamSpec *param_spec;
1098
1099   object_class->dispose = dispatcher_dispose;
1100   object_class->finalize = dispatcher_finalize;
1101   object_class->constructor = dispatcher_constructor;
1102
1103   object_class->get_property = dispatcher_get_property;
1104   object_class->set_property = dispatcher_set_property;
1105
1106   param_spec = g_param_spec_object ("handler", "handler",
1107     "The main Telepathy Client Hander object",
1108     EMPATHY_TYPE_HANDLER,
1109     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1110   g_object_class_install_property (object_class,
1111     PROP_HANDLER, param_spec);
1112
1113   signals[OBSERVE] =
1114     g_signal_new ("observe",
1115       G_TYPE_FROM_CLASS (klass),
1116       G_SIGNAL_RUN_LAST,
1117       0,
1118       NULL, NULL,
1119       g_cclosure_marshal_VOID__OBJECT,
1120       G_TYPE_NONE,
1121       1, EMPATHY_TYPE_DISPATCH_OPERATION);
1122
1123   signals[APPROVE] =
1124     g_signal_new ("approve",
1125       G_TYPE_FROM_CLASS (klass),
1126       G_SIGNAL_RUN_LAST,
1127       0,
1128       NULL, NULL,
1129       g_cclosure_marshal_VOID__OBJECT,
1130       G_TYPE_NONE,
1131       1, EMPATHY_TYPE_DISPATCH_OPERATION);
1132
1133   signals[DISPATCH] =
1134     g_signal_new ("dispatch",
1135       G_TYPE_FROM_CLASS (klass),
1136       G_SIGNAL_RUN_LAST,
1137       0,
1138       NULL, NULL,
1139       g_cclosure_marshal_VOID__OBJECT,
1140       G_TYPE_NONE,
1141       1, EMPATHY_TYPE_DISPATCH_OPERATION);
1142
1143
1144   g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
1145 }
1146
1147 static void
1148 connect_account (EmpathyDispatcher *self,
1149     TpAccount *account)
1150 {
1151   EmpathyDispatcherPriv *priv = GET_PRIV (self);
1152   TpConnection *conn = tp_account_get_connection (account);
1153   gulong id;
1154
1155   id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->status_changed_handlers,
1156         account));
1157
1158   if (id != 0)
1159     return;
1160
1161   if (conn != NULL)
1162     dispatcher_status_changed_cb (account, 0, 0, 0, NULL, NULL, self);
1163
1164   id = g_signal_connect (account, "status-changed",
1165       G_CALLBACK (dispatcher_status_changed_cb), self);
1166
1167   g_hash_table_insert (priv->status_changed_handlers, account,
1168       GUINT_TO_POINTER (id));
1169 }
1170
1171 static void
1172 account_manager_prepared_cb (GObject *source_object,
1173                              GAsyncResult *result,
1174                              gpointer user_data)
1175 {
1176   GList *accounts, *l;
1177   EmpathyDispatcher *self = user_data;
1178   TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1179   GError *error = NULL;
1180
1181   if (!tp_account_manager_prepare_finish (account_manager, result, &error))
1182     {
1183       DEBUG ("Failed to prepare account manager: %s", error->message);
1184       g_error_free (error);
1185       return;
1186     }
1187
1188   accounts = tp_account_manager_get_valid_accounts (account_manager);
1189   for (l = accounts; l; l = l->next)
1190     {
1191       TpAccount *a = l->data;
1192
1193       connect_account (self, a);
1194     }
1195   g_list_free (accounts);
1196 }
1197
1198 static void
1199 account_prepare_cb (GObject *source_object,
1200     GAsyncResult *result,
1201     gpointer user_data)
1202 {
1203   EmpathyDispatcher *self = user_data;
1204   TpAccount *account = TP_ACCOUNT (source_object);
1205   GError *error = NULL;
1206
1207   if (!tp_account_prepare_finish (account, result, &error))
1208     {
1209       DEBUG ("Failed to prepare account: %s", error->message);
1210       g_error_free (error);
1211       return;
1212     }
1213
1214   connect_account (self, account);
1215 }
1216
1217 static void
1218 account_validity_changed_cb (TpAccountManager *manager,
1219     TpAccount *account,
1220     gboolean valid,
1221     gpointer user_data)
1222 {
1223   if (!valid)
1224     return;
1225
1226   tp_account_prepare_async (account, NULL, account_prepare_cb, user_data);
1227 }
1228
1229 static void
1230 empathy_dispatcher_init (EmpathyDispatcher *self)
1231 {
1232   EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1233     EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
1234
1235   self->priv = priv;
1236   priv->account_manager = tp_account_manager_dup ();
1237
1238   priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1239     g_object_unref, (GDestroyNotify) free_connection_data);
1240
1241   priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash,
1242     g_direct_equal, g_object_unref, NULL);
1243
1244   priv->channels = NULL;
1245
1246   tp_account_manager_prepare_async (priv->account_manager, NULL,
1247       account_manager_prepared_cb, self);
1248
1249   empathy_signal_connect_weak (priv->account_manager,
1250       "account-validity-changed", G_CALLBACK (account_validity_changed_cb),
1251       G_OBJECT (self));
1252
1253   priv->request_channel_class_async_ids = g_hash_table_new (g_direct_hash,
1254     g_direct_equal);
1255   priv->status_changed_handlers = g_hash_table_new (g_direct_hash,
1256       g_direct_equal);
1257 }
1258
1259 EmpathyDispatcher *
1260 empathy_dispatcher_new (const gchar *name,
1261   GPtrArray *filters,
1262   GStrv capabilities)
1263 {
1264   EmpathyHandler *handler;
1265   EmpathyDispatcher *ret;
1266
1267   g_assert (dispatcher == NULL);
1268   handler = empathy_handler_new (name, filters, capabilities);
1269
1270   ret = EMPATHY_DISPATCHER (
1271     g_object_new (EMPATHY_TYPE_DISPATCHER,
1272       "handler", handler,
1273       NULL));
1274   g_object_unref (handler);
1275
1276   return ret;
1277 }
1278
1279 EmpathyDispatcher *
1280 empathy_dispatcher_dup_singleton (void)
1281 {
1282   return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1283 }
1284
1285 static void
1286 dispatcher_request_failed (EmpathyDispatcher *self,
1287                            DispatcherRequestData *request_data,
1288                            const GError *error)
1289 {
1290   EmpathyDispatcherPriv *priv = GET_PRIV (self);
1291   ConnectionData *conn_data;
1292
1293   conn_data = g_hash_table_lookup (priv->connections,
1294       request_data->connection);
1295   if (request_data->cb != NULL)
1296     request_data->cb (NULL, error, request_data->user_data);
1297
1298   if (conn_data != NULL)
1299     {
1300       conn_data->outstanding_requests =
1301           g_list_remove (conn_data->outstanding_requests, request_data);
1302     }
1303   /* else Connection has been invalidated */
1304
1305   free_dispatcher_request_data (request_data);
1306 }
1307
1308 static void
1309 dispatcher_connection_new_requested_channel (EmpathyDispatcher *self,
1310   DispatcherRequestData *request_data,
1311   const gchar *object_path,
1312   GHashTable *properties,
1313   const GError *error)
1314 {
1315   EmpathyDispatcherPriv *priv = GET_PRIV (self);
1316   EmpathyDispatchOperation *operation = NULL;
1317   ConnectionData *conn_data;
1318
1319   /* The DispatcherRequestData owns a ref on the self object. As the request
1320    * data could be destroyed (when calling dispatcher_request_failed for
1321    * example) we keep a ref on self to be sure it stays alive while we are
1322    * executing this function. */
1323   g_object_ref (self);
1324
1325   conn_data = g_hash_table_lookup (priv->connections,
1326     request_data->connection);
1327
1328   if (error)
1329     {
1330       DEBUG ("Channel request failed: %s", error->message);
1331
1332       dispatcher_request_failed (self, request_data, error);
1333
1334       goto out;
1335     }
1336
1337   operation = g_hash_table_lookup (conn_data->outstanding_channels,
1338     object_path);
1339
1340   if (operation != NULL)
1341     g_hash_table_remove (conn_data->outstanding_channels, object_path);
1342   else
1343     operation = g_hash_table_lookup (conn_data->dispatching_channels,
1344         object_path);
1345
1346   if (operation == NULL)
1347     {
1348       DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1349         object_path);
1350
1351       if (data != NULL)
1352         {
1353           operation = empathy_dispatch_operation_new_with_wrapper (
1354             request_data->connection,
1355             data->channel, request_data->contact, FALSE,
1356             data->channel_wrapper);
1357         }
1358       else
1359         {
1360           TpChannel *channel;
1361
1362           if (properties != NULL)
1363             channel = tp_channel_new_from_properties (request_data->connection,
1364               object_path, properties, NULL);
1365           else
1366             channel = tp_channel_new (request_data->connection, object_path,
1367               request_data->channel_type, request_data->handle_type,
1368               request_data->handle, NULL);
1369
1370           g_signal_connect (channel, "invalidated",
1371             G_CALLBACK (dispatcher_channel_invalidated_cb),
1372             request_data->dispatcher);
1373
1374           priv->channels = g_list_prepend (priv->channels, channel);
1375
1376           operation = empathy_dispatch_operation_new (request_data->connection,
1377              channel, request_data->contact, FALSE);
1378           g_object_unref (channel);
1379         }
1380     }
1381   else
1382     {
1383       /* Already existed set potential extra information */
1384       g_object_set (G_OBJECT (operation),
1385         "contact", request_data->contact,
1386         NULL);
1387     }
1388
1389   request_data->operation = operation;
1390
1391   /* (pre)-approve this right away as we requested it
1392    * This might cause the channel to be claimed, in which case the operation
1393    * will disappear. So ref it, and check the status before starting the
1394    * dispatching */
1395
1396   g_object_ref (operation);
1397   empathy_dispatch_operation_approve (operation);
1398
1399    if (empathy_dispatch_operation_get_status (operation) <
1400      EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1401       dispatcher_start_dispatching (request_data->dispatcher, operation,
1402           conn_data);
1403
1404   g_object_unref (operation);
1405
1406 out:
1407   dispatcher_flush_outstanding_operations (self, conn_data);
1408   g_object_unref (self);
1409 }
1410
1411 static void
1412 dispatcher_request_channel_cb (TpConnection *connection,
1413                                const gchar  *object_path,
1414                                const GError *error,
1415                                gpointer user_data,
1416                                GObject *weak_object)
1417 {
1418   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1419   EmpathyDispatcher *self =
1420       EMPATHY_DISPATCHER (request_data->dispatcher);
1421
1422   request_data->pending_call = NULL;
1423
1424   dispatcher_connection_new_requested_channel (self,
1425     request_data, object_path, NULL, error);
1426 }
1427
1428 static void
1429 dispatcher_request_channel (DispatcherRequestData *request_data)
1430 {
1431   if (tp_proxy_has_interface_by_id (TP_PROXY (request_data->connection),
1432       TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
1433     {
1434       /* Extend the request_data to be a valid request */
1435       g_assert (request_data->request == NULL);
1436       request_data->request = tp_asv_new (
1437         TP_IFACE_CHANNEL ".ChannelType",
1438           G_TYPE_STRING, request_data->channel_type,
1439         TP_IFACE_CHANNEL ".TargetHandleType",
1440           G_TYPE_UINT, request_data->handle_type,
1441         NULL);
1442
1443       if (request_data->handle_type != TP_HANDLE_TYPE_NONE)
1444         tp_asv_set_uint32 (request_data->request,
1445           TP_IFACE_CHANNEL ".TargetHandle", request_data->handle);
1446
1447       empathy_dispatcher_call_create_or_ensure_channel (
1448         request_data->dispatcher, request_data);
1449     }
1450   else
1451     {
1452       request_data->pending_call = tp_cli_connection_call_request_channel (
1453         request_data->connection, -1,
1454         request_data->channel_type,
1455         request_data->handle_type,
1456         request_data->handle,
1457         TRUE, dispatcher_request_channel_cb,
1458         request_data, NULL, NULL);
1459     }
1460 }
1461
1462 void
1463 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1464                                       EmpathyDispatcherRequestCb *callback,
1465                                       gpointer user_data)
1466 {
1467   EmpathyDispatcher *self;
1468   EmpathyDispatcherPriv *priv;
1469   TpConnection *connection;
1470   ConnectionData *connection_data;
1471   DispatcherRequestData *request_data;
1472
1473   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1474
1475   self = empathy_dispatcher_dup_singleton ();
1476   priv = GET_PRIV (self);
1477
1478   connection = empathy_contact_get_connection (contact);
1479   connection_data = g_hash_table_lookup (priv->connections, connection);
1480
1481   /* The contact handle might not be known yet */
1482   request_data = new_dispatcher_request_data (self, connection,
1483     TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1484     empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1485   request_data->should_ensure = TRUE;
1486
1487   connection_data->outstanding_requests = g_list_prepend
1488     (connection_data->outstanding_requests, request_data);
1489
1490   dispatcher_request_channel (request_data);
1491
1492   g_object_unref (self);
1493 }
1494
1495 typedef struct
1496 {
1497   EmpathyDispatcher *dispatcher;
1498   EmpathyDispatcherRequestCb *callback;
1499   gpointer user_data;
1500 } ChatWithContactIdData;
1501
1502 static void
1503 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1504                                     EmpathyContact          *contact,
1505                                     const GError            *error,
1506                                     gpointer                 user_data,
1507                                     GObject                 *weak_object)
1508 {
1509   ChatWithContactIdData *data = user_data;
1510
1511   if (error)
1512     {
1513       /* FIXME: Should call data->callback with the error */
1514       DEBUG ("Error: %s", error->message);
1515     }
1516   else
1517     {
1518       empathy_dispatcher_chat_with_contact (contact, data->callback,
1519           data->user_data);
1520     }
1521
1522   g_object_unref (data->dispatcher);
1523   g_slice_free (ChatWithContactIdData, data);
1524 }
1525
1526 void
1527 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1528                                          const gchar *contact_id,
1529                                          EmpathyDispatcherRequestCb *callback,
1530                                          gpointer user_data)
1531 {
1532   EmpathyDispatcher *self;
1533   EmpathyTpContactFactory *factory;
1534   ChatWithContactIdData *data;
1535
1536   g_return_if_fail (TP_IS_CONNECTION (connection));
1537   g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1538
1539   self = empathy_dispatcher_dup_singleton ();
1540   factory = empathy_tp_contact_factory_dup_singleton (connection);
1541   data = g_slice_new0 (ChatWithContactIdData);
1542   data->dispatcher = self;
1543   data->callback = callback;
1544   data->user_data = user_data;
1545   empathy_tp_contact_factory_get_from_id (factory, contact_id,
1546       dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1547
1548   g_object_unref (factory);
1549 }
1550
1551 static void
1552 dispatcher_request_handles_cb (TpConnection *connection,
1553                                const GArray *handles,
1554                                const GError *error,
1555                                gpointer user_data,
1556                                GObject *object)
1557 {
1558   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1559
1560   request_data->pending_call = NULL;
1561
1562   if (error != NULL)
1563     {
1564       EmpathyDispatcher *self = request_data->dispatcher;
1565       EmpathyDispatcherPriv *priv = GET_PRIV (self);
1566       ConnectionData *cd;
1567
1568       cd = g_hash_table_lookup (priv->connections, request_data->connection);
1569
1570       if (request_data->cb)
1571         request_data->cb (NULL, error, request_data->user_data);
1572
1573       cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1574         request_data);
1575
1576       free_dispatcher_request_data (request_data);
1577
1578       dispatcher_flush_outstanding_operations (self, cd);
1579       return;
1580     }
1581
1582   request_data->handle = g_array_index (handles, guint, 0);
1583   dispatcher_request_channel (request_data);
1584 }
1585
1586 void
1587 empathy_dispatcher_join_muc (TpConnection *connection,
1588                              const gchar *roomname,
1589                              EmpathyDispatcherRequestCb *callback,
1590                              gpointer user_data)
1591 {
1592   EmpathyDispatcher *self;
1593   EmpathyDispatcherPriv *priv;
1594   DispatcherRequestData *request_data;
1595   ConnectionData *connection_data;
1596   const gchar *names[] = { roomname, NULL };
1597
1598   g_return_if_fail (TP_IS_CONNECTION (connection));
1599   g_return_if_fail (!EMP_STR_EMPTY (roomname));
1600
1601   self = empathy_dispatcher_dup_singleton ();
1602   priv = GET_PRIV (self);
1603
1604   connection_data = g_hash_table_lookup (priv->connections, connection);
1605   g_assert (connection_data != NULL);
1606
1607   /* Don't know the room handle yet */
1608   request_data  = new_dispatcher_request_data (self, connection,
1609     TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1610     NULL, callback, user_data);
1611   request_data->should_ensure = TRUE;
1612
1613   connection_data->outstanding_requests = g_list_prepend
1614     (connection_data->outstanding_requests, request_data);
1615
1616   request_data->pending_call = tp_cli_connection_call_request_handles (
1617     connection, -1,
1618     TP_HANDLE_TYPE_ROOM, names,
1619     dispatcher_request_handles_cb, request_data, NULL, NULL);
1620
1621   g_object_unref (self);
1622 }
1623
1624 static void
1625 dispatcher_create_channel_cb (TpConnection *connect,
1626                               const gchar *object_path,
1627                               GHashTable *properties,
1628                               const GError *error,
1629                               gpointer user_data,
1630                               GObject *weak_object)
1631 {
1632   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1633   EmpathyDispatcher *self =
1634       EMPATHY_DISPATCHER (request_data->dispatcher);
1635
1636   request_data->pending_call = NULL;
1637
1638   dispatcher_connection_new_requested_channel (self,
1639     request_data, object_path, properties, error);
1640 }
1641
1642 static void
1643 dispatcher_ensure_channel_cb (TpConnection *connect,
1644                               gboolean is_ours,
1645                               const gchar *object_path,
1646                               GHashTable *properties,
1647                               const GError *error,
1648                               gpointer user_data,
1649                               GObject *weak_object)
1650 {
1651   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1652   EmpathyDispatcher *self =
1653       EMPATHY_DISPATCHER (request_data->dispatcher);
1654
1655   request_data->pending_call = NULL;
1656
1657   dispatcher_connection_new_requested_channel (self,
1658     request_data, object_path, properties, error);
1659 }
1660
1661 static void
1662 empathy_dispatcher_call_create_or_ensure_channel (
1663     EmpathyDispatcher *self,
1664     DispatcherRequestData *request_data)
1665 {
1666   if (request_data->should_ensure)
1667     {
1668       request_data->pending_call =
1669           tp_cli_connection_interface_requests_call_ensure_channel (
1670           request_data->connection, -1,
1671           request_data->request, dispatcher_ensure_channel_cb,
1672           request_data, NULL, NULL);
1673     }
1674   else
1675     {
1676       request_data->pending_call =
1677           tp_cli_connection_interface_requests_call_create_channel (
1678           request_data->connection, -1,
1679           request_data->request, dispatcher_create_channel_cb,
1680           request_data, NULL, NULL);
1681     }
1682 }
1683
1684 /**
1685  * empathy_dispatcher_create_channel:
1686  * @self: the EmpathyDispatcher
1687  * @connection: the Connection to dispatch on
1688  * @request: an a{sv} map of properties for the request, i.e. using tp_asv_new()
1689  * @callback: a callback for when the channel arrives (or NULL)
1690  * @user_data: optional user data (or NULL)
1691  *
1692  * When calling this function, #EmpathyDispatcher takes ownership of your
1693  * reference to @request. DO NOT unref or destroy @request. When the request is
1694  * done, @request will be unreferenced. Take another reference if you want to
1695  * keep it around.
1696  */
1697 void
1698 empathy_dispatcher_create_channel (EmpathyDispatcher *self,
1699                                    TpConnection *connection,
1700                                    GHashTable *request,
1701                                    EmpathyDispatcherRequestCb *callback,
1702                                    gpointer user_data)
1703 {
1704   EmpathyDispatcherPriv *priv = GET_PRIV (self);
1705   ConnectionData *connection_data;
1706   DispatcherRequestData *request_data;
1707   const gchar *channel_type;
1708   guint handle_type;
1709   guint handle;
1710   gboolean valid;
1711
1712   g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
1713   g_return_if_fail (TP_IS_CONNECTION (connection));
1714   g_return_if_fail (request != NULL);
1715
1716   connection_data = g_hash_table_lookup (priv->connections, connection);
1717   g_assert (connection_data != NULL);
1718
1719   channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1720
1721   handle_type = tp_asv_get_uint32 (request,
1722     TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1723   if (!valid)
1724     handle_type = TP_UNKNOWN_HANDLE_TYPE;
1725
1726   handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1727
1728   request_data  = new_dispatcher_request_data (self, connection,
1729     channel_type, handle_type, handle, request,
1730     NULL, callback, user_data);
1731
1732   connection_data->outstanding_requests = g_list_prepend
1733     (connection_data->outstanding_requests, request_data);
1734
1735   empathy_dispatcher_call_create_or_ensure_channel (self, request_data);
1736 }
1737
1738 static gboolean
1739 channel_class_matches (GValueArray *class,
1740                        const char *channel_type,
1741                        guint handle_type,
1742                        GArray *fixed_properties)
1743 {
1744   GHashTable *fprops;
1745   GValue *v;
1746   const char *c_type;
1747   guint h_type;
1748   gboolean valid;
1749
1750   v = g_value_array_get_nth (class, 0);
1751
1752   /* if the class doesn't match channel type discard it. */
1753   fprops = g_value_get_boxed (v);
1754   c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1755
1756   if (tp_strdiff (channel_type, c_type))
1757     return FALSE;
1758
1759   /* we have the right channel type, see if the handle type matches */
1760   h_type = tp_asv_get_uint32 (fprops,
1761                               TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1762
1763   if (!valid || handle_type != h_type)
1764     return FALSE;
1765
1766   if (fixed_properties != NULL)
1767     {
1768       gpointer h_key, h_val;
1769       guint idx;
1770       GHashTableIter iter;
1771       gboolean found;
1772
1773       g_hash_table_iter_init (&iter, fprops);
1774
1775       while (g_hash_table_iter_next (&iter, &h_key, &h_val))
1776         {
1777           /* discard ChannelType and TargetHandleType, as we already
1778            * checked them.
1779            */
1780           if (!tp_strdiff ((char *) h_key, TP_IFACE_CHANNEL ".ChannelType") ||
1781               !tp_strdiff
1782                 ((char *) h_key, TP_IFACE_CHANNEL ".TargetHandleType"))
1783             continue;
1784
1785           found = FALSE;
1786
1787           for (idx = 0; idx < fixed_properties->len; idx++)
1788             {
1789               /* if |key| doesn't exist in |fixed_properties|, discard
1790                * the class.
1791                */
1792               if (!tp_strdiff
1793                     ((char *) h_key,
1794                      g_array_index (fixed_properties, char *, idx)))
1795                 {
1796                   found = TRUE;
1797                   /* exit the for() loop */
1798                   break;
1799                 }
1800             }
1801
1802           if (!found)
1803             return FALSE;
1804         }
1805     }
1806   else
1807     {
1808       /* if no fixed_properties are specified, discard the classes
1809        * with some fixed properties other than the two we already
1810        * checked.
1811        */
1812       if (g_hash_table_size (fprops) > 2)
1813         return FALSE;
1814     }
1815
1816   return TRUE;
1817 }
1818
1819 static GList *
1820 empathy_dispatcher_find_channel_classes (EmpathyDispatcher *self,
1821                                          TpConnection *connection,
1822                                          const gchar *channel_type,
1823                                          guint handle_type,
1824                                          GArray *fixed_properties)
1825 {
1826   EmpathyDispatcherPriv *priv = GET_PRIV (self);
1827   GValueArray *class;
1828   GPtrArray *classes;
1829   GList *matching_classes;
1830   guint i;
1831   ConnectionData *cd;
1832
1833   g_return_val_if_fail (channel_type != NULL, NULL);
1834   g_return_val_if_fail (handle_type != 0, NULL);
1835
1836   cd = g_hash_table_lookup (priv->connections, connection);
1837
1838   if (cd == NULL)
1839     return NULL;
1840
1841   classes = cd->requestable_channels;
1842   if (classes == NULL)
1843     return NULL;
1844
1845   matching_classes = NULL;
1846
1847   for (i = 0; i < classes->len; i++)
1848     {
1849       class = g_ptr_array_index (classes, i);
1850
1851       if (!channel_class_matches
1852           (class, channel_type, handle_type, fixed_properties))
1853         continue;
1854
1855       matching_classes = g_list_prepend (matching_classes, class);
1856     }
1857
1858   return matching_classes;
1859 }
1860
1861 static gboolean
1862 find_channel_class_idle_cb (gpointer user_data)
1863 {
1864   GList *retval;
1865   GList *requests;
1866   FindChannelRequest *request = user_data;
1867   ConnectionData *cd;
1868   gboolean is_ready = TRUE;
1869   EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher);
1870
1871   g_hash_table_remove (priv->request_channel_class_async_ids, request);
1872
1873   cd = g_hash_table_lookup (priv->connections, request->connection);
1874
1875   if (cd == NULL)
1876     is_ready = FALSE;
1877   else if (cd->requestable_channels == NULL)
1878     is_ready = FALSE;
1879
1880   if (is_ready)
1881     {
1882       retval = empathy_dispatcher_find_channel_classes (request->dispatcher,
1883           request->connection, request->channel_type, request->handle_type,
1884           request->properties);
1885
1886       request->callback (retval, request->user_data);
1887       free_find_channel_request (request);
1888       g_list_free (retval);
1889
1890       return FALSE;
1891     }
1892
1893   requests = g_hash_table_lookup (priv->outstanding_classes_requests,
1894       request->connection);
1895   requests = g_list_prepend (requests, request);
1896
1897   g_hash_table_insert (priv->outstanding_classes_requests,
1898       request->connection, requests);
1899
1900   return FALSE;
1901 }
1902
1903 static GArray *
1904 setup_varargs (va_list var_args,
1905                const char *channel_namespace,
1906                const char *first_property_name)
1907 {
1908   const char *name;
1909   char *name_full;
1910   GArray *properties;
1911
1912   if (first_property_name == NULL)
1913     return NULL;
1914
1915   name = first_property_name;
1916   properties = g_array_new (TRUE, TRUE, sizeof (char *));
1917
1918   while (name != NULL)
1919     {
1920       name_full = g_strdup (name);
1921       properties = g_array_append_val (properties, name_full);
1922       name = va_arg (var_args, char *);
1923     }
1924
1925   return properties;
1926 }
1927
1928 /**
1929  * empathy_dispatcher_find_requestable_channel_classes:
1930  * @dispatcher: an #EmpathyDispatcher
1931  * @connection: a #TpConnection
1932  * @channel_type: a string identifying the type of the channel to lookup
1933  * @handle_type: the handle type for the channel
1934  * @first_property_name: %NULL, or the name of the first fixed property,
1935  * followed optionally by more names, followed by %NULL.
1936  *
1937  * Returns all the channel classes that a client can request for the connection
1938  * @connection, of the type identified by @channel_type, @handle_type and the
1939  * fixed properties list.
1940  * The classes which are compatible with a fixed properties list (i.e. those
1941  * that will be returned by this function) are intended as those that do not
1942  * contain any fixed property other than those in the list; note that this
1943  * doesn't guarantee that all the classes compatible with the list will contain
1944  * all the requested fixed properties, so the clients will have to filter
1945  * the returned list themselves.
1946  * If @first_property_name is %NULL, only the classes with no other fixed
1947  * properties than ChannelType and TargetHandleType will be returned.
1948  * Note that this function may return %NULL without performing any lookup if
1949  * @connection is not ready. To ensure that @connection is always ready,
1950  * use the empathy_dispatcher_find_requestable_channel_classes_async() variant.
1951  *
1952  * Return value: a #GList of #GValueArray objects, where the first element in
1953  * the array is a #GHashTable of the fixed properties, and the second is
1954  * a #GStrv of the allowed properties for the class. The list should be free'd
1955  * with g_list_free() when done, but the objects inside the list are owned
1956  * by the #EmpathyDispatcher and must not be modified.
1957  */
1958 GList *
1959 empathy_dispatcher_find_requestable_channel_classes
1960                                  (EmpathyDispatcher *self,
1961                                   TpConnection *connection,
1962                                   const gchar *channel_type,
1963                                   guint handle_type,
1964                                   const char *first_property_name,
1965                                   ...)
1966 {
1967   va_list var_args;
1968   GArray *properties;
1969   EmpathyDispatcherPriv *priv;
1970   GList *retval;
1971   guint idx;
1972   char *str;
1973
1974   g_return_val_if_fail (EMPATHY_IS_DISPATCHER (self), NULL);
1975   g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
1976   g_return_val_if_fail (channel_type != NULL, NULL);
1977   g_return_val_if_fail (handle_type != 0, NULL);
1978
1979   priv = GET_PRIV (self);
1980
1981   va_start (var_args, first_property_name);
1982
1983   properties = setup_varargs (var_args, channel_type, first_property_name);
1984
1985   va_end (var_args);
1986
1987   retval = empathy_dispatcher_find_channel_classes (self, connection,
1988     channel_type, handle_type, properties);
1989
1990   if (properties != NULL)
1991     {
1992       /* free the properties array */
1993       for (idx = 0; idx < properties->len ; idx++)
1994         {
1995           str = g_array_index (properties, char *, idx);
1996           g_free (str);
1997         }
1998
1999       g_array_free (properties, TRUE);
2000     }
2001
2002   return retval;
2003 }
2004
2005 /**
2006  * empathy_dispatcher_find_requestable_channel_classes_async:
2007  * @dispatcher: an #EmpathyDispatcher
2008  * @connection: a #TpConnection
2009  * @channel_type: a string identifying the type of the channel to lookup
2010  * @handle_type: the handle type for the channel
2011  * @callback: the callback to call when @connection is ready
2012  * @user_data: the user data to pass to @callback
2013  * @first_property_name: %NULL, or the name of the first fixed property,
2014  * followed optionally by more names, followed by %NULL.
2015  *
2016  * Please see the documentation of
2017  * empathy_dispatcher_find_requestable_channel_classes() for a detailed
2018  * description of this function.
2019  */
2020 void
2021 empathy_dispatcher_find_requestable_channel_classes_async
2022                                  (EmpathyDispatcher *self,
2023                                   TpConnection *connection,
2024                                   const gchar *channel_type,
2025                                   guint handle_type,
2026                                   EmpathyDispatcherFindChannelClassCb callback,
2027                                   gpointer user_data,
2028                                   const char *first_property_name,
2029                                   ...)
2030 {
2031   va_list var_args;
2032   GArray *properties;
2033   FindChannelRequest *request;
2034   EmpathyDispatcherPriv *priv;
2035   guint source_id;
2036
2037   g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
2038   g_return_if_fail (TP_IS_CONNECTION (connection));
2039   g_return_if_fail (channel_type != NULL);
2040   g_return_if_fail (handle_type != 0);
2041
2042   priv = GET_PRIV (self);
2043
2044   va_start (var_args, first_property_name);
2045
2046   properties = setup_varargs (var_args, channel_type, first_property_name);
2047
2048   va_end (var_args);
2049
2050   /* append another request for this connection */
2051   request = g_slice_new0 (FindChannelRequest);
2052   request->dispatcher = g_object_ref (self);
2053   request->channel_type = g_strdup (channel_type);
2054   request->handle_type = handle_type;
2055   request->connection = connection;
2056   request->callback = callback;
2057   request->user_data = user_data;
2058   request->properties = properties;
2059
2060   source_id = g_idle_add (find_channel_class_idle_cb, request);
2061
2062   g_hash_table_insert (priv->request_channel_class_async_ids,
2063     request, GUINT_TO_POINTER (source_id));
2064 }
2065
2066 static GList *
2067 empathy_dispatcher_get_channels (EmpathyHandler *handler,
2068   gpointer user_data)
2069 {
2070   EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2071   EmpathyDispatcherPriv *priv = GET_PRIV (self);
2072
2073   return priv->channels;
2074 }
2075
2076 static gboolean
2077 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
2078     const gchar *account_path,
2079     const gchar *connection_path,
2080     const GPtrArray *channels,
2081     const GPtrArray *requests_satisfied,
2082     guint64 timestamp,
2083     GHashTable *handler_info,
2084     gpointer user_data,
2085     GError **error)
2086 {
2087   EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2088   EmpathyDispatcherPriv *priv = GET_PRIV (self);
2089   guint i;
2090   TpAccount *account;
2091   TpConnection *connection;
2092
2093   /* FIXME: should probably find out whether the account manager is prepared
2094    * before ensuring. See bug #600111. */
2095   account = tp_account_manager_ensure_account (priv->account_manager,
2096     account_path);
2097   g_assert (account != NULL);
2098
2099   connection = tp_account_ensure_connection (account, connection_path);
2100   if (connection == NULL)
2101     {
2102       g_set_error_literal (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
2103         "Invalid connection argument");
2104       return FALSE;
2105     }
2106
2107   for (i = 0; i < channels->len ; i++)
2108     {
2109       GValueArray *arr = g_ptr_array_index (channels, i);
2110       const gchar *object_path;
2111       GHashTable *properties;
2112
2113       object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
2114       properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
2115
2116       dispatcher_connection_new_channel_with_properties (self,
2117         connection, object_path, properties);
2118     }
2119
2120   return TRUE;
2121 }
2122
2123
2124 EmpathyHandler *
2125 empathy_dispatcher_add_handler (EmpathyDispatcher *self,
2126     const gchar *name,
2127     GPtrArray *filters,
2128     GStrv capabilities)
2129 {
2130   EmpathyDispatcherPriv *priv = GET_PRIV (self);
2131   EmpathyHandler *handler;
2132
2133   handler = empathy_handler_new (name, filters, capabilities);
2134   priv->handlers = g_list_prepend (priv->handlers, handler);
2135
2136   /* Only set the handle_channels function, the Channel property on the main
2137    * handler will always report all dispatched channels even if they came from
2138    * a different Handler */
2139   empathy_handler_set_handle_channels_func (handler,
2140     empathy_dispatcher_handle_channels, self);
2141
2142   return handler;
2143 }
2144
2145 void
2146 empathy_dispatcher_remove_handler (EmpathyDispatcher *self,
2147   EmpathyHandler *handler)
2148 {
2149   EmpathyDispatcherPriv *priv = GET_PRIV (self);
2150   GList *h;
2151
2152   h = g_list_find (priv->handlers, handler);
2153   g_return_if_fail (h != NULL);
2154
2155   priv->handlers = g_list_delete_link (priv->handlers, h);
2156
2157   g_object_unref (handler);
2158 }