QTfrontend/hwform.cpp
changeset 8181 70e980278080
parent 8179 a1ffcb559f99
child 8183 9b1c206a6cf2
equal deleted inserted replaced
8180:33868aea694c 8181:70e980278080
   957     NetConnectServer("netserver.hedgewars.org", 46631);
   957     NetConnectServer("netserver.hedgewars.org", 46631);
   958 }
   958 }
   959 
   959 
   960 void HWForm::NetPassword(const QString & nick)
   960 void HWForm::NetPassword(const QString & nick)
   961 {
   961 {
   962     int passLength = config->value("net/passwordlength", 0).toInt();
   962     //Get hashes
   963     QString hash = QString::fromLatin1(config->value("net/passwordhash", "").toByteArray());
   963     QString hash =  config->value("net/passwordhash", "").toString();
   964 
   964     QString temphash =  config->value("net/temppasswordhash", "").toString();
   965     // If the password is blank, ask the user to enter one in
   965 
   966     if (passLength == 0)
   966     //Check them
   967     {
   967 
   968         HWPasswordDialog hpd(this, tr("Your nickname %1 is\nregistered on Hedgewars.org\nPlease provide your password below\nor pick another nickname in game config:").arg(nick));
   968     if (temphash.isEmpty() && hash.isEmpty()) { //If the user enters a registered nick with no password, sends a bogus hash
   969         hpd.cbSave->setChecked(config->value("net/savepassword", true).toBool());
   969         hwnet->SendPasswordHash("THISISNOHASH");
   970         if (hpd.exec() != QDialog::Accepted)
   970     }
   971         {
   971     else if (temphash.isEmpty()) { //Send saved hash as default
   972             ForcedDisconnect(tr("No password supplied."));
   972         hwnet->SendPasswordHash(hash);
   973             return;
   973     }
   974         }
   974     else { //Send the hash
   975 
   975         hwnet->SendPasswordHash(temphash);
   976         QString password = hpd.lePassword->text();
   976     }
   977         hash = QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Md5).toHex();
   977 
   978 
   978     //Remove temporary hash from config
   979         bool save = hpd.cbSave->isChecked();
   979     QString key = "net/temppasswordhash";
   980         config->setValue("net/savepassword", save);
   980     config->setValue(key, "");
   981         if (save) // user wants to save password
   981     config->remove(key);
   982         {
       
   983             config->setValue("net/passwordhash", hash.toLatin1());
       
   984             config->setValue("net/passwordlength", password.size());
       
   985             config->setNetPasswordLength(password.size());
       
   986             config->sync();
       
   987         }
       
   988     }
       
   989 
       
   990     hwnet->SendPasswordHash(hash);
       
   991 }
   982 }
   992 
   983 
   993 void HWForm::NetNickTaken(const QString & nick)
   984 void HWForm::NetNickTaken(const QString & nick)
   994 {
   985 {
   995     bool ok = false;
   986     bool ok = false;
  1184 
  1175 
  1185 // config stuff
  1176 // config stuff
  1186     connect(hwnet, SIGNAL(paramChanged(const QString &, const QStringList &)), ui.pageNetGame->pGameCFG, SLOT(setParam(const QString &, const QStringList &)));
  1177     connect(hwnet, SIGNAL(paramChanged(const QString &, const QStringList &)), ui.pageNetGame->pGameCFG, SLOT(setParam(const QString &, const QStringList &)));
  1187     connect(ui.pageNetGame->pGameCFG, SIGNAL(paramChanged(const QString &, const QStringList &)), hwnet, SLOT(onParamChanged(const QString &, const QStringList &)));
  1178     connect(ui.pageNetGame->pGameCFG, SIGNAL(paramChanged(const QString &, const QStringList &)), hwnet, SLOT(onParamChanged(const QString &, const QStringList &)));
  1188     connect(hwnet, SIGNAL(configAsked()), ui.pageNetGame->pGameCFG, SLOT(fullNetConfig()));
  1179     connect(hwnet, SIGNAL(configAsked()), ui.pageNetGame->pGameCFG, SLOT(fullNetConfig()));
  1189 
  1180    
  1190     while (nick.isEmpty())
  1181 //nick and pass stuff
  1191     {
  1182     
  1192         nick = QInputDialog::getText(this,
  1183     //remove temppasswordhash just in case
  1193                                      QObject::tr("Nickname"),
  1184     config->value("net/temppasswordhash", "");
  1194                                      QObject::tr("Please enter your nickname"),
  1185     config->remove("net/temppasswordhash");
  1195                                      QLineEdit::Normal,
  1186     
  1196                                      QDir::home().dirName());
  1187     //initialize
  1197         config->setValue("net/nick",nick);
  1188     QString hash = config->value("net/passwordhash", "").toString();
       
  1189     QString temphash = config->value("net/temppasswordhash", "").toString();
       
  1190     QString nickname = config->value("net/nick", "").toString();
       
  1191     QString password;
       
  1192     
       
  1193     if (nickname.isEmpty() || hash.isEmpty()) { //if something from login is missing, start dialog loop
       
  1194     
       
  1195     while (nickname.isEmpty() || (hash.isEmpty() && temphash.isEmpty())) //while a nickname, or both hashes are missing
       
  1196     {
       
  1197 	//open dialog
       
  1198         HWPasswordDialog * hpd = new HWPasswordDialog(this);
       
  1199         hpd->cbSave->setChecked(config->value("net/savepassword", true).toBool());
       
  1200 
       
  1201 	//if nickname is present, put it into the field
       
  1202 	if (!nickname.isEmpty()) {
       
  1203 	    hpd->leNickname->setText(nickname);
       
  1204 	    hpd->lePassword->setFocus();
       
  1205 	}
       
  1206 
       
  1207 	//if dialog close, create an error message
       
  1208         if (hpd->exec() != QDialog::Accepted)
       
  1209         {
       
  1210             ForcedDisconnect(tr("Login info not supplied."));
       
  1211             delete hpd;
       
  1212             return;
       
  1213         }
       
  1214 
       
  1215 	//set nick and pass from the dialog
       
  1216 	nickname = hpd->leNickname->text();
       
  1217         password = hpd->lePassword->text();
       
  1218 
       
  1219 	//calculate temphash and set it into config
       
  1220 	temphash = QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Md5).toHex();
       
  1221 	config->setValue("net/temppasswordhash", temphash);
       
  1222 
       
  1223 	//if user wants to save password
       
  1224         bool save = hpd->cbSave->isChecked();
       
  1225         config->setValue("net/savepassword", save);
       
  1226         if (save) // user wants to save password
       
  1227         {
       
  1228             config->setValue("net/passwordhash", temphash);
       
  1229             config->setValue("net/passwordlength", password.size());
       
  1230             config->setNetPasswordLength(password.size());
       
  1231         }
       
  1232 
       
  1233         delete hpd;
       
  1234 
       
  1235 
       
  1236 	//update nickname
       
  1237         config->setValue("net/nick", nickname);
  1198         config->updNetNick();
  1238         config->updNetNick();
  1199     }
  1239 	
  1200 
  1240 	//and all the variables
  1201     ui.pageRoomsList->setUser(nick);
  1241 	hash = config->value("net/passwordhash", "").toString();
  1202     ui.pageNetGame->setUser(nick);
  1242 	temphash = config->value("net/temppasswordhash", "").toString();
  1203 
  1243 	nickname = config->value("net/nick", "").toString();
  1204     hwnet->Connect(hostName, port, nick);
  1244     }
  1205 }
  1245     
       
  1246     
       
  1247 	//if pass is none (hash is generated anyway), remove the hash
       
  1248 	if (password.size() <= 0) {
       
  1249 	    config->setValue("net/temppasswordhash", "");
       
  1250 	    config->remove("net/temppasswordhash");
       
  1251 	}
       
  1252     }
       
  1253 
       
  1254     ui.pageRoomsList->setUser(nickname);
       
  1255     ui.pageNetGame->setUser(nickname);
       
  1256 
       
  1257     hwnet->Connect(hostName, port, nickname);
       
  1258 }
       
  1259 
       
  1260 
  1206 
  1261 
  1207 void HWForm::NetConnect()
  1262 void HWForm::NetConnect()
  1208 {
  1263 {
  1209     HWHostPortDialog * hpd = new HWHostPortDialog(this);
  1264     HWHostPortDialog * hpd = new HWHostPortDialog(this);
  1210     hpd->leHost->setText(*netHost);
  1265     hpd->leHost->setText(*netHost);