mirror-chatterino2/src/singletons/Updates.cpp

158 lines
4.6 KiB
C++
Raw Normal View History

2018-06-28 19:46:45 +02:00
#include "Updates.hpp"
2018-04-19 22:16:55 +02:00
2018-06-26 15:33:51 +02:00
#include "common/NetworkRequest.hpp"
#include "common/Version.hpp"
2018-06-26 14:09:39 +02:00
#include "util/CombinePath.hpp"
#include "util/PostToThread.hpp"
2018-04-19 22:16:55 +02:00
2018-06-21 22:02:35 +02:00
#include <QMessageBox>
#include <QProcess>
2018-04-19 22:16:55 +02:00
namespace chatterino {
2018-06-28 19:51:07 +02:00
Updates::Updates()
2018-06-01 14:20:46 +02:00
: currentVersion_(CHATTERINO_VERSION)
2018-04-19 22:16:55 +02:00
{
qDebug() << "init UpdateManager";
2018-04-19 22:16:55 +02:00
}
2018-06-28 19:51:07 +02:00
Updates &Updates::getInstance()
2018-04-19 22:16:55 +02:00
{
2018-06-01 14:20:46 +02:00
// fourtf: don't add this class to the application class
2018-06-28 19:51:07 +02:00
static Updates instance;
2018-06-01 14:20:46 +02:00
2018-04-19 22:16:55 +02:00
return instance;
}
2018-06-28 19:51:07 +02:00
const QString &Updates::getCurrentVersion() const
2018-04-19 22:16:55 +02:00
{
2018-06-01 14:20:46 +02:00
return currentVersion_;
2018-04-19 22:16:55 +02:00
}
2018-06-28 19:51:07 +02:00
const QString &Updates::getOnlineVersion() const
2018-04-19 22:16:55 +02:00
{
2018-06-01 14:20:46 +02:00
return onlineVersion_;
}
2018-06-28 19:51:07 +02:00
void Updates::installUpdates()
2018-06-01 14:20:46 +02:00
{
2018-06-21 22:02:35 +02:00
if (this->status_ != UpdateAvailable) {
assert(false);
return;
}
#ifdef Q_OS_WIN
2018-06-21 22:02:35 +02:00
QMessageBox *box = new QMessageBox(QMessageBox::Information, "Chatterino Update",
"Chatterino is downloading the update "
"in the background and will run the "
"updater once it is finished.");
box->setAttribute(Qt::WA_DeleteOnClose);
box->show();
2018-06-26 17:06:17 +02:00
NetworkRequest req(this->updateUrl_);
2018-06-21 22:02:35 +02:00
req.setTimeout(600000);
req.onError([this](int) -> bool {
this->setStatus_(DownloadFailed);
2018-06-21 22:39:17 +02:00
2018-06-26 17:06:17 +02:00
postToThread([] {
2018-06-21 23:02:42 +02:00
QMessageBox *box = new QMessageBox(QMessageBox::Information, "Chatterino Update",
"Failed while trying to download the update.");
box->setAttribute(Qt::WA_DeleteOnClose);
box->show();
2018-06-22 10:22:25 +02:00
box->raise();
2018-06-21 23:02:42 +02:00
});
2018-06-21 22:39:17 +02:00
2018-06-21 22:02:35 +02:00
return true;
});
req.get([this](QByteArray &object) {
2018-06-26 17:06:17 +02:00
auto filename = combinePath(getApp()->paths->miscDirectory, "update.zip");
2018-06-21 22:02:35 +02:00
QFile file(filename);
2018-06-21 23:02:42 +02:00
file.open(QIODevice::Truncate | QIODevice::WriteOnly);
2018-06-21 22:02:35 +02:00
if (file.write(object) == -1) {
this->setStatus_(WriteFileFailed);
return false;
}
2018-06-26 17:20:03 +02:00
QProcess::startDetached(
combinePath(QCoreApplication::applicationDirPath(), "updater.1/ChatterinoUpdater.exe"),
{filename, "restart"});
2018-06-21 22:02:35 +02:00
QApplication::exit(0);
return false;
});
this->setStatus_(Downloading);
req.execute();
#endif
2018-04-19 22:16:55 +02:00
}
2018-06-28 19:51:07 +02:00
void Updates::checkForUpdates()
2018-04-19 22:16:55 +02:00
{
#ifdef Q_OS_WIN
2018-04-19 22:16:55 +02:00
QString url = "https://notitia.chatterino.com/version/chatterino/" CHATTERINO_OS "/stable";
2018-06-26 17:06:17 +02:00
NetworkRequest req(url);
2018-06-01 14:20:46 +02:00
req.setTimeout(30000);
2018-04-20 00:15:57 +02:00
req.getJSON([this](QJsonObject &object) {
QJsonValue version_val = object.value("version");
2018-06-21 22:02:35 +02:00
QJsonValue update_val = object.value("update");
if (!version_val.isString() || !update_val.isString()) {
this->setStatus_(SearchFailed);
2018-04-19 22:16:55 +02:00
qDebug() << "error updating";
2018-06-21 22:02:35 +02:00
2018-06-26 17:06:17 +02:00
postToThread([] {
QMessageBox *box = new QMessageBox(
QMessageBox::Information, "Chatterino Update",
"Error while searching for updates.\n\nEither the service is down "
"temporarily or everything is broken.");
box->setAttribute(Qt::WA_DeleteOnClose);
box->show();
2018-06-22 10:22:25 +02:00
box->raise();
});
2018-04-19 22:16:55 +02:00
return;
}
2018-06-01 14:20:46 +02:00
this->onlineVersion_ = version_val.toString();
2018-06-21 22:02:35 +02:00
this->updateUrl_ = update_val.toString();
2018-06-01 14:20:46 +02:00
if (this->currentVersion_ != this->onlineVersion_) {
this->setStatus_(UpdateAvailable);
2018-06-26 17:06:17 +02:00
postToThread([this] {
2018-06-22 10:22:25 +02:00
QMessageBox *box =
new QMessageBox(QMessageBox::Information, "Chatterino Update",
"An update for chatterino is available.\n\nDo you "
"want to download and install it?",
QMessageBox::Yes | QMessageBox::No);
box->setAttribute(Qt::WA_DeleteOnClose);
box->show();
box->raise();
if (box->exec() == QMessageBox::Yes) {
this->installUpdates();
}
});
2018-06-01 14:20:46 +02:00
} else {
this->setStatus_(NoUpdateAvailable);
}
2018-04-19 22:16:55 +02:00
});
2018-06-01 14:20:46 +02:00
this->setStatus_(Searching);
req.execute();
#endif
2018-06-01 14:20:46 +02:00
}
2018-06-28 19:51:07 +02:00
Updates::UpdateStatus Updates::getStatus() const
2018-06-01 14:20:46 +02:00
{
return this->status_;
}
2018-06-28 19:51:07 +02:00
void Updates::setStatus_(UpdateStatus status)
2018-06-01 14:20:46 +02:00
{
if (this->status_ != status) {
this->status_ = status;
2018-06-26 17:06:17 +02:00
postToThread([this, status] { this->statusUpdated.invoke(status); });
2018-06-01 14:20:46 +02:00
}
2018-04-19 22:16:55 +02:00
}
} // namespace chatterino