Mailcommon

expirecollectionattribute.cpp
1/*
2
3 SPDX-FileCopyrightText: 2011-2024 Laurent Montel <montel@kde.org>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6*/
7
8#include "expirecollectionattribute.h"
9#include <QDataStream>
10#include <QIODevice>
11
12using namespace MailCommon;
13
14ExpireCollectionAttribute::ExpireCollectionAttribute() = default;
15
16QByteArray ExpireCollectionAttribute::type() const
17{
18 static const QByteArray sType("expirationcollectionattribute");
19 return sType;
20}
21
22ExpireCollectionAttribute *ExpireCollectionAttribute::clone() const
23{
24 auto expireAttr = new ExpireCollectionAttribute();
25 expireAttr->setAutoExpire(mExpireMessages);
26 expireAttr->setUnreadExpireAge(mUnreadExpireAge);
27 expireAttr->setUnreadExpireUnits(mUnreadExpireUnits);
28 expireAttr->setReadExpireAge(mReadExpireAge);
29 expireAttr->setReadExpireUnits(mReadExpireUnits);
30 expireAttr->setExpireAction(mExpireAction);
31 expireAttr->setExpireToFolderId(mExpireToFolderId);
32 expireAttr->setExpireMessagesWithValidDate(mExpireMessagesWithValidDate);
33 return expireAttr;
34}
35
36void ExpireCollectionAttribute::setAutoExpire(bool enabled)
37{
38 mExpireMessages = enabled;
39}
40
41bool ExpireCollectionAttribute::isAutoExpire() const
42{
43 return mExpireMessages;
44}
45
46void ExpireCollectionAttribute::setUnreadExpireAge(int age)
47{
48 if (age >= 0 && age != mUnreadExpireAge) {
49 mUnreadExpireAge = age;
50 }
51}
52
53int ExpireCollectionAttribute::unreadExpireAge() const
54{
55 return mUnreadExpireAge;
56}
57
58void ExpireCollectionAttribute::setUnreadExpireUnits(ExpireUnits units)
59{
60 if (units >= ExpireNever && units < ExpireMaxUnits) {
61 mUnreadExpireUnits = units;
62 }
63}
64
65void ExpireCollectionAttribute::setReadExpireAge(int age)
66{
67 if (age >= 0 && age != mReadExpireAge) {
68 mReadExpireAge = age;
69 }
70}
71
72int ExpireCollectionAttribute::readExpireAge() const
73{
74 return mReadExpireAge;
75}
76
77void ExpireCollectionAttribute::setReadExpireUnits(ExpireUnits units)
78{
79 if (units >= ExpireNever && units <= ExpireMaxUnits) {
80 mReadExpireUnits = units;
81 }
82}
83
84void ExpireCollectionAttribute::setExpireAction(ExpireAction a)
85{
86 mExpireAction = a;
87}
88
89ExpireCollectionAttribute::ExpireAction ExpireCollectionAttribute::expireAction() const
90{
91 return mExpireAction;
92}
93
94void ExpireCollectionAttribute::setExpireToFolderId(Akonadi::Collection::Id id)
95{
96 mExpireToFolderId = id;
97}
98
99Akonadi::Collection::Id ExpireCollectionAttribute::expireToFolderId() const
100{
101 return mExpireToFolderId;
102}
103
104ExpireCollectionAttribute::ExpireUnits ExpireCollectionAttribute::unreadExpireUnits() const
105{
106 return mUnreadExpireUnits;
107}
108
109ExpireCollectionAttribute::ExpireUnits ExpireCollectionAttribute::readExpireUnits() const
110{
111 return mReadExpireUnits;
112}
113
114bool ExpireCollectionAttribute::operator==(const ExpireCollectionAttribute &other) const
115{
116 return (mExpireMessages == other.isAutoExpire()) && (mUnreadExpireAge == other.unreadExpireAge()) && (mReadExpireAge == other.readExpireAge())
117 && (mUnreadExpireUnits == other.unreadExpireUnits()) && (mReadExpireUnits == other.readExpireUnits()) && (mExpireAction == other.expireAction())
118 && (mExpireToFolderId == other.expireToFolderId()) && (mExpireMessagesWithValidDate == other.expireMessagesWithValidDate());
119}
120
121int ExpireCollectionAttribute::daysToExpire(int number, ExpireCollectionAttribute::ExpireUnits units)
122{
123 switch (units) {
124 case ExpireCollectionAttribute::ExpireDays: // Days
125 return number;
126 case ExpireCollectionAttribute::ExpireWeeks: // Weeks
127 return number * 7;
128 case ExpireCollectionAttribute::ExpireMonths: // Months - this could be better
129 // rather than assuming 31day months.
130 return number * 31;
131 default: // this avoids a compiler warning (not handled enumeration values)
132 ;
133 }
134 return -1;
135}
136
137bool ExpireCollectionAttribute::expireMessagesWithValidDate() const
138{
139 return mExpireMessagesWithValidDate;
140}
141
142void ExpireCollectionAttribute::setExpireMessagesWithValidDate(bool expireMessagesWithValidDate)
143{
144 mExpireMessagesWithValidDate = expireMessagesWithValidDate;
145}
146
147void ExpireCollectionAttribute::daysToExpire(int &unreadDays, int &readDays) const
148{
149 unreadDays = ExpireCollectionAttribute::daysToExpire(unreadExpireAge(), unreadExpireUnits());
150 readDays = ExpireCollectionAttribute::daysToExpire(readExpireAge(), readExpireUnits());
151}
152
153QByteArray ExpireCollectionAttribute::serialized() const
154{
155 QByteArray result;
157
158 s << mExpireToFolderId;
159 s << static_cast<int>(mExpireAction);
160 s << static_cast<int>(mReadExpireUnits);
161 s << mReadExpireAge;
162 s << static_cast<int>(mUnreadExpireUnits);
163 s << mUnreadExpireAge;
164 s << mExpireMessages;
165 s << mExpireMessagesWithValidDate;
166
167 return result;
168}
169
170void ExpireCollectionAttribute::deserialize(const QByteArray &data)
171{
172 QDataStream s(data);
173 s >> mExpireToFolderId;
174 int action;
175 s >> action;
176 mExpireAction = static_cast<ExpireCollectionAttribute::ExpireAction>(action);
177 int valUnitRead;
178 s >> valUnitRead;
179 mReadExpireUnits = static_cast<ExpireCollectionAttribute::ExpireUnits>(valUnitRead);
180 s >> mReadExpireAge;
181 int valUnitUnread;
182 s >> valUnitUnread;
183 mUnreadExpireUnits = static_cast<ExpireCollectionAttribute::ExpireUnits>(valUnitUnread);
184 s >> mUnreadExpireAge;
185 s >> mExpireMessages;
186 s >> mExpireMessagesWithValidDate;
187}
188
189QDebug operator<<(QDebug d, const ExpireCollectionAttribute &t)
190{
191 d << " mExpireMessages " << t.isAutoExpire();
192 d << " mUnreadExpireAge " << t.unreadExpireAge();
193 d << " mReadExpireAge " << t.readExpireAge();
194 d << " mUnreadExpireUnits " << t.unreadExpireUnits();
195 d << " mReadExpireUnits " << t.readExpireUnits();
196 d << " mExpireAction " << t.expireAction();
197 d << " mExpireToFolderId " << t.expireToFolderId();
198 d << " mExpireMessagesWithValidDate " << t.expireMessagesWithValidDate();
199 return d;
200}
QDebug operator<<(QDebug dbg, const DcrawInfoContainer &c)
KIOCORE_EXPORT QString number(KIO::filesize_t size)
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:24 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.