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