diff --git a/apps/browser/MainWindow.cpp b/apps/browser/MainWindow.cpp index 9b936a38f..b6a42413e 100644 --- a/apps/browser/MainWindow.cpp +++ b/apps/browser/MainWindow.cpp @@ -5,8 +5,13 @@ #include "MainWindow.hpp" #include "NetController.hpp" #include "ServerInfoDialog.hpp" +#include "components/files/configurationmanager.hpp" #include #include +#include +#include +#include +#include using namespace Process; @@ -34,6 +39,8 @@ MainWindow::MainWindow(QWidget *parent) connect(actionPlay, SIGNAL(triggered(bool)), this, SLOT(play())); connect(tblServerBrowser, SIGNAL(clicked(QModelIndex)), this, SLOT(serverSelected())); connect(tblFavorites, SIGNAL(clicked(QModelIndex)), this, SLOT(serverSelected())); + + loadFavorites(); } MainWindow::~MainWindow() @@ -58,7 +65,7 @@ void MainWindow::addServerByIP() QString text = QInputDialog::getText(this, tr("Add Server by address"), tr("Address:"), QLineEdit::Normal, "", &ok); if(ok && !text.isEmpty()) { - favorites->insertRows(0, 1); + favorites->insertRow(0); QModelIndex mi = favorites->index(0, ServerData::ADDR); favorites->setData(mi, text, Qt::EditRole); NetController::get()->updateInfo(favorites, mi); @@ -136,5 +143,47 @@ void MainWindow::serverSelected() void MainWindow::closeEvent(QCloseEvent *event) { + Files::ConfigurationManager cfgMgr; + QString cfgPath = (cfgMgr.getUserConfigPath() / "favorites.dat").c_str(); + + QJsonArray data; + for(auto server : favorites->myData) + data.push_back(server.addr); + + QFile file(cfgPath); + + if(!file.open(QIODevice::WriteOnly)) + { + qDebug() << "Cannot save " << cfgPath; + return; + } + file.write(QJsonDocument(data).toJson()); + file.close(); } + + +void MainWindow::loadFavorites() +{ + Files::ConfigurationManager cfgMgr; + QString cfgPath = (cfgMgr.getUserConfigPath() / "favorites.dat").c_str(); + + QFile file(cfgPath); + if(!file.open(QIODevice::ReadOnly)) + { + qDebug() << "Cannot open " << cfgPath; + return; + } + + QJsonDocument jsonDoc(QJsonDocument::fromJson(file.readAll())); + + for(auto server : jsonDoc.array()) + { + favorites->insertRows(0, 1); + QModelIndex mi = favorites->index(0, ServerData::ADDR); + favorites->setData(mi, server.toString(), Qt::EditRole); + NetController::get()->updateInfo(favorites, mi); + } + + file.close(); +} \ No newline at end of file diff --git a/apps/browser/MainWindow.hpp b/apps/browser/MainWindow.hpp index d349d77f1..df9623a85 100644 --- a/apps/browser/MainWindow.hpp +++ b/apps/browser/MainWindow.hpp @@ -32,6 +32,7 @@ private: Process::ProcessInvoker *mGameInvoker; ServerModel *browser, *favorites; QSortFilterProxyModel *proxyModel; + void loadFavorites(); }; diff --git a/apps/browser/main.cpp b/apps/browser/main.cpp index f1dc96fb2..16f9833c4 100644 --- a/apps/browser/main.cpp +++ b/apps/browser/main.cpp @@ -30,7 +30,6 @@ std::string loadSettings (Settings::Manager & settings) int main(int argc, char *argv[]) { Settings::Manager mgr; - Files::ConfigurationManager cfgMgr; loadSettings(mgr);