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