diff options
| author | Mikaël Capelle <capelle.mikael@gmail.com> | 2022-05-17 11:47:01 +0200 |
|---|---|---|
| committer | Mikaël Capelle <capelle.mikael@gmail.com> | 2023-07-09 17:20:40 +0200 |
| commit | d13f6bb870cdda71257f665367be8ef9fca86255 (patch) | |
| tree | 52e214718478f1e52856572f5aa1a2ac58537f9f /src/selfupdater.cpp | |
| parent | 86bb01ba9eac879d3685c439ac9da0028bc4bc80 (diff) | |
Apply clang-format.
Diffstat (limited to 'src/selfupdater.cpp')
| -rw-r--r-- | src/selfupdater.cpp | 173 |
1 files changed, 81 insertions, 92 deletions
diff --git a/src/selfupdater.cpp b/src/selfupdater.cpp index 50100faa..7762c900 100644 --- a/src/selfupdater.cpp +++ b/src/selfupdater.cpp @@ -19,21 +19,22 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>. #include "selfupdater.h" -#include "utility.h" +#include "bbcode.h" +#include "downloadmanager.h" #include "iplugingame.h" #include "messagedialog.h" -#include "downloadmanager.h" #include "nexusinterface.h" #include "nxmaccessmanager.h" -#include "settings.h" -#include "bbcode.h" -#include "plugincontainer.h" #include "organizercore.h" -#include <versioninfo.h> -#include <report.h> +#include "plugincontainer.h" +#include "settings.h" #include "shared/util.h" #include "updatedialog.h" +#include "utility.h" +#include <report.h> +#include <versioninfo.h> +#include <QAbstractButton> #include <QApplication> #include <QCoreApplication> #include <QDir> @@ -41,8 +42,8 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>. #include <QLibrary> #include <QMessageBox> #include <QNetworkAccessManager> -#include <QNetworkRequest> #include <QNetworkReply> +#include <QNetworkRequest> #include <QProcess> #include <QProgressDialog> #include <QStringList> @@ -50,44 +51,37 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>. #include <QUrl> #include <QVariantList> #include <QVariantMap> -#include <QAbstractButton> #include <Qt> -#include <QtDebug> #include <QtAlgorithms> +#include <QtDebug> #include <boost/bind/bind.hpp> -#include <Windows.h> //for VS_FIXEDFILEINFO, GetLastError +#include <Windows.h> //for VS_FIXEDFILEINFO, GetLastError #include <exception> #include <map> -#include <stddef.h> //for size_t +#include <stddef.h> //for size_t #include <stdexcept> using namespace MOBase; using namespace MOShared; -SelfUpdater::SelfUpdater(NexusInterface *nexusInterface) - : m_Parent(nullptr) - , m_Interface(nexusInterface) - , m_Reply(nullptr) - , m_Attempts(3) +SelfUpdater::SelfUpdater(NexusInterface* nexusInterface) + : m_Parent(nullptr), m_Interface(nexusInterface), m_Reply(nullptr), m_Attempts(3) { m_MOVersion = createVersionInfo(); } +SelfUpdater::~SelfUpdater() {} -SelfUpdater::~SelfUpdater() -{ -} - -void SelfUpdater::setUserInterface(QWidget *widget) +void SelfUpdater::setUserInterface(QWidget* widget) { m_Parent = widget; } -void SelfUpdater::setPluginContainer(PluginContainer *pluginContainer) +void SelfUpdater::setPluginContainer(PluginContainer* pluginContainer) { m_Interface->setPluginContainer(pluginContainer); } @@ -107,53 +101,52 @@ void SelfUpdater::testForUpdate(const Settings& settings) // TODO: if prereleases are disabled we could just request the latest release // directly try { - m_GitHub.releases(GitHub::Repository("Modorganizer2", "modorganizer"), - [this](const QJsonArray &releases) { - if (releases.isEmpty()) { - // error message already logged - return; - } + m_GitHub.releases( + GitHub::Repository("Modorganizer2", "modorganizer"), + [this](const QJsonArray& releases) { + if (releases.isEmpty()) { + // error message already logged + return; + } - // We store all releases: - CandidatesMap mreleases; - for (const QJsonValue &releaseVal : releases) { - QJsonObject release = releaseVal.toObject(); - if (!release["draft"].toBool() && (Settings::instance().usePrereleases() - || !release["prerelease"].toBool())) { - auto version = VersionInfo(release["tag_name"].toString()); - mreleases[version] = release; - } - } + // We store all releases: + CandidatesMap mreleases; + for (const QJsonValue& releaseVal : releases) { + QJsonObject release = releaseVal.toObject(); + if (!release["draft"].toBool() && (Settings::instance().usePrereleases() || + !release["prerelease"].toBool())) { + auto version = VersionInfo(release["tag_name"].toString()); + mreleases[version] = release; + } + } - if (!mreleases.empty()) { - auto lastKey = mreleases.begin()->first; - if (lastKey > this->m_MOVersion) { + if (!mreleases.empty()) { + auto lastKey = mreleases.begin()->first; + if (lastKey > this->m_MOVersion) { - // Fill m_UpdateCandidates with version strictly greater than the - // current version: - m_UpdateCandidates.clear(); - for (auto p : mreleases) { - if (p.first > this->m_MOVersion) { - m_UpdateCandidates.insert(p); + // Fill m_UpdateCandidates with version strictly greater than the + // current version: + m_UpdateCandidates.clear(); + for (auto p : mreleases) { + if (p.first > this->m_MOVersion) { + m_UpdateCandidates.insert(p); + } + } + log::info("update available: {} -> {}", + this->m_MOVersion.displayString(3), lastKey.displayString(3)); + emit updateAvailable(); + } else if (lastKey < this->m_MOVersion) { + // this could happen if the user switches from using prereleases to + // stable builds. Should we downgrade? + log::debug("This version is newer than the latest released one: {} -> {}", + this->m_MOVersion.displayString(3), lastKey.displayString(3)); } } - log::info("update available: {} -> {}", - this->m_MOVersion.displayString(3), - lastKey.displayString(3)); - emit updateAvailable(); - } else if (lastKey < this->m_MOVersion) { - // this could happen if the user switches from using prereleases to - // stable builds. Should we downgrade? - log::debug("This version is newer than the latest released one: {} -> {}", - this->m_MOVersion.displayString(3), - lastKey.displayString(3)); - } - } - }); + }); } - //Catch all is bad by design, should be improved + // Catch all is bad by design, should be improved catch (...) { - log::debug("Unable to connect to github.com to check version"); + log::debug("Unable to connect to github.com to check version"); } } @@ -165,10 +158,8 @@ void SelfUpdater::startUpdate() auto latestRelease = m_UpdateCandidates.begin()->second; UpdateDialog dialog(m_Parent); - dialog.setVersions( - MOShared::createVersionInfo().displayString(3), - latestRelease["tag_name"].toString() - ); + dialog.setVersions(MOShared::createVersionInfo().displayString(3), + latestRelease["tag_name"].toString()); // We concatenate release details. We only include pre-release if those are // the latest release: @@ -191,14 +182,15 @@ void SelfUpdater::startUpdate() details += release["body"].toString(); } - // Need to call setDetailedText to create the QTextEdit and then be able to retrieve it: + // Need to call setDetailedText to create the QTextEdit and then be able to retrieve + // it: dialog.setChangeLogs(details); int res = dialog.exec(); if (dialog.result() == QDialog::Accepted) { bool found = false; - for (const QJsonValue &assetVal : latestRelease["assets"].toArray()) { + for (const QJsonValue& assetVal : latestRelease["assets"].toArray()) { QJsonObject asset = assetVal.toObject(); if (asset["content_type"].toString() == "application/x-msdownload") { openOutputFile(asset["name"].toString()); @@ -215,7 +207,6 @@ void SelfUpdater::startUpdate() } } - void SelfUpdater::showProgress() { if (m_Progress == nullptr) { @@ -238,29 +229,31 @@ void SelfUpdater::closeProgress() } } -void SelfUpdater::openOutputFile(const QString &fileName) +void SelfUpdater::openOutputFile(const QString& fileName) { - QString outputPath = QDir::fromNativeSeparators(qApp->property("dataPath").toString()) + "/" + fileName; + QString outputPath = + QDir::fromNativeSeparators(qApp->property("dataPath").toString()) + "/" + + fileName; log::debug("downloading to {}", outputPath); m_UpdateFile.setFileName(outputPath); m_UpdateFile.open(QIODevice::WriteOnly); } -void SelfUpdater::download(const QString &downloadLink) +void SelfUpdater::download(const QString& downloadLink) { - QNetworkAccessManager *accessManager = m_Interface->getAccessManager(); + QNetworkAccessManager* accessManager = m_Interface->getAccessManager(); QUrl dlUrl(downloadLink); QNetworkRequest request(dlUrl); m_Canceled = false; - m_Reply = accessManager->get(request); + m_Reply = accessManager->get(request); showProgress(); - connect(m_Reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64))); + connect(m_Reply, SIGNAL(downloadProgress(qint64, qint64)), this, + SLOT(downloadProgress(qint64, qint64))); connect(m_Reply, SIGNAL(finished()), this, SLOT(downloadFinished())); connect(m_Reply, SIGNAL(readyRead()), this, SLOT(downloadReadyRead())); } - void SelfUpdater::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) { if (m_Reply != nullptr) { @@ -276,7 +269,6 @@ void SelfUpdater::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) } } - void SelfUpdater::downloadReadyRead() { if (m_Reply != nullptr) { @@ -284,14 +276,14 @@ void SelfUpdater::downloadReadyRead() } } - void SelfUpdater::downloadFinished() { int error = QNetworkReply::NoError; if (m_Reply != nullptr) { if (m_Reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 302) { - QUrl url = m_Reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); + QUrl url = + m_Reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); m_UpdateFile.reset(); download(url.toString()); return; @@ -300,7 +292,9 @@ void SelfUpdater::downloadFinished() error = m_Reply->error(); - if (m_Reply->header(QNetworkRequest::ContentTypeHeader).toString().startsWith("text", Qt::CaseInsensitive)) { + if (m_Reply->header(QNetworkRequest::ContentTypeHeader) + .toString() + .startsWith("text", Qt::CaseInsensitive)) { m_Canceled = true; } @@ -313,9 +307,7 @@ void SelfUpdater::downloadFinished() m_UpdateFile.close(); - if ((m_UpdateFile.size() == 0) || - (error != QNetworkReply::NoError) || - m_Canceled) { + if ((m_UpdateFile.size() == 0) || (error != QNetworkReply::NoError) || m_Canceled) { if (!m_Canceled) { reportError(tr("Download failed: %1").arg(error)); } @@ -327,35 +319,32 @@ void SelfUpdater::downloadFinished() try { installUpdate(); - } catch (const std::exception &e) { + } catch (const std::exception& e) { reportError(tr("Failed to install update: %1").arg(e.what())); } } - void SelfUpdater::downloadCancel() { m_Canceled = true; } - void SelfUpdater::installUpdate() { const QString parameters = "/DIR=\"" + qApp->applicationDirPath() + "\" "; - const auto r = shell::Execute(m_UpdateFile.fileName(), parameters); + const auto r = shell::Execute(m_UpdateFile.fileName(), parameters); if (r.success()) { QCoreApplication::quit(); } else { - reportError(tr("Failed to start %1: %2") - .arg(m_UpdateFile.fileName()) - .arg(r.toString())); + reportError( + tr("Failed to start %1: %2").arg(m_UpdateFile.fileName()).arg(r.toString())); } m_UpdateFile.remove(); } -void SelfUpdater::report7ZipError(QString const &errorMessage) +void SelfUpdater::report7ZipError(QString const& errorMessage) { QMessageBox::critical(m_Parent, tr("Error"), errorMessage); } |
