2011-08-26 22:02:35 +04:00
|
|
|
/*
|
|
|
|
This file is part of the PhantomJS project from Ofi Labs.
|
|
|
|
|
|
|
|
Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
|
|
|
|
Copyright (C) 2011 execjosh, http://execjosh.blogspot.com
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the <organization> nor the
|
|
|
|
names of its contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
|
|
|
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
#include "config.h"
|
2011-08-26 22:02:35 +04:00
|
|
|
|
2011-08-26 22:03:06 +04:00
|
|
|
#include <QDir>
|
2011-08-29 19:43:41 +04:00
|
|
|
#include <QWebPage>
|
|
|
|
#include <QWebFrame>
|
2011-09-26 08:51:44 +04:00
|
|
|
#include <QNetworkProxy>
|
2011-08-29 19:43:41 +04:00
|
|
|
|
|
|
|
#include "terminal.h"
|
2011-08-26 22:03:06 +04:00
|
|
|
|
2011-08-26 22:02:35 +04:00
|
|
|
// public:
|
2011-08-29 18:23:01 +04:00
|
|
|
Config::Config(QObject *parent)
|
|
|
|
: QObject(parent)
|
2011-08-26 22:02:35 +04:00
|
|
|
{
|
|
|
|
resetToDefaults();
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:40:00 +04:00
|
|
|
void Config::init(const QStringList *const args)
|
2011-08-26 22:02:35 +04:00
|
|
|
{
|
|
|
|
resetToDefaults();
|
2011-08-26 22:03:06 +04:00
|
|
|
|
2011-08-28 16:40:00 +04:00
|
|
|
processArgs(*args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::processArgs(const QStringList &args)
|
|
|
|
{
|
|
|
|
QStringListIterator it(args);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
const QString &arg = it.next();
|
|
|
|
|
|
|
|
if (arg == "--version") {
|
|
|
|
setVersionFlag(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (arg == "--load-images=yes") {
|
|
|
|
setAutoLoadImages(true);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (arg == "--load-images=no") {
|
|
|
|
setAutoLoadImages(false);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (arg == "--load-plugins=yes") {
|
|
|
|
setPluginsEnabled(true);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (arg == "--load-plugins=no") {
|
|
|
|
setPluginsEnabled(false);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (arg == "--disk-cache=yes") {
|
|
|
|
setDiskCacheEnabled(true);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (arg == "--disk-cache=no") {
|
|
|
|
setDiskCacheEnabled(false);
|
|
|
|
continue;
|
|
|
|
}
|
2011-09-07 03:33:11 +04:00
|
|
|
if (arg.startsWith("--max-disk-cache-size=")) {
|
|
|
|
setMaxDiskCacheSize(arg.mid(arg.indexOf("=") + 1).trimmed().toInt());
|
|
|
|
continue;
|
|
|
|
}
|
2011-08-28 16:40:00 +04:00
|
|
|
if (arg == "--ignore-ssl-errors=yes") {
|
|
|
|
setIgnoreSslErrors(true);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (arg == "--ignore-ssl-errors=no") {
|
|
|
|
setIgnoreSslErrors(false);
|
|
|
|
continue;
|
|
|
|
}
|
2011-09-07 20:27:20 +04:00
|
|
|
if (arg == "--local-to-remote-url-access=no") {
|
|
|
|
setLocalToRemoteUrlAccessEnabled(false);
|
2011-08-28 16:40:00 +04:00
|
|
|
continue;
|
|
|
|
}
|
2011-09-07 20:27:20 +04:00
|
|
|
if (arg == "--local-to-remote-url-access=yes") {
|
|
|
|
setLocalToRemoteUrlAccessEnabled(true);
|
2011-08-28 16:40:00 +04:00
|
|
|
continue;
|
|
|
|
}
|
2011-09-26 08:51:44 +04:00
|
|
|
if (arg == "--proxy-type=") {
|
|
|
|
QString proxyType = arg.mid(13).trimmed();
|
|
|
|
|
|
|
|
if (proxyType == "socks5") {
|
|
|
|
setProxyType(QNetworkProxy::Socks5Proxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2011-08-28 16:40:00 +04:00
|
|
|
if (arg.startsWith("--proxy=")) {
|
|
|
|
setProxy(arg.mid(8).trimmed());
|
|
|
|
continue;
|
|
|
|
}
|
2011-09-15 20:40:22 +04:00
|
|
|
if (arg.startsWith("--cookies-file=")) {
|
|
|
|
setCookiesFile(arg.mid(15).trimmed());
|
2011-08-28 16:40:00 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (arg.startsWith("--output-encoding=")) {
|
|
|
|
setOutputEncoding(arg.mid(18).trimmed());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (arg.startsWith("--script-encoding=")) {
|
|
|
|
setScriptEncoding(arg.mid(18).trimmed());
|
|
|
|
continue;
|
|
|
|
}
|
2011-08-29 19:45:39 +04:00
|
|
|
if (arg.startsWith("--config=")) {
|
|
|
|
QString configPath = arg.mid(9).trimmed();
|
|
|
|
loadJsonFile(configPath);
|
|
|
|
continue;
|
|
|
|
}
|
2011-08-28 16:40:00 +04:00
|
|
|
if (arg.startsWith("--")) {
|
|
|
|
setUnknownOption(QString("Unknown option '%1'").arg(arg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are no more options at this point.
|
|
|
|
// The remaining arguments are available for the script.
|
|
|
|
|
|
|
|
m_scriptFile = arg;
|
|
|
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
m_scriptArgs += it.next();
|
|
|
|
}
|
|
|
|
}
|
2011-08-26 22:03:06 +04:00
|
|
|
}
|
|
|
|
|
2011-09-07 03:48:24 +04:00
|
|
|
static QString normalizePath(const QString &path)
|
|
|
|
{
|
|
|
|
return path.isEmpty() ? path : QDir::fromNativeSeparators(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// THIS METHOD ASSUMES THAT content IS *NEVER* NULL!
|
|
|
|
static bool 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;
|
|
|
|
}
|
|
|
|
|
2011-08-29 19:43:41 +04:00
|
|
|
void Config::loadJsonFile(const QString &filePath)
|
|
|
|
{
|
|
|
|
QString jsonConfig;
|
2011-09-07 03:48:24 +04:00
|
|
|
if (!readFile(normalizePath(filePath), &jsonConfig)) {
|
2011-08-29 19:43:41 +04:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
bool Config::autoLoadImages() const
|
2011-08-26 22:02:59 +04:00
|
|
|
{
|
|
|
|
return m_autoLoadImages;
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
void Config::setAutoLoadImages(const bool value)
|
2011-08-26 22:02:59 +04:00
|
|
|
{
|
|
|
|
m_autoLoadImages = value;
|
|
|
|
}
|
|
|
|
|
2011-09-15 20:40:22 +04:00
|
|
|
QString Config::cookiesFile() const
|
2011-08-26 22:02:51 +04:00
|
|
|
{
|
2011-09-15 20:40:22 +04:00
|
|
|
return m_cookiesFile;
|
2011-08-26 22:02:51 +04:00
|
|
|
}
|
|
|
|
|
2011-09-15 20:40:22 +04:00
|
|
|
void Config::setCookiesFile(const QString &value)
|
2011-08-26 22:02:51 +04:00
|
|
|
{
|
2011-09-15 20:40:22 +04:00
|
|
|
m_cookiesFile = value;
|
2011-08-26 22:02:51 +04:00
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
bool Config::diskCacheEnabled() const
|
2011-08-26 22:02:51 +04:00
|
|
|
{
|
|
|
|
return m_diskCacheEnabled;
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
void Config::setDiskCacheEnabled(const bool value)
|
2011-08-26 22:02:51 +04:00
|
|
|
{
|
|
|
|
m_diskCacheEnabled = value;
|
|
|
|
}
|
|
|
|
|
2011-09-07 03:33:11 +04:00
|
|
|
int Config::maxDiskCacheSize() const
|
|
|
|
{
|
|
|
|
return m_maxDiskCacheSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::setMaxDiskCacheSize(int maxDiskCacheSize)
|
|
|
|
{
|
|
|
|
m_maxDiskCacheSize = maxDiskCacheSize;
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
bool Config::ignoreSslErrors() const
|
2011-08-26 22:02:51 +04:00
|
|
|
{
|
|
|
|
return m_ignoreSslErrors;
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
void Config::setIgnoreSslErrors(const bool value)
|
2011-08-26 22:02:51 +04:00
|
|
|
{
|
|
|
|
m_ignoreSslErrors = value;
|
|
|
|
}
|
|
|
|
|
2011-09-07 20:27:20 +04:00
|
|
|
bool Config::localToRemoteUrlAccessEnabled() const
|
2011-08-26 22:02:59 +04:00
|
|
|
{
|
2011-09-07 20:27:20 +04:00
|
|
|
return m_localToRemoteUrlAccessEnabled;
|
2011-08-26 22:02:59 +04:00
|
|
|
}
|
|
|
|
|
2011-09-07 20:27:20 +04:00
|
|
|
void Config::setLocalToRemoteUrlAccessEnabled(const bool value)
|
2011-08-26 22:02:59 +04:00
|
|
|
{
|
2011-09-07 20:27:20 +04:00
|
|
|
m_localToRemoteUrlAccessEnabled = value;
|
2011-08-26 22:02:59 +04:00
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
QString Config::outputEncoding() const
|
2011-08-26 22:02:35 +04:00
|
|
|
{
|
|
|
|
return m_outputEncoding;
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
void Config::setOutputEncoding(const QString &value)
|
2011-08-26 22:02:35 +04:00
|
|
|
{
|
|
|
|
if (value.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_outputEncoding = value;
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
bool Config::pluginsEnabled() const
|
2011-08-26 22:02:59 +04:00
|
|
|
{
|
|
|
|
return m_pluginsEnabled;
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
void Config::setPluginsEnabled(const bool value)
|
2011-08-26 22:02:59 +04:00
|
|
|
{
|
|
|
|
m_pluginsEnabled = value;
|
|
|
|
}
|
|
|
|
|
2011-09-26 08:51:44 +04:00
|
|
|
QString Config::proxyType() const
|
|
|
|
{
|
|
|
|
return m_proxyType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::setProxyType(const QString value)
|
|
|
|
{
|
|
|
|
m_proxyType = value;
|
|
|
|
}
|
|
|
|
|
2011-08-29 18:23:01 +04:00
|
|
|
QString Config::proxy() const
|
|
|
|
{
|
|
|
|
return proxyHost() + ":" + proxyPort();
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
void Config::setProxy(const QString &value)
|
2011-08-26 22:02:43 +04:00
|
|
|
{
|
|
|
|
QString proxyHost = value;
|
|
|
|
int proxyPort = 1080;
|
|
|
|
|
|
|
|
if (proxyHost.lastIndexOf(':') > 0) {
|
|
|
|
bool ok = true;
|
|
|
|
int port = proxyHost.mid(proxyHost.lastIndexOf(':') + 1).toInt(&ok);
|
|
|
|
if (ok) {
|
|
|
|
proxyHost = proxyHost.left(proxyHost.lastIndexOf(':')).trimmed();
|
|
|
|
proxyPort = port;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setProxyHost(proxyHost);
|
|
|
|
setProxyPort(proxyPort);
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
QString Config::proxyHost() const
|
2011-08-26 22:02:43 +04:00
|
|
|
{
|
|
|
|
return m_proxyHost;
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
int Config::proxyPort() const
|
2011-08-26 22:02:43 +04:00
|
|
|
{
|
|
|
|
return m_proxyPort;
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:40:00 +04:00
|
|
|
QStringList Config::scriptArgs() const
|
|
|
|
{
|
|
|
|
return m_scriptArgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::setScriptArgs(const QStringList &value)
|
|
|
|
{
|
|
|
|
m_scriptArgs.clear();
|
|
|
|
|
|
|
|
QStringListIterator it(value);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
m_scriptArgs.append(it.next());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
QString Config::scriptEncoding() const
|
2011-08-26 22:02:35 +04:00
|
|
|
{
|
|
|
|
return m_scriptEncoding;
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
void Config::setScriptEncoding(const QString &value)
|
2011-08-26 22:02:35 +04:00
|
|
|
{
|
|
|
|
if (value.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_scriptEncoding = value;
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:40:00 +04:00
|
|
|
QString Config::scriptFile() const
|
|
|
|
{
|
|
|
|
return m_scriptFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::setScriptFile(const QString &value)
|
|
|
|
{
|
|
|
|
m_scriptFile = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Config::unknownOption() const
|
|
|
|
{
|
|
|
|
return m_unknownOption;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::setUnknownOption(const QString &value)
|
|
|
|
{
|
|
|
|
m_unknownOption = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Config::versionFlag() const
|
|
|
|
{
|
|
|
|
return m_versionFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Config::setVersionFlag(const bool value)
|
|
|
|
{
|
|
|
|
m_versionFlag = value;
|
|
|
|
}
|
|
|
|
|
2011-08-26 22:02:35 +04:00
|
|
|
// private:
|
2011-08-28 16:05:34 +04:00
|
|
|
void Config::resetToDefaults()
|
2011-08-26 22:02:35 +04:00
|
|
|
{
|
2011-08-26 22:02:59 +04:00
|
|
|
m_autoLoadImages = true;
|
2011-09-15 20:40:22 +04:00
|
|
|
m_cookiesFile = QString();
|
2011-08-26 22:02:51 +04:00
|
|
|
m_diskCacheEnabled = false;
|
2011-09-07 03:33:11 +04:00
|
|
|
m_maxDiskCacheSize = -1;
|
2011-08-26 22:02:51 +04:00
|
|
|
m_ignoreSslErrors = false;
|
2011-09-07 20:27:20 +04:00
|
|
|
m_localToRemoteUrlAccessEnabled = false;
|
2011-08-26 22:02:35 +04:00
|
|
|
m_outputEncoding = "UTF-8";
|
2011-08-26 22:02:59 +04:00
|
|
|
m_pluginsEnabled = false;
|
2011-09-26 08:51:44 +04:00
|
|
|
m_proxyType = QNetworkProxy::HttpProxy;
|
2011-08-28 16:40:00 +04:00
|
|
|
m_proxyHost.clear();
|
2011-08-26 22:02:43 +04:00
|
|
|
m_proxyPort = 1080;
|
2011-08-28 16:40:00 +04:00
|
|
|
m_scriptArgs.clear();
|
2011-08-26 22:02:35 +04:00
|
|
|
m_scriptEncoding = "UTF-8";
|
2011-08-28 16:40:00 +04:00
|
|
|
m_scriptFile.clear();
|
|
|
|
m_unknownOption.clear();
|
|
|
|
m_versionFlag = false;
|
2011-08-26 22:02:35 +04:00
|
|
|
}
|
2011-08-26 22:02:43 +04:00
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
void Config::setProxyHost(const QString &value)
|
2011-08-26 22:02:43 +04:00
|
|
|
{
|
|
|
|
m_proxyHost = value;
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
void Config::setProxyPort(const int value)
|
2011-08-26 22:02:43 +04:00
|
|
|
{
|
|
|
|
m_proxyPort = value;
|
|
|
|
}
|