]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tube-handler.c
Fix the creation of bus_name and object_path for dispatching tubes. It drops escaping...
[empathy.git] / libempathy / empathy-tube-handler.c
1 /*
2  * Copyright (C) 2008 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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
17  * 
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  *          Elliot Fairweather <elliot.fairweather@collabora.co.uk>
20  */
21
22 #include <config.h>
23
24 #include <dbus/dbus-glib.h>
25
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>
31
32 #include <extensions/extensions.h>
33
34 #include "empathy-tp-tube.h"
35 #include "empathy-tube-handler.h"
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include "empathy-debug.h"
39
40 static void empathy_tube_handler_iface_init (EmpSvcTubeHandlerClass *klass);
41
42 enum
43 {
44   NEW_TUBE,
45   LAST_SIGNAL
46 };
47
48 static guint signals[LAST_SIGNAL];
49
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))
53
54 typedef struct
55 {
56   EmpathyTubeHandler *thandler;
57   gchar *bus_name;
58   gchar *connection;
59   gchar *channel;
60   guint handle_type;
61   guint handle;
62   guint id;
63 } IdleData;
64
65 static gboolean
66 tube_handler_handle_tube_idle_cb (gpointer data)
67 {
68   IdleData *idle_data = data;
69   TpConnection *connection;
70   TpChannel *channel;
71   EmpathyTpTube *tube;
72   static TpDBusDaemon *daemon = NULL;
73
74   DEBUG ("New tube to be handled id=%d", idle_data->id);
75
76   if (!daemon)
77     daemon = tp_dbus_daemon_new (tp_get_bus ());
78
79   connection = tp_connection_new (daemon, idle_data->bus_name,
80       idle_data->connection, NULL);
81   channel = tp_channel_new (connection, idle_data->channel,
82       TP_IFACE_CHANNEL_TYPE_TUBES, idle_data->handle_type,
83       idle_data->handle, NULL);
84   tp_channel_run_until_ready (channel, NULL, NULL);
85
86   tube = empathy_tp_tube_new (channel, idle_data->id);
87   g_signal_emit (idle_data->thandler, signals[NEW_TUBE], 0, tube);
88
89   g_object_unref (tube);
90   g_object_unref (channel);
91   g_object_unref (connection);
92   g_free (idle_data->bus_name);
93   g_free (idle_data->connection);
94   g_free (idle_data->channel);
95   g_slice_free (IdleData, idle_data);
96
97   return FALSE;
98 }
99
100 static void
101 tube_handler_handle_tube (EmpSvcTubeHandler *self,
102                           const gchar *bus_name,
103                           const gchar *connection,
104                           const gchar *channel,
105                           guint handle_type,
106                           guint handle,
107                           guint id,
108                           DBusGMethodInvocation *context)
109 {
110   EmpathyTubeHandler *thandler = EMPATHY_TUBE_HANDLER (self);
111   IdleData *data;
112
113   data = g_slice_new (IdleData);
114   data->thandler = thandler;
115   data->bus_name = g_strdup (bus_name);
116   data->connection = g_strdup (connection);
117   data->channel = g_strdup (channel);
118   data->handle_type = handle_type;
119   data->handle = handle;
120   data->id = id;
121
122   g_idle_add_full (G_PRIORITY_HIGH, tube_handler_handle_tube_idle_cb,
123       data, NULL);
124
125   emp_svc_tube_handler_return_from_handle_tube (context);
126 }
127
128 static void
129 empathy_tube_handler_class_init (EmpathyTubeHandlerClass *klass)
130 {
131   signals[NEW_TUBE] =
132       g_signal_new ("new-tube", G_OBJECT_CLASS_TYPE (klass),
133       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
134       G_TYPE_NONE, 1, EMPATHY_TYPE_TP_TUBE);
135 }
136
137 static void
138 empathy_tube_handler_iface_init (EmpSvcTubeHandlerClass *klass)
139 {
140   emp_svc_tube_handler_implement_handle_tube (klass,
141       tube_handler_handle_tube);
142 }
143
144 static void
145 empathy_tube_handler_init (EmpathyTubeHandler *thandler)
146 {
147 }
148
149 EmpathyTubeHandler *
150 empathy_tube_handler_new (TpTubeType type, const gchar *service)
151 {
152   EmpathyTubeHandler *thandler = NULL;
153   DBusGProxy *proxy;
154   guint result;
155   gchar *bus_name;
156   gchar *object_path;
157   GError *error = NULL;
158
159   g_return_val_if_fail (type <= TP_TUBE_TYPE_STREAM, NULL);
160   g_return_val_if_fail (service != NULL, NULL);
161
162   bus_name = empathy_tube_handler_build_bus_name (type, service);
163   object_path = empathy_tube_handler_build_object_path (type, service);
164
165   proxy = dbus_g_proxy_new_for_name (tp_get_bus (), DBUS_SERVICE_DBUS,
166       DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
167
168   if (!dbus_g_proxy_call (proxy, "RequestName", &error,
169       G_TYPE_STRING, bus_name, G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
170       G_TYPE_INVALID, G_TYPE_UINT, &result, G_TYPE_INVALID))
171     {
172       DEBUG ("Failed to request name: %s",
173           error ? error->message : "No error given");
174       g_clear_error (&error);
175       goto OUT;
176     }
177
178   thandler = g_object_new (EMPATHY_TYPE_TUBE_HANDLER, NULL);
179   dbus_g_connection_register_g_object (tp_get_bus (), object_path,
180       G_OBJECT (thandler));
181
182 OUT:
183   g_object_unref (proxy);
184   g_free (bus_name);
185   g_free (object_path);
186
187   return thandler;
188 }
189
190 gchar *
191 empathy_tube_handler_build_bus_name (TpTubeType type, const gchar *service)
192 {
193   gchar *str = NULL;
194   const gchar *prefix = NULL;
195
196   g_return_val_if_fail (type <= TP_TUBE_TYPE_STREAM, NULL);
197   g_return_val_if_fail (service != NULL, NULL);
198
199   if (type == TP_TUBE_TYPE_DBUS)
200     prefix = "org.gnome.Empathy.DTubeHandler.";
201   else if (type == TP_TUBE_TYPE_STREAM)
202     prefix = "org.gnome.Empathy.StreamTubeHandler.";
203   else
204     g_return_val_if_reached (NULL);
205
206   str = g_strconcat (prefix, service, NULL);
207
208   return str;
209 }
210
211 gchar *
212 empathy_tube_handler_build_object_path (TpTubeType type, const gchar *service)
213 {
214   gchar *bus_name;
215   gchar *str;
216
217   g_return_val_if_fail (type <= TP_TUBE_TYPE_STREAM, NULL);
218   g_return_val_if_fail (service != NULL, NULL);
219
220   bus_name = empathy_tube_handler_build_bus_name (type, service);
221   str = g_strdelimit (g_strdup_printf ("/%s", bus_name), ".", '/');
222   g_free (bus_name);
223
224   return str;
225 }
226