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