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