1 | /* $Id: UIGlobalSettingsGeneral.cpp 104313 2024-04-12 13:10:30Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIGlobalSettingsGeneral class implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-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 <QDir>
|
---|
30 | #include <QVBoxLayout>
|
---|
31 |
|
---|
32 | /* GUI includes: */
|
---|
33 | #include "UIDefaultMachineFolderEditor.h"
|
---|
34 | #include "UIErrorString.h"
|
---|
35 | #include "UIGlobalSettingsGeneral.h"
|
---|
36 | #include "UIVRDEAuthLibraryEditor.h"
|
---|
37 |
|
---|
38 |
|
---|
39 | /** Global settings: General page data structure. */
|
---|
40 | struct UIDataSettingsGlobalGeneral
|
---|
41 | {
|
---|
42 | /** Constructs data. */
|
---|
43 | UIDataSettingsGlobalGeneral()
|
---|
44 | : m_strDefaultMachineFolder(QString())
|
---|
45 | , m_strVRDEAuthLibrary(QString())
|
---|
46 | {}
|
---|
47 |
|
---|
48 | /** Returns whether the @a other passed data is equal to this one. */
|
---|
49 | bool equal(const UIDataSettingsGlobalGeneral &other) const
|
---|
50 | {
|
---|
51 | return true
|
---|
52 | && (m_strDefaultMachineFolder == other.m_strDefaultMachineFolder)
|
---|
53 | && (m_strVRDEAuthLibrary == other.m_strVRDEAuthLibrary)
|
---|
54 | ;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /** Returns whether the @a other passed data is equal to this one. */
|
---|
58 | bool operator==(const UIDataSettingsGlobalGeneral &other) const { return equal(other); }
|
---|
59 | /** Returns whether the @a other passed data is different from this one. */
|
---|
60 | bool operator!=(const UIDataSettingsGlobalGeneral &other) const { return !equal(other); }
|
---|
61 |
|
---|
62 | /** Holds the 'default machine folder' path. */
|
---|
63 | QString m_strDefaultMachineFolder;
|
---|
64 | /** Holds the 'VRDE auth library' name. */
|
---|
65 | QString m_strVRDEAuthLibrary;
|
---|
66 | };
|
---|
67 |
|
---|
68 |
|
---|
69 | /*********************************************************************************************************************************
|
---|
70 | * Class UIGlobalSettingsGeneral implementation. *
|
---|
71 | *********************************************************************************************************************************/
|
---|
72 |
|
---|
73 | UIGlobalSettingsGeneral::UIGlobalSettingsGeneral()
|
---|
74 | : m_pCache(0)
|
---|
75 | , m_pEditorDefaultMachineFolder(0)
|
---|
76 | , m_pEditorVRDEAuthLibrary(0)
|
---|
77 | {
|
---|
78 | prepare();
|
---|
79 | }
|
---|
80 |
|
---|
81 | UIGlobalSettingsGeneral::~UIGlobalSettingsGeneral()
|
---|
82 | {
|
---|
83 | cleanup();
|
---|
84 | }
|
---|
85 |
|
---|
86 | bool UIGlobalSettingsGeneral::changed() const
|
---|
87 | {
|
---|
88 | return m_pCache ? m_pCache->wasChanged() : false;
|
---|
89 | }
|
---|
90 |
|
---|
91 | void UIGlobalSettingsGeneral::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 | UIDataSettingsGlobalGeneral oldData;
|
---|
105 | oldData.m_strDefaultMachineFolder = m_properties.GetDefaultMachineFolder();
|
---|
106 | oldData.m_strVRDEAuthLibrary = m_properties.GetVRDEAuthLibrary();
|
---|
107 | m_pCache->cacheInitialData(oldData);
|
---|
108 |
|
---|
109 | /* Upload properties to data: */
|
---|
110 | UISettingsPageGlobal::uploadData(data);
|
---|
111 | }
|
---|
112 |
|
---|
113 | void UIGlobalSettingsGeneral::getFromCache()
|
---|
114 | {
|
---|
115 | /* Sanity check: */
|
---|
116 | if (!m_pCache)
|
---|
117 | return;
|
---|
118 |
|
---|
119 | /* Load old data from cache: */
|
---|
120 | const UIDataSettingsGlobalGeneral &oldData = m_pCache->base();
|
---|
121 | if (m_pEditorDefaultMachineFolder)
|
---|
122 | m_pEditorDefaultMachineFolder->setValue(oldData.m_strDefaultMachineFolder);
|
---|
123 | if (m_pEditorVRDEAuthLibrary)
|
---|
124 | m_pEditorVRDEAuthLibrary->setValue(oldData.m_strVRDEAuthLibrary);
|
---|
125 |
|
---|
126 | /* Revalidate: */
|
---|
127 | revalidate();
|
---|
128 | }
|
---|
129 |
|
---|
130 | void UIGlobalSettingsGeneral::putToCache()
|
---|
131 | {
|
---|
132 | /* Sanity check: */
|
---|
133 | if (!m_pCache)
|
---|
134 | return;
|
---|
135 |
|
---|
136 | /* Prepare new data: */
|
---|
137 | UIDataSettingsGlobalGeneral newData = m_pCache->base();
|
---|
138 |
|
---|
139 | /* Cache new data: */
|
---|
140 | if (m_pEditorDefaultMachineFolder)
|
---|
141 | newData.m_strDefaultMachineFolder = m_pEditorDefaultMachineFolder->value();
|
---|
142 | if (m_pEditorVRDEAuthLibrary)
|
---|
143 | newData.m_strVRDEAuthLibrary = m_pEditorVRDEAuthLibrary->value();
|
---|
144 | m_pCache->cacheCurrentData(newData);
|
---|
145 | }
|
---|
146 |
|
---|
147 | void UIGlobalSettingsGeneral::saveFromCacheTo(QVariant &data)
|
---|
148 | {
|
---|
149 | /* Fetch data to properties: */
|
---|
150 | UISettingsPageGlobal::fetchData(data);
|
---|
151 |
|
---|
152 | /* Update data and failing state: */
|
---|
153 | setFailed(!saveData());
|
---|
154 |
|
---|
155 | /* Upload properties to data: */
|
---|
156 | UISettingsPageGlobal::uploadData(data);
|
---|
157 | }
|
---|
158 |
|
---|
159 | bool UIGlobalSettingsGeneral::validate(QList<UIValidationMessage> &messages)
|
---|
160 | {
|
---|
161 | /* Pass by default: */
|
---|
162 | bool fPass = true;
|
---|
163 |
|
---|
164 | /* Prepare message: */
|
---|
165 | UIValidationMessage message;
|
---|
166 |
|
---|
167 | /* Check for the folder presence: */
|
---|
168 | if ( m_pEditorDefaultMachineFolder
|
---|
169 | && !QDir(m_pEditorDefaultMachineFolder->value()).exists())
|
---|
170 | {
|
---|
171 | message.second << tr("Default machine folder is missing.");
|
---|
172 | fPass = false;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /* Serialize message: */
|
---|
176 | if (!message.second.isEmpty())
|
---|
177 | messages << message;
|
---|
178 |
|
---|
179 | /* Return result: */
|
---|
180 | return fPass;
|
---|
181 | }
|
---|
182 |
|
---|
183 | void UIGlobalSettingsGeneral::sltRetranslateUI()
|
---|
184 | {
|
---|
185 | updateMinimumLayoutHint();
|
---|
186 | }
|
---|
187 |
|
---|
188 | void UIGlobalSettingsGeneral::handleFilterChange()
|
---|
189 | {
|
---|
190 | updateMinimumLayoutHint();
|
---|
191 | }
|
---|
192 |
|
---|
193 | void UIGlobalSettingsGeneral::prepare()
|
---|
194 | {
|
---|
195 | /* Prepare cache: */
|
---|
196 | m_pCache = new UISettingsCacheGlobalGeneral;
|
---|
197 | AssertPtrReturnVoid(m_pCache);
|
---|
198 |
|
---|
199 | /* Prepare everything: */
|
---|
200 | prepareWidgets();
|
---|
201 |
|
---|
202 | /* Apply language settings: */
|
---|
203 | sltRetranslateUI();
|
---|
204 | }
|
---|
205 |
|
---|
206 | void UIGlobalSettingsGeneral::prepareWidgets()
|
---|
207 | {
|
---|
208 | /* Prepare main layout: */
|
---|
209 | QVBoxLayout *pLayout = new QVBoxLayout(this);
|
---|
210 | if (pLayout)
|
---|
211 | {
|
---|
212 | /* Prepare 'default machine folder' editor: */
|
---|
213 | m_pEditorDefaultMachineFolder = new UIDefaultMachineFolderEditor(this);
|
---|
214 | if (m_pEditorDefaultMachineFolder)
|
---|
215 | {
|
---|
216 | addEditor(m_pEditorDefaultMachineFolder);
|
---|
217 | pLayout->addWidget(m_pEditorDefaultMachineFolder);
|
---|
218 | connect(m_pEditorDefaultMachineFolder, &UIDefaultMachineFolderEditor::sigPathChanged,
|
---|
219 | this, &UIGlobalSettingsGeneral::revalidate);
|
---|
220 | }
|
---|
221 |
|
---|
222 | /* Prepare 'VRDE auth library' editor: */
|
---|
223 | m_pEditorVRDEAuthLibrary = new UIVRDEAuthLibraryEditor(this);
|
---|
224 | if (m_pEditorVRDEAuthLibrary)
|
---|
225 | {
|
---|
226 | addEditor(m_pEditorVRDEAuthLibrary);
|
---|
227 | pLayout->addWidget(m_pEditorVRDEAuthLibrary);
|
---|
228 | }
|
---|
229 |
|
---|
230 | /* Add stretch to the end: */
|
---|
231 | pLayout->addStretch();
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | void UIGlobalSettingsGeneral::cleanup()
|
---|
236 | {
|
---|
237 | /* Cleanup cache: */
|
---|
238 | delete m_pCache;
|
---|
239 | m_pCache = 0;
|
---|
240 | }
|
---|
241 |
|
---|
242 | bool UIGlobalSettingsGeneral::saveData()
|
---|
243 | {
|
---|
244 | /* Sanity check: */
|
---|
245 | if (!m_pCache)
|
---|
246 | return false;
|
---|
247 |
|
---|
248 | /* Prepare result: */
|
---|
249 | bool fSuccess = true;
|
---|
250 | /* Save settings from cache: */
|
---|
251 | if ( fSuccess
|
---|
252 | && m_pCache->wasChanged())
|
---|
253 | {
|
---|
254 | /* Get old data from cache: */
|
---|
255 | const UIDataSettingsGlobalGeneral &oldData = m_pCache->base();
|
---|
256 | /* Get new data from cache: */
|
---|
257 | const UIDataSettingsGlobalGeneral &newData = m_pCache->data();
|
---|
258 |
|
---|
259 | /* Save 'default machine folder': */
|
---|
260 | if ( fSuccess
|
---|
261 | && newData.m_strDefaultMachineFolder != oldData.m_strDefaultMachineFolder)
|
---|
262 | {
|
---|
263 | m_properties.SetDefaultMachineFolder(newData.m_strDefaultMachineFolder);
|
---|
264 | fSuccess = m_properties.isOk();
|
---|
265 | }
|
---|
266 | /* Save 'VRDE auth library': */
|
---|
267 | if ( fSuccess
|
---|
268 | && newData.m_strVRDEAuthLibrary != oldData.m_strVRDEAuthLibrary)
|
---|
269 | {
|
---|
270 | m_properties.SetVRDEAuthLibrary(newData.m_strVRDEAuthLibrary);
|
---|
271 | fSuccess = m_properties.isOk();
|
---|
272 | }
|
---|
273 |
|
---|
274 | /* Show error message if necessary: */
|
---|
275 | if (!fSuccess)
|
---|
276 | notifyOperationProgressError(UIErrorString::formatErrorInfo(m_properties));
|
---|
277 | }
|
---|
278 | /* Return result: */
|
---|
279 | return fSuccess;
|
---|
280 | }
|
---|
281 |
|
---|
282 | void UIGlobalSettingsGeneral::updateMinimumLayoutHint()
|
---|
283 | {
|
---|
284 | /* These editors have own labels, but we want them to be properly layouted according to each other: */
|
---|
285 | int iMinimumLayoutHint = 0;
|
---|
286 | if (m_pEditorDefaultMachineFolder && !m_pEditorDefaultMachineFolder->isHidden())
|
---|
287 | iMinimumLayoutHint = qMax(iMinimumLayoutHint, m_pEditorDefaultMachineFolder->minimumLabelHorizontalHint());
|
---|
288 | if (m_pEditorVRDEAuthLibrary && !m_pEditorVRDEAuthLibrary->isHidden())
|
---|
289 | iMinimumLayoutHint = qMax(iMinimumLayoutHint, m_pEditorVRDEAuthLibrary->minimumLabelHorizontalHint());
|
---|
290 | if (m_pEditorDefaultMachineFolder)
|
---|
291 | m_pEditorDefaultMachineFolder->setMinimumLayoutIndent(iMinimumLayoutHint);
|
---|
292 | if (m_pEditorVRDEAuthLibrary)
|
---|
293 | m_pEditorVRDEAuthLibrary->setMinimumLayoutIndent(iMinimumLayoutHint);
|
---|
294 | }
|
---|