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