]> git.0d.be Git - empathy.git/blob - libempathy/empathy-dispatcher.c
Remove dead code related to outstanding channels
[empathy.git] / libempathy / empathy-dispatcher.c
1 /* * Copyright (C) 2007-2009 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  *          Sjoerd Simons <sjoerd.simons@collabora.co.uk>
19  *          Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
20  */
21
22 #define DISPATCHER_BUS_NAME TP_CLIENT_BUS_NAME_BASE "Empathy"
23 #define DISPATCHER_OBJECT_PATH TP_CLIENT_OBJECT_PATH_BASE "Empathy"
24
25 #include <config.h>
26
27 #include <string.h>
28
29 #include <glib/gi18n-lib.h>
30
31 #include <telepathy-glib/account-manager.h>
32 #include <telepathy-glib/enums.h>
33 #include <telepathy-glib/connection.h>
34 #include <telepathy-glib/channel-dispatcher.h>
35 #include <telepathy-glib/channel-request.h>
36 #include <telepathy-glib/util.h>
37 #include <telepathy-glib/dbus.h>
38 #include <telepathy-glib/proxy-subclass.h>
39 #include <telepathy-glib/gtypes.h>
40 #include <telepathy-glib/defs.h>
41 #include <telepathy-glib/svc-client.h>
42 #include <telepathy-glib/svc-generic.h>
43 #include <telepathy-glib/interfaces.h>
44
45 #include <extensions/extensions.h>
46
47 #include "empathy-dispatcher.h"
48 #include "empathy-handler.h"
49 #include "empathy-utils.h"
50 #include "empathy-tp-contact-factory.h"
51 #include "empathy-chatroom-manager.h"
52 #include "empathy-utils.h"
53
54 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
55 #include <libempathy/empathy-debug.h>
56
57 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyDispatcher)
58 typedef struct
59 {
60   gboolean dispose_has_run;
61
62   TpAccountManager *account_manager;
63   /* connection to connection data mapping */
64   GHashTable *connections;
65   GHashTable *outstanding_classes_requests;
66   gpointer token;
67
68   /* channels which the dispatcher is listening "invalidated" */
69   GList *channels;
70   GPtrArray *array;
71
72   /* main handler */
73   EmpathyHandler *handler;
74
75   /* extra handlers */
76   GList *handlers;
77
78   GHashTable *request_channel_class_async_ids;
79   /* (TpAccount *) => gulong
80    * Signal handler ID of the "status-changed" signal */
81   GHashTable *status_changed_handlers;
82
83   TpChannelDispatcher *channel_dispatcher;
84   TpDBusDaemon *dbus;
85 } EmpathyDispatcherPriv;
86
87 static GList *
88 empathy_dispatcher_get_channels (EmpathyHandler *handler,
89     gpointer user_data);
90
91 static gboolean
92 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
93     const gchar *account_path,
94     const gchar *connection_path,
95     const GPtrArray *channels,
96     const GPtrArray *requests_satisfied,
97     guint64 timestamp,
98     GHashTable *handler_info,
99     gpointer user_data,
100     GError **error);
101
102 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
103
104 enum
105 {
106   PROP_INTERFACES = 1,
107   PROP_HANDLER,
108 };
109
110 enum
111 {
112   OBSERVE,
113   APPROVE,
114   DISPATCH,
115   LAST_SIGNAL
116 };
117
118 static guint signals[LAST_SIGNAL];
119 static EmpathyDispatcher *dispatcher = NULL;
120
121 static void dispatcher_init_connection_if_needed (
122     EmpathyDispatcher *dispatcher,
123     TpConnection *connection);
124
125 static GList * empathy_dispatcher_find_channel_classes
126   (EmpathyDispatcher *dispatcher, TpConnection *connection,
127    const gchar *channel_type, guint handle_type, GArray *fixed_properties);
128
129
130 typedef struct
131 {
132   EmpathyDispatcher *dispatcher;
133   EmpathyDispatchOperation *operation;
134   TpConnection *connection;
135   gboolean should_ensure;
136   gchar *channel_type;
137   guint handle_type;
138   guint handle;
139   EmpathyContact *contact;
140   TpProxyPendingCall *pending_call;
141
142   /* Properties to pass to the channel when requesting it */
143   GHashTable *request;
144   EmpathyDispatcherRequestCb *cb;
145   gpointer user_data;
146   gpointer *request_data;
147
148   TpChannelRequest *channel_request;
149 } DispatcherRequestData;
150
151 typedef struct
152 {
153   TpChannel *channel;
154   /* Channel type specific wrapper object */
155   GObject *channel_wrapper;
156 } DispatchData;
157
158 typedef struct
159 {
160   /* ObjectPath => DispatchData.. */
161   GHashTable *dispatched_channels;
162   /* ObjectPath -> EmpathyDispatchOperations */
163   GHashTable *dispatching_channels;
164
165   /* List of DispatcherRequestData */
166   GList *outstanding_requests;
167   /* List of requestable channel classes */
168   GPtrArray *requestable_channels;
169 } ConnectionData;
170
171 typedef struct
172 {
173   EmpathyDispatcher *dispatcher;
174   TpConnection *connection;
175   char *channel_type;
176   guint handle_type;
177   GArray *properties;
178   EmpathyDispatcherFindChannelClassCb *callback;
179   gpointer user_data;
180 } FindChannelRequest;
181
182 static void
183 empathy_dispatcher_call_create_or_ensure_channel (
184     EmpathyDispatcher *dispatcher,
185     DispatcherRequestData *request_data);
186
187 static void
188 dispatcher_request_failed (EmpathyDispatcher *dispatcher,
189     DispatcherRequestData *request_data,
190     const GError *error);
191
192 static DispatchData *
193 new_dispatch_data (TpChannel *channel,
194                    GObject *channel_wrapper)
195 {
196   DispatchData *d = g_slice_new0 (DispatchData);
197   d->channel = g_object_ref (channel);
198   if (channel_wrapper != NULL)
199     d->channel_wrapper = g_object_ref (channel_wrapper);
200
201   return d;
202 }
203
204 static void
205 free_dispatch_data (DispatchData *data)
206 {
207   g_object_unref (data->channel);
208   if (data->channel_wrapper != NULL)
209     g_object_unref (data->channel_wrapper);
210
211   g_slice_free (DispatchData, data);
212 }
213
214 static DispatcherRequestData *
215 new_dispatcher_request_data (EmpathyDispatcher *self,
216                              TpConnection *connection,
217                              const gchar *channel_type,
218                              guint handle_type,
219                              guint handle,
220                              GHashTable *request,
221                              EmpathyContact *contact,
222                              EmpathyDispatcherRequestCb *cb,
223                              gpointer user_data)
224 {
225   DispatcherRequestData *result = g_slice_new0 (DispatcherRequestData);
226
227   result->dispatcher = g_object_ref (self);
228   result->connection = connection;
229
230   result->should_ensure = FALSE;
231
232   result->channel_type = g_strdup (channel_type);
233   result->handle_type = handle_type;
234   result->handle = handle;
235   result->request = request;
236
237   if (contact != NULL)
238     result->contact = g_object_ref (contact);
239
240   result->cb = cb;
241   result->user_data = user_data;
242
243   return result;
244 }
245
246 static void
247 free_dispatcher_request_data (DispatcherRequestData *r)
248 {
249   g_free (r->channel_type);
250
251   if (r->dispatcher != NULL)
252     g_object_unref (r->dispatcher);
253
254   if (r->contact != NULL)
255     g_object_unref (r->contact);
256
257   if (r->request != NULL)
258     g_hash_table_unref (r->request);
259
260
261   if (r->pending_call != NULL)
262     tp_proxy_pending_call_cancel (r->pending_call);
263
264   if (r->channel_request != NULL)
265     g_object_unref (r->channel_request);
266
267   g_slice_free (DispatcherRequestData, r);
268 }
269
270 static ConnectionData *
271 new_connection_data (void)
272 {
273   ConnectionData *cd = g_slice_new0 (ConnectionData);
274
275   cd->dispatched_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
276       g_free, (GDestroyNotify) free_dispatch_data);
277
278   cd->dispatching_channels = g_hash_table_new_full (g_str_hash, g_str_equal,
279       g_free, g_object_unref);
280
281   return cd;
282 }
283
284 static void
285 free_connection_data (ConnectionData *cd)
286 {
287   GList *l;
288   guint i;
289
290   g_hash_table_destroy (cd->dispatched_channels);
291   g_hash_table_destroy (cd->dispatching_channels);
292
293   for (l = cd->outstanding_requests ; l != NULL; l = g_list_delete_link (l,l))
294     {
295       free_dispatcher_request_data (l->data);
296     }
297
298   if (cd->requestable_channels  != NULL)
299     {
300       for (i = 0 ; i < cd->requestable_channels->len ; i++)
301           g_value_array_free (
302             g_ptr_array_index (cd->requestable_channels, i));
303       g_ptr_array_free (cd->requestable_channels, TRUE);
304     }
305 }
306
307 static void
308 free_find_channel_request (FindChannelRequest *r)
309 {
310   guint idx;
311   char *str;
312
313   g_object_unref (r->dispatcher);
314   g_free (r->channel_type);
315
316   if (r->properties != NULL)
317     {
318       for (idx = 0; idx < r->properties->len ; idx++)
319         {
320           str = g_array_index (r->properties, char *, idx);
321           g_free (str);
322         }
323
324       g_array_free (r->properties, TRUE);
325     }
326
327   g_slice_free (FindChannelRequest, r);
328 }
329
330 static void
331 dispatcher_connection_invalidated_cb (TpConnection *connection,
332                                       guint domain,
333                                       gint code,
334                                       gchar *message,
335                                       EmpathyDispatcher *self)
336 {
337   EmpathyDispatcherPriv *priv = GET_PRIV (self);
338
339   DEBUG ("Error: %s", message);
340   g_hash_table_remove (priv->connections, connection);
341 }
342
343 static void
344 dispatch_operation_flush_requests (EmpathyDispatcher *self,
345                                    EmpathyDispatchOperation *operation,
346                                    GError *error,
347                                    ConnectionData *cd)
348 {
349   GList *l;
350
351   l = cd->outstanding_requests;
352   while (l != NULL)
353     {
354       DispatcherRequestData *d = (DispatcherRequestData *) l->data;
355       GList *lt = l;
356
357       l = g_list_next (l);
358
359       if (d->operation == operation)
360         {
361           if (d->cb != NULL)
362             {
363               if (error != NULL)
364                 d->cb (NULL, error, d->user_data);
365               else
366                 d->cb (operation, NULL, d->user_data);
367             }
368
369           cd->outstanding_requests = g_list_delete_link
370             (cd->outstanding_requests, lt);
371
372           free_dispatcher_request_data (d);
373         }
374     }
375 }
376
377 static void
378 dispatcher_channel_invalidated_cb (TpProxy *proxy,
379                                    guint domain,
380                                    gint code,
381                                    gchar *message,
382                                    EmpathyDispatcher *self)
383 {
384   /* Channel went away... */
385   EmpathyDispatcherPriv *priv = GET_PRIV (self);
386   TpConnection *connection;
387   ConnectionData *cd;
388   const gchar *object_path;
389
390   connection = tp_channel_borrow_connection (TP_CHANNEL (proxy));
391
392   cd = g_hash_table_lookup (priv->connections, connection);
393   /* Connection itself invalidated? */
394   if (cd == NULL)
395     return;
396
397   object_path = tp_proxy_get_object_path (proxy);
398
399   DEBUG ("Channel %s invalidated", object_path);
400
401   g_hash_table_remove (cd->dispatched_channels, object_path);
402   g_hash_table_remove (cd->dispatching_channels, object_path);
403
404   priv->channels = g_list_remove (priv->channels, proxy);
405 }
406
407 static void
408 dispatch_operation_approved_cb (EmpathyDispatchOperation *operation,
409                                 EmpathyDispatcher *self)
410 {
411   g_assert (empathy_dispatch_operation_is_incoming (operation));
412   DEBUG ("Send of for dispatching: %s",
413     empathy_dispatch_operation_get_object_path (operation));
414   g_signal_emit (self, signals[DISPATCH], 0, operation);
415 }
416
417 static void
418 dispatch_operation_claimed_cb (EmpathyDispatchOperation *operation,
419                                EmpathyDispatcher *self)
420 {
421   /* Our job is done, remove the dispatch operation and mark the channel as
422    * dispatched */
423   EmpathyDispatcherPriv *priv = GET_PRIV (self);
424   TpConnection *connection;
425   ConnectionData *cd;
426   const gchar *object_path;
427
428   connection = empathy_dispatch_operation_get_tp_connection (operation);
429   cd = g_hash_table_lookup (priv->connections, connection);
430   g_assert (cd != NULL);
431
432   object_path = empathy_dispatch_operation_get_object_path (operation);
433
434   if (g_hash_table_lookup (cd->dispatched_channels, object_path) == NULL)
435     {
436       DispatchData *d;
437       d = new_dispatch_data (
438         empathy_dispatch_operation_get_channel (operation),
439         empathy_dispatch_operation_get_channel_wrapper (operation));
440       g_hash_table_insert (cd->dispatched_channels,
441         g_strdup (object_path), d);
442     }
443   g_hash_table_remove (cd->dispatching_channels, object_path);
444
445   DEBUG ("Channel claimed: %s", object_path);
446 }
447
448 static void
449 dispatch_operation_ready_cb (EmpathyDispatchOperation *operation,
450                              EmpathyDispatcher *self)
451 {
452   EmpathyDispatcherPriv *priv = GET_PRIV (self);
453   TpConnection *connection;
454   ConnectionData *cd;
455   EmpathyDispatchOperationState status;
456
457   g_signal_connect (operation, "approved",
458     G_CALLBACK (dispatch_operation_approved_cb), self);
459
460   g_signal_connect (operation, "claimed",
461     G_CALLBACK (dispatch_operation_claimed_cb), self);
462
463   /* Signal the observers */
464   DEBUG ("Send to observers: %s",
465     empathy_dispatch_operation_get_object_path (operation));
466   g_signal_emit (self, signals[OBSERVE], 0, operation);
467
468   empathy_dispatch_operation_start (operation);
469
470   /* Signal potential requestors */
471   connection =  empathy_dispatch_operation_get_tp_connection (operation);
472   cd = g_hash_table_lookup (priv->connections, connection);
473   g_assert (cd != NULL);
474
475   g_object_ref (operation);
476   g_object_ref (self);
477
478   dispatch_operation_flush_requests (self, operation, NULL, cd);
479   status = empathy_dispatch_operation_get_status (operation);
480   g_object_unref (operation);
481
482   if (status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
483     return;
484
485   if (status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
486     {
487       DEBUG ("Send to approvers: %s",
488         empathy_dispatch_operation_get_object_path (operation));
489       g_signal_emit (self, signals[APPROVE], 0, operation);
490     }
491   else
492     {
493       g_assert (status == EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
494       DEBUG ("Send of for dispatching: %s",
495         empathy_dispatch_operation_get_object_path (operation));
496       g_signal_emit (self, signals[DISPATCH], 0, operation);
497     }
498
499   g_object_unref (self);
500 }
501
502 static void
503 dispatcher_start_dispatching (EmpathyDispatcher *self,
504                               EmpathyDispatchOperation *operation,
505                               ConnectionData *cd)
506 {
507   const gchar *object_path =
508     empathy_dispatch_operation_get_object_path (operation);
509
510   DEBUG ("Dispatching process started for %s", object_path);
511
512   if (g_hash_table_lookup (cd->dispatching_channels, object_path) == NULL)
513     {
514       g_hash_table_insert (cd->dispatching_channels,
515         g_strdup (object_path), operation);
516
517       switch (empathy_dispatch_operation_get_status (operation))
518         {
519           case EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING:
520             g_signal_connect (operation, "ready",
521               G_CALLBACK (dispatch_operation_ready_cb), self);
522             break;
523           case EMPATHY_DISPATCHER_OPERATION_STATE_PENDING:
524             dispatch_operation_ready_cb (operation, self);
525             break;
526           default:
527             g_assert_not_reached ();
528         }
529
530     }
531   else if (empathy_dispatch_operation_get_status (operation) >=
532       EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
533     {
534       /* Already dispatching and the operation is pending, thus the observers
535        * have seen it (if applicable), so we can flush the request right away.
536        */
537       dispatch_operation_flush_requests (self, operation, NULL, cd);
538     }
539 }
540
541 static void
542 dispatcher_connection_new_channel (EmpathyDispatcher *self,
543                                    TpConnection *connection,
544                                    const gchar *object_path,
545                                    const gchar *channel_type,
546                                    guint handle_type,
547                                    guint handle,
548                                    GHashTable *properties,
549                                    gboolean incoming,
550                                    const GPtrArray *requests_satisfied)
551 {
552   EmpathyDispatcherPriv *priv = GET_PRIV (self);
553   TpChannel         *channel;
554   ConnectionData *cd;
555   EmpathyDispatchOperation *operation;
556   int i;
557   /* Channel types we never want to dispatch because they're either deprecated
558    * or can't sensibly be dispatch (e.g. channels that should always be
559    * requested) */
560   const char *blacklist[] = {
561     TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
562     TP_IFACE_CHANNEL_TYPE_TUBES,
563     TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
564     NULL
565   };
566
567   dispatcher_init_connection_if_needed (self, connection);
568
569   cd = g_hash_table_lookup (priv->connections, connection);
570
571   for (i = 0 ; blacklist[i] != NULL; i++)
572     {
573       if (!tp_strdiff (channel_type, blacklist[i]))
574         {
575           DEBUG ("Ignoring blacklisted channel type %s on %s",
576             channel_type, object_path);
577           return;
578         }
579     }
580
581   DEBUG ("New channel of type %s on %s", channel_type, object_path);
582
583   if (properties == NULL)
584     channel = tp_channel_new (connection, object_path, channel_type,
585       handle_type, handle, NULL);
586   else
587     channel = tp_channel_new_from_properties (connection, object_path,
588       properties, NULL);
589
590   g_signal_connect (channel, "invalidated",
591     G_CALLBACK (dispatcher_channel_invalidated_cb),
592     self);
593
594   priv->channels = g_list_prepend (priv->channels, channel);
595
596   operation = empathy_dispatch_operation_new (connection, channel, NULL,
597     incoming);
598
599   g_object_unref (channel);
600
601   if (!incoming && requests_satisfied != NULL)
602     {
603       GList *l;
604       gboolean found = FALSE;
605
606       l = cd->outstanding_requests;
607
608       while (l != NULL)
609         {
610           DispatcherRequestData *d = (DispatcherRequestData *) l->data;
611           guint n;
612           const gchar *path;
613
614           l = g_list_next (l);
615           if (d->request == NULL)
616             continue;
617
618           if (d->operation != NULL)
619             continue;
620
621           path = tp_proxy_get_object_path (d->channel_request);
622           for (n = 0; n < requests_satisfied->len ; n++)
623             {
624               const gchar *p = g_ptr_array_index (requests_satisfied, n);
625               if (!tp_strdiff (p, path))
626                 {
627                   DEBUG ("Channel satified request %s"
628                    "(already dispatched: %d)", p, found);
629                   if (!found)
630                     {
631                       d->operation = operation;
632                       found = TRUE;
633                       continue;
634                     }
635                   else
636                     {
637                       GError err = { TP_ERRORS, TP_ERROR_NOT_YOURS,
638                         "Not yours!" };
639                       dispatcher_request_failed (dispatcher, d, &err);
640                     }
641                 }
642             }
643         }
644     }
645   dispatcher_start_dispatching (dispatcher, operation, cd);
646 }
647
648 static void
649 dispatcher_connection_new_channel_with_properties (
650     EmpathyDispatcher *self,
651     TpConnection *connection,
652     const gchar *object_path,
653     GHashTable *properties,
654     const GPtrArray *requests_satisfied)
655 {
656   const gchar *channel_type;
657   guint handle_type;
658   guint handle;
659   gboolean requested;
660   gboolean valid;
661
662
663   channel_type = tp_asv_get_string (properties,
664     TP_IFACE_CHANNEL ".ChannelType");
665   if (channel_type == NULL)
666     {
667       g_message ("%s had an invalid ChannelType property", object_path);
668       return;
669     }
670
671   handle_type = tp_asv_get_uint32 (properties,
672     TP_IFACE_CHANNEL ".TargetHandleType", &valid);
673   if (!valid)
674     {
675       g_message ("%s had an invalid TargetHandleType property", object_path);
676       return;
677     }
678
679   handle = tp_asv_get_uint32 (properties,
680     TP_IFACE_CHANNEL ".TargetHandle", &valid);
681   if (!valid)
682     {
683       g_message ("%s had an invalid TargetHandle property", object_path);
684       return;
685     }
686
687   /* We assume there is no channel dispather, so we're the only one dispatching
688    * it. Which means that a requested channel it is outgoing one */
689   requested = tp_asv_get_boolean (properties,
690     TP_IFACE_CHANNEL ".Requested", &valid);
691   if (!valid)
692     {
693       g_message ("%s had an invalid Requested property", object_path);
694       requested = FALSE;
695     }
696
697   dispatcher_connection_new_channel (self, connection,
698     object_path, channel_type, handle_type, handle, properties, !requested,
699     requests_satisfied);
700 }
701
702 static void
703 dispatcher_connection_got_all (TpProxy *proxy,
704                                GHashTable *properties,
705                                const GError *error,
706                                gpointer user_data,
707                                GObject *object)
708 {
709   EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
710   EmpathyDispatcherPriv *priv = GET_PRIV (self);
711   GPtrArray *requestable_channels;
712   GPtrArray *existing_channels;
713
714   if (error) {
715     DEBUG ("Error: %s", error->message);
716     return;
717   }
718
719   requestable_channels = tp_asv_get_boxed (properties,
720     "RequestableChannelClasses", TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST);
721
722   if (requestable_channels == NULL)
723     DEBUG ("No RequestableChannelClasses property !?! on connection");
724   else
725     {
726       ConnectionData *cd;
727       GList *requests, *l;
728       FindChannelRequest *request;
729       GList *retval;
730
731       cd = g_hash_table_lookup (priv->connections, proxy);
732       g_assert (cd != NULL);
733
734       cd->requestable_channels = g_boxed_copy (
735         TP_ARRAY_TYPE_REQUESTABLE_CHANNEL_CLASS_LIST, requestable_channels);
736
737       requests = g_hash_table_lookup (priv->outstanding_classes_requests,
738           proxy);
739
740       for (l = requests; l != NULL; l = l->next)
741         {
742           request = l->data;
743
744           retval = empathy_dispatcher_find_channel_classes (self,
745               TP_CONNECTION (proxy), request->channel_type,
746               request->handle_type, request->properties);
747           request->callback (retval, request->user_data);
748
749           free_find_channel_request (request);
750           g_list_free (retval);
751         }
752
753       g_list_free (requests);
754
755       g_hash_table_remove (priv->outstanding_classes_requests, proxy);
756     }
757
758   existing_channels = tp_asv_get_boxed (properties,
759       "Channels", TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST);
760
761   if (existing_channels != NULL)
762     {
763       guint idx;
764
765       for (idx = 0; idx < existing_channels->len; idx++)
766         {
767           GValueArray *values = g_ptr_array_index (existing_channels, idx);
768           const gchar *object_path;
769           GHashTable *props;
770
771           object_path = g_value_get_boxed (g_value_array_get_nth (values, 0));
772           props = g_value_get_boxed (g_value_array_get_nth (values, 1));
773
774           if (tp_strdiff (tp_asv_get_string (props,
775                       TP_IFACE_CHANNEL ".ChannelType"),
776                   TP_IFACE_CHANNEL_TYPE_TEXT))
777             continue;
778
779           dispatcher_connection_new_channel_with_properties (self,
780               TP_CONNECTION (proxy), object_path, properties, NULL);
781         }
782     }
783 }
784
785 static void
786 dispatcher_connection_advertise_capabilities_cb (TpConnection    *connection,
787                                                  const GPtrArray *capabilities,
788                                                  const GError    *error,
789                                                  gpointer         user_data,
790                                                  GObject         *self)
791 {
792   if (error)
793     DEBUG ("Error: %s", error->message);
794 }
795
796 static void
797 connection_ready_cb (TpConnection *connection,
798     const GError *error,
799     gpointer user_data)
800 {
801   EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
802   GPtrArray   *capabilities;
803   GType        cap_type;
804   GValue       cap = {0, };
805   const gchar *remove_ = NULL;
806
807   if (error != NULL)
808     {
809       DEBUG ("Error: %s", error->message);
810       goto out;
811     }
812
813   if (tp_proxy_has_interface_by_id (TP_PROXY (connection),
814       TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
815     {
816       tp_cli_dbus_properties_call_get_all (connection, -1,
817         TP_IFACE_CONNECTION_INTERFACE_REQUESTS,
818         dispatcher_connection_got_all,
819         NULL, NULL, G_OBJECT (self));
820     }
821
822   /* Advertise VoIP capabilities */
823   capabilities = g_ptr_array_sized_new (1);
824   cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
825     G_TYPE_UINT, G_TYPE_INVALID);
826   g_value_init (&cap, cap_type);
827   g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
828   dbus_g_type_struct_set (&cap,
829         0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
830         1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
831            TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
832            TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN  |
833            TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P |
834            TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_ICE_UDP,
835            G_MAXUINT);
836   g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
837
838   tp_cli_connection_interface_capabilities_call_advertise_capabilities (
839     connection, -1, capabilities, &remove_,
840     dispatcher_connection_advertise_capabilities_cb,
841     NULL, NULL, G_OBJECT (self));
842
843   g_value_unset (&cap);
844   g_ptr_array_free (capabilities, TRUE);
845 out:
846   g_object_unref (self);
847 }
848
849 static void
850 dispatcher_init_connection_if_needed (EmpathyDispatcher *self,
851     TpConnection *connection)
852 {
853   EmpathyDispatcherPriv *priv = GET_PRIV (self);
854
855   if (g_hash_table_lookup (priv->connections, connection) != NULL)
856     return;
857
858   g_hash_table_insert (priv->connections, g_object_ref (connection),
859     new_connection_data ());
860
861   g_signal_connect (connection, "invalidated",
862     G_CALLBACK (dispatcher_connection_invalidated_cb), self);
863
864   /* Ensure to keep the self object alive while the call_when_ready is
865    * running */
866   g_object_ref (self);
867   tp_connection_call_when_ready (connection, connection_ready_cb, self);
868 }
869
870 static void
871 dispatcher_status_changed_cb (TpAccount *account,
872                               guint old_status,
873                               guint new_status,
874                               guint reason,
875                               gchar *dbus_error_name,
876                               GHashTable *details,
877                               EmpathyDispatcher *self)
878 {
879   TpConnection *conn = tp_account_get_connection (account);
880
881   if (conn != NULL)
882     dispatcher_init_connection_if_needed (self, conn);
883 }
884
885 static void
886 remove_idle_handlers (gpointer key,
887                       gpointer value,
888                       gpointer user_data)
889 {
890   guint source_id;
891
892   source_id = GPOINTER_TO_UINT (value);
893   g_source_remove (source_id);
894 }
895
896 static GObject *
897 dispatcher_constructor (GType type,
898                         guint n_construct_params,
899                         GObjectConstructParam *construct_params)
900 {
901   GObject *retval;
902   EmpathyDispatcherPriv *priv;
903
904   if (dispatcher != NULL)
905     return g_object_ref (dispatcher);
906
907   retval = G_OBJECT_CLASS (empathy_dispatcher_parent_class)->constructor
908     (type, n_construct_params, construct_params);
909
910   dispatcher = EMPATHY_DISPATCHER (retval);
911   g_object_add_weak_pointer (retval, (gpointer) &dispatcher);
912
913   priv = GET_PRIV (dispatcher);
914
915   if (priv->handler == NULL)
916     priv->handler = empathy_handler_new (NULL, NULL, NULL);
917
918   empathy_handler_set_handle_channels_func (priv->handler,
919     empathy_dispatcher_handle_channels,
920     dispatcher);
921
922   empathy_handler_set_channels_func (priv->handler,
923     empathy_dispatcher_get_channels,
924     dispatcher);
925
926   return retval;
927 }
928
929 static void
930 dispatcher_dispose (GObject *object)
931 {
932   EmpathyDispatcherPriv *priv = GET_PRIV (object);
933   GHashTableIter iter;
934   gpointer connection;
935   GList *l;
936
937   if (priv->dispose_has_run)
938     return;
939
940   priv->dispose_has_run = TRUE;
941
942   for (l = priv->handlers ; l != NULL; l = g_list_next (l))
943     g_object_unref (G_OBJECT (l->data));
944
945   g_list_free (priv->handlers);
946   priv->handlers = NULL;
947
948   if (priv->handler != NULL)
949     g_object_unref (priv->handler);
950   priv->handler = NULL;
951
952   g_hash_table_iter_init (&iter, priv->connections);
953   while (g_hash_table_iter_next (&iter, &connection, NULL))
954     {
955       g_signal_handlers_disconnect_by_func (connection,
956           dispatcher_connection_invalidated_cb, object);
957     }
958
959   g_hash_table_destroy (priv->connections);
960   priv->connections = NULL;
961
962   if (priv->channel_dispatcher != NULL)
963     g_object_unref (priv->channel_dispatcher);
964   priv->channel_dispatcher = NULL;
965
966   if (priv->dbus != NULL)
967     g_object_unref (priv->dbus);
968   priv->dbus = NULL;
969
970   G_OBJECT_CLASS (empathy_dispatcher_parent_class)->dispose (object);
971 }
972
973 static void
974 dispatcher_finalize (GObject *object)
975 {
976   EmpathyDispatcherPriv *priv = GET_PRIV (object);
977   GList *l;
978   GHashTableIter iter;
979   gpointer connection;
980   GList *list;
981   gpointer account, id;
982
983   if (priv->request_channel_class_async_ids != NULL)
984     {
985       g_hash_table_foreach (priv->request_channel_class_async_ids,
986         remove_idle_handlers, NULL);
987       g_hash_table_destroy (priv->request_channel_class_async_ids);
988     }
989
990   for (l = priv->channels; l; l = l->next)
991     {
992       g_signal_handlers_disconnect_by_func (l->data,
993           dispatcher_channel_invalidated_cb, object);
994     }
995
996   g_list_free (priv->channels);
997
998   g_hash_table_iter_init (&iter, priv->outstanding_classes_requests);
999   while (g_hash_table_iter_next (&iter, &connection, (gpointer *) &list))
1000     {
1001       g_list_foreach (list, (GFunc) free_find_channel_request, NULL);
1002       g_list_free (list);
1003     }
1004
1005   g_hash_table_iter_init (&iter, priv->status_changed_handlers);
1006   while (g_hash_table_iter_next (&iter, &account, &id))
1007     {
1008       g_signal_handler_disconnect (account, GPOINTER_TO_UINT (id));
1009     }
1010   g_hash_table_destroy (priv->status_changed_handlers);
1011
1012   g_object_unref (priv->account_manager);
1013
1014   g_hash_table_destroy (priv->outstanding_classes_requests);
1015 }
1016
1017 static void
1018 dispatcher_set_property (GObject *object,
1019   guint property_id,
1020   const GValue *value,
1021   GParamSpec *pspec)
1022 {
1023   EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
1024   EmpathyDispatcherPriv *priv = GET_PRIV (self);
1025
1026   switch (property_id)
1027     {
1028       case PROP_HANDLER:
1029         priv->handler = g_value_dup_object (value);
1030         break;
1031       default:
1032         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1033         break;
1034     }
1035 }
1036
1037 static void
1038 dispatcher_get_property (GObject *object,
1039   guint property_id,
1040   GValue *value,
1041   GParamSpec *pspec)
1042 {
1043   EmpathyDispatcher *self = EMPATHY_DISPATCHER (object);
1044   EmpathyDispatcherPriv *priv = GET_PRIV (self);
1045
1046   switch (property_id)
1047     {
1048       case PROP_HANDLER:
1049         g_value_set_object (value, priv->handler);
1050         break;
1051       default:
1052         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1053         break;
1054     }
1055 }
1056
1057 static void
1058 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
1059 {
1060   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1061   GParamSpec *param_spec;
1062
1063   object_class->dispose = dispatcher_dispose;
1064   object_class->finalize = dispatcher_finalize;
1065   object_class->constructor = dispatcher_constructor;
1066
1067   object_class->get_property = dispatcher_get_property;
1068   object_class->set_property = dispatcher_set_property;
1069
1070   param_spec = g_param_spec_object ("handler", "handler",
1071     "The main Telepathy Client Hander object",
1072     EMPATHY_TYPE_HANDLER,
1073     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY);
1074   g_object_class_install_property (object_class,
1075     PROP_HANDLER, param_spec);
1076
1077   signals[OBSERVE] =
1078     g_signal_new ("observe",
1079       G_TYPE_FROM_CLASS (klass),
1080       G_SIGNAL_RUN_LAST,
1081       0,
1082       NULL, NULL,
1083       g_cclosure_marshal_VOID__OBJECT,
1084       G_TYPE_NONE,
1085       1, EMPATHY_TYPE_DISPATCH_OPERATION);
1086
1087   signals[APPROVE] =
1088     g_signal_new ("approve",
1089       G_TYPE_FROM_CLASS (klass),
1090       G_SIGNAL_RUN_LAST,
1091       0,
1092       NULL, NULL,
1093       g_cclosure_marshal_VOID__OBJECT,
1094       G_TYPE_NONE,
1095       1, EMPATHY_TYPE_DISPATCH_OPERATION);
1096
1097   signals[DISPATCH] =
1098     g_signal_new ("dispatch",
1099       G_TYPE_FROM_CLASS (klass),
1100       G_SIGNAL_RUN_LAST,
1101       0,
1102       NULL, NULL,
1103       g_cclosure_marshal_VOID__OBJECT,
1104       G_TYPE_NONE,
1105       1, EMPATHY_TYPE_DISPATCH_OPERATION);
1106
1107
1108   g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
1109 }
1110
1111 static void
1112 connect_account (EmpathyDispatcher *self,
1113     TpAccount *account)
1114 {
1115   EmpathyDispatcherPriv *priv = GET_PRIV (self);
1116   TpConnection *conn = tp_account_get_connection (account);
1117   gulong id;
1118
1119   id = GPOINTER_TO_UINT (g_hash_table_lookup (priv->status_changed_handlers,
1120         account));
1121
1122   if (id != 0)
1123     return;
1124
1125   if (conn != NULL)
1126     dispatcher_status_changed_cb (account, 0, 0, 0, NULL, NULL, self);
1127
1128   id = g_signal_connect (account, "status-changed",
1129       G_CALLBACK (dispatcher_status_changed_cb), self);
1130
1131   g_hash_table_insert (priv->status_changed_handlers, account,
1132       GUINT_TO_POINTER (id));
1133 }
1134
1135 static void
1136 account_manager_prepared_cb (GObject *source_object,
1137                              GAsyncResult *result,
1138                              gpointer user_data)
1139 {
1140   GList *accounts, *l;
1141   EmpathyDispatcher *self = user_data;
1142   TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
1143   GError *error = NULL;
1144
1145   if (!tp_account_manager_prepare_finish (account_manager, result, &error))
1146     {
1147       DEBUG ("Failed to prepare account manager: %s", error->message);
1148       g_error_free (error);
1149       return;
1150     }
1151
1152   accounts = tp_account_manager_get_valid_accounts (account_manager);
1153   for (l = accounts; l; l = l->next)
1154     {
1155       TpAccount *a = l->data;
1156
1157       connect_account (self, a);
1158     }
1159   g_list_free (accounts);
1160 }
1161
1162 static void
1163 account_prepare_cb (GObject *source_object,
1164     GAsyncResult *result,
1165     gpointer user_data)
1166 {
1167   EmpathyDispatcher *self = user_data;
1168   TpAccount *account = TP_ACCOUNT (source_object);
1169   GError *error = NULL;
1170
1171   if (!tp_account_prepare_finish (account, result, &error))
1172     {
1173       DEBUG ("Failed to prepare account: %s", error->message);
1174       g_error_free (error);
1175       return;
1176     }
1177
1178   connect_account (self, account);
1179 }
1180
1181 static void
1182 account_validity_changed_cb (TpAccountManager *manager,
1183     TpAccount *account,
1184     gboolean valid,
1185     gpointer user_data)
1186 {
1187   if (!valid)
1188     return;
1189
1190   tp_account_prepare_async (account, NULL, account_prepare_cb, user_data);
1191 }
1192
1193 static void
1194 empathy_dispatcher_init (EmpathyDispatcher *self)
1195 {
1196   EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1197     EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
1198
1199   self->priv = priv;
1200   priv->account_manager = tp_account_manager_dup ();
1201
1202   priv->connections = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1203     g_object_unref, (GDestroyNotify) free_connection_data);
1204
1205   priv->outstanding_classes_requests = g_hash_table_new_full (g_direct_hash,
1206     g_direct_equal, g_object_unref, NULL);
1207
1208   priv->channels = NULL;
1209
1210   tp_account_manager_prepare_async (priv->account_manager, NULL,
1211       account_manager_prepared_cb, self);
1212
1213   empathy_signal_connect_weak (priv->account_manager,
1214       "account-validity-changed", G_CALLBACK (account_validity_changed_cb),
1215       G_OBJECT (self));
1216
1217   priv->request_channel_class_async_ids = g_hash_table_new (g_direct_hash,
1218     g_direct_equal);
1219   priv->status_changed_handlers = g_hash_table_new (g_direct_hash,
1220       g_direct_equal);
1221
1222   priv->dbus = tp_dbus_daemon_dup (NULL);
1223   priv->channel_dispatcher = tp_channel_dispatcher_new (priv->dbus);
1224 }
1225
1226 EmpathyDispatcher *
1227 empathy_dispatcher_new (const gchar *name,
1228   GPtrArray *filters,
1229   GStrv capabilities)
1230 {
1231   EmpathyHandler *handler;
1232   EmpathyDispatcher *ret;
1233
1234   g_assert (dispatcher == NULL);
1235   handler = empathy_handler_new (name, filters, capabilities);
1236
1237   ret = EMPATHY_DISPATCHER (
1238     g_object_new (EMPATHY_TYPE_DISPATCHER,
1239       "handler", handler,
1240       NULL));
1241   g_object_unref (handler);
1242
1243   return ret;
1244 }
1245
1246 EmpathyDispatcher *
1247 empathy_dispatcher_dup_singleton (void)
1248 {
1249   return EMPATHY_DISPATCHER (g_object_new (EMPATHY_TYPE_DISPATCHER, NULL));
1250 }
1251
1252 static void
1253 dispatcher_request_failed (EmpathyDispatcher *self,
1254                            DispatcherRequestData *request_data,
1255                            const GError *error)
1256 {
1257   EmpathyDispatcherPriv *priv = GET_PRIV (self);
1258   ConnectionData *conn_data;
1259
1260   conn_data = g_hash_table_lookup (priv->connections,
1261       request_data->connection);
1262   if (request_data->cb != NULL)
1263     request_data->cb (NULL, error, request_data->user_data);
1264
1265   if (conn_data != NULL)
1266     {
1267       conn_data->outstanding_requests =
1268           g_list_remove (conn_data->outstanding_requests, request_data);
1269     }
1270   /* else Connection has been invalidated */
1271
1272   free_dispatcher_request_data (request_data);
1273 }
1274
1275 static void
1276 dispatcher_connection_new_requested_channel (EmpathyDispatcher *self,
1277   DispatcherRequestData *request_data,
1278   const gchar *object_path,
1279   GHashTable *properties,
1280   const GError *error)
1281 {
1282   EmpathyDispatcherPriv *priv = GET_PRIV (self);
1283   EmpathyDispatchOperation *operation = NULL;
1284   ConnectionData *conn_data;
1285
1286   /* The DispatcherRequestData owns a ref on the self object. As the request
1287    * data could be destroyed (when calling dispatcher_request_failed for
1288    * example) we keep a ref on self to be sure it stays alive while we are
1289    * executing this function. */
1290   g_object_ref (self);
1291
1292   conn_data = g_hash_table_lookup (priv->connections,
1293     request_data->connection);
1294
1295   if (error)
1296     {
1297       DEBUG ("Channel request failed: %s", error->message);
1298
1299       dispatcher_request_failed (self, request_data, error);
1300
1301       return;
1302     }
1303
1304   operation = g_hash_table_lookup (conn_data->dispatching_channels,
1305         object_path);
1306
1307   if (operation == NULL)
1308     {
1309       DispatchData *data = g_hash_table_lookup (conn_data->dispatched_channels,
1310         object_path);
1311
1312       if (data != NULL)
1313         {
1314           operation = empathy_dispatch_operation_new_with_wrapper (
1315             request_data->connection,
1316             data->channel, request_data->contact, FALSE,
1317             data->channel_wrapper);
1318         }
1319       else
1320         {
1321           TpChannel *channel;
1322
1323           if (properties != NULL)
1324             channel = tp_channel_new_from_properties (request_data->connection,
1325               object_path, properties, NULL);
1326           else
1327             channel = tp_channel_new (request_data->connection, object_path,
1328               request_data->channel_type, request_data->handle_type,
1329               request_data->handle, NULL);
1330
1331           g_signal_connect (channel, "invalidated",
1332             G_CALLBACK (dispatcher_channel_invalidated_cb),
1333             request_data->dispatcher);
1334
1335           priv->channels = g_list_prepend (priv->channels, channel);
1336
1337           operation = empathy_dispatch_operation_new (request_data->connection,
1338              channel, request_data->contact, FALSE);
1339           g_object_unref (channel);
1340         }
1341     }
1342   else
1343     {
1344       /* Already existed set potential extra information */
1345       g_object_set (G_OBJECT (operation),
1346         "contact", request_data->contact,
1347         NULL);
1348     }
1349
1350   request_data->operation = operation;
1351
1352   /* (pre)-approve this right away as we requested it
1353    * This might cause the channel to be claimed, in which case the operation
1354    * will disappear. So ref it, and check the status before starting the
1355    * dispatching */
1356
1357   g_object_ref (operation);
1358   empathy_dispatch_operation_approve (operation);
1359
1360    if (empathy_dispatch_operation_get_status (operation) <
1361      EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
1362       dispatcher_start_dispatching (request_data->dispatcher, operation,
1363           conn_data);
1364
1365   g_object_unref (operation);
1366 }
1367
1368 static void
1369 dispatcher_request_channel_cb (TpConnection *connection,
1370                                const gchar  *object_path,
1371                                const GError *error,
1372                                gpointer user_data,
1373                                GObject *weak_object)
1374 {
1375   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1376   EmpathyDispatcher *self =
1377       EMPATHY_DISPATCHER (request_data->dispatcher);
1378
1379   request_data->pending_call = NULL;
1380
1381   dispatcher_connection_new_requested_channel (self,
1382     request_data, object_path, NULL, error);
1383 }
1384
1385 static void
1386 dispatcher_request_channel (DispatcherRequestData *request_data)
1387 {
1388   if (tp_proxy_has_interface_by_id (TP_PROXY (request_data->connection),
1389       TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS))
1390     {
1391       /* Extend the request_data to be a valid request */
1392       g_assert (request_data->request == NULL);
1393       request_data->request = tp_asv_new (
1394         TP_IFACE_CHANNEL ".ChannelType",
1395           G_TYPE_STRING, request_data->channel_type,
1396         TP_IFACE_CHANNEL ".TargetHandleType",
1397           G_TYPE_UINT, request_data->handle_type,
1398         NULL);
1399
1400       if (request_data->handle_type != TP_HANDLE_TYPE_NONE)
1401         tp_asv_set_uint32 (request_data->request,
1402           TP_IFACE_CHANNEL ".TargetHandle", request_data->handle);
1403
1404       empathy_dispatcher_call_create_or_ensure_channel (
1405         request_data->dispatcher, request_data);
1406     }
1407   else
1408     {
1409       request_data->pending_call = tp_cli_connection_call_request_channel (
1410         request_data->connection, -1,
1411         request_data->channel_type,
1412         request_data->handle_type,
1413         request_data->handle,
1414         TRUE, dispatcher_request_channel_cb,
1415         request_data, NULL, NULL);
1416     }
1417 }
1418
1419 void
1420 empathy_dispatcher_chat_with_contact (EmpathyContact *contact,
1421                                       EmpathyDispatcherRequestCb *callback,
1422                                       gpointer user_data)
1423 {
1424   EmpathyDispatcher *self;
1425   EmpathyDispatcherPriv *priv;
1426   TpConnection *connection;
1427   ConnectionData *connection_data;
1428   DispatcherRequestData *request_data;
1429
1430   g_return_if_fail (EMPATHY_IS_CONTACT (contact));
1431
1432   self = empathy_dispatcher_dup_singleton ();
1433   priv = GET_PRIV (self);
1434
1435   connection = empathy_contact_get_connection (contact);
1436   connection_data = g_hash_table_lookup (priv->connections, connection);
1437
1438   /* The contact handle might not be known yet */
1439   request_data = new_dispatcher_request_data (self, connection,
1440     TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_CONTACT,
1441     empathy_contact_get_handle (contact), NULL, contact, callback, user_data);
1442   request_data->should_ensure = TRUE;
1443
1444   connection_data->outstanding_requests = g_list_prepend
1445     (connection_data->outstanding_requests, request_data);
1446
1447   dispatcher_request_channel (request_data);
1448
1449   g_object_unref (self);
1450 }
1451
1452 typedef struct
1453 {
1454   EmpathyDispatcher *dispatcher;
1455   EmpathyDispatcherRequestCb *callback;
1456   gpointer user_data;
1457 } ChatWithContactIdData;
1458
1459 static void
1460 dispatcher_chat_with_contact_id_cb (EmpathyTpContactFactory *factory,
1461                                     EmpathyContact          *contact,
1462                                     const GError            *error,
1463                                     gpointer                 user_data,
1464                                     GObject                 *weak_object)
1465 {
1466   ChatWithContactIdData *data = user_data;
1467
1468   if (error)
1469     {
1470       /* FIXME: Should call data->callback with the error */
1471       DEBUG ("Error: %s", error->message);
1472     }
1473   else
1474     {
1475       empathy_dispatcher_chat_with_contact (contact, data->callback,
1476           data->user_data);
1477     }
1478
1479   g_object_unref (data->dispatcher);
1480   g_slice_free (ChatWithContactIdData, data);
1481 }
1482
1483 void
1484 empathy_dispatcher_chat_with_contact_id (TpConnection *connection,
1485                                          const gchar *contact_id,
1486                                          EmpathyDispatcherRequestCb *callback,
1487                                          gpointer user_data)
1488 {
1489   EmpathyDispatcher *self;
1490   EmpathyTpContactFactory *factory;
1491   ChatWithContactIdData *data;
1492
1493   g_return_if_fail (TP_IS_CONNECTION (connection));
1494   g_return_if_fail (!EMP_STR_EMPTY (contact_id));
1495
1496   self = empathy_dispatcher_dup_singleton ();
1497   factory = empathy_tp_contact_factory_dup_singleton (connection);
1498   data = g_slice_new0 (ChatWithContactIdData);
1499   data->dispatcher = self;
1500   data->callback = callback;
1501   data->user_data = user_data;
1502   empathy_tp_contact_factory_get_from_id (factory, contact_id,
1503       dispatcher_chat_with_contact_id_cb, data, NULL, NULL);
1504
1505   g_object_unref (factory);
1506 }
1507
1508 static void
1509 dispatcher_request_handles_cb (TpConnection *connection,
1510                                const GArray *handles,
1511                                const GError *error,
1512                                gpointer user_data,
1513                                GObject *object)
1514 {
1515   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1516
1517   request_data->pending_call = NULL;
1518
1519   if (error != NULL)
1520     {
1521       EmpathyDispatcher *self = request_data->dispatcher;
1522       EmpathyDispatcherPriv *priv = GET_PRIV (self);
1523       ConnectionData *cd;
1524
1525       cd = g_hash_table_lookup (priv->connections, request_data->connection);
1526
1527       if (request_data->cb)
1528         request_data->cb (NULL, error, request_data->user_data);
1529
1530       cd->outstanding_requests = g_list_remove (cd->outstanding_requests,
1531         request_data);
1532
1533       free_dispatcher_request_data (request_data);
1534       return;
1535     }
1536
1537   request_data->handle = g_array_index (handles, guint, 0);
1538   dispatcher_request_channel (request_data);
1539 }
1540
1541 void
1542 empathy_dispatcher_join_muc (TpConnection *connection,
1543                              const gchar *roomname,
1544                              EmpathyDispatcherRequestCb *callback,
1545                              gpointer user_data)
1546 {
1547   EmpathyDispatcher *self;
1548   EmpathyDispatcherPriv *priv;
1549   DispatcherRequestData *request_data;
1550   ConnectionData *connection_data;
1551   const gchar *names[] = { roomname, NULL };
1552
1553   g_return_if_fail (TP_IS_CONNECTION (connection));
1554   g_return_if_fail (!EMP_STR_EMPTY (roomname));
1555
1556   self = empathy_dispatcher_dup_singleton ();
1557   priv = GET_PRIV (self);
1558
1559   connection_data = g_hash_table_lookup (priv->connections, connection);
1560   g_assert (connection_data != NULL);
1561
1562   /* Don't know the room handle yet */
1563   request_data  = new_dispatcher_request_data (self, connection,
1564     TP_IFACE_CHANNEL_TYPE_TEXT, TP_HANDLE_TYPE_ROOM, 0, NULL,
1565     NULL, callback, user_data);
1566   request_data->should_ensure = TRUE;
1567
1568   connection_data->outstanding_requests = g_list_prepend
1569     (connection_data->outstanding_requests, request_data);
1570
1571   request_data->pending_call = tp_cli_connection_call_request_handles (
1572     connection, -1,
1573     TP_HANDLE_TYPE_ROOM, names,
1574     dispatcher_request_handles_cb, request_data, NULL, NULL);
1575
1576   g_object_unref (self);
1577 }
1578
1579 static void
1580 dispatcher_channel_request_failed_cb (TpChannelRequest *request,
1581   const gchar *error, const gchar *message,
1582   gpointer user_data,
1583   GObject *weak_object)
1584 {
1585   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1586   EmpathyDispatcher *self =
1587       EMPATHY_DISPATCHER (request_data->dispatcher);
1588   GError *err = NULL;
1589
1590   request_data->pending_call = NULL;
1591
1592   DEBUG ("Request failed: %s - %s %s",
1593     tp_proxy_get_object_path (request),
1594     error, message);
1595
1596   tp_proxy_dbus_error_to_gerror (TP_PROXY (request),
1597     error, message, &err);
1598
1599   dispatcher_request_failed (self, request_data, err);
1600
1601   g_error_free (err);
1602 }
1603
1604 static void
1605 dispatcher_channel_request_succeeded_cb (TpChannelRequest *request,
1606   gpointer user_data,
1607   GObject *weak_object)
1608 {
1609   EmpathyDispatcher *self = EMPATHY_DISPATCHER (weak_object);
1610   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1611   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1612   ConnectionData *conn_data;
1613
1614   conn_data = g_hash_table_lookup (priv->connections,
1615     request_data->connection);
1616
1617   DEBUG ("Request succeeded: %s", tp_proxy_get_object_path (request));
1618
1619   /* When success gets called the internal request should have been satisfied,
1620    * if it's still in outstanding_requests and doesn't have an operation
1621    * assigned to it, the channel got handled by someone else.. */
1622
1623   if (g_list_find (conn_data->outstanding_requests, request_data) == NULL)
1624     return;
1625
1626   if (request_data->operation == NULL)
1627     {
1628       GError err = { TP_ERRORS, TP_ERROR_NOT_YOURS, "Not yours!" };
1629       dispatcher_request_failed (self, request_data, &err);
1630     }
1631 }
1632
1633 static void
1634 dispatcher_channel_request_proceed_cb (TpChannelRequest *request,
1635   const GError *error,
1636   gpointer user_data,
1637   GObject *weak_object)
1638 {
1639   EmpathyDispatcher *self = EMPATHY_DISPATCHER (weak_object);
1640   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1641
1642   request_data->pending_call = NULL;
1643
1644   if (error != NULL)
1645     dispatcher_request_failed (self, request_data, error);
1646 }
1647
1648 static void
1649 dispatcher_create_channel_cb (TpChannelDispatcher *proxy,
1650     const gchar *request_path,
1651     const GError *error,
1652     gpointer user_data,
1653     GObject *weak_object)
1654 {
1655   EmpathyDispatcher *self = EMPATHY_DISPATCHER (weak_object);
1656   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1657   TpChannelRequest *request;
1658   GError *err = NULL;
1659   DispatcherRequestData *request_data = (DispatcherRequestData *) user_data;
1660
1661   request_data->pending_call = NULL;
1662
1663   if (error != NULL)
1664     {
1665       dispatcher_request_failed (self, request_data, error);
1666       return;
1667     }
1668
1669   request = tp_channel_request_new (priv->dbus, request_path, NULL, NULL);
1670   request_data->channel_request = request;
1671
1672   if (tp_cli_channel_request_connect_to_failed (request,
1673       dispatcher_channel_request_failed_cb, request_data,
1674       NULL, G_OBJECT (self), &err) == NULL)
1675     {
1676       dispatcher_request_failed (dispatcher, request_data, err);
1677       g_error_free (err);
1678       return;
1679     }
1680
1681   if (tp_cli_channel_request_connect_to_succeeded (request,
1682       dispatcher_channel_request_succeeded_cb, request_data,
1683       NULL, G_OBJECT (self), &err) == NULL)
1684     {
1685       dispatcher_request_failed (self, request_data, err);
1686       g_error_free (err);
1687       return;
1688     }
1689
1690   request_data->pending_call = tp_cli_channel_request_call_proceed (request,
1691     -1, dispatcher_channel_request_proceed_cb,
1692     request_data, NULL, G_OBJECT (self));
1693 }
1694
1695 static void
1696 empathy_dispatcher_call_create_or_ensure_channel (
1697     EmpathyDispatcher *self,
1698     DispatcherRequestData *request_data)
1699 {
1700   EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
1701   TpAccount *account;
1702
1703   account = empathy_get_account_for_connection (request_data->connection);
1704
1705   if (request_data->should_ensure)
1706     {
1707       request_data->pending_call =
1708           tp_cli_channel_dispatcher_call_ensure_channel (
1709               priv->channel_dispatcher,
1710               -1, tp_proxy_get_object_path (TP_PROXY (account)),
1711               request_data->request, 0, "",
1712               dispatcher_create_channel_cb, request_data, NULL, NULL);
1713     }
1714   else
1715     {
1716       request_data->pending_call =
1717           tp_cli_channel_dispatcher_call_create_channel (
1718               priv->channel_dispatcher,
1719               -1, tp_proxy_get_object_path (TP_PROXY (account)),
1720               request_data->request, 0, "",
1721               dispatcher_create_channel_cb, request_data, NULL,
1722               G_OBJECT (dispatcher));
1723     }
1724 }
1725
1726 /**
1727  * empathy_dispatcher_create_channel:
1728  * @self: the EmpathyDispatcher
1729  * @connection: the Connection to dispatch on
1730  * @request: an a{sv} map of properties for the request, i.e. using tp_asv_new()
1731  * @callback: a callback for when the channel arrives (or NULL)
1732  * @user_data: optional user data (or NULL)
1733  *
1734  * When calling this function, #EmpathyDispatcher takes ownership of your
1735  * reference to @request. DO NOT unref or destroy @request. When the request is
1736  * done, @request will be unreferenced. Take another reference if you want to
1737  * keep it around.
1738  */
1739 void
1740 empathy_dispatcher_create_channel (EmpathyDispatcher *self,
1741                                    TpConnection *connection,
1742                                    GHashTable *request,
1743                                    EmpathyDispatcherRequestCb *callback,
1744                                    gpointer user_data)
1745 {
1746   EmpathyDispatcherPriv *priv = GET_PRIV (self);
1747   ConnectionData *connection_data;
1748   DispatcherRequestData *request_data;
1749   const gchar *channel_type;
1750   guint handle_type;
1751   guint handle;
1752   gboolean valid;
1753
1754   g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
1755   g_return_if_fail (TP_IS_CONNECTION (connection));
1756   g_return_if_fail (request != NULL);
1757
1758   connection_data = g_hash_table_lookup (priv->connections, connection);
1759   g_assert (connection_data != NULL);
1760
1761   channel_type = tp_asv_get_string (request, TP_IFACE_CHANNEL ".ChannelType");
1762
1763   handle_type = tp_asv_get_uint32 (request,
1764     TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1765   if (!valid)
1766     handle_type = TP_UNKNOWN_HANDLE_TYPE;
1767
1768   handle = tp_asv_get_uint32 (request, TP_IFACE_CHANNEL ".TargetHandle", NULL);
1769
1770   request_data  = new_dispatcher_request_data (self, connection,
1771     channel_type, handle_type, handle, request,
1772     NULL, callback, user_data);
1773
1774   connection_data->outstanding_requests = g_list_prepend
1775     (connection_data->outstanding_requests, request_data);
1776
1777   empathy_dispatcher_call_create_or_ensure_channel (self, request_data);
1778 }
1779
1780 static gboolean
1781 channel_class_matches (GValueArray *class,
1782                        const char *channel_type,
1783                        guint handle_type,
1784                        GArray *fixed_properties)
1785 {
1786   GHashTable *fprops;
1787   GValue *v;
1788   const char *c_type;
1789   guint h_type;
1790   gboolean valid;
1791
1792   v = g_value_array_get_nth (class, 0);
1793
1794   /* if the class doesn't match channel type discard it. */
1795   fprops = g_value_get_boxed (v);
1796   c_type = tp_asv_get_string (fprops, TP_IFACE_CHANNEL ".ChannelType");
1797
1798   if (tp_strdiff (channel_type, c_type))
1799     return FALSE;
1800
1801   /* we have the right channel type, see if the handle type matches */
1802   h_type = tp_asv_get_uint32 (fprops,
1803                               TP_IFACE_CHANNEL ".TargetHandleType", &valid);
1804
1805   if (!valid || handle_type != h_type)
1806     return FALSE;
1807
1808   if (fixed_properties != NULL)
1809     {
1810       gpointer h_key, h_val;
1811       guint idx;
1812       GHashTableIter iter;
1813       gboolean found;
1814
1815       g_hash_table_iter_init (&iter, fprops);
1816
1817       while (g_hash_table_iter_next (&iter, &h_key, &h_val))
1818         {
1819           /* discard ChannelType and TargetHandleType, as we already
1820            * checked them.
1821            */
1822           if (!tp_strdiff ((char *) h_key, TP_IFACE_CHANNEL ".ChannelType") ||
1823               !tp_strdiff
1824                 ((char *) h_key, TP_IFACE_CHANNEL ".TargetHandleType"))
1825             continue;
1826
1827           found = FALSE;
1828
1829           for (idx = 0; idx < fixed_properties->len; idx++)
1830             {
1831               /* if |key| doesn't exist in |fixed_properties|, discard
1832                * the class.
1833                */
1834               if (!tp_strdiff
1835                     ((char *) h_key,
1836                      g_array_index (fixed_properties, char *, idx)))
1837                 {
1838                   found = TRUE;
1839                   /* exit the for() loop */
1840                   break;
1841                 }
1842             }
1843
1844           if (!found)
1845             return FALSE;
1846         }
1847     }
1848   else
1849     {
1850       /* if no fixed_properties are specified, discard the classes
1851        * with some fixed properties other than the two we already
1852        * checked.
1853        */
1854       if (g_hash_table_size (fprops) > 2)
1855         return FALSE;
1856     }
1857
1858   return TRUE;
1859 }
1860
1861 static GList *
1862 empathy_dispatcher_find_channel_classes (EmpathyDispatcher *self,
1863                                          TpConnection *connection,
1864                                          const gchar *channel_type,
1865                                          guint handle_type,
1866                                          GArray *fixed_properties)
1867 {
1868   EmpathyDispatcherPriv *priv = GET_PRIV (self);
1869   GValueArray *class;
1870   GPtrArray *classes;
1871   GList *matching_classes;
1872   guint i;
1873   ConnectionData *cd;
1874
1875   g_return_val_if_fail (channel_type != NULL, NULL);
1876   g_return_val_if_fail (handle_type != 0, NULL);
1877
1878   cd = g_hash_table_lookup (priv->connections, connection);
1879
1880   if (cd == NULL)
1881     return NULL;
1882
1883   classes = cd->requestable_channels;
1884   if (classes == NULL)
1885     return NULL;
1886
1887   matching_classes = NULL;
1888
1889   for (i = 0; i < classes->len; i++)
1890     {
1891       class = g_ptr_array_index (classes, i);
1892
1893       if (!channel_class_matches
1894           (class, channel_type, handle_type, fixed_properties))
1895         continue;
1896
1897       matching_classes = g_list_prepend (matching_classes, class);
1898     }
1899
1900   return matching_classes;
1901 }
1902
1903 static gboolean
1904 find_channel_class_idle_cb (gpointer user_data)
1905 {
1906   GList *retval;
1907   GList *requests;
1908   FindChannelRequest *request = user_data;
1909   ConnectionData *cd;
1910   gboolean is_ready = TRUE;
1911   EmpathyDispatcherPriv *priv = GET_PRIV (request->dispatcher);
1912
1913   g_hash_table_remove (priv->request_channel_class_async_ids, request);
1914
1915   cd = g_hash_table_lookup (priv->connections, request->connection);
1916
1917   if (cd == NULL)
1918     is_ready = FALSE;
1919   else if (cd->requestable_channels == NULL)
1920     is_ready = FALSE;
1921
1922   if (is_ready)
1923     {
1924       retval = empathy_dispatcher_find_channel_classes (request->dispatcher,
1925           request->connection, request->channel_type, request->handle_type,
1926           request->properties);
1927
1928       request->callback (retval, request->user_data);
1929       free_find_channel_request (request);
1930       g_list_free (retval);
1931
1932       return FALSE;
1933     }
1934
1935   requests = g_hash_table_lookup (priv->outstanding_classes_requests,
1936       request->connection);
1937   requests = g_list_prepend (requests, request);
1938
1939   g_hash_table_insert (priv->outstanding_classes_requests,
1940       request->connection, requests);
1941
1942   return FALSE;
1943 }
1944
1945 static GArray *
1946 setup_varargs (va_list var_args,
1947                const char *channel_namespace,
1948                const char *first_property_name)
1949 {
1950   const char *name;
1951   char *name_full;
1952   GArray *properties;
1953
1954   if (first_property_name == NULL)
1955     return NULL;
1956
1957   name = first_property_name;
1958   properties = g_array_new (TRUE, TRUE, sizeof (char *));
1959
1960   while (name != NULL)
1961     {
1962       name_full = g_strdup (name);
1963       properties = g_array_append_val (properties, name_full);
1964       name = va_arg (var_args, char *);
1965     }
1966
1967   return properties;
1968 }
1969
1970 /**
1971  * empathy_dispatcher_find_requestable_channel_classes:
1972  * @dispatcher: an #EmpathyDispatcher
1973  * @connection: a #TpConnection
1974  * @channel_type: a string identifying the type of the channel to lookup
1975  * @handle_type: the handle type for the channel
1976  * @first_property_name: %NULL, or the name of the first fixed property,
1977  * followed optionally by more names, followed by %NULL.
1978  *
1979  * Returns all the channel classes that a client can request for the connection
1980  * @connection, of the type identified by @channel_type, @handle_type and the
1981  * fixed properties list.
1982  * The classes which are compatible with a fixed properties list (i.e. those
1983  * that will be returned by this function) are intended as those that do not
1984  * contain any fixed property other than those in the list; note that this
1985  * doesn't guarantee that all the classes compatible with the list will contain
1986  * all the requested fixed properties, so the clients will have to filter
1987  * the returned list themselves.
1988  * If @first_property_name is %NULL, only the classes with no other fixed
1989  * properties than ChannelType and TargetHandleType will be returned.
1990  * Note that this function may return %NULL without performing any lookup if
1991  * @connection is not ready. To ensure that @connection is always ready,
1992  * use the empathy_dispatcher_find_requestable_channel_classes_async() variant.
1993  *
1994  * Return value: a #GList of #GValueArray objects, where the first element in
1995  * the array is a #GHashTable of the fixed properties, and the second is
1996  * a #GStrv of the allowed properties for the class. The list should be free'd
1997  * with g_list_free() when done, but the objects inside the list are owned
1998  * by the #EmpathyDispatcher and must not be modified.
1999  */
2000 GList *
2001 empathy_dispatcher_find_requestable_channel_classes
2002                                  (EmpathyDispatcher *self,
2003                                   TpConnection *connection,
2004                                   const gchar *channel_type,
2005                                   guint handle_type,
2006                                   const char *first_property_name,
2007                                   ...)
2008 {
2009   va_list var_args;
2010   GArray *properties;
2011   EmpathyDispatcherPriv *priv;
2012   GList *retval;
2013   guint idx;
2014   char *str;
2015
2016   g_return_val_if_fail (EMPATHY_IS_DISPATCHER (self), NULL);
2017   g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
2018   g_return_val_if_fail (channel_type != NULL, NULL);
2019   g_return_val_if_fail (handle_type != 0, NULL);
2020
2021   priv = GET_PRIV (self);
2022
2023   va_start (var_args, first_property_name);
2024
2025   properties = setup_varargs (var_args, channel_type, first_property_name);
2026
2027   va_end (var_args);
2028
2029   retval = empathy_dispatcher_find_channel_classes (self, connection,
2030     channel_type, handle_type, properties);
2031
2032   if (properties != NULL)
2033     {
2034       /* free the properties array */
2035       for (idx = 0; idx < properties->len ; idx++)
2036         {
2037           str = g_array_index (properties, char *, idx);
2038           g_free (str);
2039         }
2040
2041       g_array_free (properties, TRUE);
2042     }
2043
2044   return retval;
2045 }
2046
2047 /**
2048  * empathy_dispatcher_find_requestable_channel_classes_async:
2049  * @dispatcher: an #EmpathyDispatcher
2050  * @connection: a #TpConnection
2051  * @channel_type: a string identifying the type of the channel to lookup
2052  * @handle_type: the handle type for the channel
2053  * @callback: the callback to call when @connection is ready
2054  * @user_data: the user data to pass to @callback
2055  * @first_property_name: %NULL, or the name of the first fixed property,
2056  * followed optionally by more names, followed by %NULL.
2057  *
2058  * Please see the documentation of
2059  * empathy_dispatcher_find_requestable_channel_classes() for a detailed
2060  * description of this function.
2061  */
2062 void
2063 empathy_dispatcher_find_requestable_channel_classes_async
2064                                  (EmpathyDispatcher *self,
2065                                   TpConnection *connection,
2066                                   const gchar *channel_type,
2067                                   guint handle_type,
2068                                   EmpathyDispatcherFindChannelClassCb callback,
2069                                   gpointer user_data,
2070                                   const char *first_property_name,
2071                                   ...)
2072 {
2073   va_list var_args;
2074   GArray *properties;
2075   FindChannelRequest *request;
2076   EmpathyDispatcherPriv *priv;
2077   guint source_id;
2078
2079   g_return_if_fail (EMPATHY_IS_DISPATCHER (self));
2080   g_return_if_fail (TP_IS_CONNECTION (connection));
2081   g_return_if_fail (channel_type != NULL);
2082   g_return_if_fail (handle_type != 0);
2083
2084   priv = GET_PRIV (self);
2085
2086   va_start (var_args, first_property_name);
2087
2088   properties = setup_varargs (var_args, channel_type, first_property_name);
2089
2090   va_end (var_args);
2091
2092   /* append another request for this connection */
2093   request = g_slice_new0 (FindChannelRequest);
2094   request->dispatcher = g_object_ref (self);
2095   request->channel_type = g_strdup (channel_type);
2096   request->handle_type = handle_type;
2097   request->connection = connection;
2098   request->callback = callback;
2099   request->user_data = user_data;
2100   request->properties = properties;
2101
2102   source_id = g_idle_add (find_channel_class_idle_cb, request);
2103
2104   g_hash_table_insert (priv->request_channel_class_async_ids,
2105     request, GUINT_TO_POINTER (source_id));
2106 }
2107
2108 static GList *
2109 empathy_dispatcher_get_channels (EmpathyHandler *handler,
2110   gpointer user_data)
2111 {
2112   EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2113   EmpathyDispatcherPriv *priv = GET_PRIV (self);
2114
2115   return priv->channels;
2116 }
2117
2118 static gboolean
2119 empathy_dispatcher_handle_channels (EmpathyHandler *handler,
2120     const gchar *account_path,
2121     const gchar *connection_path,
2122     const GPtrArray *channels,
2123     const GPtrArray *requests_satisfied,
2124     guint64 timestamp,
2125     GHashTable *handler_info,
2126     gpointer user_data,
2127     GError **error)
2128 {
2129   EmpathyDispatcher *self = EMPATHY_DISPATCHER (user_data);
2130   EmpathyDispatcherPriv *priv = GET_PRIV (self);
2131   guint i;
2132   TpAccount *account;
2133   TpConnection *connection;
2134
2135   /* FIXME: should probably find out whether the account manager is prepared
2136    * before ensuring. See bug #600111. */
2137   account = tp_account_manager_ensure_account (priv->account_manager,
2138     account_path);
2139   g_assert (account != NULL);
2140
2141   connection = tp_account_ensure_connection (account, connection_path);
2142   if (connection == NULL)
2143     {
2144       g_set_error_literal (error, TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
2145         "Invalid connection argument");
2146       return FALSE;
2147     }
2148
2149   for (i = 0; i < channels->len ; i++)
2150     {
2151       GValueArray *arr = g_ptr_array_index (channels, i);
2152       const gchar *object_path;
2153       GHashTable *properties;
2154
2155       object_path = g_value_get_boxed (g_value_array_get_nth (arr, 0));
2156       properties = g_value_get_boxed (g_value_array_get_nth (arr, 1));
2157
2158       dispatcher_connection_new_channel_with_properties (self,
2159         connection, object_path, properties, requests_satisfied);
2160     }
2161
2162   return TRUE;
2163 }
2164
2165
2166 EmpathyHandler *
2167 empathy_dispatcher_add_handler (EmpathyDispatcher *self,
2168     const gchar *name,
2169     GPtrArray *filters,
2170     GStrv capabilities)
2171 {
2172   EmpathyDispatcherPriv *priv = GET_PRIV (self);
2173   EmpathyHandler *handler;
2174
2175   handler = empathy_handler_new (name, filters, capabilities);
2176   priv->handlers = g_list_prepend (priv->handlers, handler);
2177
2178   /* Only set the handle_channels function, the Channel property on the main
2179    * handler will always report all dispatched channels even if they came from
2180    * a different Handler */
2181   empathy_handler_set_handle_channels_func (handler,
2182     empathy_dispatcher_handle_channels, self);
2183
2184   return handler;
2185 }
2186
2187 void
2188 empathy_dispatcher_remove_handler (EmpathyDispatcher *self,
2189   EmpathyHandler *handler)
2190 {
2191   EmpathyDispatcherPriv *priv = GET_PRIV (self);
2192   GList *h;
2193
2194   h = g_list_find (priv->handlers, handler);
2195   g_return_if_fail (h != NULL);
2196
2197   priv->handlers = g_list_delete_link (priv->handlers, h);
2198
2199   g_object_unref (handler);
2200 }