1 /***************************************************************************
2 * Copyright (C) 2005-2020 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"
27 DccSettingsPage::DccSettingsPage(QWidget* parent)
28 : SettingsPage(tr("IRC"), tr("DCC"), parent)
32 connect(ui.ipDetectionMode, selectOverload<int>(&QComboBox::currentIndexChanged), this, &DccSettingsPage::updateWidgetStates);
33 connect(ui.portSelectionMode, selectOverload<int>(&QComboBox::currentIndexChanged), this, &DccSettingsPage::updateWidgetStates);
36 connect(Client::instance(), &Client::coreConnectionStateChanged, this, &DccSettingsPage::onClientConfigChanged);
37 setClientConfig(Client::dccConfig());
40 bool DccSettingsPage::isClientConfigValid() const
42 return _clientConfig != nullptr;
45 void DccSettingsPage::setClientConfig(DccConfig* config)
48 disconnect(_clientConfig, nullptr, this, nullptr);
50 if (config && !isClientConfigValid()) {
51 qWarning() << "Client DCC config is not valid/synchronized!";
52 _clientConfig = nullptr;
53 ui.dccEnabled->setEnabled(false);
56 _clientConfig = config;
58 connect(_clientConfig, &DccConfig::updated, this, &DccSettingsPage::load);
60 ui.dccEnabled->setEnabled(true);
63 ui.dccEnabled->setEnabled(false);
67 void DccSettingsPage::onClientConfigChanged()
69 if (Client::isConnected() && Client::dccConfig() && !Client::dccConfig()->isInitialized()) {
70 connect(Client::dccConfig(), &SyncableObject::initDone, this, &DccSettingsPage::onClientConfigChanged);
73 setClientConfig(Client::isConnected() ? Client::dccConfig() : nullptr);
77 bool DccSettingsPage::hasDefaults() const
82 void DccSettingsPage::defaults()
84 _localConfig.fromVariantMap(DccConfig{}.toVariantMap());
89 void DccSettingsPage::load()
91 _localConfig.fromVariantMap(isClientConfigValid() ? _clientConfig->toVariantMap() : DccConfig{}.toVariantMap());
96 void DccSettingsPage::save()
99 if (isClientConfigValid()) {
100 Client::dccConfig()->requestUpdate(_localConfig.toVariantMap());
102 setChangedState(false);
105 QVariant DccSettingsPage::loadAutoWidgetValue(const QString& widgetName)
107 if (widgetName == "dccEnabled")
108 return _localConfig.isDccEnabled();
109 if (widgetName == "ipDetectionMode")
110 // NOTE: Use mapping if item order differs from enum order
111 return static_cast<int>(_localConfig.ipDetectionMode());
112 if (widgetName == "portSelectionMode")
113 // NOTE: Use mapping if item order differs from enum order
114 return static_cast<int>(_localConfig.portSelectionMode());
115 if (widgetName == "minPort")
116 return _localConfig.minPort();
117 if (widgetName == "maxPort")
118 return _localConfig.maxPort();
119 if (widgetName == "chunkSize")
120 return _localConfig.chunkSize();
121 if (widgetName == "sendTimeout")
122 return _localConfig.sendTimeout();
123 if (widgetName == "usePassiveDcc")
124 return _localConfig.usePassiveDcc();
125 if (widgetName == "useFastSend")
126 return _localConfig.useFastSend();
127 if (widgetName == "outgoingIp")
128 return _localConfig.outgoingIp().toString();
130 qWarning() << "Unknown auto widget" << widgetName;
134 void DccSettingsPage::saveAutoWidgetValue(const QString& widgetName, const QVariant& value)
136 if (widgetName == "dccEnabled")
137 _localConfig.setDccEnabled(value.toBool());
138 else if (widgetName == "ipDetectionMode")
139 // NOTE: Use mapping if item order differs from enum order
140 _localConfig.setIpDetectionMode(static_cast<DccConfig::IpDetectionMode>(value.toInt()));
141 else if (widgetName == "portSelectionMode")
142 // NOTE: Use mapping if item order differs from enum order
143 _localConfig.setPortSelectionMode(static_cast<DccConfig::PortSelectionMode>(value.toInt()));
144 else if (widgetName == "minPort")
145 _localConfig.setMinPort(value.toInt());
146 else if (widgetName == "maxPort")
147 _localConfig.setMaxPort(value.toInt());
148 else if (widgetName == "chunkSize")
149 _localConfig.setChunkSize(value.toInt());
150 else if (widgetName == "sendTimeout")
151 _localConfig.setSendTimeout(value.toInt());
152 else if (widgetName == "usePassiveDcc")
153 _localConfig.setUsePassiveDcc(value.toBool());
154 else if (widgetName == "useFastSend")
155 _localConfig.setUseFastSend(value.toBool());
156 else if (widgetName == "outgoingIp") {
157 QHostAddress address{QHostAddress::LocalHost};
158 if (!address.setAddress(value.toString())) {
159 qWarning() << "Invalid IP address!";
160 address = QHostAddress{QHostAddress::LocalHost};
162 _localConfig.setOutgoingIp(std::move(address));
165 qWarning() << "Unknown auto widget" << widgetName;
169 void DccSettingsPage::widgetHasChanged()
171 bool same = isClientConfigValid() && (_localConfig == *_clientConfig);
172 setChangedState(!same);
175 void DccSettingsPage::updateWidgetStates()
177 ui.outgoingIp->setEnabled(ui.ipDetectionMode->currentIndex() != 0);
178 bool enablePorts = ui.portSelectionMode->currentIndex() != 0;
179 ui.minPort->setEnabled(enablePorts);
180 ui.maxPort->setEnabled(enablePorts);
181 ui.portsToLabel->setEnabled(enablePorts);