From 2adcd4f24eca2fa529c42fc1165319bcc4ea179e Mon Sep 17 00:00:00 2001 From: Victor Tran Date: Thu, 12 Jul 2018 18:12:14 +1000 Subject: Initial commit --- installer/process/installworker.cpp | 78 +++++++++++++++++++++++++++++++++++++ installer/process/installworker.h | 35 +++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 installer/process/installworker.cpp create mode 100644 installer/process/installworker.h (limited to 'installer/process') diff --git a/installer/process/installworker.cpp b/installer/process/installworker.cpp new file mode 100644 index 0000000..48c7c10 --- /dev/null +++ b/installer/process/installworker.cpp @@ -0,0 +1,78 @@ +#include "installworker.h" + +InstallWorker::InstallWorker(QObject *parent) : QObject(parent) +{ +} + +bool InstallWorker::startWork() { + QLocalSocket* sock = new QLocalSocket(); + QString vendor, name, url, destPath; + bool isStableStream = true, isGlobalInstall = true; + + QString previousToken; + for (QString arg : QApplication::arguments()) { + if (previousToken != "") { + if (previousToken == "--socket") { + sock->setServerName(arg); + } else if (previousToken == "--vendor") { + vendor = arg; + } else if (previousToken == "--name") { + name = arg; + } else if (previousToken == "--url") { + url = arg; + } else if (previousToken == "--destdir") { + destPath = arg; + } + previousToken = ""; + } else { + if (arg == "--socket" || arg == "--vendor" || arg == "--name" || arg == "--url" || arg == "--destdir") { + previousToken = arg; + } else if (arg == "--blueprint") { + isStableStream = false; + } else if (arg == "--stable") { + isStableStream = true; + } else if (arg == "--local") { + isGlobalInstall = false; + } else if (arg == "--global") { + isGlobalInstall = true; + } + } + } + + if (sock->serverName() == "") { + qDebug() << "Required argument --socket missing"; + return false; + } + + qDebug() << "Connecting to socket server..."; + sock->connectToServer(); + if (!sock->waitForConnected()) { + qDebug() << "Failed to connect to socket server"; + return false; + } + connect(sock, &QLocalSocket::disconnected, [=] { + qDebug() << "Socket closed"; + QApplication::exit(1); + }); + + if (!packageFile.open() || !packageTemporaryDir.isValid()) { + return false; + } + sock->write(QString("STATUS ").append(tr("Downloading %1...").arg(name)).append("\n").toUtf8()); + sock->write(QString("DEBUG %1").arg(packageFile.fileName()).toUtf8()); + + QNetworkRequest req(QUrl((QString) url)); + req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true); + req.setHeader(QNetworkRequest::UserAgentHeader, "theInstaller/1.0"); + QNetworkReply* reply = mgr.get(req); + connect(reply, &QNetworkReply::finished, [=] { + sock->write(QString("STATUS ").append(tr("Unpacking %1...").arg(name)).append("\n").toUtf8()); + sock->write("PROGRESS 0 0\n"); + sock->write(QString("DEBUG %1").arg(packageTemporaryDir.path()).toUtf8()); + }); + connect(reply, &QNetworkReply::downloadProgress, [=](qint64 bytesReceived, qint64 bytesTotal) { + sock->write(QString("PROGRESS %1 %2\n").arg(QString::number(bytesReceived), QString::number(bytesTotal)).toUtf8()); + }); + + return true; +} diff --git a/installer/process/installworker.h b/installer/process/installworker.h new file mode 100644 index 0000000..84cbc48 --- /dev/null +++ b/installer/process/installworker.h @@ -0,0 +1,35 @@ +#ifndef INSTALLWORKER_H +#define INSTALLWORKER_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +class InstallWorker : public QObject +{ + Q_OBJECT +public: + explicit InstallWorker(QObject *parent = nullptr); + +signals: + +public slots: + bool startWork(); + +private: + QNetworkAccessManager mgr; + QTemporaryFile packageFile; + QTemporaryDir packageTemporaryDir; +}; + +#endif // INSTALLWORKER_H -- cgit v1.2.3