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>
30 #include <telepathy-glib/account.h>
31 #include <telepathy-glib/account-manager.h>
33 #include <telepathy-logger/entity.h>
34 #include <telepathy-logger/entry.h>
35 #include <telepathy-logger/entry-text.h>
37 #include "empathy-message.h"
38 #include "empathy-utils.h"
39 #include "empathy-enum-types.h"
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyMessage)
43 TpChannelTextMessageType type;
44 EmpathyContact *sender;
45 EmpathyContact *receiver;
51 TpChannelTextMessageFlags flags;
54 static void empathy_message_finalize (GObject *object);
55 static void message_get_property (GObject *object,
59 static void message_set_property (GObject *object,
64 G_DEFINE_TYPE (EmpathyMessage, empathy_message, G_TYPE_OBJECT);
79 empathy_message_class_init (EmpathyMessageClass *class)
81 GObjectClass *object_class;
83 object_class = G_OBJECT_CLASS (class);
84 object_class->finalize = empathy_message_finalize;
85 object_class->get_property = message_get_property;
86 object_class->set_property = message_set_property;
88 g_object_class_install_property (object_class,
90 g_param_spec_uint ("type",
92 "The type of message",
93 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
94 TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY,
95 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
97 g_object_class_install_property (object_class,
99 g_param_spec_object ("sender",
101 "The sender of the message",
102 EMPATHY_TYPE_CONTACT,
104 g_object_class_install_property (object_class,
106 g_param_spec_object ("receiver",
108 "The receiver of the message",
109 EMPATHY_TYPE_CONTACT,
111 g_object_class_install_property (object_class,
113 g_param_spec_string ("body",
115 "The content of the message",
118 g_object_class_install_property (object_class,
120 g_param_spec_long ("timestamp",
127 g_object_class_install_property (object_class,
129 g_param_spec_boolean ("is-backlog",
131 "If the message belongs to history",
136 g_object_class_install_property (object_class,
138 g_param_spec_boolean ("incoming",
140 "If this is an incoming (as opposed to sent) message",
142 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
144 g_object_class_install_property (object_class,
146 g_param_spec_uint ("flags",
148 "The TpChannelTextMessageFlags of this message",
150 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152 g_type_class_add_private (object_class, sizeof (EmpathyMessagePriv));
157 empathy_message_init (EmpathyMessage *message)
159 EmpathyMessagePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
160 EMPATHY_TYPE_MESSAGE, EmpathyMessagePriv);
162 message->priv = priv;
163 priv->timestamp = empathy_time_get_current ();
167 empathy_message_finalize (GObject *object)
169 EmpathyMessagePriv *priv;
171 priv = GET_PRIV (object);
174 g_object_unref (priv->sender);
176 if (priv->receiver) {
177 g_object_unref (priv->receiver);
182 G_OBJECT_CLASS (empathy_message_parent_class)->finalize (object);
186 message_get_property (GObject *object,
191 EmpathyMessagePriv *priv;
193 priv = GET_PRIV (object);
197 g_value_set_uint (value, priv->type);
200 g_value_set_object (value, priv->sender);
203 g_value_set_object (value, priv->receiver);
206 g_value_set_string (value, priv->body);
209 g_value_set_boolean (value, priv->incoming);
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
218 message_set_property (GObject *object,
223 EmpathyMessagePriv *priv;
225 priv = GET_PRIV (object);
229 empathy_message_set_tptype (EMPATHY_MESSAGE (object),
230 g_value_get_uint (value));
233 empathy_message_set_sender (EMPATHY_MESSAGE (object),
234 EMPATHY_CONTACT (g_value_get_object (value)));
237 empathy_message_set_receiver (EMPATHY_MESSAGE (object),
238 EMPATHY_CONTACT (g_value_get_object (value)));
241 empathy_message_set_body (EMPATHY_MESSAGE (object),
242 g_value_get_string (value));
245 priv->incoming = g_value_get_boolean (value);
248 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
254 empathy_message_new (const gchar *body)
256 return g_object_new (EMPATHY_TYPE_MESSAGE,
262 empathy_message_from_tpl_log_entry (TplEntry *logentry)
264 EmpathyMessage *retval = NULL;
265 TpAccountManager *acc_man = NULL;
266 TpAccount *account = NULL;
267 TplEntity *receiver = NULL;
268 TplEntity *sender = NULL;
271 g_return_val_if_fail (TPL_IS_ENTRY (logentry), NULL);
273 acc_man = tp_account_manager_dup ();
274 /* FIXME Currently Empathy shows in the log viewer only valid accounts, so it
275 * won't be selected any non-existing (ie removed) account.
276 * When #610455 will be fixed, calling tp_account_manager_ensure_account ()
277 * might add a not existing account to the AM. tp_account_new () probably
278 * will be the best way to handle it.
279 * Note: When creating an EmpathyContact from a TplEntity instance, the
280 * TpAccount is passed *only* to let EmpathyContact be able to retrieve the
281 * avatar (contact_get_avatar_filename () need a TpAccount).
282 * If the way EmpathyContact stores the avatar is changes, it might not be
283 * needed anymore any TpAccount passing and the following call will be
285 account = tp_account_manager_ensure_account (acc_man,
286 tpl_entry_get_account_path (logentry));
287 g_object_unref (acc_man);
289 /* TODO Currently only TplLogEntryText exists as subclass of TplEntry, in
290 * future more TplEntry will exist and EmpathyMessage should probably
291 * be enhanced to support other types of log entries (ie TplLogEntryCall).
293 * For now we just check (simply) that we are dealing with the only supported type,
294 * then there will be a if/then/else or switch handling all the supported
297 if (!TPL_IS_ENTRY_TEXT (logentry))
300 body = g_strdup (tpl_entry_text_get_message (
301 TPL_ENTRY_TEXT (logentry)));
302 receiver = tpl_entry_get_receiver (logentry);
303 sender = tpl_entry_get_sender (logentry);
305 retval = empathy_message_new (body);
306 if (receiver != NULL)
307 empathy_message_set_receiver (retval,
308 empathy_contact_from_tpl_contact (account, receiver));
310 empathy_message_set_sender (retval,
311 empathy_contact_from_tpl_contact (account, sender));
313 empathy_message_set_timestamp (retval,
314 tpl_entry_get_timestamp (logentry));
315 empathy_message_set_id (retval,
316 tpl_entry_text_get_pending_msg_id (TPL_ENTRY_TEXT (logentry)));
317 empathy_message_set_is_backlog (retval, TRUE);
324 TpChannelTextMessageType
325 empathy_message_get_tptype (EmpathyMessage *message)
327 EmpathyMessagePriv *priv;
329 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message),
330 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL);
332 priv = GET_PRIV (message);
338 empathy_message_set_tptype (EmpathyMessage *message,
339 TpChannelTextMessageType type)
341 EmpathyMessagePriv *priv;
343 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
345 priv = GET_PRIV (message);
349 g_object_notify (G_OBJECT (message), "type");
353 empathy_message_get_sender (EmpathyMessage *message)
355 EmpathyMessagePriv *priv;
357 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
359 priv = GET_PRIV (message);
365 empathy_message_set_sender (EmpathyMessage *message, EmpathyContact *contact)
367 EmpathyMessagePriv *priv;
368 EmpathyContact *old_sender;
370 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
371 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
373 priv = GET_PRIV (message);
375 old_sender = priv->sender;
376 priv->sender = g_object_ref (contact);
379 g_object_unref (old_sender);
382 g_object_notify (G_OBJECT (message), "sender");
386 empathy_message_get_receiver (EmpathyMessage *message)
388 EmpathyMessagePriv *priv;
390 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
392 priv = GET_PRIV (message);
394 return priv->receiver;
398 empathy_message_set_receiver (EmpathyMessage *message, EmpathyContact *contact)
400 EmpathyMessagePriv *priv;
401 EmpathyContact *old_receiver;
403 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
404 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
406 priv = GET_PRIV (message);
408 old_receiver = priv->receiver;
409 priv->receiver = g_object_ref (contact);
412 g_object_unref (old_receiver);
415 g_object_notify (G_OBJECT (message), "receiver");
419 empathy_message_get_body (EmpathyMessage *message)
421 EmpathyMessagePriv *priv;
423 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
425 priv = GET_PRIV (message);
431 empathy_message_set_body (EmpathyMessage *message,
434 EmpathyMessagePriv *priv = GET_PRIV (message);
436 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
441 priv->body = g_strdup (body);
446 g_object_notify (G_OBJECT (message), "body");
450 empathy_message_get_timestamp (EmpathyMessage *message)
452 EmpathyMessagePriv *priv;
454 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
456 priv = GET_PRIV (message);
458 return priv->timestamp;
462 empathy_message_set_timestamp (EmpathyMessage *message,
465 EmpathyMessagePriv *priv;
467 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
468 g_return_if_fail (timestamp >= -1);
470 priv = GET_PRIV (message);
472 if (timestamp <= 0) {
473 priv->timestamp = empathy_time_get_current ();
475 priv->timestamp = timestamp;
478 g_object_notify (G_OBJECT (message), "timestamp");
482 empathy_message_is_backlog (EmpathyMessage *message)
484 EmpathyMessagePriv *priv;
486 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
488 priv = GET_PRIV (message);
490 return priv->is_backlog;
494 empathy_message_set_is_backlog (EmpathyMessage *message,
497 EmpathyMessagePriv *priv;
499 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
501 priv = GET_PRIV (message);
503 priv->is_backlog = is_backlog;
505 g_object_notify (G_OBJECT (message), "is-backlog");
508 #define IS_SEPARATOR(ch) (ch == ' ' || ch == ',' || ch == '.' || ch == ':')
510 empathy_message_should_highlight (EmpathyMessage *message)
512 EmpathyContact *contact;
513 const gchar *msg, *to;
514 gchar *cf_msg, *cf_to;
517 TpChannelTextMessageFlags flags;
519 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
523 msg = empathy_message_get_body (message);
528 contact = empathy_message_get_receiver (message);
529 if (!contact || !empathy_contact_is_user (contact)) {
533 to = empathy_contact_get_alias (contact);
538 flags = empathy_message_get_flags (message);
539 if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) {
540 /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
541 * have already been received by the user before (and so are in the logs) */
545 cf_msg = g_utf8_casefold (msg, -1);
546 cf_to = g_utf8_casefold (to, -1);
548 ch = strstr (cf_msg, cf_to);
553 /* Not first in the message */
554 if (!IS_SEPARATOR (*(ch - 1))) {
559 ch = ch + strlen (cf_to);
560 if (ch >= cf_msg + strlen (cf_msg)) {
565 if (IS_SEPARATOR (*ch)) {
577 TpChannelTextMessageType
578 empathy_message_type_from_str (const gchar *type_str)
580 if (strcmp (type_str, "normal") == 0) {
581 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
583 if (strcmp (type_str, "action") == 0) {
584 return TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
586 else if (strcmp (type_str, "notice") == 0) {
587 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE;
589 else if (strcmp (type_str, "auto-reply") == 0) {
590 return TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY;
593 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
597 empathy_message_type_to_str (TpChannelTextMessageType type)
600 case TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION:
602 case TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE:
604 case TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY:
612 empathy_message_get_id (EmpathyMessage *message)
614 EmpathyMessagePriv *priv = GET_PRIV (message);
616 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), 0);
622 empathy_message_set_id (EmpathyMessage *message, guint id)
624 EmpathyMessagePriv *priv = GET_PRIV (message);
630 empathy_message_set_incoming (EmpathyMessage *message, gboolean incoming)
632 EmpathyMessagePriv *priv;
634 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
636 priv = GET_PRIV (message);
638 priv->incoming = incoming;
640 g_object_notify (G_OBJECT (message), "incoming");
644 empathy_message_is_incoming (EmpathyMessage *message)
646 EmpathyMessagePriv *priv = GET_PRIV (message);
648 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
650 return priv->incoming;
654 empathy_message_equal (EmpathyMessage *message1, EmpathyMessage *message2)
656 EmpathyMessagePriv *priv1;
657 EmpathyMessagePriv *priv2;
659 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message1), FALSE);
660 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message2), FALSE);
662 priv1 = GET_PRIV (message1);
663 priv2 = GET_PRIV (message2);
665 if (priv1->timestamp == priv2->timestamp &&
666 !tp_strdiff (priv1->body, priv2->body)) {
673 TpChannelTextMessageFlags
674 empathy_message_get_flags (EmpathyMessage *self)
676 EmpathyMessagePriv *priv = GET_PRIV (self);
678 g_return_val_if_fail (EMPATHY_IS_MESSAGE (self), 0);
684 empathy_message_set_flags (EmpathyMessage *self,
685 TpChannelTextMessageFlags flags)
687 EmpathyMessagePriv *priv;
689 g_return_if_fail (EMPATHY_IS_MESSAGE (self));
691 priv = GET_PRIV (self);
695 g_object_notify (G_OBJECT (self), "flags");