diff --git a/apps/browser/CMakeLists.txt b/apps/browser/CMakeLists.txt index 83e3ac7ad..0f094ed4c 100644 --- a/apps/browser/CMakeLists.txt +++ b/apps/browser/CMakeLists.txt @@ -14,6 +14,7 @@ set(BROWSER PingUpdater.cpp PingHelper.cpp QueryHelper.cpp + Settings.cpp ${CMAKE_SOURCE_DIR}/files/tes3mp/browser.rc ) @@ -33,6 +34,7 @@ set(BROWSER_HEADER netutils/Utils.hpp netutils/QueryClient.hpp Types.hpp + Settings.hpp ) source_group(browser FILES ${BROWSER} ${BROWSER_HEADER}) diff --git a/apps/browser/MainWindow.cpp b/apps/browser/MainWindow.cpp index fd20f19ee..04b2e2a37 100644 --- a/apps/browser/MainWindow.cpp +++ b/apps/browser/MainWindow.cpp @@ -13,6 +13,8 @@ #include #include #include +#include +#include using namespace Process; @@ -31,9 +33,6 @@ MainWindow::MainWindow(QWidget *parent) tblServerBrowser->setModel(proxyModel); tblFavorites->setModel(proxyModel); - // Remove Favorites tab while it remains broken - tabWidget->removeTab(1); - tblServerBrowser->hideColumn(ServerData::ADDR); tblFavorites->hideColumn(ServerData::ADDR); @@ -57,12 +56,20 @@ MainWindow::MainWindow(QWidget *parent) connect(cBBoxWOPass, SIGNAL(toggled(bool)), this, SLOT(noPasswordSwitch(bool))); connect(comboLatency, SIGNAL(currentIndexChanged(int)), this, SLOT(maxLatencyChanged(int))); connect(leGamemode, SIGNAL(textChanged(const QString &)), this, SLOT(gamemodeChanged(const QString &))); + connect(pbModulePath, &QPushButton::clicked, [this](bool) { + QString str = QFileDialog::getExistingDirectory(this, tr("Module path"), + leModulePath->text(), QFileDialog::ShowDirsOnly); + if(!str.isEmpty()) + leModulePath->setText(str); + }); loadFavorites(); queryHelper->refresh(); + settingsMgr.loadBrowserSettings(*this); } MainWindow::~MainWindow() { + settingsMgr.saveBrowserSettings(*this); delete mGameInvoker; } @@ -71,6 +78,9 @@ void MainWindow::addServerAndUpdate(const QString &addr) favorites->insertRow(0); QModelIndex mi = favorites->index(0, ServerData::ADDR); favorites->setData(mi, addr, Qt::EditRole); + /*auto address = addr.split(":"); + auto data = getExtendedData(address[0].toLatin1(), address[1].toUShort());*/ + //NetController::get()->updateInfo(favorites, mi); //QueryClient::Update(RakNet::SystemAddress()) /*auto data = QueryClient::Get().Query(); diff --git a/apps/browser/MainWindow.hpp b/apps/browser/MainWindow.hpp index 31e686295..d5fa4b8c4 100644 --- a/apps/browser/MainWindow.hpp +++ b/apps/browser/MainWindow.hpp @@ -9,6 +9,7 @@ #include "ui_Main.h" #include "ServerModel.hpp" #include "MySortFilterProxyModel.hpp" +#include "Settings.hpp" #include class QueryHelper; @@ -39,6 +40,7 @@ private: Process::ProcessInvoker *mGameInvoker; ServerModel *browser, *favorites; MySortFilterProxyModel *proxyModel; + SettingsMgr settingsMgr; void loadFavorites(); }; diff --git a/apps/browser/Settings.cpp b/apps/browser/Settings.cpp new file mode 100644 index 000000000..964a30983 --- /dev/null +++ b/apps/browser/Settings.cpp @@ -0,0 +1,189 @@ +// +// Created by koncord on 31.03.18. +// + +#include "Settings.hpp" +#include + +#include +#include +#include "MainWindow.hpp" + +std::string loadSettings (Settings::Manager & settings, const std::string &cfgName) +{ + Files::ConfigurationManager mCfgMgr; + // Create the settings manager and load default settings file + const std::string localdefault = (mCfgMgr.getLocalPath() / (cfgName + "-default.cfg")).string(); + const std::string globaldefault = (mCfgMgr.getGlobalPath() / (cfgName + "-default.cfg")).string(); + + // prefer local + if (boost::filesystem::exists(localdefault)) + settings.loadDefault(localdefault); + else if (boost::filesystem::exists(globaldefault)) + settings.loadDefault(globaldefault); + else + throw std::runtime_error ("No default settings file found! Make sure the file \"" + cfgName + "-default.cfg\" was properly installed."); + + // load user settings if they exist + const std::string settingspath = (mCfgMgr.getUserConfigPath() / (cfgName + ".cfg")).string(); + if (boost::filesystem::exists(settingspath)) + settings.loadUser(settingspath); + + return settingspath; +} + +SettingsMgr::SettingsMgr() +{ + clientCfg = loadSettings(clientMgr, "tes3mp-client"); + switchMgr(); + serverCfg = loadSettings(serverMgr, "tes3mp-server"); + switchMgr(); + + std::string addr = clientMgr.getString("address", "Master"); + int port = clientMgr.getInt("port", "Master"); + QueryClient::Get().SetServer(addr, port); + +} + +void SettingsMgr::loadBrowserSettings(Ui::MainWindow &mw) +{ + mw.comboLatency->setCurrentIndex(clientMgr.getInt("maxLatency", "Browser")); + mw.leGamemode->setText(QString::fromStdString(clientMgr.getString("gameMode", "Browser"))); + mw.cBoxNotFull->setCheckState(clientMgr.getBool("notFull", "Browser") ? Qt::Checked : Qt::Unchecked); + mw.cBoxWithPlayers->setCheckState(clientMgr.getBool("withPlayers", "Browser") ? Qt::Checked : Qt::Unchecked); + mw.cBBoxWOPass->setCheckState(clientMgr.getBool("noPassword", "Browser") ? Qt::Checked : Qt::Unchecked); + + mw.tblServerBrowser->sortByColumn(clientMgr.getInt("sortByCol", "Browser"), + clientMgr.getBool("sortByColAscending", "Browser") ? Qt::AscendingOrder : Qt::DescendingOrder); + + loadClientSettings(mw); + switchMgr(); + loadServerSettings(mw); + switchMgr(); +} + +void SettingsMgr::saveBrowserSettings(Ui::MainWindow &mw) +{ + clientMgr.setInt("maxLatency", "Browser", mw.comboLatency->currentIndex()); + clientMgr.setString("gameMode", "Browser", mw.leGamemode->text().toStdString()); + clientMgr.setBool("notFull", "Browser", mw.cBoxNotFull->checkState() == Qt::Checked); + clientMgr.setBool("withPlayers", "Browser", mw.cBoxWithPlayers->checkState() == Qt::Checked); + clientMgr.setBool("noPassword", "Browser", mw.cBBoxWOPass->checkState() == Qt::Checked); + + clientMgr.setInt("sortByCol", "Browser", mw.tblServerBrowser->horizontalHeader()->sortIndicatorSection()); + clientMgr.setBool("sortByColAscending", "Browser", mw.tblServerBrowser->horizontalHeader()->sortIndicatorOrder() == Qt::AscendingOrder); + + saveClientSettings(mw); + + clientMgr.saveUser(clientCfg); + switchMgr(); + saveServerSettings(mw); + switchMgr(); +} + +void SettingsMgr::loadClientSettings(Ui::MainWindow &mw) +{ + mw.leClientAddress->setText(QString::fromStdString(clientMgr.getString("destinationAddress", "General"))); + mw.leClientPort->setText(QString::fromStdString(clientMgr.getString("port", "General"))); + mw.leClientPassword->setText(QString::fromStdString(clientMgr.getString("password", "General"))); + mw.combLoglevel->setCurrentIndex(clientMgr.getInt("loglevel", "General")); + + mw.leClientMAddress->setText(QString::fromStdString(clientMgr.getString("address", "Master"))); + mw.leClientMPort->setText(QString::fromStdString(clientMgr.getString("port", "Master"))); + + mw.pbChatKey->setText(QString::fromStdString(clientMgr.getString("keySay", "Chat"))); + mw.pbModeKey->setText(QString::fromStdString(clientMgr.getString("keyChatMode", "Chat"))); + + mw.sbPosX->setValue(clientMgr.getInt("x", "Chat")); + mw.sbPosY->setValue(clientMgr.getInt("y", "Chat")); + mw.sbPosW->setValue(clientMgr.getInt("w", "Chat")); + mw.sbPosH->setValue(clientMgr.getInt("h", "Chat")); + + mw.sbDelay->setValue(clientMgr.getFloat("delay", "Chat")); +} + +void SettingsMgr::saveClientSettings(Ui::MainWindow &mw) +{ + clientMgr.setString("destinationAddress", "General", mw.leClientAddress->text().toStdString()); + clientMgr.setString("port", "General", mw.leClientPort->text().toStdString()); + clientMgr.setString("password", "General", mw.leClientPassword->text().toStdString()); + clientMgr.setInt("loglevel", "General", mw.combLoglevel->currentIndex()); + + clientMgr.setString("address", "Master", mw.leClientMAddress->text().toStdString()); + clientMgr.setString("port", "Master", mw.leClientMPort->text().toStdString()); + + clientMgr.setString("keySay", "Chat", mw.pbChatKey->text().toStdString()); + clientMgr.setString("keyChatMode", "Chat", mw.pbModeKey->text().toStdString()); + + clientMgr.setInt("x", "Chat", mw.sbPosX->value()); + clientMgr.setInt("y", "Chat", mw.sbPosY->value()); + clientMgr.setInt("w", "Chat", mw.sbPosW->value()); + clientMgr.setInt("h", "Chat", mw.sbPosH->value()); + + clientMgr.setFloat("delay", "Chat", mw.sbDelay->value()); + +} + +void SettingsMgr::loadServerSettings(Ui::MainWindow &mw) +{ + mw.leServerAddress->setText(QString::fromStdString(serverMgr.getString("localAddress", "General"))); + mw.leServerPort->setText(QString::fromStdString(serverMgr.getString("port", "General"))); + mw.sbMaxPlayers->setValue(serverMgr.getInt("maximumPlayers", "General")); + mw.leHostname->setText(QString::fromStdString(serverMgr.getString("hostname", "General"))); + mw.combServerLoglevel->setCurrentIndex(serverMgr.getInt("logLevel", "General")); + mw.leServerPassword->setText(QString::fromStdString(serverMgr.getString("password", "General"))); + + mw.chbMSenabled->setCheckState(serverMgr.getBool("enabled", "MasterServer") ? Qt::Checked : Qt::Unchecked); + mw.leServerMaddress->setText(QString::fromStdString(serverMgr.getString("address", "MasterServer"))); + mw.leServerMPort->setText(QString::fromStdString(serverMgr.getString("port", "MasterServer"))); + mw.sbRate->setValue(serverMgr.getInt("rate", "MasterServer")); + + mw.leModulePath->setText(QString::fromStdString(serverMgr.getString("home", "Modules"))); + mw.chbAutosort->setCheckState(serverMgr.getBool("autoSort", "Modules") ? Qt::Checked : Qt::Unchecked); + +} + +void SettingsMgr::saveServerSettings(Ui::MainWindow &mw) +{ + serverMgr.setString("localAddress", "General", mw.leServerAddress->text().toStdString()); + serverMgr.setString("port", "General", mw.leServerPort->text().toStdString()); + serverMgr.setInt("maximumPlayers", "General", mw.sbMaxPlayers->value()); + serverMgr.setString("hostname", "General", mw.leHostname->text().toStdString()); + serverMgr.setInt("logLevel", "General", mw.combServerLoglevel->currentIndex()); + serverMgr.setString("password", "General", mw.leServerPassword->text().toStdString()); + + serverMgr.setBool("enabled", "MasterServer", mw.chbMSenabled->checkState() == Qt::Checked); + serverMgr.setString("address", "MasterServer", mw.leServerMaddress->text().toStdString()); + serverMgr.setString("port", "MasterServer", mw.leServerMPort->text().toStdString()); + serverMgr.setInt("rate", "MasterServer", mw.sbRate->value()); + + serverMgr.setString("home", "Modules", mw.leModulePath->text().toStdString()); + serverMgr.setBool("autoSort", "Modules", mw.chbAutosort->checkState() == Qt::Checked); + serverMgr.saveUser(serverCfg); +} + +void SettingsMgr::switchMgr() +{ + static Settings::CategorySettingValueMap saveUserSettings; + static Settings::CategorySettingValueMap saveDefaultSettings; + static Settings::CategorySettingVector saveChangedSettings; + static bool currentMgrIsClient = true; + + if(!currentMgrIsClient) + { + saveUserSettings.swap(clientMgr.mUserSettings); + saveDefaultSettings.swap(clientMgr.mDefaultSettings); + saveChangedSettings.swap(clientMgr.mChangedSettings); + + qDebug() << "Manager switched to Client config"; + } + else + { + saveUserSettings.swap(serverMgr.mUserSettings); + saveDefaultSettings.swap(serverMgr.mDefaultSettings); + saveChangedSettings.swap(serverMgr.mChangedSettings); + + qDebug() << "Manager switched to Server config"; + } + currentMgrIsClient = !currentMgrIsClient; +} diff --git a/apps/browser/Settings.hpp b/apps/browser/Settings.hpp new file mode 100644 index 000000000..0734f977c --- /dev/null +++ b/apps/browser/Settings.hpp @@ -0,0 +1,32 @@ +// +// Created by koncord on 31.03.18. +// + +#pragma once + +#include + +namespace Ui +{ + class MainWindow; +} + +class SettingsMgr +{ +public: + SettingsMgr(); + + void loadBrowserSettings(Ui::MainWindow &mw); + void saveBrowserSettings(Ui::MainWindow &mw); + + void loadClientSettings(Ui::MainWindow &mw); + void saveClientSettings(Ui::MainWindow &mw); + + void loadServerSettings(Ui::MainWindow &mw); + void saveServerSettings(Ui::MainWindow &mw); + +private: + Settings::Manager serverMgr, clientMgr; + std::string serverCfg, clientCfg; + void switchMgr(); +}; diff --git a/apps/browser/main.cpp b/apps/browser/main.cpp index 00c8bd633..5da624d83 100644 --- a/apps/browser/main.cpp +++ b/apps/browser/main.cpp @@ -4,42 +4,8 @@ #include #include "MainWindow.hpp" -std::string loadSettings (Settings::Manager & settings) -{ - Files::ConfigurationManager mCfgMgr; - // Create the settings manager and load default settings file - const std::string localdefault = (mCfgMgr.getLocalPath() / "tes3mp-client-default.cfg").string(); - const std::string globaldefault = (mCfgMgr.getGlobalPath() / "tes3mp-client-default.cfg").string(); - - // prefer local - if (boost::filesystem::exists(localdefault)) - settings.loadDefault(localdefault); - else if (boost::filesystem::exists(globaldefault)) - settings.loadDefault(globaldefault); - else - throw std::runtime_error ("No default settings file found! Make sure the file \"tes3mp-client-default.cfg\" was properly installed."); - - // load user settings if they exist - const std::string settingspath = (mCfgMgr.getUserConfigPath() / "tes3mp-client.cfg").string(); - if (boost::filesystem::exists(settingspath)) - settings.loadUser(settingspath); - - return settingspath; -} - int main(int argc, char *argv[]) { - Settings::Manager mgr; - - loadSettings(mgr); - - std::string addr = mgr.getString("address", "Master"); - int port = mgr.getInt("port", "Master"); - - // initialize resources, if needed - // Q_INIT_RESOURCE(resfile); - - QueryClient::Get().SetServer(addr, port); QApplication app(argc, argv); MainWindow d; diff --git a/files/tes3mp/ui/Main.ui b/files/tes3mp/ui/Main.ui index 5a370a7fe..981740018 100644 --- a/files/tes3mp/ui/Main.ui +++ b/files/tes3mp/ui/Main.ui @@ -66,6 +66,688 @@ + + + Client Settings + + + + + + + + Default Server + + + + + + + + + + Address + + + + + + + 127.0.0.1 + + + + + + + + + + + Port + + + + + + + 25565 + + + 5 + + + + + + + + + + + + + Password + + + + + + + + + + + + + + + Master Server + + + + + + + + Address + + + + + + + 127.0.0.1 + + + + + + + + + + + Port + + + + + + + 25565 + + + 5 + + + + + + + + + + + + + + Log Level + + + + + + + + Off + + + + + Fatal errors + + + + + General errors + + + + + Warnings + + + + + Info + + + + + Verbose + + + + + Trace + + + + + + + + + + + + Chat + + + + + + + + + + Key switch mode + + + + + + + F2 + + + + + + + + + + + Key + + + + + + + Y + + + + + + + + + + + Delay + + + + + + + 1 + + + 0.500000000000000 + + + 101.000000000000000 + + + 5.000000000000000 + + + + + + + + + + + Window position + + + + + + + + W + + + + + + + 65535 + + + + + + + + + + + X + + + + + + + 65535 + + + + + + + + + + + Y + + + + + + + 65535 + + + + + + + + + + + H + + + + + + + 65535 + + + + + + + + + + + + + + + + Server Settings + + + + + + General + + + + + + Address + + + + + + + + Broadcast address + + + + + + + 0.0.0.0 + + + + + + + + + + + Port + + + + + + + 25565 + + + 5 + + + + + + + + + + + + + + Hostname + + + + + + + My TES3MP server + + + + + + + + + + + Maximum players + + + + + + + 1 + + + 655535 + + + + + + + + + + + Password + + + + + + + + + + + + + + Log Level + + + + + + + + Off + + + + + Fatal errors + + + + + General errors + + + + + Warnings + + + + + Info + + + + + Verbose + + + + + Trace + + + + + + + + + + + + + true + + + Modules + + + + + + AutoSort + + + true + + + + + + + true + + + + + + + + + + + + + + Add + + + + + + + + + + + Up + + + + + + + Down + + + + + + + Remove + + + + + + + + + + + + + + Path + + + + + + + + + + ... + + + + + + + + + + + + Master Server + + + + + + Enabled + + + true + + + + + + + + + + + Address + + + + + + + 127.0.0.1 + + + + + + + + + + + Port + + + + + + + 25565 + + + 5 + + + + + + + + + + + + + Update rate + + + + + + + 500 + + + 10000 + + + 100 + + + 1000 + + + + + + + + + +