File indexing completed on 2024-11-10 04:30:25

0001 
0002 // Own
0003 #include "netpref.h"
0004 
0005 // Qt
0006 #include <QCheckBox>
0007 #include <QFormLayout>
0008 #include <QGroupBox>
0009 
0010 // KDE
0011 #include <KConfig>
0012 #include <KConfigGroup>
0013 #include <KLocalizedString>
0014 #include <KPluginFactory>
0015 #include <KPluralHandlingSpinBox>
0016 #include <KProtocolManager>
0017 #include <kio/ioworker_defaults.h>
0018 
0019 // Local
0020 #include "../ksaveioconfig.h"
0021 
0022 static constexpr int s_maxTimeoutValue = 3600;
0023 
0024 K_PLUGIN_CLASS_WITH_JSON(KIOPreferences, "kcm_netpref.json")
0025 
0026 KIOPreferences::KIOPreferences(QObject *parent, const KPluginMetaData &data)
0027     : KCModule(parent, data)
0028 {
0029     QVBoxLayout *mainLayout = new QVBoxLayout(widget());
0030     mainLayout->setContentsMargins(0, 0, 0, 0);
0031     gb_Timeout = new QGroupBox(i18n("Timeout Values"), widget());
0032     gb_Timeout->setWhatsThis(
0033         i18np("Here you can set timeout values. "
0034               "You might want to tweak them if your "
0035               "connection is very slow. The maximum "
0036               "allowed value is 1 second.",
0037               "Here you can set timeout values. "
0038               "You might want to tweak them if your "
0039               "connection is very slow. The maximum "
0040               "allowed value is %1 seconds.",
0041               s_maxTimeoutValue));
0042     mainLayout->addWidget(gb_Timeout);
0043 
0044     QFormLayout *timeoutLayout = new QFormLayout(gb_Timeout);
0045     sb_socketRead = new KPluralHandlingSpinBox(widget());
0046     sb_socketRead->setSuffix(ki18np(" second", " seconds"));
0047     connect(sb_socketRead, qOverload<int>(&QSpinBox::valueChanged), this, &KIOPreferences::configChanged);
0048     timeoutLayout->addRow(i18n("Soc&ket read:"), sb_socketRead);
0049 
0050     sb_proxyConnect = new KPluralHandlingSpinBox(widget());
0051     sb_proxyConnect->setValue(0);
0052     sb_proxyConnect->setSuffix(ki18np(" second", " seconds"));
0053     connect(sb_proxyConnect, qOverload<int>(&QSpinBox::valueChanged), this, &KIOPreferences::configChanged);
0054     timeoutLayout->addRow(i18n("Pro&xy connect:"), sb_proxyConnect);
0055 
0056     sb_serverConnect = new KPluralHandlingSpinBox(widget());
0057     sb_serverConnect->setValue(0);
0058     sb_serverConnect->setSuffix(ki18np(" second", " seconds"));
0059     connect(sb_serverConnect, qOverload<int>(&QSpinBox::valueChanged), this, &KIOPreferences::configChanged);
0060     timeoutLayout->addRow(i18n("Server co&nnect:"), sb_serverConnect);
0061 
0062     sb_serverResponse = new KPluralHandlingSpinBox(widget());
0063     sb_serverResponse->setValue(0);
0064     sb_serverResponse->setSuffix(ki18np(" second", " seconds"));
0065     connect(sb_serverResponse, qOverload<int>(&QSpinBox::valueChanged), this, &KIOPreferences::configChanged);
0066     timeoutLayout->addRow(i18n("&Server response:"), sb_serverResponse);
0067 
0068     QGroupBox *gb_Global = new QGroupBox(i18n("Global Options"), widget());
0069     mainLayout->addWidget(gb_Global);
0070     QVBoxLayout *globalLayout = new QVBoxLayout(gb_Global);
0071 
0072     cb_globalMarkPartial = new QCheckBox(i18n("Mark &partially uploaded files"), widget());
0073     cb_globalMarkPartial->setWhatsThis(
0074         i18n("<p>Marks partially uploaded files "
0075              "through SMB, SFTP and other protocols."
0076              "</p><p>When this option is "
0077              "enabled, partially uploaded files "
0078              "will have a \".part\" extension. "
0079              "This extension will be removed "
0080              "once the transfer is complete.</p>"));
0081     connect(cb_globalMarkPartial, &QAbstractButton::toggled, this, &KIOPreferences::configChanged);
0082     globalLayout->addWidget(cb_globalMarkPartial);
0083 
0084     auto partialWidget = new QWidget(widget());
0085     connect(cb_globalMarkPartial, &QAbstractButton::toggled, partialWidget, &QWidget::setEnabled);
0086     globalLayout->addWidget(partialWidget);
0087     auto partialLayout = new QFormLayout(partialWidget);
0088     partialLayout->setContentsMargins(20, 0, 0, 0); // indent below mark partial
0089 
0090     sb_globalMinimumKeepSize = new KPluralHandlingSpinBox(widget());
0091     sb_globalMinimumKeepSize->setSuffix(ki18np(" byte", " bytes"));
0092     connect(sb_globalMinimumKeepSize, qOverload<int>(&QSpinBox::valueChanged), this, &KIOPreferences::configChanged);
0093     partialLayout->addRow(i18nc("@label:spinbox", "If cancelled, automatically delete partially uploaded files smaller than:"), sb_globalMinimumKeepSize);
0094 
0095     gb_Ftp = new QGroupBox(i18n("FTP Options"), widget());
0096     mainLayout->addWidget(gb_Ftp);
0097     QVBoxLayout *ftpLayout = new QVBoxLayout(gb_Ftp);
0098 
0099     cb_ftpEnablePasv = new QCheckBox(i18n("Enable passive &mode (PASV)"), widget());
0100     cb_ftpEnablePasv->setWhatsThis(
0101         i18n("Enables FTP's \"passive\" mode. "
0102              "This is required to allow FTP to "
0103              "work from behind firewalls."));
0104     connect(cb_ftpEnablePasv, &QAbstractButton::toggled, this, &KIOPreferences::configChanged);
0105     ftpLayout->addWidget(cb_ftpEnablePasv);
0106 
0107     cb_ftpMarkPartial = new QCheckBox(i18n("Mark &partially uploaded files"), widget());
0108     cb_ftpMarkPartial->setWhatsThis(
0109         i18n("<p>Marks partially uploaded FTP "
0110              "files.</p><p>When this option is "
0111              "enabled, partially uploaded files "
0112              "will have a \".part\" extension. "
0113              "This extension will be removed "
0114              "once the transfer is complete.</p>"));
0115     connect(cb_ftpMarkPartial, &QAbstractButton::toggled, this, &KIOPreferences::configChanged);
0116     ftpLayout->addWidget(cb_ftpMarkPartial);
0117 
0118     mainLayout->addStretch(1);
0119 }
0120 
0121 KIOPreferences::~KIOPreferences()
0122 {
0123 }
0124 
0125 void KIOPreferences::load()
0126 {
0127     KProtocolManager proto;
0128 
0129     sb_socketRead->setRange(MIN_TIMEOUT_VALUE, s_maxTimeoutValue);
0130     sb_serverResponse->setRange(MIN_TIMEOUT_VALUE, s_maxTimeoutValue);
0131     sb_serverConnect->setRange(MIN_TIMEOUT_VALUE, s_maxTimeoutValue);
0132     sb_proxyConnect->setRange(MIN_TIMEOUT_VALUE, s_maxTimeoutValue);
0133 
0134     sb_socketRead->setValue(proto.readTimeout());
0135     sb_serverResponse->setValue(proto.responseTimeout());
0136     sb_serverConnect->setValue(proto.connectTimeout());
0137     sb_proxyConnect->setValue(proto.proxyConnectTimeout());
0138 
0139     cb_globalMarkPartial->setChecked(proto.markPartial());
0140     sb_globalMinimumKeepSize->setRange(0, 1024 * 1024 * 1024 /* 1 GiB */);
0141     sb_globalMinimumKeepSize->setValue(proto.minimumKeepSize());
0142 
0143     KConfig config(QStringLiteral("kio_ftprc"), KConfig::NoGlobals);
0144     cb_ftpEnablePasv->setChecked(!config.group(QString()).readEntry("DisablePassiveMode", false));
0145     cb_ftpMarkPartial->setChecked(config.group(QString()).readEntry("MarkPartial", true));
0146     setNeedsSave(false);
0147 }
0148 
0149 void KIOPreferences::save()
0150 {
0151     KSaveIOConfig::setReadTimeout(sb_socketRead->value());
0152     KSaveIOConfig::setResponseTimeout(sb_serverResponse->value());
0153     KSaveIOConfig::setConnectTimeout(sb_serverConnect->value());
0154     KSaveIOConfig::setProxyConnectTimeout(sb_proxyConnect->value());
0155 
0156     KSaveIOConfig::setMarkPartial(cb_globalMarkPartial->isChecked());
0157     KSaveIOConfig::setMinimumKeepSize(sb_globalMinimumKeepSize->value());
0158 
0159     KConfig config(QStringLiteral("kio_ftprc"), KConfig::NoGlobals);
0160     config.group(QString()).writeEntry("DisablePassiveMode", !cb_ftpEnablePasv->isChecked());
0161     config.group(QString()).writeEntry("MarkPartial", cb_ftpMarkPartial->isChecked());
0162     config.sync();
0163 
0164     KSaveIOConfig::updateRunningWorkers(widget());
0165 
0166     setNeedsSave(false);
0167 }
0168 
0169 void KIOPreferences::defaults()
0170 {
0171     sb_socketRead->setValue(DEFAULT_READ_TIMEOUT);
0172     sb_serverResponse->setValue(DEFAULT_RESPONSE_TIMEOUT);
0173     sb_serverConnect->setValue(DEFAULT_CONNECT_TIMEOUT);
0174     sb_proxyConnect->setValue(DEFAULT_PROXY_CONNECT_TIMEOUT);
0175 
0176     cb_globalMarkPartial->setChecked(true);
0177 
0178     cb_ftpEnablePasv->setChecked(true);
0179     cb_ftpMarkPartial->setChecked(true);
0180 
0181     setNeedsSave(true);
0182 }
0183 
0184 #include "netpref.moc"