summaryrefslogtreecommitdiff
path: root/telepathy-logger/contact.c
blob: 6b008b327af8b48eb85f04d679c616ccbd98d60a (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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2009 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: Cosimo Alfarano <cosimo.alfarano@collabora.co.uk>
 */

#include "contact.h"

#include <telepathy-glib/account.h>

#include <telepathy-logger/util.h>

#define DEBUG_FLAG TPL_DEBUG_CONTACT
#include <telepathy-logger/debug.h>

G_DEFINE_TYPE (TplContact, tpl_contact, G_TYPE_OBJECT)

#define GET_PRIV(obj) TPL_GET_PRIV (obj, TplContact)
struct _TplContactPriv
{
  TpContact *contact;
  TpAccount *account;

  TplContactType contact_type;
  gchar *alias;
  gchar *identifier;
  gchar *presence_status;
  gchar *presence_message;
  gchar *avatar_token;
};

enum
{
  PROP0,
  PROP_IDENTIFIER,
  PROP_ALIAS,
  PROP_PRESENCE_STATUS,
  PROP_PRESENCE_MESSAGE,
  PROP_AVATAR_TOKEN
};

static void
tpl_contact_finalize (GObject *obj)
{
  TplContact *self = TPL_CONTACT (obj);
  TplContactPriv *priv = GET_PRIV (self);

  g_free (priv->alias);
  priv->alias = NULL;
  g_free (priv->identifier);
  priv->identifier = NULL;
  g_free (priv->presence_status);
  priv->presence_status = NULL;
  g_free (priv->presence_message);
  priv->presence_message = NULL;

  G_OBJECT_CLASS (tpl_contact_parent_class)->finalize (obj);
}


static void
tpl_contact_dispose (GObject *obj)
{
  TplContact *self = TPL_CONTACT (obj);
  TplContactPriv *priv = GET_PRIV (self);

  tpl_object_unref_if_not_null (priv->contact);
  priv->contact = NULL;

  G_OBJECT_CLASS (tpl_contact_parent_class)->dispose (obj);
}


static void
tpl_contact_get_property (GObject *object,
    guint param_id,
    GValue *value,
    GParamSpec *pspec)
{
  TplContactPriv *priv = GET_PRIV (object);

  switch (param_id)
    {
      case PROP_IDENTIFIER:
        g_value_set_string (value, priv->identifier);
        break;
      case PROP_ALIAS:
        g_value_set_string (value, priv->alias);
        break;
      case PROP_PRESENCE_STATUS:
        g_value_set_string (value, priv->presence_status);
        break;
      case PROP_PRESENCE_MESSAGE:
        g_value_set_string (value, priv->presence_message);
        break;
      case PROP_AVATAR_TOKEN:
        g_value_set_string (value, priv->avatar_token);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
    };
}


static void
tpl_contact_set_property (GObject *object, guint param_id, const GValue *value,
    GParamSpec *pspec)
{
  TplContact *self = TPL_CONTACT (object);

  switch (param_id) {
      case PROP_IDENTIFIER:
        tpl_contact_set_identifier (self, g_value_get_string (value));
        break;
      case PROP_ALIAS:
        tpl_contact_set_alias (self, g_value_get_string (value));
        break;
      case PROP_PRESENCE_STATUS:
        tpl_contact_set_presence_status (self, g_value_get_string (value));
        break;
      case PROP_PRESENCE_MESSAGE:
        tpl_contact_set_presence_message (self, g_value_get_string (value));
        break;
      case PROP_AVATAR_TOKEN:
        tpl_contact_set_avatar_token (self, g_value_get_string (value));
        break;

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

}


static void tpl_contact_class_init (TplContactClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GParamSpec *param_spec;

  object_class->finalize = tpl_contact_finalize;
  object_class->dispose = tpl_contact_dispose;
  object_class->get_property = tpl_contact_get_property;
  object_class->set_property = tpl_contact_set_property;

  /**
   * TplContact:identifier:
   *
   * The contact's identifier
   */
  param_spec = g_param_spec_string ("identifier",
      "Identifier",
      "The contact's identifier",
      NULL,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_IDENTIFIER, param_spec);

  /**
   * TplContact:alias:
   *
   * The contact's alias
   */
  param_spec = g_param_spec_string ("alias",
      "Alias",
      "The contact's alias",
      NULL,
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_ALIAS, param_spec);

  /**
   * TplContact:presence-status:
   *
   * The contact's presence status string
   */
  param_spec = g_param_spec_string ("presence-status",
      "PresenceStatus",
      "The contact's presence status string",
      NULL,
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_ALIAS, param_spec);

  /**
   * TplContact:presence-message:
   *
   * The contact's presence message string
   */
  param_spec = g_param_spec_string ("presence-message",
      "PresenceMessage",
      "The contact's presence message",
      NULL,
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_ALIAS, param_spec);

  /**
   * TplContact:avatar-token:
   *
   * The contact's avatar token
   */
  param_spec = g_param_spec_string ("avatar-token",
      "AvatarToken",
      "The contact's avatar's token",
      NULL,
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_ALIAS, param_spec);

  g_type_class_add_private (object_class, sizeof (TplContactPriv));
}


static void
tpl_contact_init (TplContact *self)
{
  TplContactPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      TPL_TYPE_CONTACT, TplContactPriv);

  self->priv = priv;
}


TplContact *
tpl_contact_from_tp_contact (TpContact *contact)
{
  TplContact *ret;

  g_return_val_if_fail (TP_IS_CONTACT (contact), NULL);

  ret = tpl_contact_new (tp_contact_get_identifier (contact));
  tpl_contact_set_contact (ret, contact);
  if (tp_contact_get_alias (contact) != NULL)
    tpl_contact_set_alias (ret, (gchar *) tp_contact_get_alias (contact));
  if (tp_contact_get_presence_status (contact))
    tpl_contact_set_presence_status (ret, tp_contact_get_presence_status (
          contact));
  if (tp_contact_get_presence_message (contact) != NULL)
    tpl_contact_set_presence_message (ret,
        tp_contact_get_presence_message (contact));
  if (tp_contact_get_avatar_token (contact) != NULL)
    tpl_contact_set_avatar_token (ret, tp_contact_get_avatar_token (contact));

  DEBUG ("ID: %s, TOK: %s", tpl_contact_get_identifier (ret),
      tpl_contact_get_avatar_token (ret));
  return ret;
}


TplContact *
tpl_contact_new (const gchar *identifier)
{
  g_return_val_if_fail (!TPL_STR_EMPTY (identifier), NULL);

  return g_object_new (TPL_TYPE_CONTACT,
      "identifier", identifier, NULL);
}


TpContact *
tpl_contact_get_contact (TplContact *self)
{
  TplContactPriv *priv = GET_PRIV (self);

  g_return_val_if_fail (TPL_IS_CONTACT (self), NULL);

  return priv->contact;
}


const gchar *
tpl_contact_get_alias (TplContact *self)
{
  TplContactPriv *priv = GET_PRIV (self);

  g_return_val_if_fail (TPL_IS_CONTACT (self), NULL);

  return priv->alias;
}


const gchar *
tpl_contact_get_identifier (TplContact *self)
{
  TplContactPriv *priv = GET_PRIV (self);

  g_return_val_if_fail (TPL_IS_CONTACT (self), NULL);

  return priv->identifier;
}


const gchar *
tpl_contact_get_presence_status (TplContact *self)
{
  TplContactPriv *priv = GET_PRIV (self);

  g_return_val_if_fail (TPL_IS_CONTACT (self), NULL);

  return priv->presence_status;
}


const gchar *
tpl_contact_get_presence_message (TplContact *self)
{
  TplContactPriv *priv = GET_PRIV (self);

  g_return_val_if_fail (TPL_IS_CONTACT (self), NULL);

  return priv->presence_message;
}


TplContactType
tpl_contact_get_contact_type (TplContact *self)
{
  TplContactPriv *priv = GET_PRIV (self);

  g_return_val_if_fail (TPL_IS_CONTACT (self), TPL_CONTACT_UNKNOWN);

  return priv->contact_type;
}


const gchar *
tpl_contact_get_avatar_token (TplContact *self)
{
  TplContactPriv *priv = GET_PRIV (self);

  g_return_val_if_fail (TPL_IS_CONTACT (self), NULL);

  return priv->avatar_token;
}


TpAccount *
tpl_contact_get_account (TplContact *self)
{
  TplContactPriv *priv = GET_PRIV (self);

  g_return_val_if_fail (TPL_IS_CONTACT (self), NULL);

  return priv->account;
}


void
tpl_contact_set_contact (TplContact *self,
    TpContact *data)
{
  TplContactPriv *priv = GET_PRIV (self);

  g_return_if_fail (TPL_IS_CONTACT (self));
  g_return_if_fail (TP_IS_CONTACT (data));
  g_return_if_fail (priv->contact == NULL);

  priv->contact = data;
  g_object_ref (data);
}


void
tpl_contact_set_account (TplContact *self,
    TpAccount *data)
{
  TplContactPriv *priv = GET_PRIV (self);

  g_return_if_fail (TPL_IS_CONTACT (self));
  g_return_if_fail (TP_IS_ACCOUNT (data));
  g_return_if_fail (priv->account == NULL);

  priv->account = data;
  g_object_ref (data);
}


void
tpl_contact_set_alias (TplContact *self,
    const gchar *data)
{
  TplContactPriv *priv = GET_PRIV (self);

  g_return_if_fail (TPL_IS_CONTACT (self));
  g_return_if_fail (!TPL_STR_EMPTY (data));
  g_return_if_fail (priv->alias == NULL);

  priv->alias = g_strdup (data);
}


void
tpl_contact_set_identifier (TplContact *self,
    const gchar *data)
{
  TplContactPriv *priv = GET_PRIV (self);

  g_return_if_fail (TPL_IS_CONTACT (self));
  g_return_if_fail (!TPL_STR_EMPTY (data));
  g_return_if_fail (priv->identifier == NULL);

  priv->identifier = g_strdup (data);
}


void
tpl_contact_set_presence_status (TplContact *self,
    const gchar *data)
{
  TplContactPriv *priv = GET_PRIV (self);

  g_return_if_fail (TPL_IS_CONTACT (self));
  g_return_if_fail (priv->presence_status == NULL);
  /* data can be NULL, if no presence_status is set */

  priv->presence_status = g_strdup (data);
}


void
tpl_contact_set_presence_message (TplContact *self,
    const gchar *data)
{
  TplContactPriv *priv = GET_PRIV (self);

  g_return_if_fail (TPL_IS_CONTACT (self));
  g_return_if_fail (priv->presence_message == NULL);
  /* data can be NULL, if no presence_message is set */

  priv->presence_message = g_strdup (data);
}


void
tpl_contact_set_contact_type (TplContact *self,
    TplContactType data)
{
  TplContactPriv *priv = GET_PRIV (self);

  g_return_if_fail (TPL_IS_CONTACT (self));

  priv->contact_type = data;
}


void
tpl_contact_set_avatar_token (TplContact *self,
    const gchar *data)
{
  TplContactPriv *priv = GET_PRIV (self);

  g_return_if_fail (TPL_IS_CONTACT (self));
  g_return_if_fail (priv->avatar_token == NULL);
  /* data can be NULL, if no avatar_token is set */

  priv->avatar_token = g_strdup (data);
}