1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2004-2007 Imendio AB
4 * Copyright (C) 2007-2008 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Xavier Claessens <xclaesse@gmail.com>
29 #include <telepathy-glib/util.h>
31 #include "empathy-message.h"
32 #include "empathy-utils.h"
33 #include "empathy-enum-types.h"
35 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyMessage)
37 TpChannelTextMessageType type;
38 EmpathyContact *sender;
39 EmpathyContact *receiver;
47 static void empathy_message_finalize (GObject *object);
48 static void message_get_property (GObject *object,
52 static void message_set_property (GObject *object,
57 G_DEFINE_TYPE (EmpathyMessage, empathy_message, G_TYPE_OBJECT);
71 empathy_message_class_init (EmpathyMessageClass *class)
73 GObjectClass *object_class;
75 object_class = G_OBJECT_CLASS (class);
76 object_class->finalize = empathy_message_finalize;
77 object_class->get_property = message_get_property;
78 object_class->set_property = message_set_property;
80 g_object_class_install_property (object_class,
82 g_param_spec_uint ("type",
84 "The type of message",
85 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
86 TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY,
87 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
89 g_object_class_install_property (object_class,
91 g_param_spec_object ("sender",
93 "The sender of the message",
96 g_object_class_install_property (object_class,
98 g_param_spec_object ("receiver",
100 "The receiver of the message",
101 EMPATHY_TYPE_CONTACT,
103 g_object_class_install_property (object_class,
105 g_param_spec_string ("body",
107 "The content of the message",
110 g_object_class_install_property (object_class,
112 g_param_spec_long ("timestamp",
119 g_object_class_install_property (object_class,
121 g_param_spec_boolean ("is-backlog",
123 "If the message belongs to history",
128 g_object_class_install_property (object_class,
130 g_param_spec_boolean ("incoming",
132 "If this is an incoming (as opposed to sent) message",
134 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
136 g_type_class_add_private (object_class, sizeof (EmpathyMessagePriv));
141 empathy_message_init (EmpathyMessage *message)
143 EmpathyMessagePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
144 EMPATHY_TYPE_MESSAGE, EmpathyMessagePriv);
146 message->priv = priv;
147 priv->timestamp = empathy_time_get_current ();
151 empathy_message_finalize (GObject *object)
153 EmpathyMessagePriv *priv;
155 priv = GET_PRIV (object);
158 g_object_unref (priv->sender);
160 if (priv->receiver) {
161 g_object_unref (priv->receiver);
166 G_OBJECT_CLASS (empathy_message_parent_class)->finalize (object);
170 message_get_property (GObject *object,
175 EmpathyMessagePriv *priv;
177 priv = GET_PRIV (object);
181 g_value_set_uint (value, priv->type);
184 g_value_set_object (value, priv->sender);
187 g_value_set_object (value, priv->receiver);
190 g_value_set_string (value, priv->body);
193 g_value_set_boolean (value, priv->incoming);
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
202 message_set_property (GObject *object,
207 EmpathyMessagePriv *priv;
209 priv = GET_PRIV (object);
213 empathy_message_set_tptype (EMPATHY_MESSAGE (object),
214 g_value_get_uint (value));
217 empathy_message_set_sender (EMPATHY_MESSAGE (object),
218 EMPATHY_CONTACT (g_value_get_object (value)));
221 empathy_message_set_receiver (EMPATHY_MESSAGE (object),
222 EMPATHY_CONTACT (g_value_get_object (value)));
225 empathy_message_set_body (EMPATHY_MESSAGE (object),
226 g_value_get_string (value));
229 priv->incoming = g_value_get_boolean (value);
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
238 empathy_message_new (const gchar *body)
240 return g_object_new (EMPATHY_TYPE_MESSAGE,
245 TpChannelTextMessageType
246 empathy_message_get_tptype (EmpathyMessage *message)
248 EmpathyMessagePriv *priv;
250 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message),
251 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL);
253 priv = GET_PRIV (message);
259 empathy_message_set_tptype (EmpathyMessage *message,
260 TpChannelTextMessageType type)
262 EmpathyMessagePriv *priv;
264 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
266 priv = GET_PRIV (message);
270 g_object_notify (G_OBJECT (message), "type");
274 empathy_message_get_sender (EmpathyMessage *message)
276 EmpathyMessagePriv *priv;
278 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
280 priv = GET_PRIV (message);
286 empathy_message_set_sender (EmpathyMessage *message, EmpathyContact *contact)
288 EmpathyMessagePriv *priv;
289 EmpathyContact *old_sender;
291 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
292 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
294 priv = GET_PRIV (message);
296 old_sender = priv->sender;
297 priv->sender = g_object_ref (contact);
300 g_object_unref (old_sender);
303 g_object_notify (G_OBJECT (message), "sender");
307 empathy_message_get_receiver (EmpathyMessage *message)
309 EmpathyMessagePriv *priv;
311 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
313 priv = GET_PRIV (message);
315 return priv->receiver;
319 empathy_message_set_receiver (EmpathyMessage *message, EmpathyContact *contact)
321 EmpathyMessagePriv *priv;
322 EmpathyContact *old_receiver;
324 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
325 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
327 priv = GET_PRIV (message);
329 old_receiver = priv->receiver;
330 priv->receiver = g_object_ref (contact);
333 g_object_unref (old_receiver);
336 g_object_notify (G_OBJECT (message), "receiver");
340 empathy_message_get_body (EmpathyMessage *message)
342 EmpathyMessagePriv *priv;
344 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
346 priv = GET_PRIV (message);
352 empathy_message_set_body (EmpathyMessage *message,
355 EmpathyMessagePriv *priv = GET_PRIV (message);
357 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
362 priv->body = g_strdup (body);
367 g_object_notify (G_OBJECT (message), "body");
371 empathy_message_get_timestamp (EmpathyMessage *message)
373 EmpathyMessagePriv *priv;
375 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
377 priv = GET_PRIV (message);
379 return priv->timestamp;
383 empathy_message_set_timestamp (EmpathyMessage *message,
386 EmpathyMessagePriv *priv;
388 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
389 g_return_if_fail (timestamp >= -1);
391 priv = GET_PRIV (message);
393 if (timestamp <= 0) {
394 priv->timestamp = empathy_time_get_current ();
396 priv->timestamp = timestamp;
399 g_object_notify (G_OBJECT (message), "timestamp");
403 empathy_message_is_backlog (EmpathyMessage *message)
405 EmpathyMessagePriv *priv;
407 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
409 priv = GET_PRIV (message);
411 return priv->is_backlog;
415 empathy_message_set_is_backlog (EmpathyMessage *message,
418 EmpathyMessagePriv *priv;
420 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
422 priv = GET_PRIV (message);
424 priv->is_backlog = is_backlog;
426 g_object_notify (G_OBJECT (message), "is-backlog");
429 #define IS_SEPARATOR(ch) (ch == ' ' || ch == ',' || ch == '.' || ch == ':')
431 empathy_message_should_highlight (EmpathyMessage *message)
433 EmpathyContact *contact;
434 const gchar *msg, *to;
435 gchar *cf_msg, *cf_to;
439 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
443 msg = empathy_message_get_body (message);
448 contact = empathy_message_get_receiver (message);
449 if (!contact || !empathy_contact_is_user (contact)) {
453 to = empathy_contact_get_name (contact);
458 cf_msg = g_utf8_casefold (msg, -1);
459 cf_to = g_utf8_casefold (to, -1);
461 ch = strstr (cf_msg, cf_to);
466 /* Not first in the message */
467 if (!IS_SEPARATOR (*(ch - 1))) {
472 ch = ch + strlen (cf_to);
473 if (ch >= cf_msg + strlen (cf_msg)) {
478 if (IS_SEPARATOR (*ch)) {
490 TpChannelTextMessageType
491 empathy_message_type_from_str (const gchar *type_str)
493 if (strcmp (type_str, "normal") == 0) {
494 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
496 if (strcmp (type_str, "action") == 0) {
497 return TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
499 else if (strcmp (type_str, "notice") == 0) {
500 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE;
502 else if (strcmp (type_str, "auto-reply") == 0) {
503 return TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY;
506 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
510 empathy_message_type_to_str (TpChannelTextMessageType type)
513 case TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION:
515 case TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE:
517 case TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY:
525 empathy_message_get_id (EmpathyMessage *message)
527 EmpathyMessagePriv *priv = GET_PRIV (message);
529 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), 0);
535 empathy_message_set_id (EmpathyMessage *message, guint id)
537 EmpathyMessagePriv *priv = GET_PRIV (message);
543 empathy_message_set_incoming (EmpathyMessage *message, gboolean incoming)
545 EmpathyMessagePriv *priv;
547 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
549 priv = GET_PRIV (message);
551 priv->incoming = incoming;
553 g_object_notify (G_OBJECT (message), "incoming");
557 empathy_message_is_incoming (EmpathyMessage *message)
559 EmpathyMessagePriv *priv = GET_PRIV (message);
561 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
563 return priv->incoming;
567 empathy_message_equal (EmpathyMessage *message1, EmpathyMessage *message2)
569 EmpathyMessagePriv *priv1;
570 EmpathyMessagePriv *priv2;
572 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message1), FALSE);
573 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message2), FALSE);
575 priv1 = GET_PRIV (message1);
576 priv2 = GET_PRIV (message2);
578 if (priv1->id == priv2->id && !tp_strdiff (priv1->body, priv2->body)) {