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

GreaseMonkey: Reload scripts on when source file changes.

Closes #729
This commit is contained in:
nowrep 2013-01-30 14:12:09 +01:00
parent e9b3442ef9
commit 949bfe42ed
13 changed files with 85 additions and 43 deletions

View File

@ -1,6 +1,6 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
* Copyright (C) 2010-2013 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
@ -163,7 +163,7 @@ void Plugins::loadAvailablePlugins()
QStringList dirs;
dirs << mApp->DATADIR + "plugins/"
#ifdef QZ_WS_X11
#if defined(QZ_WS_X11) && !defined(NO_SYSTEM_DATAPATH)
#ifdef USE_LIBPATH
<< USE_LIBPATH "qupzilla/"
#else

View File

@ -1,4 +1,4 @@
TARGET = AccessKeysNavigation
TARGET = $$qtLibraryTarget(AccessKeysNavigation)
os2: TARGET = AcKeyNav
SOURCES = \

View File

@ -1,4 +1,4 @@
TARGET = GreaseMonkey
TARGET = $$qtLibraryTarget(GreaseMonkey)
os2: TARGET = GreaseMo
INCLUDEPATH += . settings\

View File

@ -1,6 +1,6 @@
/* ============================================================
* GreaseMonkey plugin for QupZilla
* Copyright (C) 2012 David Rosca <nowrep@gmail.com>
* Copyright (C) 2012-2013 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
@ -34,10 +34,6 @@ class GM_Downloader : public QObject
public:
explicit GM_Downloader(const QNetworkRequest &request, GM_Manager* manager);
signals:
public slots:
private slots:
void scriptDownloaded();
void requireDownloaded();

View File

@ -1,6 +1,6 @@
/* ============================================================
* GreaseMonkey plugin for QupZilla
* Copyright (C) 2012 David Rosca <nowrep@gmail.com>
* Copyright (C) 2012-2013 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
@ -152,7 +152,7 @@ bool GM_Manager::addScript(GM_Script* script)
return true;
}
bool GM_Manager::removeScript(GM_Script* script)
bool GM_Manager::removeScript(GM_Script* script, bool removeFile)
{
if (!script) {
return false;
@ -166,8 +166,11 @@ bool GM_Manager::removeScript(GM_Script* script)
}
m_disabledScripts.removeOne(script->fullName());
QFile::remove(script->fileName());
delete script;
if (removeFile) {
QFile::remove(script->fileName());
delete script;
}
emit scriptsChanged();
return true;

View File

@ -1,6 +1,6 @@
/* ============================================================
* GreaseMonkey plugin for QupZilla
* Copyright (C) 2012 David Rosca <nowrep@gmail.com>
* Copyright (C) 2013 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
@ -50,7 +50,7 @@ public:
void disableScript(GM_Script* script);
bool addScript(GM_Script* script);
bool removeScript(GM_Script* script);
bool removeScript(GM_Script* script, bool removeFile = true);
void showNotification(const QString &message, const QString &title = QString());

View File

@ -1,6 +1,6 @@
/* ============================================================
* GreaseMonkey plugin for QupZilla
* Copyright (C) 2012 David Rosca <nowrep@gmail.com>
* Copyright (C) 2012-2013 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
@ -24,16 +24,18 @@
#include <QWebFrame>
#include <QDebug>
#include <QElapsedTimer>
#include <QFileSystemWatcher>
GM_Script::GM_Script(GM_Manager* manager, const QString &filePath)
: m_manager(manager)
, m_namespace("GreaseMonkeyNS")
, m_startAt(DocumentEnd)
: QObject(manager)
, m_manager(manager)
, m_fileWatcher(new QFileSystemWatcher(this))
, m_fileName(filePath)
, m_enabled(true)
, m_valid(false)
{
parseScript(filePath);
parseScript();
connect(m_fileWatcher, SIGNAL(fileChanged(QString)),
this, SLOT(watchedFileChanged(QString)));
}
bool GM_Script::isValid() const
@ -78,7 +80,7 @@ GM_Script::StartAt GM_Script::startAt() const
bool GM_Script::isEnabled() const
{
return m_enabled;
return m_valid && m_enabled;
}
void GM_Script::setEnabled(bool enable)
@ -120,7 +122,7 @@ QString GM_Script::fileName() const
bool GM_Script::match(const QString &urlString)
{
if (!m_enabled) {
if (!isEnabled()) {
return false;
}
@ -139,14 +141,39 @@ bool GM_Script::match(const QString &urlString)
return false;
}
void GM_Script::parseScript(const QString &filePath)
void GM_Script::watchedFileChanged(const QString &file)
{
QFile file(filePath);
if (m_fileName == file) {
parseScript();
m_manager->removeScript(this, false);
m_manager->addScript(this);
}
}
void GM_Script::parseScript()
{
m_name.clear();
m_namespace = "GreaseMonkeyNS";
m_description.clear();
m_version.clear();
m_include.clear();
m_exclude.clear();
m_downloadUrl.clear();
m_startAt = DocumentEnd;
m_enabled = true;
m_valid = false;
QFile file(m_fileName);
if (!file.open(QFile::ReadOnly)) {
qWarning() << "GreaseMonkey: Cannot open file for reading" << filePath;
qWarning() << "GreaseMonkey: Cannot open file for reading" << m_fileName;
return;
}
if (!m_fileWatcher->files().contains(m_fileName)) {
m_fileWatcher->addPath(m_fileName);
}
QString fileData = QString::fromUtf8(file.readAll());
QRegExp rx("// ==UserScript==(.*)// ==/UserScript==");
@ -154,7 +181,7 @@ void GM_Script::parseScript(const QString &filePath)
QString metadataBlock = rx.cap(1).trimmed();
if (metadataBlock.isEmpty()) {
qWarning() << "GreaseMonkey: File does not contain metadata block" << filePath;
qWarning() << "GreaseMonkey: File does not contain metadata block" << m_fileName;
return;
}

View File

@ -1,6 +1,6 @@
/* ============================================================
* GreaseMonkey plugin for QupZilla
* Copyright (C) 2012 David Rosca <nowrep@gmail.com>
* Copyright (C) 2012-2013 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
@ -25,14 +25,16 @@
#include <QUrl>
class QWebFrame;
class QFileSystemWatcher;
class GM_Manager;
class GM_UrlMatcher;
class GM_Script
class GM_Script : public QObject
{
Q_OBJECT
public:
GM_Script(GM_Manager* manager, const QString &filePath);
explicit GM_Script(GM_Manager* manager, const QString &filePath);
enum StartAt { DocumentStart, DocumentEnd };
@ -58,10 +60,14 @@ public:
bool match(const QString &urlString);
private slots:
void watchedFileChanged(const QString &file);
private:
void parseScript(const QString &filePath);
void parseScript();
GM_Manager* m_manager;
QFileSystemWatcher* m_fileWatcher;
QString m_name;
QString m_namespace;

View File

@ -1,6 +1,6 @@
/* ============================================================
* GreaseMonkey plugin for QupZilla
* Copyright (C) 2012 David Rosca <nowrep@gmail.com>
* Copyright (C) 2012-2013 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
@ -34,10 +34,16 @@ GM_Settings::GM_Settings(GM_Manager* manager, QWidget* parent)
setAttribute(Qt::WA_DeleteOnClose);
ui->setupUi(this);
connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(showItemInfo(QListWidgetItem*)));
connect(ui->listWidget, SIGNAL(removeItemRequested(QListWidgetItem*)), this, SLOT(removeItem(QListWidgetItem*)));
connect(ui->openDirectory, SIGNAL(clicked()), this, SLOT(openScriptsDirectory()));
connect(ui->link, SIGNAL(clicked(QPoint)), this, SLOT(openUserscripts()));
connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
this, SLOT(showItemInfo(QListWidgetItem*)));
connect(ui->listWidget, SIGNAL(removeItemRequested(QListWidgetItem*)),
this, SLOT(removeItem(QListWidgetItem*)));
connect(ui->openDirectory, SIGNAL(clicked()),
this, SLOT(openScriptsDirectory()));
connect(ui->link, SIGNAL(clicked(QPoint)),
this, SLOT(openUserscripts()));
connect(manager, SIGNAL(scriptsChanged()),
this, SLOT(loadScripts()));
loadScripts();
}
@ -97,7 +103,10 @@ void GM_Settings::openScriptsDirectory()
void GM_Settings::loadScripts()
{
disconnect(ui->listWidget, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(itemChanged(QListWidgetItem*)));
disconnect(ui->listWidget, SIGNAL(itemChanged(QListWidgetItem*)),
this, SLOT(itemChanged(QListWidgetItem*)));
ui->listWidget->clear();
foreach(GM_Script * script, m_manager->allScripts()) {
QListWidgetItem* item = new QListWidgetItem(ui->listWidget);

View File

@ -1,6 +1,6 @@
/* ============================================================
* GreaseMonkey plugin for QupZilla
* Copyright (C) 2012 David Rosca <nowrep@gmail.com>
* Copyright (C) 2012-2013 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
@ -47,8 +47,9 @@ private slots:
void openScriptsDirectory();
void openUserscripts();
private:
void loadScripts();
private:
inline GM_Script* getScript(QListWidgetItem* item);
Ui::GM_Settings* ui;

View File

@ -1,4 +1,4 @@
TARGET = MouseGestures
TARGET = $$qtLibraryTarget(MouseGestures)
os2: TARGET = MouseGes
INCLUDEPATH = 3rdparty

View File

@ -1,4 +1,4 @@
TARGET = PIM
TARGET = $$qtLibraryTarget(PIM)
os2: TARGET = PIM
SOURCES = \

View File

@ -3,7 +3,7 @@
# Project created by QtCreator 2011-02-13T10:23:13
#
#-------------------------------------------------
TARGET = TestPlugin
TARGET = $$qtLibraryTarget(TestPlugin)
# OS/2 allows only 8 chars in TARGET
os2: TARGET = TestPlug