Attempt to fix issue 125. The password pop-up doesn't appear every time when going into the official server anymore, now it only does it when the password is blank. If a user enters an invalid password, the password is set blank to avoid the user going back to the official server just to be rejected. When entering an invalid password, the unknown error dialog doesn't show up anymore, but the connection lost to server one still does. This fixes the bug where the user would be spammed with error messages. The user can also now change his password in the settings page.
authorZorg <zorgiepoo@gmail.com>
Wed, 08 Jun 2011 03:36:54 -0400
changeset 5229 148d581b17ab
parent 5228 b018f6117fc1
child 5230 c088be28d5e8
Attempt to fix issue #125. The password pop-up doesn't appear every time when going into the official server anymore, now it only does it when the password is blank. If a user enters an invalid password, the password is set blank to avoid the user going back to the official server just to be rejected. When entering an invalid password, the unknown error dialog doesn't show up anymore, but the connection lost to server one still does. This fixes the bug where the user would be spammed with error messages. The user can also now change his password in the settings page.
QTfrontend/gameuiconfig.cpp
QTfrontend/gameuiconfig.h
QTfrontend/newnetclient.cpp
QTfrontend/pageoptions.cpp
QTfrontend/pageoptions.h
--- a/QTfrontend/gameuiconfig.cpp	Sat Jun 04 16:09:12 2011 -0400
+++ b/QTfrontend/gameuiconfig.cpp	Wed Jun 08 03:36:54 2011 -0400
@@ -22,6 +22,7 @@
 #include <QDesktopWidget>
 #include <QApplication>
 #include <QInputDialog>
+#include <QCryptographicHash>
 
 #include "gameuiconfig.h"
 #include "hwform.h"
@@ -66,6 +67,9 @@
                 QDir::home().dirName());
 
     Form->ui.pageOptions->editNetNick->setText(netNick);
+    
+    int passLength = value("net/passwordlength", 0).toInt();
+    setNetPasswordLength(passLength);
 
     delete netHost;
     netHost = new QString(value("net/ip", "").toString());
@@ -138,6 +142,11 @@
     setValue("audio/volume", Form->ui.pageOptions->volumeBox->value());
 
     setValue("net/nick", netNick());
+    if (netPasswordIsValid())
+    {
+        setValue("net/passwordhash", netPasswordHash());
+        setValue("net/passwordlength", netPasswordLength());
+    }
     setValue("net/ip", *netHost);
     setValue("net/port", netPort);
     setValue("net/servername", Form->ui.pageNetServer->leServerDescr->text());
@@ -145,7 +154,7 @@
 
     setValue("fps/show", isShowFPSEnabled());
     setValue("fps/limit", Form->ui.pageOptions->fpsedit->value());
-
+    
     setValue("misc/altdamage", isAltDamageEnabled());
     setValue("misc/appendTimeToRecords", appendDateTimeToRecordName());
     setValue("misc/locale", language());
@@ -296,6 +305,33 @@
     return Form->ui.pageOptions->editNetNick->text();
 }
 
+QByteArray GameUIConfig::netPasswordHash()
+{
+    return QCryptographicHash::hash(Form->ui.pageOptions->editNetPassword->text().toLatin1(), QCryptographicHash::Md5).toHex();
+}
+
+int GameUIConfig::netPasswordLength()
+{
+    return Form->ui.pageOptions->editNetPassword->text().size();
+}
+
+bool GameUIConfig::netPasswordIsValid()
+{
+    return (netPasswordLength() == 0 || Form->ui.pageOptions->editNetPassword->text() != QString(netPasswordLength(), '\0'));
+}
+
+void GameUIConfig::setNetPasswordLength(int passwordLength)
+{
+    if (passwordLength > 0)
+    {
+        Form->ui.pageOptions->editNetPassword->setText(QString(passwordLength, '\0'));
+    }
+    else
+    {
+        Form->ui.pageOptions->editNetPassword->setText("");
+    }
+}
+
 quint8 GameUIConfig::volume()
 {
     return Form->ui.pageOptions->volumeBox->value() * 128 / 100;
--- a/QTfrontend/gameuiconfig.h	Sat Jun 04 16:09:12 2011 -0400
+++ b/QTfrontend/gameuiconfig.h	Wed Jun 08 03:36:54 2011 -0400
@@ -49,6 +49,9 @@
     quint8 timerInterval();
     quint8 bitDepth();
     QString netNick();
+    QByteArray netPasswordHash();
+    int netPasswordLength();
+    void setNetPasswordLength(int passwordLength);
     bool isReducedQuality() const;
     bool isFrontendEffects() const;
     bool isFrontendFullscreen() const;
@@ -68,6 +71,7 @@
     void SaveOptions();
 
 private:
+    bool netPasswordIsValid();
     quint8 depth;
 };
 
--- a/QTfrontend/newnetclient.cpp	Sat Jun 04 16:09:12 2011 -0400
+++ b/QTfrontend/newnetclient.cpp	Wed Jun 08 03:36:54 2011 -0400
@@ -486,23 +486,38 @@
         emit AskForRunGame();
         return;
     }
+    
+    if (lst[0] == "BYE") {
+        if (lst[1] == "Authentication failed")
+        {
+            // Set the password blank if case the user tries to join and enter his password again
+            config->setValue("net/passwordlength", 0);
+            config->setNetPasswordLength(0);
+        }
+        // return early so the user won't get an unknown error message dialog (the user already gets a server connection is lost one)
+        return;
+    }
 
     if (lst[0] == "ASKPASSWORD") {
         bool ok = false;
         int passLength = config->value("net/passwordlength", 0).toInt();
         QString hash = config->value("net/passwordhash", "").toString();
-        QString password = QInputDialog::getText(0, tr("Password"), tr("Your nickname %1 is\nregistered on Hedgewars.org\nPlease provide your password below\nor pick another nickname in game config:").arg(mynick), QLineEdit::Password, passLength==0?NULL:QString(passLength,'\0'), &ok);
+        
+        // If the password is blank, ask the user to enter one in
+        if (passLength == 0)
+        {
+            QString password = QInputDialog::getText(0, tr("Password"), tr("Your nickname %1 is\nregistered on Hedgewars.org\nPlease provide your password below\nor pick another nickname in game config:").arg(mynick), QLineEdit::Password, passLength==0?NULL:QString(passLength,'\0'), &ok);
 
-        if (!ok) {
-            Disconnect();
-            emit Disconnected();
-            return;
-        }
-
-        if (!passLength || password!=QString(passLength, '\0')) {
+            if (!ok) {
+                Disconnect();
+                emit Disconnected();
+                return;
+            }
+            
             hash = QCryptographicHash::hash(password.toLatin1(), QCryptographicHash::Md5).toHex();
             config->setValue("net/passwordhash", hash);
             config->setValue("net/passwordlength", password.size());
+            config->setNetPasswordLength(password.size());
         }
 
         RawSendNet(QString("PASSWORD%1%2").arg(delimeter).arg(hash));
--- a/QTfrontend/pageoptions.cpp	Sat Jun 04 16:09:12 2011 -0400
+++ b/QTfrontend/pageoptions.cpp	Wed Jun 08 03:36:54 2011 -0400
@@ -192,10 +192,18 @@
             editNetNick->setText(QLineEdit::tr("unnamed"));
             connect(editNetNick, SIGNAL(editingFinished()), this, SLOT(trimNetNick()));
             MiscLayout->addWidget(editNetNick, 0, 1);
+            
+            labelNetPassword = new QLabel(groupMisc);
+            labelNetPassword->setText(QLabel::tr("Password"));
+            MiscLayout->addWidget(labelNetPassword, 1, 0);
+            
+            editNetPassword = new QLineEdit(groupMisc);
+            editNetPassword->setEchoMode(QLineEdit::Password);
+            MiscLayout->addWidget(editNetPassword, 1, 1);
 
             QLabel *labelLanguage = new QLabel(groupMisc);
             labelLanguage->setText(QLabel::tr("Locale") + " *");
-            MiscLayout->addWidget(labelLanguage, 1, 0);
+            MiscLayout->addWidget(labelLanguage, 2, 0);
 
             CBLanguage = new QComboBox(groupMisc);
             QDir tmpdir;
@@ -210,26 +218,26 @@
                 CBLanguage->addItem(QLocale::languageToString(loc.language()) + " (" + QLocale::countryToString(loc.country()) + ")", loc.name());
             }
 
-            MiscLayout->addWidget(CBLanguage, 1, 1);
+            MiscLayout->addWidget(CBLanguage, 2, 1);
 
             CBAltDamage = new QCheckBox(groupMisc);
             CBAltDamage->setText(QCheckBox::tr("Alternative damage show"));
-            MiscLayout->addWidget(CBAltDamage, 2, 0, 1, 2);
+            MiscLayout->addWidget(CBAltDamage, 3, 0, 1, 2);
 
             CBNameWithDate = new QCheckBox(groupMisc);
             CBNameWithDate->setText(QCheckBox::tr("Append date and time to record file name"));
-            MiscLayout->addWidget(CBNameWithDate, 3, 0, 1, 2);
+            MiscLayout->addWidget(CBNameWithDate, 4, 0, 1, 2);
 
 #ifdef SPARKLE_ENABLED
             CBAutoUpdate = new QCheckBox(groupMisc);
             CBAutoUpdate->setText(QCheckBox::tr("Check for updates at startup"));
-            MiscLayout->addWidget(CBAutoUpdate, 4, 0, 1, 2);
+            MiscLayout->addWidget(CBAutoUpdate, 5, 0, 1, 2);
 #endif
 #ifndef __APPLE__
             BtnAssociateFiles = new QPushButton(groupMisc);
             BtnAssociateFiles->setText(QPushButton::tr("Associate file extensions"));
             BtnAssociateFiles->setEnabled(!custom_data && !custom_config);
-            MiscLayout->addWidget(BtnAssociateFiles, 4, 0, 1, 2);
+            MiscLayout->addWidget(BtnAssociateFiles, 5, 0, 1, 2);
 #endif
             gbTBLayout->addWidget(groupMisc, 2, 0);
         }
--- a/QTfrontend/pageoptions.h	Sat Jun 04 16:09:12 2011 -0400
+++ b/QTfrontend/pageoptions.h	Wed Jun 08 03:36:54 2011 -0400
@@ -70,8 +70,10 @@
     FPSEdit *fpsedit;
     QPushButton *BtnSaveOptions;
     QLabel *labelNN;
+    QLabel *labelNetPassword;
     QSpinBox * volumeBox;
     QLineEdit *editNetNick;
+    QLineEdit *editNetPassword;
     QSlider *SLQuality;
     QCheckBox *CBFrontendEffects;