VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/ui/VBoxRegistrationDlg.ui.h@ 5487

Last change on this file since 5487 was 5487, checked in by vboxsync, 17 years ago

Update config registration data inner storage for utf-8.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 15.1 KB
Line 
1/**
2 *
3 * VBox frontends: Qt GUI ("VirtualBox"):
4 * VBoxRegistrationDlg UI include (Qt Designer)
5 */
6
7/*
8 * Copyright (C) 2007 innotek GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19/****************************************************************************
20** ui.h extension file, included from the uic-generated form implementation.
21**
22** If you want to add, delete, or rename functions or slots, use
23** Qt Designer to update this file, preserving your code.
24**
25** You should not define a constructor or destructor in this file.
26** Instead, write your code in functions called init() and destroy().
27** These will automatically be called by the form's constructor and
28** destructor.
29*****************************************************************************/
30
31/**
32 * This class is used to encode/decode the registration data.
33 */
34class VBoxRegistrationData
35{
36public:
37
38 VBoxRegistrationData (const QString &aData)
39 : mIsValid (false), mIsRegistered (false)
40 , mData (aData)
41 , mTriesLeft (3 /* the initial tries value! */)
42 {
43 decode (aData);
44 }
45
46 VBoxRegistrationData (const QString &aName, const QString &aEmail,
47 const QString &aPrivate)
48 : mIsValid (true), mIsRegistered (true)
49 , mName (aName), mEmail (aEmail), mPrivate (aPrivate)
50 , mTriesLeft (0)
51 {
52 encode (aName, aEmail, aPrivate);
53 }
54
55 bool isValid() const { return mIsValid; }
56 bool isRegistered() const { return mIsRegistered; }
57
58 const QString &data() const { return mData; }
59 const QString &name() const { return mName; }
60 const QString &email() const { return mEmail; }
61 const QString &isPrivate() const { return mPrivate; }
62
63 uint triesLeft() const { return mTriesLeft; }
64
65private:
66
67 void decode (const QString &aData)
68 {
69 mIsValid = mIsRegistered = false;
70
71 if (aData.isEmpty())
72 return;
73
74 if (aData.startsWith ("triesLeft="))
75 {
76 bool ok = false;
77 uint triesLeft = aData.section ('=', 1, 1).toUInt (&ok);
78 if (!ok)
79 return;
80 mTriesLeft = triesLeft;
81 mIsValid = true;
82 return;
83 }
84
85 /* Decoding CRC32 */
86 QString data = aData;
87 QString crcData = data.right (2 * sizeof (ulong));
88 ulong crcNeed = 0;
89 for (ulong i = 0; i < crcData.length(); i += 2)
90 {
91 crcNeed <<= 8;
92 uchar curByte = (uchar) crcData.mid (i, 2).toUShort (0, 16);
93 crcNeed += curByte;
94 }
95 data.truncate (data.length() - 2 * sizeof (ulong));
96
97 /* Decoding data */
98 QString result;
99 for (ulong i = 0; i < data.length(); i += 4)
100 result += QChar (data.mid (i, 4).toUShort (0, 16));
101 ulong crcNow = crc32 ((uchar*)result.ascii(), result.length());
102
103 /* Check the CRC32 */
104 if (crcNeed != crcNow)
105 return;
106
107 /* Initialize values */
108 QStringList dataList = QStringList::split ("|", result);
109 mName = dataList [0];
110 mEmail = dataList [1];
111 mPrivate = dataList [2];
112
113 mIsValid = true;
114 mIsRegistered = true;
115 }
116
117 void encode (const QString &aName, const QString &aEmail,
118 const QString &aPrivate)
119 {
120 /* Encoding data */
121 QString data = QString ("%1|%2|%3")
122 .arg (aName).arg (aEmail).arg (aPrivate);
123 mData = QString::null;
124 for (ulong i = 0; i < data.length(); ++ i)
125 {
126 QString curPair = QString::number (data.at (i).unicode(), 16);
127 while (curPair.length() < 4)
128 curPair.prepend ('0');
129 mData += curPair;
130 }
131
132 /* Enconding CRC32 */
133 ulong crcNow = crc32 ((uchar*)data.ascii(), data.length());
134 QString crcData;
135 for (ulong i = 0; i < sizeof (ulong); ++ i)
136 {
137 ushort curByte = crcNow & 0xFF;
138 QString curPair = QString::number (curByte, 16);
139 if (curPair.length() == 1)
140 curPair.prepend ("0");
141 crcData = curPair + crcData;
142 crcNow >>= 8;
143 }
144
145 mData += crcData;
146 }
147
148 ulong crc32 (unsigned char *aBufer, int aSize)
149 {
150 /* Filling CRC32 table */
151 ulong crc32;
152 ulong crc_table [256];
153 ulong temp;
154 for (int i = 0; i < 256; ++ i)
155 {
156 temp = i;
157 for (int j = 8; j > 0; -- j)
158 {
159 if (temp & 1)
160 temp = (temp >> 1) ^ 0xedb88320;
161 else
162 temp >>= 1;
163 };
164 crc_table [i] = temp;
165 }
166
167 /* CRC32 calculation */
168 crc32 = 0xffffffff;
169 for (int i = 0; i < aSize; ++ i)
170 {
171 crc32 = crc_table [(crc32 ^ (*aBufer ++)) & 0xff] ^ (crc32 >> 8);
172 }
173 crc32 = crc32 ^ 0xffffffff;
174 return crc32;
175 };
176
177 bool mIsValid : 1;
178 bool mIsRegistered : 1;
179
180 QString mData;
181 QString mName;
182 QString mEmail;
183 QString mPrivate;
184
185 uint mTriesLeft;
186};
187
188/* static */
189bool VBoxRegistrationDlg::hasToBeShown()
190{
191 VBoxRegistrationData regData (vboxGlobal().virtualBox().
192 GetExtraData (VBoxDefs::GUI_RegistrationData));
193
194 return !regData.isValid() ||
195 (!regData.isRegistered() && regData.triesLeft() > 0);
196}
197
198/* Default constructor initialization. */
199void VBoxRegistrationDlg::init()
200{
201 /* Hide unnecessary buttons */
202 helpButton()->setShown (false);
203 cancelButton()->setShown (false);
204 backButton()->setShown (false);
205
206 /* Confirm button initially disabled */
207 finishButton()->setEnabled (false);
208 finishButton()->setAutoDefault (true);
209 finishButton()->setDefault (true);
210
211 /* Setup the label colors for nice scaling */
212 VBoxGlobal::adoptLabelPixmap (pictureLabel);
213
214 /* Adjust text label size */
215 mTextLabel->setMinimumWidth (widthSpacer->minimumSize().width());
216
217 /* Setup validations and maximum text-edit text length */
218 QRegExp nameExp ("[\\S\\s]+");
219 /* E-mail address is validated according to RFC2821, RFC2822,
220 * see http://en.wikipedia.org/wiki/E-mail_address. */
221 QRegExp emailExp ("(([a-zA-Z0-9_\\-\\.!#$%\\*/?|^{}`~&'\\+=]*"
222 "[a-zA-Z0-9_\\-!#$%\\*/?|^{}`~&'\\+=])|"
223 "(\"([\\x0001-\\x0008,\\x000B,\\x000C,\\x000E-\\x0019,\\x007F,"
224 "\\x0021,\\x0023-\\x005B,\\x005D-\\x007E,"
225 "\\x0009,\\x0020]|"
226 "(\\\\[\\x0001-\\x0009,\\x000B,\\x000C,"
227 "\\x000E-\\x007F]))*\"))"
228 "@"
229 "[a-zA-Z0-9\\-]+(\\.[a-zA-Z0-9\\-]+)*");
230 mNameEdit->setValidator (new QRegExpValidator (nameExp, this));
231 mEmailEdit->setValidator (new QRegExpValidator (emailExp, this));
232 mNameEdit->setMaxLength (50);
233 mEmailEdit->setMaxLength (50);
234
235 /* Create connection-timeout timer */
236 mTimeout = new QTimer (this);
237
238 /* Load language constraints */
239 languageChangeImp();
240
241 /* Set required boolean flags */
242 mSuicide = false;
243 mHandshake = true;
244
245 /* Network framework */
246 mNetfw = 0;
247
248 /* Setup connections */
249 disconnect (finishButton(), SIGNAL (clicked()), 0, 0);
250 connect (finishButton(), SIGNAL (clicked()), SLOT (registration()));
251 connect (mTimeout, SIGNAL (timeout()), this, SLOT (processTimeout()));
252 connect (mNameEdit, SIGNAL (textChanged (const QString&)), this, SLOT (validate()));
253 connect (mEmailEdit, SIGNAL (textChanged (const QString&)), this, SLOT (validate()));
254 connect (mTextLabel, SIGNAL (clickedOnLink (const QString &)),
255 &vboxGlobal(), SLOT (openURL (const QString &)));
256
257 /* Resize the dialog initially to minimum size */
258 resize (minimumSize());
259}
260
261/* Default destructor cleanup. */
262void VBoxRegistrationDlg::destroy()
263{
264 delete mNetfw;
265 *mSelf = 0;
266}
267
268/* Setup necessary dialog parameters. */
269void VBoxRegistrationDlg::setup (VBoxRegistrationDlg **aSelf)
270{
271 mSelf = aSelf;
272 *mSelf = this;
273 mUrl = "http://www.innotek.de/register762.php";
274
275 VBoxRegistrationData regData (vboxGlobal().virtualBox().
276 GetExtraData (VBoxDefs::GUI_RegistrationData));
277
278 mNameEdit->setText (regData.name());
279 mEmailEdit->setText (regData.email());
280 mUseCheckBox->setChecked (regData.isPrivate() == "yes");
281}
282
283/* String constants initializer. */
284void VBoxRegistrationDlg::languageChangeImp()
285{
286 finishButton()->setText (tr ("&Confirm"));
287}
288
289void VBoxRegistrationDlg::postRequest (const QString &aHost,
290 const QString &aUrl)
291{
292 delete mNetfw;
293 mNetfw = new VBoxNetworkFramework();
294 connect (mNetfw, SIGNAL (netBegin (int)),
295 SLOT (onNetBegin (int)));
296 connect (mNetfw, SIGNAL (netData (const QByteArray&)),
297 SLOT (onNetData (const QByteArray&)));
298 connect (mNetfw, SIGNAL (netEnd (const QByteArray&)),
299 SLOT (onNetEnd (const QByteArray&)));
300 connect (mNetfw, SIGNAL (netError (const QString&)),
301 SLOT (onNetError (const QString&)));
302
303 mNetfw->postRequest (aHost, aUrl);
304}
305
306
307/* Post the handshake request into the innotek register site. */
308void VBoxRegistrationDlg::registration()
309{
310 /* Disable control elements */
311 mNameEdit->setEnabled (false);
312 mEmailEdit->setEnabled (false);
313 mUseCheckBox->setEnabled (false);
314 finishButton()->setEnabled (false);
315
316 /* Handshake arguments initializing */
317 QString version = vboxGlobal().virtualBox().GetVersion();
318 QUrl::encode (version);
319
320 /* Handshake */
321 QString argument = QString ("?version=%1").arg (version);
322 mTimeout->start (20000, true);
323 postRequest (mUrl.host(), mUrl.path() + argument);
324}
325
326/* This slot is used to control the connection timeout. */
327void VBoxRegistrationDlg::processTimeout()
328{
329 abortRegisterRequest (tr ("Connection timed out."));
330}
331
332/* Handles the network request begining. */
333void VBoxRegistrationDlg::onNetBegin (int aStatus)
334{
335 if (aStatus == 404)
336 abortRegisterRequest (tr ("Could not locate the registration form on "
337 "the server (response: %1).").arg (aStatus));
338 else
339 mTimeout->start (20000, true);
340}
341
342/* Handles the network request data incoming. */
343void VBoxRegistrationDlg::onNetData (const QByteArray&)
344{
345 if (!mSuicide)
346 mTimeout->start (20000, true);
347}
348
349/* Handles the network request end. */
350void VBoxRegistrationDlg::onNetEnd (const QByteArray &aTotalData)
351{
352 if (mSuicide)
353 return;
354
355 mTimeout->stop();
356 if (mHandshake)
357 {
358 /* Registration arguments initializing */
359 QString version = vboxGlobal().virtualBox().GetVersion();
360 QString key (aTotalData);
361 QString platform = getPlatform();
362 QString name = mNameEdit->text();
363 QString email = mEmailEdit->text();
364 QString prvt = mUseCheckBox->isChecked() ? "1" : "0";
365 QUrl::encode (version);
366 QUrl::encode (platform);
367 QUrl::encode (name);
368 QUrl::encode (email);
369
370 /* Registration */
371 QString argument;
372 argument += QString ("?version=%1").arg (version);
373 argument += QString ("&key=%1").arg (key);
374 argument += QString ("&platform=%1").arg (platform);
375 argument += QString ("&name=%1").arg (name);
376 argument += QString ("&email=%1").arg (email);
377 argument += QString ("&private=%1").arg (prvt);
378
379 mHandshake = false;
380 mTimeout->start (20000, true);
381 postRequest (mUrl.host(), mUrl.path() + argument);
382 }
383 else
384 {
385 /* Show registration result */
386 QString result (aTotalData);
387 vboxProblem().showRegisterResult (this, result);
388
389 /* Close the dialog */
390 result == "OK" ? accept() : reject();
391 }
392}
393
394void VBoxRegistrationDlg::onNetError (const QString &aError)
395{
396 abortRegisterRequest (aError);
397}
398
399/* SLOT: QDialog accept slot reimplementation. */
400void VBoxRegistrationDlg::accept()
401{
402 vboxGlobal().virtualBox().SetExtraData (VBoxDefs::GUI_RegistrationDlgWinID,
403 QString::null);
404
405 VBoxRegistrationData regData (mNameEdit->text(), mEmailEdit->text(),
406 mUseCheckBox->isChecked() ? "yes" : "no");
407 vboxGlobal().virtualBox().SetExtraData (VBoxDefs::GUI_RegistrationData,
408 regData.data());
409
410 QDialog::accept();
411}
412
413/* SLOT: QDialog reject slot reimplementation. */
414void VBoxRegistrationDlg::reject()
415{
416 vboxGlobal().virtualBox().SetExtraData (VBoxDefs::GUI_RegistrationDlgWinID,
417 QString::null);
418
419 /* Decrement the triesLeft. */
420 VBoxRegistrationData regData (vboxGlobal().virtualBox().
421 GetExtraData (VBoxDefs::GUI_RegistrationData));
422 if (!regData.isValid() || !regData.isRegistered())
423 {
424 uint triesLeft = regData.triesLeft();
425 if (triesLeft)
426 {
427 -- triesLeft;
428 vboxGlobal().virtualBox().SetExtraData (VBoxDefs::GUI_RegistrationData,
429 QString ("triesLeft=%1")
430 .arg (triesLeft));
431 }
432 }
433
434 QDialog::reject();
435}
436
437void VBoxRegistrationDlg::validate()
438{
439 int pos = 0;
440 QString name = mNameEdit->text();
441 QString email = mEmailEdit->text();
442 finishButton()->setEnabled (
443 mNameEdit->validator()->validate (name, pos) == QValidator::Acceptable &&
444 mEmailEdit->validator()->validate (email, pos) == QValidator::Acceptable);
445}
446
447QString VBoxRegistrationDlg::getPlatform()
448{
449 QString platform;
450
451#if defined (Q_OS_WIN)
452 platform = "win";
453#elif defined (Q_OS_LINUX)
454 platform = "linux";
455#elif defined (Q_OS_MACX)
456 platform = "macosx";
457#elif defined (Q_OS_OS2)
458 platform = "os2";
459#elif defined (Q_OS_FREEBSD)
460 platform = "freebsd";
461#elif defined (Q_OS_SOLARIS)
462 platform = "solaris";
463#else
464 platform = "unknown";
465#endif
466
467 /* the format is <system>.<bitness> */
468 platform += QString (".%1").arg (ARCH_BITS);
469
470 return platform;
471}
472
473/* This wrapper displays an error message box (unless aReason is
474 * QString::null) with the cause of the request-send procedure
475 * termination. After the message box is dismissed, the downloader signals
476 * to close itself on the next event loop iteration. */
477void VBoxRegistrationDlg::abortRegisterRequest (const QString &aReason)
478{
479 /* Protect against double kill request. */
480 if (mSuicide)
481 return;
482 mSuicide = true;
483
484 if (!aReason.isNull())
485 vboxProblem().cannotConnectRegister (this, mUrl.toString(), aReason);
486
487 /* Allows all the queued signals to be processed before quit. */
488 QTimer::singleShot (0, this, SLOT (reject()));
489}
490
491void VBoxRegistrationDlg::showEvent (QShowEvent *aEvent)
492{
493 validate();
494 QWizard::showEvent (aEvent);
495}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette