]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/SaveDialog.cpp
622bc363a1ef2945338651a4774136aa3f3fa30b
[PrivacyBrowserPC.git] / src / dialogs / SaveDialog.cpp
1 /*
2  * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 // Application headers.
21 #include "SaveDialog.h"
22 #include "ui_SaveDialog.h"
23
24 // KDE Frameworks headers.
25 #include <KLocalizedString>
26
27 // Qt toolkit headers.
28 #include <QDebug>
29 #include <QMimeDatabase>
30 #include <QPushButton>
31 #include <QShortcut>
32 #include <QStandardPaths>
33
34 SaveDialog::SaveDialog(QWebEngineDownloadItem *downloadItemPointer)
35 {
36     // Set the dialog window title.
37     setWindowTitle(i18nc("The save dialog window title", "Save"));
38
39     // Set the window modality.
40     setWindowModality(Qt::WindowModality::ApplicationModal);
41
42     // Instantiate the save dialog UI.
43     Ui::SaveDialog saveDialogUi;
44
45     // Setup the UI.
46     saveDialogUi.setupUi(this);
47
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);
56
57     // Get the URL and the suggested file name.
58     downloadUrl = downloadItemPointer->url();
59     suggestedFileName = downloadItemPointer->suggestedFileName();
60     QString mimeTypeString = downloadItemPointer->mimeType();
61
62     // Get a MIME type database.
63     QMimeDatabase mimeDatabase;
64
65     // Get the MIME type.
66     QMimeType mimeType = mimeDatabase.mimeTypeForName(mimeTypeString);
67
68     // Get the MIME type icon.
69     QIcon mimeTypeIcon = QIcon::fromTheme(mimeType.iconName());
70
71     // Create a graphics scene.
72     QGraphicsScene *mimeGraphicsScenePointer = new QGraphicsScene(this);
73
74     // Set the graphics scene.
75     mimeGraphicsViewPointer->setScene(mimeGraphicsScenePointer);
76
77     // Set the background of the graphics view to be the same as the window
78     mimeGraphicsViewPointer->setBackgroundRole(QPalette::Window);
79
80     // Add the MIME type icon to the scene.
81     mimeGraphicsScenePointer->addPixmap(mimeTypeIcon.pixmap(64, 64));
82
83     // Populate the labels.
84     urlLabelPointer->setText("<b>" + downloadUrl.toString() + "</b>");
85     filetypeLabelPointer->setText("<b>" + mimeType.comment() + "</b>");
86     mimeTypeLabelPointer->setText("<b>" + mimeTypeString + "</b>");
87
88     // Populate the download size label.
89     if (downloadItemPointer->totalBytes() == -1)  // The file size is unknown.
90         sizeLabelPointer->setText(i18nc("Unknown download file size.  The bold style should be preserved.", "<b>unknown</b>"));
91     else  // The file size is known.  Format it according to the locale.
92         sizeLabelPointer->setText(ki18nc("Download file size.  The bold style should be preserved.", "<b>%1 bytes</b>").subs(downloadItemPointer->totalBytes()).toString());
93
94     // Connect the buttons.
95     connect(saveButtonPointer, SIGNAL(clicked()), this, SLOT(showFileDialog()));
96     connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
97
98     // Create the keyboard shortcuts.
99     QShortcut *sShortcutPointer = new QShortcut(QKeySequence(i18nc("The save key shortcut.", "s")), this);
100     QShortcut *cShortcutPointer = new QShortcut(QKeySequence(i18nc("The close key shortcut.", "c")), this);
101
102     // Connect the shortcuts.
103     connect(sShortcutPointer, SIGNAL(activated()), this, SLOT(showFileDialog()));
104     connect(cShortcutPointer, SIGNAL(activated()), this, SLOT(reject()));
105 }
106
107 void SaveDialog::showFileDialog()
108 {
109     // Show the file picker dialog.
110     emit showSaveFilePickerDialog(downloadUrl, suggestedFileName);
111
112     // Close the dialog.
113     reject();
114 }