File: connection-aliasing.c

package info (click to toggle)
telepathy-rakia 0.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 3,028 kB
  • ctags: 1,720
  • sloc: sh: 11,552; ansic: 9,892; python: 4,532; makefile: 421; xml: 15
file content (398 lines) | stat: -rw-r--r-- 10,981 bytes parent folder | download
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * connection-aliasing.c - Implementation for RakiaConnectionAliasing interface
 * Copyright (C) 2008-2011 Nokia Corporation
 *   @author Mikhail Zabaluev <mikhail.zabaluev@nokia.com>
 *   @author Pekka Pessi <pekka.pessi@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 "config.h"

#include <rakia/connection-aliasing.h>
#include <rakia/base-connection.h>
#include <rakia/handles.h>

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

#include "rakia/handles.h"

#include <string.h>

#define DEBUG_FLAG RAKIA_DEBUG_CONNECTION
#include "rakia/debug.h"

enum {
  PROP_NONE,
  PROP_ALIAS,
};

static void
rakia_connection_aliasing_base_init (gpointer klass)
{
  static gboolean initialized = FALSE;

  if (!initialized)
    {
      initialized = TRUE;

      g_object_interface_install_property (klass,
          g_param_spec_string ("alias", "Alias",
              "User's display name",
              NULL,
              G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
    }
}

GType
rakia_connection_aliasing_get_type (void)
{
  static GType type = 0;

  if (G_UNLIKELY (type == 0))
    {
      static const GTypeInfo info = {
        sizeof (RakiaConnectionAliasingInterface),
        rakia_connection_aliasing_base_init, /* base_init */
        NULL, /* base_finalize */
        NULL, /* class_init */
        NULL, /* class_finalize */
        NULL, /* class_data */
        0,
        0, /* n_preallocs */
        NULL /* instance_init */
      };

      type = g_type_register_static (G_TYPE_INTERFACE,
          "RakiaConnectionAliasingInterface", &info, 0);

      g_type_interface_add_prerequisite (type, RAKIA_TYPE_BASE_CONNECTION);
      g_type_interface_add_prerequisite (type,
          TP_TYPE_SVC_CONNECTION_INTERFACE_ALIASING);
    }

  return type;
}

static void
rakia_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 (TpBaseConnection *base,
                        TpHandleRepoIface *contact_handles,
                        TpHandle handle)
{
  const url_t *url;
  gchar *alias = NULL;

  url = rakia_base_connection_handle_to_uri (RAKIA_BASE_CONNECTION (base),
      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 (TpBaseConnection *base,
                TpHandleRepoIface *contact_handles,
                TpHandle handle)
{
  gchar *alias = NULL;

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

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

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

  return alias;
}

static void
rakia_connection_request_aliases (TpSvcConnectionInterfaceAliasing *iface,
                                  const GArray *contacts,
                                  DBusGMethodInvocation *context)
{
  TpBaseConnection *base = TP_BASE_CONNECTION (iface);
  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 (base, 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
rakia_connection_get_aliases (TpSvcConnectionInterfaceAliasing *iface,
                              const GArray *contacts,
                              DBusGMethodInvocation *context)
{
  TpBaseConnection *base = TP_BASE_CONNECTION (iface);
  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 (base, 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_unref (result);
}

static void
emit_self_alias_change (TpBaseConnection *base, const gchar *alias)
{
  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 (base, change_data);

  g_ptr_array_unref (change_data);
  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
rakia_connection_set_aliases (TpSvcConnectionInterfaceAliasing *iface,
                              GHashTable *aliases,
                              DBusGMethodInvocation *context)
{
  TpBaseConnection *base = TP_BASE_CONNECTION (iface);
  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_ERROR, 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 (base,
      contact_handles, base->self_handle);

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

  emit_self_alias_change (base, alias);

  g_free (default_alias);
  g_free (to_free);

  tp_svc_connection_interface_aliasing_return_from_set_aliases (context);
}

static void
rakia_conn_aliasing_fill_contact_attributes (GObject *obj,
    const GArray *contacts, GHashTable *attributes_hash)
{
  TpBaseConnection *base = TP_BASE_CONNECTION (obj);
  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 (base, contact_handles, handle));

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

void
rakia_connection_aliasing_init (gpointer instance)
{
  tp_contacts_mixin_add_contact_attributes_iface (G_OBJECT (instance),
      TP_IFACE_CONNECTION_INTERFACE_ALIASING,
      rakia_conn_aliasing_fill_contact_attributes);
}


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

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