Messagelib

sortorder.cpp
1 /******************************************************************************
2  *
3  * SPDX-FileCopyrightText: 2009 Thomas McGuire <[email protected]>
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  *
7  *******************************************************************************/
8 #include "core/sortorder.h"
9 
10 #include "messagelistutil.h"
11 #include "messagelistutil_p.h"
12 
13 #include <KLocalizedString>
14 
15 #include <QMetaEnum>
16 
17 using namespace MessageList::Core;
18 
19 SortOrder::SortOrder() = default;
20 
22 {
23  return mGroupSorting;
24 }
25 
27 {
28  mGroupSorting = gs;
29 }
30 
32 {
33  return mGroupSortDirection;
34 }
35 
37 {
38  mGroupSortDirection = groupSortDirection;
39 }
40 
42 {
43  return mMessageSorting;
44 }
45 
47 {
48  mMessageSorting = ms;
49 }
50 
52 {
53  return mMessageSortDirection;
54 }
55 
57 {
58  mMessageSortDirection = messageSortDirection;
59 }
60 
62 {
64  ret.append({i18n("None (Storage Order)"), SortOrder::NoMessageSorting});
65  ret.append({i18n("By Date/Time"), SortOrder::SortMessagesByDateTime});
66  if (t != Aggregation::NoThreading) {
67  ret.append({i18n("By Date/Time of Most Recent in Subtree"), SortOrder::SortMessagesByDateTimeOfMostRecent});
68  }
69  ret.append({i18n("By Sender"), SortOrder::SortMessagesBySender});
70  ret.append({i18n("By Receiver"), SortOrder::SortMessagesByReceiver});
71  ret.append({i18n("By Smart Sender/Receiver"), SortOrder::SortMessagesBySenderOrReceiver});
72  ret.append({i18n("By Subject"), SortOrder::SortMessagesBySubject});
73  ret.append({i18n("By Size"), SortOrder::SortMessagesBySize});
74  ret.append({i18n("By Action Item Status"), SortOrder::SortMessagesByActionItemStatus});
75  ret.append({i18n("By Unread Status"), SortOrder::SortMessagesByUnreadStatus});
76  ret.append({i18n("By Important Status"), SortOrder::SortMessagesByImportantStatus});
77  ret.append({i18n("By Attachment Status"), SortOrder::SortMessagesByAttachmentStatus});
78  return ret;
79 }
80 
82 {
84  if (ms == SortOrder::NoMessageSorting) {
85  return ret;
86  }
87 
89  ret.append({i18n("Least Recent on Top"), SortOrder::Ascending});
90  ret.append({i18n("Most Recent on Top"), SortOrder::Descending});
91  return ret;
92  }
93 
94  ret.append({i18nc("Sort order for messages", "Ascending"), SortOrder::Ascending});
95  ret.append({i18nc("Sort order for messages", "Descending"), SortOrder::Descending});
96  return ret;
97 }
98 
100 {
102  if (g == Aggregation::NoGrouping) {
103  return ret;
104  }
106  ret.append({i18n("by Date/Time"), SortOrder::SortGroupsByDateTime});
107  } else {
108  ret.append({i18n("None (Storage Order)"), SortOrder::NoGroupSorting});
109  ret.append({i18n("by Date/Time of Most Recent Message in Group"), SortOrder::SortGroupsByDateTimeOfMostRecent});
110  }
111 
113  ret.append({i18n("by Sender/Receiver"), SortOrder::SortGroupsBySenderOrReceiver});
114  } else if (g == Aggregation::GroupBySender) {
115  ret.append({i18n("by Sender"), SortOrder::SortGroupsBySender});
116  } else if (g == Aggregation::GroupByReceiver) {
117  ret.append({i18n("by Receiver"), SortOrder::SortGroupsByReceiver});
118  }
119 
120  return ret;
121 }
122 
124 {
127  return ret;
128  }
129 
131  ret.append({i18n("Least Recent on Top"), SortOrder::Ascending});
132  ret.append({i18n("Most Recent on Top"), SortOrder::Descending});
133  return ret;
134  }
135  ret.append({i18nc("Sort order for mail groups", "Ascending"), SortOrder::Ascending});
136  ret.append({i18nc("Sort order for mail groups", "Descending"), SortOrder::Descending});
137  return ret;
138 }
139 
141 static bool optionListHasOption(const OptionList &optionList, int optionValue, int defaultOptionValue)
142 {
143  for (const auto &pair : optionList) {
144  if (pair.second == optionValue) {
145  return true;
146  }
147  }
148  if (optionValue != defaultOptionValue) {
149  return false;
150  } else {
151  return true;
152  }
153 }
154 
155 bool SortOrder::validForAggregation(const Aggregation *aggregation) const
156 {
157  OptionList messageSortings = enumerateMessageSortingOptions(aggregation->threading());
158  OptionList messageSortDirections = enumerateMessageSortDirectionOptions(mMessageSorting);
159  OptionList groupSortings = enumerateGroupSortingOptions(aggregation->grouping());
160  OptionList groupSortDirections = enumerateGroupSortDirectionOptions(aggregation->grouping(), mGroupSorting);
161  SortOrder defaultSortOrder = defaultForAggregation(aggregation, SortOrder());
162  bool messageSortingOk = optionListHasOption(messageSortings, mMessageSorting, defaultSortOrder.messageSorting());
163  bool messageSortDirectionOk = optionListHasOption(messageSortDirections, mMessageSortDirection, defaultSortOrder.messageSortDirection());
164 
165  bool groupSortingOk = optionListHasOption(groupSortings, mGroupSorting, defaultSortOrder.groupSorting());
166  bool groupSortDirectionOk = optionListHasOption(groupSortDirections, mGroupSortDirection, defaultSortOrder.groupSortDirection());
167  return messageSortingOk && messageSortDirectionOk && groupSortingOk && groupSortDirectionOk;
168 }
169 
171 {
172  SortOrder newSortOrder;
173 
174  //
175  // First check if we can adopt the message sorting and the message sort direction from
176  // the old sort order. This is mostly true, except, for example, when the old message sorting
177  // was "by most recent in subtree", and the aggregation doesn't use threading.
178  //
179  OptionList messageSortings = enumerateMessageSortingOptions(aggregation->threading());
180  bool messageSortingOk = optionListHasOption(messageSortings, oldSortOrder.messageSorting(), SortOrder().messageSorting());
181  bool messageSortDirectionOk = false;
182  if (messageSortingOk) {
183  const OptionList messageSortDirections = enumerateMessageSortDirectionOptions(oldSortOrder.messageSorting());
184  messageSortDirectionOk = optionListHasOption(messageSortDirections, oldSortOrder.messageSortDirection(), SortOrder().messageSortDirection());
185  newSortOrder.setMessageSorting(oldSortOrder.messageSorting());
186  } else {
188  }
189  if (messageSortDirectionOk) {
190  newSortOrder.setMessageSortDirection(oldSortOrder.messageSortDirection());
191  } else {
192  newSortOrder.setMessageSortDirection(Descending);
193  }
194 
195  //
196  // Now set the group sorting and group sort direction, depending on the aggregation.
197  //
198  Aggregation::Grouping grouping = aggregation->grouping();
199  if (grouping == Aggregation::GroupByDate || grouping == Aggregation::GroupByDateRange) {
200  newSortOrder.setGroupSortDirection(Descending);
202  } else if (grouping == Aggregation::GroupByReceiver || grouping == Aggregation::GroupBySender || grouping == Aggregation::GroupBySenderOrReceiver) {
203  newSortOrder.setGroupSortDirection(Descending);
204  switch (grouping) {
207  break;
209  newSortOrder.setGroupSorting(SortGroupsBySender);
210  break;
213  break;
214  default:
215  break;
216  }
217  }
218 
219  return newSortOrder;
220 }
221 
222 bool SortOrder::readConfigHelper(KConfigGroup &conf, const QString &id)
223 {
224  if (!conf.hasKey(id + MessageList::Util::messageSortingConfigName())) {
225  return false;
226  }
227  mMessageSorting = messageSortingForName(conf.readEntry(id + MessageList::Util::messageSortingConfigName()));
228  mMessageSortDirection = sortDirectionForName(conf.readEntry(id + MessageList::Util::messageSortDirectionConfigName()));
229  mGroupSorting = groupSortingForName(conf.readEntry(id + MessageList::Util::groupSortingConfigName()));
230  mGroupSortDirection = sortDirectionForName(conf.readEntry(id + MessageList::Util::groupSortDirectionConfigName()));
231  return true;
232 }
233 
234 void SortOrder::readConfig(KConfigGroup &conf, const QString &storageId, bool *storageUsesPrivateSortOrder)
235 {
236  SortOrder privateSortOrder;
237  SortOrder globalSortOrder;
238  globalSortOrder.readConfigHelper(conf, QStringLiteral("GlobalSortOrder"));
239  *storageUsesPrivateSortOrder = privateSortOrder.readConfigHelper(conf, storageId);
240  if (*storageUsesPrivateSortOrder) {
241  *this = privateSortOrder;
242  } else {
243  *this = globalSortOrder;
244  }
245 }
246 
247 void SortOrder::writeConfig(KConfigGroup &conf, const QString &storageId, bool storageUsesPrivateSortOrder) const
248 {
249  QString id = storageId;
250  if (!storageUsesPrivateSortOrder) {
251  id = QStringLiteral("GlobalSortOrder");
252  conf.deleteEntry(storageId + MessageList::Util::messageSortingConfigName());
253  conf.deleteEntry(storageId + MessageList::Util::messageSortDirectionConfigName());
254  conf.deleteEntry(storageId + MessageList::Util::groupSortingConfigName());
255  conf.deleteEntry(storageId + MessageList::Util::groupSortDirectionConfigName());
256  }
257 
258  conf.writeEntry(id + MessageList::Util::messageSortingConfigName(), nameForMessageSorting(mMessageSorting));
259  conf.writeEntry(id + MessageList::Util::messageSortDirectionConfigName(), nameForSortDirection(mMessageSortDirection));
260  conf.writeEntry(id + MessageList::Util::groupSortingConfigName(), nameForGroupSorting(mGroupSorting));
261  conf.writeEntry(id + MessageList::Util::groupSortDirectionConfigName(), nameForSortDirection(mGroupSortDirection));
262 }
263 
265 {
266  switch (ms) {
277  case SortOrder::SortMessagesByImportantStatus:
279  // ok
280  break;
281  default:
282  // b0rken
283  return false;
284  }
285 
286  return true;
287 }
288 
289 const QString SortOrder::nameForSortDirection(SortDirection sortDirection)
290 {
291  int index = staticMetaObject.indexOfEnumerator("SortDirection");
292  return QLatin1String(staticMetaObject.enumerator(index).valueToKey(sortDirection));
293 }
294 
295 const QString SortOrder::nameForMessageSorting(MessageSorting messageSorting)
296 {
297  int index = staticMetaObject.indexOfEnumerator("MessageSorting");
298  return QLatin1String(staticMetaObject.enumerator(index).valueToKey(messageSorting));
299 }
300 
301 const QString SortOrder::nameForGroupSorting(GroupSorting groupSorting)
302 {
303  int index = staticMetaObject.indexOfEnumerator("GroupSorting");
304  return QLatin1String(staticMetaObject.enumerator(index).valueToKey(groupSorting));
305 }
306 
307 SortOrder::SortDirection SortOrder::sortDirectionForName(const QString &name)
308 {
309  int index = staticMetaObject.indexOfEnumerator("SortDirection");
310  return static_cast<SortDirection>(staticMetaObject.enumerator(index).keyToValue(name.toLatin1().constData()));
311 }
312 
313 SortOrder::MessageSorting SortOrder::messageSortingForName(const QString &name)
314 {
315  int index = staticMetaObject.indexOfEnumerator("MessageSorting");
316  return static_cast<MessageSorting>(staticMetaObject.enumerator(index).keyToValue(name.toLatin1().constData()));
317 }
318 
319 SortOrder::GroupSorting SortOrder::groupSortingForName(const QString &name)
320 {
321  int index = staticMetaObject.indexOfEnumerator("GroupSorting");
322  return static_cast<GroupSorting>(staticMetaObject.enumerator(index).keyToValue(name.toLatin1().constData()));
323 }
324 
325 #include "moc_sortorder.cpp"
Sort the messages by date and time of the most recent message in subtree.
Definition: sortorder.h:60
A class which holds information about sorting, e.g.
Definition: sortorder.h:22
A set of aggregation options that can be applied to the MessageList::Model in a single shot...
Definition: aggregation.h:28
Sort groups by date/time of the group.
Definition: sortorder.h:37
SortDirection messageSortDirection() const
Returns the current message SortDirection.
Definition: sortorder.cpp:51
void append(const T &value)
static bool isValidMessageSorting(SortOrder::MessageSorting ms)
Returns true if the ms parameter specifies a valid MessageSorting option.
Definition: sortorder.cpp:264
Perform no threading at all.
Definition: aggregation.h:66
MessageSorting messageSorting() const
Returns the current message sorting option.
Definition: sortorder.cpp:41
static QVector< QPair< QString, int > > enumerateMessageSortDirectionOptions(MessageSorting ms)
Enumerates the available message sorting directions for the specified MessageSorting option...
Definition: sortorder.cpp:81
Threading
The available threading methods.
Definition: aggregation.h:65
Threading threading() const
Returns the current threading method.
Definition: aggregation.h:185
Group the messages by the date of the thread leader.
Definition: aggregation.h:38
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
Sort groups by receiver (makes sense only with GroupByReceiver)
Definition: sortorder.h:41
Use smart (thread leader) date ranges ("Today","Yesterday","Last Week"...)
Definition: aggregation.h:39
SortDirection groupSortDirection() const
Returns the current group SortDirection.
Definition: sortorder.cpp:31
Grouping grouping() const
Returns the currently set Grouping option.
Definition: aggregation.cpp:38
The implementation independent part of the MessageList library.
Definition: aggregation.h:21
static QVector< QPair< QString, int > > enumerateMessageSortingOptions(Aggregation::Threading t)
Enumerates the message sorting options compatible with the specified Threading setting.
Definition: sortorder.cpp:61
const QLatin1String name
Don&#39;t group messages at all.
Definition: aggregation.h:37
SortDirection
The "generic" sort direction: used for groups and for messages If you add values here please look at ...
Definition: sortorder.h:50
void deleteEntry(const QString &pKey, WriteConfigFlags pFlags=Normal)
void writeConfig(KConfigGroup &conf, const QString &storageId, bool storageUsesPrivateSortOrder) const
Writes the sort order to a config group.
Definition: sortorder.cpp:247
Don&#39;t sort the groups at all, add them as they come in.
Definition: sortorder.h:36
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setGroupSorting(GroupSorting gs)
Sets the GroupSorting option.
Definition: sortorder.cpp:26
MessageSorting
The available message sorting options.
Definition: sortorder.h:57
void setMessageSortDirection(SortDirection messageSortDirection)
Sets the SortDirection for the message.
Definition: sortorder.cpp:56
const char * constData() const const
static QVector< QPair< QString, int > > enumerateGroupSortingOptions(Aggregation::Grouping g)
Enumerates the group sorting options compatible with the specified Grouping.
Definition: sortorder.cpp:99
void setMessageSorting(MessageSorting ms)
Sets the current message sorting option.
Definition: sortorder.cpp:46
bool hasKey(const QString &key) const
Sort groups by sender (makes sense only with GroupBySender)
Definition: sortorder.h:40
QString i18n(const char *text, const TYPE &arg...)
void readConfig(KConfigGroup &conf, const QString &storageId, bool *storageUsesPrivateSortOrder)
Reads the sort order from a config group.
Definition: sortorder.cpp:234
Grouping
Message grouping.
Definition: aggregation.h:36
Don&#39;t sort the messages at all.
Definition: sortorder.h:58
GroupSorting
How to sort the groups If you add values here please look at the implementations of the enumerate* fu...
Definition: sortorder.h:35
QByteArray toLatin1() const const
Sort the messages by sender or receiver.
Definition: sortorder.h:61
void setGroupSortDirection(SortDirection groupSortDirection)
Sets the SortDirection for the groups.
Definition: sortorder.cpp:36
GroupSorting groupSorting() const
Returns the GroupSorting.
Definition: sortorder.cpp:21
static SortOrder defaultForAggregation(const Aggregation *aggregation, SortOrder oldSortOrder)
Returns the default sort order for the given aggregation.
Definition: sortorder.cpp:170
Sort groups by sender or receiver (makes sense only with GroupBySenderOrReceiver) ...
Definition: sortorder.h:39
Sort the messages by the "Unread" flags of status.
Definition: sortorder.h:67
Sort the messages by date and time.
Definition: sortorder.h:59
Group by sender (incoming) or receiver (outgoing) field.
Definition: aggregation.h:40
T readEntry(const QString &key, const T &aDefault) const
Sort the messages by the "Action Item" flag of status.
Definition: sortorder.h:66
static QVector< QPair< QString, int > > enumerateGroupSortDirectionOptions(Aggregation::Grouping g, GroupSorting groupSorting)
Enumerates the group sort direction options compatible with the specified Grouping and GroupSorting...
Definition: sortorder.cpp:123
bool validForAggregation(const Aggregation *aggregation) const
Checks if this sort order can be used in combination with the given aggregation.
Definition: sortorder.cpp:155
Sort groups by date/time of the most recent message.
Definition: sortorder.h:38
Sort the messages By "Important" flags of status.
Definition: sortorder.h:69
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Nov 26 2021 23:16:43 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.