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