2 * Copyright 2022-2024 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
6 * Privacy Browser PC is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser PC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser PC. If not, see <http://www.gnu.org/licenses/>.
20 // Application headers.
21 #include "SaveDialog.h"
22 #include "ui_SaveDialog.h"
24 // KDE Frameworks headers.
25 #include <KLocalizedString>
27 // Qt toolkit headers.
28 #include <QMimeDatabase>
29 #include <QPushButton>
31 #include <QStandardPaths>
33 SaveDialog::SaveDialog(QWidget *parentWidgetPointer, QUrl &url, QString &mimeTypeString, int totalBytes, QString fileName, bool nativeDownloader) :
34 QDialog(parentWidgetPointer), downloadUrl(url), suggestedFileName(fileName)
36 // Set the dialog window title.
37 setWindowTitle(i18nc("The save dialog window title", "Save"));
39 // Set the window modality.
40 setWindowModality(Qt::WindowModality::ApplicationModal);
42 // Instantiate the save dialog UI.
43 Ui::SaveDialog saveDialogUi;
46 saveDialogUi.setupUi(this);
48 // Get handles for the widgets.
49 QGraphicsView *mimeGraphicsViewPointer = saveDialogUi.mimeGraphicsView;
50 QLabel *urlLabelPointer = saveDialogUi.urlLabel;
51 QLabel *filetypeLabelPointer = saveDialogUi.fileTypeLabel;
52 QLabel *mimeTypeLabelPointer = saveDialogUi.mimeTypeLabel;
53 QLabel *sizeLabelPointer = saveDialogUi.sizeLabel;
54 QDialogButtonBox *dialogButtonBoxPointer = saveDialogUi.dialogButtonBox;
55 QPushButton *saveButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Save);
57 // Get a MIME type database.
58 QMimeDatabase mimeDatabase;
61 QMimeType mimeType = mimeDatabase.mimeTypeForName(mimeTypeString);
63 // Get the MIME type icon.
64 QIcon mimeTypeIcon = QIcon::fromTheme(mimeType.iconName());
66 // Create a graphics scene.
67 QGraphicsScene *mimeGraphicsScenePointer = new QGraphicsScene(this);
69 // Set the graphics scene.
70 mimeGraphicsViewPointer->setScene(mimeGraphicsScenePointer);
72 // Set the background of the graphics view to be the same as the window
73 mimeGraphicsViewPointer->setBackgroundRole(QPalette::Window);
75 // Add the MIME type icon to the scene.
76 mimeGraphicsScenePointer->addPixmap(mimeTypeIcon.pixmap(64, 64));
78 // Populate the labels.
79 urlLabelPointer->setText("<b>" + downloadUrl.toString() + "</b>");
80 filetypeLabelPointer->setText("<b>" + mimeType.comment() + "</b>");
81 mimeTypeLabelPointer->setText("<b>" + mimeTypeString + "</b>");
83 // Populate the download size label.
84 if (totalBytes == -1) // The file size is unknown.
85 sizeLabelPointer->setText(i18nc("Unknown download file size. The bold style should be preserved.", "<b>unknown</b>"));
86 else // The file size is known. Format it according to the locale.
87 sizeLabelPointer->setText(ki18nc("Download file size. The bold style should be preserved.", "<b>%1 bytes</b>").subs(totalBytes).toString());
89 // Create the keyboard shortcuts.
90 QShortcut *sShortcutPointer = new QShortcut(QKeySequence(i18nc("The save key shortcut.", "s")), this);
91 QShortcut *cShortcutPointer = new QShortcut(QKeySequence(i18nc("The close key shortcut.", "c")), this);
92 QShortcut *quitShortcutPointer = new QShortcut(QKeySequence::Quit, this);
94 // Connect the save buttons.
97 // Show the file picker for the native download.
98 connect(saveButtonPointer, SIGNAL(clicked()), this, SLOT(showFilePicker()));
99 connect(sShortcutPointer, SIGNAL(activated()), this, SLOT(showFilePicker()));
103 // Use WebEngine's downloader.
104 connect(saveButtonPointer, SIGNAL(clicked()), this, SLOT(accept()));
105 connect(sShortcutPointer, SIGNAL(activated()), this, SLOT(accept()));
108 // Connect the cancel button.
109 connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
110 connect(cShortcutPointer, SIGNAL(activated()), this, SLOT(reject()));
111 connect(quitShortcutPointer, SIGNAL(activated()), this, SLOT(reject()));
114 void SaveDialog::showFilePicker()
116 // Show the file picker dialog.
117 emit useNativeKdeDownloader(downloadUrl, suggestedFileName);