From e7f602bc44faa3d256e2a2680a4ce09cacb98d91 Mon Sep 17 00:00:00 2001 From: Stefan Comanescu Date: Fri, 22 Nov 2019 15:21:34 +0200 Subject: [PATCH] Fixes for SaveInformation::canSave --- src/core/TransfersManager.cpp | 12 ++++----- src/core/Utils.cpp | 8 +----- src/core/Utils.h | 6 ++++- .../web/qtwebengine/QtWebEngineWebWidget.cpp | 6 ++--- .../web/qtwebkit/QtWebKitWebWidget.cpp | 26 +++++++++---------- src/ui/SourceViewerWebWidget.cpp | 8 +++--- src/ui/TransferDialog.cpp | 8 +++--- src/ui/WebWidget.cpp | 14 +++++----- 8 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/core/TransfersManager.cpp b/src/core/TransfersManager.cpp index bc08a0d529..c44fd74282 100644 --- a/src/core/TransfersManager.cpp +++ b/src/core/TransfersManager.cpp @@ -265,11 +265,11 @@ void Transfer::start(QNetworkReply *reply, const QString &target) { m_isSelectingPath = true; - const SaveInformation information(Utils::getSavePath(fileName, directory)); + const SaveInformation saveInfo(Utils::getSavePath(fileName, directory)); m_isSelectingPath = false; - if (!information.canSave) + if (!saveInfo.canSave()) { if (m_reply) { @@ -283,7 +283,7 @@ void Transfer::start(QNetworkReply *reply, const QString &target) return; } - finalTarget = information.path; + finalTarget = saveInfo.path; canOverwriteExisting = true; } @@ -879,16 +879,16 @@ bool Transfer::setTarget(const QString &target, bool canOverwriteExisting) if (result == QMessageBox::No) { const QFileInfo information(target); - const QString path(Utils::getSavePath(information.fileName(), information.path(), {}, true).path); + const SaveInformation saveInfo(Utils::getSavePath(information.fileName(), information.path(), {}, true)); - if (path.isEmpty()) + if (!saveInfo.canSave()) { cancel(); return false; } - mutableTarget = path; + mutableTarget = saveInfo.path; } else if (result == QMessageBox::Cancel) { diff --git a/src/core/Utils.cpp b/src/core/Utils.cpp index 437f8f2211..4f5761b7c4 100644 --- a/src/core/Utils.cpp +++ b/src/core/Utils.cpp @@ -655,8 +655,6 @@ SaveInformation getSavePath(const QString &fileName, const QString &directory, Q } information.filter = dialog.selectedNameFilter(); - information.canSave = true; - path = dialog.selectedFiles().value(0); } @@ -687,11 +685,7 @@ SaveInformation getSavePath(const QString &fileName, const QString &directory, Q } while (true); - if (path.isEmpty()) - { - information.canSave = false; - } - else + if (!path.isEmpty()) { SettingsManager::setOption(SettingsManager::Paths_SaveFileOption, QFileInfo(path).dir().canonicalPath()); } diff --git a/src/core/Utils.h b/src/core/Utils.h index a03f2d9f3d..2757e72ac3 100644 --- a/src/core/Utils.h +++ b/src/core/Utils.h @@ -85,7 +85,11 @@ struct SaveInformation final { QString path; QString filter; - bool canSave = false; + + bool canSave() const + { + return !path.isEmpty(); + } }; namespace Utils diff --git a/src/modules/backends/web/qtwebengine/QtWebEngineWebWidget.cpp b/src/modules/backends/web/qtwebengine/QtWebEngineWebWidget.cpp index d9e3cce38b..43e387e8db 100644 --- a/src/modules/backends/web/qtwebengine/QtWebEngineWebWidget.cpp +++ b/src/modules/backends/web/qtwebengine/QtWebEngineWebWidget.cpp @@ -249,11 +249,11 @@ void QtWebEngineWebWidget::triggerAction(int identifier, const QVariantMap ¶ case ActionsManager::SaveAction: if (m_page->isViewingMedia()) { - const SaveInformation information(Utils::getSavePath(suggestSaveFileName(SingleFileSaveFormat))); + const SaveInformation saveInfo(Utils::getSavePath(suggestSaveFileName(SingleFileSaveFormat))); - if (information.canSave) + if (saveInfo.canSave()) { - m_page->save(information.path, QWebEngineDownloadItem::SingleHtmlSaveFormat); + m_page->save(saveInfo.path, QWebEngineDownloadItem::SingleHtmlSaveFormat); } } else diff --git a/src/modules/backends/web/qtwebkit/QtWebKitWebWidget.cpp b/src/modules/backends/web/qtwebkit/QtWebKitWebWidget.cpp index ad7cb2524d..c3f5efc605 100644 --- a/src/modules/backends/web/qtwebkit/QtWebKitWebWidget.cpp +++ b/src/modules/backends/web/qtwebkit/QtWebKitWebWidget.cpp @@ -402,16 +402,16 @@ void QtWebKitWebWidget::handleDownloadRequested(const QNetworkRequest &request) if (device && device->size() > 0) { - const QString path(Utils::getSavePath(request.url().fileName()).path); + const SaveInformation saveInfo(Utils::getSavePath(request.url().fileName())); - if (path.isEmpty()) + if (!saveInfo.canSave()) { device->deleteLater(); return; } - QFile file(path); + QFile file(saveInfo.path); if (!file.open(QIODevice::WriteOnly)) { @@ -435,15 +435,15 @@ void QtWebKitWebWidget::handleDownloadRequested(const QNetworkRequest &request) { const QString imageUrl(hitResult.imageUrl.url()); const QString imageType(imageUrl.mid(11, (imageUrl.indexOf(QLatin1Char(';')) - 11))); - const QString path(Utils::getSavePath(tr("file") + QLatin1Char('.') + imageType).path); + const SaveInformation saveInfo(Utils::getSavePath(tr("file") + QLatin1Char('.') + imageType)); - if (!path.isEmpty()) + if (saveInfo.canSave()) { - QImageWriter writer(path); + QImageWriter writer(saveInfo.path); if (!writer.write(QImage::fromData(QByteArray::fromBase64(imageUrl.mid(imageUrl.indexOf(QLatin1String(";base64,")) + 7).toUtf8()), imageType.toStdString().c_str()))) { - Console::addMessage(tr("Failed to save image: %1").arg(writer.errorString()), Console::OtherCategory, Console::ErrorLevel, path, -1, getWindowIdentifier()); + Console::addMessage(tr("Failed to save image: %1").arg(writer.errorString()), Console::OtherCategory, Console::ErrorLevel, saveInfo.path, -1, getWindowIdentifier()); } } @@ -875,14 +875,14 @@ void QtWebKitWebWidget::triggerAction(int identifier, const QVariantMap ¶met case ActionsManager::SaveAction: if (m_page->isViewingMedia()) { - const SaveInformation information(Utils::getSavePath(suggestSaveFileName(SingleFileSaveFormat))); + const SaveInformation saveInfo(Utils::getSavePath(suggestSaveFileName(SingleFileSaveFormat))); - if (information.canSave) + if (saveInfo.canSave()) { QNetworkRequest request(getUrl()); request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); - TransfersManager::startTransfer(m_networkManager->get(request), information.path, (Transfer::CanAskForPathOption | Transfer::CanAutoDeleteOption | Transfer::CanOverwriteOption | Transfer::IsPrivateOption)); + TransfersManager::startTransfer(m_networkManager->get(request), saveInfo.path, (Transfer::CanAskForPathOption | Transfer::CanAutoDeleteOption | Transfer::CanOverwriteOption | Transfer::IsPrivateOption)); } } else @@ -1598,11 +1598,11 @@ void QtWebKitWebWidget::triggerAction(int identifier, const QVariantMap ¶met m_page->setViewportSize(viewportSize); const QStringList filters({tr("PNG image (*.png)"), tr("JPEG image (*.jpg *.jpeg)")}); - const SaveInformation result(Utils::getSavePath(suggestSaveFileName(QLatin1String(".png")), {}, filters)); + const SaveInformation saveInfo(Utils::getSavePath(suggestSaveFileName(QLatin1String(".png")), {}, filters)); - if (result.canSave) + if (saveInfo.canSave()) { - pixmap.save(result.path, ((filters.indexOf(result.filter) == 0) ? "PNG" : "JPEG")); + pixmap.save(saveInfo.path, ((filters.indexOf(saveInfo.filter) == 0) ? "PNG" : "JPEG")); } } diff --git a/src/ui/SourceViewerWebWidget.cpp b/src/ui/SourceViewerWebWidget.cpp index 7a933177b1..e75b97290f 100644 --- a/src/ui/SourceViewerWebWidget.cpp +++ b/src/ui/SourceViewerWebWidget.cpp @@ -69,11 +69,11 @@ void SourceViewerWebWidget::triggerAction(int identifier, const QVariantMap &par { case ActionsManager::SaveAction: { - const QString path(Utils::getSavePath(suggestSaveFileName(SingleFileSaveFormat)).path); + const SaveInformation saveInfo(Utils::getSavePath(suggestSaveFileName(SingleFileSaveFormat))); - if (!path.isEmpty()) + if (saveInfo.canSave()) { - QFile file(path); + QFile file(saveInfo.path); if (file.open(QIODevice::WriteOnly)) { @@ -84,7 +84,7 @@ void SourceViewerWebWidget::triggerAction(int identifier, const QVariantMap &par } else { - Console::addMessage(tr("Failed to save file: %1").arg(file.errorString()), Console::OtherCategory, Console::ErrorLevel, path); + Console::addMessage(tr("Failed to save file: %1").arg(file.errorString()), Console::OtherCategory, Console::ErrorLevel, saveInfo.path); QMessageBox::warning(nullptr, tr("Error"), tr("Failed to save file."), QMessageBox::Close); } diff --git a/src/ui/TransferDialog.cpp b/src/ui/TransferDialog.cpp index 09f4310289..458cba3067 100644 --- a/src/ui/TransferDialog.cpp +++ b/src/ui/TransferDialog.cpp @@ -134,9 +134,9 @@ void TransferDialog::handleButtonClicked(QAbstractButton *button) dialog = dialog->parentWidget(); } - const QString path(Utils::getSavePath(m_transfer->getSuggestedFileName()).path); + const SaveInformation saveInfo(Utils::getSavePath(m_transfer->getSuggestedFileName())); - if (path.isEmpty()) + if (!saveInfo.canSave()) { m_transfer->cancel(); @@ -145,13 +145,13 @@ void TransferDialog::handleButtonClicked(QAbstractButton *button) return; } - m_transfer->setTarget(path, true); + m_transfer->setTarget(saveInfo.path, true); if (m_ui->rememberChoiceCheckBox->isChecked()) { HandlersManager::HandlerDefinition definition; definition.transferMode = HandlersManager::HandlerDefinition::SaveTransfer; - definition.downloadsPath = QFileInfo(path).canonicalPath(); + definition.downloadsPath = QFileInfo(saveInfo.path).canonicalPath(); HandlersManager::setHandler(m_transfer->getMimeType(), definition); } diff --git a/src/ui/WebWidget.cpp b/src/ui/WebWidget.cpp index e729566590..04b51dad73 100644 --- a/src/ui/WebWidget.cpp +++ b/src/ui/WebWidget.cpp @@ -201,9 +201,9 @@ void WebWidget::startTransfer(Transfer *transfer) break; case HandlersManager::HandlerDefinition::SaveAsTransfer: { - const QString path(Utils::getSavePath(transfer->getSuggestedFileName(), handler.downloadsPath, {}, true).path); + const SaveInformation saveInfo(Utils::getSavePath(transfer->getSuggestedFileName(), handler.downloadsPath, {}, true)); - if (path.isEmpty()) + if (!saveInfo.canSave()) { transfer->cancel(); transfer->deleteLater(); @@ -211,7 +211,7 @@ void WebWidget::startTransfer(Transfer *transfer) return; } - transfer->setTarget(path, true); + transfer->setTarget(saveInfo.path, true); TransfersManager::addTransfer(transfer); } @@ -776,16 +776,16 @@ QString WebWidget::getSavePath(const QVector &allowedFormats, SaveFo filters.append(formats.value(allowedFormats.at(i))); } - const SaveInformation result(Utils::getSavePath(suggestSaveFileName(SingleFileSaveFormat), {}, filters)); + const SaveInformation saveInfo(Utils::getSavePath(suggestSaveFileName(SingleFileSaveFormat), {}, filters)); - if (!result.canSave) + if (!saveInfo.canSave()) { return {}; } - *selectedFormat = formats.key(result.filter); + *selectedFormat = formats.key(saveInfo.filter); - return result.path; + return saveInfo.path; } QString WebWidget::getOpenActionText(SessionsManager::OpenHints hints) const