]> git.0d.be Git - empathy.git/blob - libempathy/empathy-dispatcher.c
add joined room to EmpathyChatroomManager. Fixes bug #542176 (Guillaume Desmottes).
[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, room_ids[0]);
475           empathy_chatroom_manager_add (mgr, chatroom);
476         }
477       else
478         {
479           g_object_ref (chatroom);
480         }
481
482       g_object_set (chatroom, "tp-channel", channel, NULL);
483
484       g_signal_connect (channel, "invalidated",
485           G_CALLBACK (chatroom_invalidated_cb), chatroom);
486
487       g_free (room_ids[0]);
488       g_free (room_ids);
489       g_array_free (handles, TRUE);
490       g_object_unref (mc);
491       g_object_unref (account);
492       g_object_unref (chatroom);
493       g_object_unref (mgr);
494     }
495
496         if (suppress_handler) {
497                 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
498         } else {
499                 g_signal_emit (dispatcher, signals[FILTER_CHANNEL], 0, channel);
500         }
501
502         g_object_unref (channel);
503 }
504
505 static void
506 dispatcher_connection_list_channels_cb (TpConnection    *connection,
507                                         const GPtrArray *channels,
508                                         const GError    *error,
509                                         gpointer         user_data,
510                                         GObject         *dispatcher)
511 {
512         guint i;
513
514         if (error) {
515                 DEBUG ("Error: %s", error->message);
516                 return;
517         }
518
519         g_object_set_data (G_OBJECT (connection), "had-channels",
520                            GUINT_TO_POINTER (1));
521
522         for (i = 0; i < channels->len; i++) {
523                 GValueArray *values;
524
525                 values = g_ptr_array_index (channels, i);
526                 dispatcher_connection_new_channel_cb (connection,
527                         g_value_get_boxed (g_value_array_get_nth (values, 0)),
528                         g_value_get_string (g_value_array_get_nth (values, 1)),
529                         g_value_get_uint (g_value_array_get_nth (values, 2)),
530                         g_value_get_uint (g_value_array_get_nth (values, 3)),
531                         FALSE, user_data, dispatcher);
532         }
533 }
534
535 static void
536 dispatcher_connection_advertise_capabilities_cb (TpConnection    *connection,
537                                                  const GPtrArray *capabilities,
538                                                  const GError    *error,
539                                                  gpointer         user_data,
540                                                  GObject         *dispatcher)
541 {
542         if (error) {
543                 DEBUG ("Error: %s", error->message);
544         }
545 }
546
547 static void
548 dispatcher_connection_ready_cb (TpConnection  *connection,
549                                 const GError  *error,
550                                 gpointer       dispatcher)
551 {
552         GPtrArray   *capabilities;
553         GType        cap_type;
554         GValue       cap = {0, };
555         const gchar *remove = NULL;
556
557         if (error) {
558                 dispatcher_connection_invalidated_cb (connection,
559                                                       error->domain,
560                                                       error->code,
561                                                       error->message,
562                                                       dispatcher);
563                 return;
564         }
565
566         g_signal_connect (connection, "invalidated",
567                           G_CALLBACK (dispatcher_connection_invalidated_cb),
568                           dispatcher);
569         tp_cli_connection_connect_to_new_channel (connection,
570                                                   dispatcher_connection_new_channel_cb,
571                                                   NULL, NULL,
572                                                   G_OBJECT (dispatcher), NULL);
573         tp_cli_connection_call_list_channels (connection, -1,
574                                               dispatcher_connection_list_channels_cb,
575                                               NULL, NULL,
576                                               G_OBJECT (dispatcher));
577
578         /* Advertise VoIP capabilities */
579         capabilities = g_ptr_array_sized_new (1);
580         cap_type = dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING,
581                                            G_TYPE_UINT, G_TYPE_INVALID);
582         g_value_init (&cap, cap_type);
583         g_value_take_boxed (&cap, dbus_g_type_specialized_construct (cap_type));
584         dbus_g_type_struct_set (&cap,
585                                 0, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
586                                 1, TP_CHANNEL_MEDIA_CAPABILITY_AUDIO |
587                                    TP_CHANNEL_MEDIA_CAPABILITY_VIDEO |
588                                    TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_STUN  |
589                                    TP_CHANNEL_MEDIA_CAPABILITY_NAT_TRAVERSAL_GTALK_P2P,
590                                 G_MAXUINT);
591         g_ptr_array_add (capabilities, g_value_get_boxed (&cap));
592
593         tp_cli_connection_interface_capabilities_call_advertise_capabilities (
594                 connection, -1,
595                 capabilities, &remove,
596                 dispatcher_connection_advertise_capabilities_cb,
597                 NULL, NULL, G_OBJECT (dispatcher));
598         /* FIXME: Is that leaked? */
599 }
600
601 static void
602 dispatcher_update_account (EmpathyDispatcher *dispatcher,
603                            McAccount         *account)
604 {
605         EmpathyDispatcherPriv *priv = GET_PRIV (dispatcher);
606         TpConnection          *connection;
607
608         connection = g_hash_table_lookup (priv->connections, account);
609         if (connection) {
610                 return;
611         }
612
613         connection = mission_control_get_tpconnection (priv->mc, account, NULL);
614         if (!connection) {
615                 return;
616         }
617
618         g_hash_table_insert (priv->connections, g_object_ref (account), connection);
619         tp_connection_call_when_ready (connection,
620                                        dispatcher_connection_ready_cb,
621                                        dispatcher);
622 }
623
624 static void
625 dispatcher_status_changed_cb (MissionControl           *mc,
626                               TpConnectionStatus        status,
627                               McPresence                presence,
628                               TpConnectionStatusReason  reason,
629                               const gchar              *unique_name,
630                               EmpathyDispatcher            *dispatcher)
631 {
632         McAccount *account;
633
634         account = mc_account_lookup (unique_name);
635         dispatcher_update_account (dispatcher, account);
636         g_object_unref (account);
637 }
638
639 static void
640 dispatcher_finalize (GObject *object)
641 {
642         EmpathyDispatcherPriv *priv = GET_PRIV (object);
643         GSList                *l;
644
645         empathy_disconnect_account_status_changed (priv->token);
646         g_object_unref (priv->mc);
647
648         for (l = priv->tubes; l; l = l->next) {
649                 g_signal_handlers_disconnect_by_func (l->data,
650                                                       dispatcher_tubes_channel_invalidated_cb,
651                                                       object);
652                 g_object_unref (l->data);
653         }
654         g_slist_free (priv->tubes);
655
656         g_hash_table_destroy (priv->connections);
657 }
658
659 static void
660 empathy_dispatcher_class_init (EmpathyDispatcherClass *klass)
661 {
662         GObjectClass *object_class = G_OBJECT_CLASS (klass);
663
664         object_class->finalize = dispatcher_finalize;
665
666         signals[DISPATCH_CHANNEL] =
667                 g_signal_new ("dispatch-channel",
668                               G_TYPE_FROM_CLASS (klass),
669                               G_SIGNAL_RUN_LAST,
670                               0,
671                               NULL, NULL,
672                               g_cclosure_marshal_VOID__OBJECT,
673                               G_TYPE_NONE,
674                               1, TP_TYPE_CHANNEL);
675         signals[FILTER_CHANNEL] =
676                 g_signal_new ("filter-channel",
677                               G_TYPE_FROM_CLASS (klass),
678                               G_SIGNAL_RUN_LAST,
679                               0,
680                               NULL, NULL,
681                               g_cclosure_marshal_VOID__OBJECT,
682                               G_TYPE_NONE,
683                               1, TP_TYPE_CHANNEL);
684         signals[FILTER_TUBE] =
685                 g_signal_new ("filter-tube",
686                               G_TYPE_FROM_CLASS (klass),
687                               G_SIGNAL_RUN_LAST,
688                               0,
689                               NULL, NULL,
690                               g_cclosure_marshal_VOID__BOXED,
691                               G_TYPE_NONE,
692                               1, EMPATHY_TYPE_DISPATCHER_TUBE);
693
694         g_type_class_add_private (object_class, sizeof (EmpathyDispatcherPriv));
695 }
696
697 static void
698 empathy_dispatcher_init (EmpathyDispatcher *dispatcher)
699 {
700         GList                 *accounts, *l;
701         EmpathyDispatcherPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (dispatcher,
702                 EMPATHY_TYPE_DISPATCHER, EmpathyDispatcherPriv);
703
704         dispatcher->priv = priv;
705         priv->mc = empathy_mission_control_new ();
706         priv->token = empathy_connect_to_account_status_changed (priv->mc,
707                 G_CALLBACK (dispatcher_status_changed_cb),
708                 dispatcher, NULL);
709
710         priv->connections = g_hash_table_new_full (empathy_account_hash,
711                                                    empathy_account_equal,
712                                                    g_object_unref,
713                                                    g_object_unref);
714         accounts = mc_accounts_list_by_enabled (TRUE);
715         for (l = accounts; l; l = l->next) {
716                 dispatcher_update_account (dispatcher, l->data);
717                 g_object_unref (l->data);
718         }
719         g_list_free (accounts);
720 }
721
722 EmpathyDispatcher *
723 empathy_dispatcher_new (void)
724 {
725         if (!dispatcher) {
726                 dispatcher = g_object_new (EMPATHY_TYPE_DISPATCHER, NULL);
727                 g_object_add_weak_pointer (G_OBJECT (dispatcher), (gpointer) &dispatcher);
728         } else {
729                 g_object_ref (dispatcher);
730         }
731
732         return dispatcher;
733 }
734
735 typedef struct {
736         const gchar *channel_type;
737         guint        handle_type;
738         guint        handle;
739 } DispatcherRequestData;
740
741 static void
742 dispatcher_request_channel_cb (TpConnection *connection,
743                                const gchar  *object_path,
744                                const GError *error,
745                                gpointer      user_data,
746                                GObject      *weak_object)
747 {
748         DispatcherRequestData *data = (DispatcherRequestData*) user_data;
749
750         if (error) {
751                 DEBUG ("Error: %s", error->message);
752                 return;
753         }
754
755         if (dispatcher) {
756                 TpChannel *channel;
757
758                 channel = tp_channel_new (connection, object_path,
759                                           data->channel_type,
760                                           data->handle_type,
761                                           data->handle, NULL);
762
763                 g_signal_emit (dispatcher, signals[DISPATCH_CHANNEL], 0, channel);
764         }
765 }
766
767 void
768 empathy_dispatcher_call_with_contact (EmpathyContact *contact)
769 {
770         MissionControl        *mc;
771         McAccount             *account;
772         TpConnection          *connection;
773         gchar                 *object_path;
774         TpChannel             *channel;
775         EmpathyContactFactory *factory;
776         EmpathyTpGroup        *group;
777         EmpathyContact        *self_contact;
778         GError                *error = NULL;
779
780         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
781
782         mc = empathy_mission_control_new ();
783         account = empathy_contact_get_account (contact);
784         connection = mission_control_get_tpconnection (mc, account, NULL);
785         tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
786         g_object_unref (mc);
787
788         /* We abuse of suppress_handler, TRUE means OUTGOING. The channel
789          * will be catched in EmpathyFilter */
790         if (!tp_cli_connection_run_request_channel (connection, -1,
791                                                     TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
792                                                     TP_HANDLE_TYPE_NONE,
793                                                     0,
794                                                     TRUE,
795                                                     &object_path,
796                                                     &error,
797                                                     NULL)) {
798                 DEBUG ("Couldn't request channel: %s",
799                         error ? error->message : "No error given");
800                 g_clear_error (&error);
801                 g_object_unref (connection);
802                 return;
803         }
804
805         channel = tp_channel_new (connection,
806                                   object_path, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
807                                   TP_HANDLE_TYPE_NONE, 0, NULL);
808
809         group = empathy_tp_group_new (channel);
810         empathy_run_until_ready (group);
811
812         factory = empathy_contact_factory_new ();
813         self_contact = empathy_contact_factory_get_user (factory, account);
814         empathy_contact_run_until_ready (self_contact,
815                                          EMPATHY_CONTACT_READY_HANDLE,
816                                          NULL);
817
818         empathy_tp_group_add_member (group, contact, "");
819         empathy_tp_group_add_member (group, self_contact, "");  
820
821         g_object_unref (factory);
822         g_object_unref (self_contact);
823         g_object_unref (group);
824         g_object_unref (connection);
825         g_object_unref (channel);
826         g_free (object_path);
827 }
828
829 void
830 empathy_dispatcher_call_with_contact_id (McAccount *account, const gchar *contact_id)
831 {
832         EmpathyContactFactory *factory;
833         EmpathyContact        *contact;
834
835         factory = empathy_contact_factory_new ();
836         contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
837         empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL);
838
839         empathy_dispatcher_call_with_contact (contact);
840
841         g_object_unref (contact);
842         g_object_unref (factory);
843 }
844
845 void
846 empathy_dispatcher_chat_with_contact (EmpathyContact  *contact)
847 {
848         MissionControl        *mc;
849         McAccount             *account;
850         TpConnection          *connection;
851         DispatcherRequestData *data;
852
853         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
854
855         mc = empathy_mission_control_new ();
856         account = empathy_contact_get_account (contact);
857         connection = mission_control_get_tpconnection (mc, account, NULL);
858         tp_connection_run_until_ready (connection, FALSE, NULL, NULL);
859         g_object_unref (mc);
860
861         /* We abuse of suppress_handler, TRUE means OUTGOING. */
862         data = g_new (DispatcherRequestData, 1);
863         data->channel_type = TP_IFACE_CHANNEL_TYPE_TEXT;
864         data->handle_type = TP_HANDLE_TYPE_CONTACT;
865         data->handle = empathy_contact_get_handle (contact);
866         tp_cli_connection_call_request_channel (connection, -1,
867                                                 data->channel_type,
868                                                 data->handle_type,
869                                                 data->handle,
870                                                 TRUE,
871                                                 dispatcher_request_channel_cb,
872                                                 data, g_free,
873                                                 NULL);
874         g_object_unref (connection);
875 }
876
877 void
878 empathy_dispatcher_chat_with_contact_id (McAccount   *account,
879                                          const gchar *contact_id)
880 {
881         EmpathyContactFactory *factory;
882         EmpathyContact        *contact;
883
884         factory = empathy_contact_factory_new ();
885         contact = empathy_contact_factory_get_from_id (factory, account, contact_id);
886         empathy_contact_run_until_ready (contact, EMPATHY_CONTACT_READY_HANDLE, NULL);
887
888         empathy_dispatcher_chat_with_contact (contact);
889
890         g_object_unref (contact);
891         g_object_unref (factory);
892 }
893