File indexing completed on 2024-05-19 05:00:54
0001 /* 0002 This file is part of the KDE project. 0003 0004 SPDX-FileCopyrightText: 2020 Stefano Crocco <stefano.crocco@alice.it> 0005 0006 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0007 */ 0008 0009 #include "cache.h" 0010 #include "ui_cache.h" 0011 0012 #include <KConfigGroup> 0013 0014 #include <QDBusMessage> 0015 #include <QDBusConnection> 0016 0017 int constexpr conversionFactor = 1000000; 0018 0019 Cache::Cache(QObject *parent, const KPluginMetaData &md, const QVariantList &): KCModule(parent, md), 0020 m_ui(new Ui::Cache), 0021 m_config(KSharedConfig::openConfig(QString(), KConfig::NoGlobals)) 0022 { 0023 m_ui->setupUi(widget()); 0024 connect(m_ui->memoryCache, &QCheckBox::toggled, this, &Cache::toggleMemoryCache); 0025 connect(m_ui->cacheSize, QOverload<int>::of(&QSpinBox::valueChanged), this, [this](int){markAsChanged();}); 0026 auto changedBoolArg = [this](bool){markAsChanged();}; 0027 connect(m_ui->memoryCache, &QCheckBox::clicked, this, changedBoolArg); 0028 connect(m_ui->cacheEnabled, &QGroupBox::clicked, this, changedBoolArg); 0029 connect(m_ui->useCustomCacheDir, &QGroupBox::clicked, this, changedBoolArg); 0030 connect(m_ui->customCacheDir, &KUrlRequester::textChanged, this, [this](const QString &){markAsChanged();}); 0031 } 0032 0033 Cache::~Cache() 0034 { 0035 } 0036 0037 void Cache::defaults() 0038 { 0039 m_ui->cacheEnabled->setChecked(true); 0040 m_ui->memoryCache->setChecked(false); 0041 m_ui->cacheSize->setValue(0); 0042 m_ui->useCustomCacheDir->setChecked(false); 0043 m_ui->customCacheDir->setUrl(QUrl()); 0044 setNeedsSave(true); 0045 0046 #if QT_VERSION_MAJOR > 5 0047 setRepresentsDefaults(true); 0048 #endif 0049 } 0050 0051 void Cache::load() 0052 { 0053 KConfigGroup grp = m_config->group("Cache"); 0054 m_ui->cacheEnabled->setChecked(grp.readEntry("CacheEnabled", true)); 0055 m_ui->memoryCache->setChecked(grp.readEntry("MemoryCache", false)); 0056 int maxSizeInBytes = grp.readEntry("MaximumCacheSize", 0); 0057 //Ensure that maxSizeInMB is greater than 0 if maxSizeInBytes is not 0 0058 int maxSizeInMB = maxSizeInBytes == 0 ? 0 : std::max(1, maxSizeInBytes / conversionFactor); 0059 m_ui->cacheSize->setValue(maxSizeInMB); 0060 QString path = grp.readEntry("CustomCacheDir", QString()); 0061 m_ui->useCustomCacheDir->setChecked(!path.isEmpty()); 0062 m_ui->customCacheDir->setUrl(QUrl::fromLocalFile(path)); 0063 KCModule::load(); 0064 } 0065 0066 void Cache::save() 0067 { 0068 KConfigGroup grp = m_config->group("Cache"); 0069 bool cacheEnabled = m_ui->cacheEnabled->isChecked(); 0070 grp.writeEntry("CacheEnabled", cacheEnabled); 0071 grp.writeEntry("MemoryCache", m_ui->memoryCache->isChecked()); 0072 //We store the size in bytes, not in MB 0073 grp.writeEntry("MaximumCacheSize", m_ui->cacheSize->value()*conversionFactor); 0074 QString path = m_ui->customCacheDir->isEnabled() ? m_ui->customCacheDir->url().path() : QString(); 0075 grp.writeEntry("CustomCacheDir", path); 0076 m_config->sync(); 0077 0078 QDBusMessage message = 0079 QDBusMessage::createSignal(QStringLiteral("/KonqMain"), QStringLiteral("org.kde.Konqueror.Main"), QStringLiteral("reparseConfiguration")); 0080 QDBusConnection::sessionBus().send(message); 0081 KCModule::save(); 0082 } 0083 0084 void Cache::toggleMemoryCache(bool on) 0085 { 0086 m_ui->useCustomCacheDir->setEnabled(!on); 0087 }