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