File indexing completed on 2024-05-12 05:46:38

0001 /*
0002    cache.cpp - Proxy configuration dialog
0003 
0004    Copyright (C) 2001,02,03 Dawit Alemayehu <adawit@kde.org>
0005 
0006    This library is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public
0008    License (GPL) version 2 as published by the Free Software
0009    Foundation.
0010 
0011    This library is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014    Library General Public License for more details.
0015 
0016    You should have received a copy of the GNU General Public License
0017    along with this library; see the file COPYING.LIB.  If not, write to
0018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019    Boston, MA 02110-1301, USA.
0020 */
0021 
0022 // Own
0023 #include "cache.h"
0024 
0025 // Qt
0026 #include <QCheckBox>
0027 #include <QPushButton>
0028 #include <QStandardPaths>
0029 
0030 // KDE
0031 #include <kprocess.h>
0032 #include <kpluginfactory.h>
0033 #include <http_slave_defaults.h>
0034 #include <config-kiocore.h>
0035 #include <KLocalizedString>
0036 
0037 // Local
0038 #include "ksaveioconfig.h"
0039 
0040 K_PLUGIN_FACTORY_DECLARATION(KioConfigFactory)
0041 
0042 CacheConfigModule::CacheConfigModule(QWidget *parent, const QVariantList &)
0043                   :KCModule(parent)
0044 {
0045   ui.setupUi(this);
0046 }
0047 
0048 CacheConfigModule::~CacheConfigModule()
0049 {
0050 }
0051 
0052 void CacheConfigModule::load()
0053 {
0054   ui.cbUseCache->setChecked(KProtocolManager::useCache());
0055   ui.sbMaxCacheSize->setValue( KProtocolManager::maxCacheSize() );
0056 
0057   KIO::CacheControl cc = KProtocolManager::cacheControl();
0058 
0059   if (cc==KIO::CC_Verify)
0060       ui.rbVerifyCache->setChecked( true );
0061   else if (cc==KIO::CC_Refresh)
0062       ui.rbVerifyCache->setChecked( true );
0063   else if (cc==KIO::CC_CacheOnly)
0064       ui.rbOfflineMode->setChecked( true );
0065   else if (cc==KIO::CC_Cache)
0066       ui.rbCacheIfPossible->setChecked( true );
0067 
0068   // Config changed notifications...
0069   connect(ui.cbUseCache, &QAbstractButton::toggled,
0070           this, &CacheConfigModule::configChanged);
0071   connect(ui.rbVerifyCache, &QAbstractButton::toggled,
0072           this, &CacheConfigModule::configChanged);
0073   connect(ui.rbOfflineMode, &QAbstractButton::toggled,
0074           this, &CacheConfigModule::configChanged);
0075   connect(ui.rbCacheIfPossible, &QAbstractButton::toggled,
0076           this, &CacheConfigModule::configChanged);
0077   connect(ui.sbMaxCacheSize, QOverload<int>::of(&QSpinBox::valueChanged),
0078           this, &CacheConfigModule::configChanged);
0079   emit changed( false );
0080 }
0081 
0082 void CacheConfigModule::save()
0083 {
0084   KSaveIOConfig::setUseCache( ui.cbUseCache->isChecked() );
0085   KSaveIOConfig::setMaxCacheSize( ui.sbMaxCacheSize->value() );
0086 
0087   if ( !ui.cbUseCache->isChecked() )
0088       KSaveIOConfig::setCacheControl(KIO::CC_Refresh);
0089   else if ( ui.rbVerifyCache->isChecked() )
0090       KSaveIOConfig::setCacheControl(KIO::CC_Refresh);
0091   else if ( ui.rbOfflineMode->isChecked() )
0092       KSaveIOConfig::setCacheControl(KIO::CC_CacheOnly);
0093   else if ( ui.rbCacheIfPossible->isChecked() )
0094       KSaveIOConfig::setCacheControl(KIO::CC_Cache);
0095 
0096   KProtocolManager::reparseConfiguration();
0097 
0098   // Update running io-slaves...
0099   KSaveIOConfig::updateRunningIOSlaves (this);
0100 
0101   emit changed( false );
0102 }
0103 
0104 void CacheConfigModule::defaults()
0105 {
0106   ui.cbUseCache->setChecked( true );
0107   ui.rbVerifyCache->setChecked( true );
0108   ui.sbMaxCacheSize->setValue( DEFAULT_MAX_CACHE_SIZE );
0109 }
0110 
0111 QString CacheConfigModule::quickHelp() const
0112 {
0113   return i18n( "<h1>Cache</h1><p>This module lets you configure your cache settings.</p>"
0114                 "<p>This specific cache is an area on the disk where recently "
0115                 "read web pages are stored. If you want to retrieve a web "
0116                 "page again that you have recently read, it will not be "
0117                 "downloaded from the Internet, but rather retrieved from the "
0118                 "cache, which is a lot faster.</p>" );
0119 }
0120 
0121 void CacheConfigModule::configChanged()
0122 {
0123   emit changed( true );
0124 }
0125 
0126 void CacheConfigModule::on_clearCacheButton_clicked()
0127 {
0128     const QString exe = QFile::decodeName(CMAKE_INSTALL_FULL_LIBEXECDIR_KF5 "/kio_http_cache_cleaner");
0129 
0130     if (QFile::exists(exe)) {
0131         QProcess::startDetached(exe, QStringList(QStringLiteral("--clear-all")));
0132     }
0133 }
0134 
0135