diff --git a/src/handlers/xdgurl.cpp b/src/handlers/xdgurl.cpp index 82b7cae2b9de9735028e29baefbd2f26387d885f..4921132f064dabd7fc4c8c35d687390c33e4dba2 100644 --- a/src/handlers/xdgurl.cpp +++ b/src/handlers/xdgurl.cpp @@ -1,6 +1,7 @@ #include <QDebug> #include <QUrl> #include <QUrlQuery> +#include <QTemporaryFile> #include <QMimeDatabase> #include <QProcess> #include <QNetworkReply> @@ -189,16 +190,61 @@ bool XdgUrl::_uncompressArchive(const QString &path, const QString &targetDir) return false; } +void XdgUrl::_saveDownloadedFile(const QTemporaryFile &temporaryFile) +{ +} + +void XdgUrl::_installDownloadedFile(const QTemporaryFile &temporaryFile) +{ +} + /** * Private slots */ -void XdgUrl::_saveDownloadedFile(QNetworkReply *reply) +void XdgUrl::_downloaded(QNetworkReply *reply) { -} + QJsonObject result; -void XdgUrl::_installDownloadedFile(QNetworkReply *reply) -{ + if (reply->error() != QNetworkReply::NoError) { + result["error"] = QString("network_error"); + emit finished(Utility::Json::convertObjToStr(result)); + return; + } + + // If the network reply has a refresh header, retry download + if (reply->hasRawHeader("Refresh")) { + QString refreshUrl = QString(reply->rawHeader("Refresh")).split("url=").last(); + if (refreshUrl.startsWith("/")) { + QUrl url = reply->url(); + refreshUrl = url.scheme() + "://" + url.host() + refreshUrl; + } + _asyncNetwork->get(QUrl(refreshUrl)); + return; + } + + QTemporaryFile temporaryFile; + if (!temporaryFile.open()) { + result["error"] = QString("save_error"); + emit finished(Utility::Json::convertObjToStr(result)); + return; + } + temporaryFile.write(reply->readAll()); + + QMimeDatabase mimeDb; + QString mimeType = mimeDb.mimeTypeForFile(temporaryFile.fileName()).name(); + if (mimeType == "text/html" || mimeType == "application/xhtml+xml") { + result["error"] = QString("filetype_error"); + emit finished(Utility::Json::convertObjToStr(result)); + return; + } + + if (_metadata["command"].toString() == "download") { + _saveDownloadedFile(temporaryFile); + } + else if (_metadata["command"].toString() == "install") { + _installDownloadedFile(temporaryFile); + } } /** diff --git a/src/handlers/xdgurl.h b/src/handlers/xdgurl.h index 789d54fb9d4725385d46101c35852743108ba897..fe6b64c2040efe482ac0127effd1168ebf5bbf16 100644 --- a/src/handlers/xdgurl.h +++ b/src/handlers/xdgurl.h @@ -4,6 +4,7 @@ #include <QObject> #include <QJsonObject> +class QTemporaryFile; class QNetworkReply; namespace Core { @@ -37,10 +38,11 @@ private: QJsonObject _loadArchiveTypes(); bool _installPlasmapkg(const QString &path, const QString &type = "plasmoid"); bool _uncompressArchive(const QString &path, const QString &targetDir); + void _saveDownloadedFile(const QTemporaryFile &temporaryFile); + void _installDownloadedFile(const QTemporaryFile &temporaryFile); private slots: - void _saveDownloadedFile(QNetworkReply *reply); - void _installDownloadedFile(QNetworkReply *reply); + void _downloaded(QNetworkReply *reply); public slots: QString getXdgUrl();