VirtualBox

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

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

2332: Registration feature:

New symbols [according RFC 2822 for eMail name] are currently allowed to be entered in Reg.Dlg.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 14.6 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 += 2)
100 result += QChar (data.mid (i, 2).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 if (curPair.length() == 1)
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 ("[a-zA-Z0-9\\(\\)_\\-\\.\\s]+");
219 QRegExp emailExp ("[a-zA-Z][a-zA-Z0-9_\\-\\.!#$%\\*/?|^{}`~&'\\+=]*[a-zA-Z0-9]@[a-zA-Z][a-zA-Z0-9_\\-\\.]*[a-zA-Z]");
220 mNameEdit->setValidator (new QRegExpValidator (nameExp, this));
221 mEmailEdit->setValidator (new QRegExpValidator (emailExp, this));
222 mNameEdit->setMaxLength (50);
223 mEmailEdit->setMaxLength (50);
224
225 /* Create connection-timeout timer */
226 mTimeout = new QTimer (this);
227
228 /* Load language constraints */
229 languageChangeImp();
230
231 /* Set required boolean flags */
232 mSuicide = false;
233 mHandshake = true;
234
235 /* Network framework */
236 mNetfw = 0;
237
238 /* Setup connections */
239 disconnect (finishButton(), SIGNAL (clicked()), 0, 0);
240 connect (finishButton(), SIGNAL (clicked()), SLOT (registration()));
241 connect (mTimeout, SIGNAL (timeout()), this, SLOT (processTimeout()));
242 connect (mNameEdit, SIGNAL (textChanged (const QString&)), this, SLOT (validate()));
243 connect (mEmailEdit, SIGNAL (textChanged (const QString&)), this, SLOT (validate()));
244 connect (mTextLabel, SIGNAL (clickedOnLink (const QString &)),
245 &vboxGlobal(), SLOT (openURL (const QString &)));
246
247 /* Resize the dialog initially to minimum size */
248 resize (minimumSize());
249}
250
251/* Default destructor cleanup. */
252void VBoxRegistrationDlg::destroy()
253{
254 delete mNetfw;
255 *mSelf = 0;
256}
257
258/* Setup necessary dialog parameters. */
259void VBoxRegistrationDlg::setup (VBoxRegistrationDlg **aSelf)
260{
261 mSelf = aSelf;
262 *mSelf = this;
263 mUrl = "http://www.innotek.de/register762.php";
264
265 VBoxRegistrationData regData (vboxGlobal().virtualBox().
266 GetExtraData (VBoxDefs::GUI_RegistrationData));
267
268 mNameEdit->setText (regData.name());
269 mEmailEdit->setText (regData.email());
270 mUseCheckBox->setChecked (regData.isPrivate() == "yes");
271}
272
273/* String constants initializer. */
274void VBoxRegistrationDlg::languageChangeImp()
275{
276 finishButton()->setText (tr ("&Confirm"));
277}
278
279void VBoxRegistrationDlg::postRequest (const QString &aHost,
280 const QString &aUrl)
281{
282 delete mNetfw;
283 mNetfw = new VBoxNetworkFramework();
284 connect (mNetfw, SIGNAL (netBegin (int)),
285 SLOT (onNetBegin (int)));
286 connect (mNetfw, SIGNAL (netData (const QByteArray&)),
287 SLOT (onNetData (const QByteArray&)));
288 connect (mNetfw, SIGNAL (netEnd (const QByteArray&)),
289 SLOT (onNetEnd (const QByteArray&)));
290 connect (mNetfw, SIGNAL (netError (const QString&)),
291 SLOT (onNetError (const QString&)));
292
293 mNetfw->postRequest (aHost, aUrl);
294}
295
296
297/* Post the handshake request into the innotek register site. */
298void VBoxRegistrationDlg::registration()
299{
300 /* Disable control elements */
301 mNameEdit->setEnabled (false);
302 mEmailEdit->setEnabled (false);
303 mUseCheckBox->setEnabled (false);
304 finishButton()->setEnabled (false);
305
306 /* Handshake arguments initializing */
307 QString version = vboxGlobal().virtualBox().GetVersion();
308 QUrl::encode (version);
309
310 /* Handshake */
311 QString argument = QString ("?version=%1").arg (version);
312 mTimeout->start (20000, true);
313 postRequest (mUrl.host(), mUrl.path() + argument);
314}
315
316/* This slot is used to control the connection timeout. */
317void VBoxRegistrationDlg::processTimeout()
318{
319 abortRegisterRequest (tr ("Connection timed out."));
320}
321
322/* Handles the network request begining. */
323void VBoxRegistrationDlg::onNetBegin (int aStatus)
324{
325 if (aStatus == 404)
326 abortRegisterRequest (tr ("Could not locate the registration form on "
327 "the server (response: %1).").arg (aStatus));
328 else
329 mTimeout->start (20000, true);
330}
331
332/* Handles the network request data incoming. */
333void VBoxRegistrationDlg::onNetData (const QByteArray&)
334{
335 if (!mSuicide)
336 mTimeout->start (20000, true);
337}
338
339/* Handles the network request end. */
340void VBoxRegistrationDlg::onNetEnd (const QByteArray &aTotalData)
341{
342 if (mSuicide)
343 return;
344
345 mTimeout->stop();
346 if (mHandshake)
347 {
348 /* Registration arguments initializing */
349 QString version = vboxGlobal().virtualBox().GetVersion();
350 QString key (aTotalData);
351 QString platform = getPlatform();
352 QString name = mNameEdit->text();
353 QString email = mEmailEdit->text();
354 QString prvt = mUseCheckBox->isChecked() ? "1" : "0";
355 QUrl::encode (version);
356 QUrl::encode (platform);
357 QUrl::encode (name);
358 QUrl::encode (email);
359
360 /* Registration */
361 QString argument;
362 argument += QString ("?version=%1").arg (version);
363 argument += QString ("&key=%1").arg (key);
364 argument += QString ("&platform=%1").arg (platform);
365 argument += QString ("&name=%1").arg (name);
366 argument += QString ("&email=%1").arg (email);
367 argument += QString ("&private=%1").arg (prvt);
368
369 mHandshake = false;
370 mTimeout->start (20000, true);
371 postRequest (mUrl.host(), mUrl.path() + argument);
372 }
373 else
374 {
375 /* Show registration result */
376 QString result (aTotalData);
377 vboxProblem().showRegisterResult (this, result);
378
379 /* Close the dialog */
380 result == "OK" ? accept() : reject();
381 }
382}
383
384void VBoxRegistrationDlg::onNetError (const QString &aError)
385{
386 abortRegisterRequest (aError);
387}
388
389/* SLOT: QDialog accept slot reimplementation. */
390void VBoxRegistrationDlg::accept()
391{
392 vboxGlobal().virtualBox().SetExtraData (VBoxDefs::GUI_RegistrationDlgWinID,
393 QString::null);
394
395 VBoxRegistrationData regData (mNameEdit->text(), mEmailEdit->text(),
396 mUseCheckBox->isChecked() ? "yes" : "no");
397 vboxGlobal().virtualBox().SetExtraData (VBoxDefs::GUI_RegistrationData,
398 regData.data());
399
400 QDialog::accept();
401}
402
403/* SLOT: QDialog reject slot reimplementation. */
404void VBoxRegistrationDlg::reject()
405{
406 vboxGlobal().virtualBox().SetExtraData (VBoxDefs::GUI_RegistrationDlgWinID,
407 QString::null);
408
409 /* Decrement the triesLeft. */
410 VBoxRegistrationData regData (vboxGlobal().virtualBox().
411 GetExtraData (VBoxDefs::GUI_RegistrationData));
412 if (!regData.isValid() || !regData.isRegistered())
413 {
414 uint triesLeft = regData.triesLeft();
415 if (triesLeft)
416 {
417 -- triesLeft;
418 vboxGlobal().virtualBox().SetExtraData (VBoxDefs::GUI_RegistrationData,
419 QString ("triesLeft=%1")
420 .arg (triesLeft));
421 }
422 }
423
424 QDialog::reject();
425}
426
427void VBoxRegistrationDlg::validate()
428{
429 int pos = 0;
430 QString name = mNameEdit->text();
431 QString email = mEmailEdit->text();
432 finishButton()->setEnabled (
433 mNameEdit->validator()->validate (name, pos) == QValidator::Acceptable &&
434 mEmailEdit->validator()->validate (email, pos) == QValidator::Acceptable);
435}
436
437QString VBoxRegistrationDlg::getPlatform()
438{
439 QString platform;
440
441#if defined (Q_OS_WIN)
442 platform = "win";
443#elif defined (Q_OS_LINUX)
444 platform = "linux";
445#elif defined (Q_OS_MACX)
446 platform = "macosx";
447#elif defined (Q_OS_OS2)
448 platform = "os2";
449#elif defined (Q_OS_FREEBSD)
450 platform = "freebsd";
451#elif defined (Q_OS_SOLARIS)
452 platform = "solaris";
453#else
454 platform = "unknown";
455#endif
456
457 /* the format is <system>.<bitness> */
458 platform += QString (".%1").arg (ARCH_BITS);
459
460 return platform;
461}
462
463/* This wrapper displays an error message box (unless aReason is
464 * QString::null) with the cause of the request-send procedure
465 * termination. After the message box is dismissed, the downloader signals
466 * to close itself on the next event loop iteration. */
467void VBoxRegistrationDlg::abortRegisterRequest (const QString &aReason)
468{
469 /* Protect against double kill request. */
470 if (mSuicide)
471 return;
472 mSuicide = true;
473
474 if (!aReason.isNull())
475 vboxProblem().cannotConnectRegister (this, mUrl.toString(), aReason);
476
477 /* Allows all the queued signals to be processed before quit. */
478 QTimer::singleShot (0, this, SLOT (reject()));
479}
480
481void VBoxRegistrationDlg::showEvent (QShowEvent *aEvent)
482{
483 validate();
484 QWizard::showEvent (aEvent);
485}
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