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 <glib/gi18n-lib.h>
31 #include <telepathy-glib/util.h>
32 #include <telepathy-glib/account.h>
33 #include <telepathy-glib/account-manager.h>
35 #include <telepathy-logger/entity.h>
36 #include <telepathy-logger/event.h>
37 #include <telepathy-logger/text-event.h>
39 # include <telepathy-logger/call-event.h>
42 #include "empathy-message.h"
43 #include "empathy-utils.h"
44 #include "empathy-enum-types.h"
46 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyMessage)
48 TpMessage *tp_message;
49 TpChannelTextMessageType type;
50 EmpathyContact *sender;
51 EmpathyContact *receiver;
56 gint64 original_timestamp;
60 TpChannelTextMessageFlags flags;
63 static void empathy_message_finalize (GObject *object);
64 static void message_get_property (GObject *object,
68 static void message_set_property (GObject *object,
73 G_DEFINE_TYPE (EmpathyMessage, empathy_message, G_TYPE_OBJECT);
84 PROP_ORIGINAL_TIMESTAMP,
92 empathy_message_class_init (EmpathyMessageClass *class)
94 GObjectClass *object_class;
96 object_class = G_OBJECT_CLASS (class);
97 object_class->finalize = empathy_message_finalize;
98 object_class->get_property = message_get_property;
99 object_class->set_property = message_set_property;
101 g_object_class_install_property (object_class,
103 g_param_spec_uint ("type",
105 "The type of message",
106 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
107 TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY,
108 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
109 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
110 G_PARAM_CONSTRUCT_ONLY));
111 g_object_class_install_property (object_class,
113 g_param_spec_object ("sender",
115 "The sender of the message",
116 EMPATHY_TYPE_CONTACT,
117 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
118 g_object_class_install_property (object_class,
120 g_param_spec_object ("receiver",
122 "The receiver of the message",
123 EMPATHY_TYPE_CONTACT,
124 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
125 g_object_class_install_property (object_class,
127 g_param_spec_string ("token",
131 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
132 g_object_class_install_property (object_class,
134 g_param_spec_string ("supersedes",
136 "The message-token this message supersedes",
138 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
139 g_object_class_install_property (object_class,
141 g_param_spec_string ("body",
143 "The content of the message",
145 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
146 G_PARAM_CONSTRUCT_ONLY));
147 g_object_class_install_property (object_class,
149 g_param_spec_int64 ("timestamp",
152 G_MININT64, G_MAXINT64, 0,
153 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
154 G_PARAM_CONSTRUCT_ONLY));
155 g_object_class_install_property (object_class,
156 PROP_ORIGINAL_TIMESTAMP,
157 g_param_spec_int64 ("original-timestamp",
158 "Original Timestamp",
159 "Timestamp of the original message",
160 G_MININT64, G_MAXINT64, 0,
161 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
162 g_object_class_install_property (object_class,
164 g_param_spec_boolean ("is-backlog",
166 "If the message belongs to history",
168 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
169 G_PARAM_CONSTRUCT_ONLY));
172 g_object_class_install_property (object_class,
174 g_param_spec_boolean ("incoming",
176 "If this is an incoming (as opposed to sent) message",
178 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
179 G_PARAM_CONSTRUCT_ONLY));
181 g_object_class_install_property (object_class,
183 g_param_spec_uint ("flags",
185 "The TpChannelTextMessageFlags of this message",
187 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
188 G_PARAM_CONSTRUCT_ONLY));
190 g_object_class_install_property (object_class,
192 g_param_spec_object ("tp-message",
194 "The TpMessage of this message",
196 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
197 G_PARAM_CONSTRUCT_ONLY));
199 g_type_class_add_private (object_class, sizeof (EmpathyMessagePriv));
204 empathy_message_init (EmpathyMessage *message)
206 EmpathyMessagePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
207 EMPATHY_TYPE_MESSAGE, EmpathyMessagePriv);
209 message->priv = priv;
210 priv->timestamp = empathy_time_get_current ();
214 empathy_message_finalize (GObject *object)
216 EmpathyMessagePriv *priv;
218 priv = GET_PRIV (object);
221 g_object_unref (priv->sender);
223 if (priv->receiver) {
224 g_object_unref (priv->receiver);
227 if (priv->tp_message) {
228 g_object_unref (priv->tp_message);
231 g_free (priv->token);
232 g_free (priv->supersedes);
235 G_OBJECT_CLASS (empathy_message_parent_class)->finalize (object);
239 message_get_property (GObject *object,
244 EmpathyMessagePriv *priv;
246 priv = GET_PRIV (object);
250 g_value_set_uint (value, priv->type);
253 g_value_set_object (value, priv->sender);
256 g_value_set_object (value, priv->receiver);
259 g_value_set_string (value, priv->token);
261 case PROP_SUPERSEDES:
262 g_value_set_string (value, priv->supersedes);
265 g_value_set_string (value, priv->body);
268 g_value_set_int64 (value, priv->timestamp);
270 case PROP_ORIGINAL_TIMESTAMP:
271 g_value_set_int64 (value, priv->original_timestamp);
273 case PROP_IS_BACKLOG:
274 g_value_set_boolean (value, priv->is_backlog);
277 g_value_set_boolean (value, priv->incoming);
280 g_value_set_uint (value, priv->flags);
282 case PROP_TP_MESSAGE:
283 g_value_set_object (value, priv->tp_message);
286 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
292 message_set_property (GObject *object,
297 EmpathyMessagePriv *priv;
299 priv = GET_PRIV (object);
303 priv->type = g_value_get_uint (value);
306 empathy_message_set_sender (EMPATHY_MESSAGE (object),
307 EMPATHY_CONTACT (g_value_get_object (value)));
310 empathy_message_set_receiver (EMPATHY_MESSAGE (object),
311 EMPATHY_CONTACT (g_value_get_object (value)));
314 g_assert (priv->token == NULL); /* construct only */
315 priv->token = g_value_dup_string (value);
317 case PROP_SUPERSEDES:
318 g_assert (priv->supersedes == NULL); /* construct only */
319 priv->supersedes = g_value_dup_string (value);
322 g_assert (priv->body == NULL); /* construct only */
323 priv->body = g_value_dup_string (value);
326 priv->timestamp = g_value_get_int64 (value);
327 if (priv->timestamp <= 0)
328 priv->timestamp = empathy_time_get_current ();
330 case PROP_ORIGINAL_TIMESTAMP:
331 priv->original_timestamp = g_value_get_int64 (value);
333 case PROP_IS_BACKLOG:
334 priv->is_backlog = g_value_get_boolean (value);
337 priv->incoming = g_value_get_boolean (value);
340 priv->flags = g_value_get_uint (value);
342 case PROP_TP_MESSAGE:
343 priv->tp_message = g_value_dup_object (value);
346 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
352 empathy_message_from_tpl_log_event (TplEvent *logevent)
354 EmpathyMessage *retval = NULL;
355 TpAccountManager *acc_man = NULL;
356 TpAccount *account = NULL;
357 TplEntity *receiver = NULL;
358 TplEntity *sender = NULL;
360 const gchar *token = NULL, *supersedes = NULL;
361 EmpathyContact *contact;
362 TpChannelTextMessageType type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
363 gint64 timestamp, original_timestamp = 0;
365 g_return_val_if_fail (TPL_IS_EVENT (logevent), NULL);
367 acc_man = tp_account_manager_dup ();
368 /* FIXME Currently Empathy shows in the log viewer only valid accounts, so it
369 * won't be selected any non-existing (ie removed) account.
370 * When #610455 will be fixed, calling tp_account_manager_ensure_account ()
371 * might add a not existing account to the AM. tp_account_new () probably
372 * will be the best way to handle it.
373 * Note: When creating an EmpathyContact from a TplEntity instance, the
374 * TpAccount is passed *only* to let EmpathyContact be able to retrieve the
375 * avatar (contact_get_avatar_filename () need a TpAccount).
376 * If the way EmpathyContact stores the avatar is changes, it might not be
377 * needed anymore any TpAccount passing and the following call will be
379 account = tp_account_manager_ensure_account (acc_man,
380 tpl_event_get_account_path (logevent));
381 g_object_unref (acc_man);
383 if (TPL_IS_TEXT_EVENT (logevent)) {
384 TplTextEvent *textevent = TPL_TEXT_EVENT (logevent);
386 supersedes = tpl_text_event_get_supersedes_token (textevent);
388 /* tp-logger is kind of messy in that instead of having
389 * timestamp and original-timestamp like Telepathy it has
390 * timestamp (which is the original) and edited-timestamp,
391 * (which is when the message was edited) */
392 if (tp_str_empty (supersedes)) {
393 /* not an edited message */
394 timestamp = tpl_event_get_timestamp (logevent);
396 /* this is an edited event */
397 original_timestamp = tpl_event_get_timestamp (logevent);
398 timestamp = tpl_text_event_get_edit_timestamp (textevent);
401 body = g_strdup (tpl_text_event_get_message (textevent));
403 type = tpl_text_event_get_message_type (TPL_TEXT_EVENT (logevent));
404 token = tpl_text_event_get_message_token (textevent);
406 #ifdef HAVE_CALL_LOGS
407 else if (TPL_IS_CALL_EVENT (logevent)) {
408 TplCallEvent *call = TPL_CALL_EVENT (logevent);
410 timestamp = tpl_event_get_timestamp (logevent);
412 if (tpl_call_event_get_end_reason (call) == TPL_CALL_END_REASON_NO_ANSWER)
413 body = g_strdup_printf (_("Missed call from %s"),
414 tpl_entity_get_alias (tpl_event_get_sender (logevent)));
415 else if (tpl_entity_get_entity_type (tpl_event_get_sender (logevent)) == TPL_ENTITY_SELF)
416 /* Translators: this is an outgoing call, e.g. 'Called Alice' */
417 body = g_strdup_printf (_("Called %s"),
418 tpl_entity_get_alias (tpl_event_get_receiver (logevent)));
420 body = g_strdup_printf (_("Call from %s"),
421 tpl_entity_get_alias (tpl_event_get_sender (logevent)));
425 /* Unknown event type */
429 receiver = tpl_event_get_receiver (logevent);
430 sender = tpl_event_get_sender (logevent);
432 retval = g_object_new (EMPATHY_TYPE_MESSAGE,
435 "supersedes", supersedes,
438 "timestamp", timestamp,
439 "original-timestamp", original_timestamp,
442 if (receiver != NULL) {
443 contact = empathy_contact_from_tpl_contact (account, receiver);
444 empathy_message_set_receiver (retval, contact);
445 g_object_unref (contact);
448 if (sender != NULL) {
449 contact = empathy_contact_from_tpl_contact (account, sender);
450 empathy_message_set_sender (retval, contact);
451 g_object_unref (contact);
460 empathy_message_get_tp_message (EmpathyMessage *message)
462 EmpathyMessagePriv *priv;
464 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
466 priv = GET_PRIV (message);
468 return priv->tp_message;
471 TpChannelTextMessageType
472 empathy_message_get_tptype (EmpathyMessage *message)
474 EmpathyMessagePriv *priv;
476 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message),
477 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL);
479 priv = GET_PRIV (message);
485 empathy_message_get_sender (EmpathyMessage *message)
487 EmpathyMessagePriv *priv;
489 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
491 priv = GET_PRIV (message);
497 empathy_message_set_sender (EmpathyMessage *message, EmpathyContact *contact)
499 EmpathyMessagePriv *priv;
500 EmpathyContact *old_sender;
502 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
503 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
505 priv = GET_PRIV (message);
507 old_sender = priv->sender;
508 priv->sender = g_object_ref (contact);
511 g_object_unref (old_sender);
514 g_object_notify (G_OBJECT (message), "sender");
518 empathy_message_get_receiver (EmpathyMessage *message)
520 EmpathyMessagePriv *priv;
522 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
524 priv = GET_PRIV (message);
526 return priv->receiver;
530 empathy_message_set_receiver (EmpathyMessage *message, EmpathyContact *contact)
532 EmpathyMessagePriv *priv;
533 EmpathyContact *old_receiver;
535 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
536 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
538 priv = GET_PRIV (message);
540 old_receiver = priv->receiver;
541 priv->receiver = g_object_ref (contact);
544 g_object_unref (old_receiver);
547 g_object_notify (G_OBJECT (message), "receiver");
551 empathy_message_get_token (EmpathyMessage *message)
553 EmpathyMessagePriv *priv;
555 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
557 priv = GET_PRIV (message);
563 empathy_message_get_supersedes (EmpathyMessage *message)
565 EmpathyMessagePriv *priv;
567 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
569 priv = GET_PRIV (message);
571 return priv->supersedes;
575 empathy_message_is_edit (EmpathyMessage *message)
577 EmpathyMessagePriv *priv;
579 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
581 priv = GET_PRIV (message);
583 return !tp_str_empty (priv->supersedes);
587 empathy_message_get_body (EmpathyMessage *message)
589 EmpathyMessagePriv *priv;
591 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
593 priv = GET_PRIV (message);
599 empathy_message_get_timestamp (EmpathyMessage *message)
601 EmpathyMessagePriv *priv;
603 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
605 priv = GET_PRIV (message);
607 return priv->timestamp;
611 empathy_message_get_original_timestamp (EmpathyMessage *message)
613 EmpathyMessagePriv *priv;
615 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
617 priv = GET_PRIV (message);
619 return priv->original_timestamp;
623 empathy_message_is_backlog (EmpathyMessage *message)
625 EmpathyMessagePriv *priv;
627 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
629 priv = GET_PRIV (message);
631 return priv->is_backlog;
634 #define IS_SEPARATOR(ch) (ch == ' ' || ch == ',' || ch == '.' || ch == ':')
636 empathy_message_should_highlight (EmpathyMessage *message)
638 EmpathyContact *contact;
639 const gchar *msg, *to;
640 gchar *cf_msg, *cf_to;
643 TpChannelTextMessageFlags flags;
645 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
649 msg = empathy_message_get_body (message);
654 contact = empathy_message_get_receiver (message);
655 if (!contact || !empathy_contact_is_user (contact)) {
659 to = empathy_contact_get_alias (contact);
664 flags = empathy_message_get_flags (message);
665 if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) {
666 /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
667 * have already been received by the user before (and so are in the logs) */
671 cf_msg = g_utf8_casefold (msg, -1);
672 cf_to = g_utf8_casefold (to, -1);
674 ch = strstr (cf_msg, cf_to);
679 /* Not first in the message */
680 if (!IS_SEPARATOR (*(ch - 1))) {
685 ch = ch + strlen (cf_to);
686 if (ch >= cf_msg + strlen (cf_msg)) {
691 if (IS_SEPARATOR (*ch)) {
703 TpChannelTextMessageType
704 empathy_message_type_from_str (const gchar *type_str)
706 if (strcmp (type_str, "normal") == 0) {
707 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
709 if (strcmp (type_str, "action") == 0) {
710 return TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
712 else if (strcmp (type_str, "notice") == 0) {
713 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE;
715 else if (strcmp (type_str, "auto-reply") == 0) {
716 return TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY;
719 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
723 empathy_message_type_to_str (TpChannelTextMessageType type)
726 case TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION:
728 case TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE:
730 case TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY:
732 case TP_CHANNEL_TEXT_MESSAGE_TYPE_DELIVERY_REPORT:
733 return "delivery-report";
734 case TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
741 empathy_message_is_incoming (EmpathyMessage *message)
743 EmpathyMessagePriv *priv = GET_PRIV (message);
745 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
747 return priv->incoming;
751 empathy_message_equal (EmpathyMessage *message1, EmpathyMessage *message2)
753 EmpathyMessagePriv *priv1;
754 EmpathyMessagePriv *priv2;
756 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message1), FALSE);
757 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message2), FALSE);
759 priv1 = GET_PRIV (message1);
760 priv2 = GET_PRIV (message2);
762 if (priv1->timestamp == priv2->timestamp &&
763 !tp_strdiff (priv1->body, priv2->body)) {
770 TpChannelTextMessageFlags
771 empathy_message_get_flags (EmpathyMessage *self)
773 EmpathyMessagePriv *priv = GET_PRIV (self);
775 g_return_val_if_fail (EMPATHY_IS_MESSAGE (self), 0);
781 empathy_message_new_from_tp_message (TpMessage *tp_msg,
784 EmpathyMessage *message;
786 TpChannelTextMessageFlags flags;
788 gint64 original_timestamp;
789 const GHashTable *part = tp_message_peek (tp_msg, 0);
792 g_return_val_if_fail (TP_IS_MESSAGE (tp_msg), NULL);
794 body = tp_message_to_text (tp_msg, &flags);
796 timestamp = tp_message_get_sent_timestamp (tp_msg);
798 timestamp = tp_message_get_received_timestamp (tp_msg);
800 original_timestamp = tp_asv_get_int64 (part,
801 "original-message-received", NULL);
803 is_backlog = (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) ==
804 TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK;
806 message = g_object_new (EMPATHY_TYPE_MESSAGE,
808 "token", tp_message_get_token (tp_msg),
809 "supersedes", tp_message_get_supersedes (tp_msg),
810 "type", tp_message_get_message_type (tp_msg),
811 "timestamp", timestamp,
812 "original-timestamp", original_timestamp,
814 "is-backlog", is_backlog,
815 "incoming", incoming,
816 "tp-message", tp_msg,