Warning, file /pim/mailcommon/src/collectionpage/attributes/expirecollectionattribute.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 0003 SPDX-FileCopyrightText: 2011-2024 Laurent Montel <montel@kde.org> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "expirecollectionattribute.h" 0009 #include <QDataStream> 0010 #include <QIODevice> 0011 0012 using namespace MailCommon; 0013 0014 ExpireCollectionAttribute::ExpireCollectionAttribute() = default; 0015 0016 QByteArray ExpireCollectionAttribute::type() const 0017 { 0018 static const QByteArray sType("expirationcollectionattribute"); 0019 return sType; 0020 } 0021 0022 ExpireCollectionAttribute *ExpireCollectionAttribute::clone() const 0023 { 0024 auto expireAttr = new ExpireCollectionAttribute(); 0025 expireAttr->setAutoExpire(mExpireMessages); 0026 expireAttr->setUnreadExpireAge(mUnreadExpireAge); 0027 expireAttr->setUnreadExpireUnits(mUnreadExpireUnits); 0028 expireAttr->setReadExpireAge(mReadExpireAge); 0029 expireAttr->setReadExpireUnits(mReadExpireUnits); 0030 expireAttr->setExpireAction(mExpireAction); 0031 expireAttr->setExpireToFolderId(mExpireToFolderId); 0032 expireAttr->setExpireMessagesWithValidDate(mExpireMessagesWithValidDate); 0033 return expireAttr; 0034 } 0035 0036 void ExpireCollectionAttribute::setAutoExpire(bool enabled) 0037 { 0038 mExpireMessages = enabled; 0039 } 0040 0041 bool ExpireCollectionAttribute::isAutoExpire() const 0042 { 0043 return mExpireMessages; 0044 } 0045 0046 void ExpireCollectionAttribute::setUnreadExpireAge(int age) 0047 { 0048 if (age >= 0 && age != mUnreadExpireAge) { 0049 mUnreadExpireAge = age; 0050 } 0051 } 0052 0053 int ExpireCollectionAttribute::unreadExpireAge() const 0054 { 0055 return mUnreadExpireAge; 0056 } 0057 0058 void ExpireCollectionAttribute::setUnreadExpireUnits(ExpireUnits units) 0059 { 0060 if (units >= ExpireNever && units < ExpireMaxUnits) { 0061 mUnreadExpireUnits = units; 0062 } 0063 } 0064 0065 void ExpireCollectionAttribute::setReadExpireAge(int age) 0066 { 0067 if (age >= 0 && age != mReadExpireAge) { 0068 mReadExpireAge = age; 0069 } 0070 } 0071 0072 int ExpireCollectionAttribute::readExpireAge() const 0073 { 0074 return mReadExpireAge; 0075 } 0076 0077 void ExpireCollectionAttribute::setReadExpireUnits(ExpireUnits units) 0078 { 0079 if (units >= ExpireNever && units <= ExpireMaxUnits) { 0080 mReadExpireUnits = units; 0081 } 0082 } 0083 0084 void ExpireCollectionAttribute::setExpireAction(ExpireAction a) 0085 { 0086 mExpireAction = a; 0087 } 0088 0089 ExpireCollectionAttribute::ExpireAction ExpireCollectionAttribute::expireAction() const 0090 { 0091 return mExpireAction; 0092 } 0093 0094 void ExpireCollectionAttribute::setExpireToFolderId(Akonadi::Collection::Id id) 0095 { 0096 mExpireToFolderId = id; 0097 } 0098 0099 Akonadi::Collection::Id ExpireCollectionAttribute::expireToFolderId() const 0100 { 0101 return mExpireToFolderId; 0102 } 0103 0104 ExpireCollectionAttribute::ExpireUnits ExpireCollectionAttribute::unreadExpireUnits() const 0105 { 0106 return mUnreadExpireUnits; 0107 } 0108 0109 ExpireCollectionAttribute::ExpireUnits ExpireCollectionAttribute::readExpireUnits() const 0110 { 0111 return mReadExpireUnits; 0112 } 0113 0114 bool ExpireCollectionAttribute::operator==(const ExpireCollectionAttribute &other) const 0115 { 0116 return (mExpireMessages == other.isAutoExpire()) && (mUnreadExpireAge == other.unreadExpireAge()) && (mReadExpireAge == other.readExpireAge()) 0117 && (mUnreadExpireUnits == other.unreadExpireUnits()) && (mReadExpireUnits == other.readExpireUnits()) && (mExpireAction == other.expireAction()) 0118 && (mExpireToFolderId == other.expireToFolderId()) && (mExpireMessagesWithValidDate == other.expireMessagesWithValidDate()); 0119 } 0120 0121 int ExpireCollectionAttribute::daysToExpire(int number, ExpireCollectionAttribute::ExpireUnits units) 0122 { 0123 switch (units) { 0124 case ExpireCollectionAttribute::ExpireDays: // Days 0125 return number; 0126 case ExpireCollectionAttribute::ExpireWeeks: // Weeks 0127 return number * 7; 0128 case ExpireCollectionAttribute::ExpireMonths: // Months - this could be better 0129 // rather than assuming 31day months. 0130 return number * 31; 0131 default: // this avoids a compiler warning (not handled enumeration values) 0132 ; 0133 } 0134 return -1; 0135 } 0136 0137 bool ExpireCollectionAttribute::expireMessagesWithValidDate() const 0138 { 0139 return mExpireMessagesWithValidDate; 0140 } 0141 0142 void ExpireCollectionAttribute::setExpireMessagesWithValidDate(bool expireMessagesWithValidDate) 0143 { 0144 mExpireMessagesWithValidDate = expireMessagesWithValidDate; 0145 } 0146 0147 void ExpireCollectionAttribute::daysToExpire(int &unreadDays, int &readDays) const 0148 { 0149 unreadDays = ExpireCollectionAttribute::daysToExpire(unreadExpireAge(), unreadExpireUnits()); 0150 readDays = ExpireCollectionAttribute::daysToExpire(readExpireAge(), readExpireUnits()); 0151 } 0152 0153 QByteArray ExpireCollectionAttribute::serialized() const 0154 { 0155 QByteArray result; 0156 QDataStream s(&result, QIODevice::WriteOnly); 0157 0158 s << mExpireToFolderId; 0159 s << static_cast<int>(mExpireAction); 0160 s << static_cast<int>(mReadExpireUnits); 0161 s << mReadExpireAge; 0162 s << static_cast<int>(mUnreadExpireUnits); 0163 s << mUnreadExpireAge; 0164 s << mExpireMessages; 0165 s << mExpireMessagesWithValidDate; 0166 0167 return result; 0168 } 0169 0170 void ExpireCollectionAttribute::deserialize(const QByteArray &data) 0171 { 0172 QDataStream s(data); 0173 s >> mExpireToFolderId; 0174 int action; 0175 s >> action; 0176 mExpireAction = static_cast<ExpireCollectionAttribute::ExpireAction>(action); 0177 int valUnitRead; 0178 s >> valUnitRead; 0179 mReadExpireUnits = static_cast<ExpireCollectionAttribute::ExpireUnits>(valUnitRead); 0180 s >> mReadExpireAge; 0181 int valUnitUnread; 0182 s >> valUnitUnread; 0183 mUnreadExpireUnits = static_cast<ExpireCollectionAttribute::ExpireUnits>(valUnitUnread); 0184 s >> mUnreadExpireAge; 0185 s >> mExpireMessages; 0186 s >> mExpireMessagesWithValidDate; 0187 } 0188 0189 QDebug operator<<(QDebug d, const ExpireCollectionAttribute &t) 0190 { 0191 d << " mExpireMessages " << t.isAutoExpire(); 0192 d << " mUnreadExpireAge " << t.unreadExpireAge(); 0193 d << " mReadExpireAge " << t.readExpireAge(); 0194 d << " mUnreadExpireUnits " << t.unreadExpireUnits(); 0195 d << " mReadExpireUnits " << t.readExpireUnits(); 0196 d << " mExpireAction " << t.expireAction(); 0197 d << " mExpireToFolderId " << t.expireToFolderId(); 0198 d << " mExpireMessagesWithValidDate " << t.expireMessagesWithValidDate(); 0199 return d; 0200 }