Skip to content

Commit

Permalink
Fixes for SaveInformation::canSave
Browse files Browse the repository at this point in the history
  • Loading branch information
fnkkio committed Nov 22, 2019
1 parent 0808312 commit e7f602b
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 45 deletions.
12 changes: 6 additions & 6 deletions src/core/TransfersManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -283,7 +283,7 @@ void Transfer::start(QNetworkReply *reply, const QString &target)
return;
}

finalTarget = information.path;
finalTarget = saveInfo.path;
canOverwriteExisting = true;
}

Expand Down Expand Up @@ -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)
{
Expand Down
8 changes: 1 addition & 7 deletions src/core/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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());
}
Expand Down
6 changes: 5 additions & 1 deletion src/core/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ struct SaveInformation final
{
QString path;
QString filter;
bool canSave = false;

bool canSave() const
{
return !path.isEmpty();
}
};

namespace Utils
Expand Down
6 changes: 3 additions & 3 deletions src/modules/backends/web/qtwebengine/QtWebEngineWebWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,11 @@ void QtWebEngineWebWidget::triggerAction(int identifier, const QVariantMap &para
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
Expand Down
26 changes: 13 additions & 13 deletions src/modules/backends/web/qtwebkit/QtWebKitWebWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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());
}
}

Expand Down Expand Up @@ -875,14 +875,14 @@ void QtWebKitWebWidget::triggerAction(int identifier, const QVariantMap &paramet
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
Expand Down Expand Up @@ -1598,11 +1598,11 @@ void QtWebKitWebWidget::triggerAction(int identifier, const QVariantMap &paramet
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"));
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/ui/SourceViewerWebWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/ui/TransferDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
}
Expand Down
14 changes: 7 additions & 7 deletions src/ui/WebWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,17 @@ 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();

return;
}

transfer->setTarget(path, true);
transfer->setTarget(saveInfo.path, true);

TransfersManager::addTransfer(transfer);
}
Expand Down Expand Up @@ -776,16 +776,16 @@ QString WebWidget::getSavePath(const QVector<SaveFormat> &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
Expand Down

0 comments on commit e7f602b

Please sign in to comment.