mirror of https://github.com/vitalif/phantomjs
Implement JSON config loader
parent
94c5e270cf
commit
26f7185bf6
|
@ -32,6 +32,10 @@
|
|||
|
||||
#include <QDir>
|
||||
#include <QSettings>
|
||||
#include <QWebPage>
|
||||
#include <QWebFrame>
|
||||
|
||||
#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";
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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));
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -4,5 +4,6 @@
|
|||
<file>coffee-script.js</file>
|
||||
<file>usage.txt</file>
|
||||
<file>bootstrap.js</file>
|
||||
<file>configurator.js</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
|
|
Loading…
Reference in New Issue