1 /*
2     SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
3 
4     SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "sentbehaviourattribute.h"
8 
9 using namespace Akonadi;
10 using namespace MailTransport;
11 
12 class MailTransport::SentBehaviourAttributePrivate
13 {
14 public:
SentBehaviourAttributePrivate()15     SentBehaviourAttributePrivate()
16     {
17     }
18 
19     SentBehaviourAttribute::SentBehaviour mBehaviour = SentBehaviourAttribute::MoveToDefaultSentCollection;
20     Akonadi::Collection mMoveToCollection;
21     bool mSilent = false;
22 };
23 
SentBehaviourAttribute(SentBehaviour beh,const Collection & moveToCollection,bool sendSilently)24 SentBehaviourAttribute::SentBehaviourAttribute(SentBehaviour beh, const Collection &moveToCollection, bool sendSilently)
25     : d(new SentBehaviourAttributePrivate)
26 {
27     d->mBehaviour = beh;
28     d->mMoveToCollection = moveToCollection;
29     d->mSilent = sendSilently;
30 }
31 
32 SentBehaviourAttribute::~SentBehaviourAttribute() = default;
33 
clone() const34 SentBehaviourAttribute *SentBehaviourAttribute::clone() const
35 {
36     return new SentBehaviourAttribute(d->mBehaviour, d->mMoveToCollection, d->mSilent);
37 }
38 
type() const39 QByteArray SentBehaviourAttribute::type() const
40 {
41     static const QByteArray sType("SentBehaviourAttribute");
42     return sType;
43 }
44 
serialized() const45 QByteArray SentBehaviourAttribute::serialized() const
46 {
47     QByteArray out;
48 
49     switch (d->mBehaviour) {
50     case Delete:
51         out = QByteArrayLiteral("delete");
52         break;
53     case MoveToCollection:
54         out = QByteArrayLiteral("moveTo") + QByteArray::number(d->mMoveToCollection.id());
55         break;
56     case MoveToDefaultSentCollection:
57         out = QByteArrayLiteral("moveToDefault");
58         break;
59     default:
60         Q_ASSERT(false);
61         return QByteArray();
62     }
63 
64     if (d->mSilent) {
65         out += QByteArrayLiteral(",silent");
66     }
67 
68     return out;
69 }
70 
deserialize(const QByteArray & data)71 void SentBehaviourAttribute::deserialize(const QByteArray &data)
72 {
73     const QByteArrayList in = data.split(',');
74     Q_ASSERT(!in.isEmpty());
75 
76     const QByteArray attr0 = in[0];
77     d->mMoveToCollection = Akonadi::Collection(-1);
78     if (attr0 == "delete") {
79         d->mBehaviour = Delete;
80     } else if (attr0 == "moveToDefault") {
81         d->mBehaviour = MoveToDefaultSentCollection;
82     } else if (attr0.startsWith(QByteArrayLiteral("moveTo"))) {
83         d->mBehaviour = MoveToCollection;
84         d->mMoveToCollection = Akonadi::Collection(attr0.mid(6).toLongLong());
85         // NOTE: 6 is the strlen of "moveTo".
86     } else {
87         Q_ASSERT(false);
88     }
89 
90     if (in.size() == 2 && in[1] == "silent") {
91         d->mSilent = true;
92     }
93 }
94 
sentBehaviour() const95 SentBehaviourAttribute::SentBehaviour SentBehaviourAttribute::sentBehaviour() const
96 {
97     return d->mBehaviour;
98 }
99 
setSentBehaviour(SentBehaviour beh)100 void SentBehaviourAttribute::setSentBehaviour(SentBehaviour beh)
101 {
102     d->mBehaviour = beh;
103 }
104 
moveToCollection() const105 Collection SentBehaviourAttribute::moveToCollection() const
106 {
107     return d->mMoveToCollection;
108 }
109 
setMoveToCollection(const Collection & moveToCollection)110 void SentBehaviourAttribute::setMoveToCollection(const Collection &moveToCollection)
111 {
112     d->mMoveToCollection = moveToCollection;
113 }
114 
sendSilently() const115 bool SentBehaviourAttribute::sendSilently() const
116 {
117     return d->mSilent;
118 }
119 
setSendSilently(bool sendSilently)120 void SentBehaviourAttribute::setSendSilently(bool sendSilently)
121 {
122     d->mSilent = sendSilently;
123 }
124