summaryrefslogtreecommitdiff
path: root/TelepathyQt/message.cpp
blob: 2538f41c0a2da11542d2f911aa05e9ff8407965e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
/**
 * This file is part of TelepathyQt
 *
 * @copyright Copyright (C) 2009 Collabora Ltd. <http://www.collabora.co.uk/>
 * @copyright Copyright (C) 2009 Nokia Corporation
 * @license LGPL 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <TelepathyQt/Message>
#include <TelepathyQt/ReceivedMessage>

#include "TelepathyQt/debug-internal.h"

#include <TelepathyQt/TextChannel>

#include <QDateTime>
#include <QSet>

namespace Tp
{

namespace
{

QVariant valueFromPart(const MessagePartList &parts, uint index, const char *key)
{
    return parts.at(index).value(QLatin1String(key)).variant();
}

uint uintOrZeroFromPart(const MessagePartList &parts, uint index, const char *key)
{
    return valueFromPart(parts, index, key).toUInt();
}

QString stringOrEmptyFromPart(const MessagePartList &parts, uint index, const char *key)
{
    QString s = valueFromPart(parts, index, key).toString();
    if (s.isNull()) {
        s = QLatin1String("");
    }
    return s;
}

bool booleanFromPart(const MessagePartList &parts, uint index, const char *key,
            bool assumeIfAbsent)
{
    QVariant v = valueFromPart(parts, index, key);
    if (v.isValid() && v.type() == QVariant::Bool) {
        return v.toBool();
    }
    return assumeIfAbsent;
}

MessagePartList partsFromPart(const MessagePartList &parts, uint index, const char *key)
{
    return qdbus_cast<MessagePartList>(valueFromPart(parts, index, key));
}

bool partContains(const MessagePartList &parts, uint index, const char *key)
{
    return parts.at(index).contains(QLatin1String(key));
}

}

struct TP_QT_NO_EXPORT Message::Private : public QSharedData
{
    Private(const MessagePartList &parts);
    ~Private();

    uint senderHandle() const;
    QString senderId() const;
    uint pendingId() const;
    void clearSenderHandle();

    MessagePartList parts;

    // if the Text interface says "non-text" we still only have the text,
    // because the interface can't tell us anything else...
    bool forceNonText;

    // for received messages only
    WeakPtr<TextChannel> textChannel;
    ContactPtr sender;
};

Message::Private::Private(const MessagePartList &parts)
    : parts(parts),
      forceNonText(false),
      sender(nullptr)
{
}

Message::Private::~Private()
{
}

inline uint Message::Private::senderHandle() const
{
    return uintOrZeroFromPart(parts, 0, "message-sender");
}

inline QString Message::Private::senderId() const
{
    return stringOrEmptyFromPart(parts, 0, "message-sender-id");
}

inline uint Message::Private::pendingId() const
{
    return uintOrZeroFromPart(parts, 0, "pending-message-id");
}

void Message::Private::clearSenderHandle()
{
    parts[0].remove(QLatin1String("message-sender"));
}

/**
 * \class Message
 * \ingroup clientchannel
 * \headerfile TelepathyQt/message.h <TelepathyQt/Message>
 *
 * \brief The Message class represents a Telepathy message in a TextChannel.
 *
 * This class is implicitly shared, like QString.
 */

/**
 * \internal Default constructor.
 */
Message::Message()
    : mPriv(new Private(MessagePartList()))
{
}

/**
 * Construct a new Message object.
 *
 * \param parts The parts of a message as defined by the \telepathy_spec.
 *              This list must have length at least 1.
 */
Message::Message(const MessagePartList &parts)
    : mPriv(new Private(parts))
{
    Q_ASSERT(parts.size() > 0);
}

/**
 * Construct a new Message object.
 *
 * \param timestamp The time the message was sent.
 * \param type The message type.
 * \param text The message body.
 */
Message::Message(uint timestamp, uint type, const QString &text)
    : mPriv(new Private(MessagePartList() << MessagePart() << MessagePart()))
{
    mPriv->parts[0].insert(QLatin1String("message-sent"),
            QDBusVariant(static_cast<qlonglong>(timestamp)));
    mPriv->parts[0].insert(QLatin1String("message-type"),
            QDBusVariant(type));

    mPriv->parts[1].insert(QLatin1String("content-type"),
            QDBusVariant(QLatin1String("text/plain")));
    mPriv->parts[1].insert(QLatin1String("content"), QDBusVariant(text));
}

/**
 * Construct a new Message object.
 *
 * \param type The message type.
 * \param text The message body.
 */
Message::Message(ChannelTextMessageType type, const QString &text)
    : mPriv(new Private(MessagePartList() << MessagePart() << MessagePart()))
{
    mPriv->parts[0].insert(QLatin1String("message-type"),
            QDBusVariant(static_cast<uint>(type)));

    mPriv->parts[1].insert(QLatin1String("content-type"),
            QDBusVariant(QLatin1String("text/plain")));
    mPriv->parts[1].insert(QLatin1String("content"), QDBusVariant(text));
}

/**
 * Copy constructor.
 */
Message::Message(const Message &other)
    : mPriv(other.mPriv)
{
}

/**
 * Assignment operator.
 */
Message &Message::operator=(const Message &other)
{
    if (this != &other) {
        mPriv = other.mPriv;
    }

    return *this;
}

/**
 * Equality operator.
 */
bool Message::operator==(const Message &other) const
{
    return this->mPriv == other.mPriv;
}

/**
 * Class destructor.
 */
Message::~Message()
{
}

/**
 * Return the time the message was sent, or QDateTime() if that time is
 * unknown.
 *
 * \return The timestamp as QDateTime.
 */
QDateTime Message::sent() const
{
    // FIXME See http://bugs.freedesktop.org/show_bug.cgi?id=21690
    uint stamp = valueFromPart(mPriv->parts, 0, "message-sent").toUInt();
    if (stamp != 0) {
#if QT_VERSION < QT_VERSION_CHECK(5, 8, 0)
        return QDateTime::fromTime_t(stamp);
#else
        return QDateTime::fromSecsSinceEpoch(stamp);
#endif
    } else {
        return QDateTime();
    }
}

/**
 * Return the type of this message, or #ChannelTextMessageTypeNormal
 * if the type is not recognised.
 *
 * \return The type as #ChannelTextMessageType.
 */
ChannelTextMessageType Message::messageType() const
{
    uint raw = valueFromPart(mPriv->parts, 0, "message-type").toUInt();

    if (raw < static_cast<uint>(NUM_CHANNEL_TEXT_MESSAGE_TYPES)) {
        return ChannelTextMessageType(raw);
    } else {
        return ChannelTextMessageTypeNormal;
    }
}

/**
 * Return whether this message was truncated during delivery.
 *
 * \return \c true if truncated, \c false otherwise.
 */
bool Message::isTruncated() const
{
    for (int i = 1; i < size(); i++) {
        if (booleanFromPart(mPriv->parts, i, "truncated", false)) {
            return true;
        }
    }
    return false;
}

/**
 * Return whether this message contains parts not representable as plain
 * text.
 *
 * \return \c true if it cannot completely be represented as plain text, \c false
 *         otherwise.
 */
bool Message::hasNonTextContent() const
{
    if (mPriv->forceNonText || size() <= 1 || isSpecificToDBusInterface()) {
        return true;
    }

    QSet<QString> texts;
    QSet<QString> textNeeded;

    for (int i = 1; i < size(); i++) {
        QString altGroup = stringOrEmptyFromPart(mPriv->parts, i, "alternative");
        QString contentType = stringOrEmptyFromPart(mPriv->parts, i, "content-type");

        if (contentType == QLatin1String("text/plain")) {
            if (!altGroup.isEmpty()) {
                // we can use this as an alternative for a non-text part
                // with the same altGroup
                texts << altGroup;
            }
        } else {
            QString alt = stringOrEmptyFromPart(mPriv->parts, i, "alternative");
            if (altGroup.isEmpty()) {
                // we can't possibly rescue this part by using a text/plain
                // alternative, because it's not in any alternative group
                return true;
            } else {
                // maybe we'll find a text/plain alternative for this
                textNeeded << altGroup;
            }
        }
    }

    textNeeded -= texts;
    return !textNeeded.isEmpty();
}

/**
 * Return the unique token identifying this message (e.g. the id attribute
 * for XMPP messages), or an empty string if there is no suitable token.
 *
 * \return The non-empty message identifier, or an empty string if none.
 */
QString Message::messageToken() const
{
    return stringOrEmptyFromPart(mPriv->parts, 0, "message-token");
}

/**
 * Return whether this message is specific to a D-Bus interface. This is
 * \c false in almost all cases.
 *
 * If this function returns \c true, the message is specific to the interface
 * indicated by dbusInterface(). Clients that don't understand that interface
 * should not display the message. However, if the client would acknowledge
 * an ordinary message, it must also acknowledge this interface-specific
 * message.
 *
 * \return \c true if dbusInterface() would return a non-empty string, \c false otherwise.
 * \sa dbusInterface()
 */
bool Message::isSpecificToDBusInterface() const
{
    return !dbusInterface().isEmpty();
}

/**
 * Return the D-Bus interface to which this message is specific, or an
 * empty string for normal messages.
 *
 * \return The D-Bus interface name, or an empty string.
 * \sa isSpecificToDBusInterface()
 */
QString Message::dbusInterface() const
{
    return stringOrEmptyFromPart(mPriv->parts, 0, "interface");
}

/**
 * Return the message body containing all "text/plain" parts.
 *
 * \return The body text.
 */
QString Message::text() const
{
    // Alternative-groups for which we've already emitted an alternative
    QSet<QString> altGroupsUsed;
    QString text;

    for (int i = 1; i < size(); i++) {
        const QString contentType = stringOrEmptyFromPart(mPriv->parts, i, "content-type");

        if (contentType == QLatin1String("text/plain")) {
            const QString interface = valueFromPart(mPriv->parts, i, "interface").toString();
            if (!interface.isEmpty()) {
                continue;
            }
            const QString altGroup = stringOrEmptyFromPart(mPriv->parts, i, "alternative");
            if (!altGroup.isEmpty()) {
                if (altGroupsUsed.contains(altGroup)) {
                    continue;
                } else {
                    altGroupsUsed << altGroup;
                }
            }

            QVariant content = valueFromPart(mPriv->parts, i, "content");
            if (content.type() == QVariant::String) {
                text += content.toString();
            } else {
                // O RLY?
                debug() << "allegedly text/plain part wasn't";
            }
        }
    }

    return text;
}

/**
 * Return the message's header part, as defined by the \telepathy_spec.
 *
 * This is provided for advanced clients that need to access
 * additional information not available through the normal Message API.
 *
 * \return The header as a MessagePart object. The same thing as part(0).
 */
MessagePart Message::header() const
{
    return part(0);
}

/**
 * Return the number of parts in this message.
 *
 * \return 1 greater than the largest valid argument to part().
 * \sa part(), parts()
 */
int Message::size() const
{
    return mPriv->parts.size();
}

/**
 * Return the message's part for \a index, as defined by the \telepathy_spec.
 *
 * This is provided for advanced clients that need to access
 * additional information not available through the normal Message API.
 *
 * \param index The part to access, which must be strictly less than size();
 *              part number 0 is the header, parts numbered 1 or greater
 *              are the body of the message.
 * \return A MessagePart object.
 */
MessagePart Message::part(uint index) const
{
    return mPriv->parts.at(index);
}

/**
 * Return the list of message parts forming this message.
 *
 * \return The list of MessagePart objects.
 */
MessagePartList Message::parts() const
{
    return mPriv->parts;
}

/**
 * \class ReceivedMessage
 * \ingroup clientchannel
 * \headerfile TelepathyQt/message.h <TelepathyQt/ReceivedMessage>
 *
 * \brief The ReceivedMessage class is a subclass of Message, representing a
 * received message only.
 *
 * It contains additional information that's generally only
 * available on received messages.
 */

/**
 * \class ReceivedMessage::DeliveryDetails
 * \ingroup clientchannel
 * \headerfile TelepathyQt/message.h <TelepathyQt/ReceivedMessage>
 *
 * \brief The ReceivedMessage::DeliveryDetails class represents the details of a delivery report.
 */

struct TP_QT_NO_EXPORT ReceivedMessage::DeliveryDetails::Private : public QSharedData
{
    Private(const MessagePartList &parts)
        : parts(parts)
    {
    }

    MessagePartList parts;
};

/**
 * Default constructor.
 */
ReceivedMessage::DeliveryDetails::DeliveryDetails()
{
}

/**
 * Copy constructor.
 */
ReceivedMessage::DeliveryDetails::DeliveryDetails(const DeliveryDetails &other)
    : mPriv(other.mPriv)
{
}

/**
 * Construct a new ReceivedMessage::DeliveryDetails object.
 *
 * \param The message parts.
 */
ReceivedMessage::DeliveryDetails::DeliveryDetails(const MessagePartList &parts)
    : mPriv(new Private(parts))
{
}

/**
 * Class destructor.
 */
ReceivedMessage::DeliveryDetails::~DeliveryDetails()
{
}

/**
 * Assignment operator.
 */
ReceivedMessage::DeliveryDetails &ReceivedMessage::DeliveryDetails::operator=(
        const DeliveryDetails &other)
{
    this->mPriv = other.mPriv;
    return *this;
}

/**
 * Return the delivery status of a message.
 *
 * \return The delivery status as #DeliveryStatus.
 */
DeliveryStatus ReceivedMessage::DeliveryDetails::status() const
{
    if (!isValid()) {
        return DeliveryStatusUnknown;
    }
    return static_cast<DeliveryStatus>(uintOrZeroFromPart(mPriv->parts, 0, "delivery-status"));
}

/**
 * Return whether this delivery report contains an identifier for the message to which it
 * refers.
 *
 * \return \c true if an original message token is known, \c false otherwise.
 * \sa originalToken()
 */
bool ReceivedMessage::DeliveryDetails::hasOriginalToken() const
{
    if (!isValid()) {
        return false;
    }
    return partContains(mPriv->parts, 0, "delivery-token");
}

/**
 * Return an identifier for the message to which this delivery report refers, or an empty string if
 * hasOriginalToken() returns \c false.
 *
 * Clients may match this against the token produced by the TextChannel::send() method and
 * TextChannel::messageSent() signal. A status report with no token could match any sent message,
 * and a sent message with an empty token could match any status report.
 * If multiple sent messages match, clients should use some reasonable heuristic.
 *
 * \return The message token if hasOriginalToken() returns \c true, an empty string otherwise.
 * \sa hasOriginalToken().
 */
QString ReceivedMessage::DeliveryDetails::originalToken() const
{
    if (!isValid()) {
        return QString();
    }
    return stringOrEmptyFromPart(mPriv->parts, 0, "delivery-token");
}

/**
 * Return whether the delivery of the message this delivery report refers to, failed.
 *
 * \return \c true if the message delivery failed, \c false otherwise.
 * \sa error()
 */
bool ReceivedMessage::DeliveryDetails::isError() const
{
    if (!isValid()) {
        return false;
    }
    DeliveryStatus st(status());
    return st == DeliveryStatusTemporarilyFailed || st == DeliveryStatusPermanentlyFailed;
}

/**
 * Return the reason for the delivery failure if known.
 *
 * \return The reason as #ChannelTextSendError.
 * \sa isError()
 */
ChannelTextSendError ReceivedMessage::DeliveryDetails::error() const
{
    if (!isValid()) {
        return ChannelTextSendErrorUnknown;
    }
    return static_cast<ChannelTextSendError>(uintOrZeroFromPart(mPriv->parts, 0, "delivery-error"));
}

/**
 * Return whether this delivery report contains a debugging information on why the message it refers
 * to could not be delivered.
 *
 * \return \c true if a debugging information is provided, \c false otherwise.
 * \sa debugMessage()
 */
bool ReceivedMessage::DeliveryDetails::hasDebugMessage() const
{
    if (!isValid()) {
        return false;
    }
    return partContains(mPriv->parts, 0, "delivery-error-message");
}

/**
 * Return the debugging information on why the message this delivery report refers to could not be
 * delivered.
 *
 * \return The debug string.
 * \sa hasDebugMessage()
 */
QString ReceivedMessage::DeliveryDetails::debugMessage() const
{
    if (!isValid()) {
        return QString();
    }
    return stringOrEmptyFromPart(mPriv->parts, 0, "delivery-error-message");
}

/**
 * Return the reason for the delivery failure if known, specified as a
 * (possibly implementation-specific) D-Bus error.
 *
 * \return The D-Bus error string representing the error.
 */
QString ReceivedMessage::DeliveryDetails::dbusError() const
{
    if (!isValid()) {
        return QString();
    }
    QString ret = stringOrEmptyFromPart(mPriv->parts, 0, "delivery-dbus-error");
    if (ret.isEmpty()) {
        switch (error()) {
            case ChannelTextSendErrorOffline:
                ret = TP_QT_ERROR_OFFLINE;
                break;
            case ChannelTextSendErrorInvalidContact:
                ret = TP_QT_ERROR_DOES_NOT_EXIST;
                break;
            case ChannelTextSendErrorPermissionDenied:
                ret = TP_QT_ERROR_PERMISSION_DENIED;
                break;
            case ChannelTextSendErrorTooLong:
                ret = TP_QT_ERROR_INVALID_ARGUMENT;
                break;
            case ChannelTextSendErrorNotImplemented:
                ret = TP_QT_ERROR_NOT_IMPLEMENTED;
                break;
            default:
                ret = TP_QT_ERROR_NOT_AVAILABLE;
        }
    }
    return ret;
}

/**
 * Return whether the message content for the message this delivery report refers to is known.
 *
 * \return \c true if the original message content is known, \c false otherwise.
 * \sa echoedMessage()
 */
bool ReceivedMessage::DeliveryDetails::hasEchoedMessage() const
{
    if (!isValid()) {
        return false;
    }
    return partContains(mPriv->parts, 0, "delivery-echo");
}

/**
 * Return the Message object for the message this delivery report refers to, omitted if the message
 * is unknown.
 *
 * <div class='rationale'>
 *   <h5>Rationale:</h5>
 *   <div>
 *   Some protocols, like XMPP, echo the failing message back to the sender. This is sometimes the
 *   only way to match it against the sent message, so we include it here.
 *   </div>
 * </div>
 *
 * \return The Message object, or an empty Message object if hasEchoedMessage()
 *         returns \c false.
 * \sa hasEchoedMessage()
 */
Message ReceivedMessage::DeliveryDetails::echoedMessage() const
{
    if (!isValid()) {
        return Message();
    }
    return Message(partsFromPart(mPriv->parts, 0, "delivery-echo"));
}

/**
 * \internal Default constructor.
 */
ReceivedMessage::ReceivedMessage()
{
}

/**
 * Construct a new ReceivedMessage object.
 *
 * \param parts The parts of a message as defined by the \telepathy_spec.
 *              This list must have length at least 1.
 * \param channel The channel owning this message.
 */
ReceivedMessage::ReceivedMessage(const MessagePartList &parts,
        const TextChannelPtr &channel)
    : Message(parts)
{
    if (!mPriv->parts[0].contains(QLatin1String("message-received"))) {
        mPriv->parts[0].insert(QLatin1String("message-received"),
                QDBusVariant(static_cast<qlonglong>(
#if QT_VERSION < QT_VERSION_CHECK(5, 8, 0)
                        QDateTime::currentDateTime().toTime_t())));
#else
                        QDateTime::currentDateTime().toSecsSinceEpoch())));
#endif
    }
    mPriv->textChannel = channel;
}

/**
 * Copy constructor.
 */
ReceivedMessage::ReceivedMessage(const ReceivedMessage &other)
    : Message(other)
{
}

/**
 * Assignment operator.
 */
ReceivedMessage &ReceivedMessage::operator=(const ReceivedMessage &other)
{
    if (this != &other) {
        mPriv = other.mPriv;
    }

    return *this;
}

/**
 * Class destructor.
 */
ReceivedMessage::~ReceivedMessage()
{
}

/**
 * Return the time the message was received.
 *
 * \return The timestamp as QDateTime, or QDateTime() if unknown.
 */
QDateTime ReceivedMessage::received() const
{
    // FIXME See http://bugs.freedesktop.org/show_bug.cgi?id=21690
    uint stamp = valueFromPart(mPriv->parts, 0, "message-received").toUInt();
    if (stamp != 0) {
#if QT_VERSION < QT_VERSION_CHECK(5, 8, 0)
        return QDateTime::fromTime_t(stamp);
#else
        return QDateTime::fromSecsSinceEpoch(stamp);
#endif
    } else {
        return QDateTime();
    }
}

/**
 * Return the contact who sent the message.
 *
 * \return A pointer to the Contact object.
 * \sa senderNickname()
 */
ContactPtr ReceivedMessage::sender() const
{
    return mPriv->sender;
}

/**
 * Return the nickname chosen by the sender of the message, which can be different for each
 * message in a conversation.
 *
 * \return The nickname.
 * \sa sender()
 */
QString ReceivedMessage::senderNickname() const
{
    QString ret = stringOrEmptyFromPart(mPriv->parts, 0, "sender-nickname");
    if (ret.isEmpty() && mPriv->sender) {
        ret = mPriv->sender->alias();
    }
    return ret;
}

/**
 * If this message replaces a previous message, return the value of
 * messageToken() for that previous message. Otherwise, return an empty string.
 *
 * For instance, a user interface could replace the superseded
 * message with this message, or grey out the superseded message.
 *
 * \return The message token of the superseded message or an empty string if none.
 */
QString ReceivedMessage::supersededToken() const
{
    return stringOrEmptyFromPart(mPriv->parts, 0, "supersedes");
}

/**
 * Return whether the incoming message was part of a replay of message
 * history.
 *
 * If \c true, loggers can use this to improve their heuristics for elimination
 * of duplicate messages (a simple, correct implementation would be to avoid
 * logging any message that has this flag).
 *
 * \return \c true if the scrollback flag is set, \c false otherwise.
 */
bool ReceivedMessage::isScrollback() const
{
    return booleanFromPart(mPriv->parts, 0, "scrollback", false);
}

/**
 * Return whether the incoming message was seen in a previous channel during
 * the lifetime of the connection, but was not acknowledged before that
 * channel closed, causing the channel in which it now appears to open.
 *
 * If \c true, loggers should not log this message again.
 *
 * \return \c true if the rescued flag is set, \c false otherwise.
 */
bool ReceivedMessage::isRescued() const
{
    return booleanFromPart(mPriv->parts, 0, "rescued", false);
}

/**
 * Return whether the incoming message should trigger a user notification.
 *
 * If \c true, UI should not notify the user about this message.
 *
 * \return \c true if the silent flag is set, \c false otherwise.
 */
bool ReceivedMessage::isSilent() const
{
    return booleanFromPart(mPriv->parts, 0, "silent", false);
}

/**
 * Return whether the incoming message is a delivery report.
 *
 * \return \c true if a delivery report, \c false otherwise.
 * \sa deliveryDetails()
 */
bool ReceivedMessage::isDeliveryReport() const
{
    return messageType() == ChannelTextMessageTypeDeliveryReport;
}

/**
 * Return the details of a delivery report.
 *
 * This method should only be used if isDeliveryReport() returns \c true.
 *
 * \return The delivery report as a ReceivedMessage::DeliveryDetails object.
 * \sa isDeliveryReport()
 */
ReceivedMessage::DeliveryDetails ReceivedMessage::deliveryDetails() const
{
    return DeliveryDetails(parts());
}

/**
 * Return whether this message is from \a channel.
 *
 * \return \c true if the message is from \a channel, \c false otherwise.
 */
bool ReceivedMessage::isFromChannel(const TextChannelPtr &channel) const
{
    return TextChannelPtr(mPriv->textChannel) == channel;
}

uint ReceivedMessage::pendingId() const
{
    return mPriv->pendingId();
}

uint ReceivedMessage::senderHandle() const
{
    return mPriv->senderHandle();
}

QString ReceivedMessage::senderId() const
{
    return mPriv->senderId();
}

void ReceivedMessage::setForceNonText()
{
    mPriv->forceNonText = true;
}

void ReceivedMessage::clearSenderHandle()
{
    mPriv->clearSenderHandle();
}

void ReceivedMessage::setSender(const ContactPtr &sender)
{
    mPriv->sender = sender;
}

} // Tp