VirtualBox

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

Last change on this file since 33540 was 33540, checked in by vboxsync, 14 years ago

*: spelling fixes, thanks Timeless!

  • 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 33540 2010-10-28 09:27:05Z vboxsync $ */
2/** @file
3 *
4 * VBox frontends: Qt GUI ("VirtualBox"):
5 * VBoxGlobalSettingsData, VBoxGlobalSettings class implementation
6 */
7
8/*
9 * Copyright (C) 2006-2007 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 "QIHotKeyEdit.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_WIN32)
43 hostkey = 0xA3; // VK_RCONTROL
44// hostkey = 165; // VK_RMENU
45#elif defined (Q_WS_PM)
46 hostkey = VK_CTRL;
47#elif defined (Q_WS_X11)
48 hostkey = 0xffe4; // XK_Control_R
49// hostkey = 65514; // XK_Alt_R
50#elif defined (Q_WS_MAC)
51// hostkey = 0x36; // QZ_RMETA
52 hostkey = 0x37; // QZ_LMETA
53// hostkey = 0x3e; // QZ_RCTRL
54// hostkey = 0x3d; // QZ_RALT
55#else
56# warning "port me!"
57 hostkey = 0;
58#endif
59 autoCapture = true;
60 guiFeatures = QString::null;
61 languageId = QString::null;
62 maxGuestRes = "auto";
63 remapScancodes = QString::null;
64 trayIconEnabled = false;
65 presentationModeEnabled = false;
66 hostScreenSaverDisabled = false;
67}
68
69VBoxGlobalSettingsData::VBoxGlobalSettingsData (const VBoxGlobalSettingsData &that)
70{
71 hostkey = that.hostkey;
72 autoCapture = that.autoCapture;
73 guiFeatures = that.guiFeatures;
74 languageId = that.languageId;
75 maxGuestRes = that.maxGuestRes;
76 remapScancodes = that.remapScancodes;
77 trayIconEnabled = that.trayIconEnabled;
78 presentationModeEnabled = that.presentationModeEnabled;
79 hostScreenSaverDisabled = that.hostScreenSaverDisabled;
80}
81
82VBoxGlobalSettingsData::~VBoxGlobalSettingsData()
83{
84}
85
86bool VBoxGlobalSettingsData::operator== (const VBoxGlobalSettingsData &that) const
87{
88 return this == &that ||
89 (hostkey == that.hostkey &&
90 autoCapture == that.autoCapture &&
91 guiFeatures == that.guiFeatures &&
92 languageId == that.languageId &&
93 maxGuestRes == that.maxGuestRes &&
94 remapScancodes == that.remapScancodes &&
95 trayIconEnabled == that.trayIconEnabled &&
96 presentationModeEnabled == that.presentationModeEnabled &&
97 hostScreenSaverDisabled == that.hostScreenSaverDisabled
98 );
99}
100
101/** @class VBoxGlobalSettings
102 *
103 * The VBoxGlobalSettings class is a wrapper class for VBoxGlobalSettingsData
104 * to implement implicit sharing of VirtualBox global data.
105 */
106
107/* Defined in VBoxGlobal.cpp */
108extern const char *gVBoxLangIDRegExp;
109
110static struct
111{
112 const char *publicName;
113 const char *name;
114 const char *rx;
115 bool canDelete;
116}
117gPropertyMap[] =
118{
119 { "GUI/Input/HostKey", "hostKey", "0|\\d*[1-9]\\d*", true },
120 { "GUI/Input/AutoCapture", "autoCapture", "true|false", true },
121 { "GUI/Customizations", "guiFeatures", "\\S+", true },
122 { "GUI/LanguageID", "languageId", gVBoxLangIDRegExp, true },
123 { "GUI/MaxGuestResolution", "maxGuestRes", "\\d*[1-9]\\d*,\\d*[1-9]\\d*|any|auto", true },
124 { "GUI/RemapScancodes", "remapScancodes", "(\\d+=\\d+,)*\\d+=\\d+", true },
125 { "GUI/TrayIcon/Enabled", "trayIconEnabled", "true|false", true },
126#ifdef Q_WS_MAC
127 { VBoxDefs::GUI_PresentationModeEnabled, "presentationModeEnabled", "true|false", true },
128#endif /* Q_WS_MAC */
129 { "GUI/HostScreenSaverDisabled", "hostScreenSaverDisabled", "true|false", true }
130};
131
132void VBoxGlobalSettings::setHostKey (int key)
133{
134 if (!QIHotKeyEdit::isValidKey (key))
135 {
136 last_err = tr ("'%1 (0x%2)' is an invalid host key code.")
137 .arg (key).arg (key, 0, 16);
138 return;
139 }
140
141 mData()->hostkey = key;
142 resetError();
143}
144
145bool VBoxGlobalSettings::isFeatureActive (const char *aFeature) const
146{
147 QStringList featureList = data()->guiFeatures.split (',');
148 return featureList.contains (aFeature);
149}
150
151/**
152 * Loads the settings from the (global) extra data area of VirtualBox.
153 *
154 * If an error occurs while accessing extra data area, the method immediately
155 * returns and the vbox argument will hold all error info (and therefore
156 * vbox.isOk() will be false to indicate this).
157 *
158 * If an error occurs while setting the value of some property, the method
159 * also returns immediately. #operator !() will return false in this case
160 * and #lastError() will contain the error message.
161 *
162 * @note This method emits the #propertyChanged() signal.
163 */
164void VBoxGlobalSettings::load (CVirtualBox &vbox)
165{
166 for (size_t i = 0; i < SIZEOF_ARRAY (gPropertyMap); i++)
167 {
168 QString value = vbox.GetExtraData (gPropertyMap [i].publicName);
169 if (!vbox.isOk())
170 return;
171 // empty value means the key is absent. it is ok, the default will apply
172 if (value.isEmpty())
173 continue;
174 // try to set the property validating it against rx
175 setPropertyPrivate (i, value);
176 if (!(*this))
177 break;
178 }
179}
180
181/**
182 * Saves the settings to the (global) extra data area of VirtualBox.
183 *
184 * If an error occurs while accessing extra data area, the method immediately
185 * returns and the vbox argument will hold all error info (and therefore
186 * vbox.isOk() will be false to indicate this).
187 */
188void VBoxGlobalSettings::save (CVirtualBox &vbox) const
189{
190 for (size_t i = 0; i < SIZEOF_ARRAY (gPropertyMap); i++)
191 {
192 QVariant value = property (gPropertyMap [i].name);
193 Assert (value.isValid() && value.canConvert (QVariant::String));
194
195 vbox.SetExtraData (gPropertyMap [i].publicName, value.toString());
196 if (!vbox.isOk())
197 return;
198 }
199}
200
201/**
202 * Returns a value of the property with the given public name
203 * or QString::null if there is no such public property.
204 */
205QString VBoxGlobalSettings::publicProperty (const QString &publicName) const
206{
207 for (size_t i = 0; i < SIZEOF_ARRAY (gPropertyMap); i++)
208 {
209 if (gPropertyMap [i].publicName == publicName)
210 {
211 QVariant value = property (gPropertyMap [i].name);
212 Assert (value.isValid() && value.canConvert (QVariant::String));
213
214 if (value.isValid() && value.canConvert (QVariant::String))
215 return value.toString();
216 else
217 break;
218 }
219 }
220
221 return QString::null;
222}
223
224/**
225 * Sets a value of a property with the given public name.
226 * Returns false if the specified property is not found, and true otherwise.
227 *
228 * This method (as opposed to #setProperty (const char *name, const QVariant& value))
229 * validates the value against the property's regexp.
230 *
231 * If an error occurs while setting the value of the property, #operator !()
232 * will return false after this method returns true, and #lastError() will contain
233 * the error message.
234 *
235 * @note This method emits the #propertyChanged() signal.
236 */
237bool VBoxGlobalSettings::setPublicProperty (const QString &publicName, const QString &value)
238{
239 for (size_t i = 0; i < SIZEOF_ARRAY (gPropertyMap); i++)
240 {
241 if (gPropertyMap [i].publicName == publicName)
242 {
243 setPropertyPrivate (i, value);
244 return true;
245 }
246 }
247
248 return false;
249}
250
251void VBoxGlobalSettings::setPropertyPrivate (size_t index, const QString &value)
252{
253 if (value.isEmpty())
254 {
255 if (!gPropertyMap [index].canDelete)
256 {
257 last_err = tr ("Cannot delete the key '%1'.")
258 .arg (gPropertyMap [index].publicName);
259 return;
260 }
261 }
262 else
263 {
264 if (!QRegExp (gPropertyMap [index].rx).exactMatch (value))
265 {
266 last_err = tr ("The value '%1' of the key '%2' doesn't match the "
267 "regexp constraint '%3'.")
268 .arg (value, gPropertyMap [index].publicName,
269 gPropertyMap [index].rx);
270 return;
271 }
272 }
273
274 QVariant oldVal = property (gPropertyMap [index].name);
275 Assert (oldVal.isValid() && oldVal.canConvert (QVariant::String));
276
277 if (oldVal.isValid() && oldVal.canConvert (QVariant::String) &&
278 oldVal.toString() != value)
279 {
280 bool ok = setProperty (gPropertyMap [index].name, value);
281 Assert (ok);
282 if (ok)
283 {
284 /* The individual setter may have set a specific error */
285 if (!last_err.isNull())
286 return;
287
288 last_err = QString::null;
289 emit propertyChanged (gPropertyMap [index].publicName,
290 gPropertyMap [index].name);
291 }
292 }
293}
294
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use