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

#include <glib-object.h>
#include <telepathy-glib/util.h>

#include <telepathy-logger/event.h>
#include <telepathy-logger/event-internal.h>

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

/**
 * SECTION:event-text
 * @title: TplEventText
 * @short_description: Representation of a text log event
 *
 * A subclass of #TplEvent representing a text log event.
 */

/**
 * TplEventText:
 *
 * An object representing a text log event.
 */

/**
 * TPL_EVENT_TEXT_MSG_ID_IS_VALID:
 * @msg: a message ID
 *
 * Return whether a message ID is valid.
 *
 * If %FALSE is returned, it means that either an invalid input has been
 * passed, or the TplEvent is currently set to %TPL_EVENT_TEXT_MSG_ID_UNKNOWN
 * or %TPL_EVENT_TEXT_MSG_ID_ACKNOWLEDGED.
 *
 * Returns: %TRUE if the argument is a valid message ID or %FALSE otherwise.
 */

/**
 * TPL_EVENT_TEXT_MSG_ID_UNKNOWN:
 *
 * Special value used instead of a message ID to indicate a message with an
 * unknown status (before _tpl_event_set_pending_msg_id() was called, or
 * when it wasn't possible to obtain the message ID).
 */

/**
 * TPL_EVENT_TEXT_MSG_ID_ACKNOWLEDGED:
 *
 * Special value used instead of a message ID to indicate an acknowledged
 * message.
 */

G_DEFINE_TYPE (TplEventText, tpl_event_text, TPL_TYPE_EVENT)

struct _TplEventTextPriv
{
  TplChannelText *tpl_text;
  TpChannelTextMessageType message_type;
  gchar *message;
  gboolean chatroom;
  /* in specs it's guint, TplEvent needs a way to represent ACK'd messages:
   * if pending_msg_id reachs G_MAXINT32, then the problem is elsewhere :-) */
  gint pending_msg_id;
};

enum
{
  PROP_MESSAGE_TYPE = 1,
  PROP_MESSAGE,
  PROP_PENDING_MSG_ID
};


static void
tpl_event_text_dispose (GObject * obj)
{
  TplEventText *self = TPL_EVENT_TEXT (obj);
  TplEventTextPriv *priv = self->priv;

  if (priv->tpl_text != NULL)
    {
      g_object_unref (priv->tpl_text);
      priv->tpl_text = NULL;
    }

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


static void
tpl_event_text_finalize (GObject * obj)
{
  TplEventText *self = TPL_EVENT_TEXT (obj);
  TplEventTextPriv *priv = self->priv;

  g_free (priv->message);
  priv->message = NULL;

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


static void
tpl_event_text_get_property (GObject *object,
    guint param_id,
    GValue *value,
    GParamSpec *pspec)
{
  TplEventTextPriv *priv = TPL_EVENT_TEXT (object)->priv;

  switch (param_id)
    {
      case PROP_MESSAGE_TYPE:
        g_value_set_uint (value, priv->message_type);
        break;
      case PROP_MESSAGE:
        g_value_set_string (value, priv->message);
        break;
      case PROP_PENDING_MSG_ID:
        g_value_set_int (value, priv->pending_msg_id);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
    }
}


static void
tpl_event_text_set_property (GObject *object,
    guint param_id,
    const GValue *value,
    GParamSpec *pspec)
{
  TplEventText *self = TPL_EVENT_TEXT (object);

  switch (param_id) {
      case PROP_MESSAGE_TYPE:
        _tpl_event_text_set_message_type (self, g_value_get_uint (value));
        break;
      case PROP_MESSAGE:
        _tpl_event_text_set_message (self, g_value_get_string (value));
        break;
      case PROP_PENDING_MSG_ID:
        _tpl_event_text_set_pending_msg_id (self, g_value_get_int (value));
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
  }
}


static void tpl_event_text_class_init (TplEventTextClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  TplEventClass *event_class = TPL_EVENT_CLASS (klass);
  GParamSpec *param_spec;

  object_class->finalize = tpl_event_text_finalize;
  object_class->dispose = tpl_event_text_dispose;
  object_class->get_property = tpl_event_text_get_property;
  object_class->set_property = tpl_event_text_set_property;
  event_class->equal = _tpl_event_text_equal;

  param_spec = g_param_spec_uint ("message-type",
      "MessageType",
      "The message type for a Text log event",
      0, G_MAXUINT32, TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_MESSAGE_TYPE, param_spec);

  param_spec = g_param_spec_string ("message",
      "Message",
      "The text message of the log event",
      NULL,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_MESSAGE, param_spec);

  /**
   * TplEventText::pending-msg-id:
   *
   * The pending message id for the current log event.
   * The default value, is #TPL_EVENT_TEXT_MSG_ID_UNKNOWN,
   * meaning that it's not possible to know if the message is pending or has
   * been acknowledged.
   *
   * An object instantiating a TplEvent subclass should explicitly set it
   * to a valid msg-id number (id>=0) or to #TPL_EVENT_TEXT_MSG_ID_ACKNOWLEDGED
   * when acknowledged or if the event is a result of
   * 'sent' signal.
   * In fact a sent event is considered as 'automatically' ACK by TPL.
   *
   * The pending message id value is only meaningful when associated to the
   * #TplEvent::channel-path property.
   * The couple (channel-path, pending-msg-id) cannot be considered unique,
   * though, since a message-id might be reused over time.
   *
   * Use #TplEvent::log-id for a unique identifier within TPL.
   */
  param_spec = g_param_spec_int ("pending-msg-id",
      "PendingMessageId",
      "Pending Message ID, if set, the log event is set as pending for ACK."
      " Default to -1 meaning not pending.",
      -1, G_MAXUINT32, TPL_EVENT_TEXT_MSG_ID_ACKNOWLEDGED,
      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_PENDING_MSG_ID,
      param_spec);

  g_type_class_add_private (object_class, sizeof (TplEventTextPriv));

}


static void
tpl_event_text_init (TplEventText *self)
{
  TplEventTextPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      TPL_TYPE_EVENT_TEXT, TplEventTextPriv);
  self->priv = priv;
}


TplEventText *
_tpl_event_text_new (const gchar *log_id,
    TpAccount *account)
{
  return g_object_new (TPL_TYPE_EVENT_TEXT,
      "log-id", log_id,
      "account", account,
      NULL);
}

static gchar *message_types[] = {
    "normal",
    "action",
    "notice",
    "auto-reply",
    "delivery-report",
    NULL };


/**
 * _tpl_event_text_message_type_from_str
 * @type_str: string to transform into a #TpChannelTextMessageType
 *
 * Maps strings into enum #TpChannelTextMessageType values.
 *
 * Returns: the relative value from enum #TpChannelTextMessageType if a
 * mapping is found, or defaults to %TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL if not.
 */
TpChannelTextMessageType
_tpl_event_text_message_type_from_str (const gchar *type_str)
{
  guint i;
  for (i = 0; i < G_N_ELEMENTS (message_types); ++i)
    if (!tp_strdiff (type_str, message_types[i]))
      return (TpChannelTextMessageType) i;

  /* default case */
  return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
}


/**
 * _tpl_event_text_message_type_to_str
 * @msg_type: message type to transform into a string
 *
 * Maps enum #TpChannelTextMessageType values into strings
 *
 * Returns: a string representation for @msg_type or NULL if @msg_type is not
 * a legal value for %TpChannelTextMessageType.
 */
const gchar *
_tpl_event_text_message_type_to_str (TpChannelTextMessageType msg_type)
{
  g_return_val_if_fail (G_N_ELEMENTS (message_types) >= msg_type, NULL);

  return message_types[msg_type];
}


gboolean
_tpl_event_text_is_chatroom (TplEventText * self)
{
  g_return_val_if_fail (TPL_IS_EVENT_TEXT (self), FALSE);

  return self->priv->chatroom;
}


TplChannelText *
_tpl_event_text_get_tpl_channel_text (TplEventText * self)
{
  g_return_val_if_fail (TPL_IS_EVENT_TEXT (self), NULL);

  return self->priv->tpl_text;
}


/**
 * tpl_event_text_get_message
 * @self: a #TplEventText
 *
 * Returns: the same message as the #TplEventText:message property
 */
const gchar *
tpl_event_text_get_message (TplEventText * self)
{
  g_return_val_if_fail (TPL_IS_EVENT_TEXT (self), NULL);

  return self->priv->message;
}

TpChannelTextMessageType
_tpl_event_text_get_message_type (TplEventText * self)
{
  g_return_val_if_fail (TPL_IS_EVENT_TEXT (self),
      TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL);

  return self->priv->message_type;
}


void
_tpl_event_text_set_tpl_channel_text (TplEventText * self,
    TplChannelText *data)
{
  TplEventTextPriv *priv;

  g_return_if_fail (TPL_IS_EVENT_TEXT (self));
  g_return_if_fail (TPL_IS_CHANNEL_TEXT (data) || data == NULL);

  priv = self->priv;
  if (priv->tpl_text != NULL)
    g_object_unref (priv->tpl_text);
  priv->tpl_text = g_object_ref (data);
}


void
_tpl_event_text_set_message (TplEventText *self,
    const gchar *data)
{
  TplEventTextPriv *priv;

  if (data == NULL)
    return;

  g_return_if_fail (TPL_IS_EVENT_TEXT (self));

  priv = self->priv;

  g_free (priv->message);
  priv->message = g_strdup (data);
}


void
_tpl_event_text_set_message_type (TplEventText *self,
    TpChannelTextMessageType data)
{
  g_return_if_fail (TPL_IS_EVENT_TEXT (self));

  self->priv->message_type = data;
}


void
_tpl_event_text_set_chatroom (TplEventText *self,
    gboolean data)
{
  g_return_if_fail (TPL_IS_EVENT_TEXT (self));

  self->priv->chatroom = data;
}

gboolean
_tpl_event_text_equal (TplEvent *message1,
    TplEvent *message2)
{
  g_return_val_if_fail (TPL_IS_EVENT_TEXT (message1), FALSE);
  g_return_val_if_fail (TPL_IS_EVENT_TEXT (message2), FALSE);

  /*
  if (priv1->id == priv2->id && !tp_strdiff (priv1->body, priv2->body)) {
  if (priv1->type == priv2->type)
    if (!tp_strdiff (priv1->event.text->message, priv2->event.text->message)) {
    }
  */
  return !tp_strdiff (_tpl_event_get_log_id (message1),
      _tpl_event_get_log_id (message2));
}

/**
 * _tpl_event_set_pending_msg_id:
 * @self: TplEventText instance
 * @data: the pending message ID
 *
 * Sets @self to be associated to pending message id @data.
 *
 * @see_also: #TplEvent::pending-msg-id for special values.
 */
void
_tpl_event_text_set_pending_msg_id (TplEventText *self,
    gint data)
{
  g_return_if_fail (TPL_IS_EVENT (self));

  self->priv->pending_msg_id = data;
  g_object_notify (G_OBJECT (self), "pending-msg-id");
}

/*
 * tpl_event_text_get_pending_msg_id
 * @self: a #TplEvent
 *
 * Returns: the id as the #TplEventText:pending-msg-id property
 */
gint
_tpl_event_text_get_pending_msg_id (TplEventText *self)
{
  g_return_val_if_fail (TPL_IS_EVENT_TEXT (self), -1);

  return self->priv->pending_msg_id;
}


gboolean
_tpl_event_text_is_pending (TplEventText *self)
{
  return TPL_EVENT_TEXT_MSG_ID_IS_VALID (
      _tpl_event_text_get_pending_msg_id (self));
}