1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-13 10:32:11 +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; return 0;
} }
pluginInterface->init(state, DataPaths::currentProfilePath() + "/extensions/"); pluginInterface->init(state, DataPaths::currentProfilePath() + QL1S("/extensions/"));
if (!pluginInterface->testPlugin()) { if (!pluginInterface->testPlugin()) {
pluginInterface->unload(); pluginInterface->unload();

View File

@ -23,14 +23,15 @@ EmptyNetworkReply::EmptyNetworkReply(QObject* parent)
: QNetworkReply(parent) : QNetworkReply(parent)
{ {
setOperation(QNetworkAccessManager::GetOperation); 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())); QTimer::singleShot(0, this, SLOT(delayedFinish()));
} }
void EmptyNetworkReply::delayedFinish() void EmptyNetworkReply::delayedFinish()
{ {
emit finished(); emit finished();
} }

View File

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

View File

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

View File

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

View File

@ -64,17 +64,17 @@ QString GM_Manager::settinsPath() const
QString GM_Manager::scriptsDirectory() const QString GM_Manager::scriptsDirectory() const
{ {
return m_settingsPath + "/greasemonkey/"; return m_settingsPath + QL1S("/greasemonkey");
} }
QString GM_Manager::requireScripts(const QStringList &urlList) const 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()) { if (!requiresDir.exists() || urlList.isEmpty()) {
return QString(); 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"); settings.beginGroup("Files");
QString script; QString script;
@ -245,7 +245,7 @@ void GM_Manager::load()
gmDir.mkdir("requires"); gmDir.mkdir("requires");
} }
QSettings settings(m_settingsPath + "extensions.ini", QSettings::IniFormat); QSettings settings(m_settingsPath + QL1S("/extensions.ini"), QSettings::IniFormat);
settings.beginGroup("GreaseMonkey"); settings.beginGroup("GreaseMonkey");
m_disabledScripts = settings.value("disabledScripts", QStringList()).toStringList(); 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_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) bool GM_Manager::canRunOnScheme(const QString &scheme)

View File

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