From c939c002905989b090836e9a0510da7c5f6afa73 Mon Sep 17 00:00:00 2001 From: Arto Jonsson Date: Mon, 19 Apr 2010 15:16:46 +0300 Subject: [PATCH] Use JSON to store the engine configurations --- INSTALL | 23 ++++++++--- projects/cli/src/cutechesscoreapp.cpp | 11 +++-- projects/gui/src/cutechessapp.cpp | 8 +++- projects/gui/src/mainwindow.cpp | 8 +++- projects/lib/3rdparty/README | 1 + projects/lib/lib.pri | 7 +++- projects/lib/lib.pro | 4 ++ projects/lib/src/enginemanager.cpp | 78 ++++++++++++++++++++++++----------- projects/lib/src/enginemanager.h | 4 +- 9 files changed, 108 insertions(+), 36 deletions(-) create mode 100644 projects/lib/3rdparty/README diff --git a/INSTALL b/INSTALL index c612502..3f073f4 100644 --- a/INSTALL +++ b/INSTALL @@ -2,12 +2,25 @@ Installation -Cute Chess requires Qt (at least version 4.5) and qmake, the Qt Makefile -generator. The current qmake project file doesn't have support for -installations. However the main executable has the necessary resources -built-in by default so it's the only thing you need to deploy. +Cute Chess requires Qt (at least version 4.5), qmake (the Qt Makefile +generator) and QJson. The current qmake project file doesn't have support for +installations. However the main executable has the necessary resources built-in +by default so it's the only thing you need to deploy. -First you need to build the Makefile in either release or debug mode. +First you need to make sure that QJson is installed. On Unix and derivates you +should be able to install it through package manager. On Windows the easiest +way is to drop the QJson source files to "3rdparty/qjson/" directory under +"projects/lib" and make a local installation of QJson. So, with 'cmake' program +installed in "3rdparty/qjson/" directory: + + cmake -DCMAKE_INSTALL_PREFIX=. + make + make install + +QJSon is now installed to the current directory where Cute Chess' build system +will pick it up. + +Next you need to build Cute Chess' Makefile in either release or debug mode. Release mode is for general program usage and offers the best performance. Debug mode is for debugging Cute Chess. diff --git a/projects/cli/src/cutechesscoreapp.cpp b/projects/cli/src/cutechesscoreapp.cpp index 362f014..0967925 100644 --- a/projects/cli/src/cutechesscoreapp.cpp +++ b/projects/cli/src/cutechesscoreapp.cpp @@ -16,11 +16,12 @@ */ #include "cutechesscoreapp.h" -#include -#include #include #include +#include #include +#include +#include CuteChessCoreApplication::CuteChessCoreApplication(int& argc, char* argv[]) @@ -38,7 +39,11 @@ CuteChessCoreApplication::CuteChessCoreApplication(int& argc, char* argv[]) qInstallMsgHandler(CuteChessCoreApplication::messageHandler); // Load the engines - engineManager()->loadEngines(); + // Note that we can't use QDesktopServices because of dependencies + QSettings settings; + QFileInfo fi(settings.fileName()); + engineManager()->loadEngines(fi.absolutePath() + + QLatin1String("/engines.json")); } CuteChessCoreApplication::~CuteChessCoreApplication() diff --git a/projects/gui/src/cutechessapp.cpp b/projects/gui/src/cutechessapp.cpp index 5a20ef7..b801177 100644 --- a/projects/gui/src/cutechessapp.cpp +++ b/projects/gui/src/cutechessapp.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include @@ -35,7 +36,12 @@ CuteChessApplication::CuteChessApplication(int& argc, char* argv[]) QSettings::setDefaultFormat(QSettings::IniFormat); // Load the engines - engineManager()->loadEngines(); + // We could use QDesktopServices but then this would be inconsistent with + // CuteChessCoreApp + QSettings settings; + QFileInfo fi(settings.fileName()); + engineManager()->loadEngines(fi.absolutePath() + + QLatin1String("/engines.json")); } CuteChessApplication::~CuteChessApplication() diff --git a/projects/gui/src/mainwindow.cpp b/projects/gui/src/mainwindow.cpp index 5dc0599..ba0470e 100644 --- a/projects/gui/src/mainwindow.cpp +++ b/projects/gui/src/mainwindow.cpp @@ -281,7 +281,13 @@ void MainWindow::manageEngines() if (dlg.exec() == QDialog::Accepted) { CuteChessApplication::instance()->engineManager()->setEngines(dlg.engines()); - CuteChessApplication::instance()->engineManager()->saveEngines(); + + // We could use QDesktopServices but then this would be inconsistent with + // CuteChessCoreApp + QSettings settings; + QFileInfo fi(settings.fileName()); + CuteChessApplication::instance()->engineManager()->saveEngines(fi.absolutePath() + + QLatin1String("/engines.json")); } } diff --git a/projects/lib/3rdparty/README b/projects/lib/3rdparty/README new file mode 100644 index 0000000..018d5c3 --- /dev/null +++ b/projects/lib/3rdparty/README @@ -0,0 +1 @@ +Third-party libraries used by Cute Chess should be placed here. diff --git a/projects/lib/lib.pri b/projects/lib/lib.pri index c75cc5a..00409ed 100644 --- a/projects/lib/lib.pri +++ b/projects/lib/lib.pri @@ -1,2 +1,7 @@ INCLUDEPATH += $$PWD/src -LIBS += -lchess -L$$PWD +LIBS += -lchess -lqjson -L$$PWD + +exists($$PWD/3rdparty/qjson) { + INCLUDEPATH += $$PWD/3rdparty/qjson/include + LIBS += -L$$PWD/3rdparty/qjson/lib +} diff --git a/projects/lib/lib.pro b/projects/lib/lib.pro index bccba27..8e56f21 100644 --- a/projects/lib/lib.pro +++ b/projects/lib/lib.pro @@ -9,6 +9,10 @@ win32 { DEFINES += LIB_EXPORT=\"\" } +exists($$PWD/3rdparty/qjson) { + INCLUDEPATH += $$PWD/3rdparty/qjson/include +} + include(src/src.pri) OBJECTS_DIR = .obj diff --git a/projects/lib/src/enginemanager.cpp b/projects/lib/src/enginemanager.cpp index ccadf69..d0f39df 100644 --- a/projects/lib/src/enginemanager.cpp +++ b/projects/lib/src/enginemanager.cpp @@ -17,6 +17,11 @@ #include "enginemanager.h" #include +#include +#include +#include +#include +#include EngineManager::EngineManager(QObject* parent) @@ -61,42 +66,69 @@ void EngineManager::setEngines(const QList& engines) emit enginesReset(); } -void EngineManager::loadEngines() +void EngineManager::loadEngines(const QString& fileName) { - QSettings settings; + QFile input(fileName); + QJson::Parser parser; + bool ok = false; - int size = settings.beginReadArray("engines"); - for (int i = 0; i < size; i++) + if (!QFile::exists(fileName)) + return; + + if (!input.open(QIODevice::ReadOnly | QIODevice::Text)) { - settings.setArrayIndex(i); + qWarning() << "cannot open engine configuration file:" << fileName; + return; + } + + const QVariantList engines = parser.parse(&input, &ok).toList(); + + if (!ok) + { + qWarning() << "bad engine configuration file line" << + parser.errorLine() << "in" << fileName << ":" + << parser.errorString(); + return; + } + + foreach(const QVariant& engine, engines) + { + const QVariantMap result = engine.toMap(); EngineConfiguration config; - config.setName(settings.value("name").toString()); - config.setCommand(settings.value("command").toString()); - config.setWorkingDirectory( - settings.value("working_directory").toString()); - config.setProtocol(settings.value("protocol").toString()); + config.setName(result["name"].toString()); + config.setCommand(result["command"].toString()); + config.setWorkingDirectory(result["workingDirectory"].toString()); + config.setProtocol(result["protocol"].toString()); addEngine(config); } - settings.endArray(); } -void EngineManager::saveEngines() +void EngineManager::saveEngines(const QString& fileName) { - QSettings settings; + QVariantList engines; + + foreach(const EngineConfiguration config, m_engines) + { + QVariantMap engine; + engine.insert("name", config.name()); + engine.insert("command", config.command()); + engine.insert("workingDirectory", config.workingDirectory()); + engine.insert("protocol", config.protocol()); + + engines << engine; + } + + QJson::Serializer serializer; + QFile output(fileName); - settings.beginWriteArray("engines"); - for (int i = 0; i < m_engines.size(); i++) + if (!output.open(QIODevice::WriteOnly | QIODevice::Text)) { - settings.setArrayIndex(i); - settings.setValue("name", m_engines.at(i).name()); - settings.setValue("command", m_engines.at(i).command()); - settings.setValue("working_directory", m_engines.at(i).workingDirectory()); - settings.setValue("protocol", m_engines.at(i).protocol()); + qWarning() << "cannot open engine configuration file:" << fileName; + return; } - settings.endArray(); - // Make sure that the settings are flushed to disk now - settings.sync(); + QTextStream out(&output); + out << serializer.serialize(engines); } diff --git a/projects/lib/src/enginemanager.h b/projects/lib/src/enginemanager.h index 81ad61b..be1df29 100644 --- a/projects/lib/src/enginemanager.h +++ b/projects/lib/src/enginemanager.h @@ -46,8 +46,8 @@ class LIB_EXPORT EngineManager : public QObject /*! Sets the available engines to \a engines. */ void setEngines(const QList& engines); - void loadEngines(); - void saveEngines(); + void loadEngines(const QString& fileName); + void saveEngines(const QString& fileName); signals: /*! -- 2.11.4.GIT