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