File indexing completed on 2024-12-15 04:54:36

0001 /******************************************************************************
0002  *
0003  *  SPDX-FileCopyrightText: 2008 Szymon Tomasz Stefanek <pragma@kvirc.net>
0004  *
0005  *  SPDX-License-Identifier: GPL-2.0-or-later
0006  *
0007  *******************************************************************************/
0008 
0009 #include "core/aggregation.h"
0010 
0011 #include <QDataStream>
0012 
0013 #include <KLocalizedString>
0014 
0015 using namespace MessageList::Core;
0016 
0017 static const int gAggregationCurrentVersion = 0x1009; // increase if you add new fields of change the meaning of some
0018 
0019 Aggregation::Aggregation(const QString &name,
0020                          const QString &description,
0021                          Grouping grouping,
0022                          GroupExpandPolicy groupExpandPolicy,
0023                          Threading threading,
0024                          ThreadLeader threadLeader,
0025                          ThreadExpandPolicy threadExpandPolicy,
0026                          FillViewStrategy fillViewStrategy,
0027                          bool readOnly)
0028     : OptionSet(name, description, readOnly)
0029     , mGrouping(grouping)
0030     , mGroupExpandPolicy(groupExpandPolicy)
0031     , mThreading(threading)
0032     , mThreadLeader(threadLeader)
0033     , mThreadExpandPolicy(threadExpandPolicy)
0034     , mFillViewStrategy(fillViewStrategy)
0035 {
0036 }
0037 
0038 Aggregation::Grouping Aggregation::grouping() const
0039 {
0040     return mGrouping;
0041 }
0042 
0043 Aggregation::Aggregation(const Aggregation &opt)
0044 
0045     = default;
0046 
0047 Aggregation::Aggregation()
0048     : OptionSet()
0049     , mGrouping(NoGrouping)
0050     , mGroupExpandPolicy(NeverExpandGroups)
0051     , mThreading(NoThreading)
0052     , mThreadLeader(TopmostMessage)
0053     , mThreadExpandPolicy(NeverExpandThreads)
0054     , mFillViewStrategy(FavorInteractivity)
0055 {
0056 }
0057 
0058 bool Aggregation::load(QDataStream &stream)
0059 {
0060     int val;
0061 
0062     stream >> val;
0063     if (val != gAggregationCurrentVersion) {
0064         return false; // b0rken (invalid version)
0065     }
0066 
0067     stream >> val;
0068     mGrouping = static_cast<Grouping>(val);
0069     switch (mGrouping) {
0070     case NoGrouping:
0071     case GroupByDate:
0072     case GroupByDateRange:
0073     case GroupBySenderOrReceiver:
0074     case GroupBySender:
0075     case GroupByReceiver:
0076         // ok
0077         break;
0078     default:
0079         // b0rken
0080         return false;
0081     }
0082 
0083     stream >> val; // Formerly contained group sorting
0084     stream >> val; // Formerly contained group sorting direction
0085 
0086     stream >> val;
0087     mGroupExpandPolicy = (GroupExpandPolicy)val;
0088     switch (mGroupExpandPolicy) {
0089     case NeverExpandGroups:
0090     case ExpandRecentGroups:
0091     case AlwaysExpandGroups:
0092         // ok
0093         break;
0094     default:
0095         // b0rken
0096         return false;
0097         break;
0098     }
0099 
0100     stream >> val;
0101     mThreading = (Threading)val;
0102     switch (mThreading) {
0103     case NoThreading:
0104     case PerfectOnly:
0105     case PerfectAndReferences:
0106     case PerfectReferencesAndSubject:
0107         // ok
0108         break;
0109     default:
0110         // b0rken
0111         return false;
0112     }
0113 
0114     stream >> val;
0115     mThreadLeader = (ThreadLeader)val;
0116     switch (mThreadLeader) {
0117     case MostRecentMessage:
0118     case TopmostMessage:
0119         // ok
0120         // FIXME: Should check that thread leaders setting matches grouping and threading settings.
0121         break;
0122     default:
0123         // b0rken
0124         return false;
0125     }
0126 
0127     stream >> val;
0128     mThreadExpandPolicy = (ThreadExpandPolicy)val;
0129     switch (mThreadExpandPolicy) {
0130     case NeverExpandThreads:
0131     case ExpandThreadsWithNewMessages:
0132     case ExpandThreadsWithUnreadMessages:
0133     case ExpandThreadsWithUnreadOrImportantMessages:
0134     case AlwaysExpandThreads:
0135         // ok
0136         break;
0137     default:
0138         // b0rken
0139         return false;
0140     }
0141 
0142     stream >> val; // Formerly contained message sorting
0143     stream >> val; // Formerly contained message sort direction
0144 
0145     stream >> val;
0146     mFillViewStrategy = (FillViewStrategy)val;
0147     switch (mFillViewStrategy) {
0148     case FavorSpeed:
0149     case FavorInteractivity:
0150     case BatchNoInteractivity:
0151         // ok
0152         break;
0153     default:
0154         // b0rken
0155         return false;
0156     }
0157 
0158     return true;
0159 }
0160 
0161 void Aggregation::save(QDataStream &stream) const
0162 {
0163     stream << (int)gAggregationCurrentVersion;
0164     stream << (int)mGrouping;
0165     stream << 0; // Formerly group sorting
0166     stream << 0; // Formerly group sort direction
0167     stream << (int)mGroupExpandPolicy;
0168     stream << (int)mThreading;
0169     stream << (int)mThreadLeader;
0170     stream << (int)mThreadExpandPolicy;
0171     stream << 0; // Formerly message sorting
0172     stream << 0; // Formerly message sort direction
0173     stream << (int)mFillViewStrategy;
0174 }
0175 
0176 QList<QPair<QString, int>> Aggregation::enumerateGroupingOptions()
0177 {
0178     return {{i18nc("No grouping of messages", "None"), NoGrouping},
0179             {i18n("By Exact Date (of Thread Leaders)"), GroupByDate},
0180             {i18n("By Smart Date Ranges (of Thread Leaders)"), GroupByDateRange},
0181             {i18n("By Smart Sender/Receiver"), GroupBySenderOrReceiver},
0182             {i18n("By Sender"), GroupBySender},
0183             {i18n("By Receiver"), GroupByReceiver}};
0184 }
0185 
0186 QList<QPair<QString, int>> Aggregation::enumerateGroupExpandPolicyOptions(Grouping g)
0187 {
0188     QList<QPair<QString, int>> ret;
0189     if (g == NoGrouping) {
0190         return ret;
0191     }
0192     ret.append({i18n("Never Expand Groups"), NeverExpandGroups});
0193     if ((g == GroupByDate) || (g == GroupByDateRange)) {
0194         ret.append({i18n("Expand Recent Groups"), ExpandRecentGroups});
0195     }
0196     ret.append({i18n("Always Expand Groups"), AlwaysExpandGroups});
0197     return ret;
0198 }
0199 
0200 QList<QPair<QString, int>> Aggregation::enumerateThreadingOptions()
0201 {
0202     return {{i18nc("No threading of messages", "None"), NoThreading},
0203             {i18n("Perfect Only"), PerfectOnly},
0204             {i18n("Perfect and by References"), PerfectAndReferences},
0205             {i18n("Perfect, by References and by Subject"), PerfectReferencesAndSubject}};
0206 }
0207 
0208 QList<QPair<QString, int>> Aggregation::enumerateThreadLeaderOptions(Grouping g, Threading t)
0209 {
0210     QList<QPair<QString, int>> ret;
0211     if (t == NoThreading) {
0212         return ret;
0213     }
0214     ret.append({i18n("Topmost Message"), TopmostMessage});
0215     if ((g != GroupByDate) && (g != GroupByDateRange)) {
0216         return ret;
0217     }
0218     ret.append({i18n("Most Recent Message"), MostRecentMessage});
0219     return ret;
0220 }
0221 
0222 QList<QPair<QString, int>> Aggregation::enumerateThreadExpandPolicyOptions(Threading t)
0223 {
0224     if (t == NoThreading) {
0225         return {};
0226     }
0227 
0228     return {{i18n("Never Expand Threads"), NeverExpandThreads},
0229             {i18n("Expand Threads With Unread Messages"), ExpandThreadsWithUnreadMessages},
0230             {i18n("Expand Threads With Unread or Important Messages"), ExpandThreadsWithUnreadOrImportantMessages},
0231             {i18n("Always Expand Threads"), AlwaysExpandThreads}};
0232 }
0233 
0234 QList<QPair<QString, int>> Aggregation::enumerateFillViewStrategyOptions()
0235 {
0236     return {{i18n("Favor Interactivity"), FavorInteractivity}, {i18n("Favor Speed"), FavorSpeed}, {i18n("Batch Job (No Interactivity)"), BatchNoInteractivity}};
0237 }