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>
38 #include <telepathy-logger/call-event.h>
40 #include "empathy-client-factory.h"
41 #include "empathy-message.h"
42 #include "empathy-utils.h"
43 #include "empathy-enum-types.h"
45 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyMessage)
47 TpMessage *tp_message;
48 TpChannelTextMessageType type;
49 EmpathyContact *sender;
50 EmpathyContact *receiver;
55 gint64 original_timestamp;
59 TpChannelTextMessageFlags flags;
62 static void empathy_message_finalize (GObject *object);
63 static void message_get_property (GObject *object,
67 static void message_set_property (GObject *object,
72 G_DEFINE_TYPE (EmpathyMessage, empathy_message, G_TYPE_OBJECT);
83 PROP_ORIGINAL_TIMESTAMP,
91 empathy_message_class_init (EmpathyMessageClass *class)
93 GObjectClass *object_class;
95 object_class = G_OBJECT_CLASS (class);
96 object_class->finalize = empathy_message_finalize;
97 object_class->get_property = message_get_property;
98 object_class->set_property = message_set_property;
100 g_object_class_install_property (object_class,
102 g_param_spec_uint ("type",
104 "The type of message",
105 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
106 TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY,
107 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
108 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
109 G_PARAM_CONSTRUCT_ONLY));
110 g_object_class_install_property (object_class,
112 g_param_spec_object ("sender",
114 "The sender of the message",
115 EMPATHY_TYPE_CONTACT,
116 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
117 g_object_class_install_property (object_class,
119 g_param_spec_object ("receiver",
121 "The receiver of the message",
122 EMPATHY_TYPE_CONTACT,
123 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
124 g_object_class_install_property (object_class,
126 g_param_spec_string ("token",
130 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
131 g_object_class_install_property (object_class,
133 g_param_spec_string ("supersedes",
135 "The message-token this message supersedes",
137 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
138 g_object_class_install_property (object_class,
140 g_param_spec_string ("body",
142 "The content of the message",
144 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
145 G_PARAM_CONSTRUCT_ONLY));
146 g_object_class_install_property (object_class,
148 g_param_spec_int64 ("timestamp",
151 G_MININT64, G_MAXINT64, 0,
152 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
153 G_PARAM_CONSTRUCT_ONLY));
154 g_object_class_install_property (object_class,
155 PROP_ORIGINAL_TIMESTAMP,
156 g_param_spec_int64 ("original-timestamp",
157 "Original Timestamp",
158 "Timestamp of the original message",
159 G_MININT64, G_MAXINT64, 0,
160 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
161 g_object_class_install_property (object_class,
163 g_param_spec_boolean ("is-backlog",
165 "If the message belongs to history",
167 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
168 G_PARAM_CONSTRUCT_ONLY));
171 g_object_class_install_property (object_class,
173 g_param_spec_boolean ("incoming",
175 "If this is an incoming (as opposed to sent) message",
177 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
178 G_PARAM_CONSTRUCT_ONLY));
180 g_object_class_install_property (object_class,
182 g_param_spec_uint ("flags",
184 "The TpChannelTextMessageFlags of this message",
186 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
187 G_PARAM_CONSTRUCT_ONLY));
189 g_object_class_install_property (object_class,
191 g_param_spec_object ("tp-message",
193 "The TpMessage of this message",
195 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
196 G_PARAM_CONSTRUCT_ONLY));
198 g_type_class_add_private (object_class, sizeof (EmpathyMessagePriv));
203 empathy_message_init (EmpathyMessage *message)
205 EmpathyMessagePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
206 EMPATHY_TYPE_MESSAGE, EmpathyMessagePriv);
208 message->priv = priv;
209 priv->timestamp = empathy_time_get_current ();
213 empathy_message_finalize (GObject *object)
215 EmpathyMessagePriv *priv;
217 priv = GET_PRIV (object);
220 g_object_unref (priv->sender);
222 if (priv->receiver) {
223 g_object_unref (priv->receiver);
226 if (priv->tp_message) {
227 g_object_unref (priv->tp_message);
230 g_free (priv->token);
231 g_free (priv->supersedes);
234 G_OBJECT_CLASS (empathy_message_parent_class)->finalize (object);
238 message_get_property (GObject *object,
243 EmpathyMessagePriv *priv;
245 priv = GET_PRIV (object);
249 g_value_set_uint (value, priv->type);
252 g_value_set_object (value, priv->sender);
255 g_value_set_object (value, priv->receiver);
258 g_value_set_string (value, priv->token);
260 case PROP_SUPERSEDES:
261 g_value_set_string (value, priv->supersedes);
264 g_value_set_string (value, priv->body);
267 g_value_set_int64 (value, priv->timestamp);
269 case PROP_ORIGINAL_TIMESTAMP:
270 g_value_set_int64 (value, priv->original_timestamp);
272 case PROP_IS_BACKLOG:
273 g_value_set_boolean (value, priv->is_backlog);
276 g_value_set_boolean (value, priv->incoming);
279 g_value_set_uint (value, priv->flags);
281 case PROP_TP_MESSAGE:
282 g_value_set_object (value, priv->tp_message);
285 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
291 message_set_property (GObject *object,
296 EmpathyMessagePriv *priv;
298 priv = GET_PRIV (object);
302 priv->type = g_value_get_uint (value);
305 empathy_message_set_sender (EMPATHY_MESSAGE (object),
306 EMPATHY_CONTACT (g_value_get_object (value)));
309 empathy_message_set_receiver (EMPATHY_MESSAGE (object),
310 EMPATHY_CONTACT (g_value_get_object (value)));
313 g_assert (priv->token == NULL); /* construct only */
314 priv->token = g_value_dup_string (value);
316 case PROP_SUPERSEDES:
317 g_assert (priv->supersedes == NULL); /* construct only */
318 priv->supersedes = g_value_dup_string (value);
321 g_assert (priv->body == NULL); /* construct only */
322 priv->body = g_value_dup_string (value);
325 priv->timestamp = g_value_get_int64 (value);
326 if (priv->timestamp <= 0)
327 priv->timestamp = empathy_time_get_current ();
329 case PROP_ORIGINAL_TIMESTAMP:
330 priv->original_timestamp = g_value_get_int64 (value);
332 case PROP_IS_BACKLOG:
333 priv->is_backlog = g_value_get_boolean (value);
336 priv->incoming = g_value_get_boolean (value);
339 priv->flags = g_value_get_uint (value);
341 case PROP_TP_MESSAGE:
342 priv->tp_message = g_value_dup_object (value);
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
351 empathy_message_from_tpl_log_event (TplEvent *logevent)
353 EmpathyMessage *retval = NULL;
354 EmpathyClientFactory *factory;
355 TpAccount *account = NULL;
356 TplEntity *receiver = NULL;
357 TplEntity *sender = NULL;
359 const gchar *token = NULL, *supersedes = NULL;
360 EmpathyContact *contact;
361 TpChannelTextMessageType type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
362 gint64 timestamp, original_timestamp = 0;
364 g_return_val_if_fail (TPL_IS_EVENT (logevent), NULL);
366 factory = empathy_client_factory_dup ();
367 /* FIXME Currently Empathy shows in the log viewer only valid accounts, so it
368 * won't be selected any non-existing (ie removed) account.
369 * When #610455 will be fixed, calling tp_account_manager_ensure_account ()
370 * might add a not existing account to the AM. tp_account_new () probably
371 * will be the best way to handle it.
372 * Note: When creating an EmpathyContact from a TplEntity instance, the
373 * TpAccount is passed *only* to let EmpathyContact be able to retrieve the
374 * avatar (contact_get_avatar_filename () need a TpAccount).
375 * If the way EmpathyContact stores the avatar is changes, it might not be
376 * needed anymore any TpAccount passing and the following call will be
378 account = tp_simple_client_factory_ensure_account (
379 TP_SIMPLE_CLIENT_FACTORY (factory),
380 tpl_event_get_account_path (logevent), NULL, NULL);
381 g_object_unref (factory);
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 else if (TPL_IS_CALL_EVENT (logevent)) {
407 TplCallEvent *call = TPL_CALL_EVENT (logevent);
409 timestamp = tpl_event_get_timestamp (logevent);
411 if (tpl_call_event_get_end_reason (call) == TP_CALL_STATE_CHANGE_REASON_NO_ANSWER)
412 body = g_strdup_printf (_("Missed call from %s"),
413 tpl_entity_get_alias (tpl_event_get_sender (logevent)));
414 else if (tpl_entity_get_entity_type (tpl_event_get_sender (logevent)) == TPL_ENTITY_SELF)
415 /* Translators: this is an outgoing call, e.g. 'Called Alice' */
416 body = g_strdup_printf (_("Called %s"),
417 tpl_entity_get_alias (tpl_event_get_receiver (logevent)));
419 body = g_strdup_printf (_("Call from %s"),
420 tpl_entity_get_alias (tpl_event_get_sender (logevent)));
423 /* Unknown event type */
427 receiver = tpl_event_get_receiver (logevent);
428 sender = tpl_event_get_sender (logevent);
430 retval = g_object_new (EMPATHY_TYPE_MESSAGE,
433 "supersedes", supersedes,
436 "timestamp", timestamp,
437 "original-timestamp", original_timestamp,
440 if (receiver != NULL) {
441 contact = empathy_contact_from_tpl_contact (account, receiver);
442 empathy_message_set_receiver (retval, contact);
443 g_object_unref (contact);
446 if (sender != NULL) {
447 contact = empathy_contact_from_tpl_contact (account, sender);
448 empathy_message_set_sender (retval, contact);
449 g_object_unref (contact);
458 empathy_message_get_tp_message (EmpathyMessage *message)
460 EmpathyMessagePriv *priv;
462 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
464 priv = GET_PRIV (message);
466 return priv->tp_message;
469 TpChannelTextMessageType
470 empathy_message_get_tptype (EmpathyMessage *message)
472 EmpathyMessagePriv *priv;
474 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message),
475 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL);
477 priv = GET_PRIV (message);
483 empathy_message_get_sender (EmpathyMessage *message)
485 EmpathyMessagePriv *priv;
487 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
489 priv = GET_PRIV (message);
495 empathy_message_set_sender (EmpathyMessage *message, EmpathyContact *contact)
497 EmpathyMessagePriv *priv;
498 EmpathyContact *old_sender;
500 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
501 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
503 priv = GET_PRIV (message);
505 old_sender = priv->sender;
506 priv->sender = g_object_ref (contact);
509 g_object_unref (old_sender);
512 g_object_notify (G_OBJECT (message), "sender");
516 empathy_message_get_receiver (EmpathyMessage *message)
518 EmpathyMessagePriv *priv;
520 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
522 priv = GET_PRIV (message);
524 return priv->receiver;
528 empathy_message_set_receiver (EmpathyMessage *message, EmpathyContact *contact)
530 EmpathyMessagePriv *priv;
531 EmpathyContact *old_receiver;
533 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
534 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
536 priv = GET_PRIV (message);
538 old_receiver = priv->receiver;
539 priv->receiver = g_object_ref (contact);
542 g_object_unref (old_receiver);
545 g_object_notify (G_OBJECT (message), "receiver");
549 empathy_message_get_token (EmpathyMessage *message)
551 EmpathyMessagePriv *priv;
553 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
555 priv = GET_PRIV (message);
561 empathy_message_get_supersedes (EmpathyMessage *message)
563 EmpathyMessagePriv *priv;
565 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
567 priv = GET_PRIV (message);
569 return priv->supersedes;
573 empathy_message_is_edit (EmpathyMessage *message)
575 EmpathyMessagePriv *priv;
577 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
579 priv = GET_PRIV (message);
581 return !tp_str_empty (priv->supersedes);
585 empathy_message_get_body (EmpathyMessage *message)
587 EmpathyMessagePriv *priv;
589 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
591 priv = GET_PRIV (message);
597 empathy_message_get_timestamp (EmpathyMessage *message)
599 EmpathyMessagePriv *priv;
601 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
603 priv = GET_PRIV (message);
605 return priv->timestamp;
609 empathy_message_get_original_timestamp (EmpathyMessage *message)
611 EmpathyMessagePriv *priv;
613 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
615 priv = GET_PRIV (message);
617 return priv->original_timestamp;
621 empathy_message_is_backlog (EmpathyMessage *message)
623 EmpathyMessagePriv *priv;
625 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
627 priv = GET_PRIV (message);
629 return priv->is_backlog;
632 TpChannelTextMessageType
633 empathy_message_type_from_str (const gchar *type_str)
635 if (strcmp (type_str, "normal") == 0) {
636 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
638 if (strcmp (type_str, "action") == 0) {
639 return TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
641 else if (strcmp (type_str, "notice") == 0) {
642 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE;
644 else if (strcmp (type_str, "auto-reply") == 0) {
645 return TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY;
648 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
652 empathy_message_type_to_str (TpChannelTextMessageType type)
655 case TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION:
657 case TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE:
659 case TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY:
661 case TP_CHANNEL_TEXT_MESSAGE_TYPE_DELIVERY_REPORT:
662 return "delivery-report";
663 case TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
670 empathy_message_is_incoming (EmpathyMessage *message)
672 EmpathyMessagePriv *priv = GET_PRIV (message);
674 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
676 return priv->incoming;
680 empathy_message_equal (EmpathyMessage *message1, EmpathyMessage *message2)
682 EmpathyMessagePriv *priv1;
683 EmpathyMessagePriv *priv2;
685 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message1), FALSE);
686 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message2), FALSE);
688 priv1 = GET_PRIV (message1);
689 priv2 = GET_PRIV (message2);
691 if (priv1->timestamp == priv2->timestamp &&
692 !tp_strdiff (priv1->body, priv2->body)) {
699 TpChannelTextMessageFlags
700 empathy_message_get_flags (EmpathyMessage *self)
702 EmpathyMessagePriv *priv = GET_PRIV (self);
704 g_return_val_if_fail (EMPATHY_IS_MESSAGE (self), 0);
710 empathy_message_new_from_tp_message (TpMessage *tp_msg,
713 EmpathyMessage *message;
715 TpChannelTextMessageFlags flags;
717 gint64 original_timestamp;
718 const GHashTable *part = tp_message_peek (tp_msg, 0);
721 g_return_val_if_fail (TP_IS_MESSAGE (tp_msg), NULL);
723 body = tp_message_to_text (tp_msg, &flags);
725 timestamp = tp_message_get_sent_timestamp (tp_msg);
727 timestamp = tp_message_get_received_timestamp (tp_msg);
729 original_timestamp = tp_asv_get_int64 (part,
730 "original-message-received", NULL);
732 is_backlog = (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) ==
733 TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK;
735 message = g_object_new (EMPATHY_TYPE_MESSAGE,
737 "token", tp_message_get_token (tp_msg),
738 "supersedes", tp_message_get_supersedes (tp_msg),
739 "type", tp_message_get_message_type (tp_msg),
740 "timestamp", timestamp,
741 "original-timestamp", original_timestamp,
743 "is-backlog", is_backlog,
744 "incoming", incoming,
745 "tp-message", tp_msg,