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-tube-handler.h"
36 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
37 #include "empathy-debug.h"
39 static void empathy_tube_handler_iface_init (EmpSvcTubeHandlerClass *klass);
47 static guint signals[LAST_SIGNAL];
49 G_DEFINE_TYPE_WITH_CODE (EmpathyTubeHandler, empathy_tube_handler,
50 G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (EMP_TYPE_SVC_TUBE_HANDLER,
51 empathy_tube_handler_iface_init))
55 EmpathyTubeHandler *thandler;
65 tube_ready_destroy_notify (gpointer data)
67 IdleData *idle_data = data;
69 g_object_unref (idle_data->tube);
70 g_free (idle_data->bus_name);
71 g_free (idle_data->connection);
72 g_free (idle_data->channel);
73 g_slice_free (IdleData, idle_data);
77 channel_ready_cb (TpChannel *channel,
81 IdleData *idle_data = data;
85 DEBUG ("channel has been invalidated: %s", error->message);
86 tube_ready_destroy_notify (data);
87 g_object_unref (channel);
91 g_signal_emit (idle_data->thandler, signals[NEW_TUBE], 0, idle_data->tube);
93 g_object_unref (channel);
97 connection_ready_cb (TpConnection *connection,
102 IdleData *idle_data = data;
106 DEBUG ("connection has been invalidated: %s", error->message);
107 tube_ready_destroy_notify (data);
108 g_object_unref (connection);
112 channel = tp_channel_new (connection, idle_data->channel,
113 TP_IFACE_CHANNEL_TYPE_TUBES, idle_data->handle_type,
114 idle_data->handle, NULL);
115 tp_channel_call_when_ready (channel, channel_ready_cb, idle_data);
117 g_object_unref (connection);
121 tube_handler_handle_tube_idle_cb (gpointer data)
123 IdleData *idle_data = data;
124 TpConnection *connection;
125 static TpDBusDaemon *daemon = NULL;
127 DEBUG ("New tube to be handled");
130 daemon = tp_dbus_daemon_new (tp_get_bus ());
132 connection = tp_connection_new (daemon, idle_data->bus_name,
133 idle_data->connection, NULL);
134 tp_connection_call_when_ready (connection,
135 connection_ready_cb, idle_data);
141 tube_handler_handle_tube (EmpSvcTubeHandler *self,
142 const gchar *bus_name,
143 const gchar *connection,
144 const gchar *channel,
147 DBusGMethodInvocation *context)
149 EmpathyTubeHandler *thandler = EMPATHY_TUBE_HANDLER (self);
152 data = g_slice_new (IdleData);
153 data->thandler = thandler;
154 data->bus_name = g_strdup (bus_name);
155 data->connection = g_strdup (connection);
156 data->channel = g_strdup (channel);
157 data->handle_type = handle_type;
158 data->handle = handle;
160 g_idle_add_full (G_PRIORITY_HIGH, tube_handler_handle_tube_idle_cb,
163 emp_svc_tube_handler_return_from_handle_tube (context);
167 empathy_tube_handler_class_init (EmpathyTubeHandlerClass *klass)
170 g_signal_new ("new-tube", G_OBJECT_CLASS_TYPE (klass),
171 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
172 G_TYPE_NONE, 1, TP_TYPE_CHANNEL);
176 empathy_tube_handler_iface_init (EmpSvcTubeHandlerClass *klass)
178 emp_svc_tube_handler_implement_handle_tube (klass,
179 tube_handler_handle_tube);
183 empathy_tube_handler_init (EmpathyTubeHandler *thandler)
188 empathy_tube_handler_new (TpTubeType type,
189 const gchar *service)
191 EmpathyTubeHandler *thandler = NULL;
196 GError *error = NULL;
198 g_return_val_if_fail (type < NUM_TP_TUBE_TYPES, NULL);
199 g_return_val_if_fail (service != NULL, NULL);
201 bus_name = empathy_tube_handler_build_bus_name (type, service);
202 object_path = empathy_tube_handler_build_object_path (type, service);
204 proxy = dbus_g_proxy_new_for_name (tp_get_bus (), DBUS_SERVICE_DBUS,
205 DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
207 if (!dbus_g_proxy_call (proxy, "RequestName", &error,
208 G_TYPE_STRING, bus_name, G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
209 G_TYPE_INVALID, G_TYPE_UINT, &result, G_TYPE_INVALID))
211 DEBUG ("Failed to request name: %s",
212 error ? error->message : "No error given");
213 g_clear_error (&error);
217 DEBUG ("Creating tube handler %s", bus_name);
218 thandler = g_object_new (EMPATHY_TYPE_TUBE_HANDLER, NULL);
219 dbus_g_connection_register_g_object (tp_get_bus (), object_path,
220 G_OBJECT (thandler));
223 g_object_unref (proxy);
225 g_free (object_path);
231 sanitize_service_name (const gchar *str)
236 g_assert (str != NULL);
237 result = g_strdup (str);
239 for (i = 0; result[i] != '\0'; i++)
241 if (!g_ascii_isalnum (result[i]))
249 empathy_tube_handler_build_bus_name (TpTubeType type,
250 const gchar *service)
253 const gchar *prefix = NULL;
254 gchar *service_escaped;
256 g_return_val_if_fail (type < NUM_TP_TUBE_TYPES, NULL);
257 g_return_val_if_fail (service != NULL, NULL);
259 if (type == TP_TUBE_TYPE_DBUS)
260 prefix = "org.gnome.Empathy.DTubeHandler.";
261 else if (type == TP_TUBE_TYPE_STREAM)
262 prefix = "org.gnome.Empathy.StreamTubeHandler.";
264 g_return_val_if_reached (NULL);
266 service_escaped = sanitize_service_name (service);
267 str = g_strconcat (prefix, service_escaped, NULL);
268 g_free (service_escaped);
274 empathy_tube_handler_build_object_path (TpTubeType type,
275 const gchar *service)
280 g_return_val_if_fail (type < NUM_TP_TUBE_TYPES, NULL);
281 g_return_val_if_fail (service != NULL, NULL);
283 bus_name = empathy_tube_handler_build_bus_name (type, service);
284 str = g_strdelimit (g_strdup_printf ("/%s", bus_name), ".", '/');