rssguard/src/core/webfactory.cpp
Martin Rotter a3a2f38c3e hmmmm.
2014-03-14 11:16:43 +01:00

163 lines
4.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "core/webfactory.h"
#include "core/defs.h"
#include "core/settings.h"
#include <QApplication>
#include <QRegExp>
#include <QWebSettings>
#include <QProcess>
#include <QUrl>
#include <QDesktopServices>
QPointer<WebFactory> WebFactory::s_instance;
WebFactory::WebFactory(QObject *parent) : QObject(parent) {
m_globalSettings = QWebSettings::globalSettings();
}
WebFactory::~WebFactory() {
qDebug("Destroying WebFactory instance.");
}
void WebFactory::loadState() {
Settings *settings = Settings::instance();
switchJavascript(settings->value(APP_CFG_BROWSER, "enable_javascript", true).toBool(),
false);
switchImages(settings->value(APP_CFG_BROWSER, "enable_images", true).toBool(),
false);
switchPlugins(settings->value(APP_CFG_BROWSER, "enable_plugins", false).toBool(),
false);
}
bool WebFactory::openUrlInExternalBrowser(const QString &url) {
if (Settings::instance()->value(APP_CFG_BROWSER,
"custom_external_browser",
false).toBool()) {
QString browser = Settings::instance()->value(APP_CFG_BROWSER,
"external_browser_executable").toString();
QString arguments = Settings::instance()->value(APP_CFG_BROWSER,
"external_browser_arguments",
"%1").toString();
return QProcess::startDetached(browser, QStringList() << arguments.arg(url));
}
else {
return QDesktopServices::openUrl(url);
}
}
void WebFactory::switchJavascript(bool enable, bool save_settings) {
if (save_settings) {
Settings::instance()->setValue(APP_CFG_BROWSER,
"enable_javascript",
enable);
}
m_globalSettings->setAttribute(QWebSettings::JavascriptEnabled, enable);
emit javascriptSwitched(enable);
}
void WebFactory::switchPlugins(bool enable, bool save_settings) {
if (save_settings) {
Settings::instance()->setValue(APP_CFG_BROWSER,
"enable_plugins",
enable);
}
m_globalSettings->setAttribute(QWebSettings::PluginsEnabled, enable);
emit pluginsSwitched(enable);
}
void WebFactory::switchImages(bool enable, bool save_settings) {
if (save_settings) {
Settings::instance()->setValue(APP_CFG_BROWSER,
"enable_images",
enable);
}
m_globalSettings->setAttribute(QWebSettings::AutoLoadImages, enable);
emit imagesLoadingSwitched(enable);
}
WebFactory *WebFactory::instance() {
if (s_instance.isNull()) {
s_instance = new WebFactory(qApp);
}
return s_instance;
}
bool WebFactory::javascriptEnabled() const {
return m_globalSettings->testAttribute(QWebSettings::JavascriptEnabled);
}
bool WebFactory::pluginsEnabled() const {
return m_globalSettings->testAttribute(QWebSettings::PluginsEnabled);
}
bool WebFactory::autoloadImages() const {
return m_globalSettings->testAttribute(QWebSettings::AutoLoadImages);
}
QString WebFactory::stripTags(QString text) {
return text.remove(QRegExp("<[^>]*>"));
}
QString WebFactory::escapeHtml(const QString &html) {
static QMap<QString, QString> escape_sequences = generetaEscapes();
QList<QString> keys = escape_sequences.uniqueKeys();
QString output = html;
foreach (const QString &key, keys) {
output.replace(key, escape_sequences.value(key));
}
return output;
}
QString WebFactory::deEscapeHtml(const QString &text) {
static QMap<QString, QString> deescape_sequences = generateDeescapes();
QList<QString> keys = deescape_sequences.uniqueKeys();
QString output = text;
foreach (const QString &key, keys) {
output.replace(key, deescape_sequences.value(key));
}
return output;
}
QMap<QString, QString> WebFactory::generetaEscapes() {
QMap<QString, QString> sequences;
sequences["&lt;"] = '<';
sequences["&gt;"] = '>';
sequences["&amp;"] = '&';
sequences["&quot;"] = '\"';
sequences["&nbsp;"] = ' ';
sequences["&plusmn;"] = "±";
sequences["&times;"] = "×";
sequences["&#039;"] = '\'';
return sequences;
}
QMap<QString, QString> WebFactory::generateDeescapes() {
QMap<QString, QString> sequences;
sequences["<"] = "&lt;";
sequences[">"] = "&gt;";
sequences["&"] = "&amp;";
sequences["\""] = "&quot;";
sequences["±"] = "&plusmn;";
sequences["×"] = "&times;";
sequences["\'"] = "&#039;";
return sequences;
}