Refactor: get rid of excessive QSettings instances on the same poor hedgewars.ini file
authorunc0rr
Sun, 02 Dec 2012 01:26:00 +0400
changeset 8179 a1ffcb559f99
parent 8178 8bd087478b48
child 8180 33868aea694c
Refactor: get rid of excessive QSettings instances on the same poor hedgewars.ini file
QTfrontend/gameuiconfig.cpp
QTfrontend/gameuiconfig.h
QTfrontend/hwform.cpp
QTfrontend/hwform.h
QTfrontend/ui/page/pagenetgame.cpp
QTfrontend/ui/page/pagenetgame.h
QTfrontend/ui/page/pageroomslist.cpp
QTfrontend/ui/page/pageroomslist.h
QTfrontend/ui/widget/chatwidget.cpp
QTfrontend/ui/widget/chatwidget.h
QTfrontend/ui_hwform.cpp
QTfrontend/ui_hwform.h
--- a/QTfrontend/gameuiconfig.cpp	Sun Dec 02 01:25:11 2012 +0400
+++ b/QTfrontend/gameuiconfig.cpp	Sun Dec 02 01:26:00 2012 +0400
@@ -94,11 +94,13 @@
     Form->ui.pageOptions->editNetPassword->installEventFilter(this);
 
     int passLength = value("net/passwordlength", 0).toInt();
-    setNetPasswordLength(passLength);
-    if (savePwd == false) {
-        Form->ui.pageOptions->editNetPassword->setEnabled(savePwd);
+    if (!savePwd) {
+        Form->ui.pageOptions->editNetPassword->setEnabled(false);
         Form->ui.pageOptions->editNetPassword->setText("");
         setNetPasswordLength(0);
+    } else
+    {
+        setNetPasswordLength(passLength);
     }
 
     delete netHost;
@@ -270,7 +272,7 @@
             setValue(QString("colors/color%1").arg(i), model->item(i)->data());
     }
 
-    Form->gameSettings->sync();
+    sync();
 }
 
 void GameUIConfig::SaveVideosOptions()
@@ -286,7 +288,13 @@
     setValue("videorec/usegameres", Form->ui.pageVideos->checkUseGameRes->isChecked());
     setValue("videorec/audio", recordAudio());
 
-    Form->gameSettings->sync();
+    sync();
+}
+
+void GameUIConfig::setValue(const QString &key, const QVariant &value)
+{
+    qDebug() << "[settings]" << key << value;
+    QSettings::setValue(key, value);
 }
 
 QString GameUIConfig::language()
@@ -447,7 +455,7 @@
 
 bool GameUIConfig::netPasswordIsValid()
 {
-    return (netPasswordLength() == 0 || Form->ui.pageOptions->editNetPassword->text() != QString(netPasswordLength(), '\0'));
+    return (netPasswordLength() == 0 || Form->ui.pageOptions->editNetPassword->text() != QString(netPasswordLength(), '*'));
 }
 
 // When hedgewars launches, the password field is set with null characters. If the user tries to edit the field and there are such characters, then clear the field
@@ -472,7 +480,7 @@
 {
     if (passwordLength > 0)
     {
-        Form->ui.pageOptions->editNetPassword->setText(QString(passwordLength, '\0'));
+        Form->ui.pageOptions->editNetPassword->setText(QString(passwordLength, '*'));
     }
     else
     {
--- a/QTfrontend/gameuiconfig.h	Sun Dec 02 01:25:11 2012 +0400
+++ b/QTfrontend/gameuiconfig.h	Sun Dec 02 01:26:00 2012 +0400
@@ -58,6 +58,7 @@
         bool isFrontendFullscreen() const;
         void resizeToConfigValues();
         quint32 stereoMode() const;
+        void setValue(const QString & key, const QVariant & value);
 
         QString AVFormat();
         QString videoCodec();
--- a/QTfrontend/hwform.cpp	Sun Dec 02 01:25:11 2012 +0400
+++ b/QTfrontend/hwform.cpp	Sun Dec 02 01:26:00 2012 +0400
@@ -111,7 +111,6 @@
 QString playerHash;
 
 GameUIConfig* HWForm::config = NULL;
-QSettings* HWForm::gameSettings = NULL;
 
 HWForm::HWForm(QWidget *parent, QString styleSheet)
     : QMainWindow(parent)
@@ -127,10 +126,6 @@
 #ifdef USE_XFIRE
     xfire_init();
 #endif
-    gameSettings = new QSettings("physfs://hedgewars.ini", QSettings::IniFormat);
-    frontendEffects = gameSettings->value("frontend/effects", true).toBool();
-    playerHash = QString(QCryptographicHash::hash(gameSettings->value("net/nick","").toString().toUtf8(), QCryptographicHash::Md5).toHex());
-
     this->setStyleSheet(styleSheet);
     ui.setupUi(this);
     setMinimumSize(760, 580);
@@ -140,7 +135,12 @@
     ui.pageOptions->CBResolution->addItems(SDLInteraction::instance().getResolutions());
 
     config = new GameUIConfig(this, "physfs://hedgewars.ini");
+    frontendEffects = config->value("frontend/effects", true).toBool();
+    playerHash = QString(QCryptographicHash::hash(config->value("net/nick","").toString().toUtf8(), QCryptographicHash::Md5).toHex());
 
+    ui.pageRoomsList->setSettings(config);
+    ui.pageNetGame->chatWidget->setSettings(config);
+    ui.pageRoomsList->chatWidget->setSettings(config);
     ui.pageVideos->init(config);
 
 #ifdef __APPLE__
@@ -445,7 +445,7 @@
 
     if(teamslist.empty())
     {
-        QString currentNickName = gameSettings->value("net/nick","").toString().toUtf8();
+        QString currentNickName = config->value("net/nick","").toString().toUtf8();
         QString teamName;
 
         if (currentNickName.isEmpty())
@@ -615,16 +615,6 @@
     {
         ui.pageOptions->setTeamOptionsEnabled(true);
     }
-
-    if (id == ID_PAGE_SETUP)
-    {
-        config->reloadValues();
-    }
-
-    if (id == ID_PAGE_VIDEOS )
-    {
-        config->reloadVideosValues();
-    }
 }
 
 void HWForm::GoToPage(int id)
@@ -970,33 +960,31 @@
 void HWForm::NetPassword(const QString & nick)
 {
     int passLength = config->value("net/passwordlength", 0).toInt();
-    QString hash = config->value("net/passwordhash", "").toString();
+    QString hash = QString::fromLatin1(config->value("net/passwordhash", "").toByteArray());
 
     // If the password is blank, ask the user to enter one in
     if (passLength == 0)
     {
-        HWPasswordDialog * hpd = new HWPasswordDialog(this, tr("Your nickname %1 is\nregistered on Hedgewars.org\nPlease provide your password below\nor pick another nickname in game config:").arg(nick));
-        hpd->cbSave->setChecked(config->value("net/savepassword", true).toBool());
-        if (hpd->exec() != QDialog::Accepted)
+        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));
+        hpd.cbSave->setChecked(config->value("net/savepassword", true).toBool());
+        if (hpd.exec() != QDialog::Accepted)
         {
             ForcedDisconnect(tr("No password supplied."));
-            delete hpd;
             return;
         }
 
-        QString password = hpd->lePassword->text();
+        QString password = hpd.lePassword->text();
         hash = QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Md5).toHex();
 
-        bool save = hpd->cbSave->isChecked();
+        bool save = hpd.cbSave->isChecked();
         config->setValue("net/savepassword", save);
         if (save) // user wants to save password
         {
-            config->setValue("net/passwordhash", hash);
+            config->setValue("net/passwordhash", hash.toLatin1());
             config->setValue("net/passwordlength", password.size());
             config->setNetPasswordLength(password.size());
+            config->sync();
         }
-
-        delete hpd;
     }
 
     hwnet->SendPasswordHash(hash);
@@ -1084,7 +1072,7 @@
     //connect(ui.pageNetGame->BtnBack, SIGNAL(clicked()), hwnet, SLOT(partRoom()));
 
     ui.pageRoomsList->chatWidget->setUsersModel(hwnet->lobbyPlayersModel());
-    ui.pageNetGame->pChatWidget->setUsersModel(hwnet->roomPlayersModel());
+    ui.pageNetGame->chatWidget->setUsersModel(hwnet->roomPlayersModel());
 
 // rooms list page stuff
     ui.pageRoomsList->setModel(hwnet->roomsListModel());
@@ -1113,26 +1101,26 @@
 
 // net page stuff
     connect(hwnet, SIGNAL(chatStringFromNet(const QString&)),
-            ui.pageNetGame->pChatWidget, SLOT(onChatString(const QString&)), Qt::QueuedConnection);
+            ui.pageNetGame->chatWidget, SLOT(onChatString(const QString&)), Qt::QueuedConnection);
 
     connect(hwnet, SIGNAL(chatStringFromMe(const QString&)),
-            ui.pageNetGame->pChatWidget, SLOT(onChatString(const QString&)), Qt::QueuedConnection);
+            ui.pageNetGame->chatWidget, SLOT(onChatString(const QString&)), Qt::QueuedConnection);
     connect(hwnet, SIGNAL(roomMaster(bool)),
-            ui.pageNetGame->pChatWidget, SLOT(adminAccess(bool)), Qt::QueuedConnection);
-    connect(ui.pageNetGame->pChatWidget, SIGNAL(chatLine(const QString&)),
+            ui.pageNetGame->chatWidget, SLOT(adminAccess(bool)), Qt::QueuedConnection);
+    connect(ui.pageNetGame->chatWidget, SIGNAL(chatLine(const QString&)),
             hwnet, SLOT(chatLineToNet(const QString&)));
     connect(ui.pageNetGame->BtnGo, SIGNAL(clicked()), hwnet, SLOT(ToggleReady()));
     connect(hwnet, SIGNAL(setMyReadyStatus(bool)),
             ui.pageNetGame, SLOT(setReadyStatus(bool)), Qt::QueuedConnection);
 
 // chat widget actions
-    connect(ui.pageNetGame->pChatWidget, SIGNAL(kick(const QString&)),
+    connect(ui.pageNetGame->chatWidget, SIGNAL(kick(const QString&)),
             hwnet, SLOT(kickPlayer(const QString&)));
-    connect(ui.pageNetGame->pChatWidget, SIGNAL(ban(const QString&)),
+    connect(ui.pageNetGame->chatWidget, SIGNAL(ban(const QString&)),
             hwnet, SLOT(banPlayer(const QString&)));
-    connect(ui.pageNetGame->pChatWidget, SIGNAL(info(const QString&)),
+    connect(ui.pageNetGame->chatWidget, SIGNAL(info(const QString&)),
             hwnet, SLOT(infoPlayer(const QString&)));
-    connect(ui.pageNetGame->pChatWidget, SIGNAL(follow(const QString&)),
+    connect(ui.pageNetGame->chatWidget, SIGNAL(follow(const QString&)),
             hwnet, SLOT(followPlayer(const QString&)));
     connect(ui.pageRoomsList->chatWidget, SIGNAL(kick(const QString&)),
             hwnet, SLOT(kickPlayer(const QString&)));
@@ -1155,9 +1143,9 @@
 
 // nick list stuff
     connect(hwnet, SIGNAL(nickAdded(const QString&, bool)),
-            ui.pageNetGame->pChatWidget, SLOT(nickAdded(const QString&, bool)), Qt::QueuedConnection);
+            ui.pageNetGame->chatWidget, SLOT(nickAdded(const QString&, bool)), Qt::QueuedConnection);
     connect(hwnet, SIGNAL(nickRemoved(const QString&)),
-            ui.pageNetGame->pChatWidget, SLOT(nickRemoved(const QString&)), Qt::QueuedConnection);
+            ui.pageNetGame->chatWidget, SLOT(nickRemoved(const QString&)), Qt::QueuedConnection);
     connect(hwnet, SIGNAL(nickAddedLobby(const QString&, bool)),
             ui.pageRoomsList->chatWidget, SLOT(nickAdded(const QString&, bool)), Qt::QueuedConnection);
     connect(hwnet, SIGNAL(nickRemovedLobby(const QString&)),
@@ -1303,7 +1291,7 @@
 
 void HWForm::NetGameEnter()
 {
-    ui.pageNetGame->pChatWidget->clear();
+    ui.pageNetGame->chatWidget->clear();
     GoToPage(ID_PAGE_NETGAME);
 }
 
--- a/QTfrontend/hwform.h	Sun Dec 02 01:25:11 2012 +0400
+++ b/QTfrontend/hwform.h	Sun Dec 02 01:26:00 2012 +0400
@@ -61,7 +61,6 @@
         HWForm(QWidget *parent = 0, QString styleSheet = "");
         Ui_HWForm ui;
         static GameUIConfig * config;
-        static QSettings * gameSettings; // Same file GameUIConfig points to but without the baggage.  Needs sync() calls if you want to get GameUIConfig changes though
         void updateXfire();
         void PlayDemoQuick(const QString & demofilename);
         void exit();
--- a/QTfrontend/ui/page/pagenetgame.cpp	Sun Dec 02 01:25:11 2012 +0400
+++ b/QTfrontend/ui/page/pagenetgame.cpp	Sun Dec 02 01:26:00 2012 +0400
@@ -37,10 +37,10 @@
     pageLayout->setColumnStretch(1, 50);
 
     // chatwidget
-    pChatWidget = new HWChatWidget(this, m_gameSettings, true);
-    pChatWidget->setShowFollow(false); // don't show follow in nicks' context menus
-    pChatWidget->setIgnoreListKick(true); // kick ignored players automatically
-    pageLayout->addWidget(pChatWidget, 2, 0, 1, 2);
+    chatWidget = new HWChatWidget(this, true);
+    chatWidget->setShowFollow(false); // don't show follow in nicks' context menus
+    chatWidget->setIgnoreListKick(true); // kick ignored players automatically
+    pageLayout->addWidget(chatWidget, 2, 0, 1, 2);
     pageLayout->setRowStretch(1, 100);
     pageLayout->setRowStretch(2, 100);
 
@@ -96,10 +96,8 @@
     connect(BtnUpdate, SIGNAL(clicked()), this, SLOT(onUpdateClick()));
 }
 
-PageNetGame::PageNetGame(QWidget* parent, QSettings * gameSettings) : AbstractPage(parent)
+PageNetGame::PageNetGame(QWidget* parent) : AbstractPage(parent)
 {
-    m_gameSettings = gameSettings;
-
     initPage();
 
     QMenu * menu = new QMenu(BtnMaster);
@@ -113,24 +111,23 @@
     menu->addAction(restrictTeamAdds);
 
     BtnMaster->setMenu(menu);
-
 }
 
 
 void PageNetGame::displayError(const QString & message)
 {
-    pChatWidget->displayError(message);
+    chatWidget->displayError(message);
 }
 
 
 void PageNetGame::displayNotice(const QString & message)
 {
-    pChatWidget->displayNotice(message);
+    chatWidget->displayNotice(message);
 }
 
 void PageNetGame::displayWarning(const QString & message)
 {
-    pChatWidget->displayWarning(message);
+    chatWidget->displayWarning(message);
 }
 
 
@@ -178,5 +175,5 @@
 
 void PageNetGame::setUser(const QString & nickname)
 {
-    pChatWidget->setUser(nickname);
+    chatWidget->setUser(nickname);
 }
--- a/QTfrontend/ui/page/pagenetgame.h	Sun Dec 02 01:25:11 2012 +0400
+++ b/QTfrontend/ui/page/pagenetgame.h	Sun Dec 02 01:26:00 2012 +0400
@@ -32,7 +32,7 @@
         Q_OBJECT
 
     public:
-        PageNetGame(QWidget* parent, QSettings * gameSettings);
+        PageNetGame(QWidget* parent);
 
         /**
          * Sets the room name to display.
@@ -52,7 +52,7 @@
         QAction * restrictJoins;
         QAction * restrictTeamAdds;
 
-        HWChatWidget* pChatWidget;
+        HWChatWidget* chatWidget;
 
         TeamSelWidget* pNetTeamsWidget;
         GameCFGWidget* pGameCFG;
@@ -72,8 +72,6 @@
         QLayout * footerLayoutDefinition();
         void connectSignals();
 
-        QSettings * m_gameSettings;
-
         HistoryLineEdit * leRoomName;
         QPushButton * btnSetup;
 };
--- a/QTfrontend/ui/page/pageroomslist.cpp	Sun Dec 02 01:25:11 2012 +0400
+++ b/QTfrontend/ui/page/pageroomslist.cpp	Sun Dec 02 01:26:00 2012 +0400
@@ -95,7 +95,7 @@
 
     pageLayout->addLayout(filterLayout, 4, 0, 1, 2);
 
-    chatWidget = new HWChatWidget(this, m_gameSettings, false);
+    chatWidget = new HWChatWidget(this, false);
     pageLayout->addWidget(chatWidget, 5, 0, 1, 3);
     pageLayout->setRowStretch(5, 350);
 
@@ -156,11 +156,9 @@
 }
 
 
-PageRoomsList::PageRoomsList(QWidget* parent, QSettings * gameSettings) :
+PageRoomsList::PageRoomsList(QWidget* parent) :
     AbstractPage(parent)
 {
-    m_gameSettings = gameSettings;
-
     roomsModel = NULL;
     stateFilteredModel = NULL;
     schemeFilteredModel = NULL;
@@ -612,13 +610,17 @@
             QString("*%1*").arg(CBWeapons->currentText()));
 }
 
+void PageRoomsList::setSettings(QSettings *settings)
+{
+    m_gameSettings = settings;
+}
 
 bool PageRoomsList::restoreHeaderState()
 {
     if (!m_gameSettings->contains("frontend/roomslist_header"))
         return false;
     return roomsList->horizontalHeader()->restoreState(QByteArray::fromBase64(
-        (m_gameSettings->value("frontend/roomslist_header").toString().toAscii())));
+        (m_gameSettings->value("frontend/roomslist_header").toByteArray())));
 }
 
 void PageRoomsList::saveHeaderState()
--- a/QTfrontend/ui/page/pageroomslist.h	Sun Dec 02 01:25:11 2012 +0400
+++ b/QTfrontend/ui/page/pageroomslist.h	Sun Dec 02 01:26:00 2012 +0400
@@ -32,10 +32,11 @@
         Q_OBJECT
 
     public:
-        PageRoomsList(QWidget* parent, QSettings * config);
+        PageRoomsList(QWidget* parent);
         void displayError(const QString & message);
         void displayNotice(const QString & message);
         void displayWarning(const QString & message);
+        void setSettings(QSettings * settings);
 
         QLineEdit * roomName;
         QLineEdit * searchText;
--- a/QTfrontend/ui/widget/chatwidget.cpp	Sun Dec 02 01:25:11 2012 +0400
+++ b/QTfrontend/ui/widget/chatwidget.cpp	Sun Dec 02 01:26:00 2012 +0400
@@ -177,7 +177,7 @@
 }
 
 
-HWChatWidget::HWChatWidget(QWidget* parent, QSettings * gameSettings, bool notify) :
+HWChatWidget::HWChatWidget(QWidget* parent, bool notify) :
     QWidget(parent),
     mainLayout(this)
 {
@@ -187,7 +187,6 @@
     m_isAdmin = false;
     m_autoKickEnabled = false;
 
-    if(gameSettings->value("frontend/sound", true).toBool())
     {
         QStringList vpList =
              QStringList() << "Classic" << "Default" << "Mobster" << "Russian";
@@ -281,6 +280,10 @@
     clear();
 }
 
+void HWChatWidget::setSettings(QSettings * settings)
+{
+    gameSettings = settings;
+}
 
 void HWChatWidget::linkClicked(const QUrl & link)
 {
--- a/QTfrontend/ui/widget/chatwidget.h	Sun Dec 02 01:25:11 2012 +0400
+++ b/QTfrontend/ui/widget/chatwidget.h	Sun Dec 02 01:26:00 2012 +0400
@@ -54,7 +54,7 @@
         Q_OBJECT
 
     public:
-        HWChatWidget(QWidget* parent, QSettings * gameSettings, bool notify);
+        HWChatWidget(QWidget* parent, bool notify);
         void setIgnoreListKick(bool enabled); ///< automatically kick people on ignore list (if possible)
         void setShowFollow(bool enabled);
         static const QString & styleSheet();
@@ -63,6 +63,7 @@
         void displayWarning(const QString & message);
         void setUser(const QString & nickname);
         void setUsersModel(QAbstractItemModel * model);
+        void setSettings(QSettings * settings);
 
     protected:
         virtual void dragEnterEvent(QDragEnterEvent * event);
--- a/QTfrontend/ui_hwform.cpp	Sun Dec 02 01:25:11 2012 +0400
+++ b/QTfrontend/ui_hwform.cpp	Sun Dec 02 01:26:00 2012 +0400
@@ -60,7 +60,7 @@
     centralWidget = new QWidget(HWForm);
     centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
 
-    SetupPages(centralWidget, HWForm);
+    SetupPages(centralWidget);
 
     HWForm->setCentralWidget(centralWidget);
 
@@ -74,7 +74,7 @@
     font14 = new QFont("MS Shell Dlg", 14);
 }
 
-void Ui_HWForm::SetupPages(QWidget *Parent, HWForm *HWForm)
+void Ui_HWForm::SetupPages(QWidget *Parent)
 {
     Pages = new QStackedLayout(Parent);
 
@@ -93,7 +93,7 @@
     pageNet = new PageNet();
     Pages->addWidget(pageNet);
 
-    pageNetGame = new PageNetGame(Parent, HWForm->gameSettings);
+    pageNetGame = new PageNetGame(Parent);
     Pages->addWidget(pageNetGame);
 
     pageInfo = new PageInfo();
@@ -120,7 +120,7 @@
     pageInGame = new PageInGame();
     Pages->addWidget(pageInGame);
 
-    pageRoomsList = new PageRoomsList(Parent, HWForm->gameSettings);
+    pageRoomsList = new PageRoomsList(Parent);
     Pages->addWidget(pageRoomsList);
 
     pageConnecting = new PageConnecting();
--- a/QTfrontend/ui_hwform.h	Sun Dec 02 01:25:11 2012 +0400
+++ b/QTfrontend/ui_hwform.h	Sun Dec 02 01:26:00 2012 +0400
@@ -86,7 +86,7 @@
 
         void setupUi(HWForm *HWForm);
         void SetupFonts();
-        void SetupPages(QWidget *Parent, HWForm *HWForm);
+        void SetupPages(QWidget *Parent);
 };
 
 #endif // UI_HWFORM_H