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