VirtualBox

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

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

FE/Qt: 5245: GUI support for complex host-key combinations. Initial build for Win/X11.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.1 KB
Line 
1/* $Id: VBoxGlobalSettings.cpp 35730 2011-01-27 10:49:44Z 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 // empty value means the key is absent. it is ok, the default will apply
162 if (value.isEmpty())
163 continue;
164 // try to set the property validating it against rx
165 setPropertyPrivate (i, value);
166 if (!(*this))
167 break;
168 }
169}
170
171/**
172 * Saves the settings to the (global) extra data area of VirtualBox.
173 *
174 * If an error occurs while accessing extra data area, the method immediately
175 * returns and the vbox argument will hold all error info (and therefore
176 * vbox.isOk() will be false to indicate this).
177 */
178void VBoxGlobalSettings::save (CVirtualBox &vbox) const
179{
180 for (size_t i = 0; i < SIZEOF_ARRAY (gPropertyMap); i++)
181 {
182 QVariant value = property (gPropertyMap [i].name);
183 Assert (value.isValid() && value.canConvert (QVariant::String));
184
185 vbox.SetExtraData (gPropertyMap [i].publicName, value.toString());
186 if (!vbox.isOk())
187 return;
188 }
189}
190
191/**
192 * Returns a value of the property with the given public name
193 * or QString::null if there is no such public property.
194 */
195QString VBoxGlobalSettings::publicProperty (const QString &publicName) const
196{
197 for (size_t i = 0; i < SIZEOF_ARRAY (gPropertyMap); i++)
198 {
199 if (gPropertyMap [i].publicName == publicName)
200 {
201 QVariant value = property (gPropertyMap [i].name);
202 Assert (value.isValid() && value.canConvert (QVariant::String));
203
204 if (value.isValid() && value.canConvert (QVariant::String))
205 return value.toString();
206 else
207 break;
208 }
209 }
210
211 return QString::null;
212}
213
214/**
215 * Sets a value of a property with the given public name.
216 * Returns false if the specified property is not found, and true otherwise.
217 *
218 * This method (as opposed to #setProperty (const char *name, const QVariant& value))
219 * validates the value against the property's regexp.
220 *
221 * If an error occurs while setting the value of the property, #operator !()
222 * will return false after this method returns true, and #lastError() will contain
223 * the error message.
224 *
225 * @note This method emits the #propertyChanged() signal.
226 */
227bool VBoxGlobalSettings::setPublicProperty (const QString &publicName, const QString &value)
228{
229 for (size_t i = 0; i < SIZEOF_ARRAY (gPropertyMap); i++)
230 {
231 if (gPropertyMap [i].publicName == publicName)
232 {
233 setPropertyPrivate (i, value);
234 return true;
235 }
236 }
237
238 return false;
239}
240
241void VBoxGlobalSettings::setPropertyPrivate (size_t index, const QString &value)
242{
243 if (value.isEmpty())
244 {
245 if (!gPropertyMap [index].canDelete)
246 {
247 last_err = tr ("Cannot delete the key '%1'.")
248 .arg (gPropertyMap [index].publicName);
249 return;
250 }
251 }
252 else
253 {
254 if (!QRegExp (gPropertyMap [index].rx).exactMatch (value))
255 {
256 last_err = tr ("The value '%1' of the key '%2' doesn't match the "
257 "regexp constraint '%3'.")
258 .arg (value, gPropertyMap [index].publicName,
259 gPropertyMap [index].rx);
260 return;
261 }
262 }
263
264 QVariant oldVal = property (gPropertyMap [index].name);
265 Assert (oldVal.isValid() && oldVal.canConvert (QVariant::String));
266
267 if (oldVal.isValid() && oldVal.canConvert (QVariant::String) &&
268 oldVal.toString() != value)
269 {
270 bool ok = setProperty (gPropertyMap [index].name, value);
271 Assert (ok);
272 if (ok)
273 {
274 /* The individual setter may have set a specific error */
275 if (!last_err.isNull())
276 return;
277
278 last_err = QString::null;
279 emit propertyChanged (gPropertyMap [index].publicName,
280 gPropertyMap [index].name);
281 }
282 }
283}
284
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use