]> git.0d.be Git - empathy.git/blob - libempathy/empathy-dispatcher.c
Actually see if the connection is ready
[empathy.git] / libempathy / empathy-dispatcher.c
1 /* * Copyright (C) 2007-2008 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  */
19
20 #include <config.h>
21
22 #include <string.h>
23
24 #include <glib/gi18n-lib.h>
25
26 #include <telepathy-glib/enums.h>
27 #include <telepathy-glib/connection.h>
28 #include <telepathy-glib/util.h>
29 #include <telepathy-glib/dbus.h>
30 #include <telepathy-glib/proxy-subclass.h>
31 #include <telepathy-glib/gtypes.h>
32
33 #include <libmissioncontrol/mission-control.h>
34
35 #include <extensions/extensions.h>
36
37 #include "empathy-dispatcher.h"
38 #include "empathy-utils.h"
39 #include "empathy-tube-handler.h"
40 #include "empathy-account-manager.h"
41 #include "empathy-tp-contact-factory.h"
42 #include "empathy-chatroom-manager.h"
43 #include "empathy-utils.h"
44
45 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
46 #include <libempathy/empathy-debug.h>
47
48 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
49 typedef struct
50 {
51   EmpathyAccountManager *account_manager;
52   MissionControl *mc;
53   /* connection to connection data mapping */
54   GHashTable *connections;
55   GHashTable *outstanding_classes_requests;
56   gpointer token;
57   GSList *tubes;
58
59   /* channels which the dispatcher is listening "invalidated" */
60   GList *channels;
61 } EmpathyDispatcherPriv;
62
63 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
64
65 enum
66 {
67   OBSERVE,
68   APPROVE,
69   DISPATCH,
70   LAST_SIGNAL
71 };
72
73 static guint signals[LAST_SIGNAL];
74 static EmpathyDispatcher *dispatcher = NULL;
75
76 typedef struct
77 {
78   EmpathyDispatcher *dispatcher;
79   EmpathyDispatchOperation *operation;
80   TpConnection *connection;
81   gchar *channel_type;
82   guint handle_type;
83   guint handle;
84   EmpathyContact *contact;
85
86   /* Properties to pass to the channel when requesting it */
87   GHashTable *request;
88   EmpathyDispatcherRequestCb *cb;
89   gpointer user_data;
90   gpointer *request_data;
91 } DispatcherRequestData;
92
93 typedef struct
94 {
95   TpChannel *channel;
96   /* Channel type specific wrapper object */
97   GObject *channel_wrapper;
98 } DispatchData;
99
100 typedef struct
101 {
102   /* ObjectPath => DispatchData.. */
103   GHashTable *dispatched_channels;
104   /* ObjectPath -> EmpathyDispatchOperations */
105   GHashTable *dispatching_channels;
106   /* ObjectPath -> EmpathyDispatchOperations */
107   GHashTable *outstanding_channels;
108   /* List of DispatcherRequestData */
109   GList *outstanding_requests;
110   /* List of requestable channel classes */
111   GPtrArray *requestable_channels;
112 } ConnectionData;
113
114 typedef struct
115 {
116   EmpathyDispatcher *dispatcher;
117   TpConnection *connection;
118   char *channel_type;
119   guint handle_type;
120   EmpathyDispatcherFindChannelClassCb *callback;
121   gpointer user_data;
122 } FindChannelRequest;
123
124 static DispatchData *
125 new_dispatch_data (TpChannel *channel,
126                    GObject *channel_wrapper)
127 {
128   DispatchData *d = g_slice_new0 (DispatchData);
129   d->channel = g_object_ref (channel);
130   if (channel_wrapper != NULL)
131     d->channel_wrapper = g_object_ref (channel_wrapper);
132
133   return d;
134 }
135
136 static void
137 free_dispatch_data (DispatchData *data)
138 {
139   g_object_unref (data->channel);
140   if (data->channel_wrapper != NULL)
141     g_object_unref (data->channel_wrapper);
142
143   g_slice_free (DispatchData, data);
144 }
145
146 static DispatcherRequestData *
147 new_dispatcher_request_data (EmpathyDispatcher *dispatcher,
148                              TpConnection *connection,
149                              const gchar *channel_type,
150                              guint handle_type,
151                              guint handle,
152                              GHashTable *request,
153                              EmpathyContact *contact,
154                              EmpathyDispatcherRequestCb *cb,
155                              gpointer user_data)
156 {
157   DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
158
159   result->dispatcher = g_object_ref (dispatcher);
160   result->connection = connection;
161
162   result->channel_type = g_strdup (channel_type);
163   result->handle_type = handle_type;
164   result->handle = handle;
165   result->request = request;
166
167   if (contact != NULL)
168     result->contact = g_object_ref (contact);
169
170   result->cb = cb;
171   result->user_data = user_data;
172
173   return result;
174 }
175
176 static void
177 free_dispatcher_request_data (DispatcherRequestData *r)
178 {
179   g_free (r->channel_type);
180
181   if (r->dispatcher != NULL)
182     g_object_unref (r->dispatcher);
183
184   if (r->contact != NULL)
185     g_object_unref (r->contact);
186
187   if (r->request != NULL)
188     g_hash_table_unref (r->request);
189
190   g_slice_free (DispatcherRequestData, r);
191 }
192
193 static ConnectionData *
194 new_connection_data (void)
195 {
196   ConnectionData *cd = g_slice_new0 (ConnectionData);
197
198   cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
199       g_free, (GDestroyNotify) free_dispatch_data);
200
201   cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
202       g_free, g_object_unref);
203
204   cd->outstanding_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
205       g_free, NULL);
206
207   return cd;
208 }
209
210 static void
211 free_connection_data (ConnectionData *cd)
212 {
213   GList *l;
214
215   g_hash_table_destroy (cd->dispatched_channels);
216   g_hash_table_destroy (cd->dispatching_channels);
217   int i;
218
219   for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
220     {
221       free_dispatcher_request_data (l->data);
222     }
223
224   if (cd->requestable_channels  != NULL)
225     {
226       for (i = 0 ; i < cd->requestable_channels->len ; i++)
227           g_value_array_free (
228             g_ptr_array_index (cd->requestable_channels, i));
229       g_ptr_array_free (cd->requestable_channels, TRUE);
230     }
231 }
232
233 static void
234 free_find_channel_request (FindChannelRequest *r)
235 {
236   g_object_unref (r->dispatcher);
237   g_free (r->channel_type);
238   g_slice_free (FindChannelRequest, r);
239 }
240
241 static void
242 dispatcher_connection_invalidated_cb (TpConnection *connection,
243                                       guint domain,
244                                       gint code,
245                                       gchar *message,
246                                       EmpathyDispatcher *dispatcher)
247 {
248   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
249
250   DEBUG ("Error: %s", message);
251   g_hash_table_remove (priv->connections, connection);
252 }
253
254 static gboolean
255 dispatcher_operation_can_start (EmpathyDispatcher *self,
256                                 EmpathyDispatchOperation *operation,
257                                 ConnectionData *cd)
258 {
259   GList *l;
260   const gchar *channel_type =
261     empathy_dispatch_operation_get_channel_type (operation);
262
263   for (l = cd->outstanding_requests; l != NULL; l = g_list_next (l))
264     {
265       DispatcherRequestData *d = (DispatcherRequestData *) l->data;
266
267       if (d->operation == NULL && !tp_strdiff (d->channel_type, channel_type))
268         {
269           return FALSE;
270         }
271     }
272
273   return TRUE;
274 }
275
276 static void
277 dispatch_operation_flush_requests (EmpathyDispatcher *dispatcher,
278                                    EmpathyDispatchOperation *operation,
279                                    GError *error,
280                                    ConnectionData *cd)
281 {
282   GList *l;
283
284   l = cd->outstanding_requests;
285   while (l != NULL)
286     {
287       DispatcherRequestData *d = (DispatcherRequestData *) l->data;
288       GList *lt = l;
289
290       l = g_list_next (l);
291
292       if (d->operation == operation)
293         {
294           if (d->cb != NULL)
295             {
296               if (error != NULL)
297                 d->cb (NULL, error, d->user_data);
298               else
299                 d->cb (operation, NULL, d->user_data);
300             }
301
302           cd->outstanding_requests = g_list_delete_link
303             (cd->outstanding_requests, lt);
304
305           free_dispatcher_request_data (d);
306         }
307     }
308 }
309
310 static void
311 dispatcher_channel_invalidated_cb (TpProxy *proxy,
312                                    guint domain,
313                                    gint code,
314                                    gchar *message,
315                                    EmpathyDispatcher *dispatcher)
316 {
317   /* Channel went away... */
318   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
319   TpConnection *connection;
320   EmpathyDispatchOperation *operation;
321   ConnectionData *cd;
322   const gchar *object_path;
323
324   connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
325
326   cd = g_hash_table_lookup (priv->connections, connection);
327   /* Connection itself invalidated? */
328   if (cd == NULL)
329     return;
330
331   object_path = tp_proxy_get_object_path (proxy);
332
333   DEBUG ("Channel %s invalidated", object_path);
334
335   g_hash_table_remove (cd->dispatched_channels, object_path);
336   g_hash_table_remove (cd->dispatching_channels, object_path);
337
338   priv->channels = g_list_remove (priv->channels, proxy);
339
340   operation = g_hash_table_lookup (cd->outstanding_channels, object_path);
341   if (operation != NULL)
342     {
343       GError error = { domain, code, message };
344       dispatch_operation_flush_requests (dispatcher, operation, &error, cd);
345       g_hash_table_remove (cd->outstanding_channels, object_path);
346       g_object_unref (operation);
347     }
348 }
349
350 static void
351 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
352                                 EmpathyDispatcher *dispatcher)
353 {
354   g_assert (empathy_dispatch_operation_is_incoming (operation));
355   DEBUG ("Send of for dispatching: %s",
356     empathy_dispatch_operation_get_object_path (operation));
357   g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
358 }
359
360 static void
361 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
362                                EmpathyDispatcher *dispatcher)
363 {
364   /* Our job is done, remove the dispatch operation and mark the channel as
365    * dispatched */
366   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
367   TpConnection *connection;
368   ConnectionData *cd;
369   const gchar *object_path;
370
371   connection = empathy_dispatch_operation_get_tp_connection (operation);
372   cd = g_hash_table_lookup (priv->connections, connection);
373   g_assert (cd != NULL);
374
375   object_path = empathy_dispatch_operation_get_object_path (operation);
376
377   if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
378     {
379       DispatchData *d;
380       d = new_dispatch_data (
381         empathy_dispatch_operation_get_channel (operation),
382         empathy_dispatch_operation_get_channel_wrapper (operation));
383       g_hash_table_insert (cd->dispatched_channels,
384         g_strdup (object_path), d);
385     }
386   g_hash_table_remove (cd->dispatching_channels, object_path);
387
388   DEBUG ("Channel claimed: %s", object_path);
389 }
390
391 static void
392 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
393                              EmpathyDispatcher *dispatcher)
394 {
395   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
396   TpConnection *connection;
397   ConnectionData *cd;
398   EmpathyDispatchOperationState status;
399
400   g_signal_connect (operation, "approved",
401     G_CALLBACK (dispatch_operation_approved_cb), dispatcher);
402
403   g_signal_connect (operation, "claimed",
404     G_CALLBACK (dispatch_operation_claimed_cb), dispatcher);
405
406   /* Signal the observers */
407   DEBUG ("Send to observers: %s",
408     empathy_dispatch_operation_get_object_path (operation));
409   g_signal_emit (dispatcher, signals[OBSERVE], 0, operation);
410
411   empathy_dispatch_operation_start (operation);
412
413   /* Signal potential requestors */
414   connection =  empathy_dispatch_operation_get_tp_connection (operation);
415   cd = g_hash_table_lookup (priv->connections, connection);
416   g_assert (cd != NULL);
417
418   g_object_ref (operation);
419   g_object_ref (dispatcher);
420
421   dispatch_operation_flush_requests (dispatcher, operation, NULL, cd);
422   status = empathy_dispatch_operation_get_status (operation);
423   g_object_unref (operation);
424
425   if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
426     return;
427
428   if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
429     {
430       DEBUG ("Send to approvers: %s",
431         empathy_dispatch_operation_get_object_path (operation));
432       g_signal_emit (dispatcher, signals[APPROVE], 0, operation);
433     }
434   else
435     {
436       g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
437       DEBUG ("Send of for dispatching: %s",
438         empathy_dispatch_operation_get_object_path (operation));
439       g_signal_emit (dispatcher, signals[DISPATCH], 0, operation);
440     }
441
442   g_object_unref (dispatcher);
443 }
444
445 static void
446 dispatcher_start_dispatching (EmpathyDispatcher *self,
447                               EmpathyDispatchOperation *operation,
448                               ConnectionData *cd)
449 {
450   const gchar *object_path =
451     empathy_dispatch_operation_get_object_path (operation);
452
453   DEBUG ("Dispatching process started for %s", object_path);
454
455   if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
456     {
457       g_assert (g_hash_table_lookup (cd->outstanding_channels,
458         object_path) == NULL);
459
460       g_hash_table_insert (cd->dispatching_channels,
461         g_strdup (object_path), operation);
462
463       switch (empathy_dispatch_operation_get_status (operation))
464         {
465           case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
466             g_signal_connect (operation, "ready",
467               G_CALLBACK (dispatch_operation_ready_cb), dispatcher);
468             break;
469           case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
470             dispatch_operation_ready_cb (operation, dispatcher);
471             break;
472           default:
473             g_assert_not_reached ();
474         }
475
476     }
477   else if (empathy_dispatch_operation_get_status (operation) >=
478       EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
479     {
480       /* Already dispatching and the operation is pending, thus the observers
481        * have seen it (if applicable), so we can flush the request right away.
482        */
483       dispatch_operation_flush_requests (self, operation, NULL, cd);
484     }
485 }
486
487 static void
488 dispatcher_flush_outstanding_operations (EmpathyDispatcher *self,
489                                          ConnectionData *cd)
490 {
491   GHashTableIter iter;
492   gpointer value;
493
494   g_hash_table_iter_init (&iter, cd->outstanding_channels);
495   while (g_hash_table_iter_next (&iter, NULL, &value))
496     {
497       EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (value);
498
499       if (dispatcher_operation_can_start (self, operation, cd))
500         {
501           dispatcher_start_dispatching (dispatcher, operation, cd);
502           g_hash_table_iter_remove (&iter);
503         }
504     }
505 }
506
507 static void
508 dispatcher_connection_new_channel (EmpathyDispatcher *dispatcher,
509                                    TpConnection *connection,
510                                    const gchar *object_path,
511                                    const gchar *channel_type,
512                                    guint handle_type,
513                                    guint handle,
514                                    GHashTable *properties,
515                                    gboolean incoming)
516 {
517   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
518   TpChannel         *channel;
519   ConnectionData *cd;
520   EmpathyDispatchOperation *operation;
521   int i;
522   /* Channel types we never want to dispatch because they're either deprecated
523    * or can't sensibly be dispatch (e.g. channels that should always be
524    * requested) */
525   const char *blacklist[] = {
526     TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
527     TP_IFACE_CHANNEL_TYPE_TUBES,
528     TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
529     NULL
530   };
531
532   cd = g_hash_table_lookup (priv->connections, connection);
533
534   /* Don't bother with channels we have already dispatched or are dispatching
535    * currently. This can happen when NewChannel(s) is fired after
536    * RequestChannel/CreateChannel/EnsureChannel */
537   if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
538     return;
539
540   if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
541     return;
542
543   /* Should never occur, but just in case a CM fires spurious NewChannel(s)
544    * signals */
545   if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
546     return;
547
548   /* Only pick up non-requested text and file channels. For all other it
549    * doesn't make sense to handle it if we didn't request it. The same goes
550    * for channels we discovered by the Channels property or ListChannels */
551   if (!incoming && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT)
552         && tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
553     {
554       DEBUG ("Ignoring incoming channel of type %s on %s",
555         channel_type, object_path);
556       return;
557     }
558
559   for (i = 0 ; blacklist[i] != NULL; i++)
560     {
561       if (!tp_strdiff (channel_type, blacklist[i]))
562         {
563           DEBUG ("Ignoring blacklisted channel type %s on %s",
564             channel_type, object_path);
565           return;
566         }
567     }
568
569   DEBUG ("New channel of type %s on %s", channel_type, object_path);
570
571   if (properties == NULL)
572     channel = tp_channel_new (connection, object_path, channel_type,
573       handle_type, handle, NULL);
574   else
575     channel = tp_channel_new_from_properties (connection, object_path,
576       properties, NULL);
577
578   g_signal_connect (channel, "invalidated",
579     G_CALLBACK (dispatcher_channel_invalidated_cb),
580     dispatcher);
581
582   priv->channels = g_list_prepend (priv->channels, channel);
583
584   operation = empathy_dispatch_operation_new (connection, channel, NULL,
585     incoming);
586
587   g_object_unref (channel);
588
589   if (incoming)
590     {
591       /* Request could either be by us or by a remote party. If there are no
592        * outstanding requests for this channel type we can assume it's remote.
593        * Otherwise we wait untill they are all satisfied */
594       if (dispatcher_operation_can_start (dispatcher, operation, cd))
595         dispatcher_start_dispatching (dispatcher, operation, cd);
596       else
597         g_hash_table_insert (cd->outstanding_channels,
598           g_strdup (object_path), operation);
599     }
600   else
601     {
602       dispatcher_start_dispatching (dispatcher, operation, cd);
603     }
604 }
605
606 static void
607 dispatcher_connection_new_channel_cb (TpConnection *connection,
608                                       const gchar *object_path,
609                                       const gchar  *channel_type,
610                                       guint handle_type,
611                                       guint handle,
612                                       gboolean suppress_handler,
613                                       gpointer user_data,
614                                       GObject *object)
615 {
616   EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
617
618   /* Empathy heavily abuses surpress handler (don't try this at home), if
619    * surpress handler is true then it is an outgoing channel, which is
620    * requested either by us or some other party (like the megaphone applet).
621    * Otherwise it's an incoming channel */
622   dispatcher_connection_new_channel (dispatcher, connection,
623     object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
624 }
625
626 static void
627 dispatcher_connection_new_channel_with_properties (EmpathyDispatcher *dispatcher,
628                                                    TpConnection *connection,
629                                                    const gchar *object_path,
630                                                    GHashTable *properties)
631 {
632   const gchar *channel_type;
633   guint handle_type;
634   guint handle;
635   gboolean requested;
636   gboolean valid;
637
638
639   channel_type = tp_asv_get_string (properties,
640     TP_IFACE_CHANNEL ".ChannelType");
641   if (channel_type == NULL)
642     {
643       g_message ("%s had an invalid ChannelType property", object_path);
644       return;
645     }
646
647   handle_type = tp_asv_get_uint32 (properties,
648     TP_IFACE_CHANNEL ".TargetHandleType", &valid);
649   if (!valid)
650     {
651       g_message ("%s had an invalid TargetHandleType property", object_path);
652       return;
653     }
654
655   handle = tp_asv_get_uint32 (properties,
656     TP_IFACE_CHANNEL ".TargetHandle", &valid);
657   if (!valid)
658     {
659       g_message ("%s had an invalid TargetHandle property", object_path);
660       return;
661     }
662
663   /* We assume there is no channel dispather, so we're the only one dispatching
664    * it. Which means that a requested channel it is outgoing one */
665   requested = tp_asv_get_boolean (properties,
666     TP_IFACE_CHANNEL ".Requested", &valid);
667   if (!valid)
668     {
669       g_message ("%s had an invalid Requested property", object_path);
670       return;
671     }
672
673   dispatcher_connection_new_channel (dispatcher, connection,
674     object_path, channel_type, handle_type, handle, properties, !requested);
675 }
676
677 static void
678 dispatcher_connection_new_channels_cb (TpConnection *connection,
679                                        const GPtrArray *channels,
680                                        gpointer user_data,
681                                        GObject *object)
682 {
683   EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
684   int i;
685
686   for (i = 0; i < channels->len ; i++)
687     {
688       GValueArray *arr = g_ptr_array_index (channels, i);
689       const gchar *object_path;
690       GHashTable *properties;
691
692       object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
693       properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
694
695       dispatcher_connection_new_channel_with_properties (dispatcher,
696         connection, object_path, properties);
697     }
698 }
699
700 static void
701 dispatcher_connection_got_all (TpProxy *proxy,
702                                GHashTable *properties,
703                                const GError *error,
704                                gpointer user_data,
705                                GObject *object)
706 {
707   EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
708   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
709   GPtrArray *channels;
710   GPtrArray *requestable_channels;
711
712   if (error) {
713     DEBUG ("Error: %s", error->message);
714     return;
715   }
716
717   channels = tp_asv_get_boxed (properties, "Channels",
718     TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST);
719
720   if (channels == NULL)
721     DEBUG ("No Channels property !?! on connection");
722   else
723     dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
724       channels, NULL, object);
725
726   requestable_channels = tp_asv_get_boxed (properties,
727     "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
728
729   if (requestable_channels == NULL)
730     DEBUG ("No RequestableChannelClasses property !?! on connection");
731   else
732     {
733       ConnectionData *cd;
734       GList *requests, *l;
735       FindChannelRequest *request;
736       GStrv retval;
737
738       cd = g_hash_table_lookup (priv->connections, proxy);
739       g_assert (cd != NULL);
740
741       cd->requestable_channels = g_boxed_copy (
742         TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
743
744       requests = g_hash_table_lookup (priv->outstanding_classes_requests,
745           proxy);
746
747       for (l = requests; l != NULL; l = l->next)
748         {
749           request = l->data;
750
751           retval = empathy_dispatcher_find_channel_class (dispatcher,
752               TP_CONNECTION (proxy), request->channel_type,
753               request->handle_type);
754           request->callback (retval, request->user_data);
755
756           free_find_channel_request (request);
757         }
758
759       g_list_free (requests);
760
761       g_hash_table_remove (priv->outstanding_classes_requests, proxy);
762     }
763 }
764
765 static void
766 dispatcher_connection_list_channels_cb (TpConnection    *connection,
767                                         const GPtrArray *channels,
768                                         const GError    *error,
769                                         gpointer         user_data,
770                                         GObject         *dispatcher)
771 {
772   int i;
773
774   if (error)
775     {
776       DEBUG ("Error: %s", error->message);
777       return;
778     }
779
780   for (i = 0; i < channels->len; i++)
781     {
782       GValueArray *values;
783
784       values = g_ptr_array_index (channels, i);
785       /* We don't have any extra info, so assume already existing channels are
786        * incoming... */
787       dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
788         connection,
789         g_value_get_boxed (g_value_array_get_nth (values, 0)),
790         g_value_get_string (g_value_array_get_nth (values, 1)),
791         g_value_get_uint (g_value_array_get_nth (values, 2)),
792         g_value_get_uint (g_value_array_get_nth (values, 3)),
793         NULL, TRUE);
794     }
795 }
796
797 static void
798 dispatcher_connection_advertise_capabilities_cb (TpConnection    *connection,
799                                                  const GPtrArray *capabilities,
800                                                  const GError    *error,
801                                                  gpointer         user_data,
802                                                  GObject         *dispatcher)
803 {
804   if (error)
805     DEBUG ("Error: %s", error->message);
806 }
807
808 static void
809 dispatcher_new_connection_cb (EmpathyAccountManager *manager,
810                               TpConnection *connection,
811                               EmpathyDispatcher *dispatcher)
812 {
813   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
814   GPtrArray   *capabilities;
815   GType        cap_type;
816   GValue       cap = {0, };
817   const gchar *remove = NULL;
818
819   if (g_hash_table_lookup (priv->connections, connection) != NULL)
820     return;
821
822   g_hash_table_insert (priv->connections, g_object_ref (connection),
823     new_connection_data ());
824
825   g_signal_connect (connection, "invalidated",
826     G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
827
828   if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
829       TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
830     {
831       tp_cli_connection_interface_requests_connect_to_new_channels (connection,
832         dispatcher_connection_new_channels_cb,
833         NULL, NULL, G_OBJECT (dispatcher), NULL);
834
835       tp_cli_dbus_properties_call_get_all (connection, -1,
836         TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
837         dispatcher_connection_got_all,
838         NULL, NULL, G_OBJECT (dispatcher));
839     }
840   else
841     {
842       tp_cli_connection_connect_to_new_channel (connection,
843         dispatcher_connection_new_channel_cb,
844         NULL, NULL, G_OBJECT (dispatcher), NULL);
845
846       tp_cli_connection_call_list_channels (connection, -1,
847         dispatcher_connection_list_channels_cb, NULL, NULL,
848         G_OBJECT (dispatcher));
849
850     }
851
852   /* Advertise VoIP capabilities */
853   capabilities = g_ptr_array_sized_new (1);
854   cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
855     G_TYPE_UINT, G_TYPE_INVALID);
856   g_value_init (&cap, cap_type);
857   g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
858   dbus_g_type_struct_set (&cap,
859         0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
860         1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
861            TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
862            TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN  |
863            TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
864   g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
865
866   tp_cli_connection_interface_capabilities_call_advertise_capabilities (
867     connection, -1, capabilities, &remove,
868     dispatcher_connection_advertise_capabilities_cb,
869     NULL, NULL, G_OBJECT (dispatcher));
870
871   g_value_unset (&cap);
872   g_ptr_array_free (capabilities, TRUE);
873 }
874
875 static GObject *
876 dispatcher_constructor (GType type,
877                         guint n_construct_params,
878                         GObjectConstructParam *construct_params)
879 {
880   GObject *retval;
881
882   if (dispatcher == NULL)
883     {
884       retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
885           (type, n_construct_params, construct_params);
886
887       dispatcher = EMPATHY_DISPATCHER (retval);
888       g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
889     }
890   else
891     {
892       retval = g_object_ref (dispatcher);
893     }
894
895   return retval;
896 }
897
898 static void
899 dispatcher_finalize (GObject *object)
900 {
901   EmpathyDispatcherPriv *priv = GET_PRIV (object);
902   GList *l;
903   GHashTableIter iter;
904   gpointer connection;
905   GList *list;
906
907   g_signal_handlers_disconnect_by_func (priv->account_manager,
908       dispatcher_new_connection_cb, object);
909
910   for (l = priv->channels; l; l = l->next)
911     {
912       g_signal_handlers_disconnect_by_func (l->data,
913           dispatcher_channel_invalidated_cb, object);
914     }
915
916   g_list_free (priv->channels);
917
918   g_hash_table_iter_init (&iter, priv->connections);
919   while (g_hash_table_iter_next (&iter, &connection, NULL))
920     {
921       g_signal_handlers_disconnect_by_func (connection,
922           dispatcher_connection_invalidated_cb, object);
923     }
924
925   g_hash_table_iter_init (&iter, priv->outstanding_classes_requests);
926   while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list))
927     {
928       g_list_foreach (list, (GFunc) free_find_channel_request, NULL);
929       g_list_free (list);
930     }
931
932   g_object_unref (priv->account_manager);
933   g_object_unref (priv->mc);
934
935   g_hash_table_destroy (priv->connections);
936   g_hash_table_destroy (priv->outstanding_classes_requests);
937 }
938
939 static void
940 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
941 {
942   GObjectClass *object_class = G_OBJECT_CLASS (klass);
943
944   object_class->finalize = dispatcher_finalize;
945   object_class->constructor = dispatcher_constructor;
946
947   signals[OBSERVE] =
948     g_signal_new ("observe",
949       G_TYPE_FROM_CLASS (klass),
950       G_SIGNAL_RUN_LAST,
951       0,
952       NULL, NULL,
953       g_cclosure_marshal_VOID__OBJECT,
954       G_TYPE_NONE,
955       1, EMPATHY_TYPE_DISPATCH_OPERATION);
956
957   signals[APPROVE] =
958     g_signal_new ("approve",
959       G_TYPE_FROM_CLASS (klass),
960       G_SIGNAL_RUN_LAST,
961       0,
962       NULL, NULL,
963       g_cclosure_marshal_VOID__OBJECT,
964       G_TYPE_NONE,
965       1, EMPATHY_TYPE_DISPATCH_OPERATION);
966
967   signals[DISPATCH] =
968     g_signal_new ("dispatch",
969       G_TYPE_FROM_CLASS (klass),
970       G_SIGNAL_RUN_LAST,
971       0,
972       NULL, NULL,
973       g_cclosure_marshal_VOID__OBJECT,
974       G_TYPE_NONE,
975       1, EMPATHY_TYPE_DISPATCH_OPERATION);
976
977   g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
978
979 }
980
981 static void
982 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
983 {
984   GList *connections, *l;
985   EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
986     EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
987
988   dispatcher->priv = priv;
989   priv->mc = empathy_mission_control_dup_singleton ();
990   priv->account_manager = empathy_account_manager_dup_singleton ();
991
992   g_signal_connect (priv->account_manager, "new-connection",
993     G_CALLBACK (dispatcher_new_connection_cb),
994     dispatcher);
995
996   priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
997     g_object_unref, (GDestroyNotify) free_connection_data);
998
999   priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash,
1000     g_direct_equal, g_object_unref, NULL);
1001
1002   priv->channels = NULL;
1003
1004   connections = empathy_account_manager_dup_connections (priv->account_manager);
1005   for (l = connections; l; l = l->next)
1006     {
1007       dispatcher_new_connection_cb (priv->account_manager, l->data, dispatcher);
1008       g_object_unref (l->data);
1009     }
1010   g_list_free (connections);
1011 }
1012
1013 EmpathyDispatcher *
1014 empathy_dispatcher_dup_singleton (void)
1015 {
1016   return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1017 }
1018
1019 static void
1020 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
1021                            DispatcherRequestData *request_data,
1022                            const GError *error)
1023 {
1024   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1025   ConnectionData *conn_data;
1026
1027   conn_data = g_hash_table_lookup (priv->connections, request_data->connection);
1028   if (request_data->cb != NULL)
1029     request_data->cb (NULL, error, request_data->user_data);
1030
1031   conn_data->outstanding_requests =
1032       g_list_remove (conn_data->outstanding_requests, request_data);
1033   free_dispatcher_request_data (request_data);
1034 }
1035
1036 static void
1037 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
1038                                              DispatcherRequestData *request_data,
1039                                              const gchar *object_path,
1040                                              GHashTable *properties,
1041                                              const GError *error)
1042 {
1043   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1044   EmpathyDispatchOperation *operation = NULL;
1045   ConnectionData *conn_data;
1046
1047   conn_data = g_hash_table_lookup (priv->connections,
1048     request_data->connection);
1049
1050   if (error)
1051     {
1052       DEBUG ("Channel request failed: %s", error->message);
1053
1054       dispatcher_request_failed (dispatcher, request_data, error);
1055
1056       goto out;
1057     }
1058
1059   operation = g_hash_table_lookup (conn_data->outstanding_channels,
1060     object_path);
1061
1062   if (operation != NULL)
1063     g_hash_table_remove (conn_data->outstanding_channels, object_path);
1064   else
1065     operation = g_hash_table_lookup (conn_data->dispatching_channels,
1066         object_path);
1067
1068   if (operation == NULL)
1069     {
1070       DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1071         object_path);
1072
1073       if (data != NULL)
1074         {
1075           operation = empathy_dispatch_operation_new_with_wrapper (
1076             request_data->connection,
1077             data->channel, request_data->contact, FALSE,
1078             data->channel_wrapper);
1079         }
1080       else
1081         {
1082           TpChannel *channel;
1083
1084           if (properties != NULL)
1085             channel = tp_channel_new_from_properties (request_data->connection,
1086               object_path, properties, NULL);
1087           else
1088             channel = tp_channel_new (request_data->connection, object_path,
1089               request_data->channel_type, request_data->handle_type,
1090               request_data->handle, NULL);
1091
1092           g_signal_connect (channel, "invalidated",
1093             G_CALLBACK (dispatcher_channel_invalidated_cb),
1094             request_data->dispatcher);
1095
1096           priv->channels = g_list_prepend (priv->channels, channel);
1097
1098           operation = empathy_dispatch_operation_new (request_data->connection,
1099              channel, request_data->contact, FALSE);
1100           g_object_unref (channel);
1101         }
1102     }
1103   else
1104     {
1105       /* Already existed set potential extra information */
1106       g_object_set (G_OBJECT (operation),
1107         "contact", request_data->contact,
1108         NULL);
1109     }
1110
1111   request_data->operation = operation;
1112
1113   /* (pre)-approve this right away as we requested it
1114    * This might cause the channel to be claimed, in which case the operation
1115    * will disappear. So ref it, and check the status before starting the
1116    * dispatching */
1117
1118   g_object_ref (operation);
1119   empathy_dispatch_operation_approve (operation);
1120
1121    if (empathy_dispatch_operation_get_status (operation) <
1122      EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1123       dispatcher_start_dispatching (request_data->dispatcher, operation,
1124           conn_data);
1125
1126   g_object_unref (operation);
1127
1128 out:
1129   dispatcher_flush_outstanding_operations (request_data->dispatcher,
1130     conn_data);
1131 }
1132
1133 static void
1134 dispatcher_request_channel_cb (TpConnection *connection,
1135                                const gchar  *object_path,
1136                                const GError *error,
1137                                gpointer user_data,
1138                                GObject *weak_object)
1139 {
1140   EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1141   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1142
1143   dispatcher_connection_new_requested_channel (dispatcher,
1144     request_data, object_path, NULL, error);
1145 }
1146
1147 static void
1148 dispatcher_request_channel (DispatcherRequestData *request_data)
1149 {
1150   tp_cli_connection_call_request_channel (request_data->connection, -1,
1151     request_data->channel_type,
1152     request_data->handle_type,
1153     request_data->handle,
1154     TRUE, dispatcher_request_channel_cb,
1155     request_data, NULL, G_OBJECT (request_data->dispatcher));
1156 }
1157
1158 void
1159 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1160                                       EmpathyDispatcherRequestCb *callback,
1161                                       gpointer user_data)
1162 {
1163   EmpathyDispatcher *dispatcher;
1164   EmpathyDispatcherPriv *priv;
1165   TpConnection *connection;
1166   ConnectionData *connection_data;
1167   DispatcherRequestData *request_data;
1168
1169   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1170
1171   dispatcher = empathy_dispatcher_dup_singleton ();
1172   priv = GET_PRIV (dispatcher);
1173
1174   connection = empathy_contact_get_connection (contact);
1175   connection_data = g_hash_table_lookup (priv->connections, connection);
1176
1177   /* The contact handle might not be known yet */
1178   request_data  = new_dispatcher_request_data (dispatcher, connection,
1179     TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1180     empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1181
1182   connection_data->outstanding_requests = g_list_prepend
1183     (connection_data->outstanding_requests, request_data);
1184
1185   dispatcher_request_channel (request_data);
1186
1187   g_object_unref (dispatcher);
1188 }
1189
1190 typedef struct
1191 {
1192   EmpathyDispatcher *dispatcher;
1193   EmpathyDispatcherRequestCb *callback;
1194   gpointer user_data;
1195 } ChatWithContactIdData;
1196
1197 static void
1198 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1199                                     EmpathyContact          *contact,
1200                                     const GError            *error,
1201                                     gpointer                 user_data,
1202                                     GObject                 *weak_object)
1203 {
1204   ChatWithContactIdData *data = user_data;
1205
1206   if (error)
1207     {
1208       /* FIXME: Should call data->callback with the error */
1209       DEBUG ("Error: %s", error->message);
1210     }
1211   else
1212     {
1213       empathy_dispatcher_chat_with_contact (contact, data->callback, data->user_data);
1214     }
1215
1216   g_object_unref (data->dispatcher);
1217   g_slice_free (ChatWithContactIdData, data);
1218 }
1219
1220 void
1221 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1222                                          const gchar *contact_id,
1223                                          EmpathyDispatcherRequestCb *callback,
1224                                          gpointer user_data)
1225 {
1226   EmpathyDispatcher *dispatcher;
1227   EmpathyTpContactFactory *factory;
1228   ChatWithContactIdData *data;
1229
1230   g_return_if_fail (TP_IS_CONNECTION (connection));
1231   g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1232
1233   dispatcher = empathy_dispatcher_dup_singleton ();
1234   factory = empathy_tp_contact_factory_dup_singleton (connection);
1235   data = g_slice_new0 (ChatWithContactIdData);
1236   data->dispatcher = dispatcher;
1237   data->callback = callback;
1238   data->user_data = user_data;
1239   empathy_tp_contact_factory_get_from_id (factory, contact_id,
1240       dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1241
1242   g_object_unref (factory);
1243 }
1244
1245 static void
1246 dispatcher_request_handles_cb (TpConnection *connection,
1247                                const GArray *handles,
1248                                const GError *error,
1249                                gpointer user_data,
1250                                GObject *object)
1251 {
1252   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1253
1254   if (error != NULL)
1255     {
1256       EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1257       EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1258       ConnectionData *cd;
1259
1260       cd = g_hash_table_lookup (priv->connections, request_data->connection);
1261
1262       if (request_data->cb)
1263         request_data->cb (NULL, error, request_data->user_data);
1264
1265       cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1266         request_data);
1267
1268       free_dispatcher_request_data (request_data);
1269
1270       dispatcher_flush_outstanding_operations (dispatcher, cd);
1271       return;
1272     }
1273
1274   request_data->handle = g_array_index (handles, guint, 0);
1275   dispatcher_request_channel (request_data);
1276 }
1277
1278 void
1279 empathy_dispatcher_join_muc (TpConnection *connection,
1280                              const gchar *roomname,
1281                              EmpathyDispatcherRequestCb *callback,
1282                              gpointer user_data)
1283 {
1284   EmpathyDispatcher *dispatcher;
1285   EmpathyDispatcherPriv *priv;
1286   DispatcherRequestData *request_data;
1287   ConnectionData *connection_data;
1288   const gchar *names[] = { roomname, NULL };
1289
1290   g_return_if_fail (TP_IS_CONNECTION (connection));
1291   g_return_if_fail (!EMP_STR_EMPTY (roomname));
1292
1293   dispatcher = empathy_dispatcher_dup_singleton ();
1294   priv = GET_PRIV (dispatcher);
1295
1296   connection_data = g_hash_table_lookup (priv->connections, connection);
1297
1298   /* Don't know the room handle yet */
1299   request_data  = new_dispatcher_request_data (dispatcher, connection,
1300     TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1301     NULL, callback, user_data);
1302
1303   connection_data->outstanding_requests = g_list_prepend
1304     (connection_data->outstanding_requests, request_data);
1305
1306   tp_cli_connection_call_request_handles (connection, -1,
1307     TP_HANDLE_TYPE_ROOM, names,
1308     dispatcher_request_handles_cb, request_data, NULL,
1309     G_OBJECT (dispatcher));
1310
1311   g_object_unref (dispatcher);
1312 }
1313
1314 static void
1315 dispatcher_create_channel_cb (TpConnection *connect,
1316                               const gchar *object_path,
1317                               GHashTable *properties,
1318                               const GError *error,
1319                               gpointer user_data,
1320                               GObject *weak_object)
1321 {
1322   EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1323   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1324
1325   dispatcher_connection_new_requested_channel (dispatcher,
1326     request_data, object_path, properties, error);
1327 }
1328
1329 void
1330 empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher,
1331                                    TpConnection *connection,
1332                                    GHashTable *request,
1333                                    EmpathyDispatcherRequestCb *callback,
1334                                    gpointer user_data)
1335 {
1336   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1337   ConnectionData *connection_data;
1338   DispatcherRequestData *request_data;
1339   const gchar *channel_type;
1340   guint handle_type;
1341   guint handle;
1342   gboolean valid;
1343
1344   g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1345   g_return_if_fail (TP_IS_CONNECTION (connection));
1346   g_return_if_fail (request != NULL);
1347
1348   connection_data = g_hash_table_lookup (priv->connections, connection);
1349   g_assert (connection_data != NULL);
1350
1351   channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1352
1353   handle_type = tp_asv_get_uint32 (request,
1354     TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1355   if (!valid)
1356     handle_type = TP_UNKNOWN_HANDLE_TYPE;
1357
1358   handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1359
1360   request_data  = new_dispatcher_request_data (dispatcher, connection,
1361     channel_type, handle_type, handle, request,
1362     NULL, callback, user_data);
1363
1364   connection_data->outstanding_requests = g_list_prepend
1365     (connection_data->outstanding_requests, request_data);
1366
1367   tp_cli_connection_interface_requests_call_create_channel (
1368     request_data->connection, -1,
1369     request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1370     G_OBJECT (request_data->dispatcher));
1371 }
1372
1373 GStrv
1374 empathy_dispatcher_find_channel_class (EmpathyDispatcher *dispatcher,
1375                                        TpConnection *connection,
1376                                        const gchar *channel_type,
1377                                        guint handle_type)
1378 {
1379   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1380   ConnectionData *cd;
1381   int i;
1382   GPtrArray *classes;
1383
1384   g_return_val_if_fail (channel_type != NULL, NULL);
1385   g_return_val_if_fail (handle_type != 0, NULL);
1386
1387   cd = g_hash_table_lookup (priv->connections, connection);
1388
1389   if (cd == NULL)
1390     return NULL;
1391
1392   classes = cd->requestable_channels;
1393   if (classes == NULL)
1394     return NULL;
1395
1396   for (i = 0; i < classes->len; i++)
1397     {
1398       GValueArray *class;
1399       GValue *fixed;
1400       GValue *allowed;
1401       GHashTable *fprops;
1402       const gchar *c_type;
1403       guint32 h_type;
1404       gboolean valid;
1405
1406       class = g_ptr_array_index (classes, i);
1407       fixed = g_value_array_get_nth (class, 0);
1408
1409       fprops = g_value_get_boxed (fixed);
1410       c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1411
1412       if (tp_strdiff (channel_type, c_type))
1413         continue;
1414
1415       h_type = tp_asv_get_uint32 (fprops,
1416         TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1417
1418       if (!valid || handle_type != h_type)
1419         continue;
1420
1421       allowed = g_value_array_get_nth (class, 1);
1422
1423       return g_value_get_boxed (allowed);
1424     }
1425
1426   return NULL;
1427 }
1428
1429 static gboolean
1430 find_channel_class_idle_cb (gpointer user_data)
1431 {
1432   GStrv retval;
1433   GList *requests;
1434   FindChannelRequest *request = user_data;
1435   ConnectionData *cd;
1436   gboolean is_ready = TRUE;
1437   EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher);
1438
1439   cd = g_hash_table_lookup (priv->connections, request->connection);
1440
1441   if (cd == NULL)
1442     is_ready = FALSE;
1443   else if (cd->requestable_channels == NULL)
1444     is_ready = FALSE;
1445
1446   if (is_ready)
1447     {
1448       retval = empathy_dispatcher_find_channel_class (request->dispatcher,
1449           request->connection, request->channel_type, request->handle_type);
1450
1451       request->callback (retval, request->user_data);
1452       free_find_channel_request (request);
1453       return FALSE;
1454     }
1455
1456   requests = g_hash_table_lookup (priv->outstanding_classes_requests,
1457       request->connection);
1458   requests = g_list_prepend (requests, request);
1459
1460   g_hash_table_insert (priv->outstanding_classes_requests,
1461       request->connection, requests);
1462
1463   return FALSE;
1464 }
1465
1466 void
1467 empathy_dispatcher_find_channel_class_async (EmpathyDispatcher *dispatcher,
1468                                              TpConnection *connection,
1469                                              const gchar *channel_type,
1470                                              guint handle_type,
1471                                              EmpathyDispatcherFindChannelClassCb callback,
1472                                              gpointer user_data)
1473 {
1474   FindChannelRequest *request;
1475
1476   g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1477   g_return_if_fail (TP_IS_CONNECTION (connection));
1478   g_return_if_fail (channel_type != NULL);
1479   g_return_if_fail (handle_type != 0);
1480
1481   /* append another request for this connection */
1482   request = g_slice_new0 (FindChannelRequest);
1483   request->dispatcher = dispatcher;
1484   request->channel_type = g_strdup (channel_type);
1485   request->handle_type = handle_type;
1486   request->connection = connection;
1487   request->callback = callback;
1488   request->user_data = user_data;
1489
1490   g_idle_add (find_channel_class_idle_cb, request);
1491 }