]> git.0d.be Git - empathy.git/blob - libempathy/empathy-dispatcher.c
Make the dispatch operation _get_ functions not return a ref
[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
482   cd = g_hash_table_lookup (priv->connections, connection);
483
484   /* Don't bother with channels we have already dispatched or are dispatching
485    * currently. This can happen when NewChannel(s) is fired after
486    * RequestChannel/CreateChannel/EnsureChannel */
487   if (g_hash_table_lookup (cd->dispatched_channels, object_path) != NULL)
488     return;
489
490   if (g_hash_table_lookup (cd->dispatching_channels, object_path) != NULL)
491     return;
492
493   /* Should never occur, but just in case a CM fires spurious NewChannel(s) 
494    * signals */
495   if (g_hash_table_lookup (cd->outstanding_channels, object_path) != NULL)
496     return;
497
498   DEBUG ("New channel of type %s on %s",
499     channel_type, object_path);
500
501   if (properties == NULL)
502     channel = tp_channel_new (connection, object_path, channel_type,
503       handle_type, handle, NULL);
504   else
505     channel = tp_channel_new_from_properties (connection, object_path,
506       properties, NULL);
507
508   g_signal_connect (channel, "invalidated",
509     G_CALLBACK (dispatcher_channel_invalidated_cb),
510     dispatcher);
511
512   if (handle_type == TP_CONN_HANDLE_TYPE_CONTACT)
513     {
514       EmpathyContactFactory *factory = empathy_contact_factory_new ();
515       contact = empathy_contact_factory_get_from_handle (factory,
516         cd->account, handle);
517       g_object_unref (factory);
518     }
519
520   operation = empathy_dispatch_operation_new (connection, channel, contact,
521     incoming);
522
523   g_object_unref (channel);
524
525   if (incoming)
526     {
527       /* Request could either be by us or by a remote party. If there are no
528        * outstanding requests for this channel type we can assume it's remote.
529        * Otherwise we wait untill they are all satisfied */
530       if (dispatcher_operation_can_start (dispatcher, operation, cd))
531         dispatcher_start_dispatching (dispatcher, operation, cd);
532       else
533         g_hash_table_insert (cd->outstanding_channels,
534           g_strdup (object_path), operation);
535     }
536   else
537     {
538       dispatcher_start_dispatching (dispatcher, operation, cd);
539     }
540 }
541
542 static void
543 dispatcher_connection_new_channel_cb (TpConnection *connection,
544   const gchar  *object_path, const gchar  *channel_type,
545   guint         handle_type, guint         handle,
546   gboolean      suppress_handler, gpointer      user_data,
547   GObject      *object)
548 {
549   EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
550
551   /* Empathy heavily abuses surpress handler (don't try this at home), if
552    * surpress handler is true then it is an outgoing channel, which is
553    * requested either by us or some other party (like the megaphone applet).
554    * Otherwise it's an incoming channel */
555   dispatcher_connection_new_channel (dispatcher, connection,
556     object_path, channel_type, handle_type, handle, NULL, !suppress_handler);
557 }
558
559 static void
560 dispatcher_connection_new_channel_with_properties (
561   EmpathyDispatcher *dispatcher, TpConnection *connection,
562   const gchar *object_path, GHashTable *properties)
563 {
564   const gchar *channel_type;
565   guint handle_type;
566   guint handle;
567   gboolean requested;
568   gboolean valid;
569
570
571   channel_type = tp_asv_get_string (properties,
572     TP_IFACE_CHANNEL ".ChannelType");
573   if (channel_type == NULL)
574     {
575       g_message ("%s had an invalid ChannelType property", object_path);
576       return;
577     }
578
579   handle_type = tp_asv_get_uint32 (properties,
580     TP_IFACE_CHANNEL ".TargetHandleType", &valid);
581   if (!valid)
582     {
583       g_message ("%s had an invalid TargetHandleType property", object_path);
584       return;
585     }
586
587   handle = tp_asv_get_uint32 (properties,
588     TP_IFACE_CHANNEL ".TargetHandle", &valid);
589   if (!valid)
590     {
591       g_message ("%s had an invalid TargetHandle property", object_path);
592       return;
593     }
594
595   /* We assume there is no channel dispather, so we're the only one dispatching
596    * it. Which means that a requested channel it is outgoing one */
597   requested = tp_asv_get_boolean (properties,
598     TP_IFACE_CHANNEL ".Requested", &valid);
599   if (!valid)
600     {
601       g_message ("%s had an invalid Requested property", object_path);
602       return;
603     }
604
605   dispatcher_connection_new_channel (dispatcher, connection,
606     object_path, channel_type, handle_type, handle, properties, !requested);
607 }
608
609
610 static void
611 dispatcher_connection_new_channels_cb (
612   TpConnection *connection, const GPtrArray *channels, gpointer user_data,
613   GObject *object)
614 {
615   EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
616   int i;
617
618   for (i = 0; i < channels->len ; i++)
619     {
620       GValueArray *arr = g_ptr_array_index (channels, i);
621       const gchar *object_path;
622       GHashTable *properties;
623
624       object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
625       properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
626
627       dispatcher_connection_new_channel_with_properties (dispatcher,
628         connection, object_path, properties);
629     }
630 }
631
632 static void
633 dispatcher_connection_got_channels_property (TpProxy *proxy,
634   const GValue *channels_prop, const GError *error, gpointer user_data,
635   GObject *object)
636 {
637   GPtrArray *channels;
638
639   if (error) {
640     DEBUG ("Error: %s", error->message);
641     return;
642   }
643
644   channels = g_value_get_boxed (channels_prop);
645   dispatcher_connection_new_channels_cb (TP_CONNECTION (proxy),
646     channels, NULL, object);
647 }
648
649 static void
650 dispatcher_connection_list_channels_cb (TpConnection    *connection,
651                                         const GPtrArray *channels,
652                                         const GError    *error,
653                                         gpointer         user_data,
654                                         GObject         *dispatcher)
655 {
656         int i;
657
658         if (error) {
659                 DEBUG ("Error: %s", error->message);
660                 return;
661         }
662
663         for (i = 0; i < channels->len; i++) {
664                 GValueArray *values;
665
666                 values = g_ptr_array_index (channels, i);
667                 /* We don't have any extra info, so assume already existing channels are
668                  * incoming... */
669                 dispatcher_connection_new_channel (EMPATHY_DISPATCHER (dispatcher),
670                         connection,
671                         g_value_get_boxed (g_value_array_get_nth (values, 0)),
672                         g_value_get_string (g_value_array_get_nth (values, 1)),
673                         g_value_get_uint (g_value_array_get_nth (values, 2)),
674                         g_value_get_uint (g_value_array_get_nth (values, 3)),
675                         NULL, TRUE);
676         }
677 }
678
679 static void
680 dispatcher_connection_advertise_capabilities_cb (TpConnection    *connection,
681                                                  const GPtrArray *capabilities,
682                                                  const GError    *error,
683                                                  gpointer         user_data,
684                                                  GObject         *dispatcher)
685 {
686         if (error) {
687                 DEBUG ("Error: %s", error->message);
688         }
689 }
690
691 static void
692 dispatcher_connection_ready_cb (TpConnection  *connection,
693   const GError *error, gpointer       dispatcher)
694 {
695   GPtrArray   *capabilities;
696   GType        cap_type;
697   GValue       cap = {0, };
698   const gchar *remove = NULL;
699
700   if (error)
701     {
702       dispatcher_connection_invalidated_cb (connection, error->domain,
703         error->code, error->message, dispatcher);
704       return;
705     }
706
707   g_signal_connect (connection, "invalidated",
708     G_CALLBACK (dispatcher_connection_invalidated_cb), dispatcher);
709
710
711   if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
712       TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
713     {
714       tp_cli_connection_interface_requests_connect_to_new_channels (connection,
715         dispatcher_connection_new_channels_cb,
716         NULL, NULL, G_OBJECT (dispatcher), NULL);
717
718       tp_cli_dbus_properties_call_get (connection, -1,
719         TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels",
720         dispatcher_connection_got_channels_property,
721         NULL, NULL, dispatcher);
722     }
723   else
724     {
725       tp_cli_connection_connect_to_new_channel (connection,
726         dispatcher_connection_new_channel_cb,
727         NULL, NULL, G_OBJECT (dispatcher), NULL);
728
729       tp_cli_connection_call_list_channels (connection, -1,
730         dispatcher_connection_list_channels_cb, NULL, NULL,
731         G_OBJECT (dispatcher));
732
733     }
734
735   /* Advertise VoIP capabilities */
736   capabilities = g_ptr_array_sized_new (1);
737   cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
738     G_TYPE_UINT, G_TYPE_INVALID);
739   g_value_init (&cap, cap_type);
740   g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
741   dbus_g_type_struct_set (&cap,
742         0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
743         1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
744            TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
745            TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN  |
746            TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P, G_MAXUINT);
747   g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
748
749   tp_cli_connection_interface_capabilities_call_advertise_capabilities (
750     connection, -1, capabilities, &remove,
751     dispatcher_connection_advertise_capabilities_cb,
752     NULL, NULL, G_OBJECT (dispatcher));
753 }
754
755 static void
756 dispatcher_update_account (EmpathyDispatcher *dispatcher, McAccount *account)
757 {
758   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
759   TpConnection *connection;
760
761   connection = g_hash_table_lookup (priv->accounts, account);
762   if  (connection != NULL)
763     return;
764
765   connection = mission_control_get_tpconnection (priv->mc, account, NULL);
766   if (connection == NULL)
767     return;
768
769   g_hash_table_insert (priv->connections, g_object_ref (connection),
770     new_connection_data (account));
771
772   g_hash_table_insert (priv->accounts, g_object_ref (account),
773     g_object_ref (connection));
774
775   tp_connection_call_when_ready (connection, dispatcher_connection_ready_cb,
776     dispatcher);
777 }
778
779 static void
780 dispatcher_account_connection_cb (EmpathyAccountManager *manager,
781   McAccount *account, TpConnectionStatusReason reason,
782   TpConnectionStatus status, TpConnectionStatus previous,
783   EmpathyDispatcher *dispatcher)
784 {
785   dispatcher_update_account (dispatcher, account);
786 }
787
788
789 static GObject*
790 dispatcher_constructor (GType type, guint n_construct_params,
791   GObjectConstructParam *construct_params)
792 {
793   GObject *retval;
794
795   if (dispatcher == NULL)
796     {
797       retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
798           (type, n_construct_params, construct_params);
799
800       dispatcher = EMPATHY_DISPATCHER (retval);
801       g_object_add_weak_pointer (retval, (gpointer *) &dispatcher);
802     }
803   else
804     {
805       retval = g_object_ref (dispatcher);
806     }
807
808   return retval;
809 }
810
811 static void
812 dispatcher_finalize (GObject *object)
813 {
814   EmpathyDispatcherPriv *priv = GET_PRIV (object);
815
816   g_signal_handlers_disconnect_by_func (priv->account_manager,
817       dispatcher_account_connection_cb, object);
818
819   g_object_unref (priv->account_manager);
820   g_object_unref (priv->mc);
821
822   g_hash_table_destroy (priv->accounts);
823   g_hash_table_destroy (priv->connections);
824 }
825
826 static void
827 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
828 {
829   GObjectClass *object_class = G_OBJECT_CLASS (klass);
830
831   object_class->finalize = dispatcher_finalize;
832   object_class->constructor = dispatcher_constructor;
833
834   signals[OBSERVE] =
835     g_signal_new ("observe",
836       G_TYPE_FROM_CLASS (klass),
837       G_SIGNAL_RUN_LAST,
838       0,
839       NULL, NULL,
840       g_cclosure_marshal_VOID__OBJECT,
841       G_TYPE_NONE,
842       1, EMPATHY_TYPE_DISPATCH_OPERATION);
843
844   signals[APPROVE] =
845     g_signal_new ("approve",
846       G_TYPE_FROM_CLASS (klass),
847       G_SIGNAL_RUN_LAST,
848       0,
849       NULL, NULL,
850       g_cclosure_marshal_VOID__OBJECT,
851       G_TYPE_NONE,
852       1, EMPATHY_TYPE_DISPATCH_OPERATION);
853
854   signals[DISPATCH] =
855     g_signal_new ("dispatch",
856       G_TYPE_FROM_CLASS (klass),
857       G_SIGNAL_RUN_LAST,
858       0,
859       NULL, NULL,
860       g_cclosure_marshal_VOID__OBJECT,
861       G_TYPE_NONE,
862       1, EMPATHY_TYPE_DISPATCH_OPERATION);
863
864   g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
865
866 }
867
868 static void
869 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
870 {
871   GList *accounts, *l;
872   EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
873     EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
874
875   dispatcher->priv = priv;
876   priv->mc = empathy_mission_control_new ();
877   priv->account_manager = empathy_account_manager_dup_singleton ();
878
879   g_signal_connect (priv->account_manager,
880     "account-connection-changed",
881     G_CALLBACK (dispatcher_account_connection_cb),
882     dispatcher);
883
884   priv->accounts = g_hash_table_new_full (empathy_account_hash,
885         empathy_account_equal, g_object_unref, g_object_unref);
886
887   priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
888     g_object_unref, (GDestroyNotify) free_connection_data);
889
890   accounts = mc_accounts_list_by_enabled (TRUE);
891
892   for (l = accounts; l; l = l->next) {
893     dispatcher_update_account (dispatcher, l->data);
894     g_object_unref (l->data);
895   }
896   g_list_free (accounts);
897 }
898
899 EmpathyDispatcher *
900 empathy_dispatcher_dup_singleton (void)
901 {
902   return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
903 }
904
905 static void
906 dispatcher_connection_new_requested_channel (EmpathyDispatcher *dispatcher,
907   DispatcherRequestData *request_data, const gchar *object_path,
908   GHashTable *properties, const GError *error)
909 {
910   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
911   EmpathyDispatchOperation *operation = NULL;
912   ConnectionData *conn_data;
913
914   conn_data = g_hash_table_lookup (priv->connections,
915     request_data->connection);
916
917   if (error)
918     {
919       DEBUG ("Channel request failed: %s", error->message);
920
921       if (request_data->cb != NULL)
922           request_data->cb (NULL, error, request_data->user_data);
923
924       conn_data->outstanding_requests =
925         g_list_remove (conn_data->outstanding_requests, request_data);
926       free_dispatcher_request_data (request_data);
927
928       goto out;
929   }
930
931   operation = g_hash_table_lookup (conn_data->outstanding_channels,
932     object_path);
933
934   if (operation != NULL)
935     g_hash_table_remove (conn_data->outstanding_channels, object_path);
936   else
937     operation = g_hash_table_lookup (conn_data->dispatching_channels,
938         object_path);
939
940   if (operation == NULL)
941     {
942       DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
943         object_path);
944
945       if (data != NULL)
946         {
947           operation = empathy_dispatch_operation_new_with_wrapper (
948             request_data->connection,
949             data->channel, request_data->contact, FALSE,
950             data->channel_wrapper);
951         }
952       else
953         {
954           TpChannel *channel;
955
956           if (properties != NULL)
957             channel = tp_channel_new_from_properties (request_data->connection,
958               object_path, properties, NULL);
959           else
960             channel = tp_channel_new (request_data->connection, object_path,
961               request_data->channel_type, request_data->handle_type,
962               request_data->handle, NULL);
963
964           g_signal_connect (channel, "invalidated",
965             G_CALLBACK (dispatcher_channel_invalidated_cb),
966             request_data->dispatcher);
967
968           operation = empathy_dispatch_operation_new (request_data->connection,
969              channel, request_data->contact, FALSE);
970           g_object_unref (channel);
971         }
972     }
973   else
974     {
975       /* Already existed set potential extra information */
976       g_object_set (G_OBJECT (operation),
977         "contact", request_data->contact,
978         NULL);
979     }
980
981   request_data->operation = operation;
982
983   /* (pre)-approve this right away as we requested it */
984   empathy_dispatch_operation_approve (operation);
985
986   dispatcher_start_dispatching (request_data->dispatcher, operation,
987         conn_data);
988 out:
989   dispatcher_flush_outstanding_operations (request_data->dispatcher,
990     conn_data);
991 }
992
993 static void
994 dispatcher_request_channel_cb (TpConnection *connection,
995   const gchar  *object_path, const GError *error,
996   gpointer      user_data, GObject      *weak_object)
997 {
998   EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
999   DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1000
1001   dispatcher_connection_new_requested_channel (dispatcher,
1002     request_data, object_path, NULL, error);
1003 }
1004
1005 static void
1006 dispatcher_request_channel (DispatcherRequestData *request_data)
1007 {
1008   tp_cli_connection_call_request_channel (request_data->connection, -1,
1009     request_data->channel_type,
1010     request_data->handle_type,
1011     request_data->handle,
1012     TRUE, dispatcher_request_channel_cb,
1013     request_data, NULL, G_OBJECT (request_data->dispatcher));
1014 }
1015
1016 void
1017 empathy_dispatcher_call_with_contact ( EmpathyContact *contact,
1018   EmpathyDispatcherRequestCb *callback, gpointer user_data)
1019 {
1020   EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1021   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1022   McAccount *account;
1023   TpConnection *connection;
1024   ConnectionData *cd;
1025   DispatcherRequestData *request_data;
1026
1027   account = empathy_contact_get_account (contact);
1028   connection = g_hash_table_lookup (priv->accounts, account);
1029
1030   g_assert (connection != NULL);
1031   cd = g_hash_table_lookup (priv->connections, connection);
1032   request_data  = new_dispatcher_request_data (dispatcher, connection,
1033     TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_NONE, 0, NULL,
1034     contact, callback, user_data);
1035
1036   cd->outstanding_requests = g_list_prepend
1037     (cd->outstanding_requests, request_data);
1038
1039   dispatcher_request_channel (request_data);
1040
1041   g_object_unref (dispatcher);
1042 }
1043
1044
1045 static void
1046 dispatcher_chat_with_contact_cb (EmpathyContact *contact, gpointer user_data)
1047 {
1048   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1049
1050   request_data->handle = empathy_contact_get_handle (contact);
1051
1052   dispatcher_request_channel (request_data);
1053 }
1054
1055 void
1056 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1057   EmpathyDispatcherRequestCb *callback, gpointer user_data)
1058 {
1059   EmpathyDispatcher *dispatcher;
1060   EmpathyDispatcherPriv *priv;
1061   McAccount *account;
1062   TpConnection *connection;
1063   ConnectionData *connection_data;
1064   DispatcherRequestData *request_data;
1065
1066   dispatcher = empathy_dispatcher_dup_singleton();
1067   priv = GET_PRIV (dispatcher);
1068
1069   account = empathy_contact_get_account (contact);
1070   connection = g_hash_table_lookup (priv->accounts, account);
1071   connection_data = g_hash_table_lookup (priv->connections, connection);
1072
1073   /* The contact handle might not be known yet */
1074   request_data  = new_dispatcher_request_data (dispatcher, connection,
1075     TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT, 0, NULL,
1076     contact, callback, user_data);
1077
1078   connection_data->outstanding_requests = g_list_prepend
1079     (connection_data->outstanding_requests, request_data);
1080
1081   empathy_contact_call_when_ready (contact,
1082     EMPATHY_CONTACT_READY_HANDLE, dispatcher_chat_with_contact_cb,
1083     request_data);
1084
1085   g_object_unref (dispatcher);
1086 }
1087
1088 void
1089 empathy_dispatcher_chat_with_contact_id (McAccount *account, const gchar
1090   *contact_id, EmpathyDispatcherRequestCb *callback, gpointer user_data)
1091 {
1092   EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton ();
1093   EmpathyContactFactory *factory;
1094   EmpathyContact        *contact;
1095
1096   factory = empathy_contact_factory_dup_singleton ();
1097   contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
1098
1099   empathy_dispatcher_chat_with_contact (contact, callback, user_data);
1100
1101   g_object_unref (contact);
1102   g_object_unref (factory);
1103   g_object_unref (dispatcher);
1104 }
1105
1106
1107 static void
1108 dispatcher_request_handles_cb (TpConnection *connection,
1109    const GArray *handles, const GError *error, gpointer user_data,
1110    GObject *object)
1111 {
1112   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1113
1114   if (error != NULL)
1115     {
1116       EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
1117       EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1118       ConnectionData *cd;
1119
1120       cd = g_hash_table_lookup (priv->connections, request_data->connection);
1121
1122       if (request_data->cb)
1123         request_data->cb (NULL, error, request_data->user_data);
1124
1125       cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1126         request_data);
1127
1128       free_dispatcher_request_data (request_data);
1129
1130       dispatcher_flush_outstanding_operations (dispatcher, cd);
1131       return;
1132     }
1133
1134   request_data->handle = g_array_index (handles, guint, 0);
1135   dispatcher_request_channel (request_data);
1136 }
1137
1138 void
1139 empathy_dispatcher_join_muc (McAccount *account, const gchar *roomname,
1140   EmpathyDispatcherRequestCb *callback, gpointer user_data)
1141 {
1142   EmpathyDispatcher *dispatcher;
1143   EmpathyDispatcherPriv *priv;
1144   DispatcherRequestData *request_data;
1145   TpConnection *connection;
1146   ConnectionData *connection_data;
1147   const gchar *names[] = { roomname, NULL };
1148
1149   dispatcher = empathy_dispatcher_dup_singleton();
1150   priv = GET_PRIV (dispatcher);
1151
1152   connection = g_hash_table_lookup (priv->accounts, account);
1153   connection_data = g_hash_table_lookup (priv->connections, connection);
1154
1155
1156   /* Don't know the room handle yet */
1157   request_data  = new_dispatcher_request_data (dispatcher, connection,
1158     TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1159     NULL, callback, user_data);
1160
1161   connection_data->outstanding_requests = g_list_prepend
1162     (connection_data->outstanding_requests, request_data);
1163
1164   tp_cli_connection_call_request_handles (connection, -1,
1165     TP_HANDLE_TYPE_ROOM, names,
1166     dispatcher_request_handles_cb, request_data, NULL,
1167     G_OBJECT (dispatcher));
1168
1169   g_object_unref (dispatcher);
1170 }
1171
1172 static void
1173 dispatcher_create_channel_cb (TpConnection *connect,
1174   const gchar *object_path, GHashTable *properties, const GError *error,
1175   gpointer user_data, GObject *weak_object)
1176 {
1177   EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (weak_object);
1178   DispatcherRequestData *request_data = (DispatcherRequestData*) user_data;
1179
1180   dispatcher_connection_new_requested_channel (dispatcher,
1181     request_data, object_path, properties, error);
1182 }
1183
1184
1185 static void
1186 dispatcher_create_channel_with_contact_cb (EmpathyContact *contact,
1187   gpointer user_data)
1188 {
1189   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1190   GValue *target_handle;
1191
1192   g_assert (request_data->request);
1193
1194   request_data->handle = empathy_contact_get_handle (contact);
1195
1196   target_handle = tp_g_value_slice_new (G_TYPE_UINT);
1197   g_value_set_uint (target_handle, request_data->handle);
1198   g_hash_table_insert (request_data->request,
1199     TP_IFACE_CHANNEL ".TargetHandle", target_handle);
1200
1201   tp_cli_connection_interface_requests_call_create_channel (
1202     request_data->connection, -1,
1203     request_data->request, dispatcher_create_channel_cb, request_data, NULL,
1204     G_OBJECT (request_data->dispatcher));
1205 }
1206
1207 void
1208 empathy_dispatcher_send_file_to_contact (EmpathyContact *contact,
1209   const gchar *filename, guint64 size, guint64 date,
1210   const gchar *content_type, EmpathyDispatcherRequestCb *callback,
1211   gpointer user_data)
1212 {
1213   EmpathyDispatcher *dispatcher = empathy_dispatcher_dup_singleton();
1214   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1215   McAccount *account = empathy_contact_get_account (contact);
1216   TpConnection *connection = g_hash_table_lookup (priv->accounts, account);
1217   ConnectionData *connection_data =
1218     g_hash_table_lookup (priv->connections, connection);
1219   DispatcherRequestData *request_data;
1220   GValue *value;
1221   GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
1222       (GDestroyNotify) tp_g_value_slice_free);
1223
1224   /* org.freedesktop.Telepathy.Channel.ChannelType */
1225   value = tp_g_value_slice_new (G_TYPE_STRING);
1226   g_value_set_string (value, EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER);
1227   g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
1228
1229   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
1230   value = tp_g_value_slice_new (G_TYPE_UINT);
1231   g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
1232   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
1233
1234   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.ContentType */
1235   value = tp_g_value_slice_new (G_TYPE_STRING);
1236   g_value_set_string (value, content_type);
1237   g_hash_table_insert (request,
1238     EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".ContentType", value);
1239
1240   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Filename */
1241   value = tp_g_value_slice_new (G_TYPE_STRING);
1242   g_value_set_string (value, filename);
1243   g_hash_table_insert (request,
1244     EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Filename", value);
1245
1246   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Size */
1247   value = tp_g_value_slice_new (G_TYPE_UINT64);
1248   g_value_set_uint64 (value, size);
1249   g_hash_table_insert (request,
1250     EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Size", value);
1251
1252   /* org.freedesktop.Telepathy.Channel.Type.FileTransfer.Date */
1253   value = tp_g_value_slice_new (G_TYPE_UINT64);
1254   g_value_set_uint64 (value, date);
1255   g_hash_table_insert (request,
1256     EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER ".Date", value);
1257
1258
1259   /* The contact handle might not be known yet */
1260   request_data  = new_dispatcher_request_data (dispatcher, connection,
1261     EMP_IFACE_CHANNEL_TYPE_FILE_TRANSFER, TP_HANDLE_TYPE_CONTACT, 0, request,
1262     contact, callback, user_data);
1263   connection_data->outstanding_requests = g_list_prepend
1264     (connection_data->outstanding_requests, request_data);
1265
1266   empathy_contact_call_when_ready (contact,
1267     EMPATHY_CONTACT_READY_HANDLE, dispatcher_create_channel_with_contact_cb,
1268     request_data);
1269
1270   g_object_unref (dispatcher);
1271 }