File indexing completed on 2024-05-19 04:59:15

0001 /* ============================================================
0002 * GreaseMonkey plugin for Falkon
0003 * Copyright (C) 2012-2017 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "gm_addscriptdialog.h"
0019 #include "ui_gm_addscriptdialog.h"
0020 #include "gm_script.h"
0021 #include "gm_manager.h"
0022 #include "gm_notification.h"
0023 
0024 #include "mainapplication.h"
0025 #include "browserwindow.h"
0026 #include "tabbedwebview.h"
0027 #include "tabwidget.h"
0028 #include "datapaths.h"
0029 #include "qztools.h"
0030 
0031 #include <QFile>
0032 #include <QDir>
0033 
0034 GM_AddScriptDialog::GM_AddScriptDialog(GM_Manager* manager, GM_Script* script, QWidget* parent)
0035     : QDialog(parent)
0036     , ui(new Ui::GM_AddScriptDialog)
0037     , m_manager(manager)
0038     , m_script(script)
0039 {
0040     ui->setupUi(this);
0041     ui->iconLabel->setPixmap(QIcon(QSL(":gm/data/icon.svg")).pixmap(32));
0042 
0043     QString runsAt;
0044     QString dontRunsAt;
0045 
0046     const QStringList include = script->include();
0047     const QStringList exclude = script->exclude();
0048 
0049     if (!include.isEmpty()) {
0050         runsAt = tr("<p>runs at<br/><i>%1</i></p>").arg(include.join(QSL("<br/>")));
0051     }
0052 
0053     if (!exclude.isEmpty()) {
0054         dontRunsAt = tr("<p>does not run at<br/><i>%1</i></p>").arg(exclude.join(QSL("<br/>")));
0055     }
0056 
0057     QString scriptInfo = QSL("<b>%1</b> %2<br/>%3 %4 %5").arg(script->name(), script->version(), script->description(), runsAt, dontRunsAt);
0058     ui->textBrowser->setText(scriptInfo);
0059 
0060     connect(ui->showSource, &QAbstractButton::clicked, this, &GM_AddScriptDialog::showSource);
0061     connect(this, SIGNAL(accepted()), this, SLOT(accepted()));
0062 }
0063 
0064 void GM_AddScriptDialog::showSource()
0065 {
0066     BrowserWindow* qz = mApp->getWindow();
0067     if (!qz) {
0068         return;
0069     }
0070 
0071     const QString tmpFileName = QzTools::ensureUniqueFilename(DataPaths::path(DataPaths::Temp) + QSL("/tmp-userscript.js"));
0072 
0073     if (QFile::copy(m_script->fileName(), tmpFileName)) {
0074         int index = qz->tabWidget()->addView(QUrl::fromLocalFile(tmpFileName), Qz::NT_SelectedTabAtTheEnd);
0075         TabbedWebView* view = qz->weView(index);
0076         view->addNotification(new GM_Notification(m_manager, tmpFileName, m_script->fileName()));
0077     }
0078 
0079     reject();
0080 }
0081 
0082 void GM_AddScriptDialog::accepted()
0083 {
0084     QString message = tr("Cannot install script");
0085 
0086     if (m_manager->addScript(m_script)) {
0087         message = tr("'%1' installed successfully").arg(m_script->name());
0088     }
0089 
0090     m_manager->showNotification(message);
0091 }
0092 
0093 GM_AddScriptDialog::~GM_AddScriptDialog()
0094 {
0095     delete ui;
0096 }