]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tube-handler.c
tube-handler: fix functions definition style
[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   EmpathyTpTube *tube;
63 } IdleData;
64
65 static void
66 tube_ready_cb (EmpathyTpTube *tube,
67     const GError *error,
68     gpointer user_data,
69     GObject *weak_object)
70 {
71   IdleData *idle_data = user_data;
72
73   g_signal_emit (idle_data->thandler, signals[NEW_TUBE], 0, tube);
74 }
75
76 static void
77 tube_ready_destroy_notify (gpointer data)
78 {
79   IdleData *idle_data = data;
80
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);
86 }
87
88 static void
89 channel_ready_cb (TpChannel *channel,
90     const GError *error,
91     gpointer data)
92 {
93   IdleData *idle_data = data;
94
95   if (error != NULL)
96     {
97       DEBUG ("channel has been invalidated: %s", error->message);
98       tube_ready_destroy_notify (data);
99       g_object_unref (channel);
100       return;
101     }
102
103   idle_data->tube = empathy_tp_tube_new (channel);
104   empathy_tp_tube_call_when_ready (idle_data->tube, tube_ready_cb, idle_data,
105       tube_ready_destroy_notify, NULL);
106
107   g_object_unref (channel);
108 }
109
110 static void
111 connection_ready_cb (TpConnection *connection,
112     const GError *error,
113     gpointer data)
114 {
115   TpChannel *channel;
116   IdleData *idle_data = data;
117
118   if (error != NULL)
119     {
120       DEBUG ("connection has been invalidated: %s", error->message);
121       tube_ready_destroy_notify (data);
122       g_object_unref (connection);
123       return;
124     }
125
126   channel = tp_channel_new (connection, idle_data->channel,
127       TP_IFACE_CHANNEL_TYPE_TUBES, idle_data->handle_type,
128       idle_data->handle, NULL);
129   tp_channel_call_when_ready (channel, channel_ready_cb, idle_data);
130
131   g_object_unref (connection);
132 }
133
134 static gboolean
135 tube_handler_handle_tube_idle_cb (gpointer data)
136 {
137   IdleData *idle_data = data;
138   TpConnection *connection;
139   static TpDBusDaemon *daemon = NULL;
140
141   DEBUG ("New tube to be handled");
142
143   if (!daemon)
144     daemon = tp_dbus_daemon_new (tp_get_bus ());
145
146   connection = tp_connection_new (daemon, idle_data->bus_name,
147       idle_data->connection, NULL);
148   tp_connection_call_when_ready (connection,
149       connection_ready_cb, idle_data);
150
151   return FALSE;
152 }
153
154 static void
155 tube_handler_handle_tube (EmpSvcTubeHandler *self,
156     const gchar *bus_name,
157     const gchar *connection,
158     const gchar *channel,
159     guint handle_type,
160     guint handle,
161     DBusGMethodInvocation *context)
162 {
163   EmpathyTubeHandler *thandler = EMPATHY_TUBE_HANDLER (self);
164   IdleData *data;
165
166   data = g_slice_new (IdleData);
167   data->thandler = thandler;
168   data->bus_name = g_strdup (bus_name);
169   data->connection = g_strdup (connection);
170   data->channel = g_strdup (channel);
171   data->handle_type = handle_type;
172   data->handle = handle;
173
174   g_idle_add_full (G_PRIORITY_HIGH, tube_handler_handle_tube_idle_cb,
175       data, NULL);
176
177   emp_svc_tube_handler_return_from_handle_tube (context);
178 }
179
180 static void
181 empathy_tube_handler_class_init (EmpathyTubeHandlerClass *klass)
182 {
183   signals[NEW_TUBE] =
184       g_signal_new ("new-tube", G_OBJECT_CLASS_TYPE (klass),
185       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
186       G_TYPE_NONE, 1, EMPATHY_TYPE_TP_TUBE);
187 }
188
189 static void
190 empathy_tube_handler_iface_init (EmpSvcTubeHandlerClass *klass)
191 {
192   emp_svc_tube_handler_implement_handle_tube (klass,
193       tube_handler_handle_tube);
194 }
195
196 static void
197 empathy_tube_handler_init (EmpathyTubeHandler *thandler)
198 {
199 }
200
201 EmpathyTubeHandler *
202 empathy_tube_handler_new (TpTubeType type,
203     const gchar *service)
204 {
205   EmpathyTubeHandler *thandler = NULL;
206   DBusGProxy *proxy;
207   guint result;
208   gchar *bus_name;
209   gchar *object_path;
210   GError *error = NULL;
211
212   g_return_val_if_fail (type < NUM_TP_TUBE_TYPES, NULL);
213   g_return_val_if_fail (service != NULL, NULL);
214
215   bus_name = empathy_tube_handler_build_bus_name (type, service);
216   object_path = empathy_tube_handler_build_object_path (type, service);
217
218   proxy = dbus_g_proxy_new_for_name (tp_get_bus (), DBUS_SERVICE_DBUS,
219       DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
220
221   if (!dbus_g_proxy_call (proxy, "RequestName", &error,
222       G_TYPE_STRING, bus_name, G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
223       G_TYPE_INVALID, G_TYPE_UINT, &result, G_TYPE_INVALID))
224     {
225       DEBUG ("Failed to request name: %s",
226           error ? error->message : "No error given");
227       g_clear_error (&error);
228       goto OUT;
229     }
230
231   DEBUG ("Creating tube handler %s", bus_name);
232   thandler = g_object_new (EMPATHY_TYPE_TUBE_HANDLER, NULL);
233   dbus_g_connection_register_g_object (tp_get_bus (), object_path,
234       G_OBJECT (thandler));
235
236 OUT:
237   g_object_unref (proxy);
238   g_free (bus_name);
239   g_free (object_path);
240
241   return thandler;
242 }
243
244 static gchar *
245 sanitize_service_name (const gchar *str)
246 {
247   guint i;
248   gchar *result;
249
250   g_assert (str != NULL);
251   result = g_strdup (str);
252
253   for (i = 0; result[i] != '\0'; i++)
254     {
255       if (!g_ascii_isalnum (result[i]))
256         result[i] = '_';
257     }
258
259   return result;
260 }
261
262 gchar *
263 empathy_tube_handler_build_bus_name (TpTubeType type,
264   const gchar *service)
265 {
266   gchar *str = NULL;
267   const gchar *prefix = NULL;
268   gchar *service_escaped;
269
270   g_return_val_if_fail (type < NUM_TP_TUBE_TYPES, NULL);
271   g_return_val_if_fail (service != NULL, NULL);
272
273   if (type == TP_TUBE_TYPE_DBUS)
274     prefix = "org.gnome.Empathy.DTubeHandler.";
275   else if (type == TP_TUBE_TYPE_STREAM)
276     prefix = "org.gnome.Empathy.StreamTubeHandler.";
277   else
278     g_return_val_if_reached (NULL);
279
280   service_escaped = sanitize_service_name (service);
281   str = g_strconcat (prefix, service_escaped, NULL);
282   g_free (service_escaped);
283
284   return str;
285 }
286
287 gchar *
288 empathy_tube_handler_build_object_path (TpTubeType type,
289   const gchar *service)
290 {
291   gchar *bus_name;
292   gchar *str;
293
294   g_return_val_if_fail (type < NUM_TP_TUBE_TYPES, NULL);
295   g_return_val_if_fail (service != NULL, NULL);
296
297   bus_name = empathy_tube_handler_build_bus_name (type, service);
298   str = g_strdelimit (g_strdup_printf ("/%s", bus_name), ".", '/');
299   g_free (bus_name);
300
301   return str;
302 }