2019-09-08 22:27:57 +02:00
|
|
|
#include "Updates.hpp"
|
|
|
|
|
2019-09-15 15:45:04 +02:00
|
|
|
#include "common/Modes.hpp"
|
2019-09-08 22:27:57 +02:00
|
|
|
#include "common/NetworkRequest.hpp"
|
2022-12-31 15:41:01 +01:00
|
|
|
#include "common/NetworkResult.hpp"
|
2019-09-08 22:27:57 +02:00
|
|
|
#include "common/Outcome.hpp"
|
Sort and force grouping of includes (#4172)
This change enforces strict include grouping using IncludeCategories
In addition to adding this to the .clang-format file and applying it in the tests/src and src directories, I also did the following small changes:
In ChatterSet.hpp, I changed lrucache to a <>include
In Irc2.hpp, I change common/SignalVector.hpp to a "project-include"
In AttachedWindow.cpp, NativeMessaging.cpp, WindowsHelper.hpp, BaseWindow.cpp, and StreamerMode.cpp, I disabled clang-format for the windows-includes
In WindowDescriptors.hpp, I added the missing vector include. It was previously not needed because the include was handled by another file that was previously included first.
clang-format minimum version has been bumped, so Ubuntu version used in the check-formatting job has been bumped to 22.04 (which is the latest LTS)
2022-11-27 19:32:53 +01:00
|
|
|
#include "common/QLogging.hpp"
|
2019-09-08 22:27:57 +02:00
|
|
|
#include "common/Version.hpp"
|
Sort and force grouping of includes (#4172)
This change enforces strict include grouping using IncludeCategories
In addition to adding this to the .clang-format file and applying it in the tests/src and src directories, I also did the following small changes:
In ChatterSet.hpp, I changed lrucache to a <>include
In Irc2.hpp, I change common/SignalVector.hpp to a "project-include"
In AttachedWindow.cpp, NativeMessaging.cpp, WindowsHelper.hpp, BaseWindow.cpp, and StreamerMode.cpp, I disabled clang-format for the windows-includes
In WindowDescriptors.hpp, I added the missing vector include. It was previously not needed because the include was handled by another file that was previously included first.
clang-format minimum version has been bumped, so Ubuntu version used in the check-formatting job has been bumped to 22.04 (which is the latest LTS)
2022-11-27 19:32:53 +01:00
|
|
|
#include "Settings.hpp"
|
2019-09-08 22:27:57 +02:00
|
|
|
#include "singletons/Paths.hpp"
|
|
|
|
#include "util/CombinePath.hpp"
|
|
|
|
#include "util/PostToThread.hpp"
|
|
|
|
|
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QProcess>
|
2019-10-07 19:53:46 +02:00
|
|
|
#include <QRegularExpression>
|
2023-01-26 19:22:48 +01:00
|
|
|
#include <semver/semver.hpp>
|
2019-09-08 22:27:57 +02:00
|
|
|
|
|
|
|
namespace chatterino {
|
|
|
|
namespace {
|
|
|
|
QString currentBranch()
|
|
|
|
{
|
|
|
|
return getSettings()->betaUpdates ? "beta" : "stable";
|
|
|
|
}
|
2019-10-07 19:53:46 +02:00
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Updates::Updates()
|
|
|
|
: currentVersion_(CHATTERINO_VERSION)
|
|
|
|
, updateGuideLink_("https://chatterino.com")
|
|
|
|
{
|
2020-11-21 16:20:10 +01:00
|
|
|
qCDebug(chatterinoUpdate) << "init UpdateManager";
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
|
2019-10-07 22:42:34 +02:00
|
|
|
Updates &Updates::instance()
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
|
|
|
// fourtf: don't add this class to the application class
|
|
|
|
static Updates instance;
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2023-01-26 19:22:48 +01:00
|
|
|
/// Checks if the online version is newer or older than the current version.
|
|
|
|
bool Updates::isDowngradeOf(const QString &online, const QString ¤t)
|
|
|
|
{
|
|
|
|
semver::version onlineVersion;
|
|
|
|
if (!onlineVersion.from_string_noexcept(online.toStdString()))
|
|
|
|
{
|
|
|
|
qCWarning(chatterinoUpdate) << "Unable to parse online version"
|
|
|
|
<< online << "into a proper semver string";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
semver::version currentVersion;
|
|
|
|
if (!currentVersion.from_string_noexcept(current.toStdString()))
|
|
|
|
{
|
|
|
|
qCWarning(chatterinoUpdate) << "Unable to parse current version"
|
|
|
|
<< current << "into a proper semver string";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return onlineVersion < currentVersion;
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
const QString &Updates::getCurrentVersion() const
|
|
|
|
{
|
|
|
|
return currentVersion_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &Updates::getOnlineVersion() const
|
|
|
|
{
|
|
|
|
return onlineVersion_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Updates::installUpdates()
|
|
|
|
{
|
|
|
|
if (this->status_ != UpdateAvailable)
|
|
|
|
{
|
|
|
|
assert(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef Q_OS_MACOS
|
|
|
|
QMessageBox *box = new QMessageBox(
|
|
|
|
QMessageBox::Information, "Chatterino Update",
|
|
|
|
"A link will open in your browser. Download and install to update.");
|
|
|
|
box->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
box->exec();
|
|
|
|
QDesktopServices::openUrl(this->updateExe_);
|
|
|
|
#elif defined Q_OS_LINUX
|
|
|
|
QMessageBox *box =
|
|
|
|
new QMessageBox(QMessageBox::Information, "Chatterino Update",
|
|
|
|
"Automatic updates are currently not available on "
|
|
|
|
"linux. Please redownload the app to update.");
|
|
|
|
box->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
box->exec();
|
|
|
|
QDesktopServices::openUrl(this->updateGuideLink_);
|
|
|
|
#elif defined Q_OS_WIN
|
|
|
|
if (getPaths()->isPortable())
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
|
|
|
NetworkRequest(this->updatePortable_)
|
|
|
|
.timeout(600000)
|
2019-09-19 19:03:50 +02:00
|
|
|
.onError([this](NetworkResult) {
|
2019-09-08 22:27:57 +02:00
|
|
|
this->setStatus_(DownloadFailed);
|
|
|
|
|
|
|
|
postToThread([] {
|
|
|
|
QMessageBox *box = new QMessageBox(
|
|
|
|
QMessageBox::Information, "Chatterino Update",
|
|
|
|
"Failed while trying to download the update.");
|
|
|
|
box->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
box->show();
|
|
|
|
box->raise();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.onSuccess([this](auto result) -> Outcome {
|
2023-05-02 23:22:04 +02:00
|
|
|
if (result.status() != 200)
|
|
|
|
{
|
|
|
|
auto *box = new QMessageBox(
|
|
|
|
QMessageBox::Information, "Chatterino Update",
|
|
|
|
QStringLiteral("The update couldn't be downloaded "
|
|
|
|
"(HTTP status %1).")
|
|
|
|
.arg(result.status()));
|
|
|
|
box->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
box->exec();
|
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
QByteArray object = result.getData();
|
|
|
|
auto filename =
|
|
|
|
combinePath(getPaths()->miscDirectory, "update.zip");
|
|
|
|
|
|
|
|
QFile file(filename);
|
|
|
|
file.open(QIODevice::Truncate | QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
if (file.write(object) == -1)
|
|
|
|
{
|
|
|
|
this->setStatus_(WriteFileFailed);
|
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
QProcess::startDetached(
|
|
|
|
combinePath(QCoreApplication::applicationDirPath(),
|
|
|
|
"updater.1/ChatterinoUpdater.exe"),
|
|
|
|
{filename, "restart"});
|
|
|
|
|
|
|
|
QApplication::exit(0);
|
|
|
|
return Success;
|
|
|
|
})
|
|
|
|
.execute();
|
|
|
|
this->setStatus_(Downloading);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
|
|
|
NetworkRequest(this->updateExe_)
|
|
|
|
.timeout(600000)
|
2019-09-19 19:03:50 +02:00
|
|
|
.onError([this](NetworkResult) {
|
2019-09-08 22:27:57 +02:00
|
|
|
this->setStatus_(DownloadFailed);
|
|
|
|
|
|
|
|
QMessageBox *box = new QMessageBox(
|
|
|
|
QMessageBox::Information, "Chatterino Update",
|
|
|
|
"Failed to download the update. \n\nTry manually "
|
|
|
|
"downloading the update.");
|
|
|
|
box->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
box->exec();
|
|
|
|
})
|
|
|
|
.onSuccess([this](auto result) -> Outcome {
|
2023-05-02 23:22:04 +02:00
|
|
|
if (result.status() != 200)
|
|
|
|
{
|
|
|
|
auto *box = new QMessageBox(
|
|
|
|
QMessageBox::Information, "Chatterino Update",
|
|
|
|
QStringLiteral("The update couldn't be downloaded "
|
|
|
|
"(HTTP status %1).")
|
|
|
|
.arg(result.status()));
|
|
|
|
box->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
box->exec();
|
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
QByteArray object = result.getData();
|
2022-11-10 20:11:40 +01:00
|
|
|
auto filePath =
|
2019-09-08 22:27:57 +02:00
|
|
|
combinePath(getPaths()->miscDirectory, "Update.exe");
|
|
|
|
|
2022-11-10 20:11:40 +01:00
|
|
|
QFile file(filePath);
|
2019-09-08 22:27:57 +02:00
|
|
|
file.open(QIODevice::Truncate | QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
if (file.write(object) == -1)
|
|
|
|
{
|
|
|
|
this->setStatus_(WriteFileFailed);
|
|
|
|
QMessageBox *box = new QMessageBox(
|
|
|
|
QMessageBox::Information, "Chatterino Update",
|
|
|
|
"Failed to save the update file. This could be due to "
|
|
|
|
"window settings or antivirus software.\n\nTry "
|
|
|
|
"manually "
|
|
|
|
"downloading the update.");
|
|
|
|
box->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
box->exec();
|
|
|
|
|
|
|
|
QDesktopServices::openUrl(this->updateExe_);
|
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
|
2022-11-10 20:11:40 +01:00
|
|
|
if (QProcess::startDetached(filePath, {}))
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
|
|
|
QApplication::exit(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QMessageBox *box = new QMessageBox(
|
|
|
|
QMessageBox::Information, "Chatterino Update",
|
|
|
|
"Failed to execute update binary. This could be due to "
|
|
|
|
"window "
|
|
|
|
"settings or antivirus software.\n\nTry manually "
|
|
|
|
"downloading "
|
|
|
|
"the update.");
|
|
|
|
box->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
box->exec();
|
|
|
|
|
|
|
|
QDesktopServices::openUrl(this->updateExe_);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Success;
|
|
|
|
})
|
|
|
|
.execute();
|
|
|
|
this->setStatus_(Downloading);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void Updates::checkForUpdates()
|
|
|
|
{
|
2021-08-21 13:00:01 +02:00
|
|
|
auto version = Version::instance();
|
|
|
|
|
|
|
|
if (!version.isSupportedOS())
|
2020-08-24 12:02:56 +02:00
|
|
|
{
|
2020-11-21 16:20:10 +01:00
|
|
|
qCDebug(chatterinoUpdate)
|
2020-08-24 12:02:56 +02:00
|
|
|
<< "Update checking disabled because OS doesn't appear to be one "
|
|
|
|
"of Windows, GNU/Linux or macOS.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-24 13:30:21 +02:00
|
|
|
// Disable updates on Flatpak
|
2021-08-21 13:00:01 +02:00
|
|
|
if (version.isFlatpak())
|
2021-07-24 13:30:21 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-08 10:32:55 +01:00
|
|
|
// Disable updates if on nightly
|
2019-10-07 22:42:34 +02:00
|
|
|
if (Modes::instance().isNightly)
|
2019-09-15 15:45:04 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
QString url =
|
|
|
|
"https://notitia.chatterino.com/version/chatterino/" CHATTERINO_OS "/" +
|
|
|
|
currentBranch();
|
|
|
|
|
|
|
|
NetworkRequest(url)
|
|
|
|
.timeout(60000)
|
|
|
|
.onSuccess([this](auto result) -> Outcome {
|
2023-05-02 23:22:04 +02:00
|
|
|
const auto object = result.parseJson();
|
2019-09-08 22:27:57 +02:00
|
|
|
/// Version available on every platform
|
2023-05-02 23:22:04 +02:00
|
|
|
auto version = object["version"];
|
2019-09-08 22:27:57 +02:00
|
|
|
|
2023-05-02 23:22:04 +02:00
|
|
|
if (!version.isString())
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
|
|
|
this->setStatus_(SearchFailed);
|
2023-05-02 23:22:04 +02:00
|
|
|
qCDebug(chatterinoUpdate)
|
|
|
|
<< "error checking version - missing 'version'" << object;
|
2019-09-08 22:27:57 +02:00
|
|
|
return Failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined Q_OS_WIN || defined Q_OS_MACOS
|
2020-05-10 13:13:47 +02:00
|
|
|
/// Downloads an installer for the new version
|
2023-05-02 23:22:04 +02:00
|
|
|
auto updateExeUrl = object["updateexe"];
|
|
|
|
if (!updateExeUrl.isString())
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
|
|
|
this->setStatus_(SearchFailed);
|
2023-05-02 23:22:04 +02:00
|
|
|
qCDebug(chatterinoUpdate)
|
|
|
|
<< "error checking version - missing 'updateexe'" << object;
|
2019-09-08 22:27:57 +02:00
|
|
|
return Failure;
|
|
|
|
}
|
2023-05-02 23:22:04 +02:00
|
|
|
this->updateExe_ = updateExeUrl.toString();
|
2020-08-24 09:52:24 +02:00
|
|
|
|
|
|
|
# ifdef Q_OS_WIN
|
2019-09-08 22:27:57 +02:00
|
|
|
/// Windows portable
|
2023-05-02 23:22:04 +02:00
|
|
|
auto portableUrl = object["portable_download"];
|
|
|
|
if (!portableUrl.isString())
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
|
|
|
this->setStatus_(SearchFailed);
|
2023-05-02 23:22:04 +02:00
|
|
|
qCDebug(chatterinoUpdate)
|
|
|
|
<< "error checking version - missing 'portable_download'"
|
|
|
|
<< object;
|
2019-09-08 22:27:57 +02:00
|
|
|
return Failure;
|
|
|
|
}
|
2023-05-02 23:22:04 +02:00
|
|
|
this->updatePortable_ = portableUrl.toString();
|
2020-08-24 09:52:24 +02:00
|
|
|
# endif
|
|
|
|
|
|
|
|
#elif defined Q_OS_LINUX
|
2023-05-02 23:22:04 +02:00
|
|
|
QJsonValue updateGuide = object.value("updateguide");
|
|
|
|
if (updateGuide.isString())
|
2019-09-08 22:27:57 +02:00
|
|
|
{
|
2023-05-02 23:22:04 +02:00
|
|
|
this->updateGuideLink_ = updateGuide.toString();
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
2020-08-24 09:52:24 +02:00
|
|
|
#else
|
2019-09-08 22:27:57 +02:00
|
|
|
return Failure;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/// Current version
|
2023-05-02 23:22:04 +02:00
|
|
|
this->onlineVersion_ = version.toString();
|
2019-09-08 22:27:57 +02:00
|
|
|
|
|
|
|
/// Update available :)
|
|
|
|
if (this->currentVersion_ != this->onlineVersion_)
|
|
|
|
{
|
|
|
|
this->setStatus_(UpdateAvailable);
|
2023-01-26 19:22:48 +01:00
|
|
|
this->isDowngrade_ = Updates::isDowngradeOf(
|
|
|
|
this->onlineVersion_, this->currentVersion_);
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->setStatus_(NoUpdateAvailable);
|
|
|
|
}
|
|
|
|
return Failure;
|
|
|
|
})
|
|
|
|
.execute();
|
|
|
|
this->setStatus_(Searching);
|
|
|
|
}
|
|
|
|
|
|
|
|
Updates::Status Updates::getStatus() const
|
|
|
|
{
|
|
|
|
return this->status_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Updates::shouldShowUpdateButton() const
|
|
|
|
{
|
|
|
|
switch (this->getStatus())
|
|
|
|
{
|
|
|
|
case UpdateAvailable:
|
|
|
|
case SearchFailed:
|
|
|
|
case Downloading:
|
|
|
|
case DownloadFailed:
|
|
|
|
case WriteFileFailed:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Updates::isError() const
|
|
|
|
{
|
|
|
|
switch (this->getStatus())
|
|
|
|
{
|
|
|
|
case SearchFailed:
|
|
|
|
case DownloadFailed:
|
|
|
|
case WriteFileFailed:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 19:53:46 +02:00
|
|
|
bool Updates::isDowngrade() const
|
|
|
|
{
|
|
|
|
return this->isDowngrade_;
|
|
|
|
}
|
|
|
|
|
2019-09-08 22:27:57 +02:00
|
|
|
void Updates::setStatus_(Status status)
|
|
|
|
{
|
|
|
|
if (this->status_ != status)
|
|
|
|
{
|
|
|
|
this->status_ = status;
|
2020-11-08 12:02:19 +01:00
|
|
|
postToThread([this, status] {
|
|
|
|
this->statusUpdated.invoke(status);
|
|
|
|
});
|
2019-09-08 22:27:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chatterino
|