]> git.0d.be Git - empathy.git/blob - libempathy/empathy-dispatcher.c
Remove dead code
[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 static void
790 dispatcher_finalize (GObject *object)
791 {
792   EmpathyDispatcherPriv *priv = GET_PRIV (object);
793
794   g_signal_handlers_disconnect_by_func (priv->account_manager,
795       dispatcher_account_connection_cb, object);
796
797   g_object_unref (priv->account_manager);
798   g_object_unref (priv->mc);
799
800   g_hash_table_destroy (priv->connections);
801
802   g_object_unref (priv->chatroom_mgr);
803 }
804
805 static void
806 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
807 {
808   GObjectClass *object_class = G_OBJECT_CLASS (klass);
809
810   object_class->finalize = dispatcher_finalize;
811
812   signals[OBSERVE] =
813     g_signal_new ("observe",
814       G_TYPE_FROM_CLASS (klass),
815       G_SIGNAL_RUN_LAST,
816       0,
817       NULL, NULL,
818       g_cclosure_marshal_VOID__OBJECT,
819       G_TYPE_NONE,
820       1, EMPATHY_TYPE_DISPATCH_OPERATION);
821
822   signals[APPROVE] =
823     g_signal_new ("approve",
824       G_TYPE_FROM_CLASS (klass),
825       G_SIGNAL_RUN_LAST,
826       0,
827       NULL, NULL,
828       g_cclosure_marshal_VOID__OBJECT,
829       G_TYPE_NONE,
830       1, EMPATHY_TYPE_DISPATCH_OPERATION);
831
832   signals[DISPATCH] =
833     g_signal_new ("dispatch",
834       G_TYPE_FROM_CLASS (klass),
835       G_SIGNAL_RUN_LAST,
836       0,
837       NULL, NULL,
838       g_cclosure_marshal_VOID__OBJECT,
839       G_TYPE_NONE,
840       1, EMPATHY_TYPE_DISPATCH_OPERATION);
841
842   g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
843
844 }
845
846 static void
847 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
848 {
849   GList *accounts, *l;
850   EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
851     EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
852
853   dispatcher->priv = priv;
854   priv->mc = empathy_mission_control_new ();
855   priv->account_manager = empathy_account_manager_dup_singleton ();
856
857   g_signal_connect (priv->account_manager,
858     "account-connection-changed",
859     G_CALLBACK (dispatcher_account_connection_cb),
860     dispatcher);
861
862   priv->accounts = g_hash_table_new_full (empathy_account_hash,
863         empathy_account_equal, g_object_unref, g_object_unref);
864
865   priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
866     g_object_unref, (GDestroyNotify) free_connection_data);
867
868   accounts = mc_accounts_list_by_enabled (TRUE);
869
870   for (l = accounts; l; l = l->next) {
871     dispatcher_update_account (dispatcher, l->data);
872     g_object_unref (l->data);
873   }
874   g_list_free (accounts);
875
876   /* FIXME thisshould probably be created by another object.. */
877   priv->chatroom_mgr = empathy_chatroom_manager_new (NULL);
878   empathy_chatroom_manager_observe (priv->chatroom_mgr, dispatcher);
879 }
880
881 EmpathyDispatcher *
882 empathy_get_dispatcher (void)
883 {
884   if (!dispatcher) {
885     dispatcher = g_object_new (EMPATHY_TYPE_DISPATCHER, NULL);
886     g_object_add_weak_pointer (G_OBJECT (dispatcher), (gpointer) &dispatcher);
887   } else {
888     g_object_ref (dispatcher);
889   }
890
891   return dispatcher;
892 }
893
894 static void
895 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
896   DispatcherRequestData *request_data, const gchar *object_path,
897   GHashTable *properties, const GError *error)
898 {
899   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
900   EmpathyDispatchOperation *operation = NULL;
901   ConnectionData *conn_data;
902
903   conn_data = g_hash_table_lookup (priv->connections,
904     request_data->connection);
905
906   if (error)
907     {
908       DEBUG ("Channel request failed: %s", error->message);
909
910       if (request_data->cb != NULL)
911           request_data->cb (NULL, error, request_data->user_data);
912
913       conn_data->outstanding_requests =
914         g_list_remove (conn_data->outstanding_requests, request_data);
915       free_dispatcher_request_data (request_data);
916
917       goto out;
918   }
919
920   operation = g_hash_table_lookup (conn_data->outstanding_channels,
921     object_path);
922
923   if (operation != NULL)
924     g_hash_table_remove (conn_data->outstanding_channels, object_path);
925   else
926     operation = g_hash_table_lookup (conn_data->dispatching_channels,
927         object_path);
928
929   if (operation == NULL)
930     {
931       DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
932         object_path);
933
934       if (data != NULL)
935         {
936           operation = empathy_dispatch_operation_new_with_wrapper (
937             request_data->connection,
938             data->channel, request_data->contact, FALSE,
939             data->channel_wrapper);
940         }
941       else
942         {
943           TpChannel *channel;
944
945           if (properties != NULL)
946             channel = tp_channel_new_from_properties (request_data->connection,
947               object_path, properties, NULL);
948           else
949             channel = tp_channel_new (request_data->connection, object_path,
950               request_data->channel_type, request_data->handle_type,
951               request_data->handle, NULL);
952
953           g_signal_connect (channel, "invalidated",
954             G_CALLBACK (dispatcher_channel_invalidated_cb),
955             request_data->dispatcher);
956
957           operation = empathy_dispatch_operation_new (request_data->connection,
958              channel, request_data->contact, FALSE);
959           g_object_unref (channel);
960         }
961     }
962   else
963     {
964       /* Already existed set potential extra information */
965       g_object_set (G_OBJECT (operation),
966         "contact", request_data->contact,
967         NULL);
968     }
969
970   request_data->operation = operation;
971
972   /* (pre)-approve this right away as we requested it */
973   empathy_dispatch_operation_approve (operation);
974
975   dispatcher_start_dispatching (request_data->dispatcher, operation,
976         conn_data);
977 out:
978   dispatcher_flush_outstanding_operations (request_data->dispatcher,
979     conn_data);
980 }
981
982 static void
983 dispatcher_request_channel_cb (TpConnection *connection,
984   const gchar  *object_path, const GError *error,
985   gpointer      user_data, GObject      *weak_object)
986 {
987   EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
988   DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
989
990   dispatcher_connection_new_requested_channel (dispatcher,
991     request_data, object_path, NULL, error);
992 }
993
994 static void
995 dispatcher_request_channel (DispatcherRequestData *request_data)
996 {
997   tp_cli_connection_call_request_channel (request_data->connection, -1,
998     request_data->channel_type,
999     request_data->handle_type,
1000     request_data->handle,
1001     TRUE, dispatcher_request_channel_cb,
1002     request_data, NULL, G_OBJECT (request_data->dispatcher));
1003 }
1004
1005 void
1006 empathy_dispatcher_call_with_contact ( EmpathyContact *contact,
1007   EmpathyDispatcherRequestCb *callback, gpointer user_data)
1008 {
1009   EmpathyDispatcher *dispatcher = empathy_get_dispatcher();
1010   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1011   McAccount *account;
1012   TpConnection *connection;
1013   ConnectionData *cd;
1014   DispatcherRequestData *request_data;
1015
1016   account = empathy_contact_get_account (contact);
1017   connection = g_hash_table_lookup (priv->accounts, account);
1018
1019   g_assert (connection != NULL);
1020   cd = g_hash_table_lookup (priv->connections, connection);
1021   request_data  = new_dispatcher_request_data (dispatcher, connection,
1022     TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1023     contact, callback, user_data);
1024
1025   cd->outstanding_requests = g_list_prepend
1026     (cd->outstanding_requests, request_data);
1027
1028   dispatcher_request_channel (request_data);
1029
1030   g_object_unref (dispatcher);
1031 }
1032
1033
1034 static void
1035 dispatcher_chat_with_contact_cb (EmpathyContact *contact, gpointer user_data)
1036 {
1037   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1038
1039   request_data->handle = empathy_contact_get_handle (contact);
1040
1041   dispatcher_request_channel (request_data);
1042 }
1043
1044 void
1045 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1046   EmpathyDispatcherRequestCb *callback, gpointer user_data)
1047 {
1048   EmpathyDispatcher *dispatcher = empathy_get_dispatcher();
1049   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1050   McAccount *account = empathy_contact_get_account (contact);
1051   TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1052   ConnectionData *connection_data =
1053     g_hash_table_lookup (priv->connections, connection);
1054   DispatcherRequestData *request_data;
1055
1056   /* The contact handle might not be known yet */
1057   request_data  = new_dispatcher_request_data (dispatcher, connection,
1058     TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1059     contact, callback, user_data);
1060
1061   connection_data->outstanding_requests = g_list_prepend
1062     (connection_data->outstanding_requests, request_data);
1063
1064   empathy_contact_call_when_ready (contact,
1065     EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1066     request_data);
1067
1068   g_object_unref (dispatcher);
1069 }
1070
1071 void
1072 empathy_dispatcher_chat_with_contact_id (McAccount *account, const gchar
1073   *contact_id, EmpathyDispatcherRequestCb *callback, gpointer user_data)
1074 {
1075   EmpathyDispatcher *dispatcher = empathy_get_dispatcher ();
1076   EmpathyContactFactory *factory;
1077   EmpathyContact        *contact;
1078
1079   factory = empathy_contact_factory_dup_singleton ();
1080   contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1081
1082   empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1083
1084   g_object_unref (contact);
1085   g_object_unref (factory);
1086   g_object_unref (dispatcher);
1087 }
1088
1089
1090 static void
1091 dispatcher_request_handles_cb (TpConnection *connection,
1092    const GArray *handles, const GError *error, gpointer user_data,
1093    GObject *object)
1094 {
1095   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1096
1097   if (error != NULL)
1098     {
1099       EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1100       EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1101       ConnectionData *cd;
1102
1103       cd = g_hash_table_lookup (priv->connections, request_data->connection);
1104
1105       if (request_data->cb)
1106         request_data->cb (NULL, error, request_data->user_data);
1107
1108       cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1109         request_data);
1110
1111       free_dispatcher_request_data (request_data);
1112
1113       dispatcher_flush_outstanding_operations (dispatcher, cd);
1114       return;
1115     }
1116
1117   request_data->handle = g_array_index (handles, guint, 0);
1118   dispatcher_request_channel (request_data);
1119 }
1120
1121 void
1122 empathy_dispatcher_join_muc (McAccount *account, const gchar *roomname,
1123   EmpathyDispatcherRequestCb *callback, gpointer user_data)
1124 {
1125   EmpathyDispatcher *dispatcher = empathy_get_dispatcher();
1126   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1127   DispatcherRequestData *request_data;
1128   TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1129   ConnectionData *connection_data =
1130     g_hash_table_lookup (priv->connections, connection);
1131   const gchar *names[] = { roomname, NULL };
1132
1133   /* Don't know the room handle yet */
1134   request_data  = new_dispatcher_request_data (dispatcher, connection,
1135     TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1136     NULL, callback, user_data);
1137
1138   connection_data->outstanding_requests = g_list_prepend
1139     (connection_data->outstanding_requests, request_data);
1140
1141   tp_cli_connection_call_request_handles (connection, -1,
1142     TP_HANDLE_TYPE_ROOM, names,
1143     dispatcher_request_handles_cb, request_data, NULL,
1144     G_OBJECT (dispatcher));
1145
1146   g_object_unref (dispatcher);
1147 }
1148
1149 static void
1150 dispatcher_create_channel_cb (TpConnection *connect,
1151   const gchar *object_path, GHashTable *properties, const GError *error,
1152   gpointer user_data, GObject *weak_object)
1153 {
1154   EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1155   DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1156
1157   dispatcher_connection_new_requested_channel (dispatcher,
1158     request_data, object_path, properties, error);
1159 }
1160
1161
1162 static void
1163 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1164   gpointer user_data)
1165 {
1166   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1167   GValue *target_handle;
1168
1169   g_assert (request_data->request);
1170
1171   request_data->handle = empathy_contact_get_handle (contact);
1172
1173   target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1174   g_value_set_uint (target_handle, request_data->handle);
1175   g_hash_table_insert (request_data->request,
1176     TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1177
1178   tp_cli_connection_interface_requests_call_create_channel (
1179     request_data->connection, -1,
1180     request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1181     G_OBJECT (request_data->dispatcher));
1182 }
1183
1184 void
1185 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1186   const gchar *filename, guint64 size, guint64 date,
1187   const gchar *content_type, EmpathyDispatcherRequestCb *callback,
1188   gpointer user_data)
1189 {
1190   EmpathyDispatcher *dispatcher = empathy_get_dispatcher();
1191   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1192   McAccount *account = empathy_contact_get_account (contact);
1193   TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1194   ConnectionData *connection_data =
1195     g_hash_table_lookup (priv->connections, connection);
1196   DispatcherRequestData *request_data;
1197   GValue *value;
1198   GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1199       (GDestroyNotify) tp_g_value_slice_free);
1200
1201   /* org.freedesktop.Telepathy.Channel.ChannelType */
1202   value = tp_g_value_slice_new (G_TYPE_STRING);
1203   g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1204   g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1205
1206   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1207   value = tp_g_value_slice_new (G_TYPE_UINT);
1208   g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1209   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1210
1211   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1212   value = tp_g_value_slice_new (G_TYPE_STRING);
1213   g_value_set_string (value, content_type);
1214   g_hash_table_insert (request,
1215     EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1216
1217   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1218   value = tp_g_value_slice_new (G_TYPE_STRING);
1219   g_value_set_string (value, filename);
1220   g_hash_table_insert (request,
1221     EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1222
1223   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1224   value = tp_g_value_slice_new (G_TYPE_UINT64);
1225   g_value_set_uint64 (value, size);
1226   g_hash_table_insert (request,
1227     EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1228
1229   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1230   value = tp_g_value_slice_new (G_TYPE_UINT64);
1231   g_value_set_uint64 (value, date);
1232   g_hash_table_insert (request,
1233     EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1234
1235
1236   /* The contact handle might not be known yet */
1237   request_data  = new_dispatcher_request_data (dispatcher, connection,
1238     EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1239     contact, callback, user_data);
1240   connection_data->outstanding_requests = g_list_prepend
1241     (connection_data->outstanding_requests, request_data);
1242
1243   empathy_contact_call_when_ready (contact,
1244     EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1245     request_data);
1246
1247   g_object_unref (dispatcher);
1248 }