QTfrontend/hwform.cpp
branchflibqtfrontend
changeset 8304 620560c89284
parent 8280 734b069a418e
parent 8302 a7934cd12469
child 8363 0b4ac686fc44
--- a/QTfrontend/hwform.cpp	Sun Dec 16 12:05:32 2012 +0400
+++ b/QTfrontend/hwform.cpp	Sun Dec 16 12:25:13 2012 +0400
@@ -96,6 +96,7 @@
 #include "playerslistmodel.h"
 
 #include "DataManager.h"
+#include "AutoUpdater.h"
 
 #ifdef __APPLE__
 #include "M3Panel.h"
@@ -149,15 +150,24 @@
 #endif
 
 #ifdef __APPLE__
-    panel = new M3Panel;
-
+    AutoUpdater* updater = NULL;
+    if (config->isAutoUpdateEnabled())
+    {
+#ifdef __APPLE__
 #ifdef SPARKLE_ENABLED
-    AutoUpdater* updater;
+        updater = new SparkleAutoUpdater();
+#endif
+#endif
+        if (updater)
+        {
+            updater->checkForUpdates();
+            delete updater;
+        }
+    }
+#endif
 
-    updater = new SparkleAutoUpdater();
-    if (updater && config->isAutoUpdateEnabled())
-        updater->checkForUpdates();
-#endif
+#ifdef __APPLE__
+    panel = new M3Panel;
 
     QShortcut *hideFrontend = new QShortcut(QKeySequence("Ctrl+M"), this);
     connect (hideFrontend, SIGNAL(activated()), this, SLOT(showMinimized()));
@@ -1033,8 +1043,8 @@
 void HWForm::NetPassword(const QString & nick)
 {
     //Get hashes
-    QString hash =  config->value("net/passwordhash", "").toString();
-    QString temphash =  config->value("net/temppasswordhash", "").toString();
+    QString hash =  config->passwordHash();
+    QString temphash =  config->tempHash();
 
     //Check them
 
@@ -1049,9 +1059,61 @@
     }
 
     //Remove temporary hash from config
-    QString key = "net/temppasswordhash";
-    config->setValue(key, "");
-    config->remove(key);
+    config->clearTempHash();
+}
+
+void HWForm::NetNickRegistered(const QString & nick)
+{
+    //Get hashes
+    QString hash =  config->passwordHash();
+    QString temphash =  config->tempHash();
+
+    if (hash.isEmpty()) {
+        if (temphash.isEmpty()) { //If the user enters a registered nick with no password
+            QString suppliedpass;
+            while (suppliedpass.isEmpty()) {
+                QInputDialog nickRegedDialog(this);
+                nickRegedDialog.setWindowModality(Qt::WindowModal);
+                nickRegedDialog.setInputMode(QInputDialog::TextInput);
+                nickRegedDialog.setWindowTitle(tr("Hedgewars - Nick registered"));
+                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:"));
+                nickRegedDialog.setTextEchoMode(QLineEdit::Password);
+                nickRegedDialog.exec();
+
+                suppliedpass = nickRegedDialog.textValue();
+
+                if (nickRegedDialog.result() == QDialog::Rejected) {
+                    config->clearPasswordHash();
+                    config->clearTempHash();
+                    GoBack();
+                    return;
+                }
+                temphash = QCryptographicHash::hash(suppliedpass.toUtf8(), QCryptographicHash::Md5).toHex();
+                config->setTempHash(temphash);
+            }
+        }
+    }
+    NetPassword(nick);
+}
+
+void HWForm::NetNickNotRegistered(const QString & nick)
+{
+    QMessageBox noRegMsg(this);
+    noRegMsg.setIcon(QMessageBox::Information);
+    noRegMsg.setWindowTitle(QMessageBox::tr("Hedgewars - Nick not registered"));
+    noRegMsg.setWindowModality(Qt::WindowModal);
+    noRegMsg.setText(tr("Your nickname is not registered.\nTo prevent someone else from using it,\nplease register it at www.hedgewars.org"));
+
+    if (!config->passwordHash().isEmpty())
+    {
+        config->clearPasswordHash();
+        noRegMsg.setText(noRegMsg.text()+tr("\n\nYour password wasn't saved either."));
+    }
+    if (!config->tempHash().isEmpty())
+    {
+        config->clearTempHash();
+    }
+    noRegMsg.exec();
 }
 
 void HWForm::NetNickTaken(const QString & nick)
@@ -1061,7 +1123,12 @@
 
     if (!ok || newNick.isEmpty())
     {
-        ForcedDisconnect(tr("No nickname supplied."));
+        //ForcedDisconnect(tr("No nickname supplied."));
+	bool retry = RetryDialog(tr("Hedgewars - Empty nickname"), tr("No nickname supplied."));
+	GoBack();
+        if (retry) {
+       	   NetConnectOfficialServer();
+        }
         return;
     }
 
@@ -1076,8 +1143,39 @@
 void HWForm::NetAuthFailed()
 {
     // Set the password blank if case the user tries to join and enter his password again
-    config->setValue("net/passwordlength", 0);
-    config->setNetPasswordLength(0);
+    config->clearTempHash();
+
+    //Try to login again
+    bool retry = RetryDialog(tr("Hedgewars - Wrong password"), tr("You entered a wrong password."));
+    GoBack();
+
+    config->clearPasswordHash();
+    config->clearTempHash();
+    if (retry) {
+       NetConnectOfficialServer();
+    }
+}
+
+bool HWForm::RetryDialog(const QString & title, const QString & label)
+{
+    QMessageBox retryMsg(this);
+    retryMsg.setIcon(QMessageBox::Warning);
+    retryMsg.setWindowTitle(title);
+    retryMsg.setText(label);
+    retryMsg.setWindowModality(Qt::WindowModal);
+
+    retryMsg.addButton(QMessageBox::Cancel);
+
+    QPushButton *retryButton = retryMsg.addButton(QMessageBox::Ok);
+    retryButton->setText(tr("Try Again"));
+    retryButton->setFocus();
+
+    retryMsg.exec();
+
+    if (retryMsg.clickedButton() == retryButton) {
+       return true;
+    }
+    return false;
 }
 
 void HWForm::NetTeamAccepted(const QString & team)
@@ -1130,7 +1228,8 @@
     connect(hwnet, SIGNAL(AddNetTeam(const HWTeam&)), this, SLOT(AddNetTeam(const HWTeam&)), Qt::QueuedConnection);
     connect(hwnet, SIGNAL(RemoveNetTeam(const HWTeam&)), this, SLOT(RemoveNetTeam(const HWTeam&)), Qt::QueuedConnection);
     connect(hwnet, SIGNAL(TeamAccepted(const QString&)), this, SLOT(NetTeamAccepted(const QString&)), Qt::QueuedConnection);
-    connect(hwnet, SIGNAL(AskForPassword(const QString&)), this, SLOT(NetPassword(const QString&)), Qt::QueuedConnection);
+    connect(hwnet, SIGNAL(NickRegistered(const QString&)), this, SLOT(NetNickRegistered(const QString&)), Qt::QueuedConnection);
+    connect(hwnet, SIGNAL(NickNotRegistered(const QString&)), this, SLOT(NetNickNotRegistered(const QString&)), Qt::QueuedConnection);
     connect(hwnet, SIGNAL(NickTaken(const QString&)), this, SLOT(NetNickTaken(const QString&)), Qt::QueuedConnection);
     connect(hwnet, SIGNAL(AuthFailed()), this, SLOT(NetAuthFailed()), Qt::QueuedConnection);
     //connect(ui.pageNetGame->BtnBack, SIGNAL(clicked()), hwnet, SLOT(partRoom()));
@@ -1250,78 +1349,86 @@
     connect(hwnet, SIGNAL(paramChanged(const QString &, const QStringList &)), ui.pageNetGame->pGameCFG, SLOT(setParam(const QString &, const QStringList &)));
     connect(ui.pageNetGame->pGameCFG, SIGNAL(paramChanged(const QString &, const QStringList &)), hwnet, SLOT(onParamChanged(const QString &, const QStringList &)));
     connect(hwnet, SIGNAL(configAsked()), ui.pageNetGame->pGameCFG, SLOT(fullNetConfig()));
-   
+
 //nick and pass stuff
-    
+
     //remove temppasswordhash just in case
-    config->value("net/temppasswordhash", "");
-    config->remove("net/temppasswordhash");
-    
+    config->clearTempHash();
+
     //initialize
-    QString hash = config->value("net/passwordhash", "").toString();
-    QString temphash = config->value("net/temppasswordhash", "").toString();
+    QString hash = config->passwordHash();
+    QString temphash = config->tempHash();
     QString nickname = config->value("net/nick", "").toString();
     QString password;
-    
+
     if (nickname.isEmpty() || hash.isEmpty()) { //if something from login is missing, start dialog loop
-    
-    while (nickname.isEmpty() || (hash.isEmpty() && temphash.isEmpty())) //while a nickname, or both hashes are missing
-    {
-	//open dialog
-        HWPasswordDialog * hpd = new HWPasswordDialog(this);
-        hpd->cbSave->setChecked(config->value("net/savepassword", true).toBool());
+
+        while (nickname.isEmpty() || (hash.isEmpty() && temphash.isEmpty())) //while a nickname, or both hashes are missing
+        {
+        //open dialog
+            HWPasswordDialog * pwDialog = new HWPasswordDialog(this);
+            pwDialog->cbSave->setChecked(config->value("net/savepassword", true).toBool());
 
-	//if nickname is present, put it into the field
-	if (!nickname.isEmpty()) {
-	    hpd->leNickname->setText(nickname);
-	    hpd->lePassword->setFocus();
-	}
+        //if nickname is present, put it into the field
+        if (!nickname.isEmpty()) {
+            pwDialog->leNickname->setText(nickname);
+            pwDialog->lePassword->setFocus();
+        }
 
-	//if dialog close, create an error message
-        if (hpd->exec() != QDialog::Accepted)
-        {
-            ForcedDisconnect(tr("Login info not supplied."));
-            delete hpd;
+        //if dialog close, create an error message
+        if (pwDialog->exec() != QDialog::Accepted) {
+            delete pwDialog;
+            GoBack();
             return;
         }
 
-	//set nick and pass from the dialog
-	nickname = hpd->leNickname->text();
-        password = hpd->lePassword->text();
-
-	//calculate temphash and set it into config
-	temphash = QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Md5).toHex();
-	config->setValue("net/temppasswordhash", temphash);
+        //set nick and pass from the dialog
+        nickname = pwDialog->leNickname->text();
+        password = pwDialog->lePassword->text();
 
-	//if user wants to save password
-        bool save = hpd->cbSave->isChecked();
-        config->setValue("net/savepassword", save);
-        if (save) // user wants to save password
-        {
-            config->setValue("net/passwordhash", temphash);
-            config->setValue("net/passwordlength", password.size());
-            config->setNetPasswordLength(password.size());
+        //check the nickname variable
+        if (nickname.isEmpty()) {
+            int retry = RetryDialog(tr("Hedgewars - Empty nickname"), tr("No nickname supplied."));
+            GoBack();
+            delete pwDialog;
+            if (retry) {
+                NetConnectOfficialServer();
+            }
+            return;
         }
 
-        delete hpd;
+        if (!password.isEmpty()) {
+            //calculate temphash and set it into config
+            temphash = QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Md5).toHex();
+            config->setTempHash(temphash);
 
+            //if user wants to save password
+            bool save = pwDialog->cbSave->isChecked();
+            config->setValue("net/savepassword", save);
+            if (save) // user wants to save password
+            {
+                config->setPasswordHash(temphash);
+            }
+        }
+        else {
+            delete pwDialog;
+            config->setValue("net/nick", nickname);
+            config->updNetNick();
+            config->clearPasswordHash();
+            break;
+        }
 
-	//update nickname
+        delete pwDialog;
+
+        //update nickname
         config->setValue("net/nick", nickname);
         config->updNetNick();
-	
-	//and all the variables
-	hash = config->value("net/passwordhash", "").toString();
-	temphash = config->value("net/temppasswordhash", "").toString();
-	nickname = config->value("net/nick", "").toString();
-    }
-    
-    
-	//if pass is none (hash is generated anyway), remove the hash
-	if (password.size() <= 0) {
-	    config->setValue("net/temppasswordhash", "");
-	    config->remove("net/temppasswordhash");
-	}
+
+        //and all the variables
+        hash = config->passwordHash();
+        temphash = config->tempHash();
+        nickname = config->value("net/nick", "").toString();
+        }
     }
 
     ui.pageRoomsList->setUser(nickname);
@@ -1396,6 +1503,21 @@
 
 void HWForm::ForcedDisconnect(const QString & reason)
 {
+    if (reason == "Reconnected too fast") { //TODO: this is a hack, which should be remade
+        bool retry = RetryDialog(tr("Hedgewars - Connection error"), tr("You reconnected too fast.\nPlease wait a few seconds and try again."));
+        if (retry) {
+            NetConnectOfficialServer();
+        }
+        else {
+            while (ui.Pages->currentIndex() != ID_PAGE_NET
+                && ui.Pages->currentIndex() != ID_PAGE_NETTYPE
+                && ui.Pages->currentIndex() != ID_PAGE_MAIN) 
+            {
+                GoBack();
+            }
+        }
+        return;
+    }
     if (pnetserver)
         return; // we have server - let it care of all things
     if (hwnet)
@@ -1500,7 +1622,7 @@
 void HWForm::CreateGame(GameCFGWidget * gamecfg, TeamSelWidget* pTeamSelWidget, QString ammo)
 {
     game = new HWGame(config, gamecfg, ammo, pTeamSelWidget);
-    connect(game, SIGNAL(CampStateChanged(int)), this, SLOT(UpdateCampaignPageProgress(int)));
+    connect(game, SIGNAL(campStateChanged(int)), this, SLOT(UpdateCampaignPageProgress(int)));
     connect(game, SIGNAL(GameStateChanged(GameState)), this, SLOT(GameStateChanged(GameState)));
     connect(game, SIGNAL(GameStats(char, const QString &)), ui.pageGameStats, SLOT(GameStats(char, const QString &)));
     connect(game, SIGNAL(ErrorMessage(const QString &)), this, SLOT(ShowErrorMessage(const QString &)), Qt::QueuedConnection);
@@ -1759,19 +1881,25 @@
 // used for --set-everything [screen width] [screen height] [color dept] [volume] [enable music] [enable sounds] [language file] [full screen] [show FPS] [alternate damage] [timer value] [reduced quality]
 QString HWForm::getDemoArguments()
 {
+#ifdef Q_WS_WIN
+    QString userdir = cfgdir->absolutePath().replace("/","\\");
+#else
+    QString userdir = cfgdir->absolutePath();
+#endif
+
     QRect resolution = config->vid_Resolution();
-    return QString(QString::number(resolution.width()) + " "
-                   + QString::number(resolution.height()) + " "
-                   + QString::number(config->bitDepth()) + " " // bpp
-                   + QString::number(config->volume()) + " " // sound volume
-                   + (config->isMusicEnabled() ? "1" : "0") + " "
-                   + (config->isSoundEnabled() ? "1" : "0") + " "
-                   + config->language() + ".txt "
-                   + (config->vid_Fullscreen() ? "1" : "0") + " "
-                   + (config->isShowFPSEnabled() ? "1" : "0") + " "
-                   + (config->isAltDamageEnabled() ? "1" : "0") + " "
-                   + QString::number(config->timerInterval()) + " "
-                   + QString::number(config->translateQuality()));
+    return QString("--user-dir " + userdir
+                   + " --width " + QString::number(resolution.width())
+                   + " --height " + QString::number(resolution.height())
+                   + " --volume " + QString::number(config->volume())
+                   + (config->isMusicEnabled() ? "" : " --nomusic")
+                   + (config->isSoundEnabled() ? "" : " --nosound")
+                   + " --locale " + config->language() + ".txt"
+                   + (config->vid_Fullscreen() ? " --fullscreen" : "")
+                   + (config->isShowFPSEnabled() ? " --showfps" : "")
+                   + (config->isAltDamageEnabled() ? " --altdmg" : "")
+                   + " --frame-interval " + QString::number(config->timerInterval())
+                   + " --raw-quality " + QString::number(config->translateQuality()));
 }
 
 void HWForm::AssociateFiles()
@@ -1786,8 +1914,8 @@
     registry_hkcr.setValue("Hedgewars.Save/Default", tr("Hedgewars Save File", "File Types"));
     registry_hkcr.setValue("Hedgewars.Demo/DefaultIcon/Default", "\"" + bindir->absolutePath().replace("/", "\\") + "\\hwdfile.ico\",0");
     registry_hkcr.setValue("Hedgewars.Save/DefaultIcon/Default", "\"" + bindir->absolutePath().replace("/", "\\") + "\\hwsfile.ico\",0");
-    registry_hkcr.setValue("Hedgewars.Demo/Shell/Open/Command/Default", "\"" + bindir->absolutePath().replace("/", "\\") + "\\hwengine.exe\" \"" + cfgdir->absolutePath().replace("/","\\") + "\" \"" + datadir->absolutePath().replace("/", "\\") + "\" \"%1\" --set-everything "+arguments);
-    registry_hkcr.setValue("Hedgewars.Save/Shell/Open/Command/Default", "\"" + bindir->absolutePath().replace("/", "\\") + "\\hwengine.exe\" \"" + cfgdir->absolutePath().replace("/","\\") + "\" \"" + datadir->absolutePath().replace("/", "\\") + "\" \"%1\" --set-everything "+arguments);
+    registry_hkcr.setValue("Hedgewars.Demo/Shell/Open/Command/Default", "\"" + bindir->absolutePath().replace("/", "\\") + "\\hwengine.exe\" \"" + datadir->absolutePath().replace("/", "\\") + "\" \"%1\" "+arguments);
+    registry_hkcr.setValue("Hedgewars.Save/Shell/Open/Command/Default", "\"" + bindir->absolutePath().replace("/", "\\") + "\\hwengine.exe\" \"" + datadir->absolutePath().replace("/", "\\") + "\" \"%1\" "+arguments);
 #elif defined __APPLE__
     // only useful when other apps have taken precedence over our file extensions and you want to reset it
     system("defaults write com.apple.LaunchServices LSHandlers -array-add '<dict><key>LSHandlerContentTag</key><string>hwd</string><key>LSHandlerContentTagClass</key><string>public.filename-extension</string><key>LSHandlerRoleAll</key><string>org.hedgewars.desktop</string></dict>'");
@@ -1808,7 +1936,7 @@
     if (success) success = system("xdg-mime default hwengine.desktop application/x-hedgewars-demo")==0;
     if (success) success = system("xdg-mime default hwengine.desktop application/x-hedgewars-save")==0;
     // hack to add user's settings to hwengine. might be better at this point to read in the file, append it, and write it out to its new home.  This assumes no spaces in the data dir path
-    if (success) success = system(("sed -i 's/^\\(Exec=.*\\) \\([^ ]* %f\\)/\\1 "+cfgdir->absolutePath().replace(" ","\\\\ ").replace("/","\\/")+" \\2 --set-everything "+arguments+"/' "+QDir::home().absolutePath()+"/.local/share/applications/hwengine.desktop").toLocal8Bit().constData())==0;
+    if (success) success = system(("sed -i 's/^\\(Exec=.*\\) \\([^ ]* %f\\)/\\1 \\2 "+arguments+"/' "+QDir::home().absolutePath()+"/.local/share/applications/hwengine.desktop").toLocal8Bit().constData())==0;
 #endif
     if (success)
     {