UltraScan III
gui/us_license.cpp
Go to the documentation of this file.
1 #include "us_license.h"
3 #include "us_license_t.h"
4 #include "us_settings.h"
5 #include "us_gui_settings.h"
6 #include "us_help.h"
7 #include "us_defines.h"
8 #include "us_http_post.h"
9 
10 US_License::US_License( QWidget* parent, Qt::WindowFlags flags )
11  : US_Widgets( true, parent, flags )
12 {
13  setWindowTitle( "UltraScan III Registration" );
14  setPalette( US_GuiSettings::frameColor() );
15 
16  // FIXME: This layout is positioned with absolute values. The appearance
17  // will be poor if the user's settings are set with a large font size
18  // (probably anything over 12pt).
19 
20 
21  const int width = 5 * pushbutton + 4 * spacing;
22 
23  int xpos = spacing;
24  int ypos = spacing;
25 
26  // Banner
27  QLabel* banner = us_banner(
28  tr( "UltraScan III Registration\nPlease enter all fields" ) );
29 
30  banner->setGeometry(
31  QRect( xpos, ypos, width, spacing + 2 * rowHeight ) );
32 
33  // Row 1 - Name
34  ypos += rowHeight * 3 + spacing;
35 
36  QLabel* firstname = us_label( tr( "Name (first, last):" ), 0, QFont::Bold );
37  firstname->setGeometry( xpos, ypos, buttonw, rowHeight );
38 
39  xpos += buttonw + spacing;
40 
41  le_firstname = us_lineedit( "" );
42  le_firstname->setGeometry( xpos, ypos, half_buttonw, rowHeight );
43 
44  connect( le_firstname, SIGNAL( textChanged ( const QString& ) ),
45  SLOT ( update_firstname( const QString& ) ) );
46 
47  xpos += half_buttonw + spacing;
48 
49  QLabel* comma = new QLabel( ",", this );
50  comma->setGeometry( xpos, ypos, 10, rowHeight );
51 
52  xpos += spacing + 10;
53 
54  le_lastname = us_lineedit( "" );
55  le_lastname->setGeometry( xpos, ypos, half_buttonw, rowHeight );
56 
57  connect( le_lastname, SIGNAL( textChanged ( const QString& ) ),
58  SLOT ( update_lastname( const QString& ) ) );
59 
60  // Row 2 - Email
61  xpos = spacing;
62  ypos += rowHeight + spacing;
63 
64  QLabel* email = us_label( tr( "E-mail Address:" ), 0, QFont::Bold );
65  email->setGeometry( xpos, ypos, buttonw, rowHeight );
66 
67  xpos += buttonw + spacing;
68 
69  le_email = us_lineedit( "" );
70  le_email->setGeometry( xpos, ypos, full_buttonw, rowHeight );
71  connect( le_email, SIGNAL( textChanged ( const QString& ) ),
72  SLOT ( update_email( const QString& ) ) );
73 
74  // Row 3 - Institution
75  xpos = spacing;
76  ypos += rowHeight + spacing;
77 
78  QLabel* institution = us_label( tr("Institution:" ), 0, QFont::Bold );
79  institution->setGeometry( xpos, ypos, buttonw, rowHeight );
80 
81  xpos += spacing + buttonw;
82 
84  le_institution->setGeometry( xpos, ypos, full_buttonw, rowHeight );
85  connect( le_institution, SIGNAL( textChanged ( const QString& ) ),
86  SLOT ( update_institution( const QString& ) ) );
87 
88  // Row 4 - Address
89  xpos = spacing;
90  ypos += rowHeight + spacing;
91 
92  QLabel* address = us_label( tr( "Address:" ), 0, QFont::Bold );
93  address->setGeometry( xpos, ypos, buttonw, rowHeight );
94 
95  xpos += buttonw + spacing;
96 
97  le_address = us_lineedit( "" );
98  le_address->setGeometry( xpos, ypos, full_buttonw, rowHeight );
99  connect( le_address, SIGNAL( textChanged ( const QString& ) ),
100  SLOT ( update_address( const QString& ) ) );
101 
102  // Row 5 - City/State/Zip
103  xpos = spacing;
104  ypos += rowHeight + spacing;
105 
106  QLabel* city = us_label( tr( "City:" ), 0, QFont::Bold );
107  city->setGeometry( xpos, ypos, buttonw, rowHeight );
108 
109  xpos += spacing + buttonw;
110 
111  le_city = us_lineedit( "" );
112  le_city->setGeometry( xpos, ypos, buttonw, rowHeight );
113  connect( le_city, SIGNAL( textChanged( const QString& ) ),
114  SLOT ( update_city( const QString& ) ) );
115 
116  xpos += buttonw + spacing;
117 
118  QLabel* lb_state = us_label( tr( "State:" ), 0, QFont::Bold );
119  lb_state->setGeometry( xpos, ypos, smallColumn, rowHeight );
120 
121  xpos += smallColumn + spacing;
122 
124  states << "NON-US" << "AL" << "AR" << "AZ" << "CA" << "CO" << "CT"
125  << "DC" << "DE" << "FL" << "GA" << "HI" << "IA" << "ID"
126  << "IL" << "IN" << "KS" << "KY" << "LA" << "MA" << "MD"
127  << "ME" << "MI" << "MN" << "MO" << "MS" << "MT" << "NC"
128  << "ND" << "NE" << "NH" << "NJ" << "NM" << "NV" << "NY"
129  << "OH" << "OK" << "OR" << "PA" << "PR" << "RI" << "SC"
130  << "SD" << "TN" << "TX" << "UT" << "VA" << "VI" << "VT"
131  << "WA" << "WI" << "WV" << "WY" ;
132 
133  cbb_state->addItems( states );
134 
135  state = "NON-US"; // Initialize
136  cbb_state->setGeometry( xpos, ypos, mediumColumn, rowHeight );
137  connect( cbb_state, SIGNAL( currentIndexChanged( const QString& ) ),
138  SLOT ( update_state ( const QString& ) ) );
139 
140  xpos += mediumColumn + spacing + 10; // Adjust
141 
142  QLabel* zip = us_label( tr( "Zip:" ), 0, QFont::Bold );
143  zip->setGeometry( xpos, ypos, smallColumn, rowHeight );
144 
145  xpos += smallColumn + spacing;
146 
147  le_zip = us_lineedit( "" );
148  le_zip->setGeometry( xpos, ypos, mediumColumn, rowHeight );
149  connect( le_zip, SIGNAL( textChanged( const QString& ) ),
150  SLOT ( update_zip ( const QString& ) ) );
151 
152  // Row 6 - Phone/License Type/Version
153  xpos = spacing;
154  ypos += rowHeight + spacing;
155 
156  QLabel* phone = us_label( tr( "Phone Number:" ), 0, QFont::Bold );
157  phone->setGeometry( xpos, ypos, buttonw, rowHeight );
158 
159  xpos += spacing + buttonw;
160 
161  le_phone = us_lineedit( "" );;
162  le_phone->setGeometry( xpos, ypos, buttonw, rowHeight );
163  connect( le_phone, SIGNAL( textChanged ( const QString& ) ),
164  SLOT ( update_phone( const QString& ) ) );
165 
166  xpos += buttonw + spacing;
167 
168  QLabel* lb_licensetype = us_label( tr( "License:" ), 0, QFont::Bold );
169  lb_licensetype->setGeometry( xpos, ypos, smallColumn, rowHeight );
170 
171  xpos += smallColumn + spacing;
172 
174  types << "academic" << "commercial";
175  cbb_licensetype->addItems( types );
176  licensetype = "academic"; // Initialize
177  cbb_licensetype->setGeometry( xpos, ypos, mediumColumn, rowHeight );
178 
179  connect( cbb_licensetype, SIGNAL( currentIndexChanged( const QString& ) ),
180  SLOT ( update_licensetype ( const QString& ) ) );
181 
182  xpos += mediumColumn + spacing + 10; // Adjust
183 
184  lbl_version = us_label( tr( "Version:" ), 0, QFont::Bold );
185  lbl_version->setGeometry( xpos, ypos, smallColumn, rowHeight );
186 
187  xpos = spacing;
188  ypos += 2 * rowHeight + spacing;
189 
190  QLabel* status = us_banner( tr( "License Status" ) );
191  status->setGeometry( QRect( xpos, ypos, width, spacing + rowHeight ) );
192 
193 
194  // Very light gray, for read-only line edits
195  QPalette gray = US_GuiSettings::editColor();
196  gray.setColor( QPalette::Base, QColor( 0xe0, 0xe0, 0xe0 ) );
197 
198  // Row 7 - Platform
199  xpos = spacing;
200  ypos += rowHeight + 2 * spacing;
201 
202  lbl_platform = us_label( "Platform / OS:", 0, QFont::Bold );
203  lbl_platform->setGeometry( xpos, ypos, buttonw, rowHeight );
204 
205  xpos += buttonw + spacing;
206 
207  le_platform = us_lineedit( "", 0 );
208  le_platform->setPalette ( gray );
209  le_platform->setReadOnly( true );
210  le_platform->setGeometry( xpos, ypos, full_buttonw, rowHeight );
211 
212  // Row 8 - Expiration
213  xpos = spacing;
214  ypos += rowHeight + 2 * spacing;
215 
216  lbl_expiration = us_label( "Expiration:", 0, QFont::Bold );
217  lbl_expiration->setGeometry( xpos, ypos, buttonw, rowHeight );
218 
219  xpos += buttonw + spacing;
220 
221  le_expiration = us_lineedit( "", 0 );
222  le_expiration->setPalette ( gray );
223  le_expiration->setReadOnly( true );
224  le_expiration->setGeometry( xpos, ypos, full_buttonw, rowHeight );
225 
226  // Row 9 - Version
227  xpos = spacing;
228  ypos += rowHeight + 2 * spacing;
229 
230  lbl_valid = us_label( "UltraScan III Version:", 0, QFont::Bold );
231  lbl_valid->setGeometry( xpos, ypos, buttonw, rowHeight );
232 
233  xpos += buttonw + spacing;
234 
235  le_registration = us_lineedit( "", 0 );
236  le_registration->setPalette ( gray );
237  le_registration->setReadOnly( true );
238  le_registration->setGeometry( xpos, ypos, full_buttonw, rowHeight );
239 
240  // Row 10 - Pushbuttons
241  xpos = spacing + pushbutton + spacing;
242  ypos += spacing + rowHeight;
243 
244  pb_update = us_pushbutton( tr( "Register" ) );
245  pb_update->setGeometry( xpos, ypos, pushbutton, rowHeight );
246  connect( pb_update, SIGNAL( clicked() ), SLOT( update() ) );
247 
248  QString error;
249  QString validMsg;
250 
251  switch ( US_License_t::isValid( error, QStringList() ) )
252  {
254  pb_update->setText( "Finish Registration" );
255  break;
256 
257  case US_License_t::OK:
259  pb_update->setText( "Update / Renew" );
260  break;
261  }
262 
263  xpos += pushbutton + spacing;
264 
265  pb_help = us_pushbutton( tr( "Help" ) );
266  pb_help->setGeometry( xpos, ypos, pushbutton, rowHeight );
267  connect( pb_help, SIGNAL( clicked() ), SLOT( help() ) );
268 
269  xpos += pushbutton + spacing;
270 
271  pb_cancel = us_pushbutton( tr( "Close" ) );
272  pb_cancel->setGeometry( xpos, ypos, pushbutton, rowHeight );
273  connect( pb_cancel, SIGNAL( clicked() ), SLOT( close() ) );
274 
275  // Finish up
276  ypos += 30;
277  setMinimumSize( spacing + width + spacing, ypos );
278 
279  updating_email = false;
280  load_current();
281 }
282 
283 void US_License::help( void )
284 {
285  online_help.show_help( "manual/register.html" );
286 }
287 
288 QString US_License::titleCase( const QString& phrase )
289 {
290  QString title = phrase;
291  bool cap = true;
292 
293  for ( int i = 0; i < title.size(); i++ )
294  {
295  QChar c = title[ i ];
296 
297  if ( c == QChar( ' ' ) ) cap = true;
298 
299  if ( cap )
300  {
301  title[ i ] = c.toUpper();
302  cap = false;
303  }
304  else
305  title[ i ] = c.toLower();
306  }
307 
308  return title;
309 }
310 
312 {
313  lastname = "";
314  firstname = "";
315  institution = "";
316  address = "";
317  city = "";
318  state = "NON-US";
319  zip = "";
320  phone = "";
321  email = "";
322  version = US_Version; // us_defines.h
323  validation = "";
324  expiration = "";
325 
326  os = titleCase( OS );
327  platform = titleCase( PLATFORM );
328 
329  QStringList license = US_Settings::license();
330 
331  if ( license.size() >= 12 )
332  {
333  lastname = license [ 0 ];
334  firstname = license [ 1 ];
335  institution = license [ 2 ];
336  address = license [ 3 ];
337  city = license [ 4 ];
338  state = license [ 5 ];
339  zip = license [ 6 ];
340  phone = license [ 7 ];
341  email = license [ 8 ];
342  platform = titleCase( license [ 9 ] );
343  os = titleCase( license [ 10 ] );
344  version = license [ 11 ];
345  }
346 
347  if ( license.size() == 14 )
348  {
349  validation = license [ 12 ];
350  expiration = license [ 13 ];
351  }
352 
353  update_screen();
354 }
355 
357 {
358  le_firstname ->setText( firstname );
359  le_lastname ->setText( lastname );
360  le_institution ->setText( institution );
361  le_address ->setText( address );
362  le_city ->setText( city );
363  le_zip ->setText( zip );
364  le_phone ->setText( phone );
365  le_email ->setText( email );
366 
367  le_platform ->setText( platform + " / " + os );
368  le_expiration ->setText( expiration );
369  le_registration->setText( version );
370 
371  // License type
372  for ( int i = 0; i < types.count(); i++ )
373  {
374  if ( licensetype == types.at( i ) )
375  {
376  cbb_licensetype->setCurrentIndex( i );
377  break;
378  }
379  }
380 
381  // State
382  for ( int i = 0; i < states.count(); i++ )
383  {
384  if ( state == states.value( i ) )
385  {
386  cbb_state->setCurrentIndex( i );
387  break;
388  }
389  }
390 
391  // Update status
392  QString error;
393  QString validMsg;
394  int status = US_License_t::isValid( error, QStringList() );
395 
396  switch ( status )
397  {
398  case US_License_t::OK:
399  validMsg = "Valid Registration";
400  break;
401 
403  validMsg = "Expired Registration";
404  break;
405 
408  case US_License_t::BadOS:
409  validMsg = "Invalid Registration";
410  break;
411 
413  validMsg = "Missing Registration";
414  break;
415 
417  validMsg = "Pending Registration";
418  break;
419 
420  default:
421  validMsg = "Unknown";
422  }
423 
424  //lbl_valid->setText( validMsg );
425 }
426 
427 void US_License::update( void )
428 {
429  if ( firstname == "" ||
430  lastname == "" ||
431  institution == "" ||
432  address == "" ||
433  state == "" ||
434  zip == "" ||
435  phone == "" ||
436  email == "" )
437  {
438  QMessageBox::information ( this,
439  tr( "Request Error" ),
440  tr( "Your request is incomplete.\n"
441  "Please fill out all requested information." ) );
442 
443  return;
444  }
445 
446  // If the email address has changed, just do a normal request.
447  QStringList license = US_Settings::license();
448 
449  if ( license.size() > 8 )
450  {
451  // qDebug() << "license len > 8";
452  if ( le_email->text() != license[ 8 ] )
453  {
454  int result = QMessageBox::question( this,
455  tr( "License Change" ),
456  tr( "Your email address is changing.\n"
457  "You must validate your new address using new registration procedures.\n"
458  "Continue?" ),
459  QMessageBox::Yes, QMessageBox::No );
460 
461  if ( result == QMessageBox::No )
462  {
463  return;
464  }
465  else
466  {
467  request();
468  return;
469  }
470  }
471  }
472  else
473  {
474  request();
475  return;
476  }
477 
478  // Form request
479  QString req = "firstname=" + firstname
480  + "&lastname=" + lastname
481  + "&institution=" + institution
482  + "&address=" + address
483  + "&city=" + city
484  + "&state=" + state
485  + "&zip=" + zip
486  + "&phone=" + phone
487  + "&email=" + email
488  + "&os=" + OS
489  + "&platform=" + PLATFORM
490  + "&version=" + US_Version
491  + "&licensetype=" + licensetype;
492  //qDebug() << "requesting update";
493  // Send request
494 // QString url = "http://ultrascan.uthscsa.edu/update-license.php";
495  QString url = "http://129.111.140.167/update-license.php";
496  US_HttpPost* transmit = new US_HttpPost( url, req );
497  connect( transmit, SIGNAL( US_Http_post_response( const QString& ) ),
498  this, SLOT ( update_response ( const QString& ) ) );
499 }
500 
501 // Process response
502 void US_License::update_response( const QString& response )
503 {
504  QStringList data = response.split( "-" );
505 
506  int error = data[ 0 ].toInt();
507  if ( error == 0 )
508  {
509  validation = data[ 1 ];
510  expiration = data[ 2 ];
512 
513  if ( save() )
514  {
515  pb_update->setText( "Update / Renew" );
516  update_screen();
517  }
518 
519  // After registration, ensure basic user directories are created
520  QDir dir;
521  dir.mkpath( US_Settings::dataDir() );
522  dir.mkpath( US_Settings::resultDir() );
523  dir.mkpath( US_Settings::reportDir() );
524  dir.mkpath( US_Settings::tmpDir() );
525  }
526  else
527  // Process possible errors
528  QMessageBox::information ( this,
529  tr( "Update Error" ), response );
530 }
531 
533 {
534  //qDebug() << "Starting Req";
535  // Form request
536  QString request = "firstname=" + firstname
537  + "&lastname=" + lastname
538  + "&institution=" + institution
539  + "&address=" + address
540  + "&city=" + city
541  + "&state=" + state
542  + "&zip=" + zip
543  + "&phone=" + phone
544  + "&email=" + email
545  + "&os=" + OS
546  + "&platform=" + PLATFORM
547  + "&version=" + US_Version
548  + "&licensetype=" + licensetype;
549 
550  // Save partial request
551  QStringList license;
552  license << lastname << firstname
553  << institution << address
554  << city << state
555  << zip << phone
556  << email << platform.toLower()
557  << os.toLower() << version;
558 
559  US_Settings::set_license( license );
560 
561  // Send request
562  //qDebug() << "sending: " << request;
563  QString url = "http://ultrascan.uthscsa.edu/request-license.php";
564  US_HttpPost* transmit = new US_HttpPost( url, request );
565  connect( transmit, SIGNAL( US_Http_post_response( const QString& ) ),
566  this, SLOT ( request_response ( const QString& ) ) );
567 }
568 
569 void US_License::request_response( const QString& response )
570 {
571  QStringList data = response.split( "-" );
572  //qDebug() << data;
573  int error = data[ 0 ].toInt();
574 
575  //qDebug() << "Request response data: " << error;
576  if ( error == 0 )
577  {
578  pb_update->setText( "Finish Registration" );
579 
580  QMessageBox::information ( this,
581  tr( "Request Success" ),
582  tr( "You have been sent an email to confirm your request.\n"
583  "After you respond to the email, select 'Finish Registration'." ) );
584  }
585  else
586  {
587  // Process possible errors
588  QMessageBox::information ( this,
589  tr( "Request Error" ),
590  response );
591 
592  return;
593  }
594 }
595 
596 bool US_License::save( void )
597 {
598  QString error;
599  QString message;
600  QStringList license;
601 
602  license << lastname << firstname
603  << institution << address
604  << city << state
605  << zip << phone
606  << email << platform.toLower()
607  << os.toLower() << version
608  << validation.toUpper() << expiration;
609 
610  bool isOK = false;
611  switch ( US_License_t::isValid( error, license ) )
612  {
614  message = tr( "Failure.\n"
615  "The date of the registration is more than one year ago." );
616  break;
617 
619  message = tr( "Failure.\n"
620  "The data presented is invalid." );
621  break;
622 
624  message = tr( "Failure.\n"
625  "The wrong hardware architecture has been selected." );
626  break;
627 
628  case US_License_t::BadOS:
629  message = tr( "Failure.\n"
630  "The wrong operating system has been selected." );
631  break;
632 
633  case US_License_t::OK:
634  US_Settings::set_license( license );
635  message = tr( "Success.\n"
636  "The registration has been saved successfully." );
637  isOK = true;
638  updating_email = false;
639  break;
640 
641  default:
642  message = tr( "An unknown error occured." );
643  break;
644  }
645 
646  QMessageBox::information( this, tr( "License save results" ), message );
647  return isOK;
648 }
649 
650 QString US_License::trim( const QString& st )
651 {
652  QString s = st;
653  QRegExp blanks( "(^\\s+)|(\\s+$)" );
654 
655  s.replace( blanks, "" );
656  return s;
657 }
658 
659