1 | /* $Id: UIGlobalSettingsLanguage.cpp 101230 2023-09-21 20:17:24Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - UIGlobalSettingsLanguage 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 <QVBoxLayout>
|
---|
30 |
|
---|
31 | /* GUI includes: */
|
---|
32 | #include "UIExtraDataManager.h"
|
---|
33 | #include "UIGlobalSettingsLanguage.h"
|
---|
34 | #include "UILanguageSettingsEditor.h"
|
---|
35 |
|
---|
36 |
|
---|
37 | /** Global settings: Language page data structure. */
|
---|
38 | struct UIDataSettingsGlobalLanguage
|
---|
39 | {
|
---|
40 | /** Constructs data. */
|
---|
41 | UIDataSettingsGlobalLanguage()
|
---|
42 | : m_strLanguageId(QString())
|
---|
43 | {}
|
---|
44 |
|
---|
45 | /** Returns whether the @a other passed data is equal to this one. */
|
---|
46 | bool equal(const UIDataSettingsGlobalLanguage &other) const
|
---|
47 | {
|
---|
48 | return true
|
---|
49 | && (m_strLanguageId == other.m_strLanguageId)
|
---|
50 | ;
|
---|
51 | }
|
---|
52 |
|
---|
53 | /** Returns whether the @a other passed data is equal to this one. */
|
---|
54 | bool operator==(const UIDataSettingsGlobalLanguage &other) const { return equal(other); }
|
---|
55 | /** Returns whether the @a other passed data is different from this one. */
|
---|
56 | bool operator!=(const UIDataSettingsGlobalLanguage &other) const { return !equal(other); }
|
---|
57 |
|
---|
58 | /** Holds the current language id. */
|
---|
59 | QString m_strLanguageId;
|
---|
60 | };
|
---|
61 |
|
---|
62 |
|
---|
63 | /*********************************************************************************************************************************
|
---|
64 | * Class UIGlobalSettingsLanguage implementation. *
|
---|
65 | *********************************************************************************************************************************/
|
---|
66 |
|
---|
67 | UIGlobalSettingsLanguage::UIGlobalSettingsLanguage()
|
---|
68 | : m_pCache(0)
|
---|
69 | , m_pEditorLanguageSettings(0)
|
---|
70 | {
|
---|
71 | prepare();
|
---|
72 | }
|
---|
73 |
|
---|
74 | UIGlobalSettingsLanguage::~UIGlobalSettingsLanguage()
|
---|
75 | {
|
---|
76 | cleanup();
|
---|
77 | }
|
---|
78 |
|
---|
79 | bool UIGlobalSettingsLanguage::changed() const
|
---|
80 | {
|
---|
81 | return m_pCache ? m_pCache->wasChanged() : false;
|
---|
82 | }
|
---|
83 |
|
---|
84 | void UIGlobalSettingsLanguage::loadToCacheFrom(QVariant &data)
|
---|
85 | {
|
---|
86 | /* Sanity check: */
|
---|
87 | if (!m_pCache)
|
---|
88 | return;
|
---|
89 |
|
---|
90 | /* Fetch data to properties: */
|
---|
91 | UISettingsPageGlobal::fetchData(data);
|
---|
92 |
|
---|
93 | /* Clear cache initially: */
|
---|
94 | m_pCache->clear();
|
---|
95 |
|
---|
96 | /* Cache old data: */
|
---|
97 | UIDataSettingsGlobalLanguage oldData;
|
---|
98 | oldData.m_strLanguageId = gEDataManager->languageId();
|
---|
99 | m_pCache->cacheInitialData(oldData);
|
---|
100 |
|
---|
101 | /* Upload properties to data: */
|
---|
102 | UISettingsPageGlobal::uploadData(data);
|
---|
103 | }
|
---|
104 |
|
---|
105 | void UIGlobalSettingsLanguage::getFromCache()
|
---|
106 | {
|
---|
107 | /* Sanity check: */
|
---|
108 | if (!m_pCache)
|
---|
109 | return;
|
---|
110 |
|
---|
111 | /* Load old data from cache: */
|
---|
112 | const UIDataSettingsGlobalLanguage &oldData = m_pCache->base();
|
---|
113 | if (m_pEditorLanguageSettings)
|
---|
114 | m_pEditorLanguageSettings->setValue(oldData.m_strLanguageId);
|
---|
115 | }
|
---|
116 |
|
---|
117 | void UIGlobalSettingsLanguage::putToCache()
|
---|
118 | {
|
---|
119 | /* Sanity check: */
|
---|
120 | if (!m_pCache)
|
---|
121 | return;
|
---|
122 |
|
---|
123 | /* Prepare new data: */
|
---|
124 | UIDataSettingsGlobalLanguage newData = m_pCache->base();
|
---|
125 |
|
---|
126 | /* Cache new data: */
|
---|
127 | if (m_pEditorLanguageSettings)
|
---|
128 | newData.m_strLanguageId = m_pEditorLanguageSettings->value();
|
---|
129 | m_pCache->cacheCurrentData(newData);
|
---|
130 | }
|
---|
131 |
|
---|
132 | void UIGlobalSettingsLanguage::saveFromCacheTo(QVariant &data)
|
---|
133 | {
|
---|
134 | /* Fetch data to properties: */
|
---|
135 | UISettingsPageGlobal::fetchData(data);
|
---|
136 |
|
---|
137 | /* Update data and failing state: */
|
---|
138 | setFailed(!saveData());
|
---|
139 |
|
---|
140 | /* Upload properties to data: */
|
---|
141 | UISettingsPageGlobal::uploadData(data);
|
---|
142 | }
|
---|
143 |
|
---|
144 | void UIGlobalSettingsLanguage::retranslateUi()
|
---|
145 | {
|
---|
146 | }
|
---|
147 |
|
---|
148 | void UIGlobalSettingsLanguage::prepare()
|
---|
149 | {
|
---|
150 | /* Prepare cache: */
|
---|
151 | m_pCache = new UISettingsCacheGlobalLanguage;
|
---|
152 | AssertPtrReturnVoid(m_pCache);
|
---|
153 |
|
---|
154 | /* Prepare everything: */
|
---|
155 | prepareWidgets();
|
---|
156 |
|
---|
157 | /* Apply language settings: */
|
---|
158 | retranslateUi();
|
---|
159 | }
|
---|
160 |
|
---|
161 | void UIGlobalSettingsLanguage::prepareWidgets()
|
---|
162 | {
|
---|
163 | /* Prepare main layout: */
|
---|
164 | QVBoxLayout *pLayout = new QVBoxLayout(this);
|
---|
165 | if (pLayout)
|
---|
166 | {
|
---|
167 | /* Prepare 'language settings' editor: */
|
---|
168 | m_pEditorLanguageSettings = new UILanguageSettingsEditor(this);
|
---|
169 | if (m_pEditorLanguageSettings)
|
---|
170 | {
|
---|
171 | addEditor(m_pEditorLanguageSettings);
|
---|
172 | pLayout->addWidget(m_pEditorLanguageSettings);
|
---|
173 | }
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | void UIGlobalSettingsLanguage::cleanup()
|
---|
178 | {
|
---|
179 | /* Cleanup cache: */
|
---|
180 | delete m_pCache;
|
---|
181 | m_pCache = 0;
|
---|
182 | }
|
---|
183 |
|
---|
184 | bool UIGlobalSettingsLanguage::saveData()
|
---|
185 | {
|
---|
186 | /* Sanity check: */
|
---|
187 | if (!m_pCache)
|
---|
188 | return false;
|
---|
189 |
|
---|
190 | /* Prepare result: */
|
---|
191 | bool fSuccess = true;
|
---|
192 | /* Save settings from cache: */
|
---|
193 | if ( fSuccess
|
---|
194 | && m_pCache->wasChanged())
|
---|
195 | {
|
---|
196 | /* Get old data from cache: */
|
---|
197 | const UIDataSettingsGlobalLanguage &oldData = m_pCache->base();
|
---|
198 | /* Get new data from cache: */
|
---|
199 | const UIDataSettingsGlobalLanguage &newData = m_pCache->data();
|
---|
200 |
|
---|
201 | /* Save new data from cache: */
|
---|
202 | if ( fSuccess
|
---|
203 | && newData.m_strLanguageId != oldData.m_strLanguageId)
|
---|
204 | /* fSuccess = */ gEDataManager->setLanguageId(newData.m_strLanguageId);
|
---|
205 | }
|
---|
206 | /* Return result: */
|
---|
207 | return fSuccess;
|
---|
208 | }
|
---|