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>
|
|
|
|
#include <QSettings>
|
|
|
|
|
2011-08-26 22:02:35 +04:00
|
|
|
// public:
|
2011-08-28 16:05:34 +04:00
|
|
|
Config::Config()
|
2011-08-26 22:02:35 +04:00
|
|
|
{
|
|
|
|
resetToDefaults();
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
void Config::load()
|
2011-08-26 22:02:35 +04:00
|
|
|
{
|
|
|
|
resetToDefaults();
|
2011-08-26 22:03:06 +04:00
|
|
|
|
|
|
|
// TODO Should there be a system-wide config file?
|
|
|
|
//loadConfigFile(getSystemConfigFilePath());
|
|
|
|
|
|
|
|
// Load global config
|
2011-08-28 16:12:40 +04:00
|
|
|
loadIniFile(getGlobalConfigFilePath());
|
2011-08-26 22:03:06 +04:00
|
|
|
|
|
|
|
// Load local config (allows for overrides)
|
2011-08-28 16:12:40 +04:00
|
|
|
loadIniFile(getLocalConfigFilePath());
|
2011-08-26 22:03:06 +04:00
|
|
|
}
|
|
|
|
|
2011-08-28 16:12:40 +04:00
|
|
|
void Config::loadIniFile(const QString &filePath)
|
2011-08-26 22:03:06 +04:00
|
|
|
{
|
|
|
|
QSettings settings(filePath, QSettings::IniFormat);
|
|
|
|
QStringList keys = settings.allKeys();
|
|
|
|
QStringListIterator it(keys);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
QString key = it.next().trimmed();
|
|
|
|
QVariant value = settings.value(key);
|
|
|
|
|
|
|
|
if (key == "phantomjs/loadImages") {
|
|
|
|
setAutoLoadImages(asBool(value));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (key == "phantomjs/loadPlugins") {
|
|
|
|
setPluginsEnabled(asBool(value));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (key == "phantomjs/proxy") {
|
|
|
|
setProxy(asString(value));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (key == "phantomjs/diskCache") {
|
|
|
|
setDiskCacheEnabled(asBool(value));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (key == "phantomjs/ignoreSslErrors") {
|
|
|
|
setIgnoreSslErrors(asBool(value));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (key == "phantomjs/localAccessRemote") {
|
|
|
|
setLocalAccessRemote(asBool(value));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (key == "phantomjs/cookies") {
|
|
|
|
setCookieFile(asString(value));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (key == "phantomjs/outputEncoding") {
|
|
|
|
setOutputEncoding(asString(value));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (key == "phantomjs/scriptEncoding") {
|
|
|
|
setScriptEncoding(asString(value));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2011-08-26 22:02:35 +04:00
|
|
|
}
|
|
|
|
|
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-08-28 16:05:34 +04:00
|
|
|
QString Config::cookieFile() const
|
2011-08-26 22:02:51 +04:00
|
|
|
{
|
|
|
|
return m_cookieFile;
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
void Config::setCookieFile(const QString &value)
|
2011-08-26 22:02:51 +04:00
|
|
|
{
|
|
|
|
m_cookieFile = value;
|
|
|
|
}
|
|
|
|
|
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-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-08-28 16:05:34 +04:00
|
|
|
bool Config::localAccessRemote() const
|
2011-08-26 22:02:59 +04:00
|
|
|
{
|
|
|
|
return m_localAccessRemote;
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
void Config::setLocalAccessRemote(const bool value)
|
2011-08-26 22:02:59 +04:00
|
|
|
{
|
|
|
|
m_localAccessRemote = value;
|
|
|
|
}
|
|
|
|
|
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-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: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-08-26 22:02:51 +04:00
|
|
|
m_cookieFile = QString();
|
|
|
|
m_diskCacheEnabled = false;
|
|
|
|
m_ignoreSslErrors = false;
|
2011-08-26 22:02:59 +04:00
|
|
|
m_localAccessRemote = 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-08-26 22:02:43 +04:00
|
|
|
m_proxyHost = QString();
|
|
|
|
m_proxyPort = 1080;
|
2011-08-26 22:02:35 +04:00
|
|
|
m_scriptEncoding = "UTF-8";
|
|
|
|
}
|
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;
|
|
|
|
}
|
2011-08-26 22:03:06 +04:00
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
QString Config::getGlobalConfigFilePath() const
|
2011-08-26 22:03:06 +04:00
|
|
|
{
|
|
|
|
return joinPaths(QDir::homePath(), CONFIG_FILE_NAME);
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
QString Config::getLocalConfigFilePath() const
|
2011-08-26 22:03:06 +04:00
|
|
|
{
|
|
|
|
return joinPaths(QDir::currentPath(), CONFIG_FILE_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
// private: (static)
|
2011-08-28 16:05:34 +04:00
|
|
|
bool Config::asBool(const QVariant &value)
|
2011-08-26 22:03:06 +04:00
|
|
|
{
|
|
|
|
QString strVal = asString(value).toLower();
|
|
|
|
|
|
|
|
return strVal == "true" || strVal == "1" || strVal == "yes";
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
QString Config::asString(const QVariant &value)
|
2011-08-26 22:03:06 +04:00
|
|
|
{
|
|
|
|
return value.toString().trimmed();
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
QString Config::joinPaths(const QString &path1, const QString &path2)
|
2011-08-26 22:03:06 +04:00
|
|
|
{
|
|
|
|
QString joinedPath;
|
|
|
|
|
|
|
|
if (path1.isEmpty()) {
|
|
|
|
joinedPath = path2;
|
|
|
|
} else if (path2.isEmpty()) {
|
|
|
|
joinedPath = path1;
|
|
|
|
} else {
|
|
|
|
joinedPath = path1 + QDir::separator() + path2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return normalisePath(joinedPath);
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
QString Config::normalisePath(const QString &path)
|
2011-08-26 22:03:06 +04:00
|
|
|
{
|
|
|
|
return path.isEmpty() ? path : QDir::fromNativeSeparators(path);
|
|
|
|
}
|
|
|
|
|
2011-08-28 16:05:34 +04:00
|
|
|
const QString Config::CONFIG_FILE_NAME = ".phantomjsrc";
|