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