1 | /* $Id: VBoxUtils.h 69500 2017-10-28 15:14:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox Qt GUI - Declarations of utility classes and functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 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 | public slots:
|
---|
41 |
|
---|
42 | /** Assigns property value. */
|
---|
43 | void sltAssignProperty(const QString &strValue)
|
---|
44 | { parent()->setProperty(m_strName.toLatin1().constData(), strValue); }
|
---|
45 |
|
---|
46 | private:
|
---|
47 |
|
---|
48 | /** Holds property name. */
|
---|
49 | const QString m_strName;
|
---|
50 | };
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Simple class which simulates focus-proxy rule redirecting widget
|
---|
54 | * assigned shortcut to desired widget.
|
---|
55 | */
|
---|
56 | class QIFocusProxy : protected QObject
|
---|
57 | {
|
---|
58 | Q_OBJECT;
|
---|
59 |
|
---|
60 | public:
|
---|
61 |
|
---|
62 | QIFocusProxy (QWidget *aFrom, QWidget *aTo)
|
---|
63 | : QObject (aFrom), mFrom (aFrom), mTo (aTo)
|
---|
64 | {
|
---|
65 | mFrom->installEventFilter (this);
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected:
|
---|
69 |
|
---|
70 | bool eventFilter (QObject *aObject, QEvent *aEvent)
|
---|
71 | {
|
---|
72 | if (aObject == mFrom && aEvent->type() == QEvent::Shortcut)
|
---|
73 | {
|
---|
74 | mTo->setFocus();
|
---|
75 | return true;
|
---|
76 | }
|
---|
77 | return QObject::eventFilter (aObject, aEvent);
|
---|
78 | }
|
---|
79 |
|
---|
80 | QWidget *mFrom;
|
---|
81 | QWidget *mTo;
|
---|
82 | };
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * QTextEdit reimplementation to feat some extended requirements.
|
---|
86 | */
|
---|
87 | class QRichTextEdit : public QTextEdit
|
---|
88 | {
|
---|
89 | Q_OBJECT;
|
---|
90 |
|
---|
91 | public:
|
---|
92 |
|
---|
93 | QRichTextEdit (QWidget *aParent = 0) : QTextEdit (aParent) {}
|
---|
94 |
|
---|
95 | void setViewportMargins (int aLeft, int aTop, int aRight, int aBottom)
|
---|
96 | {
|
---|
97 | QTextEdit::setViewportMargins (aLeft, aTop, aRight, aBottom);
|
---|
98 | }
|
---|
99 | };
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * QTextBrowser reimplementation to feat some extended requirements.
|
---|
103 | */
|
---|
104 | class QRichTextBrowser : public QTextBrowser
|
---|
105 | {
|
---|
106 | Q_OBJECT;
|
---|
107 |
|
---|
108 | public:
|
---|
109 |
|
---|
110 | QRichTextBrowser (QWidget *aParent) : QTextBrowser (aParent) {}
|
---|
111 |
|
---|
112 | void setViewportMargins (int aLeft, int aTop, int aRight, int aBottom)
|
---|
113 | {
|
---|
114 | QTextBrowser::setViewportMargins (aLeft, aTop, aRight, aBottom);
|
---|
115 | }
|
---|
116 | };
|
---|
117 |
|
---|
118 | /** Object containing functionality
|
---|
119 | * to (de)serialize proxy settings. */
|
---|
120 | class UIProxyManager
|
---|
121 | {
|
---|
122 | public:
|
---|
123 |
|
---|
124 | /** Proxy states. */
|
---|
125 | enum ProxyState
|
---|
126 | {
|
---|
127 | ProxyState_Disabled,
|
---|
128 | ProxyState_Enabled,
|
---|
129 | ProxyState_Auto
|
---|
130 | };
|
---|
131 |
|
---|
132 | /** Constructs object which parses passed @a strProxySettings. */
|
---|
133 | UIProxyManager(const QString &strProxySettings = QString())
|
---|
134 | : m_enmProxyState(ProxyState_Auto)
|
---|
135 | , m_fAuthEnabled(false)
|
---|
136 | {
|
---|
137 | /* Parse proxy settings: */
|
---|
138 | if (strProxySettings.isEmpty())
|
---|
139 | return;
|
---|
140 | QStringList proxySettings = strProxySettings.split(",");
|
---|
141 |
|
---|
142 | /* Parse proxy state, host and port: */
|
---|
143 | if (proxySettings.size() > 0)
|
---|
144 | m_enmProxyState = proxyStateFromString(proxySettings[0]);
|
---|
145 | if (proxySettings.size() > 1)
|
---|
146 | m_strProxyHost = proxySettings[1];
|
---|
147 | if (proxySettings.size() > 2)
|
---|
148 | m_strProxyPort = proxySettings[2];
|
---|
149 |
|
---|
150 | /* Parse whether proxy auth enabled and has login/password: */
|
---|
151 | if (proxySettings.size() > 3)
|
---|
152 | m_fAuthEnabled = proxySettings[3] == "authEnabled";
|
---|
153 | if (proxySettings.size() > 4)
|
---|
154 | m_strAuthLogin = proxySettings[4];
|
---|
155 | if (proxySettings.size() > 5)
|
---|
156 | m_strAuthPassword = proxySettings[5];
|
---|
157 | }
|
---|
158 |
|
---|
159 | /** Serializes proxy settings. */
|
---|
160 | QString toString() const
|
---|
161 | {
|
---|
162 | /* Serialize settings: */
|
---|
163 | QString strResult;
|
---|
164 | if (m_enmProxyState != ProxyState_Auto || !m_strProxyHost.isEmpty() || !m_strProxyPort.isEmpty() ||
|
---|
165 | m_fAuthEnabled || !m_strAuthLogin.isEmpty() || !m_strAuthPassword.isEmpty())
|
---|
166 | {
|
---|
167 | QStringList proxySettings;
|
---|
168 | proxySettings << proxyStateToString(m_enmProxyState);
|
---|
169 | proxySettings << m_strProxyHost;
|
---|
170 | proxySettings << m_strProxyPort;
|
---|
171 | proxySettings << QString(m_fAuthEnabled ? "authEnabled" : "authDisabled");
|
---|
172 | proxySettings << m_strAuthLogin;
|
---|
173 | proxySettings << m_strAuthPassword;
|
---|
174 | strResult = proxySettings.join(",");
|
---|
175 | }
|
---|
176 | return strResult;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /** Returns the proxy state. */
|
---|
180 | ProxyState proxyState() const { return m_enmProxyState; }
|
---|
181 | /** Returns the proxy host. */
|
---|
182 | const QString& proxyHost() const { return m_strProxyHost; }
|
---|
183 | /** Returns the proxy port. */
|
---|
184 | const QString& proxyPort() const { return m_strProxyPort; }
|
---|
185 |
|
---|
186 | /** Returns whether the proxy auth is enabled. */
|
---|
187 | bool authEnabled() const { return m_fAuthEnabled; }
|
---|
188 | /** Returns the proxy auth login. */
|
---|
189 | const QString& authLogin() const { return m_strAuthLogin; }
|
---|
190 | /** Returns the proxy auth password. */
|
---|
191 | const QString& authPassword() const { return m_strAuthPassword; }
|
---|
192 |
|
---|
193 | /** Defines the proxy @a enmState. */
|
---|
194 | void setProxyState(ProxyState enmState) { m_enmProxyState = enmState; }
|
---|
195 | /** Defines the proxy @a strHost. */
|
---|
196 | void setProxyHost(const QString &strHost) { m_strProxyHost = strHost; }
|
---|
197 | /** Defines the proxy @a strPort. */
|
---|
198 | void setProxyPort(const QString &strPort) { m_strProxyPort = strPort; }
|
---|
199 |
|
---|
200 | /** Defines whether the proxy auth is @a fEnabled. */
|
---|
201 | void setAuthEnabled(bool fEnabled) { m_fAuthEnabled = fEnabled; }
|
---|
202 | /** Defines the proxy auth @a strLogin. */
|
---|
203 | void setAuthLogin(const QString &strLogin) { m_strAuthLogin = strLogin; }
|
---|
204 | /** Defines the proxy auth @a strPassword. */
|
---|
205 | void setAuthPassword(const QString &strPassword) { m_strAuthPassword = strPassword; }
|
---|
206 |
|
---|
207 | private:
|
---|
208 |
|
---|
209 | /** Converts passed @a state to corresponding #QString. */
|
---|
210 | static QString proxyStateToString(ProxyState state)
|
---|
211 | {
|
---|
212 | switch (state)
|
---|
213 | {
|
---|
214 | case ProxyState_Disabled: return QString("ProxyDisabled");
|
---|
215 | case ProxyState_Enabled: return QString("ProxyEnabled");
|
---|
216 | case ProxyState_Auto: break;
|
---|
217 | }
|
---|
218 | return QString("ProxyAuto");
|
---|
219 | }
|
---|
220 |
|
---|
221 | /** Converts passed @a strState to corresponding #ProxyState. */
|
---|
222 | static ProxyState proxyStateFromString(const QString &strState)
|
---|
223 | {
|
---|
224 | /* Compose the map of known states: */
|
---|
225 | QMap<QString, ProxyState> states;
|
---|
226 | states["ProxyDisabled"] = ProxyState_Disabled; // New since VBox 5.0
|
---|
227 | states["proxyEnabled"] = ProxyState_Enabled; // Old since VBox 4.1
|
---|
228 | states["ProxyEnabled"] = ProxyState_Enabled; // New since VBox 5.0
|
---|
229 | /* Return one of registered or 'Auto' by default: */
|
---|
230 | return states.value(strState, ProxyState_Auto);
|
---|
231 | }
|
---|
232 |
|
---|
233 | /** Holds the proxy state. */
|
---|
234 | ProxyState m_enmProxyState;
|
---|
235 | /** Holds the proxy host. */
|
---|
236 | QString m_strProxyHost;
|
---|
237 | /** Holds the proxy port. */
|
---|
238 | QString m_strProxyPort;
|
---|
239 |
|
---|
240 | /** Holds whether the proxy auth is enabled. */
|
---|
241 | bool m_fAuthEnabled;
|
---|
242 | /** Holds the proxy auth login. */
|
---|
243 | QString m_strAuthLogin;
|
---|
244 | /** Holds the proxy auth password. */
|
---|
245 | QString m_strAuthPassword;
|
---|
246 | };
|
---|
247 |
|
---|
248 | #ifdef VBOX_WS_MAC
|
---|
249 | # include "VBoxUtils-darwin.h"
|
---|
250 | #endif /* VBOX_WS_MAC */
|
---|
251 |
|
---|
252 | #endif // !___VBoxUtils_h___
|
---|
253 |
|
---|