aboutsummaryrefslogtreecommitdiffstats
path: root/libempathy/empathy-tube-handler.c
blob: 2b50ec852ed8ffbc20d6064952d08897dbed3bd7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright (C) 2008 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Xavier Claessens <xclaesse@gmail.com>
 *          Elliot Fairweather <elliot.fairweather@collabora.co.uk>
 */

#include <config.h>

#include <dbus/dbus-glib.h>

#include <telepathy-glib/dbus.h>
#include <telepathy-glib/connection.h>
#include <telepathy-glib/channel.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/util.h>

#include <extensions/extensions.h>

#include "empathy-tube-handler.h"

#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
#include "empathy-debug.h"

static void empathy_tube_handler_iface_init (EmpSvcTubeHandlerClass *klass);

enum
{
  NEW_TUBE,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

G_DEFINE_TYPE_WITH_CODE (EmpathyTubeHandler, empathy_tube_handler,
    G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (EMP_TYPE_SVC_TUBE_HANDLER,
    empathy_tube_handler_iface_init))

typedef struct
{
  EmpathyTubeHandler *thandler;
  gchar *bus_name;
  gchar *connection;
  gchar *channel;
  guint handle_type;
  guint handle;
  TpChannel *tube;
} IdleData;

static void
tube_ready_destroy_notify (gpointer data)
{
  IdleData *idle_data = data;

  g_object_unref (idle_data->tube);
  g_free (idle_data->bus_name);
  g_free (idle_data->connection);
  g_free (idle_data->channel);
  g_slice_free (IdleData, idle_data);
}

static void
channel_ready_cb (TpChannel *channel,
    const GError *error,
    gpointer data)
{
  IdleData *idle_data = data;

  if (error != NULL)
    {
      DEBUG ("channel has been invalidated: %s", error->message);
      tube_ready_destroy_notify (data);
      g_object_unref (channel);
      return;
    }

  g_signal_emit (idle_data->thandler, signals[NEW_TUBE], 0, idle_data->tube);

  g_object_unref (channel);
}

static void
connection_ready_cb (TpConnection *connection,
    const GError *error,
    gpointer data)
{
  TpChannel *channel;
  IdleData *idle_data = data;

  if (error != NULL)
    {
      DEBUG ("connection has been invalidated: %s", error->message);
      tube_ready_destroy_notify (data);
      g_object_unref (connection);
      return;
    }

  channel = tp_channel_new (connection, idle_data->channel,
      TP_IFACE_CHANNEL_TYPE_TUBES, idle_data->handle_type,
      idle_data->handle, NULL);
  tp_channel_call_when_ready (channel, channel_ready_cb, idle_data);

  g_object_unref (connection);
}

static gboolean
tube_handler_handle_tube_idle_cb (gpointer data)
{
  IdleData *idle_data = data;
  TpConnection *connection;
  static TpDBusDaemon *daemon = NULL;

  DEBUG ("New tube to be handled");

  if (!daemon)
    daemon = tp_dbus_daemon_new (tp_get_bus ());

  connection = tp_connection_new (daemon, idle_data->bus_name,
      idle_data->connection, NULL);
  tp_connection_call_when_ready (connection,
      connection_ready_cb, idle_data);

  return FALSE;
}

static void
tube_handler_handle_tube (EmpSvcTubeHandler *self,
    const gchar *bus_name,
    const gchar *connection,
    const gchar *channel,
    guint handle_type,
    guint handle,
    DBusGMethodInvocation *context)
{
  EmpathyTubeHandler *thandler = EMPATHY_TUBE_HANDLER (self);
  IdleData *data;

  data = g_slice_new (IdleData);
  data->thandler = thandler;
  data->bus_name = g_strdup (bus_name);
  data->connection = g_strdup (connection);
  data->channel = g_strdup (channel);
  data->handle_type = handle_type;
  data->handle = handle;

  g_idle_add_full (G_PRIORITY_HIGH, tube_handler_handle_tube_idle_cb,
      data, NULL);

  emp_svc_tube_handler_return_from_handle_tube (context);
}

static void
empathy_tube_handler_class_init (EmpathyTubeHandlerClass *klass)
{
  signals[NEW_TUBE] =
      g_signal_new ("new-tube", G_OBJECT_CLASS_TYPE (klass),
      G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
      G_TYPE_NONE, 1, TP_TYPE_CHANNEL);
}

static void
empathy_tube_handler_iface_init (EmpSvcTubeHandlerClass *klass)
{
  emp_svc_tube_handler_implement_handle_tube (klass,
      tube_handler_handle_tube);
}

static void
empathy_tube_handler_init (EmpathyTubeHandler *thandler)
{
}

EmpathyTubeHandler *
empathy_tube_handler_new (TpTubeType type,
    const gchar *service)
{
  EmpathyTubeHandler *thandler = NULL;
  DBusGProxy *proxy;
  guint result;
  gchar *bus_name;
  gchar *object_path;
  GError *error = NULL;

  g_return_val_if_fail (type < NUM_TP_TUBE_TYPES, NULL);
  g_return_val_if_fail (service != NULL, NULL);

  bus_name = empathy_tube_handler_build_bus_name (type, service);
  object_path = empathy_tube_handler_build_object_path (type, service);

  proxy = dbus_g_proxy_new_for_name (tp_get_bus (), DBUS_SERVICE_DBUS,
      DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);

  if (!dbus_g_proxy_call (proxy, "RequestName", &error,
      G_TYPE_STRING, bus_name, G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
      G_TYPE_INVALID, G_TYPE_UINT, &result, G_TYPE_INVALID))
    {
      DEBUG ("Failed to request name: %s",
          error ? error->message : "No error given");
      g_clear_error (&error);
      goto OUT;
    }

  DEBUG ("Creating tube handler %s", bus_name);
  thandler = g_object_new (EMPATHY_TYPE_TUBE_HANDLER, NULL);
  dbus_g_connection_register_g_object (tp_get_bus (), object_path,
      G_OBJECT (thandler));

OUT:
  g_object_unref (proxy);
  g_free (bus_name);
  g_free (object_path);

  return thandler;
}

static gchar *
sanitize_service_name (const gchar *str)
{
  guint i;
  gchar *result;

  g_assert (str != NULL);
  result = g_strdup (str);

  for (i = 0; result[i] != '\0'; i++)
    {
      if (!g_ascii_isalnum (result[i]))
        result[i] = '_';
    }

  return result;
}

gchar *
empathy_tube_handler_build_bus_name (TpTubeType type,
  const gchar *service)
{
  gchar *str = NULL;
  const gchar *prefix = NULL;
  gchar *service_escaped;

  g_return_val_if_fail (type < NUM_TP_TUBE_TYPES, NULL);
  g_return_val_if_fail (service != NULL, NULL);

  if (type == TP_TUBE_TYPE_DBUS)
    prefix = "org.gnome.Empathy.DTubeHandler.";
  else if (type == TP_TUBE_TYPE_STREAM)
    prefix = "org.gnome.Empathy.StreamTubeHandler.";
  else
    g_return_val_if_reached (NULL);

  service_escaped = sanitize_service_name (service);
  str = g_strconcat (prefix, service_escaped, NULL);
  g_free (service_escaped);

  return str;
}

gchar *
empathy_tube_handler_build_object_path (TpTubeType type,
  const gchar *service)
{
  gchar *bus_name;
  gchar *str;

  g_return_val_if_fail (type < NUM_TP_TUBE_TYPES, NULL);
  g_return_val_if_fail (service != NULL, NULL);

  bus_name = empathy_tube_handler_build_bus_name (type, service);
  str = g_strdelimit (g_strdup_printf ("/%s", bus_name), ".", '/');
  g_free (bus_name);

  return str;
}