2012-07-11 18:30:00 +02:00
|
|
|
/* ============================================================
|
|
|
|
* GreaseMonkey plugin for QupZilla
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2012-2014 David Rosca <nowrep@gmail.com>
|
2012-07-11 18:30:00 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "gm_manager.h"
|
|
|
|
#include "gm_script.h"
|
|
|
|
#include "gm_downloader.h"
|
2013-06-02 19:09:41 +02:00
|
|
|
#include "gm_icon.h"
|
2015-10-08 23:32:18 +02:00
|
|
|
#include "gm_urlinterceptor.h"
|
2016-03-19 17:16:55 +01:00
|
|
|
#include "gm_addscriptdialog.h"
|
2012-07-11 18:30:00 +02:00
|
|
|
#include "settings/gm_settings.h"
|
|
|
|
|
2014-02-19 22:07:21 +01:00
|
|
|
#include "browserwindow.h"
|
2012-07-11 18:30:00 +02:00
|
|
|
#include "webpage.h"
|
2013-01-22 19:04:22 +01:00
|
|
|
#include "qztools.h"
|
2012-07-11 18:30:00 +02:00
|
|
|
#include "mainapplication.h"
|
2015-10-08 23:32:18 +02:00
|
|
|
#include "networkmanager.h"
|
2012-07-11 18:30:00 +02:00
|
|
|
#include "desktopnotificationsfactory.h"
|
|
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QSettings>
|
2013-06-02 19:09:41 +02:00
|
|
|
#include <QStatusBar>
|
2015-05-24 23:16:01 +02:00
|
|
|
#include <QWebEngineProfile>
|
|
|
|
#include <QWebEngineScriptCollection>
|
2012-07-11 18:30:00 +02:00
|
|
|
|
|
|
|
GM_Manager::GM_Manager(const QString &sPath, QObject* parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_settingsPath(sPath)
|
2015-10-08 23:32:18 +02:00
|
|
|
, m_interceptor(new GM_UrlInterceptor(this))
|
2012-07-11 18:30:00 +02:00
|
|
|
{
|
2015-10-08 23:32:18 +02:00
|
|
|
mApp->networkManager()->installUrlInterceptor(m_interceptor);
|
|
|
|
|
2012-07-11 18:30:00 +02:00
|
|
|
QTimer::singleShot(0, this, SLOT(load()));
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:32:18 +02:00
|
|
|
GM_Manager::~GM_Manager()
|
|
|
|
{
|
|
|
|
mApp->networkManager()->removeUrlInterceptor(m_interceptor);
|
|
|
|
}
|
|
|
|
|
2012-07-11 18:30:00 +02:00
|
|
|
void GM_Manager::showSettings(QWidget* parent)
|
|
|
|
{
|
2012-09-11 11:31:28 +02:00
|
|
|
if (!m_settings) {
|
|
|
|
m_settings = new GM_Settings(this, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_settings.data()->show();
|
|
|
|
m_settings.data()->raise();
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
|
|
|
|
2015-05-24 23:16:01 +02:00
|
|
|
void GM_Manager::downloadScript(const QUrl &url)
|
2012-07-11 18:30:00 +02:00
|
|
|
{
|
2015-10-08 23:32:18 +02:00
|
|
|
QMetaObject::invokeMethod(this, "doDownloadScript", Qt::QueuedConnection, Q_ARG(QUrl, url));
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString GM_Manager::settinsPath() const
|
|
|
|
{
|
|
|
|
return m_settingsPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GM_Manager::scriptsDirectory() const
|
|
|
|
{
|
2014-03-31 12:37:24 +02:00
|
|
|
return m_settingsPath + QL1S("/greasemonkey");
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString GM_Manager::requireScripts(const QStringList &urlList) const
|
|
|
|
{
|
2014-03-31 12:37:24 +02:00
|
|
|
QDir requiresDir(m_settingsPath + QL1S("/greasemonkey/requires"));
|
2012-07-11 18:30:00 +02:00
|
|
|
if (!requiresDir.exists() || urlList.isEmpty()) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2014-03-31 12:37:24 +02:00
|
|
|
QSettings settings(m_settingsPath + QL1S("/greasemonkey/requires/requires.ini"), QSettings::IniFormat);
|
2012-07-11 18:30:00 +02:00
|
|
|
settings.beginGroup("Files");
|
|
|
|
|
|
|
|
QString script;
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const QString &url, urlList) {
|
2012-07-11 18:30:00 +02:00
|
|
|
if (settings.contains(url)) {
|
2013-12-30 13:43:48 +01:00
|
|
|
const QString fileName = settings.value(url).toString();
|
2013-01-22 19:04:22 +01:00
|
|
|
script.append(QzTools::readAllFileContents(fileName).trimmed() + '\n');
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2015-05-25 11:34:22 +02:00
|
|
|
QString GM_Manager::bootstrapScript() const
|
|
|
|
{
|
|
|
|
return m_bootstrapScript;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GM_Manager::valuesScript() const
|
|
|
|
{
|
|
|
|
return m_valuesScript;
|
|
|
|
}
|
|
|
|
|
2012-09-11 11:31:28 +02:00
|
|
|
void GM_Manager::unloadPlugin()
|
2012-07-11 18:30:00 +02:00
|
|
|
{
|
2012-09-11 11:31:28 +02:00
|
|
|
// Save settings
|
2014-09-16 15:20:09 +02:00
|
|
|
QSettings settings(m_settingsPath + "/extensions.ini", QSettings::IniFormat);
|
2012-07-11 18:30:00 +02:00
|
|
|
settings.beginGroup("GreaseMonkey");
|
|
|
|
settings.setValue("disabledScripts", m_disabledScripts);
|
2012-09-11 11:31:28 +02:00
|
|
|
settings.endGroup();
|
|
|
|
|
|
|
|
delete m_settings.data();
|
2013-06-03 15:42:14 +02:00
|
|
|
|
|
|
|
// Remove icons from all windows
|
2014-02-19 22:07:21 +01:00
|
|
|
QHashIterator<BrowserWindow*, GM_Icon*> it(m_windows);
|
2013-06-03 15:42:14 +02:00
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
mainWindowDeleted(it.key());
|
|
|
|
}
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QList<GM_Script*> GM_Manager::allScripts() const
|
|
|
|
{
|
2015-05-24 23:16:01 +02:00
|
|
|
return m_scripts;
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GM_Manager::containsScript(const QString &fullName) const
|
|
|
|
{
|
2015-05-24 23:16:01 +02:00
|
|
|
foreach (GM_Script* script, m_scripts) {
|
2012-07-11 18:30:00 +02:00
|
|
|
if (fullName == script->fullName()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GM_Manager::enableScript(GM_Script* script)
|
|
|
|
{
|
|
|
|
script->setEnabled(true);
|
|
|
|
m_disabledScripts.removeOne(script->fullName());
|
2015-05-24 23:16:01 +02:00
|
|
|
|
2015-06-24 10:46:57 +02:00
|
|
|
QWebEngineScriptCollection *collection = mApp->webProfile()->scripts();
|
|
|
|
collection->insert(script->webScript());
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GM_Manager::disableScript(GM_Script* script)
|
|
|
|
{
|
|
|
|
script->setEnabled(false);
|
|
|
|
m_disabledScripts.append(script->fullName());
|
2015-05-24 23:16:01 +02:00
|
|
|
|
2015-06-24 10:46:57 +02:00
|
|
|
QWebEngineScriptCollection *collection = mApp->webProfile()->scripts();
|
|
|
|
collection->remove(collection->findScript(script->fullName()));
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GM_Manager::addScript(GM_Script* script)
|
|
|
|
{
|
2013-04-02 11:57:31 +02:00
|
|
|
if (!script || !script->isValid()) {
|
2012-07-11 18:30:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-24 23:16:01 +02:00
|
|
|
m_scripts.append(script);
|
2015-05-27 19:48:40 +02:00
|
|
|
connect(script, &GM_Script::scriptChanged, this, &GM_Manager::scriptChanged);
|
|
|
|
|
2015-06-24 10:46:57 +02:00
|
|
|
QWebEngineScriptCollection *collection = mApp->webProfile()->scripts();
|
|
|
|
collection->insert(script->webScript());
|
2012-07-11 18:30:00 +02:00
|
|
|
|
|
|
|
emit scriptsChanged();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-30 14:12:09 +01:00
|
|
|
bool GM_Manager::removeScript(GM_Script* script, bool removeFile)
|
2012-07-11 18:30:00 +02:00
|
|
|
{
|
|
|
|
if (!script) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-24 23:16:01 +02:00
|
|
|
m_scripts.removeOne(script);
|
2015-05-27 19:48:40 +02:00
|
|
|
|
2015-06-24 10:46:57 +02:00
|
|
|
QWebEngineScriptCollection *collection = mApp->webProfile()->scripts();
|
|
|
|
collection->remove(collection->findScript(script->fullName()));
|
2012-07-11 18:30:00 +02:00
|
|
|
|
|
|
|
m_disabledScripts.removeOne(script->fullName());
|
2013-01-30 14:12:09 +01:00
|
|
|
|
|
|
|
if (removeFile) {
|
|
|
|
QFile::remove(script->fileName());
|
|
|
|
delete script;
|
|
|
|
}
|
2012-07-11 18:30:00 +02:00
|
|
|
|
|
|
|
emit scriptsChanged();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GM_Manager::showNotification(const QString &message, const QString &title)
|
|
|
|
{
|
|
|
|
QPixmap icon(":gm/data/icon.png");
|
|
|
|
|
|
|
|
mApp->desktopNotifications()->showNotification(icon, title.isEmpty() ? tr("GreaseMonkey") : title, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GM_Manager::load()
|
|
|
|
{
|
2014-09-16 15:20:09 +02:00
|
|
|
QDir gmDir(m_settingsPath + QL1S("/greasemonkey"));
|
2012-07-11 18:30:00 +02:00
|
|
|
if (!gmDir.exists()) {
|
2014-09-16 15:20:09 +02:00
|
|
|
gmDir.mkdir(m_settingsPath + QL1S("/greasemonkey"));
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!gmDir.exists("requires")) {
|
|
|
|
gmDir.mkdir("requires");
|
|
|
|
}
|
|
|
|
|
2015-05-25 11:34:22 +02:00
|
|
|
m_bootstrapScript = QzTools::readAllFileContents(":gm/data/bootstrap.min.js");
|
|
|
|
m_valuesScript = QzTools::readAllFileContents(":gm/data/values.min.js");
|
|
|
|
|
2014-03-31 12:37:24 +02:00
|
|
|
QSettings settings(m_settingsPath + QL1S("/extensions.ini"), QSettings::IniFormat);
|
2012-07-11 18:30:00 +02:00
|
|
|
settings.beginGroup("GreaseMonkey");
|
|
|
|
m_disabledScripts = settings.value("disabledScripts", QStringList()).toStringList();
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const QString &fileName, gmDir.entryList(QStringList("*.js"), QDir::Files)) {
|
2013-12-30 13:43:48 +01:00
|
|
|
const QString absolutePath = gmDir.absoluteFilePath(fileName);
|
2012-07-11 18:30:00 +02:00
|
|
|
GM_Script* script = new GM_Script(this, absolutePath);
|
|
|
|
|
2013-02-24 10:48:23 +01:00
|
|
|
if (!script->isValid()) {
|
|
|
|
delete script;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-05-24 23:16:01 +02:00
|
|
|
m_scripts.append(script);
|
|
|
|
|
2012-07-11 18:30:00 +02:00
|
|
|
if (m_disabledScripts.contains(script->fullName())) {
|
|
|
|
script->setEnabled(false);
|
|
|
|
}
|
|
|
|
else {
|
2015-06-24 10:46:57 +02:00
|
|
|
mApp->webProfile()->scripts()->insert(script->webScript());
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 19:48:40 +02:00
|
|
|
void GM_Manager::scriptChanged()
|
|
|
|
{
|
|
|
|
GM_Script *script = qobject_cast<GM_Script*>(sender());
|
|
|
|
if (!script)
|
|
|
|
return;
|
|
|
|
|
2015-06-24 10:46:57 +02:00
|
|
|
QWebEngineScriptCollection *collection = mApp->webProfile()->scripts();
|
|
|
|
collection->remove(collection->findScript(script->fullName()));
|
|
|
|
collection->insert(script->webScript());
|
2015-05-27 19:48:40 +02:00
|
|
|
}
|
|
|
|
|
2015-10-08 23:32:18 +02:00
|
|
|
void GM_Manager::doDownloadScript(const QUrl &url)
|
|
|
|
{
|
2016-03-19 17:16:55 +01:00
|
|
|
GM_Downloader *downloader = new GM_Downloader(url, this);
|
|
|
|
connect(downloader, &GM_Downloader::finished, this, [=](const QString &fileName) {
|
|
|
|
bool deleteScript = true;
|
|
|
|
GM_Script *script = new GM_Script(this, fileName);
|
|
|
|
if (script->isValid()) {
|
|
|
|
if (!containsScript(script->fullName())) {
|
|
|
|
GM_AddScriptDialog dialog(this, script);
|
|
|
|
deleteScript = dialog.exec() != QDialog::Accepted;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
showNotification(tr("'%1' is already installed").arg(script->name()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (deleteScript) {
|
|
|
|
delete script;
|
|
|
|
QFile(fileName).remove();
|
|
|
|
}
|
|
|
|
});
|
2015-10-08 23:32:18 +02:00
|
|
|
}
|
|
|
|
|
2012-07-11 18:30:00 +02:00
|
|
|
bool GM_Manager::canRunOnScheme(const QString &scheme)
|
|
|
|
{
|
2012-09-04 11:29:47 +02:00
|
|
|
return (scheme == QLatin1String("http") || scheme == QLatin1String("https")
|
|
|
|
|| scheme == QLatin1String("data") || scheme == QLatin1String("ftp"));
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
2013-06-02 19:09:41 +02:00
|
|
|
|
2014-02-19 22:07:21 +01:00
|
|
|
void GM_Manager::mainWindowCreated(BrowserWindow* window)
|
2013-06-02 19:09:41 +02:00
|
|
|
{
|
|
|
|
GM_Icon* icon = new GM_Icon(this, window);
|
|
|
|
window->statusBar()->addPermanentWidget(icon);
|
|
|
|
m_windows[window] = icon;
|
|
|
|
}
|
|
|
|
|
2014-02-19 22:07:21 +01:00
|
|
|
void GM_Manager::mainWindowDeleted(BrowserWindow* window)
|
2013-06-02 19:09:41 +02:00
|
|
|
{
|
|
|
|
window->statusBar()->removeWidget(m_windows[window]);
|
|
|
|
delete m_windows[window];
|
2013-06-03 15:42:14 +02:00
|
|
|
m_windows.remove(window);
|
2013-06-02 19:09:41 +02:00
|
|
|
}
|