X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserPC.git;a=blobdiff_plain;f=src%2Fdialogs%2FSaveDialog.cpp;fp=src%2Fdialogs%2FSaveDialog.cpp;h=fff857d41e18679f2b61cf374152f8316ced57a8;hp=0000000000000000000000000000000000000000;hb=1ce11bc5f6630bf81aa67bdaca411fbea93dc017;hpb=273588ac3f6077b62c1fb2b55b9cdc6b5e200fad diff --git a/src/dialogs/SaveDialog.cpp b/src/dialogs/SaveDialog.cpp new file mode 100644 index 0000000..fff857d --- /dev/null +++ b/src/dialogs/SaveDialog.cpp @@ -0,0 +1,94 @@ +/* + * Copyright © 2022 Soren Stoutner . + * + * This file is part of 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. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with Privacy Browser PC. If not, see . + */ + +// Application headers. +#include "SaveDialog.h" +#include "ui_SaveDialog.h" + +// KDE Frameworks headers. +#include + +// Qt toolkit headers. +#include +#include +#include +#include + +SaveDialog::SaveDialog(QWebEngineDownloadItem *downloadItemPointer) +{ + // Set the dialog window title. + setWindowTitle(i18nc("The save dialog window title", "Save")); + + // Set the window modality. + setWindowModality(Qt::WindowModality::ApplicationModal); + + // Instantiate the save dialog UI. + Ui::SaveDialog saveDialogUi; + + // Setup the UI. + saveDialogUi.setupUi(this); + + // Get handles for the widgets. + QLabel *urlLabelPointer = saveDialogUi.urlLabel; + QLabel *filetypeLabelPointer = saveDialogUi.fileTypeLabel; + QLabel *mimeTypeLabelPointer = saveDialogUi.mimeTypeLabel; + QLabel *sizeLabelPointer = saveDialogUi.sizeLabel; + 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; + + // Populate the labels. + urlLabelPointer->setText("" + downloadUrl.toString() + ""); + filetypeLabelPointer->setText("" + mimeDatabase.mimeTypeForName(mimeType).comment() + ""); + mimeTypeLabelPointer->setText("" + mimeType + ""); + + // Populate the download size label. + if (downloadItemPointer->totalBytes() == -1) // The file size is unknown. + sizeLabelPointer->setText(i18nc("Unknown download file size. The bold style should be preserved.", "unknown")); + else // The file size is known. Format it according to the locale. + sizeLabelPointer->setText(ki18nc("Download file size. The bold style should be preserved.", "%1 bytes").subs(downloadItemPointer->totalBytes()).toString()); + + // Connect the buttons. + connect(saveButtonPointer, SIGNAL(clicked()), this, SLOT(showFileDialog())); + connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject())); + + // 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(cShortcutPointer, SIGNAL(activated()), this, SLOT(reject())); +} + +void SaveDialog::showFileDialog() +{ + // Show the file picker dialog. + emit showSaveFilePickerDialog(downloadUrl, suggestedFileName); + + // Close the dialog. + reject(); +}