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>
26 #include "empathy-message.h"
28 #include <glib/gi18n-lib.h>
29 #include <tp-account-widgets/tpaw-time.h>
31 #include "empathy-client-factory.h"
32 #include "empathy-utils.h"
33 #include "empathy-enum-types.h"
35 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyMessage)
37 TpMessage *tp_message;
38 TpChannelTextMessageType type;
39 EmpathyContact *sender;
40 EmpathyContact *receiver;
45 gint64 original_timestamp;
51 static void empathy_message_finalize (GObject *object);
52 static void message_get_property (GObject *object,
56 static void message_set_property (GObject *object,
61 G_DEFINE_TYPE (EmpathyMessage, empathy_message, G_TYPE_OBJECT);
72 PROP_ORIGINAL_TIMESTAMP,
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,
96 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
97 G_PARAM_CONSTRUCT_ONLY));
98 g_object_class_install_property (object_class,
100 g_param_spec_object ("sender",
102 "The sender of the message",
103 EMPATHY_TYPE_CONTACT,
104 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
105 g_object_class_install_property (object_class,
107 g_param_spec_object ("receiver",
109 "The receiver 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_string ("token",
118 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
119 g_object_class_install_property (object_class,
121 g_param_spec_string ("supersedes",
123 "The message-token this message supersedes",
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 ("body",
130 "The content of the message",
132 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
133 G_PARAM_CONSTRUCT_ONLY));
134 g_object_class_install_property (object_class,
136 g_param_spec_int64 ("timestamp",
139 G_MININT64, G_MAXINT64, 0,
140 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
141 G_PARAM_CONSTRUCT_ONLY));
142 g_object_class_install_property (object_class,
143 PROP_ORIGINAL_TIMESTAMP,
144 g_param_spec_int64 ("original-timestamp",
145 "Original Timestamp",
146 "Timestamp of the original message",
147 G_MININT64, G_MAXINT64, 0,
148 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
149 g_object_class_install_property (object_class,
151 g_param_spec_boolean ("is-backlog",
153 "If the message belongs to history",
155 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
156 G_PARAM_CONSTRUCT_ONLY));
159 g_object_class_install_property (object_class,
161 g_param_spec_boolean ("incoming",
163 "If this is an incoming (as opposed to sent) message",
165 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
166 G_PARAM_CONSTRUCT_ONLY));
168 g_object_class_install_property (object_class,
170 g_param_spec_object ("tp-message",
172 "The TpMessage of this message",
174 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
175 G_PARAM_CONSTRUCT_ONLY));
177 g_type_class_add_private (object_class, sizeof (EmpathyMessagePriv));
182 empathy_message_init (EmpathyMessage *message)
184 EmpathyMessagePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
185 EMPATHY_TYPE_MESSAGE, EmpathyMessagePriv);
187 message->priv = priv;
188 priv->timestamp = tpaw_time_get_current ();
192 empathy_message_finalize (GObject *object)
194 EmpathyMessagePriv *priv;
196 priv = GET_PRIV (object);
199 g_object_unref (priv->sender);
201 if (priv->receiver) {
202 g_object_unref (priv->receiver);
205 if (priv->tp_message) {
206 g_object_unref (priv->tp_message);
209 g_free (priv->token);
210 g_free (priv->supersedes);
213 G_OBJECT_CLASS (empathy_message_parent_class)->finalize (object);
217 message_get_property (GObject *object,
222 EmpathyMessagePriv *priv;
224 priv = GET_PRIV (object);
228 g_value_set_uint (value, priv->type);
231 g_value_set_object (value, priv->sender);
234 g_value_set_object (value, priv->receiver);
237 g_value_set_string (value, priv->token);
239 case PROP_SUPERSEDES:
240 g_value_set_string (value, priv->supersedes);
243 g_value_set_string (value, priv->body);
246 g_value_set_int64 (value, priv->timestamp);
248 case PROP_ORIGINAL_TIMESTAMP:
249 g_value_set_int64 (value, priv->original_timestamp);
251 case PROP_IS_BACKLOG:
252 g_value_set_boolean (value, priv->is_backlog);
255 g_value_set_boolean (value, priv->incoming);
257 case PROP_TP_MESSAGE:
258 g_value_set_object (value, priv->tp_message);
261 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
267 message_set_property (GObject *object,
272 EmpathyMessagePriv *priv;
274 priv = GET_PRIV (object);
278 priv->type = g_value_get_uint (value);
281 empathy_message_set_sender (EMPATHY_MESSAGE (object),
282 EMPATHY_CONTACT (g_value_get_object (value)));
285 empathy_message_set_receiver (EMPATHY_MESSAGE (object),
286 EMPATHY_CONTACT (g_value_get_object (value)));
289 g_assert (priv->token == NULL); /* construct only */
290 priv->token = g_value_dup_string (value);
292 case PROP_SUPERSEDES:
293 g_assert (priv->supersedes == NULL); /* construct only */
294 priv->supersedes = g_value_dup_string (value);
297 g_assert (priv->body == NULL); /* construct only */
298 priv->body = g_value_dup_string (value);
301 priv->timestamp = g_value_get_int64 (value);
302 if (priv->timestamp <= 0)
303 priv->timestamp = tpaw_time_get_current ();
305 case PROP_ORIGINAL_TIMESTAMP:
306 priv->original_timestamp = g_value_get_int64 (value);
308 case PROP_IS_BACKLOG:
309 priv->is_backlog = g_value_get_boolean (value);
312 priv->incoming = g_value_get_boolean (value);
314 case PROP_TP_MESSAGE:
315 priv->tp_message = g_value_dup_object (value);
318 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
324 empathy_message_from_tpl_log_event (TplEvent *logevent)
326 EmpathyMessage *retval = NULL;
327 EmpathyClientFactory *factory;
328 TpAccount *account = NULL;
329 TplEntity *receiver = NULL;
330 TplEntity *sender = NULL;
332 const gchar *token = NULL, *supersedes = NULL;
333 EmpathyContact *contact;
334 TpChannelTextMessageType type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
335 gint64 timestamp, original_timestamp = 0;
337 g_return_val_if_fail (TPL_IS_EVENT (logevent), NULL);
339 factory = empathy_client_factory_dup ();
340 /* FIXME Currently Empathy shows in the log viewer only valid accounts, so it
341 * won't be selected any non-existing (ie removed) account.
342 * When #610455 will be fixed, calling tp_account_manager_ensure_account ()
343 * might add a not existing account to the AM. tp_account_new () probably
344 * will be the best way to handle it.
345 * Note: When creating an EmpathyContact from a TplEntity instance, the
346 * TpAccount is passed *only* to let EmpathyContact be able to retrieve the
347 * avatar (contact_get_avatar_filename () need a TpAccount).
348 * If the way EmpathyContact stores the avatar is changes, it might not be
349 * needed anymore any TpAccount passing and the following call will be
351 account = tp_simple_client_factory_ensure_account (
352 TP_SIMPLE_CLIENT_FACTORY (factory),
353 tpl_event_get_account_path (logevent), NULL, NULL);
354 g_object_unref (factory);
356 if (TPL_IS_TEXT_EVENT (logevent)) {
357 TplTextEvent *textevent = TPL_TEXT_EVENT (logevent);
359 supersedes = tpl_text_event_get_supersedes_token (textevent);
361 /* tp-logger is kind of messy in that instead of having
362 * timestamp and original-timestamp like Telepathy it has
363 * timestamp (which is the original) and edited-timestamp,
364 * (which is when the message was edited) */
365 if (tp_str_empty (supersedes)) {
366 /* not an edited message */
367 timestamp = tpl_event_get_timestamp (logevent);
369 /* this is an edited event */
370 original_timestamp = tpl_event_get_timestamp (logevent);
371 timestamp = tpl_text_event_get_edit_timestamp (textevent);
374 body = g_strdup (tpl_text_event_get_message (textevent));
376 type = tpl_text_event_get_message_type (TPL_TEXT_EVENT (logevent));
377 token = tpl_text_event_get_message_token (textevent);
379 else if (TPL_IS_CALL_EVENT (logevent)) {
380 TplCallEvent *call = TPL_CALL_EVENT (logevent);
382 timestamp = tpl_event_get_timestamp (logevent);
384 if (tpl_call_event_get_end_reason (call) == TP_CALL_STATE_CHANGE_REASON_NO_ANSWER)
385 body = g_strdup_printf (_("Missed call from %s"),
386 tpl_entity_get_alias (tpl_event_get_sender (logevent)));
387 else if (tpl_entity_get_entity_type (tpl_event_get_sender (logevent)) == TPL_ENTITY_SELF)
388 /* Translators: this is an outgoing call, e.g. 'Called Alice' */
389 body = g_strdup_printf (_("Called %s"),
390 tpl_entity_get_alias (tpl_event_get_receiver (logevent)));
392 body = g_strdup_printf (_("Call from %s"),
393 tpl_entity_get_alias (tpl_event_get_sender (logevent)));
396 /* Unknown event type */
400 receiver = tpl_event_get_receiver (logevent);
401 sender = tpl_event_get_sender (logevent);
403 retval = g_object_new (EMPATHY_TYPE_MESSAGE,
406 "supersedes", supersedes,
409 "timestamp", timestamp,
410 "original-timestamp", original_timestamp,
413 if (receiver != NULL) {
414 contact = empathy_contact_from_tpl_contact (account, receiver);
415 empathy_message_set_receiver (retval, contact);
416 g_object_unref (contact);
419 if (sender != NULL) {
420 contact = empathy_contact_from_tpl_contact (account, sender);
421 empathy_message_set_sender (retval, contact);
422 g_object_unref (contact);
431 empathy_message_get_tp_message (EmpathyMessage *message)
433 EmpathyMessagePriv *priv;
435 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
437 priv = GET_PRIV (message);
439 return priv->tp_message;
442 TpChannelTextMessageType
443 empathy_message_get_tptype (EmpathyMessage *message)
445 EmpathyMessagePriv *priv;
447 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message),
448 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL);
450 priv = GET_PRIV (message);
456 empathy_message_get_sender (EmpathyMessage *message)
458 EmpathyMessagePriv *priv;
460 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
462 priv = GET_PRIV (message);
468 empathy_message_set_sender (EmpathyMessage *message, EmpathyContact *contact)
470 EmpathyMessagePriv *priv;
471 EmpathyContact *old_sender;
473 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
474 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
476 priv = GET_PRIV (message);
478 old_sender = priv->sender;
479 priv->sender = g_object_ref (contact);
482 g_object_unref (old_sender);
485 g_object_notify (G_OBJECT (message), "sender");
489 empathy_message_get_receiver (EmpathyMessage *message)
491 EmpathyMessagePriv *priv;
493 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
495 priv = GET_PRIV (message);
497 return priv->receiver;
501 empathy_message_set_receiver (EmpathyMessage *message, EmpathyContact *contact)
503 EmpathyMessagePriv *priv;
504 EmpathyContact *old_receiver;
506 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
507 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
509 priv = GET_PRIV (message);
511 old_receiver = priv->receiver;
512 priv->receiver = g_object_ref (contact);
515 g_object_unref (old_receiver);
518 g_object_notify (G_OBJECT (message), "receiver");
522 empathy_message_get_token (EmpathyMessage *message)
524 EmpathyMessagePriv *priv;
526 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
528 priv = GET_PRIV (message);
534 empathy_message_get_supersedes (EmpathyMessage *message)
536 EmpathyMessagePriv *priv;
538 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
540 priv = GET_PRIV (message);
542 return priv->supersedes;
546 empathy_message_is_edit (EmpathyMessage *message)
548 EmpathyMessagePriv *priv;
550 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
552 priv = GET_PRIV (message);
554 return !tp_str_empty (priv->supersedes);
558 empathy_message_get_body (EmpathyMessage *message)
560 EmpathyMessagePriv *priv;
562 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
564 priv = GET_PRIV (message);
570 empathy_message_get_timestamp (EmpathyMessage *message)
572 EmpathyMessagePriv *priv;
574 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
576 priv = GET_PRIV (message);
578 return priv->timestamp;
582 empathy_message_get_original_timestamp (EmpathyMessage *message)
584 EmpathyMessagePriv *priv;
586 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
588 priv = GET_PRIV (message);
590 return priv->original_timestamp;
594 empathy_message_is_backlog (EmpathyMessage *message)
596 EmpathyMessagePriv *priv;
598 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
600 priv = GET_PRIV (message);
602 return priv->is_backlog;
605 TpChannelTextMessageType
606 empathy_message_type_from_str (const gchar *type_str)
608 if (strcmp (type_str, "normal") == 0) {
609 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
611 if (strcmp (type_str, "action") == 0) {
612 return TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
614 else if (strcmp (type_str, "notice") == 0) {
615 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE;
617 else if (strcmp (type_str, "auto-reply") == 0) {
618 return TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY;
621 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
625 empathy_message_type_to_str (TpChannelTextMessageType type)
628 case TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION:
630 case TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE:
632 case TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY:
634 case TP_CHANNEL_TEXT_MESSAGE_TYPE_DELIVERY_REPORT:
635 return "delivery-report";
636 case TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
643 empathy_message_is_incoming (EmpathyMessage *message)
645 EmpathyMessagePriv *priv = GET_PRIV (message);
647 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
649 return priv->incoming;
653 empathy_message_equal (EmpathyMessage *message1, EmpathyMessage *message2)
655 EmpathyMessagePriv *priv1;
656 EmpathyMessagePriv *priv2;
658 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message1), FALSE);
659 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message2), FALSE);
661 priv1 = GET_PRIV (message1);
662 priv2 = GET_PRIV (message2);
664 if (priv1->timestamp == priv2->timestamp &&
665 !tp_strdiff (priv1->body, priv2->body)) {
673 empathy_message_new_from_tp_message (TpMessage *tp_msg,
676 EmpathyMessage *message;
679 gint64 original_timestamp;
680 const GHashTable *part = tp_message_peek (tp_msg, 0);
682 g_return_val_if_fail (TP_IS_MESSAGE (tp_msg), NULL);
684 body = tp_message_to_text (tp_msg, NULL);
686 timestamp = tp_message_get_sent_timestamp (tp_msg);
688 timestamp = tp_message_get_received_timestamp (tp_msg);
690 original_timestamp = tp_asv_get_int64 (part,
691 "original-message-received", NULL);
693 message = g_object_new (EMPATHY_TYPE_MESSAGE,
695 "token", tp_message_get_token (tp_msg),
696 "supersedes", tp_message_get_supersedes (tp_msg),
697 "type", tp_message_get_message_type (tp_msg),
698 "timestamp", timestamp,
699 "original-timestamp", original_timestamp,
700 "is-backlog", tp_message_is_scrollback (tp_msg),
701 "incoming", incoming,
702 "tp-message", tp_msg,