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