diff --git a/src/config.cpp b/src/config.cpp index b4f35590..fd8c0a26 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -32,6 +32,10 @@ #include #include +#include +#include + +#include "terminal.h" // public: Config::Config(QObject *parent) @@ -186,6 +190,38 @@ void Config::loadIniFile(const QString &filePath) } } +void Config::loadJsonFile(const QString &filePath) +{ + QString jsonConfig; + if (!readFile(normalisePath(filePath), &jsonConfig)) { + Terminal::instance()->cerr("Unable to open config: \"" + filePath + "\""); + return; + } else if (jsonConfig.isEmpty()) { + return; + } else if (!jsonConfig.startsWith('{') || !jsonConfig.endsWith('}')) { + Terminal::instance()->cerr("Config file MUST be in JSON format!"); + return; + } + + // Load configurator + QString configurator; + if (!readFile(":/configurator.js", &configurator)) { + Terminal::instance()->cerr("Unable to load JSON configurator!"); + return; + } else if (configurator.isEmpty()) { + Terminal::instance()->cerr("Unable to set-up JSON configurator!"); + return; + } + + QWebPage webPage; + + // Add the config object + webPage.mainFrame()->addToJavaScriptWindowObject("config", this); + + // Apply the settings + webPage.mainFrame()->evaluateJavaScript(configurator.arg(jsonConfig)); +} + bool Config::autoLoadImages() const { return m_autoLoadImages; @@ -424,4 +460,23 @@ QString Config::normalisePath(const QString &path) return path.isEmpty() ? path : QDir::fromNativeSeparators(path); } +// THIS METHOD ASSUMES THAT content IS *NEVER* NULL! +bool Config::readFile(const QString &path, QString *const content) +{ + // Ensure empty content + content->clear(); + + // Check existence and try to open as text + QFile file(path); + if (!file.exists() || !file.open(QFile::ReadOnly | QFile::Text)) { + return false; + } + + content->append(QString::fromUtf8(file.readAll()).trimmed()); + + file.close(); + + return true; +} + const QString Config::CONFIG_FILE_NAME = ".phantomjsrc"; diff --git a/src/config.h b/src/config.h index 110ba16c..8b7c4090 100644 --- a/src/config.h +++ b/src/config.h @@ -53,6 +53,7 @@ public: void init(const QStringList *const args); void processArgs(const QStringList &args); void loadIniFile(const QString &filePath); + void loadJsonFile(const QString &filePath); bool autoLoadImages() const; void setAutoLoadImages(const bool value); @@ -122,6 +123,7 @@ private: static QString asString(const QVariant &value); static QString joinPaths(const QString &path1, const QString &path2); static QString normalisePath(const QString &path); + static bool readFile(const QString &path, QString *const content); static const QString CONFIG_FILE_NAME; }; diff --git a/src/configurator.js b/src/configurator.js new file mode 100644 index 00000000..36174adb --- /dev/null +++ b/src/configurator.js @@ -0,0 +1,17 @@ +(function (opts) { + var i; + + opts = opts || {}; + + if (typeof opts !== 'object') { + return; + } + + for (i in opts) { + if (opts.hasOwnProperty(i) && typeof opts[i] !== 'undefined') { + config[i] = opts[i]; + } + } + + return null; +})((%1)); diff --git a/src/phantomjs.pro b/src/phantomjs.pro index 8a520a94..12581a48 100644 --- a/src/phantomjs.pro +++ b/src/phantomjs.pro @@ -35,7 +35,8 @@ SOURCES += phantom.cpp \ encoding.cpp \ config.cpp -OTHER_FILES = bootstrap.js usage.txt +OTHER_FILES = bootstrap.js usage.txt \ + configurator.js include(gif/gif.pri) diff --git a/src/phantomjs.qrc b/src/phantomjs.qrc index 0669a16c..820461f7 100644 --- a/src/phantomjs.qrc +++ b/src/phantomjs.qrc @@ -4,5 +4,6 @@ coffee-script.js usage.txt bootstrap.js + configurator.js