modernize: Replace most remaining old-style connects by PMF ones
[quassel.git] / src / qtui / settingspages / dccsettingspage.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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
41 bool DccSettingsPage::isClientConfigValid() const
42 {
43     return _clientConfig != nullptr;
44 }
45
46
47 void DccSettingsPage::setClientConfig(DccConfig *config)
48 {
49     if (_clientConfig) {
50         disconnect(_clientConfig, nullptr, this, nullptr);
51     }
52     if (config && !isClientConfigValid()) {
53         qWarning() << "Client DCC config is not valid/synchronized!";
54         _clientConfig = nullptr;
55         ui.dccEnabled->setEnabled(false);
56         return;
57     }
58     _clientConfig = config;
59     if (_clientConfig) {
60         connect(_clientConfig, &DccConfig::updated, this, &DccSettingsPage::load);
61         load();
62         ui.dccEnabled->setEnabled(true);
63     }
64     else {
65         ui.dccEnabled->setEnabled(false);
66     }
67 }
68
69
70 void DccSettingsPage::onClientConfigChanged()
71 {
72     if (Client::isConnected() && Client::dccConfig() && !Client::dccConfig()->isInitialized()) {
73         connect(Client::dccConfig(), &SyncableObject::initDone, this, &DccSettingsPage::onClientConfigChanged);
74     }
75     else {
76         setClientConfig(Client::isConnected() ? Client::dccConfig() : nullptr);
77     }
78 }
79
80
81 bool DccSettingsPage::hasDefaults() const
82 {
83     return true;
84 }
85
86
87 void DccSettingsPage::defaults()
88 {
89     _localConfig = DccConfig();
90     SettingsPage::load();
91     widgetHasChanged();
92 }
93
94
95 void DccSettingsPage::load()
96 {
97     _localConfig = isClientConfigValid() ? *_clientConfig : DccConfig{};
98     SettingsPage::load();
99     widgetHasChanged();
100 }
101
102
103 void DccSettingsPage::save()
104 {
105     SettingsPage::save();
106     if (isClientConfigValid()) {
107         Client::dccConfig()->requestUpdate(_localConfig.toVariantMap());
108     }
109     setChangedState(false);
110 }
111
112
113 QVariant DccSettingsPage::loadAutoWidgetValue(const QString& widgetName)
114 {
115     if (widgetName == "dccEnabled")
116         return _localConfig.isDccEnabled();
117     if (widgetName == "ipDetectionMode")
118         // NOTE: Use mapping if item order differs from enum order
119         return static_cast<int>(_localConfig.ipDetectionMode());
120     if (widgetName == "portSelectionMode")
121         // NOTE: Use mapping if item order differs from enum order
122         return static_cast<int>(_localConfig.portSelectionMode());
123     if (widgetName == "minPort")
124         return _localConfig.minPort();
125     if (widgetName == "maxPort")
126         return _localConfig.maxPort();
127     if (widgetName == "chunkSize")
128         return _localConfig.chunkSize();
129     if (widgetName == "sendTimeout")
130         return _localConfig.sendTimeout();
131     if (widgetName == "usePassiveDcc")
132         return _localConfig.usePassiveDcc();
133     if (widgetName == "useFastSend")
134         return _localConfig.useFastSend();
135     if (widgetName == "outgoingIp")
136         return _localConfig.outgoingIp().toString();
137
138     qWarning() << "Unknown auto widget" << widgetName;
139     return {};
140 }
141
142
143 void DccSettingsPage::saveAutoWidgetValue(const QString& widgetName, const QVariant& value)
144 {
145     if (widgetName == "dccEnabled")
146         _localConfig.setDccEnabled(value.toBool());
147     else if (widgetName == "ipDetectionMode")
148         // NOTE: Use mapping if item order differs from enum order
149         _localConfig.setIpDetectionMode(static_cast<DccConfig::IpDetectionMode>(value.toInt()));
150     else if (widgetName == "portSelectionMode")
151         // NOTE: Use mapping if item order differs from enum order
152         _localConfig.setPortSelectionMode(static_cast<DccConfig::PortSelectionMode>(value.toInt()));
153     else if (widgetName == "minPort")
154         _localConfig.setMinPort(value.toInt());
155     else if (widgetName == "maxPort")
156         _localConfig.setMaxPort(value.toInt());
157     else if (widgetName == "chunkSize")
158         _localConfig.setChunkSize(value.toInt());
159     else if (widgetName == "sendTimeout")
160         _localConfig.setSendTimeout(value.toInt());
161     else if (widgetName == "usePassiveDcc")
162         _localConfig.setUsePassiveDcc(value.toBool());
163     else if (widgetName == "useFastSend")
164         _localConfig.setUseFastSend(value.toBool());
165     else if (widgetName == "outgoingIp") {
166         QHostAddress address {QHostAddress::LocalHost};
167         if (!address.setAddress(value.toString())) {
168             qWarning() << "Invalid IP address!";
169             address = QHostAddress{QHostAddress::LocalHost};
170         }
171         _localConfig.setOutgoingIp(std::move(address));
172     }
173     else {
174         qWarning() << "Unknown auto widget" << widgetName;
175     }
176 }
177
178
179 void DccSettingsPage::widgetHasChanged()
180 {
181     bool same = isClientConfigValid() && (_localConfig == *_clientConfig);
182     setChangedState(!same);
183 }
184
185
186 void DccSettingsPage::updateWidgetStates()
187 {
188     ui.outgoingIp->setEnabled(ui.ipDetectionMode->currentIndex() != 0);
189     bool enablePorts = ui.portSelectionMode->currentIndex() != 0;
190     ui.minPort->setEnabled(enablePorts);
191     ui.maxPort->setEnabled(enablePorts);
192     ui.portsToLabel->setEnabled(enablePorts);
193 }