QTfrontend/hwform.cpp
changeset 8291 e4a0d980d1e2
parent 8287 e2a5dece221f
child 8293 fae7a18afe03
equal deleted inserted replaced
8289:302a2521fe7a 8291:e4a0d980d1e2
  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     QString key = "net/temppasswordhash";
  1060     clearPasswordHashes(true, false);
  1061     config->setValue(key, "");
  1061 }
  1062     config->remove(key);
  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 }
       
  1075 
       
  1076 void HWForm::NetNickRegistered(const QString & nick)
       
  1077 {
       
  1078     //Get hashes
       
  1079     QString hash =  config->value("net/passwordhash", "").toString();
       
  1080     QString temphash =  config->value("net/temppasswordhash", "").toString();
       
  1081     
       
  1082     if (hash.isEmpty()) {
       
  1083       if (temphash.isEmpty()) { //If the user enters a registered nick with no password
       
  1084 	  QString suppliedpass;
       
  1085 	  while (suppliedpass.isEmpty()) {
       
  1086 	      QInputDialog nickRegedDialog(this);
       
  1087 	      nickRegedDialog.setWindowModality(Qt::WindowModal);
       
  1088 	      nickRegedDialog.setInputMode(QInputDialog::TextInput);
       
  1089 	      nickRegedDialog.setWindowTitle(tr("Hedgewars - Nick registered"));
       
  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:"));
       
  1091 	      nickRegedDialog.setTextEchoMode(QLineEdit::Password);
       
  1092 	      nickRegedDialog.exec();
       
  1093       
       
  1094 	      suppliedpass = nickRegedDialog.textValue();
       
  1095 
       
  1096 	      if (nickRegedDialog.result() == QDialog::Rejected) {
       
  1097 		clearPasswordHashes(true, true);
       
  1098 		GoBack();
       
  1099 		return;
       
  1100 	      }
       
  1101 	      temphash = QCryptographicHash::hash(suppliedpass.toUtf8(), QCryptographicHash::Md5).toHex();
       
  1102 	      config->setValue("net/temppasswordhash", temphash);	    
       
  1103 	  }
       
  1104       }
       
  1105     }    
       
  1106     NetPassword(nick);
  1063 }
  1107 }
  1064 
  1108 
  1065 void HWForm::NetNickTaken(const QString & nick)
  1109 void HWForm::NetNickTaken(const QString & nick)
  1066 {
  1110 {
  1067     bool ok = false;
  1111     bool ok = false;
  1068     QString newNick = QInputDialog::getText(this, tr("Nickname"), tr("Someone already uses your nickname %1 on the server.\nPlease pick another nickname:").arg(nick), QLineEdit::Normal, nick, &ok);
  1112     QString newNick = QInputDialog::getText(this, tr("Nickname"), tr("Someone already uses your nickname %1 on the server.\nPlease pick another nickname:").arg(nick), QLineEdit::Normal, nick, &ok);
  1069 
  1113 
  1070     if (!ok || newNick.isEmpty())
  1114     if (!ok || newNick.isEmpty())
  1071     {
  1115     {
  1072         ForcedDisconnect(tr("No nickname supplied."));
  1116         //ForcedDisconnect(tr("No nickname supplied."));
       
  1117 	int retry = RetryDialog("Hedgewars - Empty nickname", "No nickname supplied.");
       
  1118 	GoBack();
       
  1119         if (retry) {
       
  1120        	   NetConnectOfficialServer();
       
  1121         }
  1073         return;
  1122         return;
  1074     }
  1123     }
  1075 
  1124 
  1076     hwnet->NewNick(newNick);
  1125     hwnet->NewNick(newNick);
  1077     config->setValue("net/nick", newNick);
  1126     config->setValue("net/nick", newNick);
  1082 }
  1131 }
  1083 
  1132 
  1084 void HWForm::NetAuthFailed()
  1133 void HWForm::NetAuthFailed()
  1085 {
  1134 {
  1086     // Set the password blank if case the user tries to join and enter his password again
  1135     // Set the password blank if case the user tries to join and enter his password again
  1087     config->setValue("net/passwordlength", 0);
  1136     clearPasswordHashes(false, true);
  1088     config->setNetPasswordLength(0);
  1137     
       
  1138     //Try to login again
       
  1139     bool retry = RetryDialog("Hedgewars - Wrong password", "You entered a wrong password.");
       
  1140     GoBack();
       
  1141 
       
  1142     if (retry) {
       
  1143        NetConnectOfficialServer();
       
  1144     }
       
  1145 }
       
  1146 
       
  1147 bool HWForm::RetryDialog(const QString & title, const QString & label)
       
  1148 {
       
  1149     QMessageBox retryMsg(this);
       
  1150     retryMsg.setIcon(QMessageBox::Warning);
       
  1151     retryMsg.setWindowTitle(title);
       
  1152     retryMsg.setText(label);
       
  1153     retryMsg.setWindowModality(Qt::WindowModal);
       
  1154 
       
  1155     retryMsg.addButton(QMessageBox::Cancel);
       
  1156 
       
  1157     QPushButton *retryButton = retryMsg.addButton(QMessageBox::Ok);
       
  1158     retryButton->setText("Try Again");
       
  1159     retryButton->setFocus();
       
  1160 
       
  1161     retryMsg.exec();
       
  1162 
       
  1163     if (retryMsg.clickedButton() == retryButton) {
       
  1164        return true;
       
  1165     }
       
  1166     return false;
  1089 }
  1167 }
  1090 
  1168 
  1091 void HWForm::NetTeamAccepted(const QString & team)
  1169 void HWForm::NetTeamAccepted(const QString & team)
  1092 {
  1170 {
  1093     ui.pageNetGame->pNetTeamsWidget->changeTeamStatus(team);
  1171     ui.pageNetGame->pNetTeamsWidget->changeTeamStatus(team);
  1136     connect(hwnet, SIGNAL(EnteredGame()), this, SLOT(NetGameEnter()), Qt::QueuedConnection);
  1214     connect(hwnet, SIGNAL(EnteredGame()), this, SLOT(NetGameEnter()), Qt::QueuedConnection);
  1137     connect(hwnet, SIGNAL(LeftRoom(const QString&)), this, SLOT(NetLeftRoom(const QString&)), Qt::QueuedConnection);
  1215     connect(hwnet, SIGNAL(LeftRoom(const QString&)), this, SLOT(NetLeftRoom(const QString&)), Qt::QueuedConnection);
  1138     connect(hwnet, SIGNAL(AddNetTeam(const HWTeam&)), this, SLOT(AddNetTeam(const HWTeam&)), Qt::QueuedConnection);
  1216     connect(hwnet, SIGNAL(AddNetTeam(const HWTeam&)), this, SLOT(AddNetTeam(const HWTeam&)), Qt::QueuedConnection);
  1139     connect(hwnet, SIGNAL(RemoveNetTeam(const HWTeam&)), this, SLOT(RemoveNetTeam(const HWTeam&)), Qt::QueuedConnection);
  1217     connect(hwnet, SIGNAL(RemoveNetTeam(const HWTeam&)), this, SLOT(RemoveNetTeam(const HWTeam&)), Qt::QueuedConnection);
  1140     connect(hwnet, SIGNAL(TeamAccepted(const QString&)), this, SLOT(NetTeamAccepted(const QString&)), Qt::QueuedConnection);
  1218     connect(hwnet, SIGNAL(TeamAccepted(const QString&)), this, SLOT(NetTeamAccepted(const QString&)), Qt::QueuedConnection);
  1141     connect(hwnet, SIGNAL(AskForPassword(const QString&)), this, SLOT(NetPassword(const QString&)), Qt::QueuedConnection);
  1219     connect(hwnet, SIGNAL(NickRegistered(const QString&)), this, SLOT(NetNickRegistered(const QString&)), Qt::QueuedConnection);
  1142     connect(hwnet, SIGNAL(NickTaken(const QString&)), this, SLOT(NetNickTaken(const QString&)), Qt::QueuedConnection);
  1220     connect(hwnet, SIGNAL(NickTaken(const QString&)), this, SLOT(NetNickTaken(const QString&)), Qt::QueuedConnection);
  1143     connect(hwnet, SIGNAL(AuthFailed()), this, SLOT(NetAuthFailed()), Qt::QueuedConnection);
  1221     connect(hwnet, SIGNAL(AuthFailed()), this, SLOT(NetAuthFailed()), Qt::QueuedConnection);
  1144     //connect(ui.pageNetGame->BtnBack, SIGNAL(clicked()), hwnet, SLOT(partRoom()));
  1222     //connect(ui.pageNetGame->BtnBack, SIGNAL(clicked()), hwnet, SLOT(partRoom()));
  1145 
  1223 
  1146     ui.pageRoomsList->chatWidget->setUsersModel(hwnet->lobbyPlayersModel());
  1224     ui.pageRoomsList->chatWidget->setUsersModel(hwnet->lobbyPlayersModel());
  1260     connect(hwnet, SIGNAL(configAsked()), ui.pageNetGame->pGameCFG, SLOT(fullNetConfig()));
  1338     connect(hwnet, SIGNAL(configAsked()), ui.pageNetGame->pGameCFG, SLOT(fullNetConfig()));
  1261    
  1339    
  1262 //nick and pass stuff
  1340 //nick and pass stuff
  1263     
  1341     
  1264     //remove temppasswordhash just in case
  1342     //remove temppasswordhash just in case
  1265     config->value("net/temppasswordhash", "");
  1343     clearPasswordHashes(true, false);
  1266     config->remove("net/temppasswordhash");
       
  1267     
  1344     
  1268     //initialize
  1345     //initialize
  1269     QString hash = config->value("net/passwordhash", "").toString();
  1346     QString hash = config->value("net/passwordhash", "").toString();
  1270     QString temphash = config->value("net/temppasswordhash", "").toString();
  1347     QString temphash = config->value("net/temppasswordhash", "").toString();
  1271     QString nickname = config->value("net/nick", "").toString();
  1348     QString nickname = config->value("net/nick", "").toString();
  1286 	}
  1363 	}
  1287 
  1364 
  1288 	//if dialog close, create an error message
  1365 	//if dialog close, create an error message
  1289         if (hpd->exec() != QDialog::Accepted)
  1366         if (hpd->exec() != QDialog::Accepted)
  1290         {
  1367         {
  1291             ForcedDisconnect(tr("Login info not supplied."));
  1368             /*//ForcedDisconnect(tr("Login info not supplied."));
  1292             delete hpd;
  1369             int retry = RetryDialog("Hedgewars - Empty login info", "Login info not supplied.");
       
  1370 	    GoBack();
       
  1371 	    delete hpd;
       
  1372     	    if (retry) {
       
  1373 	    	NetConnectOfficialServer();
       
  1374 	    }
       
  1375 	    //delete hpd;*/
       
  1376 	    delete hpd;
       
  1377 	    GoBack();
  1293             return;
  1378             return;
  1294         }
  1379         }
  1295 
  1380 
  1296 	//set nick and pass from the dialog
  1381 	//set nick and pass from the dialog
  1297 	nickname = hpd->leNickname->text();
  1382 	nickname = hpd->leNickname->text();
  1298         password = hpd->lePassword->text();
  1383         password = hpd->lePassword->text();
  1299 
  1384 
  1300 	//calculate temphash and set it into config
  1385 	//check the nickname variable
  1301 	temphash = QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Md5).toHex();
  1386 	if (nickname.isEmpty()) {
  1302 	config->setValue("net/temppasswordhash", temphash);
  1387 	    //ForcedDisconnect(tr("No nickname supplied."));
  1303 
  1388 	    int retry = RetryDialog("Hedgewars - Empty nickname", "No nickname supplied.");
  1304 	//if user wants to save password
  1389 	    GoBack();
  1305         bool save = hpd->cbSave->isChecked();
  1390             delete hpd;
  1306         config->setValue("net/savepassword", save);
  1391             if (retry) {
  1307         if (save) // user wants to save password
  1392                 NetConnectOfficialServer();
  1308         {
  1393             }
  1309             config->setValue("net/passwordhash", temphash);
  1394 	    //delete hpd;
  1310             config->setValue("net/passwordlength", password.size());
  1395 	    return;
  1311             config->setNetPasswordLength(password.size());
  1396 	}
  1312         }
  1397 	
       
  1398 	if (!password.isEmpty()) {
       
  1399 	  //calculate temphash and set it into config
       
  1400 	  temphash = QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Md5).toHex();
       
  1401 	  config->setValue("net/temppasswordhash", temphash);
       
  1402 	  
       
  1403 	  //if user wants to save password
       
  1404 	  bool save = hpd->cbSave->isChecked();
       
  1405 	  config->setValue("net/savepassword", save);
       
  1406 	  if (save) // user wants to save password
       
  1407 	  {
       
  1408 	    config->setValue("net/passwordhash", temphash);
       
  1409 	    config->setValue("net/passwordlength", password.size());
       
  1410 	    config->setNetPasswordLength(password.size());
       
  1411 	  }
       
  1412 	}
       
  1413 	else {
       
  1414 	  config->setValue("net/temppasswordhash", "EMPTY");
       
  1415 	}
  1313 
  1416 
  1314         delete hpd;
  1417         delete hpd;
  1315 
       
  1316 
  1418 
  1317 	//update nickname
  1419 	//update nickname
  1318         config->setValue("net/nick", nickname);
  1420         config->setValue("net/nick", nickname);
  1319         config->updNetNick();
  1421         config->updNetNick();
  1320 	
  1422 	
  1325     }
  1427     }
  1326     
  1428     
  1327     
  1429     
  1328 	//if pass is none (hash is generated anyway), remove the hash
  1430 	//if pass is none (hash is generated anyway), remove the hash
  1329 	if (password.size() <= 0) {
  1431 	if (password.size() <= 0) {
  1330 	    config->setValue("net/temppasswordhash", "");
  1432 	    clearPasswordHashes(true, false);
  1331 	    config->remove("net/temppasswordhash");
       
  1332 	}
  1433 	}
  1333     }
  1434     }
  1334 
  1435 
  1335     ui.pageRoomsList->setUser(nickname);
  1436     ui.pageRoomsList->setUser(nickname);
  1336     ui.pageNetGame->setUser(nickname);
  1437     ui.pageNetGame->setUser(nickname);