empathy-dispatcher: deal properly with NULL channel_wrapper
[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, EMP_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   /* FIXME: Capabilities is leaked */
834   capabilities = g_ptr_array_sized_new (1);
835   cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
836     G_TYPE_UINT, G_TYPE_INVALID);
837   g_value_init (&cap, cap_type);
838   g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
839   dbus_g_type_struct_set (&cap,
840         0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
841         1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
842            TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
843            TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN  |
844            TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
845   g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
846
847   tp_cli_connection_interface_capabilities_call_advertise_capabilities (
848     connection, -1, capabilities, &remove,
849     dispatcher_connection_advertise_capabilities_cb,
850     NULL, NULL, G_OBJECT (dispatcher));
851 }
852
853 static void
854 dispatcher_update_account (EmpathyDispatcher *dispatcher,
855                            McAccount *account)
856 {
857   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
858   TpConnection *connection;
859
860   connection = g_hash_table_lookup (priv->accounts, account);
861   if (connection != NULL)
862     return;
863
864   connection = mission_control_get_tpconnection (priv->mc, account, NULL);
865   if (connection == NULL)
866     return;
867
868   g_hash_table_insert (priv->connections, g_object_ref (connection),
869     new_connection_data (account));
870
871   g_hash_table_insert (priv->accounts, g_object_ref (account),
872     g_object_ref (connection));
873
874   tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
875     dispatcher);
876
877   g_object_unref (connection);
878 }
879
880 static void
881 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
882                                   McAccount *account,
883                                   TpConnectionStatusReason reason,
884                                   TpConnectionStatus status,
885                                   TpConnectionStatus previous,
886                                   EmpathyDispatcher *dispatcher)
887 {
888   dispatcher_update_account (dispatcher, account);
889 }
890
891 static GObject*
892 dispatcher_constructor (GType type,
893                         guint n_construct_params,
894                         GObjectConstructParam *construct_params)
895 {
896   GObject *retval;
897
898   if (dispatcher == NULL)
899     {
900       retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
901           (type, n_construct_params, construct_params);
902
903       dispatcher = EMPATHY_DISPATCHER (retval);
904       g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
905     }
906   else
907     {
908       retval = g_object_ref (dispatcher);
909     }
910
911   return retval;
912 }
913
914 static void
915 dispatcher_finalize (GObject *object)
916 {
917   EmpathyDispatcherPriv *priv = GET_PRIV (object);
918   GList *l;
919   GHashTableIter iter;
920   gpointer connection;
921
922   g_signal_handlers_disconnect_by_func (priv->account_manager,
923       dispatcher_account_connection_cb, object);
924
925   for (l = priv->channels; l; l = l->next)
926     {
927       g_signal_handlers_disconnect_by_func (l->data,
928           dispatcher_channel_invalidated_cb, object);
929     }
930
931   g_list_free (priv->channels);
932
933   g_hash_table_iter_init (&iter, priv->connections);
934   while (g_hash_table_iter_next (&iter, &connection, NULL))
935     {
936       g_signal_handlers_disconnect_by_func (connection,
937           dispatcher_connection_invalidated_cb, object);
938     }
939
940   g_object_unref (priv->account_manager);
941   g_object_unref (priv->mc);
942
943   g_hash_table_destroy (priv->accounts);
944   g_hash_table_destroy (priv->connections);
945 }
946
947 static void
948 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
949 {
950   GObjectClass *object_class = G_OBJECT_CLASS (klass);
951
952   object_class->finalize = dispatcher_finalize;
953   object_class->constructor = dispatcher_constructor;
954
955   signals[OBSERVE] =
956     g_signal_new ("observe",
957       G_TYPE_FROM_CLASS (klass),
958       G_SIGNAL_RUN_LAST,
959       0,
960       NULL, NULL,
961       g_cclosure_marshal_VOID__OBJECT,
962       G_TYPE_NONE,
963       1, EMPATHY_TYPE_DISPATCH_OPERATION);
964
965   signals[APPROVE] =
966     g_signal_new ("approve",
967       G_TYPE_FROM_CLASS (klass),
968       G_SIGNAL_RUN_LAST,
969       0,
970       NULL, NULL,
971       g_cclosure_marshal_VOID__OBJECT,
972       G_TYPE_NONE,
973       1, EMPATHY_TYPE_DISPATCH_OPERATION);
974
975   signals[DISPATCH] =
976     g_signal_new ("dispatch",
977       G_TYPE_FROM_CLASS (klass),
978       G_SIGNAL_RUN_LAST,
979       0,
980       NULL, NULL,
981       g_cclosure_marshal_VOID__OBJECT,
982       G_TYPE_NONE,
983       1, EMPATHY_TYPE_DISPATCH_OPERATION);
984
985   g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
986
987 }
988
989 static void
990 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
991 {
992   GList *accounts, *l;
993   EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
994     EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
995
996   dispatcher->priv = priv;
997   priv->mc = empathy_mission_control_dup_singleton ();
998   priv->account_manager = empathy_account_manager_dup_singleton ();
999
1000   g_signal_connect (priv->account_manager,
1001     "account-connection-changed",
1002     G_CALLBACK (dispatcher_account_connection_cb),
1003     dispatcher);
1004
1005   priv->accounts = g_hash_table_new_full (empathy_account_hash,
1006         empathy_account_equal, g_object_unref, g_object_unref);
1007
1008   priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1009     g_object_unref, (GDestroyNotify) free_connection_data);
1010
1011   priv->channels = NULL;
1012
1013   accounts = mc_accounts_list_by_enabled (TRUE);
1014
1015   for (l = accounts; l; l = l->next)
1016     {
1017       dispatcher_update_account (dispatcher, l->data);
1018       g_object_unref (l->data);
1019     }
1020   g_list_free (accounts);
1021 }
1022
1023 EmpathyDispatcher *
1024 empathy_dispatcher_dup_singleton (void)
1025 {
1026   return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1027 }
1028
1029 static void
1030 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
1031                            DispatcherRequestData *request_data,
1032                            const GError *error)
1033 {
1034   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1035   ConnectionData *conn_data;
1036
1037   conn_data = g_hash_table_lookup (priv->connections, request_data->connection);
1038   if (request_data->cb != NULL)
1039     request_data->cb (NULL, error, request_data->user_data);
1040
1041   conn_data->outstanding_requests =
1042       g_list_remove (conn_data->outstanding_requests, request_data);
1043   free_dispatcher_request_data (request_data);
1044 }
1045
1046 static void
1047 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
1048                                              DispatcherRequestData *request_data,
1049                                              const gchar *object_path,
1050                                              GHashTable *properties,
1051                                              const GError *error)
1052 {
1053   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1054   EmpathyDispatchOperation *operation = NULL;
1055   ConnectionData *conn_data;
1056
1057   conn_data = g_hash_table_lookup (priv->connections,
1058     request_data->connection);
1059
1060   if (error)
1061     {
1062       DEBUG ("Channel request failed: %s", error->message);
1063
1064       dispatcher_request_failed (dispatcher, request_data, error);
1065
1066       goto out;
1067     }
1068
1069   operation = g_hash_table_lookup (conn_data->outstanding_channels,
1070     object_path);
1071
1072   if (operation != NULL)
1073     g_hash_table_remove (conn_data->outstanding_channels, object_path);
1074   else
1075     operation = g_hash_table_lookup (conn_data->dispatching_channels,
1076         object_path);
1077
1078   if (operation == NULL)
1079     {
1080       DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1081         object_path);
1082
1083       if (data != NULL)
1084         {
1085           operation = empathy_dispatch_operation_new_with_wrapper (
1086             request_data->connection,
1087             data->channel, request_data->contact, FALSE,
1088             data->channel_wrapper);
1089         }
1090       else
1091         {
1092           TpChannel *channel;
1093
1094           if (properties != NULL)
1095             channel = tp_channel_new_from_properties (request_data->connection,
1096               object_path, properties, NULL);
1097           else
1098             channel = tp_channel_new (request_data->connection, object_path,
1099               request_data->channel_type, request_data->handle_type,
1100               request_data->handle, NULL);
1101
1102           g_signal_connect (channel, "invalidated",
1103             G_CALLBACK (dispatcher_channel_invalidated_cb),
1104             request_data->dispatcher);
1105
1106           priv->channels = g_list_prepend (priv->channels, channel);
1107
1108           operation = empathy_dispatch_operation_new (request_data->connection,
1109              channel, request_data->contact, FALSE);
1110           g_object_unref (channel);
1111         }
1112     }
1113   else
1114     {
1115       /* Already existed set potential extra information */
1116       g_object_set (G_OBJECT (operation),
1117         "contact", request_data->contact,
1118         NULL);
1119     }
1120
1121   request_data->operation = operation;
1122
1123   /* (pre)-approve this right away as we requested it */
1124   empathy_dispatch_operation_approve (operation);
1125
1126   dispatcher_start_dispatching (request_data->dispatcher, operation,
1127         conn_data);
1128 out:
1129   dispatcher_flush_outstanding_operations (request_data->dispatcher,
1130     conn_data);
1131 }
1132
1133 static void
1134 dispatcher_request_channel_cb (TpConnection *connection,
1135                                const gchar  *object_path,
1136                                const GError *error,
1137                                gpointer user_data,
1138                                GObject *weak_object)
1139 {
1140   EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1141   DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1142
1143   dispatcher_connection_new_requested_channel (dispatcher,
1144     request_data, object_path, NULL, error);
1145 }
1146
1147 static void
1148 dispatcher_request_channel (DispatcherRequestData *request_data)
1149 {
1150   tp_cli_connection_call_request_channel (request_data->connection, -1,
1151     request_data->channel_type,
1152     request_data->handle_type,
1153     request_data->handle,
1154     TRUE, dispatcher_request_channel_cb,
1155     request_data, NULL, G_OBJECT (request_data->dispatcher));
1156 }
1157
1158 void
1159 empathy_dispatcher_call_with_contact (EmpathyContact *contact,
1160                                       EmpathyDispatcherRequestCb *callback,
1161                                       gpointer user_data)
1162 {
1163   EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1164   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1165   McAccount *account;
1166   TpConnection *connection;
1167   ConnectionData *cd;
1168   DispatcherRequestData *request_data;
1169
1170   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1171
1172   account = empathy_contact_get_account (contact);
1173   connection = g_hash_table_lookup (priv->accounts, account);
1174
1175   g_assert (connection != NULL);
1176   cd = g_hash_table_lookup (priv->connections, connection);
1177   request_data  = new_dispatcher_request_data (dispatcher, connection,
1178     TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1179     contact, callback, user_data);
1180
1181   cd->outstanding_requests = g_list_prepend
1182     (cd->outstanding_requests, request_data);
1183
1184   dispatcher_request_channel (request_data);
1185
1186   g_object_unref (dispatcher);
1187 }
1188
1189 static void
1190 dispatcher_chat_with_contact_cb (EmpathyContact *contact,
1191                                  const GError *error,
1192                                  gpointer user_data,
1193                                  GObject *object)
1194 {
1195   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1196
1197   request_data->handle = empathy_contact_get_handle (contact);
1198
1199   dispatcher_request_channel (request_data);
1200 }
1201
1202 void
1203 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1204                                       EmpathyDispatcherRequestCb *callback,
1205                                       gpointer user_data)
1206 {
1207   EmpathyDispatcher *dispatcher;
1208   EmpathyDispatcherPriv *priv;
1209   McAccount *account;
1210   TpConnection *connection;
1211   ConnectionData *connection_data;
1212   DispatcherRequestData *request_data;
1213
1214   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1215
1216   dispatcher = empathy_dispatcher_dup_singleton();
1217   priv = GET_PRIV (dispatcher);
1218
1219   account = empathy_contact_get_account (contact);
1220   connection = g_hash_table_lookup (priv->accounts, account);
1221   connection_data = g_hash_table_lookup (priv->connections, connection);
1222
1223   /* The contact handle might not be known yet */
1224   request_data  = new_dispatcher_request_data (dispatcher, connection,
1225     TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1226     contact, callback, user_data);
1227
1228   connection_data->outstanding_requests = g_list_prepend
1229     (connection_data->outstanding_requests, request_data);
1230
1231   empathy_contact_call_when_ready (contact,
1232     EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1233     request_data, NULL, G_OBJECT (dispatcher));
1234
1235   g_object_unref (dispatcher);
1236 }
1237
1238 void
1239 empathy_dispatcher_chat_with_contact_id (McAccount *account,
1240                                          const gchar *contact_id,
1241                                          EmpathyDispatcherRequestCb *callback,
1242                                          gpointer user_data)
1243 {
1244   EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
1245   EmpathyContactFactory *factory;
1246   EmpathyContact        *contact;
1247
1248   g_return_if_fail (MC_IS_ACCOUNT (account));
1249   g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1250
1251   factory = empathy_contact_factory_dup_singleton ();
1252   contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1253
1254   empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1255
1256   g_object_unref (contact);
1257   g_object_unref (factory);
1258   g_object_unref (dispatcher);
1259 }
1260
1261 static void
1262 dispatcher_request_handles_cb (TpConnection *connection,
1263                                const GArray *handles,
1264                                const GError *error,
1265                                gpointer user_data,
1266                                GObject *object)
1267 {
1268   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1269
1270   if (error != NULL)
1271     {
1272       EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1273       EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1274       ConnectionData *cd;
1275
1276       cd = g_hash_table_lookup (priv->connections, request_data->connection);
1277
1278       if (request_data->cb)
1279         request_data->cb (NULL, error, request_data->user_data);
1280
1281       cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1282         request_data);
1283
1284       free_dispatcher_request_data (request_data);
1285
1286       dispatcher_flush_outstanding_operations (dispatcher, cd);
1287       return;
1288     }
1289
1290   request_data->handle = g_array_index (handles, guint, 0);
1291   dispatcher_request_channel (request_data);
1292 }
1293
1294 void
1295 empathy_dispatcher_join_muc (McAccount *account,
1296                              const gchar *roomname,
1297                              EmpathyDispatcherRequestCb *callback,
1298                              gpointer user_data)
1299 {
1300   EmpathyDispatcher *dispatcher;
1301   EmpathyDispatcherPriv *priv;
1302   DispatcherRequestData *request_data;
1303   TpConnection *connection;
1304   ConnectionData *connection_data;
1305   const gchar *names[] = { roomname, NULL };
1306
1307   g_return_if_fail (MC_IS_ACCOUNT (account));
1308   g_return_if_fail (!EMP_STR_EMPTY (roomname));
1309
1310   dispatcher = empathy_dispatcher_dup_singleton();
1311   priv = GET_PRIV (dispatcher);
1312
1313   connection = g_hash_table_lookup (priv->accounts, account);
1314   connection_data = g_hash_table_lookup (priv->connections, connection);
1315
1316
1317   /* Don't know the room handle yet */
1318   request_data  = new_dispatcher_request_data (dispatcher, connection,
1319     TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1320     NULL, callback, user_data);
1321
1322   connection_data->outstanding_requests = g_list_prepend
1323     (connection_data->outstanding_requests, request_data);
1324
1325   tp_cli_connection_call_request_handles (connection, -1,
1326     TP_HANDLE_TYPE_ROOM, names,
1327     dispatcher_request_handles_cb, request_data, NULL,
1328     G_OBJECT (dispatcher));
1329
1330   g_object_unref (dispatcher);
1331 }
1332
1333 static void
1334 dispatcher_create_channel_cb (TpConnection *connect,
1335                               const gchar *object_path,
1336                               GHashTable *properties,
1337                               const GError *error,
1338                               gpointer user_data,
1339                               GObject *weak_object)
1340 {
1341   EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1342   DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1343
1344   dispatcher_connection_new_requested_channel (dispatcher,
1345     request_data, object_path, properties, error);
1346 }
1347
1348 void
1349 empathy_dispatcher_create_channel (EmpathyDispatcher *dispatcher,
1350                                    McAccount *account,
1351                                    GHashTable *request,
1352                                    EmpathyDispatcherRequestCb *callback,
1353                                    gpointer user_data)
1354 {
1355   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1356   ConnectionData *connection_data;
1357   DispatcherRequestData *request_data;
1358   const gchar *channel_type;
1359   guint handle_type;
1360   guint handle;
1361   gboolean valid;
1362   TpConnection *connection;
1363
1364   g_return_if_fail (EMPATHY_IS_DISPATCHER (dispatcher));
1365   g_return_if_fail (MC_IS_ACCOUNT (account));
1366   g_return_if_fail (request != NULL);
1367
1368   connection = g_hash_table_lookup (priv->accounts, account);
1369   g_assert (connection != NULL);
1370
1371   connection_data = g_hash_table_lookup (priv->connections, connection);
1372   g_assert (connection_data != NULL);
1373
1374   channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1375
1376   handle_type = tp_asv_get_uint32 (request,
1377     TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1378   if (!valid)
1379     handle_type = TP_UNKNOWN_HANDLE_TYPE;
1380
1381   handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1382
1383   request_data  = new_dispatcher_request_data (dispatcher, connection,
1384     channel_type, handle_type, handle, request,
1385     NULL, callback, user_data);
1386
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
1396 static void
1397 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1398                                            const GError *error,
1399                                            gpointer user_data,
1400                                            GObject *object)
1401 {
1402   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1403   GValue *target_handle;
1404
1405   g_assert (request_data->request);
1406
1407   if (error != NULL)
1408     {
1409       dispatcher_request_failed (request_data->dispatcher,
1410         request_data, error);
1411       return;
1412     }
1413
1414   request_data->handle = empathy_contact_get_handle (contact);
1415
1416   target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1417   g_value_set_uint (target_handle, request_data->handle);
1418   g_hash_table_insert (request_data->request,
1419     TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1420
1421   tp_cli_connection_interface_requests_call_create_channel (
1422     request_data->connection, -1,
1423     request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1424     G_OBJECT (request_data->dispatcher));
1425 }
1426
1427 static void
1428 dispatcher_send_file_connection_ready_cb (TpConnection *connection,
1429                                           const GError *error,
1430                                           gpointer user_data)
1431 {
1432   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1433
1434   if (error !=  NULL)
1435     {
1436       dispatcher_request_failed (request_data->dispatcher,
1437           request_data, error);
1438       return;
1439     }
1440
1441   empathy_contact_call_when_ready (request_data->contact,
1442     EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1443     request_data, NULL, G_OBJECT (request_data->dispatcher));
1444 }
1445
1446 void
1447 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1448                                          const gchar *filename,
1449                                          guint64 size,
1450                                          guint64 date,
1451                                          const gchar *content_type,
1452                                          EmpathyDispatcherRequestCb *callback,
1453                                          gpointer user_data)
1454 {
1455   EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1456   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1457   McAccount *account = empathy_contact_get_account (contact);
1458   TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1459   ConnectionData *connection_data =
1460     g_hash_table_lookup (priv->connections, connection);
1461   DispatcherRequestData *request_data;
1462   GValue *value;
1463   GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1464       (GDestroyNotify) tp_g_value_slice_free);
1465
1466   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1467   g_return_if_fail (!EMP_STR_EMPTY (filename));
1468   g_return_if_fail (!EMP_STR_EMPTY (content_type));
1469
1470   /* org.freedesktop.Telepathy.Channel.ChannelType */
1471   value = tp_g_value_slice_new (G_TYPE_STRING);
1472   g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1473   g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1474
1475   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1476   value = tp_g_value_slice_new (G_TYPE_UINT);
1477   g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1478   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1479
1480   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1481   value = tp_g_value_slice_new (G_TYPE_STRING);
1482   g_value_set_string (value, content_type);
1483   g_hash_table_insert (request,
1484     EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1485
1486   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1487   value = tp_g_value_slice_new (G_TYPE_STRING);
1488   g_value_set_string (value, filename);
1489   g_hash_table_insert (request,
1490     EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1491
1492   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1493   value = tp_g_value_slice_new (G_TYPE_UINT64);
1494   g_value_set_uint64 (value, size);
1495   g_hash_table_insert (request,
1496     EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1497
1498   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1499   value = tp_g_value_slice_new (G_TYPE_UINT64);
1500   g_value_set_uint64 (value, date);
1501   g_hash_table_insert (request,
1502     EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1503
1504
1505   /* The contact handle might not be known yet */
1506   request_data  = new_dispatcher_request_data (dispatcher, connection,
1507     EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1508     contact, callback, user_data);
1509   connection_data->outstanding_requests = g_list_prepend
1510     (connection_data->outstanding_requests, request_data);
1511
1512   tp_connection_call_when_ready (connection,
1513       dispatcher_send_file_connection_ready_cb, (gpointer) request_data);
1514
1515   g_object_unref (dispatcher);
1516 }
1517
1518 GStrv
1519 empathy_dispatcher_find_channel_class (EmpathyDispatcher *dispatcher,
1520                                        McAccount *account,
1521                                        const gchar *channel_type,
1522                                        guint handle_type)
1523 {
1524   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1525   ConnectionData *cd;
1526   TpConnection *connection;
1527   int i;
1528   GPtrArray *classes;
1529
1530   g_return_val_if_fail (channel_type != NULL, NULL);
1531   g_return_val_if_fail (handle_type != 0, NULL);
1532
1533   connection = g_hash_table_lookup (priv->accounts, account);
1534
1535   if (connection == NULL)
1536     return NULL;
1537
1538   cd = g_hash_table_lookup (priv->connections, connection);
1539
1540   if (cd == NULL)
1541     return NULL;
1542
1543
1544   classes = cd->requestable_channels;
1545   if (classes == NULL)
1546     return NULL;
1547
1548   for (i = 0; i < classes->len; i++)
1549     {
1550       GValueArray *class;
1551       GValue *fixed;
1552       GValue *allowed;
1553       GHashTable *fprops;
1554       const gchar *c_type;
1555       guint32 h_type;
1556       gboolean valid;
1557
1558       class = g_ptr_array_index (classes, i);
1559       fixed = g_value_array_get_nth (class, 0);
1560
1561       fprops = g_value_get_boxed (fixed);
1562       c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1563
1564       if (tp_strdiff (channel_type, c_type))
1565         continue;
1566
1567       h_type = tp_asv_get_uint32 (fprops,
1568         TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1569
1570       if (!valid || handle_type != h_type)
1571         continue;
1572
1573       allowed = g_value_array_get_nth (class, 1);
1574
1575       return g_value_get_boxed (allowed);
1576     }
1577
1578   return NULL;
1579 }
1580