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

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_notification.h"
0019 #include "ui_gm_notification.h"
0020 #include "gm_manager.h"
0021 #include "gm_script.h"
0022 
0023 #include "iconprovider.h"
0024 
0025 #include <QFile>
0026 
0027 GM_Notification::GM_Notification(GM_Manager* manager, const QString &tmpfileName, const QString &fileName)
0028     : AnimatedWidget(AnimatedWidget::Down, 300, nullptr)
0029     , ui(new Ui::GM_Notification)
0030     , m_manager(manager)
0031     , m_tmpFileName(tmpfileName)
0032     , m_fileName(fileName)
0033 {
0034     setAutoFillBackground(true);
0035     ui->setupUi(widget());
0036 
0037     ui->iconLabel->setPixmap(QIcon(QSL(":gm/data/icon.svg")).pixmap(24));
0038     ui->close->setIcon(IconProvider::standardIcon(QStyle::SP_DialogCloseButton));
0039 
0040     connect(ui->install, &QAbstractButton::clicked, this, &GM_Notification::installScript);
0041     connect(ui->close, SIGNAL(clicked()), this, SLOT(hide()));
0042 
0043     startAnimation();
0044 }
0045 
0046 void GM_Notification::installScript()
0047 {
0048     bool success = false;
0049 
0050     GM_Script* script = nullptr;
0051     QString message = tr("Cannot install script");
0052 
0053     if (QFile::copy(m_tmpFileName, m_fileName)) {
0054         script = new GM_Script(m_manager, m_fileName);
0055         success = m_manager->addScript(script);
0056     }
0057 
0058     if (success) {
0059         message = tr("'%1' installed successfully").arg(script->name());
0060     }
0061 
0062     m_manager->showNotification(message);
0063 
0064     hide();
0065 }
0066 
0067 GM_Notification::~GM_Notification()
0068 {
0069     delete ui;
0070 }