summaryrefslogtreecommitdiff
path: root/telepathy-glib/handle-repo-static.c
blob: 322adced5a30e7ef7cbae90bd1e2fd0c3522977e (plain)
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
/*
 * handle-repo-static.c - mechanism to store and retrieve handles on a
 * connection (implementation for handle types with a fixed list of possible
 * handles)
 *
 * Copyright (C) 2007 Collabora Ltd. <http://www.collabora.co.uk/>
 * Copyright (C) 2007 Nokia Corporation
 *
 * 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
 */

/**
 * SECTION:handle-repo-static
 * @title: TpStaticHandleRepo
 * @short_description: handle repository implementation with a fixed, static
 *  set of handle names
 * @see_also: TpHandleRepoIface, TpDynamicHandleRepo
 *
 * A static handle repository has a fixed, static set of allowed names;
 * these handles can never be destroyed, and no more can be created, so
 * no reference counting is performed.
 *
 * The #TpHandleRepoIface:handle-type property must be set at construction
 * time.
 *
 * Most connection managers will use this for handles of type
 * %TP_HANDLE_TYPE_LIST.
 */

#include "config.h"

#include <telepathy-glib/handle-repo-static.h>

#include <string.h>

#include <telepathy-glib/handle-repo-internal.h>
#include <telepathy-glib/util.h>

enum
{
  PROP_HANDLE_TYPE = 1,
  PROP_HANDLE_NAMES,
};

struct _TpStaticHandleRepoClass {
  GObjectClass parent_class;
};

struct _TpStaticHandleRepo {
  GObject parent;
  TpHandleType handle_type;
  TpHandle last_handle;
  gchar **handle_names;
  GData **datalists;
};

static void static_repo_iface_init (gpointer g_iface,
    gpointer iface_data);

G_DEFINE_TYPE_WITH_CODE (TpStaticHandleRepo, tp_static_handle_repo,
    G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (TP_TYPE_HANDLE_REPO_IFACE,
        static_repo_iface_init))

static void
tp_static_handle_repo_init (TpStaticHandleRepo *self)
{
  self->handle_type = 0;
  self->last_handle = 0;
  self->handle_names = NULL;
  self->datalists = NULL;
}

static void
static_finalize (GObject *object)
{
  TpStaticHandleRepo *self = TP_STATIC_HANDLE_REPO (object);
  guint i;

  if (self->datalists)
    {
      for (i = 0; i < self->last_handle; i++)
        {
          g_datalist_clear (self->datalists + i);
        }
    }

  g_strfreev (self->handle_names);

  G_OBJECT_CLASS (tp_static_handle_repo_parent_class)->finalize (object);
}

static void
static_get_property (GObject *object,
    guint property_id,
    GValue *value,
    GParamSpec *pspec)
{
  TpStaticHandleRepo *self = TP_STATIC_HANDLE_REPO (object);

  switch (property_id)
    {
    case PROP_HANDLE_TYPE:
      g_value_set_uint (value, self->handle_type);
      break;
    case PROP_HANDLE_NAMES:
      g_value_set_boxed (value, g_strdupv (self->handle_names));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
static_set_property (GObject *object,
    guint property_id,
    const GValue *value,
    GParamSpec *pspec)
{
  TpStaticHandleRepo *self = TP_STATIC_HANDLE_REPO (object);
  TpHandle i;

  switch (property_id)
    {
    case PROP_HANDLE_TYPE:
      self->handle_type = g_value_get_uint (value);
      break;

    case PROP_HANDLE_NAMES:

      if (self->datalists)
        {
          for (i = 0; i < self->last_handle; i++)
            {
              g_datalist_clear (self->datalists + i);
            }
        }

      g_strfreev (self->handle_names);
      self->handle_names = g_strdupv (g_value_get_boxed (value));
      i = 0;
      while (self->handle_names[i] != NULL)
        {
          i++;
        }
      self->last_handle = i;

      g_free (self->datalists);
      self->datalists = NULL;

      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
tp_static_handle_repo_class_init (TpStaticHandleRepoClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GParamSpec *param_spec;

  object_class->get_property = static_get_property;
  object_class->set_property = static_set_property;
  object_class->finalize = static_finalize;

  g_object_class_override_property (object_class, PROP_HANDLE_TYPE,
      "handle-type");
  param_spec = g_param_spec_boxed ("handle-names", "Handle names",
      "The static set of handle names supported by this repo.",
      G_TYPE_STRV,
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_HANDLE_NAMES,
      param_spec);
}

static gboolean
static_handle_is_valid (TpHandleRepoIface *irepo,
                        TpHandle handle,
                        GError **error)
{
  TpStaticHandleRepo *self = TP_STATIC_HANDLE_REPO (irepo);

  if (handle <= 0 || handle > self->last_handle)
    {
      g_set_error (error, TP_ERROR, TP_ERROR_INVALID_HANDLE,
          "handle %u is not a valid %s handle (type %u)",
          handle, tp_handle_type_to_string (self->handle_type),
          self->handle_type);
      return FALSE;
    }
  else
    {
      return TRUE;
    }
}

static gboolean
static_handles_are_valid (TpHandleRepoIface *irepo, const GArray *handles,
    gboolean allow_zero, GError **error)
{
  guint i;

  g_return_val_if_fail (handles != NULL, FALSE);

  for (i = 0; i < handles->len; i++)
    {
      TpHandle handle = g_array_index (handles, TpHandle, i);

      if (handle == 0 && allow_zero)
        continue;

      if (!static_handle_is_valid (irepo, handle, error))
        return FALSE;
    }

  return TRUE;
}

static TpHandle
static_ref_handle (TpHandleRepoIface *self, TpHandle handle)
{
  /* nothing to do, handles in this repo are permanent */

  return handle;
}

static void
static_unref_handle (TpHandleRepoIface *self, TpHandle handle)
{
  /* nothing to do, handles in this repo are permanent */
}

static gboolean
static_client_hold_or_release_handle (TpHandleRepoIface *self,
    const gchar *client_name,
    TpHandle handle,
    GError **error)
{
  /* nothing to do, handles in this repo are permanent */
  return TRUE;
}

static const char *
static_inspect_handle (TpHandleRepoIface *irepo, TpHandle handle)
{
  TpStaticHandleRepo *self = TP_STATIC_HANDLE_REPO (irepo);

  if (handle <= 0 || handle > self->last_handle)
    return NULL;

  return self->handle_names[handle-1];
}

static TpHandle
static_lookup_handle (TpHandleRepoIface *irepo,
                      const char *id,
                      gpointer context,
                      GError **error)
{
  TpStaticHandleRepo *self = TP_STATIC_HANDLE_REPO (irepo);
  guint i;

  for (i = 0; i < self->last_handle; i++)
    {
      if (!tp_strdiff (self->handle_names[i], id))
        return (TpHandle) i + 1;
    }

  g_set_error (error, TP_ERROR, TP_ERROR_NOT_AVAILABLE,
      "'%s' is not one of the valid handles", id);
  return 0;
}


static void
static_set_qdata (TpHandleRepoIface *repo, TpHandle handle,
    GQuark key_id, gpointer data, GDestroyNotify destroy)
{
  TpStaticHandleRepo *self = TP_STATIC_HANDLE_REPO (repo);
  guint i;

  g_return_if_fail (handle > 0);
  g_return_if_fail (handle <= self->last_handle);

  if (!self->datalists)
    {
      self->datalists = g_new (GData *, self->last_handle);
      for (i = 0; i < self->last_handle; i++)
        {
          g_datalist_init (self->datalists + i);
        }
    }

  g_datalist_id_set_data_full (self->datalists + handle - 1, key_id, data,
      destroy);
}

static gpointer
static_get_qdata (TpHandleRepoIface *repo, TpHandle handle,
    GQuark key_id)
{
  TpStaticHandleRepo *self = TP_STATIC_HANDLE_REPO (repo);

  g_return_val_if_fail (handle > 0, NULL);
  g_return_val_if_fail (handle <= self->last_handle, NULL);

  /* if we have no datalists that's not a bug - it means nobody has called
   * static_set_qdata yet */
  if (!self->datalists)
    return NULL;

  return g_datalist_id_get_data (self->datalists + handle - 1, key_id);
}

static void
static_repo_iface_init (gpointer g_iface,
    gpointer iface_data)
{
  TpHandleRepoIfaceClass *klass = (TpHandleRepoIfaceClass *) g_iface;

  klass->handle_is_valid = static_handle_is_valid;
  klass->handles_are_valid = static_handles_are_valid;
  klass->ref_handle = static_ref_handle;
  klass->unref_handle = static_unref_handle;
  klass->client_hold_handle = static_client_hold_or_release_handle;
  klass->client_release_handle = static_client_hold_or_release_handle;
  klass->inspect_handle = static_inspect_handle;
  /* this repo is static, so lookup and ensure are identical */
  klass->lookup_handle = static_lookup_handle;
  klass->ensure_handle = static_lookup_handle;
  klass->set_qdata = static_set_qdata;
  klass->get_qdata = static_get_qdata;
}