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