2 * Copyright (C) 2008 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
19 * Elliot Fairweather <elliot.fairweather@collabora.co.uk>
24 #include <dbus/dbus-glib.h>
26 #include <telepathy-glib/dbus.h>
27 #include <telepathy-glib/connection.h>
28 #include <telepathy-glib/channel.h>
29 #include <telepathy-glib/interfaces.h>
30 #include <telepathy-glib/util.h>
32 #include <extensions/extensions.h>
34 #include "empathy-tp-tube.h"
35 #include "empathy-tube-handler.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include "empathy-debug.h"
40 static void empathy_tube_handler_iface_init (EmpSvcTubeHandlerClass *klass);
48 static guint signals[LAST_SIGNAL];
50 G_DEFINE_TYPE_WITH_CODE (EmpathyTubeHandler, empathy_tube_handler,
51 G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (EMP_TYPE_SVC_TUBE_HANDLER,
52 empathy_tube_handler_iface_init))
56 EmpathyTubeHandler *thandler;
66 tube_ready_cb (EmpathyTpTube *tube,
71 IdleData *idle_data = user_data;
73 g_signal_emit (idle_data->thandler, signals[NEW_TUBE], 0, tube);
77 tube_ready_destroy_notify (gpointer data)
79 IdleData *idle_data = data;
81 g_object_unref (idle_data->tube);
82 g_free (idle_data->bus_name);
83 g_free (idle_data->connection);
84 g_free (idle_data->channel);
85 g_slice_free (IdleData, idle_data);
89 tube_handler_handle_tube_idle_cb (gpointer data)
91 IdleData *idle_data = data;
92 TpConnection *connection;
94 static TpDBusDaemon *daemon = NULL;
96 DEBUG ("New tube to be handled");
99 daemon = tp_dbus_daemon_new (tp_get_bus ());
101 connection = tp_connection_new (daemon, idle_data->bus_name,
102 idle_data->connection, NULL);
103 channel = tp_channel_new (connection, idle_data->channel,
104 TP_IFACE_CHANNEL_TYPE_TUBES, idle_data->handle_type,
105 idle_data->handle, NULL);
106 tp_channel_run_until_ready (channel, NULL, NULL);
108 idle_data->tube = empathy_tp_tube_new (channel);
109 empathy_tp_tube_call_when_ready (idle_data->tube, tube_ready_cb, idle_data,
110 tube_ready_destroy_notify, NULL);
112 g_object_unref (channel);
113 g_object_unref (connection);
119 tube_handler_handle_tube (EmpSvcTubeHandler *self,
120 const gchar *bus_name,
121 const gchar *connection,
122 const gchar *channel,
125 DBusGMethodInvocation *context)
127 EmpathyTubeHandler *thandler = EMPATHY_TUBE_HANDLER (self);
130 data = g_slice_new (IdleData);
131 data->thandler = thandler;
132 data->bus_name = g_strdup (bus_name);
133 data->connection = g_strdup (connection);
134 data->channel = g_strdup (channel);
135 data->handle_type = handle_type;
136 data->handle = handle;
138 g_idle_add_full (G_PRIORITY_HIGH, tube_handler_handle_tube_idle_cb,
141 emp_svc_tube_handler_return_from_handle_tube (context);
145 empathy_tube_handler_class_init (EmpathyTubeHandlerClass *klass)
148 g_signal_new ("new-tube", G_OBJECT_CLASS_TYPE (klass),
149 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
150 G_TYPE_NONE, 1, EMPATHY_TYPE_TP_TUBE);
154 empathy_tube_handler_iface_init (EmpSvcTubeHandlerClass *klass)
156 emp_svc_tube_handler_implement_handle_tube (klass,
157 tube_handler_handle_tube);
161 empathy_tube_handler_init (EmpathyTubeHandler *thandler)
166 empathy_tube_handler_new (TpTubeType type, const gchar *service)
168 EmpathyTubeHandler *thandler = NULL;
173 GError *error = NULL;
175 g_return_val_if_fail (type < NUM_TP_TUBE_TYPES, NULL);
176 g_return_val_if_fail (service != NULL, NULL);
178 bus_name = empathy_tube_handler_build_bus_name (type, service);
179 object_path = empathy_tube_handler_build_object_path (type, service);
181 proxy = dbus_g_proxy_new_for_name (tp_get_bus (), DBUS_SERVICE_DBUS,
182 DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
184 if (!dbus_g_proxy_call (proxy, "RequestName", &error,
185 G_TYPE_STRING, bus_name, G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
186 G_TYPE_INVALID, G_TYPE_UINT, &result, G_TYPE_INVALID))
188 DEBUG ("Failed to request name: %s",
189 error ? error->message : "No error given");
190 g_clear_error (&error);
194 DEBUG ("Creating tube handler %s", bus_name);
195 thandler = g_object_new (EMPATHY_TYPE_TUBE_HANDLER, NULL);
196 dbus_g_connection_register_g_object (tp_get_bus (), object_path,
197 G_OBJECT (thandler));
200 g_object_unref (proxy);
202 g_free (object_path);
208 sanitize_service_name (const gchar *str)
213 g_assert (str != NULL);
214 result = g_strdup (str);
216 for (i = 0; result[i] != '\0'; i++)
218 if (!g_ascii_isalnum (result[i]))
226 empathy_tube_handler_build_bus_name (TpTubeType type,
227 const gchar *service)
230 const gchar *prefix = NULL;
231 gchar *service_escaped;
233 g_return_val_if_fail (type < NUM_TP_TUBE_TYPES, NULL);
234 g_return_val_if_fail (service != NULL, NULL);
236 if (type == TP_TUBE_TYPE_DBUS)
237 prefix = "org.gnome.Empathy.DTubeHandler.";
238 else if (type == TP_TUBE_TYPE_STREAM)
239 prefix = "org.gnome.Empathy.StreamTubeHandler.";
241 g_return_val_if_reached (NULL);
243 service_escaped = sanitize_service_name (service);
244 str = g_strconcat (prefix, service_escaped, NULL);
245 g_free (service_escaped);
251 empathy_tube_handler_build_object_path (TpTubeType type,
252 const gchar *service)
257 g_return_val_if_fail (type < NUM_TP_TUBE_TYPES, NULL);
258 g_return_val_if_fail (service != NULL, NULL);
260 bus_name = empathy_tube_handler_build_bus_name (type, service);
261 str = g_strdelimit (g_strdup_printf ("/%s", bus_name), ".", '/');