Mailcommon

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

KDE's Doxygen guidelines are available online.