1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

[GreaseMonkey] Cleanup of GM_Downloader code

This commit is contained in:
nowrep 2014-03-31 12:37:24 +02:00
parent ed823d4d50
commit b73751f497
7 changed files with 64 additions and 46 deletions

View File

@ -212,7 +212,7 @@ PluginInterface* Plugins::initPlugin(PluginInterface::InitState state, PluginInt
return 0;
}
pluginInterface->init(state, DataPaths::currentProfilePath() + "/extensions/");
pluginInterface->init(state, DataPaths::currentProfilePath() + QL1S("/extensions/"));
if (!pluginInterface->testPlugin()) {
pluginInterface->unload();

View File

@ -23,14 +23,15 @@ EmptyNetworkReply::EmptyNetworkReply(QObject* parent)
: QNetworkReply(parent)
{
setOperation(QNetworkAccessManager::GetOperation);
setError(QNetworkReply::OperationCanceledError, "QupZilla:No Error");
setError(QNetworkReply::OperationCanceledError, QSL("QupZilla:No Error"));
open(QIODevice::ReadOnly);
QTimer::singleShot(0, this, SLOT(delayedFinish()));
}
void EmptyNetworkReply::delayedFinish()
{
emit finished();
}

View File

@ -48,6 +48,11 @@ QNetworkReply::NetworkError FollowRedirectReply::error() const
return m_reply->error();
}
QString FollowRedirectReply::errorString() const
{
return m_reply->errorString();
}
QByteArray FollowRedirectReply::readAll()
{
return m_reply->readAll();

View File

@ -40,6 +40,8 @@ public:
QUrl url() const;
QNetworkReply::NetworkError error() const;
QString errorString() const;
QByteArray readAll();
signals:

View File

@ -53,34 +53,39 @@ void GM_Downloader::scriptDownloaded()
return;
}
QByteArray response = QString::fromUtf8(m_reply->readAll()).toUtf8();
if (m_reply->error() != QNetworkReply::NoError) {
qWarning() << "GreaseMonkey: Cannot download script" << m_reply->errorString();
}
else {
const QByteArray response = QString::fromUtf8(m_reply->readAll()).toUtf8();
if (m_reply->error() == QNetworkReply::NoError && response.contains("// ==UserScript==")) {
const QString filePath = m_manager->scriptsDirectory() + QzTools::getFileNameFromUrl(m_reply->url());
m_fileName = QzTools::ensureUniqueFilename(filePath);
if (response.contains(QByteArray("// ==UserScript=="))) {
const QString filePath = m_manager->scriptsDirectory() + QzTools::getFileNameFromUrl(m_reply->url());
m_fileName = QzTools::ensureUniqueFilename(filePath);
QFile file(m_fileName);
QFile file(m_fileName);
if (!file.open(QFile::WriteOnly)) {
qWarning() << "GreaseMonkey: Cannot open file for writing" << m_fileName;
deleteLater();
return;
}
if (!file.open(QFile::WriteOnly)) {
qWarning() << "GreaseMonkey: Cannot open file for writing" << m_fileName;
deleteLater();
return;
}
file.write(response);
file.close();
file.write(response);
file.close();
QSettings settings(m_manager->settinsPath() + "greasemonkey/requires/requires.ini", QSettings::IniFormat);
settings.beginGroup("Files");
QSettings settings(m_manager->settinsPath() + QL1S("greasemonkey/requires/requires.ini"), QSettings::IniFormat);
settings.beginGroup("Files");
QzRegExp rx("@require(.*)\\n");
rx.setMinimal(true);
rx.indexIn(response);
QzRegExp rx("@require(.*)\\n");
rx.setMinimal(true);
rx.indexIn(response);
for (int i = 1; i <= rx.captureCount(); ++i) {
const QString url = rx.cap(i).trimmed();
if (!url.isEmpty() && !settings.contains(url)) {
m_requireUrls.append(QUrl(url));
for (int i = 1; i <= rx.captureCount(); ++i) {
const QString url = rx.cap(i).trimmed();
if (!url.isEmpty() && !settings.contains(url)) {
m_requireUrls.append(QUrl(url));
}
}
}
}
@ -98,26 +103,31 @@ void GM_Downloader::requireDownloaded()
return;
}
QByteArray response = QString::fromUtf8(m_reply->readAll()).toUtf8();
if (m_reply->error() != QNetworkReply::NoError) {
qWarning() << "GreaseMonkey: Cannot download require script" << m_reply->errorString();
}
else {
const QByteArray response = QString::fromUtf8(m_reply->readAll()).toUtf8();
if (m_reply->error() == QNetworkReply::NoError && !response.isEmpty()) {
const QString filePath = m_manager->settinsPath() + "/greasemonkey/requires/require.js";
const QString fileName = QzTools::ensureUniqueFilename(filePath, "%1");
if (!response.isEmpty()) {
const QString filePath = m_manager->settinsPath() + QL1S("/greasemonkey/requires/require.js");
const QString fileName = QzTools::ensureUniqueFilename(filePath, "%1");
QFile file(fileName);
QFile file(fileName);
if (!file.open(QFile::WriteOnly)) {
qWarning() << "GreaseMonkey: Cannot open file for writing" << fileName;
deleteLater();
return;
if (!file.open(QFile::WriteOnly)) {
qWarning() << "GreaseMonkey: Cannot open file for writing" << fileName;
deleteLater();
return;
}
file.write(response);
file.close();
QSettings settings(m_manager->settinsPath() + QL1S("/greasemonkey/requires/requires.ini"), QSettings::IniFormat);
settings.beginGroup("Files");
settings.setValue(m_reply->originalUrl().toString(), fileName);
}
file.write(response);
file.close();
QSettings settings(m_manager->settinsPath() + "greasemonkey/requires/requires.ini", QSettings::IniFormat);
settings.beginGroup("Files");
settings.setValue(m_reply->originalUrl().toString(), fileName);
}
m_reply->deleteLater();

View File

@ -64,17 +64,17 @@ QString GM_Manager::settinsPath() const
QString GM_Manager::scriptsDirectory() const
{
return m_settingsPath + "/greasemonkey/";
return m_settingsPath + QL1S("/greasemonkey");
}
QString GM_Manager::requireScripts(const QStringList &urlList) const
{
QDir requiresDir(m_settingsPath + "greasemonkey/requires");
QDir requiresDir(m_settingsPath + QL1S("/greasemonkey/requires"));
if (!requiresDir.exists() || urlList.isEmpty()) {
return QString();
}
QSettings settings(m_settingsPath + "greasemonkey/requires/requires.ini", QSettings::IniFormat);
QSettings settings(m_settingsPath + QL1S("/greasemonkey/requires/requires.ini"), QSettings::IniFormat);
settings.beginGroup("Files");
QString script;
@ -245,7 +245,7 @@ void GM_Manager::load()
gmDir.mkdir("requires");
}
QSettings settings(m_settingsPath + "extensions.ini", QSettings::IniFormat);
QSettings settings(m_settingsPath + QL1S("/extensions.ini"), QSettings::IniFormat);
settings.beginGroup("GreaseMonkey");
m_disabledScripts = settings.value("disabledScripts", QStringList()).toStringList();
@ -271,7 +271,7 @@ void GM_Manager::load()
}
m_bootstrap = QzTools::readAllFileContents(":gm/data/bootstrap.min.js");
m_jsObject->setSettingsFile(m_settingsPath + "extensions.ini");
m_jsObject->setSettingsFile(m_settingsPath + QL1S("/extensions.ini"));
}
bool GM_Manager::canRunOnScheme(const QString &scheme)

View File

@ -41,7 +41,7 @@ PluginSpec GM_Plugin::pluginSpec()
spec.name = "GreaseMonkey";
spec.info = "Userscripts for QupZilla";
spec.description = "Provides support for userscripts (www.userscripts.org)";
spec.version = "0.4.3";
spec.version = "0.4.4";
spec.author = "David Rosca <nowrep@gmail.com>";
spec.icon = QPixmap(":gm/data/icon.png");
spec.hasSettings = true;