1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "dccsettingspage.h"
24 #include "clienttransfermanager.h"
26 DccSettingsPage::DccSettingsPage(QWidget *parent)
27 : SettingsPage(tr("IRC"), tr("DCC"), parent)
31 connect(ui.ipDetectionMode, SIGNAL(currentIndexChanged(int)), SLOT(updateWidgetStates()));
32 connect(ui.portSelectionMode, SIGNAL(currentIndexChanged(int)), SLOT(updateWidgetStates()));
35 connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), SLOT(onClientConfigChanged()));
36 setClientConfig(Client::dccConfig());
40 bool DccSettingsPage::isClientConfigValid() const
42 return _clientConfig != nullptr;
46 void DccSettingsPage::setClientConfig(DccConfig *config)
49 disconnect(_clientConfig, 0, this, 0);
51 if (config && !isClientConfigValid()) {
52 qWarning() << "Client DCC config is not valid/synchronized!";
53 _clientConfig = nullptr;
54 ui.dccEnabled->setEnabled(false);
57 _clientConfig = config;
59 connect(_clientConfig, SIGNAL(updated()), SLOT(load()));
61 ui.dccEnabled->setEnabled(true);
64 ui.dccEnabled->setEnabled(false);
69 void DccSettingsPage::onClientConfigChanged()
71 if (Client::isConnected() && Client::dccConfig() && !Client::dccConfig()->isInitialized()) {
72 connect(Client::dccConfig(), SIGNAL(initDone()), SLOT(onClientConfigChanged()));
75 setClientConfig(Client::isConnected() ? Client::dccConfig() : nullptr);
80 bool DccSettingsPage::hasDefaults() const
86 void DccSettingsPage::defaults()
88 _localConfig = DccConfig();
94 void DccSettingsPage::load()
96 _localConfig = isClientConfigValid() ? *_clientConfig : DccConfig{};
102 void DccSettingsPage::save()
104 SettingsPage::save();
105 if (isClientConfigValid()) {
106 Client::dccConfig()->requestUpdate(_localConfig.toVariantMap());
108 setChangedState(false);
112 QVariant DccSettingsPage::loadAutoWidgetValue(const QString& widgetName)
114 if (widgetName == "dccEnabled")
115 return _localConfig.isDccEnabled();
116 if (widgetName == "ipDetectionMode")
117 // NOTE: Use mapping if item order differs from enum order
118 return static_cast<int>(_localConfig.ipDetectionMode());
119 if (widgetName == "portSelectionMode")
120 // NOTE: Use mapping if item order differs from enum order
121 return static_cast<int>(_localConfig.portSelectionMode());
122 if (widgetName == "minPort")
123 return _localConfig.minPort();
124 if (widgetName == "maxPort")
125 return _localConfig.maxPort();
126 if (widgetName == "chunkSize")
127 return _localConfig.chunkSize();
128 if (widgetName == "sendTimeout")
129 return _localConfig.sendTimeout();
130 if (widgetName == "usePassiveDcc")
131 return _localConfig.usePassiveDcc();
132 if (widgetName == "useFastSend")
133 return _localConfig.useFastSend();
134 if (widgetName == "outgoingIp")
135 return _localConfig.outgoingIp().toString();
137 qWarning() << "Unknown auto widget" << widgetName;
142 void DccSettingsPage::saveAutoWidgetValue(const QString& widgetName, const QVariant& value)
144 if (widgetName == "dccEnabled")
145 _localConfig.setDccEnabled(value.toBool());
146 else if (widgetName == "ipDetectionMode")
147 // NOTE: Use mapping if item order differs from enum order
148 _localConfig.setIpDetectionMode(static_cast<DccConfig::IpDetectionMode>(value.toInt()));
149 else if (widgetName == "portSelectionMode")
150 // NOTE: Use mapping if item order differs from enum order
151 _localConfig.setPortSelectionMode(static_cast<DccConfig::PortSelectionMode>(value.toInt()));
152 else if (widgetName == "minPort")
153 _localConfig.setMinPort(value.toInt());
154 else if (widgetName == "maxPort")
155 _localConfig.setMaxPort(value.toInt());
156 else if (widgetName == "chunkSize")
157 _localConfig.setChunkSize(value.toInt());
158 else if (widgetName == "sendTimeout")
159 _localConfig.setSendTimeout(value.toInt());
160 else if (widgetName == "usePassiveDcc")
161 _localConfig.setUsePassiveDcc(value.toBool());
162 else if (widgetName == "useFastSend")
163 _localConfig.setUseFastSend(value.toBool());
164 else if (widgetName == "outgoingIp") {
165 QHostAddress address {QHostAddress::LocalHost};
166 if (!address.setAddress(value.toString())) {
167 qWarning() << "Invalid IP address!";
168 address = QHostAddress{QHostAddress::LocalHost};
170 _localConfig.setOutgoingIp(std::move(address));
173 qWarning() << "Unknown auto widget" << widgetName;
178 void DccSettingsPage::widgetHasChanged()
180 bool same = isClientConfigValid() && (_localConfig == *_clientConfig);
181 setChangedState(!same);
185 void DccSettingsPage::updateWidgetStates()
187 ui.outgoingIp->setEnabled(ui.ipDetectionMode->currentIndex() != 0);
188 bool enablePorts = ui.portSelectionMode->currentIndex() != 0;
189 ui.minPort->setEnabled(enablePorts);
190 ui.maxPort->setEnabled(enablePorts);
191 ui.portsToLabel->setEnabled(enablePorts);