QTfrontend/hwform.cpp
changeset 8294 0e5782b0dd54
parent 8293 fae7a18afe03
child 8295 6ef114ea2298
equal deleted inserted replaced
8293:fae7a18afe03 8294:0e5782b0dd54
  1039 }
  1039 }
  1040 
  1040 
  1041 void HWForm::NetPassword(const QString & nick)
  1041 void HWForm::NetPassword(const QString & nick)
  1042 {
  1042 {
  1043     //Get hashes
  1043     //Get hashes
  1044     QString hash =  config->value("net/passwordhash", "").toString();
  1044     QString hash =  config->passwordHash();
  1045     QString temphash =  config->value("net/temppasswordhash", "").toString();
  1045     QString temphash =  config->tempHash();
  1046 
  1046 
  1047     //Check them
  1047     //Check them
  1048 
  1048 
  1049     if (temphash.isEmpty() && hash.isEmpty()) { //If the user enters a registered nick with no password, sends a bogus hash
  1049     if (temphash.isEmpty() && hash.isEmpty()) { //If the user enters a registered nick with no password, sends a bogus hash
  1050         hwnet->SendPasswordHash("THISISNOHASH");
  1050         hwnet->SendPasswordHash("THISISNOHASH");
  1055     else { //Send the hash
  1055     else { //Send the hash
  1056         hwnet->SendPasswordHash(temphash);
  1056         hwnet->SendPasswordHash(temphash);
  1057     }
  1057     }
  1058 
  1058 
  1059     //Remove temporary hash from config
  1059     //Remove temporary hash from config
  1060     clearPasswordHashes(true, false);
  1060     config->clearPasswordHash();
  1061 }
       
  1062 
       
  1063 inline void HWForm::clearPasswordHashes(bool clearTempHash, bool clearHash) 
       
  1064 {
       
  1065     if (clearTempHash) {
       
  1066       QString key = "net/temppasswordhash";
       
  1067       config->setValue(key, "");
       
  1068       config->remove(key);
       
  1069     }
       
  1070     if (clearHash) {
       
  1071       config->setValue("net/passwordhash", "");
       
  1072       config->setValue("net/passwordlength", 0);
       
  1073     }
       
  1074 }
  1061 }
  1075 
  1062 
  1076 void HWForm::NetNickRegistered(const QString & nick)
  1063 void HWForm::NetNickRegistered(const QString & nick)
  1077 {
  1064 {
  1078     //Get hashes
  1065     //Get hashes
  1079     QString hash =  config->value("net/passwordhash", "").toString();
  1066     QString hash =  config->passwordHash();
  1080     QString temphash =  config->value("net/temppasswordhash", "").toString();
  1067     QString temphash =  config->tempHash();
  1081     
  1068     qDebug("NETNICKREGISTERED");
  1082     if (hash.isEmpty()) {
  1069     if (hash.isEmpty()) {
  1083       if (temphash.isEmpty()) { //If the user enters a registered nick with no password
  1070         qDebug("NNR - HASH EMPTY");
  1084 	  QString suppliedpass;
  1071         if (temphash.isEmpty()) { //If the user enters a registered nick with no password
  1085 	  while (suppliedpass.isEmpty()) {
  1072             qDebug("NNR - TEMPHASH EMTPY");
  1086 	      QInputDialog nickRegedDialog(this);
  1073             QString suppliedpass;
  1087 	      nickRegedDialog.setWindowModality(Qt::WindowModal);
  1074             while (suppliedpass.isEmpty()) {
  1088 	      nickRegedDialog.setInputMode(QInputDialog::TextInput);
  1075                 QInputDialog nickRegedDialog(this);
  1089 	      nickRegedDialog.setWindowTitle(tr("Hedgewars - Nick registered"));
  1076                 nickRegedDialog.setWindowModality(Qt::WindowModal);
  1090 	      nickRegedDialog.setLabelText(tr("This nick is registered, and you haven't specified a password.\n\nIf this nick isn't yours, please register your own nick at www.hedgewars.org\n\nPassword:"));
  1077                 nickRegedDialog.setInputMode(QInputDialog::TextInput);
  1091 	      nickRegedDialog.setTextEchoMode(QLineEdit::Password);
  1078                 nickRegedDialog.setWindowTitle(tr("Hedgewars - Nick registered"));
  1092 	      nickRegedDialog.exec();
  1079                 nickRegedDialog.setLabelText(tr("This nick is registered, and you haven't specified a password.\n\nIf this nick isn't yours, please register your own nick at www.hedgewars.org\n\nPassword:"));
  1093       
  1080                 nickRegedDialog.setTextEchoMode(QLineEdit::Password);
  1094 	      suppliedpass = nickRegedDialog.textValue();
  1081                 nickRegedDialog.exec();
  1095 
  1082 
  1096 	      if (nickRegedDialog.result() == QDialog::Rejected) {
  1083                 suppliedpass = nickRegedDialog.textValue();
  1097 		clearPasswordHashes(true, true);
  1084 
  1098 		GoBack();
  1085                 if (nickRegedDialog.result() == QDialog::Rejected) {
  1099 		return;
  1086                     config->clearPasswordHash();
  1100 	      }
  1087                     config->clearTempHash();
  1101 	      temphash = QCryptographicHash::hash(suppliedpass.toUtf8(), QCryptographicHash::Md5).toHex();
  1088                     GoBack();
  1102 	      config->setValue("net/temppasswordhash", temphash);	    
  1089                     return;
  1103 	  }
  1090                 }
  1104       }
  1091                 temphash = QCryptographicHash::hash(suppliedpass.toUtf8(), QCryptographicHash::Md5).toHex();
  1105     }    
  1092                 config->setTempHash(temphash);
       
  1093             }
       
  1094         }
       
  1095     }
  1106     NetPassword(nick);
  1096     NetPassword(nick);
  1107 }
  1097 }
  1108 
  1098 
  1109 void HWForm::NetNickTaken(const QString & nick)
  1099 void HWForm::NetNickTaken(const QString & nick)
  1110 {
  1100 {
  1131 }
  1121 }
  1132 
  1122 
  1133 void HWForm::NetAuthFailed()
  1123 void HWForm::NetAuthFailed()
  1134 {
  1124 {
  1135     // Set the password blank if case the user tries to join and enter his password again
  1125     // Set the password blank if case the user tries to join and enter his password again
  1136     clearPasswordHashes(false, true);
  1126     config->clearTempHash();
  1137     
  1127 
  1138     //Try to login again
  1128     //Try to login again
  1139     bool retry = RetryDialog("Hedgewars - Wrong password", "You entered a wrong password.");
  1129     bool retry = RetryDialog("Hedgewars - Wrong password", "You entered a wrong password.");
  1140     GoBack();
  1130     GoBack();
  1141 
  1131 
  1142     if (retry) {
  1132     if (retry) {
  1334 
  1324 
  1335 // config stuff
  1325 // config stuff
  1336     connect(hwnet, SIGNAL(paramChanged(const QString &, const QStringList &)), ui.pageNetGame->pGameCFG, SLOT(setParam(const QString &, const QStringList &)));
  1326     connect(hwnet, SIGNAL(paramChanged(const QString &, const QStringList &)), ui.pageNetGame->pGameCFG, SLOT(setParam(const QString &, const QStringList &)));
  1337     connect(ui.pageNetGame->pGameCFG, SIGNAL(paramChanged(const QString &, const QStringList &)), hwnet, SLOT(onParamChanged(const QString &, const QStringList &)));
  1327     connect(ui.pageNetGame->pGameCFG, SIGNAL(paramChanged(const QString &, const QStringList &)), hwnet, SLOT(onParamChanged(const QString &, const QStringList &)));
  1338     connect(hwnet, SIGNAL(configAsked()), ui.pageNetGame->pGameCFG, SLOT(fullNetConfig()));
  1328     connect(hwnet, SIGNAL(configAsked()), ui.pageNetGame->pGameCFG, SLOT(fullNetConfig()));
  1339    
  1329 
  1340 //nick and pass stuff
  1330 //nick and pass stuff
  1341     
  1331 
  1342     //remove temppasswordhash just in case
  1332     //remove temppasswordhash just in case
  1343     clearPasswordHashes(true, false);
  1333     config->clearTempHash();
  1344     
  1334 
  1345     //initialize
  1335     //initialize
  1346     QString hash = config->value("net/passwordhash", "").toString();
  1336     QString hash = config->passwordHash();
  1347     QString temphash = config->value("net/temppasswordhash", "").toString();
  1337     QString temphash = config->tempHash();
  1348     QString nickname = config->value("net/nick", "").toString();
  1338     QString nickname = config->value("net/nick", "").toString();
  1349     QString password;
  1339     QString password;
  1350     
  1340 
  1351     if (nickname.isEmpty() || hash.isEmpty()) { //if something from login is missing, start dialog loop
  1341     if (nickname.isEmpty() || hash.isEmpty()) { //if something from login is missing, start dialog loop
  1352     
  1342 
  1353     while (nickname.isEmpty() || (hash.isEmpty() && temphash.isEmpty())) //while a nickname, or both hashes are missing
  1343         while (nickname.isEmpty() || (hash.isEmpty() && temphash.isEmpty())) //while a nickname, or both hashes are missing
  1354     {
  1344         {
  1355 	//open dialog
  1345         //open dialog
  1356         HWPasswordDialog * hpd = new HWPasswordDialog(this);
  1346             HWPasswordDialog * pwDialog = new HWPasswordDialog(this);
  1357         hpd->cbSave->setChecked(config->value("net/savepassword", true).toBool());
  1347             pwDialog->cbSave->setChecked(config->value("net/savepassword", true).toBool());
  1358 
  1348 
  1359 	//if nickname is present, put it into the field
  1349         //if nickname is present, put it into the field
  1360 	if (!nickname.isEmpty()) {
  1350         if (!nickname.isEmpty()) {
  1361 	    hpd->leNickname->setText(nickname);
  1351             pwDialog->leNickname->setText(nickname);
  1362 	    hpd->lePassword->setFocus();
  1352             pwDialog->lePassword->setFocus();
  1363 	}
  1353         }
  1364 
  1354 
  1365 	//if dialog close, create an error message
  1355         //if dialog close, create an error message
  1366         if (hpd->exec() != QDialog::Accepted)
  1356         if (pwDialog->exec() != QDialog::Accepted) {
  1367         {
  1357             delete pwDialog;
  1368             delete hpd;
  1358             GoBack();
  1369 	    GoBack();
       
  1370             return;
  1359             return;
  1371         }
  1360         }
  1372 
  1361 
  1373 	//set nick and pass from the dialog
  1362         //set nick and pass from the dialog
  1374 	nickname = hpd->leNickname->text();
  1363         nickname = pwDialog->leNickname->text();
  1375         password = hpd->lePassword->text();
  1364         password = pwDialog->lePassword->text();
  1376 
  1365 
  1377 	//check the nickname variable
  1366         //check the nickname variable
  1378 	if (nickname.isEmpty()) {
  1367         if (nickname.isEmpty()) {
  1379 	    //ForcedDisconnect(tr("No nickname supplied."));
  1368             int retry = RetryDialog("Hedgewars - Empty nickname", "No nickname supplied.");
  1380 	    int retry = RetryDialog("Hedgewars - Empty nickname", "No nickname supplied.");
  1369             GoBack();
  1381 	    GoBack();
  1370             delete pwDialog;
  1382             delete hpd;
       
  1383             if (retry) {
  1371             if (retry) {
  1384                 NetConnectOfficialServer();
  1372                 NetConnectOfficialServer();
  1385             }
  1373             }
  1386 	    //delete hpd;
  1374             return;
  1387 	    return;
  1375         }
  1388 	}
  1376 
  1389 	
  1377         if (!password.isEmpty()) {
  1390 	if (!password.isEmpty()) {
  1378             //calculate temphash and set it into config
  1391 	  //calculate temphash and set it into config
  1379             temphash = QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Md5).toHex();
  1392 	  temphash = QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Md5).toHex();
  1380             config->setTempHash(temphash);
  1393 	  config->setValue("net/temppasswordhash", temphash);
  1381 
  1394 	  
  1382             //if user wants to save password
  1395 	  //if user wants to save password
  1383             bool save = pwDialog->cbSave->isChecked();
  1396 	  bool save = hpd->cbSave->isChecked();
  1384             config->setValue("net/savepassword", save);
  1397 	  config->setValue("net/savepassword", save);
  1385             if (save) // user wants to save password
  1398 	  if (save) // user wants to save password
  1386             {
  1399 	  {
  1387                 config->setPasswordHash(temphash);
  1400 	    config->setValue("net/passwordhash", temphash);
  1388                 config->setNetPasswordLength(password.size());
  1401 	    config->setValue("net/passwordlength", password.size());
  1389             }
  1402 	    config->setNetPasswordLength(password.size());
  1390         }
  1403 	  }
  1391         else {
  1404 	}
  1392             delete pwDialog;
  1405 	else {
  1393             config->setValue("net/nick", nickname);
  1406 	  config->setValue("net/temppasswordhash", "EMPTY");
  1394             config->updNetNick();
  1407 	}
  1395             config->clearPasswordHash();
  1408 
  1396             break;
  1409         delete hpd;
  1397         }
  1410 
  1398 
  1411 	//update nickname
  1399         delete pwDialog;
       
  1400 
       
  1401         //update nickname
  1412         config->setValue("net/nick", nickname);
  1402         config->setValue("net/nick", nickname);
  1413         config->updNetNick();
  1403         config->updNetNick();
  1414 	
  1404 
  1415 	//and all the variables
  1405         //and all the variables
  1416 	hash = config->value("net/passwordhash", "").toString();
  1406         hash = config->passwordHash();
  1417 	temphash = config->value("net/temppasswordhash", "").toString();
  1407         temphash = config->tempHash();
  1418 	nickname = config->value("net/nick", "").toString();
  1408         nickname = config->value("net/nick", "").toString();
  1419     }
  1409         }
  1420     
       
  1421     
       
  1422 	//if pass is none (hash is generated anyway), remove the hash
       
  1423 	if (password.size() <= 0) {
       
  1424 	    clearPasswordHashes(true, false);
       
  1425 	}
       
  1426     }
  1410     }
  1427 
  1411 
  1428     ui.pageRoomsList->setUser(nickname);
  1412     ui.pageRoomsList->setUser(nickname);
  1429     ui.pageNetGame->setUser(nickname);
  1413     ui.pageNetGame->setUser(nickname);
  1430 
  1414