diff options
| author | Victor Tran <vicr12345@gmail.com> | 2018-07-12 18:12:14 +1000 |
|---|---|---|
| committer | Victor Tran <vicr12345@gmail.com> | 2018-07-12 18:12:14 +1000 |
| commit | 2adcd4f24eca2fa529c42fc1165319bcc4ea179e (patch) | |
| tree | 1c1c8292e9e864640b19c7ff1ae4acaeb90244be /installer/process | |
| download | theInstaller-2adcd4f24eca2fa529c42fc1165319bcc4ea179e.tar.gz theInstaller-2adcd4f24eca2fa529c42fc1165319bcc4ea179e.tar.bz2 theInstaller-2adcd4f24eca2fa529c42fc1165319bcc4ea179e.zip | |
Initial commit
Diffstat (limited to 'installer/process')
| -rw-r--r-- | installer/process/installworker.cpp | 78 | ||||
| -rw-r--r-- | installer/process/installworker.h | 35 |
2 files changed, 113 insertions, 0 deletions
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 <QObject> +#include <QTextStream> +#include <QLocalSocket> +#include <QDebug> +#include <QApplication> +#include <QThread> +#include <QNetworkAccessManager> +#include <QNetworkRequest> +#include <QNetworkReply> +#include <QTemporaryFile> +#include <QTemporaryDir> + +#include <iostream> + +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 |
