File indexing completed on 2024-05-12 05:11:12

0001 /*
0002     SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "sentbehaviourattribute.h"
0008 
0009 using namespace Akonadi;
0010 
0011 class Akonadi::SentBehaviourAttributePrivate
0012 {
0013 public:
0014     SentBehaviourAttributePrivate() = default;
0015 
0016     SentBehaviourAttribute::SentBehaviour mBehaviour = SentBehaviourAttribute::MoveToDefaultSentCollection;
0017     Akonadi::Collection mMoveToCollection;
0018     bool mSilent = false;
0019 };
0020 
0021 SentBehaviourAttribute::SentBehaviourAttribute(SentBehaviour beh, const Collection &moveToCollection, bool sendSilently)
0022     : d(new SentBehaviourAttributePrivate)
0023 {
0024     d->mBehaviour = beh;
0025     d->mMoveToCollection = moveToCollection;
0026     d->mSilent = sendSilently;
0027 }
0028 
0029 SentBehaviourAttribute::~SentBehaviourAttribute() = default;
0030 
0031 SentBehaviourAttribute *SentBehaviourAttribute::clone() const
0032 {
0033     return new SentBehaviourAttribute(d->mBehaviour, d->mMoveToCollection, d->mSilent);
0034 }
0035 
0036 QByteArray SentBehaviourAttribute::type() const
0037 {
0038     static const QByteArray sType("SentBehaviourAttribute");
0039     return sType;
0040 }
0041 
0042 QByteArray SentBehaviourAttribute::serialized() const
0043 {
0044     QByteArray out;
0045 
0046     switch (d->mBehaviour) {
0047     case Delete:
0048         out = QByteArrayLiteral("delete");
0049         break;
0050     case MoveToCollection:
0051         out = QByteArrayLiteral("moveTo") + QByteArray::number(d->mMoveToCollection.id());
0052         break;
0053     case MoveToDefaultSentCollection:
0054         out = QByteArrayLiteral("moveToDefault");
0055         break;
0056     default:
0057         Q_ASSERT(false);
0058         return {};
0059     }
0060 
0061     if (d->mSilent) {
0062         out += QByteArrayLiteral(",silent");
0063     }
0064 
0065     return out;
0066 }
0067 
0068 void SentBehaviourAttribute::deserialize(const QByteArray &data)
0069 {
0070     const QByteArrayList in = data.split(',');
0071     Q_ASSERT(!in.isEmpty());
0072 
0073     const QByteArray attr0 = in[0];
0074     d->mMoveToCollection = Akonadi::Collection(-1);
0075     if (attr0 == "delete") {
0076         d->mBehaviour = Delete;
0077     } else if (attr0 == "moveToDefault") {
0078         d->mBehaviour = MoveToDefaultSentCollection;
0079     } else if (attr0.startsWith(QByteArrayLiteral("moveTo"))) {
0080         d->mBehaviour = MoveToCollection;
0081         d->mMoveToCollection = Akonadi::Collection(attr0.mid(6).toLongLong());
0082         // NOTE: 6 is the strlen of "moveTo".
0083     } else {
0084         Q_ASSERT(false);
0085     }
0086 
0087     if (in.size() == 2 && in[1] == "silent") {
0088         d->mSilent = true;
0089     }
0090 }
0091 
0092 SentBehaviourAttribute::SentBehaviour SentBehaviourAttribute::sentBehaviour() const
0093 {
0094     return d->mBehaviour;
0095 }
0096 
0097 void SentBehaviourAttribute::setSentBehaviour(SentBehaviour beh)
0098 {
0099     d->mBehaviour = beh;
0100 }
0101 
0102 Collection SentBehaviourAttribute::moveToCollection() const
0103 {
0104     return d->mMoveToCollection;
0105 }
0106 
0107 void SentBehaviourAttribute::setMoveToCollection(const Collection &moveToCollection)
0108 {
0109     d->mMoveToCollection = moveToCollection;
0110 }
0111 
0112 bool SentBehaviourAttribute::sendSilently() const
0113 {
0114     return d->mSilent;
0115 }
0116 
0117 void SentBehaviourAttribute::setSendSilently(bool sendSilently)
0118 {
0119     d->mSilent = sendSilently;
0120 }