]> git.0d.be Git - empathy.git/blob - libempathy/empathy-dispatcher.c
c59e613c60577266daf79aacd4a9a33c7404cff1
[empathy.git] / libempathy / empathy-dispatcher.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  * 
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include <string.h>
25
26 #include <glib/gi18n.h>
27
28 #include <telepathy-glib/enums.h>
29 #include <telepathy-glib/connection.h>
30 #include <telepathy-glib/util.h>
31 #include <telepathy-glib/dbus.h>
32 #include <telepathy-glib/proxy-subclass.h>
33
34 #include <libmissioncontrol/mission-control.h>
35 #include <libmissioncontrol/mc-account.h>
36
37 #include <extensions/extensions.h>
38
39 #include "empathy-dispatcher.h"
40 #include "empathy-utils.h"
41 #include "empathy-tube-handler.h"
42 #include "empathy-contact-factory.h"
43 #include "empathy-tp-group.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         GHashTable     *connections;
53         gpointer        token;
54         MissionControl *mc;
55         GSList         *tubes;
56 } EmpathyDispatcherPriv;
57
58 G_DEFINE_TYPE (EmpathyDispatcher, empathy_dispatcher, G_TYPE_OBJECT);
59
60 enum {
61         DISPATCH_CHANNEL,
62         FILTER_CHANNEL,
63         FILTER_TUBE,
64         LAST_SIGNAL
65 };
66
67 static guint signals[LAST_SIGNAL];
68 static EmpathyDispatcher *dispatcher = NULL;
69
70 void
71 empathy_dispatcher_channel_process (EmpathyDispatcher *dispatcher,
72                                     TpChannel         *channel)
73 {
74         g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
75 }
76
77 typedef struct {
78         EmpathyDispatcherTube  public;
79         EmpathyContactFactory *factory;
80         gchar                 *bus_name;
81         gchar                 *object_path;
82         guint                  ref_count;
83         gboolean               handled;
84 } DispatcherTube;
85
86 GType
87 empathy_dispatcher_tube_get_type (void)
88 {
89         static GType type_id = 0;
90
91         if (!type_id) {
92                 type_id = g_boxed_type_register_static ("EmpathyDispatcherTube",
93                                                         (GBoxedCopyFunc) empathy_dispatcher_tube_ref,
94                                                         (GBoxedFreeFunc) empathy_dispatcher_tube_unref);
95         }
96
97         return type_id;
98 }
99
100 EmpathyDispatcherTube *
101 empathy_dispatcher_tube_ref (EmpathyDispatcherTube *data)
102 {
103         DispatcherTube *tube = (DispatcherTube*) data;
104
105         g_return_val_if_fail (tube != NULL, NULL);
106
107         tube->ref_count++;
108
109         return data;
110 }
111
112 void
113 empathy_dispatcher_tube_unref (EmpathyDispatcherTube *data)
114 {
115         DispatcherTube *tube = (DispatcherTube*) data;
116
117         g_return_if_fail (tube != NULL);
118
119         if (--tube->ref_count == 0) {
120                 if (!tube->handled) {
121                         DEBUG ("Tube can't be handled, closing");
122                         tp_cli_channel_type_tubes_call_close_tube (tube->public.channel, -1,
123                                                                    tube->public.id,
124                                                                    NULL, NULL, NULL,
125                                                                    NULL);
126                 }
127
128                 g_free (tube->bus_name);
129                 g_free (tube->object_path);
130                 g_object_unref (tube->factory);
131                 g_object_unref (tube->public.channel);
132                 g_object_unref (tube->public.initiator);
133                 g_slice_free (DispatcherTube, tube);
134         }
135 }
136
137 static void
138 dispatcher_tubes_handle_tube_cb (TpProxy      *channel,
139                                  const GError *error,
140                                  gpointer      user_data,
141                                  GObject      *dispatcher)
142 {
143         DispatcherTube *tube = user_data;
144
145         if (error) {
146                 DEBUG ("Error: %s", error->message);
147         } else {
148                 tube->handled = TRUE;
149         }
150 }
151
152 void
153 empathy_dispatcher_tube_process (EmpathyDispatcher     *dispatcher,
154                                  EmpathyDispatcherTube *user_data)
155 {
156         DispatcherTube *tube = (DispatcherTube*) user_data;
157
158         if (tube->public.activatable) {
159                 TpProxy *connection;
160                 TpProxy *thandler;
161                 gchar   *object_path;
162                 guint    handle_type;
163                 guint    handle;
164
165                 /* Create the proxy for the tube handler */
166                 thandler = g_object_new (TP_TYPE_PROXY,
167                                          "dbus-connection", tp_get_bus (),
168                                          "bus-name", tube->bus_name,
169                                          "object-path", tube->object_path,
170                                          NULL);
171                 tp_proxy_add_interface_by_id (thandler, EMP_IFACE_QUARK_TUBE_HANDLER);
172
173                 /* Give the tube to the handler */
174                 g_object_get (tube->public.channel,
175                               "connection", &connection,
176                               "object-path", &object_path,
177                               "handle_type", &handle_type,
178                               "handle", &handle,
179                               NULL);
180
181                 DEBUG ("Dispatching tube");
182                 emp_cli_tube_handler_call_handle_tube (thandler, -1,
183                                                        connection->bus_name,
184                                                        connection->object_path,
185                                                        object_path, handle_type,
186                                                        handle, tube->public.id,
187                                                        dispatcher_tubes_handle_tube_cb,
188                                                        empathy_dispatcher_tube_ref (user_data),
189                                                        (GDestroyNotify) empathy_dispatcher_tube_unref,
190                                                        G_OBJECT (dispatcher));
191
192                 g_object_unref (thandler);
193                 g_object_unref (connection);
194                 g_free (object_path);
195         }
196 }
197
198 static void
199 dispatcher_tubes_new_tube_cb (TpChannel   *channel,
200                               guint        id,
201                               guint        initiator,
202                               guint        type,
203                               const gchar *service,
204                               GHashTable  *parameters,
205                               guint        state,
206                               gpointer     user_data,
207                               GObject     *dispatcher)
208 {
209         static TpDBusDaemon   *daemon = NULL;
210         DispatcherTube        *tube;
211         McAccount             *account;
212         guint                  number;
213         gchar                **names;
214         gboolean               running = FALSE;
215         GError                *error = NULL;
216
217         /* Increase tube count */
218         number = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (channel), "tube-count"));
219         g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (++number));
220         DEBUG ("Increased tube count for channel %p: %d", channel, number);
221
222         /* We dispatch only local pending tubes */
223         if (state != TP_TUBE_STATE_LOCAL_PENDING) {
224                 return;
225         }
226
227         if (!daemon) {
228                 daemon = tp_dbus_daemon_new (tp_get_bus ());
229         }
230
231         account = empathy_channel_get_account (channel);
232         tube = g_slice_new (DispatcherTube);
233         tube->ref_count = 1;
234         tube->handled = FALSE;
235         tube->factory = empathy_contact_factory_new ();
236         tube->bus_name = empathy_tube_handler_build_bus_name (type, service);
237         tube->object_path = empathy_tube_handler_build_object_path (type, service);
238         tube->public.activatable = FALSE;
239         tube->public.id = id;
240         tube->public.channel = g_object_ref (channel);
241         tube->public.initiator = empathy_contact_factory_get_from_handle (tube->factory,
242                                                                           account,
243                                                                           initiator);
244         g_object_unref (account);
245
246         /* Check if that bus-name has an owner, if it has one that means the
247          * app is already running and we can directly give the channel. */
248         tp_cli_dbus_daemon_run_name_has_owner (daemon, -1, tube->bus_name,
249                                                &running, NULL, NULL);
250         if (running) {
251                 DEBUG ("Tube handler running");
252                 tube->public.activatable = TRUE;
253                 empathy_dispatcher_tube_process (EMPATHY_DISPATCHER (dispatcher),
254                                                  (EmpathyDispatcherTube*) tube);
255                 empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
256                 return;
257         }
258
259         /* Check if that bus-name is activatable, if not that means the
260          * application needed to handle this tube isn't installed. */
261         if (!tp_cli_dbus_daemon_run_list_activatable_names (daemon, -1,
262                                                             &names, &error,
263                                                             NULL)) {
264                 DEBUG ("Error listing activatable names: %s", error->message);
265                 g_clear_error (&error);
266         } else {
267                 gchar **name;
268
269                 for (name = names; *name; name++) {
270                         if (!tp_strdiff (*name, tube->bus_name)) {
271                                 tube->public.activatable = TRUE;
272                                 break;
273                         }
274                 }
275                 g_strfreev (names);
276         }
277
278         g_signal_emit (dispatcher, signals[FILTER_TUBE], 0, tube);
279         empathy_dispatcher_tube_unref ((EmpathyDispatcherTube*) tube);
280 }
281
282 static void
283 dispatcher_tubes_list_tubes_cb (TpChannel       *channel,
284                                 const GPtrArray *tubes,
285                                 const GError    *error,
286                                 gpointer         user_data,
287                                 GObject         *dispatcher)
288 {
289         guint i;
290
291         if (error) {
292                 DEBUG ("Error: %s", error->message);
293                 return;
294         }
295
296         for (i = 0; i < tubes->len; i++) {
297                 GValueArray *values;
298
299                 values = g_ptr_array_index (tubes, i);
300                 dispatcher_tubes_new_tube_cb (channel,
301                                               g_value_get_uint (g_value_array_get_nth (values, 0)),
302                                               g_value_get_uint (g_value_array_get_nth (values, 1)),
303                                               g_value_get_uint (g_value_array_get_nth (values, 2)),
304                                               g_value_get_string (g_value_array_get_nth (values, 3)),
305                                               g_value_get_boxed (g_value_array_get_nth (values, 4)),
306                                               g_value_get_uint (g_value_array_get_nth (values, 5)),
307                                               user_data, dispatcher);
308         }
309 }
310
311 static void
312 dispatcher_tubes_channel_invalidated_cb (TpProxy           *proxy,
313                                          guint              domain,
314                                          gint               code,
315                                          gchar             *message,
316                                          EmpathyDispatcher *dispatcher)
317 {
318         EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
319
320         DEBUG ("%s", message);
321
322         priv->tubes = g_slist_remove (priv->tubes, proxy);
323         g_object_unref (proxy);
324 }
325
326 static void
327 dispatcher_tubes_tube_closed_cb (TpChannel *channel,
328                                  guint      id,
329                                  gpointer   user_data,
330                                  GObject   *dispatcher)
331 {
332         guint number;
333
334         number = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (channel), "tube-count"));
335         if (number == 1) {
336                 DEBUG ("No more tube, closing channel");
337                 tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
338         }
339         else if (number > 1) {
340                 DEBUG ("Decrease tube count: %d", number);
341                 g_object_set_data (G_OBJECT (channel), "tube-count", GUINT_TO_POINTER (--number));
342         }
343 }
344
345 static void
346 dispatcher_tubes_handle_channel (EmpathyDispatcher *dispatcher,
347                                  TpChannel         *channel)
348 {
349         EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
350
351         DEBUG ("Called");
352
353         priv->tubes = g_slist_prepend (priv->tubes, g_object_ref (channel));
354         g_signal_connect (channel, "invalidated",
355                           G_CALLBACK (dispatcher_tubes_channel_invalidated_cb),
356                           dispatcher);
357
358         tp_cli_channel_type_tubes_connect_to_tube_closed (channel,
359                                                           dispatcher_tubes_tube_closed_cb,
360                                                           NULL, NULL,
361                                                           G_OBJECT (dispatcher), NULL);
362         tp_cli_channel_type_tubes_connect_to_new_tube (channel,
363                                                        dispatcher_tubes_new_tube_cb,
364                                                        NULL, NULL,
365                                                        G_OBJECT (dispatcher), NULL);
366         tp_cli_channel_type_tubes_call_list_tubes (channel, -1,
367                                                    dispatcher_tubes_list_tubes_cb,
368                                                    NULL, NULL,
369                                                    G_OBJECT (dispatcher));
370 }
371
372 static void
373 dispatcher_connection_invalidated_cb (TpConnection  *connection,
374                                       guint          domain,
375                                       gint           code,
376                                       gchar         *message,
377                                       EmpathyDispatcher *dispatcher)
378 {
379         EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
380         GHashTableIter         iter;
381         gpointer               key, value;
382
383         DEBUG ("Error: %s", message);
384
385         g_hash_table_iter_init (&iter, priv->connections);
386         while (g_hash_table_iter_next (&iter, &key, &value)) {
387                 if (value == connection) {
388                         g_hash_table_remove (priv->connections, key);
389                         break;
390                 }
391         }
392 }
393
394 static void chatroom_invalidated_cb (TpProxy *channel,
395                                      guint domain,
396                                                                          gint code,
397                                      gchar *message,
398                                                                          EmpathyChatroom *chatroom)
399 {
400   EmpathyChatroomManager *mgr;
401   mgr = empathy_chatroom_manager_new ();
402   gboolean favorite;
403
404   g_object_get (chatroom, "favorite", &favorite, NULL);
405
406   if (favorite)
407     {
408       /* Chatroom is in favorites so don't remove it from the manager */
409       g_object_set (chatroom, "tp-channel", NULL, NULL);
410     }
411   else
412     {
413       empathy_chatroom_manager_remove (mgr, chatroom);
414     }
415
416   g_object_unref (mgr);
417 }
418
419 static void
420 dispatcher_connection_new_channel_cb (TpConnection *connection,
421                                       const gchar  *object_path,
422                                       const gchar  *channel_type,
423                                       guint         handle_type,
424                                       guint         handle,
425                                       gboolean      suppress_handler,
426                                       gpointer      user_data,
427                                       GObject      *object)
428 {
429         EmpathyDispatcher *dispatcher = EMPATHY_DISPATCHER (object);
430         TpChannel         *channel;
431         gpointer           had_channels;
432
433         had_channels = g_object_get_data (G_OBJECT (connection), "had-channels");
434         if (had_channels == NULL) {
435                 /* ListChannels didn't return yet, return to avoid duplicate
436                  * dispatching */
437                 return;
438         }
439
440         channel = tp_channel_new (connection, object_path, channel_type,
441                                   handle_type, handle, NULL);
442         tp_channel_run_until_ready (channel, NULL, NULL);
443
444         if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TUBES)) {
445                 dispatcher_tubes_handle_channel (dispatcher, channel);
446         }
447
448   if (!tp_strdiff (channel_type, TP_IFACE_CHANNEL_TYPE_TEXT) &&
449       handle_type == TP_HANDLE_TYPE_ROOM)
450     {
451       /* Add the chatroom to the chatroom manager */
452       EmpathyChatroomManager *mgr;
453       EmpathyChatroom *chatroom;
454       GArray *handles;
455       gchar **room_ids;
456           MissionControl *mc;
457       McAccount *account;
458
459       mgr = empathy_chatroom_manager_new ();
460
461       handles = g_array_sized_new (FALSE, FALSE, sizeof (TpHandle), 1);
462       g_array_append_val (handles, handle);
463
464       tp_cli_connection_run_inspect_handles (connection, -1,
465           TP_HANDLE_TYPE_ROOM, handles, &room_ids, NULL, NULL);
466
467       mc = empathy_mission_control_new ();
468       account = mission_control_get_account_for_tpconnection (mc, connection,
469           NULL);
470
471       chatroom = empathy_chatroom_manager_find (mgr, account, room_ids[0]);
472       if (chatroom == NULL)
473         {
474           chatroom = empathy_chatroom_new (account);
475           empathy_chatroom_set_name (chatroom, room_ids[0]);
476           empathy_chatroom_manager_add (mgr, chatroom);
477         }
478       else
479         {
480           g_object_ref (chatroom);
481         }
482
483       g_object_set (chatroom, "tp-channel", channel, NULL);
484
485       g_signal_connect (channel, "invalidated",
486           G_CALLBACK (chatroom_invalidated_cb), chatroom);
487
488       g_free (room_ids[0]);
489       g_free (room_ids);
490       g_array_free (handles, TRUE);
491       g_object_unref (mc);
492       g_object_unref (account);
493       g_object_unref (chatroom);
494       g_object_unref (mgr);
495     }
496
497         if (suppress_handler) {
498                 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
499         } else {
500                 g_signal_emit (dispatcher, signals[FILTER_CHANNEL], 0, channel);
501         }
502
503         g_object_unref (channel);
504 }
505
506 static void
507 dispatcher_connection_list_channels_cb (TpConnection    *connection,
508                                         const GPtrArray *channels,
509                                         const GError    *error,
510                                         gpointer         user_data,
511                                         GObject         *dispatcher)
512 {
513         guint i;
514
515         if (error) {
516                 DEBUG ("Error: %s", error->message);
517                 return;
518         }
519
520         g_object_set_data (G_OBJECT (connection), "had-channels",
521                            GUINT_TO_POINTER (1));
522
523         for (i = 0; i < channels->len; i++) {
524                 GValueArray *values;
525
526                 values = g_ptr_array_index (channels, i);
527                 dispatcher_connection_new_channel_cb (connection,
528                         g_value_get_boxed (g_value_array_get_nth (values, 0)),
529                         g_value_get_string (g_value_array_get_nth (values, 1)),
530                         g_value_get_uint (g_value_array_get_nth (values, 2)),
531                         g_value_get_uint (g_value_array_get_nth (values, 3)),
532                         FALSE, user_data, dispatcher);
533         }
534 }
535
536 static void
537 dispatcher_connection_advertise_capabilities_cb (TpConnection    *connection,
538                                                  const GPtrArray *capabilities,
539                                                  const GError    *error,
540                                                  gpointer         user_data,
541                                                  GObject         *dispatcher)
542 {
543         if (error) {
544                 DEBUG ("Error: %s", error->message);
545         }
546 }
547
548 static void
549 dispatcher_connection_ready_cb (TpConnection  *connection,
550                                 const GError  *error,
551                                 gpointer       dispatcher)
552 {
553         GPtrArray   *capabilities;
554         GType        cap_type;
555         GValue       cap = {0, };
556         const gchar *remove = NULL;
557
558         if (error) {
559                 dispatcher_connection_invalidated_cb (connection,
560                                                       error->domain,
561                                                       error->code,
562                                                       error->message,
563                                                       dispatcher);
564                 return;
565         }
566
567         g_signal_connect (connection, "invalidated",
568                           G_CALLBACK (dispatcher_connection_invalidated_cb),
569                           dispatcher);
570         tp_cli_connection_connect_to_new_channel (connection,
571                                                   dispatcher_connection_new_channel_cb,
572                                                   NULL, NULL,
573                                                   G_OBJECT (dispatcher), NULL);
574         tp_cli_connection_call_list_channels (connection, -1,
575                                               dispatcher_connection_list_channels_cb,
576                                               NULL, NULL,
577                                               G_OBJECT (dispatcher));
578
579         /* Advertise VoIP capabilities */
580         capabilities = g_ptr_array_sized_new (1);
581         cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
582                                            G_TYPE_UINT, G_TYPE_INVALID);
583         g_value_init (&cap, cap_type);
584         g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
585         dbus_g_type_struct_set (&cap,
586                                 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
587                                 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
588                                    TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
589                                    TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN  |
590                                    TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P,
591                                 G_MAXUINT);
592         g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
593
594         tp_cli_connection_interface_capabilities_call_advertise_capabilities (
595                 connection, -1,
596                 capabilities, &remove,
597                 dispatcher_connection_advertise_capabilities_cb,
598                 NULL, NULL, G_OBJECT (dispatcher));
599         /* FIXME: Is that leaked? */
600 }
601
602 static void
603 dispatcher_update_account (EmpathyDispatcher *dispatcher,
604                            McAccount         *account)
605 {
606         EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
607         TpConnection          *connection;
608
609         connection = g_hash_table_lookup (priv->connections, account);
610         if (connection) {
611                 return;
612         }
613
614         connection = mission_control_get_tpconnection (priv->mc, account, NULL);
615         if (!connection) {
616                 return;
617         }
618
619         g_hash_table_insert (priv->connections, g_object_ref (account), connection);
620         tp_connection_call_when_ready (connection,
621                                        dispatcher_connection_ready_cb,
622                                        dispatcher);
623 }
624
625 static void
626 dispatcher_status_changed_cb (MissionControl           *mc,
627                               TpConnectionStatus        status,
628                               McPresence                presence,
629                               TpConnectionStatusReason  reason,
630                               const gchar              *unique_name,
631                               EmpathyDispatcher            *dispatcher)
632 {
633         McAccount *account;
634
635         account = mc_account_lookup (unique_name);
636         dispatcher_update_account (dispatcher, account);
637         g_object_unref (account);
638 }
639
640 static void
641 dispatcher_finalize (GObject *object)
642 {
643         EmpathyDispatcherPriv *priv = GET_PRIV (object);
644         GSList                *l;
645
646         empathy_disconnect_account_status_changed (priv->token);
647         g_object_unref (priv->mc);
648
649         for (l = priv->tubes; l; l = l->next) {
650                 g_signal_handlers_disconnect_by_func (l->data,
651                                                       dispatcher_tubes_channel_invalidated_cb,
652                                                       object);
653                 g_object_unref (l->data);
654         }
655         g_slist_free (priv->tubes);
656
657         g_hash_table_destroy (priv->connections);
658 }
659
660 static void
661 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
662 {
663         GObjectClass *object_class = G_OBJECT_CLASS (klass);
664
665         object_class->finalize = dispatcher_finalize;
666
667         signals[DISPATCH_CHANNEL] =
668                 g_signal_new ("dispatch-channel",
669                               G_TYPE_FROM_CLASS (klass),
670                               G_SIGNAL_RUN_LAST,
671                               0,
672                               NULL, NULL,
673                               g_cclosure_marshal_VOID__OBJECT,
674                               G_TYPE_NONE,
675                               1, TP_TYPE_CHANNEL);
676         signals[FILTER_CHANNEL] =
677                 g_signal_new ("filter-channel",
678                               G_TYPE_FROM_CLASS (klass),
679                               G_SIGNAL_RUN_LAST,
680                               0,
681                               NULL, NULL,
682                               g_cclosure_marshal_VOID__OBJECT,
683                               G_TYPE_NONE,
684                               1, TP_TYPE_CHANNEL);
685         signals[FILTER_TUBE] =
686                 g_signal_new ("filter-tube",
687                               G_TYPE_FROM_CLASS (klass),
688                               G_SIGNAL_RUN_LAST,
689                               0,
690                               NULL, NULL,
691                               g_cclosure_marshal_VOID__BOXED,
692                               G_TYPE_NONE,
693                               1, EMPATHY_TYPE_DISPATCHER_TUBE);
694
695         g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
696 }
697
698 static void
699 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
700 {
701         GList                 *accounts, *l;
702         EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
703                 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
704
705         dispatcher->priv = priv;
706         priv->mc = empathy_mission_control_new ();
707         priv->token = empathy_connect_to_account_status_changed (priv->mc,
708                 G_CALLBACK (dispatcher_status_changed_cb),
709                 dispatcher, NULL);
710
711         priv->connections = g_hash_table_new_full (empathy_account_hash,
712                                                    empathy_account_equal,
713                                                    g_object_unref,
714                                                    g_object_unref);
715         accounts = mc_accounts_list_by_enabled (TRUE);
716         for (l = accounts; l; l = l->next) {
717                 dispatcher_update_account (dispatcher, l->data);
718                 g_object_unref (l->data);
719         }
720         g_list_free (accounts);
721 }
722
723 EmpathyDispatcher *
724 empathy_dispatcher_new (void)
725 {
726         if (!dispatcher) {
727                 dispatcher = g_object_new (EMPATHY_TYPE_DISPATCHER, NULL);
728                 g_object_add_weak_pointer (G_OBJECT (dispatcher), (gpointer) &dispatcher);
729         } else {
730                 g_object_ref (dispatcher);
731         }
732
733         return dispatcher;
734 }
735
736 typedef struct {
737         const gchar *channel_type;
738         guint        handle_type;
739         guint        handle;
740 } DispatcherRequestData;
741
742 static void
743 dispatcher_request_channel_cb (TpConnection *connection,
744                                const gchar  *object_path,
745                                const GError *error,
746                                gpointer      user_data,
747                                GObject      *weak_object)
748 {
749         DispatcherRequestData *data = (DispatcherRequestData*) user_data;
750
751         if (error) {
752                 DEBUG ("Error: %s", error->message);
753                 return;
754         }
755
756         if (dispatcher) {
757                 TpChannel *channel;
758
759                 channel = tp_channel_new (connection, object_path,
760                                           data->channel_type,
761                                           data->handle_type,
762                                           data->handle, NULL);
763
764                 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
765         }
766 }
767
768 void
769 empathy_dispatcher_call_with_contact (EmpathyContact *contact)
770 {
771         MissionControl        *mc;
772         McAccount             *account;
773         TpConnection          *connection;
774         gchar                 *object_path;
775         TpChannel             *channel;
776         EmpathyContactFactory *factory;
777         EmpathyTpGroup        *group;
778         EmpathyContact        *self_contact;
779         GError                *error = NULL;
780
781         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
782
783         mc = empathy_mission_control_new ();
784         account = empathy_contact_get_account (contact);
785         connection = mission_control_get_tpconnection (mc, account, NULL);
786         tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
787         g_object_unref (mc);
788
789         /* We abuse of suppress_handler, TRUE means OUTGOING. The channel
790          * will be catched in EmpathyFilter */
791         if (!tp_cli_connection_run_request_channel (connection, -1,
792                                                     TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
793                                                     TP_HANDLE_TYPE_NONE,
794                                                     0,
795                                                     TRUE,
796                                                     &object_path,
797                                                     &error,
798                                                     NULL)) {
799                 DEBUG ("Couldn't request channel: %s",
800                         error ? error->message : "No error given");
801                 g_clear_error (&error);
802                 g_object_unref (connection);
803                 return;
804         }
805
806         channel = tp_channel_new (connection,
807                                   object_path, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
808                                   TP_HANDLE_TYPE_NONE, 0, NULL);
809
810         group = empathy_tp_group_new (channel);
811         empathy_run_until_ready (group);
812
813         factory = empathy_contact_factory_new ();
814         self_contact = empathy_contact_factory_get_user (factory, account);
815         empathy_contact_run_until_ready (self_contact,
816                                          EMPATHY_CONTACT_READY_HANDLE,
817                                          NULL);
818
819         empathy_tp_group_add_member (group, contact, "");
820         empathy_tp_group_add_member (group, self_contact, "");  
821
822         g_object_unref (factory);
823         g_object_unref (self_contact);
824         g_object_unref (group);
825         g_object_unref (connection);
826         g_object_unref (channel);
827         g_free (object_path);
828 }
829
830 void
831 empathy_dispatcher_call_with_contact_id (McAccount *account, const gchar *contact_id)
832 {
833         EmpathyContactFactory *factory;
834         EmpathyContact        *contact;
835
836         factory = empathy_contact_factory_new ();
837         contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
838         empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL);
839
840         empathy_dispatcher_call_with_contact (contact);
841
842         g_object_unref (contact);
843         g_object_unref (factory);
844 }
845
846 void
847 empathy_dispatcher_chat_with_contact (EmpathyContact  *contact)
848 {
849         MissionControl        *mc;
850         McAccount             *account;
851         TpConnection          *connection;
852         DispatcherRequestData *data;
853
854         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
855
856         mc = empathy_mission_control_new ();
857         account = empathy_contact_get_account (contact);
858         connection = mission_control_get_tpconnection (mc, account, NULL);
859         tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
860         g_object_unref (mc);
861
862         /* We abuse of suppress_handler, TRUE means OUTGOING. */
863         data = g_new (DispatcherRequestData, 1);
864         data->channel_type = TP_IFACE_CHANNEL_TYPE_TEXT;
865         data->handle_type = TP_HANDLE_TYPE_CONTACT;
866         data->handle = empathy_contact_get_handle (contact);
867         tp_cli_connection_call_request_channel (connection, -1,
868                                                 data->channel_type,
869                                                 data->handle_type,
870                                                 data->handle,
871                                                 TRUE,
872                                                 dispatcher_request_channel_cb,
873                                                 data, g_free,
874                                                 NULL);
875         g_object_unref (connection);
876 }
877
878 void
879 empathy_dispatcher_chat_with_contact_id (McAccount   *account,
880                                          const gchar *contact_id)
881 {
882         EmpathyContactFactory *factory;
883         EmpathyContact        *contact;
884
885         factory = empathy_contact_factory_new ();
886         contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
887         empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL);
888
889         empathy_dispatcher_chat_with_contact (contact);
890
891         g_object_unref (contact);
892         g_object_unref (factory);
893 }
894