]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/dialogs/SaveDialog.cpp
Block all CSP requests. https://redmine.stoutner.com/issues/1193
[PrivacyBrowserPC.git] / src / dialogs / SaveDialog.cpp
index fff857d41e18679f2b61cf374152f8316ced57a8..e94f38a00c6ef8be592e876efcb29029ac552a39 100644 (file)
@@ -1,20 +1,20 @@
-/*
- * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
+/* SPDX-License-Identifier: GPL-3.0-or-later
+ * SPDX-FileCopyrightText: 2022-2024 Soren Stoutner <soren@stoutner.com>
  *
- * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
+ * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc/>.
  *
- * Privacy Browser PC is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
+ * 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 the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
  *
- * Privacy Browser PC is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
  *
- * You should have received a copy of the GNU General Public License
- * along with Privacy Browser PC.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License along with
+ * this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
 // Application headers.
@@ -30,7 +30,8 @@
 #include <QShortcut>
 #include <QStandardPaths>
 
-SaveDialog::SaveDialog(QWebEngineDownloadItem *downloadItemPointer)
+SaveDialog::SaveDialog(QWidget *parentWidgetPointer, QUrl &url, QString &mimeTypeString, int totalBytes, QString fileName, bool nativeDownloader) :
+                       QDialog(parentWidgetPointer), downloadUrl(url), suggestedFileName(fileName)
 {
     // Set the dialog window title.
     setWindowTitle(i18nc("The save dialog window title", "Save"));
@@ -45,6 +46,7 @@ SaveDialog::SaveDialog(QWebEngineDownloadItem *downloadItemPointer)
     saveDialogUi.setupUi(this);
 
     // Get handles for the widgets.
+    QGraphicsView *mimeGraphicsViewPointer = saveDialogUi.mimeGraphicsView;
     QLabel *urlLabelPointer = saveDialogUi.urlLabel;
     QLabel *filetypeLabelPointer = saveDialogUi.fileTypeLabel;
     QLabel *mimeTypeLabelPointer = saveDialogUi.mimeTypeLabel;
@@ -52,42 +54,67 @@ SaveDialog::SaveDialog(QWebEngineDownloadItem *downloadItemPointer)
     QDialogButtonBox *dialogButtonBoxPointer = saveDialogUi.dialogButtonBox;
     QPushButton *saveButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Save);
 
-    // Get the URL and the suggested file name.
-    downloadUrl = downloadItemPointer->url();
-    suggestedFileName = downloadItemPointer->suggestedFileName();
-    QString mimeType = downloadItemPointer->mimeType();
-
     // Get a MIME type database.
     QMimeDatabase mimeDatabase;
 
+    // Get the MIME type.
+    QMimeType mimeType = mimeDatabase.mimeTypeForName(mimeTypeString);
+
+    // Get the MIME type icon.
+    QIcon mimeTypeIcon = QIcon::fromTheme(mimeType.iconName());
+
+    // Create a graphics scene.
+    QGraphicsScene *mimeGraphicsScenePointer = new QGraphicsScene(this);
+
+    // Set the graphics scene.
+    mimeGraphicsViewPointer->setScene(mimeGraphicsScenePointer);
+
+    // Set the background of the graphics view to be the same as the window
+    mimeGraphicsViewPointer->setBackgroundRole(QPalette::Window);
+
+    // Add the MIME type icon to the scene.
+    mimeGraphicsScenePointer->addPixmap(mimeTypeIcon.pixmap(64, 64));
+
     // Populate the labels.
-    urlLabelPointer->setText("<b>" + downloadUrl.toString() + "</b>");
-    filetypeLabelPointer->setText("<b>" + mimeDatabase.mimeTypeForName(mimeType).comment() + "</b>");
-    mimeTypeLabelPointer->setText("<b>" + mimeType + "</b>");
+    urlLabelPointer->setText(QLatin1String("<b>") + downloadUrl.toString() + QLatin1String("</b>"));
+    filetypeLabelPointer->setText(QLatin1String("<b>") + mimeType.comment() + QLatin1String("</b>"));
+    mimeTypeLabelPointer->setText(QLatin1String("<b>") + mimeTypeString + QLatin1String("</b>"));
 
     // Populate the download size label.
-    if (downloadItemPointer->totalBytes() == -1)  // The file size is unknown.
+    if (totalBytes == -1)  // The file size is unknown.
         sizeLabelPointer->setText(i18nc("Unknown download file size.  The bold style should be preserved.", "<b>unknown</b>"));
     else  // The file size is known.  Format it according to the locale.
-        sizeLabelPointer->setText(ki18nc("Download file size.  The bold style should be preserved.", "<b>%1 bytes</b>").subs(downloadItemPointer->totalBytes()).toString());
-
-    // Connect the buttons.
-    connect(saveButtonPointer, SIGNAL(clicked()), this, SLOT(showFileDialog()));
-    connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
+        sizeLabelPointer->setText(ki18nc("Download file size.  The bold style should be preserved.", "<b>%1 bytes</b>").subs(totalBytes).toString());
 
     // Create the keyboard shortcuts.
     QShortcut *sShortcutPointer = new QShortcut(QKeySequence(i18nc("The save key shortcut.", "s")), this);
     QShortcut *cShortcutPointer = new QShortcut(QKeySequence(i18nc("The close key shortcut.", "c")), this);
-
-    // Connect the shortcuts.
-    connect(sShortcutPointer, SIGNAL(activated()), this, SLOT(showFileDialog()));
+    QShortcut *quitShortcutPointer = new QShortcut(QKeySequence::Quit, this);
+
+    // Connect the save buttons.
+    if (nativeDownloader)
+    {
+        // Show the file picker for the native download.
+        connect(saveButtonPointer, SIGNAL(clicked()), this, SLOT(showFilePicker()));
+        connect(sShortcutPointer, SIGNAL(activated()), this, SLOT(showFilePicker()));
+    }
+    else
+    {
+        // Use WebEngine's downloader.
+        connect(saveButtonPointer, SIGNAL(clicked()), this, SLOT(accept()));
+        connect(sShortcutPointer, SIGNAL(activated()), this, SLOT(accept()));
+    }
+
+    // Connect the cancel button.
+    connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
     connect(cShortcutPointer, SIGNAL(activated()), this, SLOT(reject()));
+    connect(quitShortcutPointer, SIGNAL(activated()), this, SLOT(reject()));
 }
 
-void SaveDialog::showFileDialog()
+void SaveDialog::showFilePicker()
 {
     // Show the file picker dialog.
-    emit showSaveFilePickerDialog(downloadUrl, suggestedFileName);
+    Q_EMIT useNativeKdeDownloader(downloadUrl, suggestedFileName);
 
     // Close the dialog.
     reject();