cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / qtui / settingspages / dccsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2022 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 #include "util.h"
26
27 DccSettingsPage::DccSettingsPage(QWidget* parent)
28     : SettingsPage(tr("IRC"), tr("DCC"), parent)
29 {
30     ui.setupUi(this);
31     initAutoWidgets();
32     connect(ui.ipDetectionMode, selectOverload<int>(&QComboBox::currentIndexChanged), this, &DccSettingsPage::updateWidgetStates);
33     connect(ui.portSelectionMode, selectOverload<int>(&QComboBox::currentIndexChanged), this, &DccSettingsPage::updateWidgetStates);
34     updateWidgetStates();
35
36     connect(Client::instance(), &Client::coreConnectionStateChanged, this, &DccSettingsPage::onClientConfigChanged);
37     setClientConfig(Client::dccConfig());
38 }
39
40 bool DccSettingsPage::isClientConfigValid() const
41 {
42     return _clientConfig != nullptr;
43 }
44
45 void DccSettingsPage::setClientConfig(DccConfig* config)
46 {
47     if (_clientConfig) {
48         disconnect(_clientConfig, nullptr, this, nullptr);
49     }
50     if (config && !isClientConfigValid()) {
51         qWarning() << "Client DCC config is not valid/synchronized!";
52         _clientConfig = nullptr;
53         ui.dccEnabled->setEnabled(false);
54         return;
55     }
56     _clientConfig = config;
57     if (_clientConfig) {
58         connect(_clientConfig, &DccConfig::updated, this, &DccSettingsPage::load);
59         load();
60         ui.dccEnabled->setEnabled(true);
61     }
62     else {
63         ui.dccEnabled->setEnabled(false);
64     }
65 }
66
67 void DccSettingsPage::onClientConfigChanged()
68 {
69     if (Client::isConnected() && Client::dccConfig() && !Client::dccConfig()->isInitialized()) {
70         connect(Client::dccConfig(), &SyncableObject::initDone, this, &DccSettingsPage::onClientConfigChanged);
71     }
72     else {
73         setClientConfig(Client::isConnected() ? Client::dccConfig() : nullptr);
74     }
75 }
76
77 bool DccSettingsPage::hasDefaults() const
78 {
79     return true;
80 }
81
82 void DccSettingsPage::defaults()
83 {
84     _localConfig.fromVariantMap(DccConfig{}.toVariantMap());
85     SettingsPage::load();
86     widgetHasChanged();
87 }
88
89 void DccSettingsPage::load()
90 {
91     _localConfig.fromVariantMap(isClientConfigValid() ? _clientConfig->toVariantMap() : DccConfig{}.toVariantMap());
92     SettingsPage::load();
93     widgetHasChanged();
94 }
95
96 void DccSettingsPage::save()
97 {
98     SettingsPage::save();
99     if (isClientConfigValid()) {
100         Client::dccConfig()->requestUpdate(_localConfig.toVariantMap());
101     }
102     setChangedState(false);
103 }
104
105 QVariant DccSettingsPage::loadAutoWidgetValue(const QString& widgetName)
106 {
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();
129
130     qWarning() << "Unknown auto widget" << widgetName;
131     return {};
132 }
133
134 void DccSettingsPage::saveAutoWidgetValue(const QString& widgetName, const QVariant& value)
135 {
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};
161         }
162         _localConfig.setOutgoingIp(std::move(address));
163     }
164     else {
165         qWarning() << "Unknown auto widget" << widgetName;
166     }
167 }
168
169 void DccSettingsPage::widgetHasChanged()
170 {
171     bool same = isClientConfigValid() && (_localConfig == *_clientConfig);
172     setChangedState(!same);
173 }
174
175 void DccSettingsPage::updateWidgetStates()
176 {
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);
182 }