File indexing completed on 2024-05-12 04:58:05

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-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 "downloadoptionsdialog.h"
0019 #include "ui_downloadoptionsdialog.h"
0020 #include "iconprovider.h"
0021 
0022 #include <QClipboard>
0023 #include <QMimeDatabase>
0024 #include <QWebEngineDownloadRequest>
0025 
0026 DownloadOptionsDialog::DownloadOptionsDialog(const QString &fileName, QWebEngineDownloadRequest *downloadItem, QWidget *parent)
0027     : QDialog(parent)
0028     , ui(new Ui::DownloadOptionsDialog)
0029     , m_downloadItem(downloadItem)
0030     , m_signalEmited(false)
0031 {
0032     ui->setupUi(this);
0033 
0034     ui->fileName->setText(QSL("<b>") + fileName + QSL("</b>"));
0035     ui->fromServer->setText(m_downloadItem->url().host());
0036 
0037     const QIcon fileIcon = IconProvider::instance()->standardIcon(QStyle::SP_FileIcon);
0038 
0039     QMimeDatabase db;
0040     const QMimeType mime = db.mimeTypeForName(downloadItem->mimeType());
0041     if (mime.isValid() && !mime.isDefault()) {
0042         ui->mimeName->setText(mime.comment());
0043         ui->iconLabel->setPixmap(QIcon::fromTheme(mime.iconName(), fileIcon).pixmap(22));
0044     } else {
0045         ui->mimeFrame->hide();
0046         ui->iconLabel->setPixmap(fileIcon.pixmap(22));
0047     }
0048 
0049     setWindowTitle(tr("Opening %1").arg(fileName));
0050 
0051     ui->buttonBox->setFocus();
0052 
0053     connect(ui->copyDownloadLink, &ClickableLabel::clicked, this, &DownloadOptionsDialog::copyDownloadLink);
0054     connect(this, &QDialog::finished, this, &DownloadOptionsDialog::emitDialogFinished);
0055 }
0056 
0057 void DownloadOptionsDialog::showExternalManagerOption(bool show)
0058 {
0059     ui->radioExternal->setVisible(show);
0060 }
0061 
0062 void DownloadOptionsDialog::showFromLine(bool show)
0063 {
0064     ui->fromFrame->setVisible(show);
0065 }
0066 
0067 void DownloadOptionsDialog::setLastDownloadOption(const DownloadManager::DownloadOption &option)
0068 {
0069     switch (option) {
0070     case DownloadManager::ExternalManager:
0071         if (!ui->radioExternal->isHidden()) {
0072             ui->radioExternal->setChecked(true);
0073             break;
0074         }
0075         // fallthrough
0076 
0077     case DownloadManager::OpenFile:
0078         ui->radioOpen->setChecked(true);
0079         break;
0080 
0081     case DownloadManager::SaveFile:
0082         ui->radioSave->setChecked(true);
0083         break;
0084 
0085     default:
0086         break;
0087     }
0088 }
0089 
0090 int DownloadOptionsDialog::exec()
0091 {
0092     int status = QDialog::exec();
0093 
0094     if (status != 0) {
0095         if (ui->radioOpen->isChecked()) {
0096             status =  1;
0097         }
0098         else if (ui->radioSave->isChecked()) {
0099             status =  2;
0100         }
0101         else if (ui->radioExternal->isChecked()) {
0102             status = 3;
0103         }
0104     }
0105 
0106     return status;
0107 }
0108 
0109 void DownloadOptionsDialog::copyDownloadLink()
0110 {
0111     QApplication::clipboard()->setText(m_downloadItem->url().toString());
0112     ui->copyDownloadLink->setText(tr("Download link copied."));
0113 }
0114 
0115 void DownloadOptionsDialog::emitDialogFinished(int status)
0116 {
0117     if (status != 0) {
0118         if (ui->radioOpen->isChecked()) {
0119             status =  1;
0120         }
0121         else if (ui->radioSave->isChecked()) {
0122             status =  2;
0123         }
0124         else if (ui->radioExternal->isChecked()) {
0125             status = 3;
0126         }
0127     }
0128 
0129     m_signalEmited = true;
0130     Q_EMIT dialogFinished(status);
0131 }
0132 
0133 DownloadOptionsDialog::~DownloadOptionsDialog()
0134 {
0135     if (!m_signalEmited) {
0136         Q_EMIT dialogFinished(-1);
0137     }
0138 
0139     delete ui;
0140 }