File indexing completed on 2024-04-21 11:33:11

0001 /*
0002     SPDX-FileCopyrightText: 2001 Dawit Alemayehu <adawit@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 // Own
0008 #include "ksaveioconfig.h"
0009 
0010 // Qt
0011 #include <QDBusConnection>
0012 #include <QDBusInterface>
0013 #include <QDBusMessage>
0014 #include <QDBusReply>
0015 
0016 // KDE
0017 #include <KConfig>
0018 #include <KConfigGroup>
0019 #include <KLocalizedString>
0020 #include <KMessageBox>
0021 #include <ioworker_defaults.h>
0022 class KSaveIOConfigPrivate
0023 {
0024 public:
0025     KSaveIOConfigPrivate();
0026     ~KSaveIOConfigPrivate();
0027 
0028     KConfig *config = nullptr;
0029     KConfig *http_config = nullptr;
0030 };
0031 
0032 Q_GLOBAL_STATIC(KSaveIOConfigPrivate, d)
0033 
0034 KSaveIOConfigPrivate::KSaveIOConfigPrivate()
0035 {
0036 }
0037 
0038 KSaveIOConfigPrivate::~KSaveIOConfigPrivate()
0039 {
0040     delete config;
0041     delete http_config;
0042 }
0043 
0044 static KConfig *config()
0045 {
0046     if (!d->config) {
0047         d->config = new KConfig(QStringLiteral("kioslaverc"), KConfig::NoGlobals);
0048         // KF6 TODO: rename to kioworkerrc here and elsewhere. See also T15956.
0049     }
0050 
0051     return d->config;
0052 }
0053 
0054 static KConfig *http_config()
0055 {
0056     if (!d->http_config) {
0057         d->http_config = new KConfig(QStringLiteral("kio_httprc"), KConfig::NoGlobals);
0058     }
0059 
0060     return d->http_config;
0061 }
0062 
0063 int KSaveIOConfig::proxyDisplayUrlFlags()
0064 {
0065     KConfigGroup cfg(config(), QString());
0066     return cfg.readEntry("ProxyUrlDisplayFlags", 0);
0067 }
0068 
0069 void KSaveIOConfig::setProxyDisplayUrlFlags(int flags)
0070 {
0071     KConfigGroup cfg(config(), QString());
0072     cfg.writeEntry("ProxyUrlDisplayFlags", flags);
0073     cfg.sync();
0074 }
0075 
0076 void KSaveIOConfig::reparseConfiguration()
0077 {
0078     delete d->config;
0079     d->config = nullptr;
0080     delete d->http_config;
0081     d->http_config = nullptr;
0082 }
0083 
0084 void KSaveIOConfig::setReadTimeout(int _timeout)
0085 {
0086     KConfigGroup cfg(config(), QString());
0087     cfg.writeEntry("ReadTimeout", qMax(MIN_TIMEOUT_VALUE, _timeout));
0088     cfg.sync();
0089 }
0090 
0091 void KSaveIOConfig::setConnectTimeout(int _timeout)
0092 {
0093     KConfigGroup cfg(config(), QString());
0094     cfg.writeEntry("ConnectTimeout", qMax(MIN_TIMEOUT_VALUE, _timeout));
0095     cfg.sync();
0096 }
0097 
0098 void KSaveIOConfig::setProxyConnectTimeout(int _timeout)
0099 {
0100     KConfigGroup cfg(config(), QString());
0101     cfg.writeEntry("ProxyConnectTimeout", qMax(MIN_TIMEOUT_VALUE, _timeout));
0102     cfg.sync();
0103 }
0104 
0105 void KSaveIOConfig::setResponseTimeout(int _timeout)
0106 {
0107     KConfigGroup cfg(config(), QString());
0108     cfg.writeEntry("ResponseTimeout", qMax(MIN_TIMEOUT_VALUE, _timeout));
0109     cfg.sync();
0110 }
0111 
0112 void KSaveIOConfig::setMarkPartial(bool _mode)
0113 {
0114     KConfigGroup cfg(config(), QString());
0115     cfg.writeEntry("MarkPartial", _mode);
0116     cfg.sync();
0117 }
0118 
0119 void KSaveIOConfig::setMinimumKeepSize(int _size)
0120 {
0121     KConfigGroup cfg(config(), QString());
0122     cfg.writeEntry("MinimumKeepSize", _size);
0123     cfg.sync();
0124 }
0125 
0126 void KSaveIOConfig::setAutoResume(bool _mode)
0127 {
0128     KConfigGroup cfg(config(), QString());
0129     cfg.writeEntry("AutoResume", _mode);
0130     cfg.sync();
0131 }
0132 
0133 void KSaveIOConfig::setUseCache(bool _mode)
0134 {
0135     KConfigGroup cfg(http_config(), QString());
0136     cfg.writeEntry("UseCache", _mode);
0137     cfg.sync();
0138 }
0139 
0140 void KSaveIOConfig::setMaxCacheSize(int cache_size)
0141 {
0142     KConfigGroup cfg(http_config(), QString());
0143     cfg.writeEntry("MaxCacheSize", cache_size);
0144     cfg.sync();
0145 }
0146 
0147 void KSaveIOConfig::setCacheControl(KIO::CacheControl policy)
0148 {
0149     KConfigGroup cfg(http_config(), QString());
0150     QString tmp = KIO::getCacheControlString(policy);
0151     cfg.writeEntry("cache", tmp);
0152     cfg.sync();
0153 }
0154 
0155 void KSaveIOConfig::setMaxCacheAge(int cache_age)
0156 {
0157     KConfigGroup cfg(http_config(), QString());
0158     cfg.writeEntry("MaxCacheAge", cache_age);
0159     cfg.sync();
0160 }
0161 
0162 void KSaveIOConfig::setUseReverseProxy(bool mode)
0163 {
0164     KConfigGroup cfg(config(), "Proxy Settings");
0165     cfg.writeEntry("ReversedException", mode);
0166     cfg.sync();
0167 }
0168 
0169 void KSaveIOConfig::setProxyType(KProtocolManager::ProxyType type)
0170 {
0171     KConfigGroup cfg(config(), "Proxy Settings");
0172     cfg.writeEntry("ProxyType", static_cast<int>(type));
0173     cfg.sync();
0174 }
0175 
0176 QString KSaveIOConfig::noProxyFor()
0177 {
0178     KConfigGroup cfg(config(), "Proxy Settings");
0179     return cfg.readEntry("NoProxyFor");
0180 }
0181 
0182 void KSaveIOConfig::setNoProxyFor(const QString &_noproxy)
0183 {
0184     KConfigGroup cfg(config(), "Proxy Settings");
0185     cfg.writeEntry("NoProxyFor", _noproxy);
0186     cfg.sync();
0187 }
0188 
0189 void KSaveIOConfig::setProxyFor(const QString &protocol, const QString &_proxy)
0190 {
0191     KConfigGroup cfg(config(), "Proxy Settings");
0192     cfg.writeEntry(protocol.toLower() + QLatin1String("Proxy"), _proxy);
0193     cfg.sync();
0194 }
0195 
0196 void KSaveIOConfig::setProxyConfigScript(const QString &_url)
0197 {
0198     KConfigGroup cfg(config(), "Proxy Settings");
0199     cfg.writeEntry("Proxy Config Script", _url);
0200     cfg.sync();
0201 }
0202 
0203 void KSaveIOConfig::updateRunningWorkers(QWidget *parent)
0204 {
0205     // Inform all running KIO workers about the changes...
0206     // if we cannot update, KIO workers inform the end user...
0207     QDBusMessage message =
0208         QDBusMessage::createSignal(QStringLiteral("/KIO/Scheduler"), QStringLiteral("org.kde.KIO.Scheduler"), QStringLiteral("reparseSlaveConfiguration"));
0209     message << QString();
0210     if (!QDBusConnection::sessionBus().send(message)) {
0211         KMessageBox::information(parent,
0212                                  i18n("You have to restart the running applications "
0213                                       "for these changes to take effect."),
0214                                  i18nc("@title:window", "Update Failed"));
0215     }
0216 }
0217 
0218 void KSaveIOConfig::updateProxyScout(QWidget *parent)
0219 {
0220     // Inform the proxyscout kded module about changes if we cannot update,
0221     // KIO workers inform the end user...
0222     QDBusInterface kded(QStringLiteral("org.kde.kcookiejar5"), QStringLiteral("/modules/proxyscout"), QStringLiteral("org.kde.KPAC.ProxyScout"));
0223     QDBusReply<void> reply = kded.call(QStringLiteral("reset"));
0224     if (!reply.isValid()) {
0225         KMessageBox::information(parent, i18n("You have to restart KDE for these changes to take effect."), i18nc("@title:window", "Update Failed"));
0226     }
0227 }