]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/SaveDialog.cpp
Add a default folder icon to the edit folder dialog. https://redmine.stoutner.com...
[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 <QMimeDatabase>
29 #include <QPushButton>
30 #include <QShortcut>
31 #include <QStandardPaths>
32
33 SaveDialog::SaveDialog(QWebEngineDownloadItem *downloadItemPointer)
34 {
35     // Set the dialog window title.
36     setWindowTitle(i18nc("The save dialog window title", "Save"));
37
38     // Set the window modality.
39     setWindowModality(Qt::WindowModality::ApplicationModal);
40
41     // Instantiate the save dialog UI.
42     Ui::SaveDialog saveDialogUi;
43
44     // Setup the UI.
45     saveDialogUi.setupUi(this);
46
47     // Get handles for the widgets.
48     QGraphicsView *mimeGraphicsViewPointer = saveDialogUi.mimeGraphicsView;
49     QLabel *urlLabelPointer = saveDialogUi.urlLabel;
50     QLabel *filetypeLabelPointer = saveDialogUi.fileTypeLabel;
51     QLabel *mimeTypeLabelPointer = saveDialogUi.mimeTypeLabel;
52     QLabel *sizeLabelPointer = saveDialogUi.sizeLabel;
53     QDialogButtonBox *dialogButtonBoxPointer = saveDialogUi.dialogButtonBox;
54     QPushButton *saveButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Save);
55
56     // Get the URL and the suggested file name.
57     downloadUrl = downloadItemPointer->url();
58     suggestedFileName = downloadItemPointer->suggestedFileName();
59     QString mimeTypeString = downloadItemPointer->mimeType();
60
61     // Get a MIME type database.
62     QMimeDatabase mimeDatabase;
63
64     // Get the MIME type.
65     QMimeType mimeType = mimeDatabase.mimeTypeForName(mimeTypeString);
66
67     // Get the MIME type icon.
68     QIcon mimeTypeIcon = QIcon::fromTheme(mimeType.iconName());
69
70     // Create a graphics scene.
71     QGraphicsScene *mimeGraphicsScenePointer = new QGraphicsScene(this);
72
73     // Set the graphics scene.
74     mimeGraphicsViewPointer->setScene(mimeGraphicsScenePointer);
75
76     // Set the background of the graphics view to be the same as the window
77     mimeGraphicsViewPointer->setBackgroundRole(QPalette::Window);
78
79     // Add the MIME type icon to the scene.
80     mimeGraphicsScenePointer->addPixmap(mimeTypeIcon.pixmap(64, 64));
81
82     // Populate the labels.
83     urlLabelPointer->setText("<b>" + downloadUrl.toString() + "</b>");
84     filetypeLabelPointer->setText("<b>" + mimeType.comment() + "</b>");
85     mimeTypeLabelPointer->setText("<b>" + mimeTypeString + "</b>");
86
87     // Populate the download size label.
88     if (downloadItemPointer->totalBytes() == -1)  // The file size is unknown.
89         sizeLabelPointer->setText(i18nc("Unknown download file size.  The bold style should be preserved.", "<b>unknown</b>"));
90     else  // The file size is known.  Format it according to the locale.
91         sizeLabelPointer->setText(ki18nc("Download file size.  The bold style should be preserved.", "<b>%1 bytes</b>").subs(downloadItemPointer->totalBytes()).toString());
92
93     // Connect the buttons.
94     connect(saveButtonPointer, SIGNAL(clicked()), this, SLOT(showFileDialog()));
95     connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
96
97     // Create the keyboard shortcuts.
98     QShortcut *sShortcutPointer = new QShortcut(QKeySequence(i18nc("The save key shortcut.", "s")), this);
99     QShortcut *cShortcutPointer = new QShortcut(QKeySequence(i18nc("The close key shortcut.", "c")), this);
100
101     // Connect the shortcuts.
102     connect(sShortcutPointer, SIGNAL(activated()), this, SLOT(showFileDialog()));
103     connect(cShortcutPointer, SIGNAL(activated()), this, SLOT(reject()));
104 }
105
106 void SaveDialog::showFileDialog()
107 {
108     // Show the file picker dialog.
109     emit showSaveFilePickerDialog(downloadUrl, suggestedFileName);
110
111     // Close the dialog.
112     reject();
113 }