]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/dialogs/SaveDialog.cpp
Enable downloading of files that require login cookies. https://redmine.stoutner...
[PrivacyBrowserPC.git] / src / dialogs / SaveDialog.cpp
index 281cc77c79a4c4ff97e606e4ce50402159c38242..39d2b1855bbb9d83a3dcd25909c7fa9717d9149d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
+ * Copyright 2022 Soren Stoutner <soren@stoutner.com>.
  *
  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
  *
@@ -30,7 +30,7 @@
 #include <QShortcut>
 #include <QStandardPaths>
 
-SaveDialog::SaveDialog(QWebEngineDownloadItem *downloadItemPointer)
+SaveDialog::SaveDialog(QUrl &url, QString &mimeTypeString, int totalBytes, QString fileName, bool nativeDownloader): downloadUrl(url), suggestedFileName(fileName)
 {
     // Set the dialog window title.
     setWindowTitle(i18nc("The save dialog window title", "Save"));
@@ -53,11 +53,6 @@ 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 mimeTypeString = downloadItemPointer->mimeType();
-
     // Get a MIME type database.
     QMimeDatabase mimeDatabase;
 
@@ -85,29 +80,40 @@ SaveDialog::SaveDialog(QWebEngineDownloadItem *downloadItemPointer)
     mimeTypeLabelPointer->setText("<b>" + mimeTypeString + "</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()));
+    // 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()));
 }
 
-void SaveDialog::showFileDialog()
+void SaveDialog::showFilePicker()
 {
     // Show the file picker dialog.
-    emit showSaveFilePickerDialog(downloadUrl, suggestedFileName);
+    emit useNativeDownloader(downloadUrl, suggestedFileName);
 
     // Close the dialog.
     reject();
 }
+