Permalink
Browse files

test

  • Loading branch information...
DolceTriade committed Aug 6, 2018
1 parent 767ece9 commit 72171dba751c0d8cdbb81a7e6a41e192d4b11762
Showing with 59 additions and 40 deletions.
  1. +4 −4 ariadownloader.cpp
  2. +19 −13 downloadworker.cpp
  3. +7 −7 downloadworker.h
  4. +6 −2 ret.h
  5. +4 −2 system.h
  6. +19 −12 unix.cpp
@@ -45,7 +45,7 @@ Ret AriaDownloader::addUri(const std::string& uri)
{
int ret = aria2::addUri(session, nullptr, { uri }, aria2::KeyVals());
if (ret < 0) {
return Ret(QString("Error adding uri: %1").arg(QString(uri.c_str())));
return Ret(false) << "Error adding uri: " << uri.c_str();
}
return true;
}
@@ -54,7 +54,7 @@ Ret AriaDownloader::run(void)
{
int ret = aria2::run(session, aria2::RUN_ONCE);
if (ret < 0) {
return Ret("Error running aria loop.");
return Ret(false) << "Error running aria loop.";
}
return true;
}
@@ -110,7 +110,7 @@ Ret AriaDownloader::updateStats(void)
for(const auto& gid : gids) {
aria2::DownloadHandle* dh = aria2::getDownloadHandle(session, gid);
if(!dh) {
return Ret(QString("Invalid download handle for gid: %1").arg(gid));
return Ret(false) << "Invalid download handle for gid: " << std::to_string(gid);
}
downloadSpeed_ = dh->getDownloadSpeed();
uploadSpeed_ = dh->getUploadSpeed();
@@ -145,7 +145,7 @@ Ret AriaDownloader::setDownloadDirectory(const std::string& dir)
{
int ret = aria2::changeGlobalOption(session, {{ "dir", dir }});
if (ret < 0) {
return Ret(QString("Error setting download directory to %1").arg(dir.c_str()));
return Ret(false) << "Error setting download directory to " << dir;
}
return true;
}
@@ -7,6 +7,7 @@

#include "quazip/quazip/JlCompress.h"
#include "system.h"
#include "ret.h"
#include <QDebug>

DownloadWorker::DownloadWorker(QObject *parent) : QObject(parent), downloadSpeed(0), uploadSpeed(0),
@@ -20,21 +21,26 @@ DownloadWorker::~DownloadWorker()
downloader.unregisterCallback(this);
}

void DownloadWorker::addUpdaterUri(const std::string& uri)
Ret DownloadWorker::addUpdaterUri(const std::string& uri)
{
downloader.addUri(uri);
Ret ret = downloader.addUri(uri);
if (!ret.ok()) return ret;
state = DOWNLOADING_UPDATER;
return true;
}

void DownloadWorker::addTorrent(const std::string& uri)
Ret DownloadWorker::addTorrent(const std::string& uri)
{
downloader.addUri(uri);
Ret ret = downloader.addUri(uri);
if (!ret.ok()) return ret;
state = DOWNLOADING_TORRENT;
return true;
}

void DownloadWorker::onDownloadCallback(aria2::Session* session, aria2::DownloadEvent event,
aria2::A2Gid gid, void* /* userData */)
{
Ret ret;
switch (event) {
case aria2::EVENT_ON_BT_DOWNLOAD_COMPLETE:
if (!extractUpdate()) return;
@@ -99,7 +105,7 @@ std::string DownloadWorker::getAriaIndexOut(size_t index, std::string path)



void DownloadWorker::download(void)
void DownloadWorker::download()
{
auto start = std::chrono::steady_clock::now();
paused = false;
@@ -137,29 +143,29 @@ void DownloadWorker::download(void)
}
}

void DownloadWorker::toggle(void)
void DownloadWorker::toggle()
{
downloader.toggleDownloads();
return downloader.toggleDownloads();
}

void DownloadWorker::setDownloadDirectory(const std::string& dir)
Ret DownloadWorker::setDownloadDirectory(const std::string& dir)
{
Ret ret = downloader.setDownloadDirectory(dir);
if (!ret.ok()) return ret;
downloadDir = dir.c_str();
downloader.setDownloadDirectory(dir);
}

void DownloadWorker::stop(void)
void DownloadWorker::stop()
{
running = false;
}

bool DownloadWorker::extractUpdate(void)
Ret DownloadWorker::extractUpdate()
{
QString filename = Sys::archiveName();
auto out = JlCompress::extractDir(downloadDir + "/" + filename, downloadDir);
if (out.size() < 1) {
emit onDownloadEvent(ERROR_EXTRACTING);
return false;
return Ret(false) << "Error extracting " << filename << " to " << downloadDir;
}
return true;
}
@@ -19,14 +19,14 @@ class DownloadWorker : public QObject, public AriaDownloader::DownloadCallback
~DownloadWorker();
void onDownloadCallback(aria2::Session* session, aria2::DownloadEvent event,
aria2::A2Gid gid, void* userDataevent);
void addUpdaterUri(const std::string& uri);
void addTorrent(const std::string& uri);
void setDownloadDirectory(const std::string& dir);
void toggle(void);
void stop(void);
Ret addUpdaterUri(const std::string& uri);
Ret addTorrent(const std::string& uri);
Ret setDownloadDirectory(const std::string& dir);
Ret toggle();
void stop();

public slots:
void download(void);
void download();

signals:
void downloadSpeedChanged(int speed);
@@ -37,7 +37,7 @@ public slots:

private:
void setDownloadPathAndFiles(aria2::Session* session, aria2::A2Gid gid);
bool extractUpdate(void);
Ret extractUpdate();
std::string getAriaIndexOut(size_t index, std::string path);

enum State {
8 ret.h
@@ -10,11 +10,15 @@ class Ret {
Ret(const QString& msg) : ok_(false), msg_(msg) {}
Ret(bool ok) : ok_(ok) {}

Ret& operator<<(const char* str) { msg_.append(str); return *this; }
Ret& operator<<(const std::string& str) { msg_.append(str.c_str()); return *this; }
Ret& operator<<(const QString& str) { msg_.append(str); return *this; }

bool ok() const { return ok_; }
const QString& msg() const { return msg_; }

const bool ok_;
const QString msg_;
bool ok_;
QString msg_;
};

#endif // RET_H
@@ -2,12 +2,14 @@

#include <QString>

#include "ret.h"

namespace Sys {
QString archiveName(void);
QString defaultInstallPath(void);
QString executableName(void);
bool install(void);
bool updateUpdater(const QString& updaterArchive);
Ret install(void);
Ret updateUpdater(const QString& updaterArchive);
QString updaterArchiveName(void);
std::string getCertStore(void);
}
@@ -22,51 +22,58 @@ QString executableName(void)
return "daemon";
}

bool install(void)
Ret install(void)
{
// Set up menu and protocol handler
Settings settings;
QFile desktopFile(":resources/unvanquished.desktop");
if (!desktopFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
return false;
return Ret(false) << "Error openning the desktop file for reading: " << desktopFile.fileName();
}
QString desktopStr = QString(desktopFile.readAll().data())
.arg(settings.installPath());
QFile outputFile(QDir::homePath() + "/.local/share/applications/unvanquished.desktop");
if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
desktopFile.close();
return false;
return Ret(false) << "Error openning output file for writing: " << outputFile.fileName();
}
qint64 written = outputFile.write(desktopStr.toUtf8().constData(), desktopStr.size());
if (written != desktopStr.size()) {
return Ret(false) << "Mismatched write. Expected to write "
<< std::to_string(desktopStr.size()) << " but only wrote "
<< std::to_string(written);
}
outputFile.write(desktopStr.toUtf8().constData(), desktopStr.size());
outputFile.close();

// install icon
QString iconDir = QDir::homePath() + "/.local/share/icons/hicolor/128x128/apps/";
QDir dir(iconDir);
if (!dir.exists()) {
if (!dir.mkpath(dir.path())) {
return false;
return Ret(false) << "Error creating path: " << dir.path();
}
}
QFile::copy(":resources/unvanquished.png",
iconDir + "unvanquished.png");

// Don't error out on icon installation failure.
if (!QFile::copy(":resources/unvanquished.png",
iconDir + "unvanquished.png")) {
qDebug() << "WARNING: Error installing icon to " << iconDir;
}
return true;
}

bool updateUpdater(const QString& updaterArchive)
Ret updateUpdater(const QString& updaterArchive)
{
QString current = QCoreApplication::applicationFilePath();
QString backup = current + ".bak";
QFile backupUpdater(backup);
if (backupUpdater.exists()) {
if (!backupUpdater.remove()) {
qDebug() << "Could not remove backup updater. Aboring autoupdate.";
return false;
return Ret(false) << "Could not remove backup updater. Aboring autoupdate.";
}
}
if (!QFile::rename(current, backup)) {
qDebug() << "Could not move " << current << " to " << backup;
return false;
return Ret(false) << "Could not move " << current << " to " << backup;
}
QDir destination(current);
if (!destination.cdUp()) {

0 comments on commit 72171db

Please sign in to comment.