]> git.0d.be Git - empathy.git/blob - libempathy/empathy-message.c
0a8ca408b7bb4de72f207a3368306c2c71d661e2
[empathy.git] / libempathy / empathy-message.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2004-2007 Imendio AB
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
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.
10  *
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.
15  *
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
20  *
21  * Authors: Mikael Hallendal <micke@imendio.com>
22  *          Xavier Claessens <xclaesse@gmail.com>
23  */
24
25 #include "config.h"
26
27 #include <string.h>
28
29 #include <glib/gi18n-lib.h>
30
31 #include <telepathy-glib/util.h>
32 #include <telepathy-glib/account.h>
33 #include <telepathy-glib/account-manager.h>
34
35 #include "empathy-client-factory.h"
36 #include "empathy-message.h"
37 #include "empathy-utils.h"
38 #include "empathy-enum-types.h"
39
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyMessage)
41 typedef struct {
42         TpMessage *tp_message;
43         TpChannelTextMessageType  type;
44         EmpathyContact           *sender;
45         EmpathyContact           *receiver;
46         gchar                    *token;
47         gchar                    *supersedes;
48         gchar                    *body;
49         gint64                    timestamp;
50         gint64                    original_timestamp;
51         gboolean                  is_backlog;
52         guint                     id;
53         gboolean                  incoming;
54         TpChannelTextMessageFlags flags;
55 } EmpathyMessagePriv;
56
57 static void empathy_message_finalize   (GObject            *object);
58 static void message_get_property      (GObject            *object,
59                                        guint               param_id,
60                                        GValue             *value,
61                                        GParamSpec         *pspec);
62 static void message_set_property      (GObject            *object,
63                                        guint               param_id,
64                                        const GValue       *value,
65                                        GParamSpec         *pspec);
66
67 G_DEFINE_TYPE (EmpathyMessage, empathy_message, G_TYPE_OBJECT);
68
69 enum {
70         PROP_0,
71         PROP_TYPE,
72         PROP_SENDER,
73         PROP_RECEIVER,
74         PROP_TOKEN,
75         PROP_SUPERSEDES,
76         PROP_BODY,
77         PROP_TIMESTAMP,
78         PROP_ORIGINAL_TIMESTAMP,
79         PROP_IS_BACKLOG,
80         PROP_INCOMING,
81         PROP_FLAGS,
82         PROP_TP_MESSAGE,
83 };
84
85 static void
86 empathy_message_class_init (EmpathyMessageClass *class)
87 {
88         GObjectClass *object_class;
89
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;
94
95         g_object_class_install_property (object_class,
96                                          PROP_TYPE,
97                                          g_param_spec_uint ("type",
98                                                             "Message 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,
106                                          PROP_SENDER,
107                                          g_param_spec_object ("sender",
108                                                               "Message 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,
113                                          PROP_RECEIVER,
114                                          g_param_spec_object ("receiver",
115                                                               "Message 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,
120                                          PROP_TOKEN,
121                                          g_param_spec_string ("token",
122                                                               "Message Token",
123                                                               "The message-token",
124                                                               NULL,
125                                                               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
126         g_object_class_install_property (object_class,
127                                          PROP_SUPERSEDES,
128                                          g_param_spec_string ("supersedes",
129                                                               "Supersedes Token",
130                                                               "The message-token this message supersedes",
131                                                               NULL,
132                                                               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
133         g_object_class_install_property (object_class,
134                                          PROP_BODY,
135                                          g_param_spec_string ("body",
136                                                               "Message Body",
137                                                               "The content of the message",
138                                                               NULL,
139                                                               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
140                                                               G_PARAM_CONSTRUCT_ONLY));
141         g_object_class_install_property (object_class,
142                                          PROP_TIMESTAMP,
143                                          g_param_spec_int64 ("timestamp",
144                                                             "timestamp",
145                                                             "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,
157                                          PROP_IS_BACKLOG,
158                                          g_param_spec_boolean ("is-backlog",
159                                                                "History message",
160                                                                "If the message belongs to history",
161                                                                FALSE,
162                                                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
163                                                                G_PARAM_CONSTRUCT_ONLY));
164
165
166         g_object_class_install_property (object_class,
167                                          PROP_INCOMING,
168                                          g_param_spec_boolean ("incoming",
169                                                                "Incoming",
170                                                                "If this is an incoming (as opposed to sent) message",
171                                                                FALSE,
172                                                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
173                                                                G_PARAM_CONSTRUCT_ONLY));
174
175         g_object_class_install_property (object_class,
176                                          PROP_FLAGS,
177                                          g_param_spec_uint ("flags",
178                                                                "Flags",
179                                                                "The TpChannelTextMessageFlags of this message",
180                                                                0, G_MAXUINT, 0,
181                                                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
182                                                                G_PARAM_CONSTRUCT_ONLY));
183
184         g_object_class_install_property (object_class,
185                                          PROP_TP_MESSAGE,
186                                          g_param_spec_object ("tp-message",
187                                                                "TpMessage",
188                                                                "The TpMessage of this message",
189                                                                TP_TYPE_MESSAGE,
190                                                                G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
191                                                                G_PARAM_CONSTRUCT_ONLY));
192
193         g_type_class_add_private (object_class, sizeof (EmpathyMessagePriv));
194
195 }
196
197 static void
198 empathy_message_init (EmpathyMessage *message)
199 {
200         EmpathyMessagePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
201                 EMPATHY_TYPE_MESSAGE, EmpathyMessagePriv);
202
203         message->priv = priv;
204         priv->timestamp = empathy_time_get_current ();
205 }
206
207 static void
208 empathy_message_finalize (GObject *object)
209 {
210         EmpathyMessagePriv *priv;
211
212         priv = GET_PRIV (object);
213
214         if (priv->sender) {
215                 g_object_unref (priv->sender);
216         }
217         if (priv->receiver) {
218                 g_object_unref (priv->receiver);
219         }
220
221         if (priv->tp_message) {
222                 g_object_unref (priv->tp_message);
223         }
224
225         g_free (priv->token);
226         g_free (priv->supersedes);
227         g_free (priv->body);
228
229         G_OBJECT_CLASS (empathy_message_parent_class)->finalize (object);
230 }
231
232 static void
233 message_get_property (GObject    *object,
234                       guint       param_id,
235                       GValue     *value,
236                       GParamSpec *pspec)
237 {
238         EmpathyMessagePriv *priv;
239
240         priv = GET_PRIV (object);
241
242         switch (param_id) {
243         case PROP_TYPE:
244                 g_value_set_uint (value, priv->type);
245                 break;
246         case PROP_SENDER:
247                 g_value_set_object (value, priv->sender);
248                 break;
249         case PROP_RECEIVER:
250                 g_value_set_object (value, priv->receiver);
251                 break;
252         case PROP_TOKEN:
253                 g_value_set_string (value, priv->token);
254                 break;
255         case PROP_SUPERSEDES:
256                 g_value_set_string (value, priv->supersedes);
257                 break;
258         case PROP_BODY:
259                 g_value_set_string (value, priv->body);
260                 break;
261         case PROP_TIMESTAMP:
262                 g_value_set_int64 (value, priv->timestamp);
263                 break;
264         case PROP_ORIGINAL_TIMESTAMP:
265                 g_value_set_int64 (value, priv->original_timestamp);
266                 break;
267         case PROP_IS_BACKLOG:
268                 g_value_set_boolean (value, priv->is_backlog);
269                 break;
270         case PROP_INCOMING:
271                 g_value_set_boolean (value, priv->incoming);
272                 break;
273         case PROP_FLAGS:
274                 g_value_set_uint (value, priv->flags);
275                 break;
276         case PROP_TP_MESSAGE:
277                 g_value_set_object (value, priv->tp_message);
278                 break;
279         default:
280                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
281                 break;
282         };
283 }
284
285 static void
286 message_set_property (GObject      *object,
287                       guint         param_id,
288                       const GValue *value,
289                       GParamSpec   *pspec)
290 {
291         EmpathyMessagePriv *priv;
292
293         priv = GET_PRIV (object);
294
295         switch (param_id) {
296         case PROP_TYPE:
297                 priv->type = g_value_get_uint (value);
298                 break;
299         case PROP_SENDER:
300                 empathy_message_set_sender (EMPATHY_MESSAGE (object),
301                                            EMPATHY_CONTACT (g_value_get_object (value)));
302                 break;
303         case PROP_RECEIVER:
304                 empathy_message_set_receiver (EMPATHY_MESSAGE (object),
305                                              EMPATHY_CONTACT (g_value_get_object (value)));
306                 break;
307         case PROP_TOKEN:
308                 g_assert (priv->token == NULL); /* construct only */
309                 priv->token = g_value_dup_string (value);
310                 break;
311         case PROP_SUPERSEDES:
312                 g_assert (priv->supersedes == NULL); /* construct only */
313                 priv->supersedes = g_value_dup_string (value);
314                 break;
315         case PROP_BODY:
316                 g_assert (priv->body == NULL); /* construct only */
317                 priv->body = g_value_dup_string (value);
318                 break;
319         case PROP_TIMESTAMP:
320                 priv->timestamp = g_value_get_int64 (value);
321                 if (priv->timestamp <= 0)
322                         priv->timestamp = empathy_time_get_current ();
323                 break;
324         case PROP_ORIGINAL_TIMESTAMP:
325                 priv->original_timestamp = g_value_get_int64 (value);
326                 break;
327         case PROP_IS_BACKLOG:
328                 priv->is_backlog = g_value_get_boolean (value);
329                 break;
330         case PROP_INCOMING:
331                 priv->incoming = g_value_get_boolean (value);
332                 break;
333         case PROP_FLAGS:
334                 priv->flags = g_value_get_uint (value);
335                 break;
336         case PROP_TP_MESSAGE:
337                 priv->tp_message = g_value_dup_object (value);
338                 break;
339         default:
340                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
341                 break;
342         };
343 }
344
345 EmpathyMessage *
346 empathy_message_from_tpl_log_event (TplEvent *logevent)
347 {
348         EmpathyMessage *retval = NULL;
349         EmpathyClientFactory *factory;
350         TpAccount *account = NULL;
351         TplEntity *receiver = NULL;
352         TplEntity *sender = NULL;
353         gchar *body = 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;
358
359         g_return_val_if_fail (TPL_IS_EVENT (logevent), NULL);
360
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
372          * useless */
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);
377
378         if (TPL_IS_TEXT_EVENT (logevent)) {
379                 TplTextEvent *textevent = TPL_TEXT_EVENT (logevent);
380
381                 supersedes = tpl_text_event_get_supersedes_token (textevent);
382
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);
390                 } else {
391                         /* this is an edited event */
392                         original_timestamp = tpl_event_get_timestamp (logevent);
393                         timestamp = tpl_text_event_get_edit_timestamp (textevent);
394                 }
395
396                 body = g_strdup (tpl_text_event_get_message (textevent));
397
398                 type = tpl_text_event_get_message_type (TPL_TEXT_EVENT (logevent));
399                 token = tpl_text_event_get_message_token (textevent);
400         }
401         else if (TPL_IS_CALL_EVENT (logevent)) {
402                 TplCallEvent *call = TPL_CALL_EVENT (logevent);
403
404                 timestamp = tpl_event_get_timestamp (logevent);
405
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)));
413                 else
414                         body = g_strdup_printf (_("Call from %s"),
415                                 tpl_entity_get_alias (tpl_event_get_sender (logevent)));
416         }
417         else {
418                 /* Unknown event type */
419                 return NULL;
420         }
421
422         receiver = tpl_event_get_receiver (logevent);
423         sender = tpl_event_get_sender (logevent);
424
425         retval = g_object_new (EMPATHY_TYPE_MESSAGE,
426                 "type", type,
427                 "token", token,
428                 "supersedes", supersedes,
429                 "body", body,
430                 "is-backlog", TRUE,
431                 "timestamp", timestamp,
432                 "original-timestamp", original_timestamp,
433                 NULL);
434
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);
439         }
440
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);
445         }
446
447         g_free (body);
448
449         return retval;
450 }
451
452 TpMessage *
453 empathy_message_get_tp_message (EmpathyMessage *message)
454 {
455         EmpathyMessagePriv *priv;
456
457         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
458
459         priv = GET_PRIV (message);
460
461         return priv->tp_message;
462 }
463
464 TpChannelTextMessageType
465 empathy_message_get_tptype (EmpathyMessage *message)
466 {
467         EmpathyMessagePriv *priv;
468
469         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message),
470                               TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL);
471
472         priv = GET_PRIV (message);
473
474         return priv->type;
475 }
476
477 EmpathyContact *
478 empathy_message_get_sender (EmpathyMessage *message)
479 {
480         EmpathyMessagePriv *priv;
481
482         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
483
484         priv = GET_PRIV (message);
485
486         return priv->sender;
487 }
488
489 void
490 empathy_message_set_sender (EmpathyMessage *message, EmpathyContact *contact)
491 {
492         EmpathyMessagePriv *priv;
493         EmpathyContact     *old_sender;
494
495         g_return_if_fail (EMPATHY_IS_MESSAGE (message));
496         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
497
498         priv = GET_PRIV (message);
499
500         old_sender = priv->sender;
501         priv->sender = g_object_ref (contact);
502
503         if (old_sender) {
504                 g_object_unref (old_sender);
505         }
506
507         g_object_notify (G_OBJECT (message), "sender");
508 }
509
510 EmpathyContact *
511 empathy_message_get_receiver (EmpathyMessage *message)
512 {
513         EmpathyMessagePriv *priv;
514
515         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
516
517         priv = GET_PRIV (message);
518
519         return priv->receiver;
520 }
521
522 void
523 empathy_message_set_receiver (EmpathyMessage *message, EmpathyContact *contact)
524 {
525         EmpathyMessagePriv *priv;
526         EmpathyContact     *old_receiver;
527
528         g_return_if_fail (EMPATHY_IS_MESSAGE (message));
529         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
530
531         priv = GET_PRIV (message);
532
533         old_receiver = priv->receiver;
534         priv->receiver = g_object_ref (contact);
535
536         if (old_receiver) {
537                 g_object_unref (old_receiver);
538         }
539
540         g_object_notify (G_OBJECT (message), "receiver");
541 }
542
543 const gchar *
544 empathy_message_get_token (EmpathyMessage *message)
545 {
546         EmpathyMessagePriv *priv;
547
548         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
549
550         priv = GET_PRIV (message);
551
552         return priv->token;
553 }
554
555 const gchar *
556 empathy_message_get_supersedes (EmpathyMessage *message)
557 {
558         EmpathyMessagePriv *priv;
559
560         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
561
562         priv = GET_PRIV (message);
563
564         return priv->supersedes;
565 }
566
567 gboolean
568 empathy_message_is_edit (EmpathyMessage *message)
569 {
570         EmpathyMessagePriv *priv;
571
572         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
573
574         priv = GET_PRIV (message);
575
576         return !tp_str_empty (priv->supersedes);
577 }
578
579 const gchar *
580 empathy_message_get_body (EmpathyMessage *message)
581 {
582         EmpathyMessagePriv *priv;
583
584         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
585
586         priv = GET_PRIV (message);
587
588         return priv->body;
589 }
590
591 gint64
592 empathy_message_get_timestamp (EmpathyMessage *message)
593 {
594         EmpathyMessagePriv *priv;
595
596         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
597
598         priv = GET_PRIV (message);
599
600         return priv->timestamp;
601 }
602
603 gint64
604 empathy_message_get_original_timestamp (EmpathyMessage *message)
605 {
606         EmpathyMessagePriv *priv;
607
608         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
609
610         priv = GET_PRIV (message);
611
612         return priv->original_timestamp;
613 }
614
615 gboolean
616 empathy_message_is_backlog (EmpathyMessage *message)
617 {
618         EmpathyMessagePriv *priv;
619
620         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
621
622         priv = GET_PRIV (message);
623
624         return priv->is_backlog;
625 }
626
627 TpChannelTextMessageType
628 empathy_message_type_from_str (const gchar *type_str)
629 {
630         if (strcmp (type_str, "normal") == 0) {
631                 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
632         }
633         if (strcmp (type_str, "action") == 0) {
634                 return TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION;
635         }
636         else if (strcmp (type_str, "notice") == 0) {
637                 return TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE;
638         }
639         else if (strcmp (type_str, "auto-reply") == 0) {
640                 return TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY;
641         }
642
643         return TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
644 }
645
646 const gchar *
647 empathy_message_type_to_str (TpChannelTextMessageType type)
648 {
649         switch (type) {
650         case TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION:
651                 return "action";
652         case TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE:
653                 return "notice";
654         case TP_CHANNEL_TEXT_MESSAGE_TYPE_AUTO_REPLY:
655                 return "auto-reply";
656         case TP_CHANNEL_TEXT_MESSAGE_TYPE_DELIVERY_REPORT:
657                 return "delivery-report";
658         case TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL:
659         default:
660                 return "normal";
661         }
662 }
663
664 gboolean
665 empathy_message_is_incoming (EmpathyMessage *message)
666 {
667         EmpathyMessagePriv *priv = GET_PRIV (message);
668
669         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
670
671         return priv->incoming;
672 }
673
674 gboolean
675 empathy_message_equal (EmpathyMessage *message1, EmpathyMessage *message2)
676 {
677         EmpathyMessagePriv *priv1;
678         EmpathyMessagePriv *priv2;
679
680         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message1), FALSE);
681         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message2), FALSE);
682
683         priv1 = GET_PRIV (message1);
684         priv2 = GET_PRIV (message2);
685
686         if (priv1->timestamp == priv2->timestamp &&
687                         !tp_strdiff (priv1->body, priv2->body)) {
688                 return TRUE;
689         }
690
691         return FALSE;
692 }
693
694 TpChannelTextMessageFlags
695 empathy_message_get_flags (EmpathyMessage *self)
696 {
697         EmpathyMessagePriv *priv = GET_PRIV (self);
698
699         g_return_val_if_fail (EMPATHY_IS_MESSAGE (self), 0);
700
701         return priv->flags;
702 }
703
704 EmpathyMessage *
705 empathy_message_new_from_tp_message (TpMessage *tp_msg,
706                                      gboolean incoming)
707 {
708         EmpathyMessage *message;
709         gchar *body;
710         TpChannelTextMessageFlags flags;
711         gint64 timestamp;
712         gint64 original_timestamp;
713         const GHashTable *part = tp_message_peek (tp_msg, 0);
714         gboolean is_backlog;
715
716         g_return_val_if_fail (TP_IS_MESSAGE (tp_msg), NULL);
717
718         body = tp_message_to_text (tp_msg, &flags);
719
720         timestamp = tp_message_get_sent_timestamp (tp_msg);
721         if (timestamp == 0)
722                 timestamp = tp_message_get_received_timestamp (tp_msg);
723
724         original_timestamp = tp_asv_get_int64 (part,
725                 "original-message-received", NULL);
726
727         is_backlog = (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) ==
728                 TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK;
729
730         message = g_object_new (EMPATHY_TYPE_MESSAGE,
731                 "body", body,
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,
737                 "flags", flags,
738                 "is-backlog", is_backlog,
739                 "incoming", incoming,
740                 "tp-message", tp_msg,
741                 NULL);
742
743         g_free (body);
744         return message;
745 }