]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tube-handler.c
Updated Basque language
[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-tube-handler.h"
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
37 #include "empathy-debug.h"
38
39 static void empathy_tube_handler_iface_init (EmpSvcTubeHandlerClass *klass);
40
41 enum
42 {
43   NEW_TUBE,
44   LAST_SIGNAL
45 };
46
47 static guint signals[LAST_SIGNAL];
48
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))
52
53 typedef struct
54 {
55   EmpathyTubeHandler *thandler;
56   gchar *bus_name;
57   gchar *connection;
58   gchar *channel;
59   guint handle_type;
60   guint handle;
61   TpChannel *tube;
62 } IdleData;
63
64 static void
65 tube_ready_destroy_notify (gpointer data)
66 {
67   IdleData *idle_data = data;
68
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);
74 }
75
76 static void
77 channel_ready_cb (TpChannel *channel,
78     const GError *error,
79     gpointer data)
80 {
81   IdleData *idle_data = data;
82
83   if (error != NULL)
84     {
85       DEBUG ("channel has been invalidated: %s", error->message);
86       tube_ready_destroy_notify (data);
87       g_object_unref (channel);
88       return;
89     }
90
91   g_signal_emit (idle_data->thandler, signals[NEW_TUBE], 0, idle_data->tube);
92
93   g_object_unref (channel);
94 }
95
96 static void
97 connection_ready_cb (TpConnection *connection,
98     const GError *error,
99     gpointer data)
100 {
101   TpChannel *channel;
102   IdleData *idle_data = data;
103
104   if (error != NULL)
105     {
106       DEBUG ("connection has been invalidated: %s", error->message);
107       tube_ready_destroy_notify (data);
108       g_object_unref (connection);
109       return;
110     }
111
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);
116
117   g_object_unref (connection);
118 }
119
120 static gboolean
121 tube_handler_handle_tube_idle_cb (gpointer data)
122 {
123   IdleData *idle_data = data;
124   TpConnection *connection;
125   static TpDBusDaemon *daemon = NULL;
126
127   DEBUG ("New tube to be handled");
128
129   if (!daemon)
130     daemon = tp_dbus_daemon_new (tp_get_bus ());
131
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);
136
137   return FALSE;
138 }
139
140 static void
141 tube_handler_handle_tube (EmpSvcTubeHandler *self,
142     const gchar *bus_name,
143     const gchar *connection,
144     const gchar *channel,
145     guint handle_type,
146     guint handle,
147     DBusGMethodInvocation *context)
148 {
149   EmpathyTubeHandler *thandler = EMPATHY_TUBE_HANDLER (self);
150   IdleData *data;
151
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;
159
160   g_idle_add_full (G_PRIORITY_HIGH, tube_handler_handle_tube_idle_cb,
161       data, NULL);
162
163   emp_svc_tube_handler_return_from_handle_tube (context);
164 }
165
166 static void
167 empathy_tube_handler_class_init (EmpathyTubeHandlerClass *klass)
168 {
169   signals[NEW_TUBE] =
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);
173 }
174
175 static void
176 empathy_tube_handler_iface_init (EmpSvcTubeHandlerClass *klass)
177 {
178   emp_svc_tube_handler_implement_handle_tube (klass,
179       tube_handler_handle_tube);
180 }
181
182 static void
183 empathy_tube_handler_init (EmpathyTubeHandler *thandler)
184 {
185 }
186
187 EmpathyTubeHandler *
188 empathy_tube_handler_new (TpTubeType type,
189     const gchar *service)
190 {
191   EmpathyTubeHandler *thandler = NULL;
192   DBusGProxy *proxy;
193   guint result;
194   gchar *bus_name;
195   gchar *object_path;
196   GError *error = NULL;
197
198   g_return_val_if_fail (type < NUM_TP_TUBE_TYPES, NULL);
199   g_return_val_if_fail (service != NULL, NULL);
200
201   bus_name = empathy_tube_handler_build_bus_name (type, service);
202   object_path = empathy_tube_handler_build_object_path (type, service);
203
204   proxy = dbus_g_proxy_new_for_name (tp_get_bus (), DBUS_SERVICE_DBUS,
205       DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
206
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))
210     {
211       DEBUG ("Failed to request name: %s",
212           error ? error->message : "No error given");
213       g_clear_error (&error);
214       goto OUT;
215     }
216
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));
221
222 OUT:
223   g_object_unref (proxy);
224   g_free (bus_name);
225   g_free (object_path);
226
227   return thandler;
228 }
229
230 static gchar *
231 sanitize_service_name (const gchar *str)
232 {
233   guint i;
234   gchar *result;
235
236   g_assert (str != NULL);
237   result = g_strdup (str);
238
239   for (i = 0; result[i] != '\0'; i++)
240     {
241       if (!g_ascii_isalnum (result[i]))
242         result[i] = '_';
243     }
244
245   return result;
246 }
247
248 gchar *
249 empathy_tube_handler_build_bus_name (TpTubeType type,
250   const gchar *service)
251 {
252   gchar *str = NULL;
253   const gchar *prefix = NULL;
254   gchar *service_escaped;
255
256   g_return_val_if_fail (type < NUM_TP_TUBE_TYPES, NULL);
257   g_return_val_if_fail (service != NULL, NULL);
258
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.";
263   else
264     g_return_val_if_reached (NULL);
265
266   service_escaped = sanitize_service_name (service);
267   str = g_strconcat (prefix, service_escaped, NULL);
268   g_free (service_escaped);
269
270   return str;
271 }
272
273 gchar *
274 empathy_tube_handler_build_object_path (TpTubeType type,
275   const gchar *service)
276 {
277   gchar *bus_name;
278   gchar *str;
279
280   g_return_val_if_fail (type < NUM_TP_TUBE_TYPES, NULL);
281   g_return_val_if_fail (service != NULL, NULL);
282
283   bus_name = empathy_tube_handler_build_bus_name (type, service);
284   str = g_strdelimit (g_strdup_printf ("/%s", bus_name), ".", '/');
285   g_free (bus_name);
286
287   return str;
288 }