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 "empathy-client-factory.h"
36 #include "empathy-message.h"
37 #include "empathy-utils.h"
38 #include "empathy-enum-types.h"
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyMessage)
42 TpMessage *tp_message;
43 TpChannelTextMessageType type;
44 EmpathyContact *sender;
45 EmpathyContact *receiver;
50 gint64 original_timestamp;
54 TpChannelTextMessageFlags flags;
57 static void empathy_message_finalize (GObject *object);
58 static void message_get_property (GObject *object,
62 static void message_set_property (GObject *object,
67 G_DEFINE_TYPE (EmpathyMessage, empathy_message, G_TYPE_OBJECT);
78 PROP_ORIGINAL_TIMESTAMP,
86 empathy_message_class_init (EmpathyMessageClass *class)
88 GObjectClass *object_class;
90 object_class = G_OBJECT_CLASS (class);
91 object_class->finalize = empathy_message_finalize;
92 object_class->get_property = message_get_property;
93 object_class->set_property = message_set_property;
95 g_object_class_install_property (object_class,
97 g_param_spec_uint ("type",
99 "The type of message",
100 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
101 TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY,
102 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
103 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
104 G_PARAM_CONSTRUCT_ONLY));
105 g_object_class_install_property (object_class,
107 g_param_spec_object ("sender",
109 "The sender of the message",
110 EMPATHY_TYPE_CONTACT,
111 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
112 g_object_class_install_property (object_class,
114 g_param_spec_object ("receiver",
116 "The receiver of the message",
117 EMPATHY_TYPE_CONTACT,
118 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
119 g_object_class_install_property (object_class,
121 g_param_spec_string ("token",
125 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
126 g_object_class_install_property (object_class,
128 g_param_spec_string ("supersedes",
130 "The message-token this message supersedes",
132 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
133 g_object_class_install_property (object_class,
135 g_param_spec_string ("body",
137 "The content of the message",
139 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
140 G_PARAM_CONSTRUCT_ONLY));
141 g_object_class_install_property (object_class,
143 g_param_spec_int64 ("timestamp",
146 G_MININT64, G_MAXINT64, 0,
147 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
148 G_PARAM_CONSTRUCT_ONLY));
149 g_object_class_install_property (object_class,
150 PROP_ORIGINAL_TIMESTAMP,
151 g_param_spec_int64 ("original-timestamp",
152 "Original Timestamp",
153 "Timestamp of the original message",
154 G_MININT64, G_MAXINT64, 0,
155 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
156 g_object_class_install_property (object_class,
158 g_param_spec_boolean ("is-backlog",
160 "If the message belongs to history",
162 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
163 G_PARAM_CONSTRUCT_ONLY));
166 g_object_class_install_property (object_class,
168 g_param_spec_boolean ("incoming",
170 "If this is an incoming (as opposed to sent) message",
172 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
173 G_PARAM_CONSTRUCT_ONLY));
175 g_object_class_install_property (object_class,
177 g_param_spec_uint ("flags",
179 "The TpChannelTextMessageFlags of this message",
181 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
182 G_PARAM_CONSTRUCT_ONLY));
184 g_object_class_install_property (object_class,
186 g_param_spec_object ("tp-message",
188 "The TpMessage of this message",
190 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
191 G_PARAM_CONSTRUCT_ONLY));
193 g_type_class_add_private (object_class, sizeof (EmpathyMessagePriv));
198 empathy_message_init (EmpathyMessage *message)
200 EmpathyMessagePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
201 EMPATHY_TYPE_MESSAGE, EmpathyMessagePriv);
203 message->priv = priv;
204 priv->timestamp = empathy_time_get_current ();
208 empathy_message_finalize (GObject *object)
210 EmpathyMessagePriv *priv;
212 priv = GET_PRIV (object);
215 g_object_unref (priv->sender);
217 if (priv->receiver) {
218 g_object_unref (priv->receiver);
221 if (priv->tp_message) {
222 g_object_unref (priv->tp_message);
225 g_free (priv->token);
226 g_free (priv->supersedes);
229 G_OBJECT_CLASS (empathy_message_parent_class)->finalize (object);
233 message_get_property (GObject *object,
238 EmpathyMessagePriv *priv;
240 priv = GET_PRIV (object);
244 g_value_set_uint (value, priv->type);
247 g_value_set_object (value, priv->sender);
250 g_value_set_object (value, priv->receiver);
253 g_value_set_string (value, priv->token);
255 case PROP_SUPERSEDES:
256 g_value_set_string (value, priv->supersedes);
259 g_value_set_string (value, priv->body);
262 g_value_set_int64 (value, priv->timestamp);
264 case PROP_ORIGINAL_TIMESTAMP:
265 g_value_set_int64 (value, priv->original_timestamp);
267 case PROP_IS_BACKLOG:
268 g_value_set_boolean (value, priv->is_backlog);
271 g_value_set_boolean (value, priv->incoming);
274 g_value_set_uint (value, priv->flags);
276 case PROP_TP_MESSAGE:
277 g_value_set_object (value, priv->tp_message);
280 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
286 message_set_property (GObject *object,
291 EmpathyMessagePriv *priv;
293 priv = GET_PRIV (object);
297 priv->type = g_value_get_uint (value);
300 empathy_message_set_sender (EMPATHY_MESSAGE (object),
301 EMPATHY_CONTACT (g_value_get_object (value)));
304 empathy_message_set_receiver (EMPATHY_MESSAGE (object),
305 EMPATHY_CONTACT (g_value_get_object (value)));
308 g_assert (priv->token == NULL); /* construct only */
309 priv->token = g_value_dup_string (value);
311 case PROP_SUPERSEDES:
312 g_assert (priv->supersedes == NULL); /* construct only */
313 priv->supersedes = g_value_dup_string (value);
316 g_assert (priv->body == NULL); /* construct only */
317 priv->body = g_value_dup_string (value);
320 priv->timestamp = g_value_get_int64 (value);
321 if (priv->timestamp <= 0)
322 priv->timestamp = empathy_time_get_current ();
324 case PROP_ORIGINAL_TIMESTAMP:
325 priv->original_timestamp = g_value_get_int64 (value);
327 case PROP_IS_BACKLOG:
328 priv->is_backlog = g_value_get_boolean (value);
331 priv->incoming = g_value_get_boolean (value);
334 priv->flags = g_value_get_uint (value);
336 case PROP_TP_MESSAGE:
337 priv->tp_message = g_value_dup_object (value);
340 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
346 empathy_message_from_tpl_log_event (TplEvent *logevent)
348 EmpathyMessage *retval = NULL;
349 EmpathyClientFactory *factory;
350 TpAccount *account = NULL;
351 TplEntity *receiver = NULL;
352 TplEntity *sender = NULL;
354 const gchar *token = NULL, *supersedes = NULL;
355 EmpathyContact *contact;
356 TpChannelTextMessageType type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
357 gint64 timestamp, original_timestamp = 0;
359 g_return_val_if_fail (TPL_IS_EVENT (logevent), NULL);
361 factory = empathy_client_factory_dup ();
362 /* FIXME Currently Empathy shows in the log viewer only valid accounts, so it
363 * won't be selected any non-existing (ie removed) account.
364 * When #610455 will be fixed, calling tp_account_manager_ensure_account ()
365 * might add a not existing account to the AM. tp_account_new () probably
366 * will be the best way to handle it.
367 * Note: When creating an EmpathyContact from a TplEntity instance, the
368 * TpAccount is passed *only* to let EmpathyContact be able to retrieve the
369 * avatar (contact_get_avatar_filename () need a TpAccount).
370 * If the way EmpathyContact stores the avatar is changes, it might not be
371 * needed anymore any TpAccount passing and the following call will be
373 account = tp_simple_client_factory_ensure_account (
374 TP_SIMPLE_CLIENT_FACTORY (factory),
375 tpl_event_get_account_path (logevent), NULL, NULL);
376 g_object_unref (factory);
378 if (TPL_IS_TEXT_EVENT (logevent)) {
379 TplTextEvent *textevent = TPL_TEXT_EVENT (logevent);
381 supersedes = tpl_text_event_get_supersedes_token (textevent);
383 /* tp-logger is kind of messy in that instead of having
384 * timestamp and original-timestamp like Telepathy it has
385 * timestamp (which is the original) and edited-timestamp,
386 * (which is when the message was edited) */
387 if (tp_str_empty (supersedes)) {
388 /* not an edited message */
389 timestamp = tpl_event_get_timestamp (logevent);
391 /* this is an edited event */
392 original_timestamp = tpl_event_get_timestamp (logevent);
393 timestamp = tpl_text_event_get_edit_timestamp (textevent);
396 body = g_strdup (tpl_text_event_get_message (textevent));
398 type = tpl_text_event_get_message_type (TPL_TEXT_EVENT (logevent));
399 token = tpl_text_event_get_message_token (textevent);
401 else if (TPL_IS_CALL_EVENT (logevent)) {
402 TplCallEvent *call = TPL_CALL_EVENT (logevent);
404 timestamp = tpl_event_get_timestamp (logevent);
406 if (tpl_call_event_get_end_reason (call) == TP_CALL_STATE_CHANGE_REASON_NO_ANSWER)
407 body = g_strdup_printf (_("Missed call from %s"),
408 tpl_entity_get_alias (tpl_event_get_sender (logevent)));
409 else if (tpl_entity_get_entity_type (tpl_event_get_sender (logevent)) == TPL_ENTITY_SELF)
410 /* Translators: this is an outgoing call, e.g. 'Called Alice' */
411 body = g_strdup_printf (_("Called %s"),
412 tpl_entity_get_alias (tpl_event_get_receiver (logevent)));
414 body = g_strdup_printf (_("Call from %s"),
415 tpl_entity_get_alias (tpl_event_get_sender (logevent)));
418 /* Unknown event type */
422 receiver = tpl_event_get_receiver (logevent);
423 sender = tpl_event_get_sender (logevent);
425 retval = g_object_new (EMPATHY_TYPE_MESSAGE,
428 "supersedes", supersedes,
431 "timestamp", timestamp,
432 "original-timestamp", original_timestamp,
435 if (receiver != NULL) {
436 contact = empathy_contact_from_tpl_contact (account, receiver);
437 empathy_message_set_receiver (retval, contact);
438 g_object_unref (contact);
441 if (sender != NULL) {
442 contact = empathy_contact_from_tpl_contact (account, sender);
443 empathy_message_set_sender (retval, contact);
444 g_object_unref (contact);
453 empathy_message_get_tp_message (EmpathyMessage *message)
455 EmpathyMessagePriv *priv;
457 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
459 priv = GET_PRIV (message);
461 return priv->tp_message;
464 TpChannelTextMessageType
465 empathy_message_get_tptype (EmpathyMessage *message)
467 EmpathyMessagePriv *priv;
469 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message),
470 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL);
472 priv = GET_PRIV (message);
478 empathy_message_get_sender (EmpathyMessage *message)
480 EmpathyMessagePriv *priv;
482 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
484 priv = GET_PRIV (message);
490 empathy_message_set_sender (EmpathyMessage *message, EmpathyContact *contact)
492 EmpathyMessagePriv *priv;
493 EmpathyContact *old_sender;
495 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
496 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
498 priv = GET_PRIV (message);
500 old_sender = priv->sender;
501 priv->sender = g_object_ref (contact);
504 g_object_unref (old_sender);
507 g_object_notify (G_OBJECT (message), "sender");
511 empathy_message_get_receiver (EmpathyMessage *message)
513 EmpathyMessagePriv *priv;
515 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
517 priv = GET_PRIV (message);
519 return priv->receiver;
523 empathy_message_set_receiver (EmpathyMessage *message, EmpathyContact *contact)
525 EmpathyMessagePriv *priv;
526 EmpathyContact *old_receiver;
528 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
529 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
531 priv = GET_PRIV (message);
533 old_receiver = priv->receiver;
534 priv->receiver = g_object_ref (contact);
537 g_object_unref (old_receiver);
540 g_object_notify (G_OBJECT (message), "receiver");
544 empathy_message_get_token (EmpathyMessage *message)
546 EmpathyMessagePriv *priv;
548 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
550 priv = GET_PRIV (message);
556 empathy_message_get_supersedes (EmpathyMessage *message)
558 EmpathyMessagePriv *priv;
560 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
562 priv = GET_PRIV (message);
564 return priv->supersedes;
568 empathy_message_is_edit (EmpathyMessage *message)
570 EmpathyMessagePriv *priv;
572 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
574 priv = GET_PRIV (message);
576 return !tp_str_empty (priv->supersedes);
580 empathy_message_get_body (EmpathyMessage *message)
582 EmpathyMessagePriv *priv;
584 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
586 priv = GET_PRIV (message);
592 empathy_message_get_timestamp (EmpathyMessage *message)
594 EmpathyMessagePriv *priv;
596 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
598 priv = GET_PRIV (message);
600 return priv->timestamp;
604 empathy_message_get_original_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->original_timestamp;
616 empathy_message_is_backlog (EmpathyMessage *message)
618 EmpathyMessagePriv *priv;
620 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
622 priv = GET_PRIV (message);
624 return priv->is_backlog;
627 TpChannelTextMessageType
628 empathy_message_type_from_str (const gchar *type_str)
630 if (strcmp (type_str, "normal") == 0) {
631 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
633 if (strcmp (type_str, "action") == 0) {
634 return TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
636 else if (strcmp (type_str, "notice") == 0) {
637 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE;
639 else if (strcmp (type_str, "auto-reply") == 0) {
640 return TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY;
643 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
647 empathy_message_type_to_str (TpChannelTextMessageType type)
650 case TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION:
652 case TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE:
654 case TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY:
656 case TP_CHANNEL_TEXT_MESSAGE_TYPE_DELIVERY_REPORT:
657 return "delivery-report";
658 case TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
665 empathy_message_is_incoming (EmpathyMessage *message)
667 EmpathyMessagePriv *priv = GET_PRIV (message);
669 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
671 return priv->incoming;
675 empathy_message_equal (EmpathyMessage *message1, EmpathyMessage *message2)
677 EmpathyMessagePriv *priv1;
678 EmpathyMessagePriv *priv2;
680 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message1), FALSE);
681 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message2), FALSE);
683 priv1 = GET_PRIV (message1);
684 priv2 = GET_PRIV (message2);
686 if (priv1->timestamp == priv2->timestamp &&
687 !tp_strdiff (priv1->body, priv2->body)) {
694 TpChannelTextMessageFlags
695 empathy_message_get_flags (EmpathyMessage *self)
697 EmpathyMessagePriv *priv = GET_PRIV (self);
699 g_return_val_if_fail (EMPATHY_IS_MESSAGE (self), 0);
705 empathy_message_new_from_tp_message (TpMessage *tp_msg,
708 EmpathyMessage *message;
710 TpChannelTextMessageFlags flags;
712 gint64 original_timestamp;
713 const GHashTable *part = tp_message_peek (tp_msg, 0);
716 g_return_val_if_fail (TP_IS_MESSAGE (tp_msg), NULL);
718 body = tp_message_to_text (tp_msg, &flags);
720 timestamp = tp_message_get_sent_timestamp (tp_msg);
722 timestamp = tp_message_get_received_timestamp (tp_msg);
724 original_timestamp = tp_asv_get_int64 (part,
725 "original-message-received", NULL);
727 is_backlog = (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) ==
728 TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK;
730 message = g_object_new (EMPATHY_TYPE_MESSAGE,
732 "token", tp_message_get_token (tp_msg),
733 "supersedes", tp_message_get_supersedes (tp_msg),
734 "type", tp_message_get_message_type (tp_msg),
735 "timestamp", timestamp,
736 "original-timestamp", original_timestamp,
738 "is-backlog", is_backlog,
739 "incoming", incoming,
740 "tp-message", tp_msg,