1 | /* $Id: UIGlobalSettingsProxy.cpp 101241 2023-09-22 15:40:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIGlobalSettingsProxy class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | /* Qt includes: */
|
---|
29 | #include <QVBoxLayout>
|
---|
30 |
|
---|
31 | /* GUI includes: */
|
---|
32 | #include "UIGlobalSettingsProxy.h"
|
---|
33 | #include "UIErrorString.h"
|
---|
34 | #include "UIExtraDataManager.h"
|
---|
35 | #include "UIProxyFeaturesEditor.h"
|
---|
36 |
|
---|
37 | /* COM includes: */
|
---|
38 | #include "CSystemProperties.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | /** Global settings: Proxy page data structure. */
|
---|
42 | struct UIDataSettingsGlobalProxy
|
---|
43 | {
|
---|
44 | /** Constructs data. */
|
---|
45 | UIDataSettingsGlobalProxy()
|
---|
46 | : m_enmProxyMode(KProxyMode_System)
|
---|
47 | , m_strProxyHost(QString())
|
---|
48 | {}
|
---|
49 |
|
---|
50 | /** Returns whether the @a other passed data is equal to this one. */
|
---|
51 | bool equal(const UIDataSettingsGlobalProxy &other) const
|
---|
52 | {
|
---|
53 | return true
|
---|
54 | && (m_enmProxyMode == other.m_enmProxyMode)
|
---|
55 | && (m_strProxyHost == other.m_strProxyHost)
|
---|
56 | ;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /** Returns whether the @a other passed data is equal to this one. */
|
---|
60 | bool operator==(const UIDataSettingsGlobalProxy &other) const { return equal(other); }
|
---|
61 | /** Returns whether the @a other passed data is different from this one. */
|
---|
62 | bool operator!=(const UIDataSettingsGlobalProxy &other) const { return !equal(other); }
|
---|
63 |
|
---|
64 | /** Holds the proxy mode. */
|
---|
65 | KProxyMode m_enmProxyMode;
|
---|
66 | /** Holds the proxy host. */
|
---|
67 | QString m_strProxyHost;
|
---|
68 | };
|
---|
69 |
|
---|
70 |
|
---|
71 | /*********************************************************************************************************************************
|
---|
72 | * Class UIGlobalSettingsProxy implementation. *
|
---|
73 | *********************************************************************************************************************************/
|
---|
74 |
|
---|
75 | UIGlobalSettingsProxy::UIGlobalSettingsProxy()
|
---|
76 | : m_pCache(0)
|
---|
77 | {
|
---|
78 | prepare();
|
---|
79 | }
|
---|
80 |
|
---|
81 | UIGlobalSettingsProxy::~UIGlobalSettingsProxy()
|
---|
82 | {
|
---|
83 | cleanup();
|
---|
84 | }
|
---|
85 |
|
---|
86 | bool UIGlobalSettingsProxy::changed() const
|
---|
87 | {
|
---|
88 | return m_pCache ? m_pCache->wasChanged() : false;
|
---|
89 | }
|
---|
90 |
|
---|
91 | void UIGlobalSettingsProxy::loadToCacheFrom(QVariant &data)
|
---|
92 | {
|
---|
93 | /* Sanity check: */
|
---|
94 | if (!m_pCache)
|
---|
95 | return;
|
---|
96 |
|
---|
97 | /* Fetch data to properties: */
|
---|
98 | UISettingsPageGlobal::fetchData(data);
|
---|
99 |
|
---|
100 | /* Clear cache initially: */
|
---|
101 | m_pCache->clear();
|
---|
102 |
|
---|
103 | /* Cache old data: */
|
---|
104 | UIDataSettingsGlobalProxy oldData;
|
---|
105 | oldData.m_enmProxyMode = m_properties.GetProxyMode();
|
---|
106 | oldData.m_strProxyHost = m_properties.GetProxyURL();
|
---|
107 | m_pCache->cacheInitialData(oldData);
|
---|
108 |
|
---|
109 | /* Upload properties to data: */
|
---|
110 | UISettingsPageGlobal::uploadData(data);
|
---|
111 | }
|
---|
112 |
|
---|
113 | void UIGlobalSettingsProxy::getFromCache()
|
---|
114 | {
|
---|
115 | /* Sanity check: */
|
---|
116 | if (!m_pCache)
|
---|
117 | return;
|
---|
118 |
|
---|
119 | /* Load old data from cache: */
|
---|
120 | const UIDataSettingsGlobalProxy &oldData = m_pCache->base();
|
---|
121 | if (m_pEditorProxyFeatures)
|
---|
122 | {
|
---|
123 | m_pEditorProxyFeatures->setProxyMode(oldData.m_enmProxyMode);
|
---|
124 | m_pEditorProxyFeatures->setProxyHost(oldData.m_strProxyHost);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /* Revalidate: */
|
---|
128 | revalidate();
|
---|
129 | }
|
---|
130 |
|
---|
131 | void UIGlobalSettingsProxy::putToCache()
|
---|
132 | {
|
---|
133 | /* Sanity check: */
|
---|
134 | if (!m_pCache)
|
---|
135 | return;
|
---|
136 |
|
---|
137 | /* Prepare new data: */
|
---|
138 | UIDataSettingsGlobalProxy newData = m_pCache->base();
|
---|
139 |
|
---|
140 | /* Cache new data: */
|
---|
141 | if (m_pEditorProxyFeatures)
|
---|
142 | {
|
---|
143 | newData.m_enmProxyMode = m_pEditorProxyFeatures->proxyMode();
|
---|
144 | newData.m_strProxyHost = m_pEditorProxyFeatures->proxyHost();
|
---|
145 | }
|
---|
146 | m_pCache->cacheCurrentData(newData);
|
---|
147 | }
|
---|
148 |
|
---|
149 | void UIGlobalSettingsProxy::saveFromCacheTo(QVariant &data)
|
---|
150 | {
|
---|
151 | /* Fetch data to properties: */
|
---|
152 | UISettingsPageGlobal::fetchData(data);
|
---|
153 |
|
---|
154 | /* Update data and failing state: */
|
---|
155 | setFailed(!saveData());
|
---|
156 |
|
---|
157 | /* Upload properties to data: */
|
---|
158 | UISettingsPageGlobal::uploadData(data);
|
---|
159 | }
|
---|
160 |
|
---|
161 | bool UIGlobalSettingsProxy::validate(QList<UIValidationMessage> &messages)
|
---|
162 | {
|
---|
163 | /* Pass if proxy is disabled: */
|
---|
164 | if (m_pEditorProxyFeatures->proxyMode() != KProxyMode_Manual)
|
---|
165 | return true;
|
---|
166 |
|
---|
167 | /* Pass by default: */
|
---|
168 | bool fPass = true;
|
---|
169 |
|
---|
170 | /* Prepare message: */
|
---|
171 | UIValidationMessage message;
|
---|
172 |
|
---|
173 | /* Check for URL presence: */
|
---|
174 | if (m_pEditorProxyFeatures->proxyHost().trimmed().isEmpty())
|
---|
175 | {
|
---|
176 | message.second << tr("No proxy URL is currently specified.");
|
---|
177 | fPass = false;
|
---|
178 | }
|
---|
179 |
|
---|
180 | else
|
---|
181 |
|
---|
182 | /* Check for URL validness: */
|
---|
183 | if (!QUrl(m_pEditorProxyFeatures->proxyHost().trimmed()).isValid())
|
---|
184 | {
|
---|
185 | message.second << tr("Invalid proxy URL is currently specified.");
|
---|
186 | fPass = true;
|
---|
187 | }
|
---|
188 |
|
---|
189 | else
|
---|
190 |
|
---|
191 | /* Check for password presence: */
|
---|
192 | if (!QUrl(m_pEditorProxyFeatures->proxyHost().trimmed()).password().isEmpty())
|
---|
193 | {
|
---|
194 | message.second << tr("You have provided a proxy password. "
|
---|
195 | "Please be aware that the password will be saved in plain text. "
|
---|
196 | "You may wish to configure a system-wide proxy instead and not "
|
---|
197 | "store application-specific settings.");
|
---|
198 | fPass = true;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /* Serialize message: */
|
---|
202 | if (!message.second.isEmpty())
|
---|
203 | messages << message;
|
---|
204 |
|
---|
205 | /* Return result: */
|
---|
206 | return fPass;
|
---|
207 | }
|
---|
208 |
|
---|
209 | void UIGlobalSettingsProxy::retranslateUi()
|
---|
210 | {
|
---|
211 | }
|
---|
212 |
|
---|
213 | void UIGlobalSettingsProxy::prepare()
|
---|
214 | {
|
---|
215 | /* Prepare cache: */
|
---|
216 | m_pCache = new UISettingsCacheGlobalProxy;
|
---|
217 | AssertPtrReturnVoid(m_pCache);
|
---|
218 |
|
---|
219 | /* Prepare everything: */
|
---|
220 | prepareWidgets();
|
---|
221 |
|
---|
222 | /* Apply language settings: */
|
---|
223 | retranslateUi();
|
---|
224 | }
|
---|
225 |
|
---|
226 | void UIGlobalSettingsProxy::prepareWidgets()
|
---|
227 | {
|
---|
228 | /* Prepare main layout: */
|
---|
229 | QVBoxLayout *pLayout = new QVBoxLayout(this);
|
---|
230 | if (pLayout)
|
---|
231 | {
|
---|
232 | /* Prepare 'proxy features' editor: */
|
---|
233 | m_pEditorProxyFeatures = new UIProxyFeaturesEditor(this);
|
---|
234 | if (m_pEditorProxyFeatures)
|
---|
235 | {
|
---|
236 | connect(m_pEditorProxyFeatures, &UIProxyFeaturesEditor::sigProxyModeChanged,
|
---|
237 | this, &UIGlobalSettingsProxy::revalidate);
|
---|
238 | connect(m_pEditorProxyFeatures, &UIProxyFeaturesEditor::sigProxyHostChanged,
|
---|
239 | this, &UIGlobalSettingsProxy::revalidate);
|
---|
240 | addEditor(m_pEditorProxyFeatures);
|
---|
241 | pLayout->addWidget(m_pEditorProxyFeatures);
|
---|
242 | }
|
---|
243 |
|
---|
244 | /* Add stretch to the end: */
|
---|
245 | pLayout->addStretch();
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | void UIGlobalSettingsProxy::cleanup()
|
---|
250 | {
|
---|
251 | /* Cleanup cache: */
|
---|
252 | delete m_pCache;
|
---|
253 | m_pCache = 0;
|
---|
254 | }
|
---|
255 |
|
---|
256 | bool UIGlobalSettingsProxy::saveData()
|
---|
257 | {
|
---|
258 | /* Sanity check: */
|
---|
259 | if (!m_pCache)
|
---|
260 | return false;
|
---|
261 |
|
---|
262 | /* Prepare result: */
|
---|
263 | bool fSuccess = true;
|
---|
264 | /* Save settings from cache: */
|
---|
265 | if ( fSuccess
|
---|
266 | && m_pCache->wasChanged())
|
---|
267 | {
|
---|
268 | /* Get old data from cache: */
|
---|
269 | const UIDataSettingsGlobalProxy &oldData = m_pCache->base();
|
---|
270 | /* Get new data from cache: */
|
---|
271 | const UIDataSettingsGlobalProxy &newData = m_pCache->data();
|
---|
272 |
|
---|
273 | /* Save new data from cache: */
|
---|
274 | if ( fSuccess
|
---|
275 | && newData.m_enmProxyMode != oldData.m_enmProxyMode)
|
---|
276 | {
|
---|
277 | m_properties.SetProxyMode(newData.m_enmProxyMode);
|
---|
278 | fSuccess &= m_properties.isOk();
|
---|
279 | }
|
---|
280 | if ( fSuccess
|
---|
281 | && newData.m_strProxyHost != oldData.m_strProxyHost)
|
---|
282 | {
|
---|
283 | m_properties.SetProxyURL(newData.m_strProxyHost);
|
---|
284 | fSuccess &= m_properties.isOk();
|
---|
285 | }
|
---|
286 |
|
---|
287 | /* Drop the old extra data setting if still around: */
|
---|
288 | if ( fSuccess
|
---|
289 | && !gEDataManager->proxySettings().isEmpty())
|
---|
290 | /* fSuccess = */ gEDataManager->setProxySettings(QString());
|
---|
291 |
|
---|
292 | /* Show error message if necessary: */
|
---|
293 | if (!fSuccess)
|
---|
294 | notifyOperationProgressError(UIErrorString::formatErrorInfo(m_properties));
|
---|
295 | }
|
---|
296 | /* Return result: */
|
---|
297 | return fSuccess;
|
---|
298 | }
|
---|