62fce35f54483a396ed59cafbd9ed42a97c9cc74
[quassel.git] / src / qtui / settingspages / dccsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "dccsettingspage.h"
22
23 #include "client.h"
24 #include "clienttransfermanager.h"
25
26 DccSettingsPage::DccSettingsPage(QWidget *parent)
27     : SettingsPage(tr("IRC"), tr("DCC"), parent)
28 {
29     ui.setupUi(this);
30     initAutoWidgets();
31     connect(ui.ipDetectionMode, SIGNAL(currentIndexChanged(int)), SLOT(updateWidgetStates()));
32     connect(ui.portSelectionMode, SIGNAL(currentIndexChanged(int)), SLOT(updateWidgetStates()));
33     updateWidgetStates();
34
35     connect(Client::instance(), SIGNAL(coreConnectionStateChanged(bool)), SLOT(onClientConfigChanged()));
36     setClientConfig(Client::dccConfig());
37 }
38
39
40 bool DccSettingsPage::isClientConfigValid() const
41 {
42     return _clientConfig != nullptr;
43 }
44
45
46 void DccSettingsPage::setClientConfig(DccConfig *config)
47 {
48     if (_clientConfig) {
49         disconnect(_clientConfig, 0, this, 0);
50     }
51     if (config && !isClientConfigValid()) {
52         qWarning() << "Client DCC config is not valid/synchronized!";
53         _clientConfig = nullptr;
54         ui.dccEnabled->setEnabled(false);
55         return;
56     }
57     _clientConfig = config;
58     if (_clientConfig) {
59         connect(_clientConfig, SIGNAL(updated()), SLOT(load()));
60         load();
61         ui.dccEnabled->setEnabled(true);
62     }
63     else {
64         ui.dccEnabled->setEnabled(false);
65     }
66 }
67
68
69 void DccSettingsPage::onClientConfigChanged()
70 {
71     if (Client::isConnected() && Client::dccConfig() && !Client::dccConfig()->isInitialized()) {
72         connect(Client::dccConfig(), SIGNAL(initDone()), SLOT(onClientConfigChanged()));
73     }
74     else {
75         setClientConfig(Client::isConnected() ? Client::dccConfig() : nullptr);
76     }
77 }
78
79
80 bool DccSettingsPage::hasDefaults() const
81 {
82     return true;
83 }
84
85
86 void DccSettingsPage::defaults()
87 {
88     _localConfig = DccConfig();
89     SettingsPage::load();
90     widgetHasChanged();
91 }
92
93
94 void DccSettingsPage::load()
95 {
96     _localConfig = isClientConfigValid() ? *_clientConfig : DccConfig{};
97     SettingsPage::load();
98     widgetHasChanged();
99 }
100
101
102 void DccSettingsPage::save()
103 {
104     SettingsPage::save();
105     if (isClientConfigValid()) {
106         Client::dccConfig()->requestUpdate(_localConfig.toVariantMap());
107     }
108     setChangedState(false);
109 }
110
111
112 QVariant DccSettingsPage::loadAutoWidgetValue(const QString& widgetName)
113 {
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();
136
137     qWarning() << "Unknown auto widget" << widgetName;
138     return {};
139 }
140
141
142 void DccSettingsPage::saveAutoWidgetValue(const QString& widgetName, const QVariant& value)
143 {
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};
169         }
170         _localConfig.setOutgoingIp(std::move(address));
171     }
172     else {
173         qWarning() << "Unknown auto widget" << widgetName;
174     }
175 }
176
177
178 void DccSettingsPage::widgetHasChanged()
179 {
180     bool same = isClientConfigValid() && (_localConfig == *_clientConfig);
181     setChangedState(!same);
182 }
183
184
185 void DccSettingsPage::updateWidgetStates()
186 {
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);
192 }