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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Xavier Claessens <xclaesse@gmail.com>
29 #include "empathy-message.h"
30 #include "empathy-enum-types.h"
32 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyMessage)
34 TpChannelTextMessageType type;
35 EmpathyContact *sender;
36 EmpathyContact *receiver;
41 static void empathy_message_finalize (GObject *object);
42 static void message_get_property (GObject *object,
46 static void message_set_property (GObject *object,
51 G_DEFINE_TYPE (EmpathyMessage, empathy_message, G_TYPE_OBJECT);
63 empathy_message_class_init (EmpathyMessageClass *class)
65 GObjectClass *object_class;
67 object_class = G_OBJECT_CLASS (class);
68 object_class->finalize = empathy_message_finalize;
69 object_class->get_property = message_get_property;
70 object_class->set_property = message_set_property;
72 g_object_class_install_property (object_class,
74 g_param_spec_uint ("type",
76 "The type of message",
77 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
78 TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY,
79 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
81 g_object_class_install_property (object_class,
83 g_param_spec_object ("sender",
85 "The sender of the message",
88 g_object_class_install_property (object_class,
90 g_param_spec_object ("receiver",
92 "The receiver of the message",
95 g_object_class_install_property (object_class,
97 g_param_spec_string ("body",
99 "The content of the message",
102 g_object_class_install_property (object_class,
104 g_param_spec_long ("timestamp",
113 g_type_class_add_private (object_class, sizeof (EmpathyMessagePriv));
118 empathy_message_init (EmpathyMessage *message)
120 EmpathyMessagePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
121 EMPATHY_TYPE_MESSAGE, EmpathyMessagePriv);
123 message->priv = priv;
124 priv->timestamp = empathy_time_get_current ();
128 empathy_message_finalize (GObject *object)
130 EmpathyMessagePriv *priv;
132 priv = GET_PRIV (object);
135 g_object_unref (priv->sender);
137 if (priv->receiver) {
138 g_object_unref (priv->receiver);
143 G_OBJECT_CLASS (empathy_message_parent_class)->finalize (object);
147 message_get_property (GObject *object,
152 EmpathyMessagePriv *priv;
154 priv = GET_PRIV (object);
158 g_value_set_uint (value, priv->type);
161 g_value_set_object (value, priv->sender);
164 g_value_set_object (value, priv->receiver);
167 g_value_set_string (value, priv->body);
170 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
176 message_set_property (GObject *object,
181 EmpathyMessagePriv *priv;
183 priv = GET_PRIV (object);
187 empathy_message_set_tptype (EMPATHY_MESSAGE (object),
188 g_value_get_uint (value));
191 empathy_message_set_sender (EMPATHY_MESSAGE (object),
192 EMPATHY_CONTACT (g_value_get_object (value)));
195 empathy_message_set_receiver (EMPATHY_MESSAGE (object),
196 EMPATHY_CONTACT (g_value_get_object (value)));
199 empathy_message_set_body (EMPATHY_MESSAGE (object),
200 g_value_get_string (value));
203 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
209 empathy_message_new (const gchar *body)
211 return g_object_new (EMPATHY_TYPE_MESSAGE,
216 TpChannelTextMessageType
217 empathy_message_get_tptype (EmpathyMessage *message)
219 EmpathyMessagePriv *priv;
221 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message),
222 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL);
224 priv = GET_PRIV (message);
230 empathy_message_set_tptype (EmpathyMessage *message,
231 TpChannelTextMessageType type)
233 EmpathyMessagePriv *priv;
235 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
237 priv = GET_PRIV (message);
241 g_object_notify (G_OBJECT (message), "type");
245 empathy_message_get_sender (EmpathyMessage *message)
247 EmpathyMessagePriv *priv;
249 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
251 priv = GET_PRIV (message);
257 empathy_message_set_sender (EmpathyMessage *message, EmpathyContact *contact)
259 EmpathyMessagePriv *priv;
260 EmpathyContact *old_sender;
262 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
263 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
265 priv = GET_PRIV (message);
267 old_sender = priv->sender;
268 priv->sender = g_object_ref (contact);
271 g_object_unref (old_sender);
274 g_object_notify (G_OBJECT (message), "sender");
278 empathy_message_get_receiver (EmpathyMessage *message)
280 EmpathyMessagePriv *priv;
282 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
284 priv = GET_PRIV (message);
286 return priv->receiver;
290 empathy_message_set_receiver (EmpathyMessage *message, EmpathyContact *contact)
292 EmpathyMessagePriv *priv;
293 EmpathyContact *old_receiver;
295 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
296 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
298 priv = GET_PRIV (message);
300 old_receiver = priv->receiver;
301 priv->receiver = g_object_ref (contact);
304 g_object_unref (old_receiver);
307 g_object_notify (G_OBJECT (message), "receiver");
311 empathy_message_get_body (EmpathyMessage *message)
313 EmpathyMessagePriv *priv;
315 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
317 priv = GET_PRIV (message);
323 empathy_message_set_body (EmpathyMessage *message,
326 EmpathyMessagePriv *priv = GET_PRIV (message);
327 TpChannelTextMessageType type;
329 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
334 type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
335 if (g_str_has_prefix (body, "/me")) {
336 type = TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
339 else if (g_str_has_prefix (body, "/say")) {
344 priv->body = g_strdup (body);
347 if (type != priv->type) {
348 empathy_message_set_tptype (message, type);
351 g_object_notify (G_OBJECT (message), "body");
355 empathy_message_get_timestamp (EmpathyMessage *message)
357 EmpathyMessagePriv *priv;
359 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
361 priv = GET_PRIV (message);
363 return priv->timestamp;
367 empathy_message_set_timestamp (EmpathyMessage *message,
370 EmpathyMessagePriv *priv;
372 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
373 g_return_if_fail (timestamp >= -1);
375 priv = GET_PRIV (message);
377 if (timestamp <= 0) {
378 priv->timestamp = empathy_time_get_current ();
380 priv->timestamp = timestamp;
383 g_object_notify (G_OBJECT (message), "timestamp");
387 empathy_message_get_date_and_time (EmpathyMessage *message, time_t *timestamp)
393 *timestamp = empathy_message_get_timestamp (message);
396 if (timestamp <= 0) {
397 *timestamp = empathy_time_get_current ();
400 date = g_date_new ();
401 g_date_set_time_t (date, *timestamp);
406 #define IS_SEPARATOR(ch) (ch == ' ' || ch == ',' || ch == '.' || ch == ':')
408 empathy_message_should_highlight (EmpathyMessage *message)
410 EmpathyContact *contact;
411 const gchar *msg, *to;
412 gchar *cf_msg, *cf_to;
416 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
420 msg = empathy_message_get_body (message);
425 contact = empathy_message_get_receiver (message);
426 if (!contact || !empathy_contact_is_user (contact)) {
430 to = empathy_contact_get_name (contact);
435 cf_msg = g_utf8_casefold (msg, -1);
436 cf_to = g_utf8_casefold (to, -1);
438 ch = strstr (cf_msg, cf_to);
443 /* Not first in the message */
444 if (!IS_SEPARATOR (*(ch - 1))) {
449 ch = ch + strlen (cf_to);
450 if (ch >= cf_msg + strlen (cf_msg)) {
455 if (IS_SEPARATOR (*ch)) {
467 TpChannelTextMessageType
468 empathy_message_type_from_str (const gchar *type_str)
470 if (strcmp (type_str, "normal") == 0) {
471 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
473 if (strcmp (type_str, "action") == 0) {
474 return TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
476 else if (strcmp (type_str, "notice") == 0) {
477 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE;
479 else if (strcmp (type_str, "auto-reply") == 0) {
480 return TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY;
483 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
487 empathy_message_type_to_str (TpChannelTextMessageType type)
490 case TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION:
492 case TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE:
494 case TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY: