1 | /* $Id: VBoxUtils.h 58312 2015-10-19 16:23:50Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Declarations of utility classes and functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2011 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef ___VBoxUtils_h___
|
---|
19 | #define ___VBoxUtils_h___
|
---|
20 |
|
---|
21 | #include <iprt/types.h>
|
---|
22 |
|
---|
23 | /* Qt includes */
|
---|
24 | #include <QMouseEvent>
|
---|
25 | #include <QWidget>
|
---|
26 | #include <QTextBrowser>
|
---|
27 |
|
---|
28 | /** QObject reimplementation,
|
---|
29 | * providing passed QObject with property assignation routine. */
|
---|
30 | class QObjectPropertySetter : public QObject
|
---|
31 | {
|
---|
32 | Q_OBJECT;
|
---|
33 |
|
---|
34 | public:
|
---|
35 |
|
---|
36 | /** Constructor. */
|
---|
37 | QObjectPropertySetter(QObject *pParent, const QString &strName)
|
---|
38 | : QObject(pParent), m_strName(strName) {}
|
---|
39 |
|
---|
40 | private slots:
|
---|
41 |
|
---|
42 | /** Assigns property value. */
|
---|
43 | void sltAssignProperty(const QString &strValue)
|
---|
44 | { parent()->setProperty(m_strName.toAscii().constData(), strValue); }
|
---|
45 |
|
---|
46 | private:
|
---|
47 |
|
---|
48 | /** Holds property name. */
|
---|
49 | const QString m_strName;
|
---|
50 | };
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Simple class that filters out all key presses and releases
|
---|
54 | * got while the Alt key is pressed. For some very strange reason,
|
---|
55 | * QLineEdit accepts those combinations that are not used as accelerators,
|
---|
56 | * and inserts the corresponding characters to the entry field.
|
---|
57 | */
|
---|
58 | class QIAltKeyFilter : protected QObject
|
---|
59 | {
|
---|
60 | Q_OBJECT;
|
---|
61 |
|
---|
62 | public:
|
---|
63 |
|
---|
64 | QIAltKeyFilter (QObject *aParent) :QObject (aParent) {}
|
---|
65 |
|
---|
66 | void watchOn (QObject *aObject) { aObject->installEventFilter (this); }
|
---|
67 |
|
---|
68 | protected:
|
---|
69 |
|
---|
70 | bool eventFilter (QObject * /* aObject */, QEvent *aEvent)
|
---|
71 | {
|
---|
72 | if (aEvent->type() == QEvent::KeyPress || aEvent->type() == QEvent::KeyRelease)
|
---|
73 | {
|
---|
74 | QKeyEvent *pEvent = static_cast<QKeyEvent *> (aEvent);
|
---|
75 | if (pEvent->modifiers() & Qt::AltModifier)
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 | return false;
|
---|
79 | }
|
---|
80 | };
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Simple class which simulates focus-proxy rule redirecting widget
|
---|
84 | * assigned shortcut to desired widget.
|
---|
85 | */
|
---|
86 | class QIFocusProxy : protected QObject
|
---|
87 | {
|
---|
88 | Q_OBJECT;
|
---|
89 |
|
---|
90 | public:
|
---|
91 |
|
---|
92 | QIFocusProxy (QWidget *aFrom, QWidget *aTo)
|
---|
93 | : QObject (aFrom), mFrom (aFrom), mTo (aTo)
|
---|
94 | {
|
---|
95 | mFrom->installEventFilter (this);
|
---|
96 | }
|
---|
97 |
|
---|
98 | protected:
|
---|
99 |
|
---|
100 | bool eventFilter (QObject *aObject, QEvent *aEvent)
|
---|
101 | {
|
---|
102 | if (aObject == mFrom && aEvent->type() == QEvent::Shortcut)
|
---|
103 | {
|
---|
104 | mTo->setFocus();
|
---|
105 | return true;
|
---|
106 | }
|
---|
107 | return QObject::eventFilter (aObject, aEvent);
|
---|
108 | }
|
---|
109 |
|
---|
110 | QWidget *mFrom;
|
---|
111 | QWidget *mTo;
|
---|
112 | };
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * QTextEdit reimplementation to feat some extended requirements.
|
---|
116 | */
|
---|
117 | class QRichTextEdit : public QTextEdit
|
---|
118 | {
|
---|
119 | Q_OBJECT;
|
---|
120 |
|
---|
121 | public:
|
---|
122 |
|
---|
123 | QRichTextEdit (QWidget *aParent = 0) : QTextEdit (aParent) {}
|
---|
124 |
|
---|
125 | void setViewportMargins (int aLeft, int aTop, int aRight, int aBottom)
|
---|
126 | {
|
---|
127 | QTextEdit::setViewportMargins (aLeft, aTop, aRight, aBottom);
|
---|
128 | }
|
---|
129 | };
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * QTextBrowser reimplementation to feat some extended requirements.
|
---|
133 | */
|
---|
134 | class QRichTextBrowser : public QTextBrowser
|
---|
135 | {
|
---|
136 | Q_OBJECT;
|
---|
137 |
|
---|
138 | public:
|
---|
139 |
|
---|
140 | QRichTextBrowser (QWidget *aParent) : QTextBrowser (aParent) {}
|
---|
141 |
|
---|
142 | void setViewportMargins (int aLeft, int aTop, int aRight, int aBottom)
|
---|
143 | {
|
---|
144 | QTextBrowser::setViewportMargins (aLeft, aTop, aRight, aBottom);
|
---|
145 | }
|
---|
146 | };
|
---|
147 |
|
---|
148 | /** Object containing functionality
|
---|
149 | * to (de)serialize proxy settings. */
|
---|
150 | class UIProxyManager
|
---|
151 | {
|
---|
152 | public:
|
---|
153 |
|
---|
154 | /** Proxy states. */
|
---|
155 | enum ProxyState
|
---|
156 | {
|
---|
157 | ProxyState_Disabled,
|
---|
158 | ProxyState_Enabled,
|
---|
159 | ProxyState_Auto
|
---|
160 | };
|
---|
161 |
|
---|
162 | /** Constructs object which parses passed @a strProxySettings. */
|
---|
163 | UIProxyManager(const QString &strProxySettings = QString())
|
---|
164 | : m_enmProxyState(ProxyState_Auto)
|
---|
165 | , m_fAuthEnabled(false)
|
---|
166 | {
|
---|
167 | /* Parse proxy settings: */
|
---|
168 | if (strProxySettings.isEmpty())
|
---|
169 | return;
|
---|
170 | QStringList proxySettings = strProxySettings.split(",");
|
---|
171 |
|
---|
172 | /* Parse proxy state, host and port: */
|
---|
173 | if (proxySettings.size() > 0)
|
---|
174 | m_enmProxyState = proxyStateFromString(proxySettings[0]);
|
---|
175 | if (proxySettings.size() > 1)
|
---|
176 | m_strProxyHost = proxySettings[1];
|
---|
177 | if (proxySettings.size() > 2)
|
---|
178 | m_strProxyPort = proxySettings[2];
|
---|
179 |
|
---|
180 | /* Parse whether proxy auth enabled and has login/password: */
|
---|
181 | if (proxySettings.size() > 3)
|
---|
182 | m_fAuthEnabled = proxySettings[3] == "authEnabled";
|
---|
183 | if (proxySettings.size() > 4)
|
---|
184 | m_strAuthLogin = proxySettings[4];
|
---|
185 | if (proxySettings.size() > 5)
|
---|
186 | m_strAuthPassword = proxySettings[5];
|
---|
187 | }
|
---|
188 |
|
---|
189 | /** Serializes proxy settings. */
|
---|
190 | QString toString() const
|
---|
191 | {
|
---|
192 | /* Serialize settings: */
|
---|
193 | QString strResult;
|
---|
194 | if (m_enmProxyState != ProxyState_Auto || !m_strProxyHost.isEmpty() || !m_strProxyPort.isEmpty() ||
|
---|
195 | m_fAuthEnabled || !m_strAuthLogin.isEmpty() || !m_strAuthPassword.isEmpty())
|
---|
196 | {
|
---|
197 | QStringList proxySettings;
|
---|
198 | proxySettings << proxyStateToString(m_enmProxyState);
|
---|
199 | proxySettings << m_strProxyHost;
|
---|
200 | proxySettings << m_strProxyPort;
|
---|
201 | proxySettings << QString(m_fAuthEnabled ? "authEnabled" : "authDisabled");
|
---|
202 | proxySettings << m_strAuthLogin;
|
---|
203 | proxySettings << m_strAuthPassword;
|
---|
204 | strResult = proxySettings.join(",");
|
---|
205 | }
|
---|
206 | return strResult;
|
---|
207 | }
|
---|
208 |
|
---|
209 | /** Returns the proxy state. */
|
---|
210 | ProxyState proxyState() const { return m_enmProxyState; }
|
---|
211 | /** Returns the proxy host. */
|
---|
212 | const QString& proxyHost() const { return m_strProxyHost; }
|
---|
213 | /** Returns the proxy port. */
|
---|
214 | const QString& proxyPort() const { return m_strProxyPort; }
|
---|
215 |
|
---|
216 | /** Returns whether the proxy auth is enabled. */
|
---|
217 | bool authEnabled() const { return m_fAuthEnabled; }
|
---|
218 | /** Returns the proxy auth login. */
|
---|
219 | const QString& authLogin() const { return m_strAuthLogin; }
|
---|
220 | /** Returns the proxy auth password. */
|
---|
221 | const QString& authPassword() const { return m_strAuthPassword; }
|
---|
222 |
|
---|
223 | /** Defines the proxy @a enmState. */
|
---|
224 | void setProxyState(ProxyState enmState) { m_enmProxyState = enmState; }
|
---|
225 | /** Defines the proxy @a strHost. */
|
---|
226 | void setProxyHost(const QString &strHost) { m_strProxyHost = strHost; }
|
---|
227 | /** Defines the proxy @a strPort. */
|
---|
228 | void setProxyPort(const QString &strPort) { m_strProxyPort = strPort; }
|
---|
229 |
|
---|
230 | /** Defines whether the proxy auth is @a fEnabled. */
|
---|
231 | void setAuthEnabled(bool fEnabled) { m_fAuthEnabled = fEnabled; }
|
---|
232 | /** Defines the proxy auth @a strLogin. */
|
---|
233 | void setAuthLogin(const QString &strLogin) { m_strAuthLogin = strLogin; }
|
---|
234 | /** Defines the proxy auth @a strPassword. */
|
---|
235 | void setAuthPassword(const QString &strPassword) { m_strAuthPassword = strPassword; }
|
---|
236 |
|
---|
237 | private:
|
---|
238 |
|
---|
239 | /** Converts passed @a state to corresponding #QString. */
|
---|
240 | static QString proxyStateToString(ProxyState state)
|
---|
241 | {
|
---|
242 | switch (state)
|
---|
243 | {
|
---|
244 | case ProxyState_Disabled: return QString("ProxyDisabled");
|
---|
245 | case ProxyState_Enabled: return QString("ProxyEnabled");
|
---|
246 | case ProxyState_Auto: break;
|
---|
247 | }
|
---|
248 | return QString("ProxyAuto");
|
---|
249 | }
|
---|
250 |
|
---|
251 | /** Converts passed @a strState to corresponding #ProxyState. */
|
---|
252 | static ProxyState proxyStateFromString(const QString &strState)
|
---|
253 | {
|
---|
254 | /* Compose the map of known states: */
|
---|
255 | QMap<QString, ProxyState> states;
|
---|
256 | states["ProxyDisabled"] = ProxyState_Disabled; // New since VBox 5.0
|
---|
257 | states["proxyEnabled"] = ProxyState_Enabled; // Old since VBox 4.1
|
---|
258 | states["ProxyEnabled"] = ProxyState_Enabled; // New since VBox 5.0
|
---|
259 | /* Return one of registered or 'Auto' by default: */
|
---|
260 | return states.value(strState, ProxyState_Auto);
|
---|
261 | }
|
---|
262 |
|
---|
263 | /** Holds the proxy state. */
|
---|
264 | ProxyState m_enmProxyState;
|
---|
265 | /** Holds the proxy host. */
|
---|
266 | QString m_strProxyHost;
|
---|
267 | /** Holds the proxy port. */
|
---|
268 | QString m_strProxyPort;
|
---|
269 |
|
---|
270 | /** Holds whether the proxy auth is enabled. */
|
---|
271 | bool m_fAuthEnabled;
|
---|
272 | /** Holds the proxy auth login. */
|
---|
273 | QString m_strAuthLogin;
|
---|
274 | /** Holds the proxy auth password. */
|
---|
275 | QString m_strAuthPassword;
|
---|
276 | };
|
---|
277 |
|
---|
278 | #ifdef Q_WS_MAC
|
---|
279 | # include "VBoxUtils-darwin.h"
|
---|
280 | #endif /* Q_WS_MAC */
|
---|
281 |
|
---|
282 | #endif // !___VBoxUtils_h___
|
---|
283 |
|
---|