Skip to content
Snippets Groups Projects
Commit d860b9c9 authored by akiraohgaki's avatar akiraohgaki
Browse files

Use qtlibs

parent 52a6619a
Branches
Tags
No related merge requests found
#include "xdgurl.h" #include "xdgurl.h"
#include <QUrl>
#include <QUrlQuery> #include <QUrlQuery>
#include <QTemporaryFile> //#include <QTemporaryFile>
#include <QNetworkReply>
#include <QDesktopServices> #include <QDesktopServices>
#include "../../libs/utils/config.h" #include "qtlibs/file.h"
#include "../../libs/utils/network.h" #include "qtlibs/dir.h"
#include "../../libs/utils/file.h" #include "qtlibs/config.h"
#include "../../libs/utils/package.h" #include "qtlibs/networkresource.h"
#include "qtlibs/package.h"
namespace handlers { namespace handlers {
XdgUrl::XdgUrl(const QString &xdgUrl, utils::Config *config, utils::Network *network, QObject *parent) : XdgUrl::XdgUrl(const QString &xdgUrl, qtlibs::Config *config, QObject *parent) :
QObject(parent), xdgUrl_(xdgUrl), config_(config), network_(network) QObject(parent), xdgUrl_(xdgUrl), config_(config)
{ {
parse(); parse();
loadDestinations(); loadDestinations();
}
connect(network_, &utils::Network::finished, this, &handlers::XdgUrl::downloaded); QString XdgUrl::xdgUrl() const
connect(network_, &utils::Network::downloadProgress, this, &handlers::XdgUrl::downloadProgress); {
return xdgUrl_;
}
QJsonObject XdgUrl::metadata() const
{
return metadata_;
} }
void XdgUrl::process() void XdgUrl::process()
...@@ -38,7 +44,11 @@ void XdgUrl::process() ...@@ -38,7 +44,11 @@ void XdgUrl::process()
return; 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(); emit started();
} }
...@@ -68,47 +78,21 @@ void XdgUrl::openDestination() ...@@ -68,47 +78,21 @@ void XdgUrl::openDestination()
} }
} }
QString XdgUrl::xdgUrl() const void XdgUrl::downloaded(qtlibs::NetworkResource *resource)
{ {
return xdgUrl_; if (resource->reply()->error() != QNetworkReply::NoError) {
}
QJsonObject XdgUrl::metadata() const
{
return metadata_;
}
void XdgUrl::downloaded(QNetworkReply *reply)
{
if (reply->error() != QNetworkReply::NoError) {
QJsonObject result; QJsonObject result;
result["status"] = QString("error_network"); result["status"] = QString("error_network");
result["message"] = reply->errorString(); result["message"] = resource->reply()->errorString();
emit error(result); emit error(result);
return; 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") { if (metadata_["command"].toString() == "download") {
saveDownloadedFile(reply); saveDownloadedFile(resource);
} }
else if (metadata_["command"].toString() == "install") { else if (metadata_["command"].toString() == "install") {
installDownloadedFile(reply); installDownloadedFile(resource);
} }
} }
...@@ -170,41 +154,32 @@ QString XdgUrl::convertPathString(const QString &path) ...@@ -170,41 +154,32 @@ QString XdgUrl::convertPathString(const QString &path)
QString newPath = path; QString newPath = path;
if (newPath.contains("$HOME")) { if (newPath.contains("$HOME")) {
newPath.replace("$HOME", utils::File::homePath()); newPath.replace("$HOME", qtlibs::Dir::homePath());
} }
else if (newPath.contains("$XDG_DATA_HOME")) { 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")) { else if (newPath.contains("$KDEHOME")) {
newPath.replace("$KDEHOME", utils::File::kdehomePath()); newPath.replace("$KDEHOME", qtlibs::Dir::kdehomePath());
} }
return newPath; return newPath;
} }
void XdgUrl::saveDownloadedFile(QNetworkReply *reply) void XdgUrl::saveDownloadedFile(qtlibs::NetworkResource *resource)
{ {
QJsonObject result; 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 type = metadata_["type"].toString();
QString destination = destinations_[type].toString(); QString destination = destinations_[type].toString();
QString path = destination + "/" + metadata_["filename"].toString(); QString path = destination + "/" + metadata_["filename"].toString();
utils::File::makeDir(destination); qtlibs::Dir(destination).make();
utils::File::remove(path); // Remove previous downloaded file qtlibs::File(path).remove(); // Remove previous downloaded file
if (!temporaryFile.copy(path)) { if (!resource->saveData(path)) {
result["status"] = QString("error_save"); result["status"] = QString("error_save");
result["message"] = temporaryFile.errorString(); result["message"] = QString("Failed to save data as " + path);
emit error(result); emit error(result);
return; return;
} }
...@@ -216,63 +191,64 @@ void XdgUrl::saveDownloadedFile(QNetworkReply *reply) ...@@ -216,63 +191,64 @@ void XdgUrl::saveDownloadedFile(QNetworkReply *reply)
emit finished(result); emit finished(result);
} }
void XdgUrl::installDownloadedFile(QNetworkReply *reply) void XdgUrl::installDownloadedFile(qtlibs::NetworkResource *resource)
{ {
QJsonObject result; 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["status"] = QString("error_save");
result["message"] = temporaryFile.errorString(); result["message"] = QString("Failed to save data as " + tempPath);
emit error(result); emit error(result);
return; return;
} }
QString type = metadata_["type"].toString(); qtlibs::Package package(tempPath);
QString destination = destinations_[type].toString();
QString path = destination + "/" + metadata_["filename"].toString();
utils::File::makeDir(destination);
utils::File::remove(path); // Remove previous downloaded file
if (type == "bin" if (type == "bin"
&& utils::Package::installProgram(temporaryFile.fileName(), path)) { && package.installAsProgram(path)) {
result["message"] = QString("The program has been installed into " + destination); result["message"] = QString("The file has been installed into " + destination);
} }
else if ((type == "plasma_plasmoids" || type == "plasma4_plasmoids" || type == "plasma5_plasmoids") 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"); result["message"] = QString("The plasmoid has been installed");
} }
else if ((type == "plasma_look_and_feel" || type == "plasma5_look_and_feel") 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"); result["message"] = QString("The plasma look and feel has been installed");
} }
else if ((type == "plasma_desktopthemes" || type == "plasma5_desktopthemes") 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"); result["message"] = QString("The plasma desktop theme has been installed");
} }
else if (type == "kwin_effects" else if (type == "kwin_effects"
&& utils::Package::installPlasmapkg(temporaryFile.fileName(), "kwineffect")) { && package.installAsPlasmapkg("kwineffect")) {
result["message"] = QString("The KWin effect has been installed"); result["message"] = QString("The KWin effect has been installed");
} }
else if (type == "kwin_scripts" else if (type == "kwin_scripts"
&& utils::Package::installPlasmapkg(temporaryFile.fileName(), "kwinscript")) { && package.installAsPlasmapkg("kwinscript")) {
result["message"] = QString("The KWin script has been installed"); result["message"] = QString("The KWin script has been installed");
} }
else if (type == "kwin_tabbox" else if (type == "kwin_tabbox"
&& utils::Package::installPlasmapkg(temporaryFile.fileName(), "windowswitcher")) { && package.installAsPlasmapkg("windowswitcher")) {
result["message"] = QString("The KWin window switcher has been installed"); result["message"] = QString("The KWin window switcher has been installed");
} }
else if (utils::Package::uncompressArchive(temporaryFile.fileName(), destination)) { else if (package.installAsArchive(destination)) {
result["message"] = QString("The archive file has been uncompressed into " + destination); result["message"] = QString("The archive file has been extracted into " + destination);
} }
else if (temporaryFile.copy(path)) { else if (package.installAsFile(path)) {
result["message"] = QString("The file has been stored into " + destination); result["message"] = QString("The file has been installed into " + destination);
} }
else { else {
result["status"] = QString("error_install"); result["status"] = QString("error_install");
result["message"] = temporaryFile.errorString(); result["message"] = QString("Failed to installation");
emit error(result); emit error(result);
return; return;
} }
......
...@@ -3,11 +3,9 @@ ...@@ -3,11 +3,9 @@
#include <QObject> #include <QObject>
#include <QJsonObject> #include <QJsonObject>
class QNetworkReply; namespace qtlibs {
namespace utils {
class Config; class Config;
class Network; class NetworkResource;
} }
namespace handlers { namespace handlers {
...@@ -17,34 +15,34 @@ class XdgUrl : public QObject ...@@ -17,34 +15,34 @@ class XdgUrl : public QObject
Q_OBJECT Q_OBJECT
public: 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: signals:
void started(); void started();
void finished(const QJsonObject &result); void finished(const QJsonObject &result);
void error(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: public slots:
QString xdgUrl() const;
QJsonObject metadata() const;
void process(); void process();
bool isValid(); bool isValid();
void openDestination(); void openDestination();
QString xdgUrl() const;
QJsonObject metadata() const;
private slots: private slots:
void downloaded(QNetworkReply *reply); void downloaded(qtlibs::NetworkResource *resource);
private: private:
void parse(); void parse();
void loadDestinations(); void loadDestinations();
QString convertPathString(const QString &path); QString convertPathString(const QString &path);
void saveDownloadedFile(QNetworkReply *reply); void saveDownloadedFile(qtlibs::NetworkResource *resource);
void installDownloadedFile(QNetworkReply *reply); void installDownloadedFile(qtlibs::NetworkResource *resource);
QString xdgUrl_; QString xdgUrl_;
utils::Config *config_; qtlibs::Config *config_;
utils::Network *network_;
QJsonObject metadata_; QJsonObject metadata_;
QJsonObject destinations_; QJsonObject destinations_;
......
...@@ -10,8 +10,7 @@ ...@@ -10,8 +10,7 @@
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QQmlContext> #include <QQmlContext>
#include "../libs/utils/config.h" #include "qtlibs/config.h"
#include "../libs/utils/network.h"
#include "handlers/xdgurl.h" #include "handlers/xdgurl.h"
...@@ -22,9 +21,8 @@ int main(int argc, char *argv[]) ...@@ -22,9 +21,8 @@ int main(int argc, char *argv[])
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif #endif
QGuiApplication app(argc, argv); 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"); QJsonObject configApplication = config->get("application");
app.setApplicationName(configApplication["name"].toString()); app.setApplicationName(configApplication["name"].toString());
...@@ -52,7 +50,7 @@ int main(int argc, char *argv[]) ...@@ -52,7 +50,7 @@ int main(int argc, char *argv[])
// Setup QML // Setup QML
QQmlApplicationEngine qmlAppEngine; QQmlApplicationEngine qmlAppEngine;
QQmlContext *qmlContext = qmlAppEngine.rootContext(); 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")); qmlAppEngine.load(QUrl("qrc:/qml/main.qml"));
return app.exec(); return app.exec();
......
...@@ -130,12 +130,12 @@ Window { ...@@ -130,12 +130,12 @@ Window {
errorDialog.open(); errorDialog.open();
}); });
xdgUrlHandler.downloadProgress.connect(function(received, total) { xdgUrlHandler.downloadProgress.connect(function(bytesReceived, bytesTotal) {
progressDialog.primaryLabel.text = 'Downloading... '; progressDialog.primaryLabel.text = 'Downloading... ';
progressDialog.informativeLabel.text = metadata.filename; progressDialog.informativeLabel.text = metadata.filename;
progressDialog.progressBar.value = received / total; progressDialog.progressBar.value = bytesReceived / bytesTotal;
progressDialog.progressLabel.text = Utility.convertByteToHumanReadable(received) progressDialog.progressLabel.text = Utility.convertByteToHumanReadable(bytesReceived)
+ ' / ' + Utility.convertByteToHumanReadable(total) + ' / ' + Utility.convertByteToHumanReadable(bytesTotal)
}); });
if (xdgUrlHandler.isValid()) { if (xdgUrlHandler.isValid()) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment