From 9287c51dc68859a48281678b88a3cf001891f2c1 Mon Sep 17 00:00:00 2001 From: Akira Ohgaki <akiraohgaki@gmail.com> Date: Sat, 22 Oct 2016 04:38:50 +0900 Subject: [PATCH] Code cleanup --- src/core/config.h | 8 +- src/core/network.h | 10 +-- src/handlers/xdgurl.cpp | 186 ++++++++++++++++++++-------------------- src/handlers/xdgurl.h | 39 ++++----- 4 files changed, 119 insertions(+), 124 deletions(-) diff --git a/src/core/config.h b/src/core/config.h index cc1832d..eb65732 100644 --- a/src/core/config.h +++ b/src/core/config.h @@ -10,15 +10,15 @@ class Config : public QObject { Q_OBJECT -private: - QString configsDir_; - QJsonObject cacheData_; - public: explicit Config(const QString &configsDir, QObject *parent = 0); QJsonObject get(const QString &name); bool set(const QString &name, const QJsonObject &jsonObj); + +private: + QString configsDir_; + QJsonObject cacheData_; }; } // namespace core diff --git a/src/core/network.h b/src/core/network.h index c2c5c83..4ebe282 100644 --- a/src/core/network.h +++ b/src/core/network.h @@ -13,11 +13,6 @@ class Network : public QObject { Q_OBJECT -private: - bool async_; - QNetworkAccessManager *manager_; - QEventLoop *eventLoop_; - public: explicit Network(const bool &async = true, QObject *parent = 0); ~Network(); @@ -28,6 +23,11 @@ public: signals: void finished(QNetworkReply *reply); void downloadProgress(const qint64 &received, const qint64 &total); + +private: + bool async_; + QNetworkAccessManager *manager_; + QEventLoop *eventLoop_; }; } // namespace core diff --git a/src/handlers/xdgurl.cpp b/src/handlers/xdgurl.cpp index 51ea650..0cf9c47 100644 --- a/src/handlers/xdgurl.cpp +++ b/src/handlers/xdgurl.cpp @@ -23,6 +23,97 @@ XdgUrl::XdgUrl(const QString &xdgUrl, core::Config *config, core::Network *netwo connect(network_, &core::Network::downloadProgress, this, &XdgUrl::downloadProgress); } +void XdgUrl::process() +{ + /** + * xdgs scheme is a reserved name, so the process of xdgs + * is the same process of the xdg scheme currently. + */ + + if (!isValid()) { + QJsonObject result; + result["status"] = QString("error_validation"); + result["message"] = QString("Invalid XDG-URL " + xdgUrl_); + emit error(result); + return; + } + + network_->get(QUrl(metadata_["url"].toString())); + emit started(); +} + +void XdgUrl::openDestination() +{ + if (!destination_.isEmpty()) { + QDesktopServices::openUrl(QUrl("file://" + destination_)); + } +} + +bool XdgUrl::isValid() +{ + QString scheme = metadata_["scheme"].toString(); + QString command = metadata_["command"].toString(); + QString url = metadata_["url"].toString(); + QString type = metadata_["type"].toString(); + QString filename = metadata_["filename"].toString(); + + if ((scheme == "xdg" || scheme == "xdgs") + && (command == "download" || command == "install") + && QUrl(url).isValid() + && destinations_.contains(type) + && !filename.isEmpty()) { + return true; + } + + return false; +} + +QString XdgUrl::getXdgUrl() +{ + return xdgUrl_; +} + +QJsonObject XdgUrl::getMetadata() +{ + return metadata_; +} + +void XdgUrl::downloaded_(QNetworkReply *reply) +{ + if (reply->error() != QNetworkReply::NoError) { + QJsonObject result; + result["status"] = QString("error_network"); + result["message"] = reply->errorString(); + emit error(result); + return; + } + + if (reply->hasRawHeader("Location")) { + QString redirectUrl = QString(reply->rawHeader("Location")); + if (redirectUrl.startsWith("/")) { + redirectUrl = reply->url().authority() + redirectUrl; + } + network_->get(QUrl(redirectUrl)); + return; + } + + 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); + } + else if (metadata_["command"].toString() == "install") { + installDownloadedFile_(reply); + } +} + void XdgUrl::parse_() { QUrl url(xdgUrl_); @@ -194,99 +285,4 @@ void XdgUrl::installDownloadedFile_(QNetworkReply *reply) emit finished(result); } -/** - * Slots - */ - -void XdgUrl::process() -{ - /** - * xdgs scheme is a reserved name, so the process of xdgs - * is the same process of the xdg scheme currently. - */ - - if (!isValid()) { - QJsonObject result; - result["status"] = QString("error_validation"); - result["message"] = QString("Invalid XDG-URL " + xdgUrl_); - emit error(result); - return; - } - - network_->get(QUrl(metadata_["url"].toString())); - emit started(); -} - -void XdgUrl::openDestination() -{ - if (!destination_.isEmpty()) { - QDesktopServices::openUrl(QUrl("file://" + destination_)); - } -} - -bool XdgUrl::isValid() -{ - QString scheme = metadata_["scheme"].toString(); - QString command = metadata_["command"].toString(); - QString url = metadata_["url"].toString(); - QString type = metadata_["type"].toString(); - QString filename = metadata_["filename"].toString(); - - if ((scheme == "xdg" || scheme == "xdgs") - && (command == "download" || command == "install") - && QUrl(url).isValid() - && destinations_.contains(type) - && !filename.isEmpty()) { - return true; - } - - return false; -} - -QString XdgUrl::getXdgUrl() -{ - return xdgUrl_; -} - -QJsonObject XdgUrl::getMetadata() -{ - return metadata_; -} - -void XdgUrl::downloaded_(QNetworkReply *reply) -{ - if (reply->error() != QNetworkReply::NoError) { - QJsonObject result; - result["status"] = QString("error_network"); - result["message"] = reply->errorString(); - emit error(result); - return; - } - - if (reply->hasRawHeader("Location")) { - QString redirectUrl = QString(reply->rawHeader("Location")); - if (redirectUrl.startsWith("/")) { - redirectUrl = reply->url().authority() + redirectUrl; - } - network_->get(QUrl(redirectUrl)); - return; - } - - 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); - } - else if (metadata_["command"].toString() == "install") { - installDownloadedFile_(reply); - } -} - } // namespace handlers diff --git a/src/handlers/xdgurl.h b/src/handlers/xdgurl.h index e426ff7..ac9eaeb 100644 --- a/src/handlers/xdgurl.h +++ b/src/handlers/xdgurl.h @@ -17,24 +17,14 @@ class XdgUrl : public QObject { Q_OBJECT -private: - QString xdgUrl_; - core::Config *config_; - core::Network *network_; - - QJsonObject metadata_; - QJsonObject destinations_; - QString destination_; - public: explicit XdgUrl(const QString &xdgUrl, core::Config *config, core::Network *network, QObject *parent = 0); -private: - void parse_(); - void loadDestinations_(); - QString convertPathString_(const QString &path); - void saveDownloadedFile_(QNetworkReply *reply); - void installDownloadedFile_(QNetworkReply *reply); +signals: + void started(); + void finished(const QJsonObject &result); + void error(const QJsonObject &result); + void downloadProgress(const qint64 &received, const qint64 &total); public slots: void process(); @@ -46,11 +36,20 @@ public slots: private slots: void downloaded_(QNetworkReply *reply); -signals: - void started(); - void finished(const QJsonObject &result); - void error(const QJsonObject &result); - void downloadProgress(const qint64 &received, const qint64 &total); +private: + void parse_(); + void loadDestinations_(); + QString convertPathString_(const QString &path); + void saveDownloadedFile_(QNetworkReply *reply); + void installDownloadedFile_(QNetworkReply *reply); + + QString xdgUrl_; + core::Config *config_; + core::Network *network_; + + QJsonObject metadata_; + QJsonObject destinations_; + QString destination_; }; } // namespace handlers -- GitLab