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