~vcs-imports/telepathy-sofiasip/trunk

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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * conn-aliasing.c - Aliasing interface implementation for TpsipConnection
 * Copyright (C) 2008, 2009 Nokia Corporation
 *   @author Mikhail Zabaluev <mikhail.zabaluev@nokia.com>
 *
 * This work 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 work 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 work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "conn-aliasing.h"

#include <telepathy-glib/errors.h>
#include <telepathy-glib/gtypes.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/svc-connection.h>

#include "sip-connection-helpers.h"

#include <string.h>

#define DEBUG_FLAG TPSIP_DEBUG_CONNECTION
#include "debug.h"

static void
tpsip_connection_get_alias_flags (TpSvcConnectionInterfaceAliasing *iface,
                                  DBusGMethodInvocation *context)
{
  TpBaseConnection *base = TP_BASE_CONNECTION (iface);

  TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED (base, context);

  /* No server-side aliasing yet */
  tp_svc_connection_interface_aliasing_return_from_get_alias_flags (
      context, 0);
}

static gchar *
conn_get_default_alias (TpsipConnection *self,
                        TpHandleRepoIface *contact_handles,
                        TpHandle handle)
{
  const url_t *url;
  gchar *alias = NULL;

  /* TODO: create our custom handle repo to be able to get the URL off it.
   * Then we can reuse the contact_handles parameter */
  url = tpsip_conn_get_contact_url (self, handle);

  switch (url->url_type)
    {
    case url_sip:
      /* Return the SIP URI stripped down to [user@]host */
      if (url->url_user != NULL)
        alias = g_strdup_printf ("%s@%s",
                                 url->url_user, url->url_host);
      else
        alias = g_strdup (url->url_host);
      break;
    case url_tel:
      /* Retrieve the telephone number */
      alias = g_strdup (url->url_host);
      break;
    default:
      /* Return the handle string as is */
      alias = g_strdup (tp_handle_inspect (contact_handles, handle));
    }
  return alias;
}

static gchar *
conn_get_alias (TpsipConnection *self,
                TpHandleRepoIface *contact_handles,
                TpHandle handle)
{
  TpBaseConnection *base = (TpBaseConnection *) self;
  gchar *alias = NULL;

  if (handle == base->self_handle)
    {
      /* Get our user-settable alias from the connection property */
      g_object_get (self, "alias", &alias, NULL);
    }

  if (alias == NULL)
    alias = conn_get_default_alias (self, contact_handles, handle);

  g_assert (alias != NULL);
  DEBUG("handle %u got alias %s", handle, alias);

  return alias;
}

static void
tpsip_connection_request_aliases (TpSvcConnectionInterfaceAliasing *iface,
                                  const GArray *contacts,
                                  DBusGMethodInvocation *context)
{
  TpsipConnection *self = TPSIP_CONNECTION (iface);
  TpBaseConnection *base = (TpBaseConnection *) self;
  TpHandleRepoIface *contact_handles;
  GArray *aliases;
  gchar **res;
  GError *error = NULL;
  guint i;

  TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED (base, context);

  contact_handles = tp_base_connection_get_handles (base,
      TP_HANDLE_TYPE_CONTACT);

  if (!tp_handles_are_valid (contact_handles, contacts, FALSE, &error))
    {
      dbus_g_method_return_error (context, error);
      g_error_free (error);
      return;
    }

  aliases = g_array_sized_new (TRUE, FALSE, sizeof (gchar *), contacts->len);

  for (i = 0; i < contacts->len; i++)
    {
      TpHandle handle;
      gchar *alias;

      handle = g_array_index (contacts, TpHandle, i);

      alias = conn_get_alias (self, contact_handles, handle);

      g_array_append_val (aliases, alias);
    }

  res = (gchar **) g_array_free (aliases, FALSE);

  tp_svc_connection_interface_aliasing_return_from_request_aliases (
      context, (const gchar **) res);

  g_strfreev (res);
}

static void
tpsip_connection_get_aliases (TpSvcConnectionInterfaceAliasing *iface,
                              const GArray *contacts,
                              DBusGMethodInvocation *context)
{
  TpsipConnection *self = TPSIP_CONNECTION (iface);
  TpBaseConnection *base = (TpBaseConnection *) self;
  TpHandleRepoIface *contact_handles;
  GHashTable *result;
  GError *error = NULL;
  guint i;

  TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED (base, context);

  contact_handles = tp_base_connection_get_handles (base,
      TP_HANDLE_TYPE_CONTACT);

  if (!tp_handles_are_valid (contact_handles, contacts, FALSE, &error))
    {
      dbus_g_method_return_error (context, error);
      g_error_free (error);
      return;
    }

  result = g_hash_table_new_full (g_direct_hash, g_direct_equal,
      NULL, g_free);

  for (i = 0; i < contacts->len; i++)
    {
      TpHandle handle;
      gchar *alias;

      handle = g_array_index (contacts, TpHandle, i);

      alias = conn_get_alias (self, contact_handles, handle);

      g_hash_table_insert (result, GUINT_TO_POINTER (handle), alias);
    }

  tp_svc_connection_interface_aliasing_return_from_get_aliases (context,
      result);

  g_hash_table_destroy (result);
}

static void
emit_self_alias_change (TpsipConnection *self, const gchar *alias)
{
  TpBaseConnection *base = (TpBaseConnection *) self;
  GPtrArray *change_data;
  GValue change_pair = { 0, };

  g_value_init (&change_pair, TP_STRUCT_TYPE_ALIAS_PAIR);
  g_value_take_boxed (&change_pair,
      dbus_g_type_specialized_construct (TP_STRUCT_TYPE_ALIAS_PAIR));
  dbus_g_type_struct_set (&change_pair,
      0, base->self_handle,
      1, alias,
      G_MAXUINT);
  change_data = g_ptr_array_sized_new (1);
  g_ptr_array_add (change_data, g_value_get_boxed (&change_pair));

  tp_svc_connection_interface_aliasing_emit_aliases_changed (self, change_data);

  g_ptr_array_free (change_data, TRUE);
  g_value_unset (&change_pair);
}

static const gchar *
collapse_whitespace (const gchar *str, gchar **to_free)
{
  static GRegex *whitespace_regex = NULL;

  const gchar *p;
  gchar *subst_res;

  p = (const gchar *) strpbrk (str, " \t\r\n");
  if (p == NULL)
    return str;

  if (whitespace_regex == NULL)
    {
      whitespace_regex = g_regex_new ("[[:space:]]+",
          G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, NULL);
    }

  subst_res = g_regex_replace_literal (whitespace_regex, str, -1, p - str, " ",
      0, NULL);

  *to_free = subst_res;
  return subst_res;
}

static void
tpsip_connection_set_aliases (TpSvcConnectionInterfaceAliasing *iface,
                              GHashTable *aliases,
                              DBusGMethodInvocation *context)
{
  TpsipConnection *self = TPSIP_CONNECTION (iface);
  TpBaseConnection *base = (TpBaseConnection *) self;
  TpHandleRepoIface *contact_handles;
  const gchar *alias;
  gchar *default_alias;
  gchar *to_free = NULL;

  TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED (base, context);

  /* We only care about the self alias */
  alias = g_hash_table_lookup (aliases, GINT_TO_POINTER (base->self_handle));

  if (alias == NULL || g_hash_table_size (aliases) > 1)
    {
      /* One of the handles (if there are any) cannot be the self handle */
      GError err = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
          "Cannot set aliases for any contact except self" };
      dbus_g_method_return_error (context, &err);
      return;
    }

  alias = collapse_whitespace (alias, &to_free);

  contact_handles = tp_base_connection_get_handles (base,
      TP_HANDLE_TYPE_CONTACT);
  default_alias = conn_get_default_alias (self,
      contact_handles, base->self_handle);

  if (strcmp (alias, default_alias) == 0)
    {
      DEBUG("using default alias for self");
      g_object_set (self, "alias", NULL, NULL);
    }
  else
    {
      DEBUG("setting alias for self: %s", alias);
      g_object_set (self, "alias", alias, NULL);
    }

  emit_self_alias_change (self, alias);

  g_free (default_alias);
  g_free (to_free);

  tp_svc_connection_interface_aliasing_return_from_set_aliases (context);
}

static void
tpsip_conn_aliasing_fill_contact_attributes (GObject *obj,
    const GArray *contacts, GHashTable *attributes_hash)
{
  TpsipConnection *self = TPSIP_CONNECTION (obj);
  TpBaseConnection *base = (TpBaseConnection *) self;
  TpHandleRepoIface *contact_handles;
  guint i;

  contact_handles = tp_base_connection_get_handles (base,
      TP_HANDLE_TYPE_CONTACT);

  for (i = 0; i < contacts->len; i++)
    {
      TpHandle handle;
      GValue *val;

      handle = g_array_index (contacts, TpHandle, i);

      val = tp_g_value_slice_new (G_TYPE_STRING);

      g_value_take_string (val,
          conn_get_alias (self, contact_handles, handle));

      tp_contacts_mixin_set_contact_attribute (attributes_hash, handle,
          TP_IFACE_CONNECTION_INTERFACE_ALIASING "/alias", val);
    }
}

void
tpsip_conn_aliasing_init (TpsipConnection *conn)
{
  tp_contacts_mixin_add_contact_attributes_iface (G_OBJECT (conn),
      TP_IFACE_CONNECTION_INTERFACE_ALIASING,
      tpsip_conn_aliasing_fill_contact_attributes);
}

void
tpsip_conn_aliasing_iface_init (gpointer g_iface, gpointer iface_data)
{
  TpSvcConnectionInterfaceAliasingClass *klass =
    (TpSvcConnectionInterfaceAliasingClass *) g_iface;

#define IMPLEMENT(x) tp_svc_connection_interface_aliasing_implement_##x (\
    klass, tpsip_connection_##x)
  IMPLEMENT(get_alias_flags);
  IMPLEMENT(request_aliases);
  IMPLEMENT(get_aliases);
  IMPLEMENT(set_aliases);
#undef IMPLEMENT
}