Save/Load favorites in browser

pull/133/head
Koncord 8 years ago
parent 777a570d93
commit ecf82a5df7

@ -5,8 +5,13 @@
#include "MainWindow.hpp"
#include "NetController.hpp"
#include "ServerInfoDialog.hpp"
#include "components/files/configurationmanager.hpp"
#include <qdebug.h>
#include <QInputDialog>
#include <QJsonObject>
#include <QJsonArray>
#include <QFile>
#include <QJsonDocument>
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();
}

@ -32,6 +32,7 @@ private:
Process::ProcessInvoker *mGameInvoker;
ServerModel *browser, *favorites;
QSortFilterProxyModel *proxyModel;
void loadFavorites();
};

@ -30,7 +30,6 @@ std::string loadSettings (Settings::Manager & settings)
int main(int argc, char *argv[])
{
Settings::Manager mgr;
Files::ConfigurationManager cfgMgr;
loadSettings(mgr);

Loading…
Cancel
Save