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 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
43 #include "empathy-debug.h"
45 #include "empathy-client-factory.h"
46 #include "empathy-message.h"
47 #include "empathy-utils.h"
48 #include "empathy-enum-types.h"
50 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyMessage)
52 TpMessage *tp_message;
53 TpChannelTextMessageType type;
54 EmpathyContact *sender;
55 EmpathyContact *receiver;
60 gint64 original_timestamp;
64 TpChannelTextMessageFlags flags;
67 static void empathy_message_finalize (GObject *object);
68 static void message_get_property (GObject *object,
72 static void message_set_property (GObject *object,
77 G_DEFINE_TYPE (EmpathyMessage, empathy_message, G_TYPE_OBJECT);
88 PROP_ORIGINAL_TIMESTAMP,
96 empathy_message_class_init (EmpathyMessageClass *class)
98 GObjectClass *object_class;
100 object_class = G_OBJECT_CLASS (class);
101 object_class->finalize = empathy_message_finalize;
102 object_class->get_property = message_get_property;
103 object_class->set_property = message_set_property;
105 g_object_class_install_property (object_class,
107 g_param_spec_uint ("type",
109 "The type of message",
110 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
111 TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY,
112 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
113 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
114 G_PARAM_CONSTRUCT_ONLY));
115 g_object_class_install_property (object_class,
117 g_param_spec_object ("sender",
119 "The sender of the message",
120 EMPATHY_TYPE_CONTACT,
121 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
122 g_object_class_install_property (object_class,
124 g_param_spec_object ("receiver",
126 "The receiver of the message",
127 EMPATHY_TYPE_CONTACT,
128 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
129 g_object_class_install_property (object_class,
131 g_param_spec_string ("token",
135 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
136 g_object_class_install_property (object_class,
138 g_param_spec_string ("supersedes",
140 "The message-token this message supersedes",
142 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
143 g_object_class_install_property (object_class,
145 g_param_spec_string ("body",
147 "The content of the message",
149 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
150 G_PARAM_CONSTRUCT_ONLY));
151 g_object_class_install_property (object_class,
153 g_param_spec_int64 ("timestamp",
156 G_MININT64, G_MAXINT64, 0,
157 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
158 G_PARAM_CONSTRUCT_ONLY));
159 g_object_class_install_property (object_class,
160 PROP_ORIGINAL_TIMESTAMP,
161 g_param_spec_int64 ("original-timestamp",
162 "Original Timestamp",
163 "Timestamp of the original message",
164 G_MININT64, G_MAXINT64, 0,
165 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
166 g_object_class_install_property (object_class,
168 g_param_spec_boolean ("is-backlog",
170 "If the message belongs to history",
172 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
173 G_PARAM_CONSTRUCT_ONLY));
176 g_object_class_install_property (object_class,
178 g_param_spec_boolean ("incoming",
180 "If this is an incoming (as opposed to sent) message",
182 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
183 G_PARAM_CONSTRUCT_ONLY));
185 g_object_class_install_property (object_class,
187 g_param_spec_uint ("flags",
189 "The TpChannelTextMessageFlags of this message",
191 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
192 G_PARAM_CONSTRUCT_ONLY));
194 g_object_class_install_property (object_class,
196 g_param_spec_object ("tp-message",
198 "The TpMessage of this message",
200 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
201 G_PARAM_CONSTRUCT_ONLY));
203 g_type_class_add_private (object_class, sizeof (EmpathyMessagePriv));
208 empathy_message_init (EmpathyMessage *message)
210 EmpathyMessagePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
211 EMPATHY_TYPE_MESSAGE, EmpathyMessagePriv);
213 message->priv = priv;
214 priv->timestamp = empathy_time_get_current ();
218 empathy_message_finalize (GObject *object)
220 EmpathyMessagePriv *priv;
222 priv = GET_PRIV (object);
225 g_object_unref (priv->sender);
227 if (priv->receiver) {
228 g_object_unref (priv->receiver);
231 if (priv->tp_message) {
232 g_object_unref (priv->tp_message);
235 g_free (priv->token);
236 g_free (priv->supersedes);
239 G_OBJECT_CLASS (empathy_message_parent_class)->finalize (object);
243 message_get_property (GObject *object,
248 EmpathyMessagePriv *priv;
250 priv = GET_PRIV (object);
254 g_value_set_uint (value, priv->type);
257 g_value_set_object (value, priv->sender);
260 g_value_set_object (value, priv->receiver);
263 g_value_set_string (value, priv->token);
265 case PROP_SUPERSEDES:
266 g_value_set_string (value, priv->supersedes);
269 g_value_set_string (value, priv->body);
272 g_value_set_int64 (value, priv->timestamp);
274 case PROP_ORIGINAL_TIMESTAMP:
275 g_value_set_int64 (value, priv->original_timestamp);
277 case PROP_IS_BACKLOG:
278 g_value_set_boolean (value, priv->is_backlog);
281 g_value_set_boolean (value, priv->incoming);
284 g_value_set_uint (value, priv->flags);
286 case PROP_TP_MESSAGE:
287 g_value_set_object (value, priv->tp_message);
290 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
296 message_set_property (GObject *object,
301 EmpathyMessagePriv *priv;
303 priv = GET_PRIV (object);
307 priv->type = g_value_get_uint (value);
310 empathy_message_set_sender (EMPATHY_MESSAGE (object),
311 EMPATHY_CONTACT (g_value_get_object (value)));
314 empathy_message_set_receiver (EMPATHY_MESSAGE (object),
315 EMPATHY_CONTACT (g_value_get_object (value)));
318 g_assert (priv->token == NULL); /* construct only */
319 priv->token = g_value_dup_string (value);
321 case PROP_SUPERSEDES:
322 g_assert (priv->supersedes == NULL); /* construct only */
323 priv->supersedes = g_value_dup_string (value);
326 g_assert (priv->body == NULL); /* construct only */
327 priv->body = g_value_dup_string (value);
330 priv->timestamp = g_value_get_int64 (value);
331 if (priv->timestamp <= 0)
332 priv->timestamp = empathy_time_get_current ();
334 case PROP_ORIGINAL_TIMESTAMP:
335 priv->original_timestamp = g_value_get_int64 (value);
337 case PROP_IS_BACKLOG:
338 priv->is_backlog = g_value_get_boolean (value);
341 priv->incoming = g_value_get_boolean (value);
344 priv->flags = g_value_get_uint (value);
346 case PROP_TP_MESSAGE:
347 priv->tp_message = g_value_dup_object (value);
350 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
356 empathy_message_from_tpl_log_event (TplEvent *logevent)
358 EmpathyMessage *retval = NULL;
359 EmpathyClientFactory *factory;
360 TpAccount *account = NULL;
361 TplEntity *receiver = NULL;
362 TplEntity *sender = NULL;
364 const gchar *token = NULL, *supersedes = NULL;
365 EmpathyContact *contact;
366 TpChannelTextMessageType type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
367 gint64 timestamp, original_timestamp = 0;
369 g_return_val_if_fail (TPL_IS_EVENT (logevent), NULL);
371 factory = empathy_client_factory_dup ();
372 /* FIXME Currently Empathy shows in the log viewer only valid accounts, so it
373 * won't be selected any non-existing (ie removed) account.
374 * When #610455 will be fixed, calling tp_account_manager_ensure_account ()
375 * might add a not existing account to the AM. tp_account_new () probably
376 * will be the best way to handle it.
377 * Note: When creating an EmpathyContact from a TplEntity instance, the
378 * TpAccount is passed *only* to let EmpathyContact be able to retrieve the
379 * avatar (contact_get_avatar_filename () need a TpAccount).
380 * If the way EmpathyContact stores the avatar is changes, it might not be
381 * needed anymore any TpAccount passing and the following call will be
383 account = tp_simple_client_factory_ensure_account (
384 TP_SIMPLE_CLIENT_FACTORY (factory),
385 tpl_event_get_account_path (logevent), NULL, NULL);
386 g_object_unref (factory);
388 if (TPL_IS_TEXT_EVENT (logevent)) {
389 TplTextEvent *textevent = TPL_TEXT_EVENT (logevent);
391 supersedes = tpl_text_event_get_supersedes_token (textevent);
393 /* tp-logger is kind of messy in that instead of having
394 * timestamp and original-timestamp like Telepathy it has
395 * timestamp (which is the original) and edited-timestamp,
396 * (which is when the message was edited) */
397 if (tp_str_empty (supersedes)) {
398 /* not an edited message */
399 timestamp = tpl_event_get_timestamp (logevent);
401 /* this is an edited event */
402 original_timestamp = tpl_event_get_timestamp (logevent);
403 timestamp = tpl_text_event_get_edit_timestamp (textevent);
406 body = g_strdup (tpl_text_event_get_message (textevent));
408 type = tpl_text_event_get_message_type (TPL_TEXT_EVENT (logevent));
409 token = tpl_text_event_get_message_token (textevent);
411 #ifdef HAVE_CALL_LOGS
412 else if (TPL_IS_CALL_EVENT (logevent)) {
413 TplCallEvent *call = TPL_CALL_EVENT (logevent);
415 timestamp = tpl_event_get_timestamp (logevent);
417 if (tpl_call_event_get_end_reason (call) == TPL_CALL_END_REASON_NO_ANSWER)
418 body = g_strdup_printf (_("Missed call from %s"),
419 tpl_entity_get_alias (tpl_event_get_sender (logevent)));
420 else if (tpl_entity_get_entity_type (tpl_event_get_sender (logevent)) == TPL_ENTITY_SELF)
421 /* Translators: this is an outgoing call, e.g. 'Called Alice' */
422 body = g_strdup_printf (_("Called %s"),
423 tpl_entity_get_alias (tpl_event_get_receiver (logevent)));
425 body = g_strdup_printf (_("Call from %s"),
426 tpl_entity_get_alias (tpl_event_get_sender (logevent)));
430 /* Unknown event type */
434 receiver = tpl_event_get_receiver (logevent);
435 sender = tpl_event_get_sender (logevent);
437 retval = g_object_new (EMPATHY_TYPE_MESSAGE,
440 "supersedes", supersedes,
443 "timestamp", timestamp,
444 "original-timestamp", original_timestamp,
447 if (receiver != NULL) {
448 contact = empathy_contact_from_tpl_contact (account, receiver);
449 empathy_message_set_receiver (retval, contact);
450 g_object_unref (contact);
453 if (sender != NULL) {
454 contact = empathy_contact_from_tpl_contact (account, sender);
455 empathy_message_set_sender (retval, contact);
456 g_object_unref (contact);
465 empathy_message_get_tp_message (EmpathyMessage *message)
467 EmpathyMessagePriv *priv;
469 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
471 priv = GET_PRIV (message);
473 return priv->tp_message;
476 TpChannelTextMessageType
477 empathy_message_get_tptype (EmpathyMessage *message)
479 EmpathyMessagePriv *priv;
481 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message),
482 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL);
484 priv = GET_PRIV (message);
490 empathy_message_get_sender (EmpathyMessage *message)
492 EmpathyMessagePriv *priv;
494 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
496 priv = GET_PRIV (message);
502 empathy_message_set_sender (EmpathyMessage *message, EmpathyContact *contact)
504 EmpathyMessagePriv *priv;
505 EmpathyContact *old_sender;
507 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
508 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
510 priv = GET_PRIV (message);
512 old_sender = priv->sender;
513 priv->sender = g_object_ref (contact);
516 g_object_unref (old_sender);
519 g_object_notify (G_OBJECT (message), "sender");
523 empathy_message_get_receiver (EmpathyMessage *message)
525 EmpathyMessagePriv *priv;
527 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
529 priv = GET_PRIV (message);
531 return priv->receiver;
535 empathy_message_set_receiver (EmpathyMessage *message, EmpathyContact *contact)
537 EmpathyMessagePriv *priv;
538 EmpathyContact *old_receiver;
540 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
541 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
543 priv = GET_PRIV (message);
545 old_receiver = priv->receiver;
546 priv->receiver = g_object_ref (contact);
549 g_object_unref (old_receiver);
552 g_object_notify (G_OBJECT (message), "receiver");
556 empathy_message_get_token (EmpathyMessage *message)
558 EmpathyMessagePriv *priv;
560 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
562 priv = GET_PRIV (message);
568 empathy_message_get_supersedes (EmpathyMessage *message)
570 EmpathyMessagePriv *priv;
572 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
574 priv = GET_PRIV (message);
576 return priv->supersedes;
580 empathy_message_is_edit (EmpathyMessage *message)
582 EmpathyMessagePriv *priv;
584 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
586 priv = GET_PRIV (message);
588 return !tp_str_empty (priv->supersedes);
592 empathy_message_get_body (EmpathyMessage *message)
594 EmpathyMessagePriv *priv;
596 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
598 priv = GET_PRIV (message);
604 empathy_message_get_timestamp (EmpathyMessage *message)
606 EmpathyMessagePriv *priv;
608 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
610 priv = GET_PRIV (message);
612 return priv->timestamp;
616 empathy_message_get_original_timestamp (EmpathyMessage *message)
618 EmpathyMessagePriv *priv;
620 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
622 priv = GET_PRIV (message);
624 return priv->original_timestamp;
628 empathy_message_is_backlog (EmpathyMessage *message)
630 EmpathyMessagePriv *priv;
632 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
634 priv = GET_PRIV (message);
636 return priv->is_backlog;
640 get_highlight_regex_for (const gchar *name)
643 gchar *name_esc, *pattern;
644 GError *error = NULL;
646 name_esc = g_regex_escape_string (name, -1);
647 pattern = g_strdup_printf ("\\b%s\\b", name_esc);
648 regex = g_regex_new (pattern, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0,
652 DEBUG ("couldn't compile regex /%s/: %s", pattern,
655 g_error_free (error);
665 empathy_message_should_highlight (EmpathyMessage *message)
667 EmpathyContact *contact;
668 const gchar *msg, *to;
669 gboolean ret_val = FALSE;
670 TpChannelTextMessageFlags flags;
673 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
675 msg = empathy_message_get_body (message);
680 contact = empathy_message_get_receiver (message);
681 if (!contact || !empathy_contact_is_user (contact)) {
685 to = empathy_contact_get_alias (contact);
690 flags = empathy_message_get_flags (message);
691 if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) {
692 /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
693 * have already been received by the user before (and so are in the logs) */
697 regex = get_highlight_regex_for (to);
699 ret_val = g_regex_match (regex, msg, 0, NULL);
700 g_regex_unref (regex);
706 TpChannelTextMessageType
707 empathy_message_type_from_str (const gchar *type_str)
709 if (strcmp (type_str, "normal") == 0) {
710 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
712 if (strcmp (type_str, "action") == 0) {
713 return TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
715 else if (strcmp (type_str, "notice") == 0) {
716 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE;
718 else if (strcmp (type_str, "auto-reply") == 0) {
719 return TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY;
722 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
726 empathy_message_type_to_str (TpChannelTextMessageType type)
729 case TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION:
731 case TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE:
733 case TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY:
735 case TP_CHANNEL_TEXT_MESSAGE_TYPE_DELIVERY_REPORT:
736 return "delivery-report";
737 case TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
744 empathy_message_is_incoming (EmpathyMessage *message)
746 EmpathyMessagePriv *priv = GET_PRIV (message);
748 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
750 return priv->incoming;
754 empathy_message_equal (EmpathyMessage *message1, EmpathyMessage *message2)
756 EmpathyMessagePriv *priv1;
757 EmpathyMessagePriv *priv2;
759 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message1), FALSE);
760 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message2), FALSE);
762 priv1 = GET_PRIV (message1);
763 priv2 = GET_PRIV (message2);
765 if (priv1->timestamp == priv2->timestamp &&
766 !tp_strdiff (priv1->body, priv2->body)) {
773 TpChannelTextMessageFlags
774 empathy_message_get_flags (EmpathyMessage *self)
776 EmpathyMessagePriv *priv = GET_PRIV (self);
778 g_return_val_if_fail (EMPATHY_IS_MESSAGE (self), 0);
784 empathy_message_new_from_tp_message (TpMessage *tp_msg,
787 EmpathyMessage *message;
789 TpChannelTextMessageFlags flags;
791 gint64 original_timestamp;
792 const GHashTable *part = tp_message_peek (tp_msg, 0);
795 g_return_val_if_fail (TP_IS_MESSAGE (tp_msg), NULL);
797 body = tp_message_to_text (tp_msg, &flags);
799 timestamp = tp_message_get_sent_timestamp (tp_msg);
801 timestamp = tp_message_get_received_timestamp (tp_msg);
803 original_timestamp = tp_asv_get_int64 (part,
804 "original-message-received", NULL);
806 is_backlog = (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) ==
807 TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK;
809 message = g_object_new (EMPATHY_TYPE_MESSAGE,
811 "token", tp_message_get_token (tp_msg),
812 "supersedes", tp_message_get_supersedes (tp_msg),
813 "type", tp_message_get_message_type (tp_msg),
814 "timestamp", timestamp,
815 "original-timestamp", original_timestamp,
817 "is-backlog", is_backlog,
818 "incoming", incoming,
819 "tp-message", tp_msg,