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 <telepathy-glib/util.h>
30 #include <telepathy-glib/account.h>
31 #include <telepathy-glib/account-manager.h>
33 #include <telepathy-logger/entity.h>
34 #include <telepathy-logger/entry.h>
35 #include <telepathy-logger/entry-text.h>
37 #include "empathy-message.h"
38 #include "empathy-utils.h"
39 #include "empathy-enum-types.h"
41 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyMessage)
43 TpChannelTextMessageType type;
44 EmpathyContact *sender;
45 EmpathyContact *receiver;
51 TpChannelTextMessageFlags flags;
54 static void empathy_message_finalize (GObject *object);
55 static void message_get_property (GObject *object,
59 static void message_set_property (GObject *object,
64 G_DEFINE_TYPE (EmpathyMessage, empathy_message, G_TYPE_OBJECT);
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,
97 g_object_class_install_property (object_class,
99 g_param_spec_object ("sender",
101 "The sender of the message",
102 EMPATHY_TYPE_CONTACT,
104 g_object_class_install_property (object_class,
106 g_param_spec_object ("receiver",
108 "The receiver of the message",
109 EMPATHY_TYPE_CONTACT,
111 g_object_class_install_property (object_class,
113 g_param_spec_string ("body",
115 "The content of the message",
118 g_object_class_install_property (object_class,
120 g_param_spec_long ("timestamp",
127 g_object_class_install_property (object_class,
129 g_param_spec_boolean ("is-backlog",
131 "If the message belongs to history",
136 g_object_class_install_property (object_class,
138 g_param_spec_boolean ("incoming",
140 "If this is an incoming (as opposed to sent) message",
142 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
144 g_object_class_install_property (object_class,
146 g_param_spec_uint ("flags",
148 "The TpChannelTextMessageFlags of this message",
150 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
152 g_type_class_add_private (object_class, sizeof (EmpathyMessagePriv));
157 empathy_message_init (EmpathyMessage *message)
159 EmpathyMessagePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
160 EMPATHY_TYPE_MESSAGE, EmpathyMessagePriv);
162 message->priv = priv;
163 priv->timestamp = empathy_time_get_current ();
167 empathy_message_finalize (GObject *object)
169 EmpathyMessagePriv *priv;
171 priv = GET_PRIV (object);
174 g_object_unref (priv->sender);
176 if (priv->receiver) {
177 g_object_unref (priv->receiver);
182 G_OBJECT_CLASS (empathy_message_parent_class)->finalize (object);
186 message_get_property (GObject *object,
191 EmpathyMessagePriv *priv;
193 priv = GET_PRIV (object);
197 g_value_set_uint (value, priv->type);
200 g_value_set_object (value, priv->sender);
203 g_value_set_object (value, priv->receiver);
206 g_value_set_string (value, priv->body);
209 g_value_set_boolean (value, priv->incoming);
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
218 message_set_property (GObject *object,
223 EmpathyMessagePriv *priv;
225 priv = GET_PRIV (object);
229 empathy_message_set_tptype (EMPATHY_MESSAGE (object),
230 g_value_get_uint (value));
233 empathy_message_set_sender (EMPATHY_MESSAGE (object),
234 EMPATHY_CONTACT (g_value_get_object (value)));
237 empathy_message_set_receiver (EMPATHY_MESSAGE (object),
238 EMPATHY_CONTACT (g_value_get_object (value)));
241 empathy_message_set_body (EMPATHY_MESSAGE (object),
242 g_value_get_string (value));
245 priv->incoming = g_value_get_boolean (value);
248 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
254 empathy_message_new (const gchar *body)
256 return g_object_new (EMPATHY_TYPE_MESSAGE,
262 empathy_message_from_tpl_log_entry (TplEntry *logentry)
264 EmpathyMessage *retval = NULL;
265 TpAccountManager *acc_man = NULL;
266 TpAccount *account = NULL;
267 TplEntity *receiver = NULL;
268 TplEntity *sender = NULL;
270 EmpathyContact *contact;
272 g_return_val_if_fail (TPL_IS_ENTRY (logentry), NULL);
274 acc_man = tp_account_manager_dup ();
275 /* FIXME Currently Empathy shows in the log viewer only valid accounts, so it
276 * won't be selected any non-existing (ie removed) account.
277 * When #610455 will be fixed, calling tp_account_manager_ensure_account ()
278 * might add a not existing account to the AM. tp_account_new () probably
279 * will be the best way to handle it.
280 * Note: When creating an EmpathyContact from a TplEntity instance, the
281 * TpAccount is passed *only* to let EmpathyContact be able to retrieve the
282 * avatar (contact_get_avatar_filename () need a TpAccount).
283 * If the way EmpathyContact stores the avatar is changes, it might not be
284 * needed anymore any TpAccount passing and the following call will be
286 account = tp_account_manager_ensure_account (acc_man,
287 tpl_entry_get_account_path (logentry));
288 g_object_unref (acc_man);
290 /* TODO Currently only TplLogEntryText exists as subclass of TplEntry, in
291 * future more TplEntry will exist and EmpathyMessage should probably
292 * be enhanced to support other types of log entries (ie TplLogEntryCall).
294 * For now we just check (simply) that we are dealing with the only supported type,
295 * then there will be a if/then/else or switch handling all the supported
298 if (!TPL_IS_ENTRY_TEXT (logentry))
301 body = g_strdup (tpl_entry_text_get_message (
302 TPL_ENTRY_TEXT (logentry)));
303 receiver = tpl_entry_get_receiver (logentry);
304 sender = tpl_entry_get_sender (logentry);
306 retval = empathy_message_new (body);
307 if (receiver != NULL) {
308 contact = empathy_contact_from_tpl_contact (account, receiver);
309 empathy_message_set_receiver (retval, contact);
310 g_object_unref (contact);
313 if (sender != NULL) {
314 contact = empathy_contact_from_tpl_contact (account, sender);
315 empathy_message_set_sender (retval, contact);
316 g_object_unref (contact);
319 empathy_message_set_timestamp (retval,
320 tpl_entry_get_timestamp (logentry));
321 empathy_message_set_id (retval,
322 tpl_entry_text_get_pending_msg_id (TPL_ENTRY_TEXT (logentry)));
323 empathy_message_set_is_backlog (retval, TRUE);
330 TpChannelTextMessageType
331 empathy_message_get_tptype (EmpathyMessage *message)
333 EmpathyMessagePriv *priv;
335 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message),
336 TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL);
338 priv = GET_PRIV (message);
344 empathy_message_set_tptype (EmpathyMessage *message,
345 TpChannelTextMessageType type)
347 EmpathyMessagePriv *priv;
349 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
351 priv = GET_PRIV (message);
355 g_object_notify (G_OBJECT (message), "type");
359 empathy_message_get_sender (EmpathyMessage *message)
361 EmpathyMessagePriv *priv;
363 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
365 priv = GET_PRIV (message);
371 empathy_message_set_sender (EmpathyMessage *message, EmpathyContact *contact)
373 EmpathyMessagePriv *priv;
374 EmpathyContact *old_sender;
376 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
377 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
379 priv = GET_PRIV (message);
381 old_sender = priv->sender;
382 priv->sender = g_object_ref (contact);
385 g_object_unref (old_sender);
388 g_object_notify (G_OBJECT (message), "sender");
392 empathy_message_get_receiver (EmpathyMessage *message)
394 EmpathyMessagePriv *priv;
396 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
398 priv = GET_PRIV (message);
400 return priv->receiver;
404 empathy_message_set_receiver (EmpathyMessage *message, EmpathyContact *contact)
406 EmpathyMessagePriv *priv;
407 EmpathyContact *old_receiver;
409 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
410 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
412 priv = GET_PRIV (message);
414 old_receiver = priv->receiver;
415 priv->receiver = g_object_ref (contact);
418 g_object_unref (old_receiver);
421 g_object_notify (G_OBJECT (message), "receiver");
425 empathy_message_get_body (EmpathyMessage *message)
427 EmpathyMessagePriv *priv;
429 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
431 priv = GET_PRIV (message);
437 empathy_message_set_body (EmpathyMessage *message,
440 EmpathyMessagePriv *priv = GET_PRIV (message);
442 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
447 priv->body = g_strdup (body);
452 g_object_notify (G_OBJECT (message), "body");
456 empathy_message_get_timestamp (EmpathyMessage *message)
458 EmpathyMessagePriv *priv;
460 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
462 priv = GET_PRIV (message);
464 return priv->timestamp;
468 empathy_message_set_timestamp (EmpathyMessage *message,
471 EmpathyMessagePriv *priv;
473 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
474 g_return_if_fail (timestamp >= -1);
476 priv = GET_PRIV (message);
478 if (timestamp <= 0) {
479 priv->timestamp = empathy_time_get_current ();
481 priv->timestamp = timestamp;
484 g_object_notify (G_OBJECT (message), "timestamp");
488 empathy_message_is_backlog (EmpathyMessage *message)
490 EmpathyMessagePriv *priv;
492 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
494 priv = GET_PRIV (message);
496 return priv->is_backlog;
500 empathy_message_set_is_backlog (EmpathyMessage *message,
503 EmpathyMessagePriv *priv;
505 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
507 priv = GET_PRIV (message);
509 priv->is_backlog = is_backlog;
511 g_object_notify (G_OBJECT (message), "is-backlog");
514 #define IS_SEPARATOR(ch) (ch == ' ' || ch == ',' || ch == '.' || ch == ':')
516 empathy_message_should_highlight (EmpathyMessage *message)
518 EmpathyContact *contact;
519 const gchar *msg, *to;
520 gchar *cf_msg, *cf_to;
523 TpChannelTextMessageFlags flags;
525 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
529 msg = empathy_message_get_body (message);
534 contact = empathy_message_get_receiver (message);
535 if (!contact || !empathy_contact_is_user (contact)) {
539 to = empathy_contact_get_alias (contact);
544 flags = empathy_message_get_flags (message);
545 if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) {
546 /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
547 * have already been received by the user before (and so are in the logs) */
551 cf_msg = g_utf8_casefold (msg, -1);
552 cf_to = g_utf8_casefold (to, -1);
554 ch = strstr (cf_msg, cf_to);
559 /* Not first in the message */
560 if (!IS_SEPARATOR (*(ch - 1))) {
565 ch = ch + strlen (cf_to);
566 if (ch >= cf_msg + strlen (cf_msg)) {
571 if (IS_SEPARATOR (*ch)) {
583 TpChannelTextMessageType
584 empathy_message_type_from_str (const gchar *type_str)
586 if (strcmp (type_str, "normal") == 0) {
587 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
589 if (strcmp (type_str, "action") == 0) {
590 return TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
592 else if (strcmp (type_str, "notice") == 0) {
593 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE;
595 else if (strcmp (type_str, "auto-reply") == 0) {
596 return TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY;
599 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
603 empathy_message_type_to_str (TpChannelTextMessageType type)
606 case TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION:
608 case TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE:
610 case TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY:
612 case TP_CHANNEL_TEXT_MESSAGE_TYPE_DELIVERY_REPORT:
613 return "delivery-report";
614 case TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
621 empathy_message_get_id (EmpathyMessage *message)
623 EmpathyMessagePriv *priv = GET_PRIV (message);
625 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), 0);
631 empathy_message_set_id (EmpathyMessage *message, guint id)
633 EmpathyMessagePriv *priv = GET_PRIV (message);
639 empathy_message_set_incoming (EmpathyMessage *message, gboolean incoming)
641 EmpathyMessagePriv *priv;
643 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
645 priv = GET_PRIV (message);
647 priv->incoming = incoming;
649 g_object_notify (G_OBJECT (message), "incoming");
653 empathy_message_is_incoming (EmpathyMessage *message)
655 EmpathyMessagePriv *priv = GET_PRIV (message);
657 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
659 return priv->incoming;
663 empathy_message_equal (EmpathyMessage *message1, EmpathyMessage *message2)
665 EmpathyMessagePriv *priv1;
666 EmpathyMessagePriv *priv2;
668 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message1), FALSE);
669 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message2), FALSE);
671 priv1 = GET_PRIV (message1);
672 priv2 = GET_PRIV (message2);
674 if (priv1->timestamp == priv2->timestamp &&
675 !tp_strdiff (priv1->body, priv2->body)) {
682 TpChannelTextMessageFlags
683 empathy_message_get_flags (EmpathyMessage *self)
685 EmpathyMessagePriv *priv = GET_PRIV (self);
687 g_return_val_if_fail (EMPATHY_IS_MESSAGE (self), 0);
693 empathy_message_set_flags (EmpathyMessage *self,
694 TpChannelTextMessageFlags flags)
696 EmpathyMessagePriv *priv;
698 g_return_if_fail (EMPATHY_IS_MESSAGE (self));
700 priv = GET_PRIV (self);
704 g_object_notify (G_OBJECT (self), "flags");