File indexing completed on 2025-01-05 04:47:07

0001 /*
0002     SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "cachepolicypage.h"
0008 
0009 #include "ui_cachepolicypage.h"
0010 
0011 #include "cachepolicy.h"
0012 #include "collection.h"
0013 #include "collectionutils.h"
0014 
0015 #include <KLocalizedString>
0016 
0017 using namespace Akonadi;
0018 
0019 class Akonadi::CachePolicyPagePrivate
0020 {
0021 public:
0022     CachePolicyPagePrivate()
0023         : mUi(new Ui::CachePolicyPage)
0024     {
0025     }
0026 
0027     ~CachePolicyPagePrivate()
0028     {
0029         delete mUi;
0030     }
0031 
0032     void slotIntervalValueChanged(int /*interval*/);
0033     void slotCacheValueChanged(int /*interval*/);
0034     void slotRetrievalOptionsGroupBoxDisabled(bool disable);
0035 
0036     Ui::CachePolicyPage *const mUi;
0037     CachePolicyPage::GuiMode mode;
0038 };
0039 
0040 void CachePolicyPagePrivate::slotIntervalValueChanged(int interval)
0041 {
0042     mUi->checkInterval->setSuffix(QLatin1Char(' ') + i18np("minute", "minutes", interval));
0043 }
0044 
0045 void CachePolicyPagePrivate::slotCacheValueChanged(int interval)
0046 {
0047     mUi->localCacheTimeout->setSuffix(QLatin1Char(' ') + i18np("minute", "minutes", interval));
0048 }
0049 
0050 void CachePolicyPagePrivate::slotRetrievalOptionsGroupBoxDisabled(bool disable)
0051 {
0052     mUi->retrieveFullMessages->setDisabled(disable);
0053     mUi->retrieveFullMessages->setDisabled(disable);
0054     mUi->retrieveOnlyHeaders->setDisabled(disable);
0055     mUi->localCacheTimeout->setDisabled(disable);
0056     mUi->retrievalOptionsLabel->setDisabled(disable);
0057     mUi->label->setDisabled(disable);
0058     if (!disable) {
0059         mUi->label->setEnabled(mUi->retrieveOnlyHeaders->isChecked());
0060         mUi->localCacheTimeout->setEnabled(mUi->retrieveOnlyHeaders->isChecked());
0061     }
0062 }
0063 
0064 CachePolicyPage::CachePolicyPage(QWidget *parent, GuiMode mode)
0065     : CollectionPropertiesPage(parent)
0066     , d(new CachePolicyPagePrivate)
0067 {
0068     setObjectName(QLatin1StringView("Akonadi::CachePolicyPage"));
0069     setPageTitle(i18n("Retrieval"));
0070     d->mode = mode;
0071 
0072     d->mUi->setupUi(this);
0073     connect(d->mUi->checkInterval, &QSpinBox::valueChanged, this, [this](int value) {
0074         d->slotIntervalValueChanged(value);
0075     });
0076     connect(d->mUi->localCacheTimeout, &QSpinBox::valueChanged, this, [this](int value) {
0077         d->slotCacheValueChanged(value);
0078     });
0079     connect(d->mUi->inherit, &QCheckBox::toggled, this, [this](bool checked) {
0080         d->slotRetrievalOptionsGroupBoxDisabled(checked);
0081     });
0082     if (mode == AdvancedMode) {
0083         d->mUi->retrievalOptionsLabel->hide();
0084         d->mUi->retrieveFullMessages->hide();
0085         d->mUi->retrieveOnlyHeaders->hide();
0086         d->mUi->localCacheTimeout->hide();
0087     } else {
0088         d->mUi->localParts->hide();
0089         d->mUi->localPartsLabel->hide();
0090     }
0091 }
0092 
0093 CachePolicyPage::~CachePolicyPage() = default;
0094 
0095 bool Akonadi::CachePolicyPage::canHandle(const Collection &collection) const
0096 {
0097     return !collection.isVirtual();
0098 }
0099 
0100 void CachePolicyPage::load(const Collection &collection)
0101 {
0102     const CachePolicy policy = collection.cachePolicy();
0103 
0104     int interval = policy.intervalCheckTime();
0105     if (interval == -1) {
0106         interval = 0;
0107     }
0108 
0109     int cache = policy.cacheTimeout();
0110     if (cache == -1) {
0111         cache = 0;
0112     }
0113 
0114     d->mUi->checkInterval->setValue(interval);
0115     d->mUi->localCacheTimeout->setValue(cache);
0116     d->mUi->syncOnDemand->setChecked(policy.syncOnDemand());
0117     d->mUi->localParts->setItems(policy.localParts());
0118 
0119     const bool fetchBodies = policy.localParts().contains(QLatin1StringView("RFC822"));
0120     d->mUi->retrieveFullMessages->setChecked(fetchBodies);
0121 
0122     // done explicitly to disable/enabled widgets
0123     d->mUi->retrieveOnlyHeaders->setChecked(!fetchBodies);
0124     d->mUi->label->setEnabled(!fetchBodies);
0125     d->mUi->localCacheTimeout->setEnabled(!fetchBodies);
0126     // last code otherwise it will call slotRetrievalOptionsGroupBoxDisabled before
0127     // calling d->mUi->label->setEnabled(!fetchBodies);
0128     d->mUi->inherit->setChecked(policy.inheritFromParent());
0129 }
0130 
0131 void CachePolicyPage::save(Collection &collection)
0132 {
0133     int interval = d->mUi->checkInterval->value();
0134     if (interval == 0) {
0135         interval = -1;
0136     }
0137 
0138     int cache = d->mUi->localCacheTimeout->value();
0139     if (cache == 0) {
0140         cache = -1;
0141     }
0142 
0143     CachePolicy policy = collection.cachePolicy();
0144     policy.setInheritFromParent(d->mUi->inherit->isChecked());
0145     policy.setIntervalCheckTime(interval);
0146     policy.setCacheTimeout(cache);
0147     policy.setSyncOnDemand(d->mUi->syncOnDemand->isChecked());
0148 
0149     QStringList localParts = d->mUi->localParts->items();
0150 
0151     // Unless we are in "raw" mode, add "bodies" to the list of message
0152     // parts to keep around locally, if the user selected that, or remove
0153     // it otherwise. In "raw" mode we simple use the values from the list
0154     // view.
0155     if (d->mode != AdvancedMode) {
0156         if (d->mUi->retrieveFullMessages->isChecked() && !localParts.contains(QLatin1StringView("RFC822"))) {
0157             localParts.append(QStringLiteral("RFC822"));
0158         } else if (!d->mUi->retrieveFullMessages->isChecked() && localParts.contains(QLatin1StringView("RFC822"))) {
0159             localParts.removeAll(QStringLiteral("RFC822"));
0160         }
0161     }
0162 
0163     policy.setLocalParts(localParts);
0164     collection.setCachePolicy(policy);
0165 }
0166 
0167 #include "moc_cachepolicypage.cpp"