From 26f091284566851e0eb6f851bc1e418f6ebd1fbb Mon Sep 17 00:00:00 2001 From: David Rosca Date: Thu, 28 May 2015 10:03:33 +0200 Subject: [PATCH] GM_Settings: Add button to create new user script --- .../GreaseMonkey/settings/gm_settings.cpp | 33 +++++++++++++++++++ .../GreaseMonkey/settings/gm_settings.h | 1 + .../GreaseMonkey/settings/gm_settings.ui | 7 ++++ 3 files changed, 41 insertions(+) diff --git a/src/plugins/GreaseMonkey/settings/gm_settings.cpp b/src/plugins/GreaseMonkey/settings/gm_settings.cpp index 13983bad1..9f6eb0002 100644 --- a/src/plugins/GreaseMonkey/settings/gm_settings.cpp +++ b/src/plugins/GreaseMonkey/settings/gm_settings.cpp @@ -20,11 +20,13 @@ #include "gm_settingsscriptinfo.h" #include "../gm_manager.h" #include "../gm_script.h" +#include "qztools.h" #include "mainapplication.h" #include #include +#include GM_Settings::GM_Settings(GM_Manager* manager, QWidget* parent) : QDialog(parent) @@ -40,6 +42,8 @@ GM_Settings::GM_Settings(GM_Manager* manager, QWidget* parent) this, SLOT(removeItem(QListWidgetItem*))); connect(ui->openDirectory, SIGNAL(clicked()), this, SLOT(openScriptsDirectory())); + connect(ui->newScript, SIGNAL(clicked()), + this, SLOT(newScript())); connect(ui->link, SIGNAL(clicked(QPoint)), this, SLOT(openUserJs())); connect(manager, SIGNAL(scriptsChanged()), @@ -101,6 +105,35 @@ void GM_Settings::openScriptsDirectory() QDesktopServices::openUrl(QUrl::fromLocalFile(m_manager->scriptsDirectory())); } +void GM_Settings::newScript() +{ + const QString name = QInputDialog::getText(this, tr("Add script"), tr("Choose name for script:")); + if (name.isEmpty()) + return; + + const QString script = QL1S("// ==UserScript== \n" + "// @name %1 \n" + "// @namespace qupzilla.com \n" + "// @description Script description \n" + "// @include * \n" + "// @version 1 \n" + "// ==/UserScript==\n" + "\n" + "// Your script implementation\n"); + + const QString fileName = QSL("%1/%2.user.js").arg(m_manager->scriptsDirectory(), QzTools::filterCharsFromFilename(name)); + + QFile file(QzTools::ensureUniqueFilename(fileName)); + file.open(QFile::WriteOnly); + file.write(script.arg(name).toUtf8()); + file.close(); + + GM_Script *gmScript = new GM_Script(m_manager, file.fileName()); + m_manager->addScript(gmScript); + + QDesktopServices::openUrl(QUrl::fromLocalFile(file.fileName())); +} + void GM_Settings::loadScripts() { disconnect(ui->listWidget, SIGNAL(itemChanged(QListWidgetItem*)), diff --git a/src/plugins/GreaseMonkey/settings/gm_settings.h b/src/plugins/GreaseMonkey/settings/gm_settings.h index 30903bc4f..519bc82b4 100644 --- a/src/plugins/GreaseMonkey/settings/gm_settings.h +++ b/src/plugins/GreaseMonkey/settings/gm_settings.h @@ -45,6 +45,7 @@ private slots: void itemChanged(QListWidgetItem* item); void openScriptsDirectory(); + void newScript(); void openUserJs(); void loadScripts(); diff --git a/src/plugins/GreaseMonkey/settings/gm_settings.ui b/src/plugins/GreaseMonkey/settings/gm_settings.ui index 5c42ce276..809c37fc5 100644 --- a/src/plugins/GreaseMonkey/settings/gm_settings.ui +++ b/src/plugins/GreaseMonkey/settings/gm_settings.ui @@ -197,6 +197,13 @@ + + + + New user script + + +