Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File download rework #1622

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ if (Qt5WebEngineWidgets_FOUND AND ENABLE_QTWEBENGINE)
${otter_src}
src/modules/backends/web/qtwebengine/QtWebEnginePage.cpp
src/modules/backends/web/qtwebengine/QtWebEngineUrlRequestInterceptor.cpp
src/modules/backends/web/qtwebengine/QtWebEngineTransfer.cpp
src/modules/backends/web/qtwebengine/QtWebEngineWebBackend.cpp
src/modules/backends/web/qtwebengine/QtWebEngineWebWidget.cpp
)
Expand Down
54 changes: 38 additions & 16 deletions src/core/TransfersManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,6 @@ void Transfer::start(QNetworkReply *reply, const QString &target)
}

m_device->reset();

m_mimeType = mimeDatabase.mimeTypeForData(m_device);

m_device->seek(m_device->size());

handleDataAvailable();
Expand All @@ -243,11 +240,6 @@ void Transfer::start(QNetworkReply *reply, const QString &target)

if (target.isEmpty())
{
if (!SettingsManager::getOption(SettingsManager::Browser_AlwaysAskWhereToSaveDownloadOption).toBool())
{
m_options |= IsQuickTransferOption;
}

const QString directory(m_options.testFlag(IsQuickTransferOption) ? Utils::normalizePath(SettingsManager::getOption(SettingsManager::Paths_DownloadsOption).toString()) : QString());
const QString fileName(getSuggestedFileName());

Expand Down Expand Up @@ -896,6 +888,20 @@ bool Transfer::setTarget(const QString &target, bool canOverwriteExisting)

return false;
}
else if (result == QMessageBox::Yes)
{
const QFileInfo information(target);
const QString path(Utils::getSavePath(information.fileName(), information.path(), {}, false).path);

if (path.isEmpty())
{
cancel();

return false;
}

mutableTarget = path;
}
}

if (!m_device)
Expand Down Expand Up @@ -1175,7 +1181,8 @@ TransfersManager* TransfersManager::getInstance()
return m_instance;
}

Transfer* TransfersManager::startTransfer(const QUrl &source, const QString &target, Transfer::TransferOptions options)
Transfer* TransfersManager::startTransfer(const QUrl &source, const QString target, Transfer::TransferOptions options,
std::function<void(Transfer*)> startCallback)
{
QNetworkRequest request;
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork);
Expand All @@ -1184,16 +1191,31 @@ Transfer* TransfersManager::startTransfer(const QUrl &source, const QString &tar
request.setUrl(QUrl(source));

Transfer *transfer(new Transfer(options, m_instance));
transfer->start(NetworkManagerFactory::getNetworkManager(options.testFlag(Transfer::IsPrivateOption))->get(request), target);

if (transfer->getState() == Transfer::CancelledState)
{
transfer->deleteLater();
QNetworkReply *reply = NetworkManagerFactory::getNetworkManager(options.testFlag(Transfer::IsPrivateOption))->get(request);
QObject *ctx = new QObject();

return nullptr;
}
connect(reply, &QNetworkReply::metaDataChanged,
ctx, [ctx, transfer, reply, request, target, startCallback]() {
delete ctx;

addTransfer(transfer);
transfer->start(reply, target);

if (transfer->getState() == Transfer::CancelledState ||
transfer->getState() == Transfer::ErrorState)
{
transfer->deleteLater();

return nullptr;
}

addTransfer(transfer);

if (startCallback)
{
startCallback(transfer);
}
});

return transfer;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/TransfersManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class TransfersManager final : public QObject
static void addTransfer(Transfer *transfer);
static void clearTransfers(int period = 0);
static TransfersManager* getInstance();
static Transfer* startTransfer(const QUrl &source, const QString &target = {}, Transfer::TransferOptions options = Transfer::CanAskForPathOption);
static Transfer* startTransfer(const QUrl &source, const QString target = {}, Transfer::TransferOptions options = Transfer::CanAskForPathOption, std::function<void(Transfer *)> startCallback = nullptr);
static Transfer* startTransfer(const QNetworkRequest &request, const QString &target = {}, Transfer::TransferOptions options = Transfer::CanAskForPathOption);
static Transfer* startTransfer(QNetworkReply *reply, const QString &target = {}, Transfer::TransferOptions options = Transfer::CanAskForPathOption);
static QVector<Transfer*> getTransfers();
Expand Down
2 changes: 2 additions & 0 deletions src/core/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,8 @@ SaveInformation getSavePath(const QString &fileName, const QString &directory, Q

if (dialog.exec() == QDialog::Rejected || dialog.selectedFiles().isEmpty())
{
path.clear();

break;
}

Expand Down
162 changes: 0 additions & 162 deletions src/modules/backends/web/qtwebengine/QtWebEngineTransfer.cpp

This file was deleted.

56 changes: 0 additions & 56 deletions src/modules/backends/web/qtwebengine/QtWebEngineTransfer.h

This file was deleted.

Loading