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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
/*
* 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 "log-entry.h"
#include <glib.h>
#include <telepathy-glib/util.h>
#include <telepathy-logger/contact.h>
#include <telepathy-logger/debug.h>
#include <telepathy-logger/util.h>
/**
* SECTION:log-entry
* @title: TplLogEntry
* @short_description: Abstract representation of a log entry
* @see_also: #TplLogEntryText and other subclasses when they'll exist
*
* The TPLogger log entry represent a generic log entry, which will be
* specialied by subclasses of #TplLogEntry.
*/
G_DEFINE_ABSTRACT_TYPE (TplLogEntry, tpl_log_entry, G_TYPE_OBJECT)
static void tpl_log_entry_set_log_id (TplLogEntry *self, guint data);
#define GET_PRIV(obj) TPL_GET_PRIV (obj, TplLogEntry)
struct _TplLogEntryPriv
{
guint log_id;
time_t timestamp;
TplLogEntrySignalType signal_type;
gchar *chat_id;
/* incoming/outgoing */
TplLogEntryDirection direction;
/* message and receiver may be NULL depending on the signal. ie. status
* changed signals set only the sender */
TplContact *sender;
TplContact *receiver;
};
enum {
PROP0,
PROP_TIMESTAMP,
PROP_SIGNAL_TYPE,
PROP_LOG_ID,
PROP_DIRECTION,
PROP_CHAT_ID,
PROP_SENDER,
PROP_RECEIVER
};
static void
tpl_log_entry_finalize (GObject *obj)
{
TplLogEntryPriv *priv = GET_PRIV (obj);
g_free (priv->chat_id);
priv->chat_id = NULL;
G_OBJECT_CLASS (tpl_log_entry_parent_class)->finalize (obj);
}
static void
tpl_log_entry_dispose (GObject *obj)
{
TplLogEntry *self = TPL_LOG_ENTRY (obj);
TplLogEntryPriv *priv = GET_PRIV (self);
tpl_object_unref_if_not_null (priv->sender);
priv->sender = NULL;
tpl_object_unref_if_not_null (priv->receiver);
priv->receiver = NULL;
G_OBJECT_CLASS (tpl_log_entry_parent_class)->dispose (obj);
}
static void
get_prop (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec)
{
TplLogEntryPriv *priv = GET_PRIV (object);
switch (param_id)
{
case PROP_TIMESTAMP:
g_value_set_uint (value, priv->timestamp);
break;
case PROP_SIGNAL_TYPE:
g_value_set_uint (value, priv->signal_type);
break;
case PROP_LOG_ID:
g_value_set_uint (value, priv->log_id);
break;
case PROP_DIRECTION:
g_value_set_uint (value, priv->direction);
break;
case PROP_CHAT_ID:
g_value_set_string (value, priv->chat_id);
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
set_prop (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec)
{
TplLogEntry *self = TPL_LOG_ENTRY (object);
switch (param_id) {
case PROP_TIMESTAMP:
tpl_log_entry_set_timestamp (self, g_value_get_uint (value));
break;
case PROP_SIGNAL_TYPE:
tpl_log_entry_set_signal_type (self, g_value_get_uint (value));
break;
case PROP_LOG_ID:
tpl_log_entry_set_log_id (self, g_value_get_uint (value));
break;
case PROP_DIRECTION:
tpl_log_entry_set_direction (self, g_value_get_uint (value));
break;
case PROP_CHAT_ID:
tpl_log_entry_set_chat_id (self, g_value_get_string (value));
break;
case PROP_SENDER:
tpl_log_entry_set_sender (self, g_value_get_object (value));
break;
case PROP_RECEIVER:
tpl_log_entry_set_receiver (self, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
};
}
static void
tpl_log_entry_class_init (TplLogEntryClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GParamSpec *param_spec;
/* to be used by subclasses */
object_class->finalize = tpl_log_entry_finalize;
object_class->dispose = tpl_log_entry_dispose;
object_class->get_property = get_prop;
object_class->set_property = set_prop;
param_spec = g_param_spec_uint ("timestamp",
"Timestamp",
"The timestamp (time_t) for the log entry",
0, G_MAXUINT32, 0,
G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_TIMESTAMP, param_spec);
param_spec = g_param_spec_uint ("signal-type",
"SignalType",
"The signal type which caused the log entry",
0, G_MAXUINT32, TPL_LOG_ENTRY_SIGNAL_NONE,
G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_SIGNAL_TYPE, param_spec);
param_spec = g_param_spec_uint ("log-id",
"LogId",
"Log identification number: the triple LogId+AccountName+ChatId is unique",
0, G_MAXUINT32, 0,
G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_LOG_ID, param_spec);
param_spec = g_param_spec_uint ("direction",
"Direction",
"The direction of the log entry (in/out)",
0, G_MAXUINT32, TPL_LOG_ENTRY_DIRECTION_NONE,
G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_DIRECTION, param_spec);
param_spec = g_param_spec_string ("chat-id",
"ChatId",
"The chat id relative to the log entry's account name",
NULL,
G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_CHAT_ID, param_spec);
param_spec = g_param_spec_object ("sender",
"Sender",
"TplContact instance who originated the log entry",
TPL_TYPE_CONTACT,
G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_SENDER, param_spec);
param_spec = g_param_spec_object ("receiver",
"Receiver",
"TplContact instance destination for the log entry",
TPL_TYPE_CONTACT,
G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_RECEIVER, param_spec);
g_type_class_add_private (object_class, sizeof (TplLogEntryPriv));
klass->get_timestamp = tpl_log_entry_get_timestamp;
klass->get_signal_type = tpl_log_entry_get_signal_type;
klass->get_log_id = tpl_log_entry_get_log_id;
klass->get_direction = tpl_log_entry_get_direction;
klass->get_sender = tpl_log_entry_get_sender;
klass->get_receiver = tpl_log_entry_get_receiver;
klass->get_chat_id = tpl_log_entry_get_chat_id;
klass->set_timestamp = tpl_log_entry_set_timestamp;
klass->set_signal_type = tpl_log_entry_set_signal_type;
klass->set_direction = tpl_log_entry_set_direction;
klass->set_sender = tpl_log_entry_set_sender;
klass->set_receiver = tpl_log_entry_set_receiver;
klass->set_chat_id = tpl_log_entry_set_chat_id;
klass->equal = NULL;
}
static void
tpl_log_entry_init (TplLogEntry *self)
{
TplLogEntryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
TPL_TYPE_LOG_ENTRY, TplLogEntryPriv);
self->priv = priv;
}
TplLogEntry *
tpl_log_entry_new (guint log_id,
const gchar *chat_id,
TplLogEntryDirection direction)
{
return g_object_new (TPL_TYPE_LOG_ENTRY,
"log-id", log_id,
"chat-id", chat_id,
"direction", direction,
NULL);
}
time_t
tpl_log_entry_get_timestamp (TplLogEntry *self)
{
TplLogEntryPriv *priv = GET_PRIV (self);
g_return_val_if_fail (TPL_IS_LOG_ENTRY (self), -1);
priv = GET_PRIV (self);
return priv->timestamp;
}
TplLogEntrySignalType
tpl_log_entry_get_signal_type (TplLogEntry *self)
{
TplLogEntryPriv *priv;
g_return_val_if_fail (TPL_IS_LOG_ENTRY (self), TPL_LOG_ENTRY_SIGNAL_NONE);
priv = GET_PRIV (self);
return priv->signal_type;
}
guint
tpl_log_entry_get_log_id (TplLogEntry *self)
{
TplLogEntryPriv *priv;
g_return_val_if_fail (TPL_IS_LOG_ENTRY (self), 0);
priv = GET_PRIV (self);
return priv->log_id;
}
TplLogEntryDirection
tpl_log_entry_get_direction (TplLogEntry *self)
{
TplLogEntryPriv *priv;
g_return_val_if_fail (TPL_IS_LOG_ENTRY (self),
TPL_LOG_ENTRY_DIRECTION_NONE);
priv = GET_PRIV (self);
return priv->direction;
}
TplContact *
tpl_log_entry_get_sender (TplLogEntry *self)
{
TplLogEntryPriv *priv;
g_return_val_if_fail (TPL_IS_LOG_ENTRY (self), NULL);
priv = GET_PRIV (self);
return priv->sender;
}
TplContact *
tpl_log_entry_get_receiver (TplLogEntry *self)
{
TplLogEntryPriv *priv;
g_return_val_if_fail (TPL_IS_LOG_ENTRY (self), NULL);
priv = GET_PRIV (self);
return priv->receiver;
}
const gchar *
tpl_log_entry_get_chat_id (TplLogEntry *self)
{
TplLogEntryPriv *priv;
g_return_val_if_fail (TPL_IS_LOG_ENTRY (self), NULL);
priv = GET_PRIV (self);
return priv->chat_id;
}
void
tpl_log_entry_set_timestamp (TplLogEntry *self,
time_t data)
{
TplLogEntryPriv *priv = GET_PRIV (self);
g_return_if_fail (TPL_IS_LOG_ENTRY (self));
priv = GET_PRIV (self);
priv->timestamp = data;
g_object_notify (G_OBJECT(self), "timestamp");
}
void
tpl_log_entry_set_signal_type (TplLogEntry *self,
TplLogEntrySignalType data)
{
TplLogEntryPriv *priv;
g_return_if_fail (TPL_IS_LOG_ENTRY (self));
priv = GET_PRIV (self);
priv->signal_type = data;
g_object_notify (G_OBJECT(self), "signal-type");
}
/* set just on construction time */
static void
tpl_log_entry_set_log_id (TplLogEntry *self,
guint data)
{
TplLogEntryPriv *priv;
g_return_if_fail (TPL_IS_LOG_ENTRY (self));
priv = GET_PRIV (self);
priv->log_id = data;
g_object_notify (G_OBJECT(self), "log-id");
}
void
tpl_log_entry_set_direction (TplLogEntry *self,
TplLogEntryDirection data)
{
TplLogEntryPriv *priv;
g_return_if_fail (TPL_IS_LOG_ENTRY (self));
priv = GET_PRIV (self);
priv->direction = data;
g_object_notify (G_OBJECT(self), "direction");
}
void
tpl_log_entry_set_sender (TplLogEntry *self,
TplContact *data)
{
TplLogEntryPriv *priv;
g_return_if_fail (TPL_IS_LOG_ENTRY (self));
g_return_if_fail (TPL_IS_CONTACT (data) || data == NULL);
priv = GET_PRIV (self);
tpl_object_unref_if_not_null (priv->sender);
priv->sender = data;
tpl_object_ref_if_not_null (data);
g_object_notify (G_OBJECT(self), "sender");
}
void
tpl_log_entry_set_receiver (TplLogEntry *self,
TplContact *data)
{
TplLogEntryPriv *priv;
g_return_if_fail (TPL_IS_LOG_ENTRY (self));
g_return_if_fail (TPL_IS_CONTACT (data) || data == NULL);
priv = GET_PRIV (self);
tpl_object_unref_if_not_null (priv->receiver);
priv->receiver = data;
tpl_object_ref_if_not_null (data);
g_object_notify (G_OBJECT(self), "receiver");
}
void
tpl_log_entry_set_chat_id (TplLogEntry *self,
const gchar *data)
{
TplLogEntryPriv *priv;
g_return_if_fail (TPL_IS_LOG_ENTRY (self));
priv = GET_PRIV (self);
g_free (priv->chat_id);
priv->chat_id = g_strdup (data);
g_object_notify (G_OBJECT(self), "chat-id");
}
/**
* log_entry:
* @self: TplLogEntry subclass instance
* @data: an instance of the same class of @self,
*
* Checks if two instances of TplLogEntry represent the same data
*
* Returns: %TRUE if @data is the same type of @self and they hold the same
* data, %FALSE otherwise
*/
gboolean
tpl_log_entry_equal (TplLogEntry *self,
TplLogEntry *data)
{
g_return_val_if_fail (TPL_IS_LOG_ENTRY (self), FALSE);
g_return_val_if_fail (TPL_IS_LOG_ENTRY (data), FALSE);
return TPL_LOG_ENTRY_GET_CLASS (self)->equal (self, data);
}
|