summaryrefslogtreecommitdiff
path: root/telepathy-logger/event.c
blob: f061ce0bdaa2af9d7b513eba15fd7698c1de1e9b (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
/*
 * 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 "config.h"
#include "event.h"
#include "event-internal.h"

#include <glib.h>
#include "entity-internal.h"

#define DEBUG_FLAG TPL_DEBUG_EVENT
#include <telepathy-logger/debug-internal.h>
#include <telepathy-logger/util-internal.h>

/**
 * SECTION:event
 * @title: TplEvent
 * @short_description: Abstract representation of a log event
 * @see_also: #TplTextEvent and other subclasses when they'll exist
 *
 * The TPLogger log event represents a generic log event, which will be
 * specialized by subclasses of #TplEvent.
 */

/**
 * TplEvent:
 *
 * An object representing a generic log event.
 */

G_DEFINE_ABSTRACT_TYPE (TplEvent, tpl_event, G_TYPE_OBJECT)

struct _TplEventPriv
{
  gint64 timestamp;
  TpAccount *account;
  gchar *channel_path;

  /* message and receiver may be NULL depending on the signal. ie. status
   * changed signals set only the sender */
  TplEntity *sender;
  TplEntity *receiver;
};

enum {
    PROP_TIMESTAMP = 1,
    PROP_TARGET_ID,
    PROP_ACCOUNT,
    PROP_ACCOUNT_PATH,
    PROP_CHANNEL_PATH,
    PROP_SENDER,
    PROP_RECEIVER
};


static void
tpl_event_finalize (GObject *obj)
{
  TplEvent *self = TPL_EVENT (obj);
  TplEventPriv *priv = self->priv;

  tp_clear_pointer (&priv->channel_path, g_free);

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


static void
tpl_event_dispose (GObject *obj)
{
  TplEvent *self = TPL_EVENT (obj);
  TplEventPriv *priv = self->priv;

  tp_clear_object (&priv->account);
  tp_clear_object (&priv->sender);
  tp_clear_object (&priv->receiver);

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


static void
tpl_event_get_property (GObject *object,
    guint param_id,
    GValue *value,
    GParamSpec *pspec)
{
  TplEvent *self = TPL_EVENT (object);
  TplEventPriv *priv = self->priv;

  switch (param_id)
    {
      case PROP_TIMESTAMP:
        g_value_set_int64 (value, priv->timestamp);
        break;
      case PROP_ACCOUNT:
        g_value_set_object (value, priv->account);
        break;
      case PROP_ACCOUNT_PATH:
        g_value_set_string (value, tpl_event_get_account_path (self));
        break;
      case PROP_CHANNEL_PATH:
        g_value_set_string (value, priv->channel_path);
        break;
      case PROP_SENDER:
        g_value_set_object (value, priv->sender);
        break;
      case PROP_RECEIVER:
        g_value_set_object (value, priv->receiver);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
    };
}


static void
tpl_event_set_property (GObject *object,
    guint param_id,
    const GValue *value,
    GParamSpec *pspec)
{
  TplEvent *self = TPL_EVENT (object);
  TplEventPriv *priv = self->priv;

  switch (param_id) {
      case PROP_TIMESTAMP:
        g_assert (priv->timestamp == 0);
        priv->timestamp = g_value_get_int64 (value);
        break;
      case PROP_ACCOUNT:
        g_assert (priv->account == NULL);
        priv->account = g_value_dup_object (value);
        break;
      case PROP_CHANNEL_PATH:
        g_assert (priv->channel_path == NULL);
        priv->channel_path = g_value_dup_string (value);
        break;
      case PROP_SENDER:
        g_assert (priv->sender == NULL);
        g_return_if_fail (TPL_IS_ENTITY (g_value_get_object (value)));
        priv->sender = g_value_dup_object (value);
        break;
      case PROP_RECEIVER:
        g_assert (priv->receiver == NULL);
        /* can be NULL */
        priv->receiver = g_value_dup_object (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
  };
}

static inline gboolean
account_equal (TpAccount *account1, TpAccount *account2)
{
  g_return_val_if_fail (TP_IS_PROXY (account1), FALSE);
  g_return_val_if_fail (TP_IS_PROXY (account2), FALSE);

  return !tp_strdiff (tp_proxy_get_object_path (TP_PROXY (account1)),
      tp_proxy_get_object_path (TP_PROXY (account2)));
}


static gboolean
tpl_event_equal_default (TplEvent *message1,
    TplEvent *message2)
{
  g_return_val_if_fail (TPL_IS_EVENT (message1), FALSE);
  g_return_val_if_fail (TPL_IS_EVENT (message2), FALSE);

  return message1->priv->timestamp == message2->priv->timestamp
    && account_equal (message1->priv->account, message2->priv->account)
    && _tpl_entity_compare (message1->priv->sender, message2->priv->sender)
    && _tpl_entity_compare (message1->priv->receiver, message2->priv->receiver);
}


static void
tpl_event_class_init (TplEventClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GParamSpec *param_spec;

  /* to be used by subclasses */
  object_class->finalize = tpl_event_finalize;
  object_class->dispose = tpl_event_dispose;
  object_class->get_property = tpl_event_get_property;
  object_class->set_property = tpl_event_set_property;

  klass->equal = tpl_event_equal_default;

  param_spec = g_param_spec_int64 ("timestamp",
      "Timestamp",
      "The timestamp (gint64) for the log event",
      G_MININT64, G_MAXINT64, 0,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_TIMESTAMP, param_spec);

  param_spec = g_param_spec_object ("account",
      "TpAccount",
      "The TpAccount to which the log event is related",
      TP_TYPE_ACCOUNT,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_ACCOUNT, param_spec);

  param_spec = g_param_spec_string ("account-path",
      "AccountPath",
      "The account path of the TpAccount to which the log event is related",
      NULL,
      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_ACCOUNT_PATH, param_spec);

  param_spec = g_param_spec_string ("channel-path",
      "ChannelPath",
      "The channel path of the TpChannel to which the log event is related",
      NULL,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_CHANNEL_PATH, param_spec);

  param_spec = g_param_spec_object ("sender",
      "Sender",
      "TplEntity instance who originated the log event",
      TPL_TYPE_ENTITY,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_SENDER, param_spec);

  param_spec = g_param_spec_object ("receiver",
      "Receiver",
      "TplEntity instance destination for the log event "
      "(may be NULL with some log stores)",
      TPL_TYPE_ENTITY,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_RECEIVER, param_spec);

  g_type_class_add_private (object_class, sizeof (TplEventPriv));
}


static void
tpl_event_init (TplEvent *self)
{
  TplEventPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      TPL_TYPE_EVENT, TplEventPriv);
  self->priv = priv;
}

/**
 * tpl_event_get_timestamp:
 * @self: a #TplEvent
 *
 * Returns: the same timestamp as the #TplEvent:timestamp property
 */
gint64
tpl_event_get_timestamp (TplEvent *self)
{
  g_return_val_if_fail (TPL_IS_EVENT (self), -1);

  return self->priv->timestamp;
}


/**
 * tpl_event_get_sender:
 * @self: a #TplEvent
 *
 * Returns: (transfer none): the same #TplEntity as the #TplEvent:sender property
 */
TplEntity *
tpl_event_get_sender (TplEvent *self)
{
  g_return_val_if_fail (TPL_IS_EVENT (self), NULL);

  return self->priv->sender;
}

/**
 * tpl_event_get_receiver:
 * @self: a #TplEvent
 *
 * Returns: (transfer none): the same #TplEntity as the #TplEvent:receiver property
 */
TplEntity *
tpl_event_get_receiver (TplEvent *self)
{
  g_return_val_if_fail (TPL_IS_EVENT (self), NULL);

  return self->priv->receiver;
}


TplEntity *
_tpl_event_get_target (TplEvent *self)
{
  g_return_val_if_fail (TPL_IS_EVENT (self), NULL);

  if (_tpl_event_target_is_room (self)
      || tpl_entity_get_entity_type (self->priv->sender) == TPL_ENTITY_SELF)
    return self->priv->receiver;
  else
    return self->priv->sender;
}


const gchar *
_tpl_event_get_target_id (TplEvent *self)
{
  g_return_val_if_fail (TPL_IS_EVENT (self), NULL);

  return tpl_entity_get_identifier (_tpl_event_get_target (self));
}

gboolean
_tpl_event_target_is_room (TplEvent *self)
{
  /* Some log-store like Pidgin text mode does not know about receiver, so
   * having a NULL receiver is fine. */
  if (self->priv->receiver == NULL)
    return FALSE;

  return (tpl_entity_get_entity_type (self->priv->receiver) == TPL_ENTITY_ROOM);
}


/**
 * tpl_event_get_account_path:
 * @self: a #TplEvent
 *
 * <!-- no more to say -->
 *
 * Returns: the path as the #TplEvent:account property
 */
const gchar *
tpl_event_get_account_path (TplEvent *self)
{
  g_return_val_if_fail (TPL_IS_EVENT (self), NULL);
  g_return_val_if_fail (TP_IS_ACCOUNT (self->priv->account), NULL);

  return tp_proxy_get_object_path (self->priv->account);
}


const gchar *
_tpl_event_get_channel_path (TplEvent *self)
{
  g_return_val_if_fail (TPL_IS_EVENT (self), NULL);

  return self->priv->channel_path;
}


/**
 * tpl_event_equal:
 * @self: TplEvent subclass instance
 * @data: an instance of the same TplEvent subclass of @self
 *
 * Checks if two instances of TplEvent represent the same data
 *
 * Returns: %TRUE if @data is the same type of @self and they hold the same
 * data, %FALSE otherwise
 */
gboolean
tpl_event_equal (TplEvent *self,
    TplEvent *data)
{
  g_return_val_if_fail (TPL_IS_EVENT (self), FALSE);
  g_return_val_if_fail (TPL_IS_EVENT (data), FALSE);

  return TPL_EVENT_GET_CLASS (self)->equal (self, data);
}

/**
 * tpl_event_get_account:
 * @self: a #TplEvent
 *
 * <!-- no more to say -->
 *
 * Returns: (transfer none): the same account as the #TplEvent:account property
 */
TpAccount *
tpl_event_get_account (TplEvent *self)
{
  return self->priv->account;
}