VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/VBoxGlobalSettings.cpp@ 35879

Last change on this file since 35879 was 35879, checked in by vboxsync, 13 years ago

FE/Qt4: upgrade path for the host key

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 KB
Line 
1/* $Id: VBoxGlobalSettings.cpp 35879 2011-02-07 16:43:02Z vboxsync $ */
2/** @file
3 *
4 * VBox frontends: Qt GUI ("VirtualBox"):
5 * VBoxGlobalSettingsData, VBoxGlobalSettings class implementation
6 */
7
8/*
9 * Copyright (C) 2006-2011 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20#ifdef VBOX_WITH_PRECOMPILED_HEADERS
21# include "precomp.h"
22#else /* !VBOX_WITH_PRECOMPILED_HEADERS */
23/* Qt includes */
24#include <QString>
25#include <QRegExp>
26#include <QVariant>
27
28#include "VBoxGlobalSettings.h"
29#include "UIHotKeyEditor.h"
30#include "COMDefs.h"
31#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
32
33/** @class VBoxGlobalSettingsData
34 *
35 * The VBoxGlobalSettingsData class encapsulates the global settings
36 * of the VirtualBox.
37 */
38
39VBoxGlobalSettingsData::VBoxGlobalSettingsData()
40{
41 /* default settings */
42#if defined (Q_WS_WIN)
43 hostCombo = "163"; // VK_RCONTROL
44#elif defined (Q_WS_X11)
45 hostCombo = "65508"; // XK_Control_R
46#elif defined (Q_WS_MAC)
47 hostCombo = "55"; // QZ_LMETA
48#else
49# warning "port me!"
50#endif
51 autoCapture = true;
52 guiFeatures = QString::null;
53 languageId = QString::null;
54 maxGuestRes = "auto";
55 remapScancodes = QString::null;
56 trayIconEnabled = false;
57 presentationModeEnabled = false;
58 hostScreenSaverDisabled = false;
59}
60
61VBoxGlobalSettingsData::VBoxGlobalSettingsData (const VBoxGlobalSettingsData &that)
62{
63 hostCombo = that.hostCombo;
64 autoCapture = that.autoCapture;
65 guiFeatures = that.guiFeatures;
66 languageId = that.languageId;
67 maxGuestRes = that.maxGuestRes;
68 remapScancodes = that.remapScancodes;
69 trayIconEnabled = that.trayIconEnabled;
70 presentationModeEnabled = that.presentationModeEnabled;
71 hostScreenSaverDisabled = that.hostScreenSaverDisabled;
72}
73
74VBoxGlobalSettingsData::~VBoxGlobalSettingsData()
75{
76}
77
78bool VBoxGlobalSettingsData::operator== (const VBoxGlobalSettingsData &that) const
79{
80 return this == &that ||
81 (hostCombo == that.hostCombo &&
82 autoCapture == that.autoCapture &&
83 guiFeatures == that.guiFeatures &&
84 languageId == that.languageId &&
85 maxGuestRes == that.maxGuestRes &&
86 remapScancodes == that.remapScancodes &&
87 trayIconEnabled == that.trayIconEnabled &&
88 presentationModeEnabled == that.presentationModeEnabled &&
89 hostScreenSaverDisabled == that.hostScreenSaverDisabled
90 );
91}
92
93/** @class VBoxGlobalSettings
94 *
95 * The VBoxGlobalSettings class is a wrapper class for VBoxGlobalSettingsData
96 * to implement implicit sharing of VirtualBox global data.
97 */
98
99/* Defined in VBoxGlobal.cpp */
100extern const char *gVBoxLangIDRegExp;
101
102static struct
103{
104 const char *publicName;
105 const char *name;
106 const char *rx;
107 bool canDelete;
108}
109gPropertyMap[] =
110{
111 { "GUI/Input/HostCombo", "hostCombo", "0|\\d*[1-9]\\d*(,\\d*[1-9]\\d*)?(,\\d*[1-9]\\d*)?", true },
112 { "GUI/Input/AutoCapture", "autoCapture", "true|false", true },
113 { "GUI/Customizations", "guiFeatures", "\\S+", true },
114 { "GUI/LanguageID", "languageId", gVBoxLangIDRegExp, true },
115 { "GUI/MaxGuestResolution", "maxGuestRes", "\\d*[1-9]\\d*,\\d*[1-9]\\d*|any|auto", true },
116 { "GUI/RemapScancodes", "remapScancodes", "(\\d+=\\d+,)*\\d+=\\d+", true },
117 { "GUI/TrayIcon/Enabled", "trayIconEnabled", "true|false", true },
118#ifdef Q_WS_MAC
119 { VBoxDefs::GUI_PresentationModeEnabled, "presentationModeEnabled", "true|false", true },
120#endif /* Q_WS_MAC */
121 { "GUI/HostScreenSaverDisabled", "hostScreenSaverDisabled", "true|false", true }
122};
123
124void VBoxGlobalSettings::setHostCombo (const QString &hostCombo)
125{
126 if (!UIHotKeyCombination::isValidKeyCombo (hostCombo))
127 {
128 last_err = tr ("'%1' is an invalid host-combination code-sequence.").arg (hostCombo);
129 return;
130 }
131 mData()->hostCombo = hostCombo;
132 resetError();
133}
134
135bool VBoxGlobalSettings::isFeatureActive (const char *aFeature) const
136{
137 QStringList featureList = data()->guiFeatures.split (',');
138 return featureList.contains (aFeature);
139}
140
141/**
142 * Loads the settings from the (global) extra data area of VirtualBox.
143 *
144 * If an error occurs while accessing extra data area, the method immediately
145 * returns and the vbox argument will hold all error info (and therefore
146 * vbox.isOk() will be false to indicate this).
147 *
148 * If an error occurs while setting the value of some property, the method
149 * also returns immediately. #operator !() will return false in this case
150 * and #lastError() will contain the error message.
151 *
152 * @note This method emits the #propertyChanged() signal.
153 */
154void VBoxGlobalSettings::load (CVirtualBox &vbox)
155{
156 for (size_t i = 0; i < SIZEOF_ARRAY(gPropertyMap); i++)
157 {
158 QString value = vbox.GetExtraData(gPropertyMap[i].publicName);
159 if (!vbox.isOk())
160 return;
161 /* Check for the host key upgrade path. */
162 if ( value.isEmpty()
163 && QString(gPropertyMap[i].publicName) == "GUI/Input/HostCombo")
164 value = vbox.GetExtraData("GUI/Input/HostKey");
165 /* Empty value means the key is absent. It is OK, the default will
166 * apply. */
167 if (value.isEmpty())
168 continue;
169 /* Try to set the property validating it against rx. */
170 setPropertyPrivate(i, value);
171 if (!(*this))
172 break;
173 }
174}
175
176/**
177 * Saves the settings to the (global) extra data area of VirtualBox.
178 *
179 * If an error occurs while accessing extra data area, the method immediately
180 * returns and the vbox argument will hold all error info (and therefore
181 * vbox.isOk() will be false to indicate this).
182 */
183void VBoxGlobalSettings::save (CVirtualBox &vbox) const
184{
185 for (size_t i = 0; i < SIZEOF_ARRAY (gPropertyMap); i++)
186 {
187 QVariant value = property (gPropertyMap [i].name);
188 Assert (value.isValid() && value.canConvert (QVariant::String));
189
190 vbox.SetExtraData (gPropertyMap [i].publicName, value.toString());
191 if (!vbox.isOk())
192 return;
193 }
194}
195
196/**
197 * Returns a value of the property with the given public name
198 * or QString::null if there is no such public property.
199 */
200QString VBoxGlobalSettings::publicProperty (const QString &publicName) const
201{
202 for (size_t i = 0; i < SIZEOF_ARRAY (gPropertyMap); i++)
203 {
204 if (gPropertyMap [i].publicName == publicName)
205 {
206 QVariant value = property (gPropertyMap [i].name);
207 Assert (value.isValid() && value.canConvert (QVariant::String));
208
209 if (value.isValid() && value.canConvert (QVariant::String))
210 return value.toString();
211 else
212 break;
213 }
214 }
215
216 return QString::null;
217}
218
219/**
220 * Sets a value of a property with the given public name.
221 * Returns false if the specified property is not found, and true otherwise.
222 *
223 * This method (as opposed to #setProperty (const char *name, const QVariant& value))
224 * validates the value against the property's regexp.
225 *
226 * If an error occurs while setting the value of the property, #operator !()
227 * will return false after this method returns true, and #lastError() will contain
228 * the error message.
229 *
230 * @note This method emits the #propertyChanged() signal.
231 */
232bool VBoxGlobalSettings::setPublicProperty (const QString &publicName, const QString &value)
233{
234 for (size_t i = 0; i < SIZEOF_ARRAY (gPropertyMap); i++)
235 {
236 if (gPropertyMap [i].publicName == publicName)
237 {
238 setPropertyPrivate (i, value);
239 return true;
240 }
241 }
242
243 return false;
244}
245
246void VBoxGlobalSettings::setPropertyPrivate (size_t index, const QString &value)
247{
248 if (value.isEmpty())
249 {
250 if (!gPropertyMap [index].canDelete)
251 {
252 last_err = tr ("Cannot delete the key '%1'.")
253 .arg (gPropertyMap [index].publicName);
254 return;
255 }
256 }
257 else
258 {
259 if (!QRegExp (gPropertyMap [index].rx).exactMatch (value))
260 {
261 last_err = tr ("The value '%1' of the key '%2' doesn't match the "
262 "regexp constraint '%3'.")
263 .arg (value, gPropertyMap [index].publicName,
264 gPropertyMap [index].rx);
265 return;
266 }
267 }
268
269 QVariant oldVal = property (gPropertyMap [index].name);
270 Assert (oldVal.isValid() && oldVal.canConvert (QVariant::String));
271
272 if (oldVal.isValid() && oldVal.canConvert (QVariant::String) &&
273 oldVal.toString() != value)
274 {
275 bool ok = setProperty (gPropertyMap [index].name, value);
276 Assert (ok);
277 if (ok)
278 {
279 /* The individual setter may have set a specific error */
280 if (!last_err.isNull())
281 return;
282
283 last_err = QString::null;
284 emit propertyChanged (gPropertyMap [index].publicName,
285 gPropertyMap [index].name);
286 }
287 }
288}
289
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use