From d860b9c96bce25bdaa537b8fe5792ab6337e4943 Mon Sep 17 00:00:00 2001 From: Akira Ohgaki <akiraohgaki@gmail.com> Date: Sat, 12 Nov 2016 04:44:08 +0900 Subject: [PATCH] Use qtlibs --- src/app/handlers/xdgurl.cpp | 142 +++++++++++++++--------------------- src/app/handlers/xdgurl.h | 24 +++--- src/app/main.cpp | 8 +- src/app/qml/main.qml | 8 +- 4 files changed, 77 insertions(+), 105 deletions(-) diff --git a/src/app/handlers/xdgurl.cpp b/src/app/handlers/xdgurl.cpp index 487a83b..c281179 100644 --- a/src/app/handlers/xdgurl.cpp +++ b/src/app/handlers/xdgurl.cpp @@ -1,26 +1,32 @@ #include "xdgurl.h" -#include <QUrl> #include <QUrlQuery> -#include <QTemporaryFile> -#include <QNetworkReply> +//#include <QTemporaryFile> #include <QDesktopServices> -#include "../../libs/utils/config.h" -#include "../../libs/utils/network.h" -#include "../../libs/utils/file.h" -#include "../../libs/utils/package.h" +#include "qtlibs/file.h" +#include "qtlibs/dir.h" +#include "qtlibs/config.h" +#include "qtlibs/networkresource.h" +#include "qtlibs/package.h" namespace handlers { -XdgUrl::XdgUrl(const QString &xdgUrl, utils::Config *config, utils::Network *network, QObject *parent) : - QObject(parent), xdgUrl_(xdgUrl), config_(config), network_(network) +XdgUrl::XdgUrl(const QString &xdgUrl, qtlibs::Config *config, QObject *parent) : + QObject(parent), xdgUrl_(xdgUrl), config_(config) { parse(); loadDestinations(); +} + +QString XdgUrl::xdgUrl() const +{ + return xdgUrl_; +} - connect(network_, &utils::Network::finished, this, &handlers::XdgUrl::downloaded); - connect(network_, &utils::Network::downloadProgress, this, &handlers::XdgUrl::downloadProgress); +QJsonObject XdgUrl::metadata() const +{ + return metadata_; } void XdgUrl::process() @@ -38,7 +44,11 @@ void XdgUrl::process() return; } - network_->get(QUrl(metadata_["url"].toString())); + QString url = metadata_["url"].toString(); + qtlibs::NetworkResource *resource = new qtlibs::NetworkResource(url, QUrl(url)); + connect(resource, &qtlibs::NetworkResource::downloadProgress, this, &XdgUrl::downloadProgress); + connect(resource, &qtlibs::NetworkResource::finished, this, &XdgUrl::downloaded); + resource->get(); emit started(); } @@ -68,47 +78,21 @@ void XdgUrl::openDestination() } } -QString XdgUrl::xdgUrl() const -{ - return xdgUrl_; -} - -QJsonObject XdgUrl::metadata() const -{ - return metadata_; -} - -void XdgUrl::downloaded(QNetworkReply *reply) +void XdgUrl::downloaded(qtlibs::NetworkResource *resource) { - if (reply->error() != QNetworkReply::NoError) { + if (resource->reply()->error() != QNetworkReply::NoError) { QJsonObject result; result["status"] = QString("error_network"); - result["message"] = reply->errorString(); + result["message"] = resource->reply()->errorString(); emit error(result); return; } - else if (reply->hasRawHeader("Location")) { - QString redirectUrl = QString(reply->rawHeader("Location")); - if (redirectUrl.startsWith("/")) { - redirectUrl = reply->url().authority() + redirectUrl; - } - network_->get(QUrl(redirectUrl)); - return; - } - else if (reply->hasRawHeader("Refresh")) { - QString refreshUrl = QString(reply->rawHeader("Refresh")).split("url=").last(); - if (refreshUrl.startsWith("/")) { - refreshUrl = reply->url().authority() + refreshUrl; - } - network_->get(QUrl(refreshUrl)); - return; - } if (metadata_["command"].toString() == "download") { - saveDownloadedFile(reply); + saveDownloadedFile(resource); } else if (metadata_["command"].toString() == "install") { - installDownloadedFile(reply); + installDownloadedFile(resource); } } @@ -170,41 +154,32 @@ QString XdgUrl::convertPathString(const QString &path) QString newPath = path; if (newPath.contains("$HOME")) { - newPath.replace("$HOME", utils::File::homePath()); + newPath.replace("$HOME", qtlibs::Dir::homePath()); } else if (newPath.contains("$XDG_DATA_HOME")) { - newPath.replace("$XDG_DATA_HOME", utils::File::genericDataPath()); + newPath.replace("$XDG_DATA_HOME", qtlibs::Dir::genericDataPath()); } else if (newPath.contains("$KDEHOME")) { - newPath.replace("$KDEHOME", utils::File::kdehomePath()); + newPath.replace("$KDEHOME", qtlibs::Dir::kdehomePath()); } return newPath; } -void XdgUrl::saveDownloadedFile(QNetworkReply *reply) +void XdgUrl::saveDownloadedFile(qtlibs::NetworkResource *resource) { QJsonObject result; - QTemporaryFile temporaryFile; - - if (!temporaryFile.open() || temporaryFile.write(reply->readAll()) == -1) { - result["status"] = QString("error_save"); - result["message"] = temporaryFile.errorString(); - emit error(result); - return; - } - QString type = metadata_["type"].toString(); QString destination = destinations_[type].toString(); QString path = destination + "/" + metadata_["filename"].toString(); - utils::File::makeDir(destination); - utils::File::remove(path); // Remove previous downloaded file + qtlibs::Dir(destination).make(); + qtlibs::File(path).remove(); // Remove previous downloaded file - if (!temporaryFile.copy(path)) { + if (!resource->saveData(path)) { result["status"] = QString("error_save"); - result["message"] = temporaryFile.errorString(); + result["message"] = QString("Failed to save data as " + path); emit error(result); return; } @@ -216,63 +191,64 @@ void XdgUrl::saveDownloadedFile(QNetworkReply *reply) emit finished(result); } -void XdgUrl::installDownloadedFile(QNetworkReply *reply) +void XdgUrl::installDownloadedFile(qtlibs::NetworkResource *resource) { QJsonObject result; - QTemporaryFile temporaryFile; + QString type = metadata_["type"].toString(); + QString destination = destinations_[type].toString(); + QString path = destination + "/" + metadata_["filename"].toString(); + QString tempPath = qtlibs::Dir::tempPath() + "/" + metadata_["filename"].toString(); + + qtlibs::Dir(destination).make(); + qtlibs::File(path).remove(); // Remove previous downloaded file - if (!temporaryFile.open() || temporaryFile.write(reply->readAll()) == -1) { + if (!resource->saveData(tempPath)) { result["status"] = QString("error_save"); - result["message"] = temporaryFile.errorString(); + result["message"] = QString("Failed to save data as " + tempPath); emit error(result); return; } - QString type = metadata_["type"].toString(); - QString destination = destinations_[type].toString(); - QString path = destination + "/" + metadata_["filename"].toString(); - - utils::File::makeDir(destination); - utils::File::remove(path); // Remove previous downloaded file + qtlibs::Package package(tempPath); if (type == "bin" - && utils::Package::installProgram(temporaryFile.fileName(), path)) { - result["message"] = QString("The program has been installed into " + destination); + && package.installAsProgram(path)) { + result["message"] = QString("The file has been installed into " + destination); } else if ((type == "plasma_plasmoids" || type == "plasma4_plasmoids" || type == "plasma5_plasmoids") - && utils::Package::installPlasmapkg(temporaryFile.fileName(), "plasmoid")) { + && package.installAsPlasmapkg("plasmoid")) { result["message"] = QString("The plasmoid has been installed"); } else if ((type == "plasma_look_and_feel" || type == "plasma5_look_and_feel") - && utils::Package::installPlasmapkg(temporaryFile.fileName(), "lookandfeel")) { + && package.installAsPlasmapkg("lookandfeel")) { result["message"] = QString("The plasma look and feel has been installed"); } else if ((type == "plasma_desktopthemes" || type == "plasma5_desktopthemes") - && utils::Package::installPlasmapkg(temporaryFile.fileName(), "theme")) { + && package.installAsPlasmapkg("theme")) { result["message"] = QString("The plasma desktop theme has been installed"); } else if (type == "kwin_effects" - && utils::Package::installPlasmapkg(temporaryFile.fileName(), "kwineffect")) { + && package.installAsPlasmapkg("kwineffect")) { result["message"] = QString("The KWin effect has been installed"); } else if (type == "kwin_scripts" - && utils::Package::installPlasmapkg(temporaryFile.fileName(), "kwinscript")) { + && package.installAsPlasmapkg("kwinscript")) { result["message"] = QString("The KWin script has been installed"); } else if (type == "kwin_tabbox" - && utils::Package::installPlasmapkg(temporaryFile.fileName(), "windowswitcher")) { + && package.installAsPlasmapkg("windowswitcher")) { result["message"] = QString("The KWin window switcher has been installed"); } - else if (utils::Package::uncompressArchive(temporaryFile.fileName(), destination)) { - result["message"] = QString("The archive file has been uncompressed into " + destination); + else if (package.installAsArchive(destination)) { + result["message"] = QString("The archive file has been extracted into " + destination); } - else if (temporaryFile.copy(path)) { - result["message"] = QString("The file has been stored into " + destination); + else if (package.installAsFile(path)) { + result["message"] = QString("The file has been installed into " + destination); } else { result["status"] = QString("error_install"); - result["message"] = temporaryFile.errorString(); + result["message"] = QString("Failed to installation"); emit error(result); return; } diff --git a/src/app/handlers/xdgurl.h b/src/app/handlers/xdgurl.h index 3cd2cd0..a86d21f 100644 --- a/src/app/handlers/xdgurl.h +++ b/src/app/handlers/xdgurl.h @@ -3,11 +3,9 @@ #include <QObject> #include <QJsonObject> -class QNetworkReply; - -namespace utils { +namespace qtlibs { class Config; -class Network; +class NetworkResource; } namespace handlers { @@ -17,34 +15,34 @@ class XdgUrl : public QObject Q_OBJECT public: - explicit XdgUrl(const QString &xdgUrl, utils::Config *config, utils::Network *network, QObject *parent = 0); + explicit XdgUrl(const QString &xdgUrl, qtlibs::Config *config, QObject *parent = 0); signals: void started(); void finished(const QJsonObject &result); void error(const QJsonObject &result); - void downloadProgress(const qint64 &received, const qint64 &total); + void downloadProgress(const qint64 &bytesReceived, const qint64 &bytesTotal); public slots: + QString xdgUrl() const; + QJsonObject metadata() const; + void process(); bool isValid(); void openDestination(); - QString xdgUrl() const; - QJsonObject metadata() const; private slots: - void downloaded(QNetworkReply *reply); + void downloaded(qtlibs::NetworkResource *resource); private: void parse(); void loadDestinations(); QString convertPathString(const QString &path); - void saveDownloadedFile(QNetworkReply *reply); - void installDownloadedFile(QNetworkReply *reply); + void saveDownloadedFile(qtlibs::NetworkResource *resource); + void installDownloadedFile(qtlibs::NetworkResource *resource); QString xdgUrl_; - utils::Config *config_; - utils::Network *network_; + qtlibs::Config *config_; QJsonObject metadata_; QJsonObject destinations_; diff --git a/src/app/main.cpp b/src/app/main.cpp index 30d5979..20319b3 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -10,8 +10,7 @@ #include <QQmlApplicationEngine> #include <QQmlContext> -#include "../libs/utils/config.h" -#include "../libs/utils/network.h" +#include "qtlibs/config.h" #include "handlers/xdgurl.h" @@ -22,9 +21,8 @@ int main(int argc, char *argv[]) QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif QGuiApplication app(argc, argv); - utils::Config *config = new utils::Config(":/configs"); - utils::Network *network = new utils::Network(true); + qtlibs::Config *config = new qtlibs::Config(":/configs"); QJsonObject configApplication = config->get("application"); app.setApplicationName(configApplication["name"].toString()); @@ -52,7 +50,7 @@ int main(int argc, char *argv[]) // Setup QML QQmlApplicationEngine qmlAppEngine; QQmlContext *qmlContext = qmlAppEngine.rootContext(); - qmlContext->setContextProperty("xdgUrlHandler", new handlers::XdgUrl(xdgUrl, config, network)); + qmlContext->setContextProperty("xdgUrlHandler", new handlers::XdgUrl(xdgUrl, config)); qmlAppEngine.load(QUrl("qrc:/qml/main.qml")); return app.exec(); diff --git a/src/app/qml/main.qml b/src/app/qml/main.qml index 3b1d8ea..40ca60a 100644 --- a/src/app/qml/main.qml +++ b/src/app/qml/main.qml @@ -130,12 +130,12 @@ Window { errorDialog.open(); }); - xdgUrlHandler.downloadProgress.connect(function(received, total) { + xdgUrlHandler.downloadProgress.connect(function(bytesReceived, bytesTotal) { progressDialog.primaryLabel.text = 'Downloading... '; progressDialog.informativeLabel.text = metadata.filename; - progressDialog.progressBar.value = received / total; - progressDialog.progressLabel.text = Utility.convertByteToHumanReadable(received) - + ' / ' + Utility.convertByteToHumanReadable(total) + progressDialog.progressBar.value = bytesReceived / bytesTotal; + progressDialog.progressLabel.text = Utility.convertByteToHumanReadable(bytesReceived) + + ' / ' + Utility.convertByteToHumanReadable(bytesTotal) }); if (xdgUrlHandler.isValid()) { -- GitLab