1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 02:36:34 +01:00

GM_Script: Download icon and display it in settings

This commit is contained in:
David Rosca 2018-01-29 21:54:33 +01:00
parent 4283260504
commit f9feab7d9d
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
3 changed files with 30 additions and 2 deletions

View File

@ -22,12 +22,15 @@
#include "delayedfilewatcher.h"
#include "mainapplication.h"
#include "webpage.h"
#include "networkmanager.h"
#include <QFile>
#include <QTextStream>
#include <QStringList>
#include <QWebEngineScript>
#include <QCryptographicHash>
#include <QNetworkReply>
#include <QNetworkRequest>
GM_Script::GM_Script(GM_Manager* manager, const QString &filePath)
: QObject(manager)
@ -76,6 +79,11 @@ QString GM_Script::version() const
return m_version;
}
QIcon GM_Script::icon() const
{
return m_icon;
}
QUrl GM_Script::iconUrl() const
{
return m_iconUrl;
@ -183,6 +191,7 @@ void GM_Script::parseScript()
m_include.clear();
m_exclude.clear();
m_require.clear();
m_icon = QIcon();
m_iconUrl.clear();
m_downloadUrl.clear();
m_updateUrl.clear();
@ -295,6 +304,7 @@ void GM_Script::parseScript()
m_script = QSL("(function(){%1\n%2\n%3\n})();").arg(gmValues, m_manager->requireScripts(m_require), fileData);
m_valid = true;
downloadIcon();
downloadRequires();
}
@ -308,6 +318,19 @@ void GM_Script::reloadScript()
emit scriptChanged();
}
void GM_Script::downloadIcon()
{
if (m_iconUrl.isValid()) {
QNetworkReply *reply = mApp->networkManager()->get(QNetworkRequest(m_iconUrl));
connect(reply, &QNetworkReply::finished, this, [=]() {
reply->deleteLater();
if (reply->error() == QNetworkReply::NoError) {
m_icon = QPixmap::fromImage(QImage::fromData(reply->readAll()));
}
});
}
}
void GM_Script::downloadRequires()
{
for (const QString &url : qAsConst(m_require)) {

View File

@ -19,7 +19,7 @@
#define GM_SCRIPT_H
#include <QObject>
#include <QVector>
#include <QIcon>
#include <QUrl>
class QWebEngineScript;
@ -44,7 +44,9 @@ public:
QString description() const;
QString version() const;
QIcon icon() const;
QUrl iconUrl() const;
QUrl downloadUrl() const;
QUrl updateUrl() const;
@ -76,6 +78,7 @@ private slots:
private:
void parseScript();
void reloadScript();
void downloadIcon();
void downloadRequires();
GM_Manager* m_manager;
@ -90,6 +93,7 @@ private:
QStringList m_exclude;
QStringList m_require;
QIcon m_icon;
QUrl m_iconUrl;
QUrl m_downloadUrl;
QUrl m_updateUrl;

View File

@ -1,6 +1,6 @@
/* ============================================================
* GreaseMonkey plugin for Falkon
* Copyright (C) 2012-2017 David Rosca <nowrep@gmail.com>
* Copyright (C) 2012-2018 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -155,6 +155,7 @@ void GM_Settings::loadScripts()
foreach (GM_Script* script, m_manager->allScripts()) {
QListWidgetItem* item = new QListWidgetItem(ui->listWidget);
item->setText(script->name());
item->setIcon(script->icon());
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
item->setCheckState(script->isEnabled() ? Qt::Checked : Qt::Unchecked);
item->setData(Qt::UserRole + 10, QVariant::fromValue((void*)script));