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