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