aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/coreSQLiteStudio/services/updatemanager.cpp
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@unit193.net>2021-12-17 07:06:30 -0500
committerLibravatarUnit 193 <unit193@unit193.net>2021-12-17 07:06:30 -0500
commit1fdc150116cad39aae5c5da407c3312b47a59e3a (patch)
tree123c79a4d7ad2d45781ba03ce939f7539fb428d8 /SQLiteStudio3/coreSQLiteStudio/services/updatemanager.cpp
parentfeda8a7db8d1d7c5439aa8f8feef7cc0dd2b59a0 (diff)
New upstream version 3.3.3+dfsg1.upstream/3.3.3+dfsg1
Diffstat (limited to 'SQLiteStudio3/coreSQLiteStudio/services/updatemanager.cpp')
-rw-r--r--SQLiteStudio3/coreSQLiteStudio/services/updatemanager.cpp143
1 files changed, 43 insertions, 100 deletions
diff --git a/SQLiteStudio3/coreSQLiteStudio/services/updatemanager.cpp b/SQLiteStudio3/coreSQLiteStudio/services/updatemanager.cpp
index 87df73b..867ef58 100644
--- a/SQLiteStudio3/coreSQLiteStudio/services/updatemanager.cpp
+++ b/SQLiteStudio3/coreSQLiteStudio/services/updatemanager.cpp
@@ -7,29 +7,18 @@
#include <QRegularExpression>
#include <QCoreApplication>
#include <QFileInfo>
+#include <QNetworkRequest>
+#include <QNetworkAccessManager>
+#include <QNetworkReply>
+#include <QJsonDocument>
+#include <QJsonObject>
#include <QtConcurrent/QtConcurrentRun>
UpdateManager::UpdateManager(QObject *parent) :
QObject(parent)
{
- qRegisterMetaType<QList<UpdateManager::UpdateEntry>>();
-
- connect(this, SIGNAL(updatingError(QString)), NOTIFY_MANAGER, SLOT(error(QString)));
-
- QString updateBinary =
-#if defined(Q_OS_WIN)
- "UpdateSQLiteStudio.exe";
-#elif defined(Q_OS_LINUX)
- "UpdateSQLiteStudio";
-#elif defined(Q_OS_OSX)
- "../../UpdateSQLiteStudio.app/Contents/MacOS/UpdateSQLiteStudio";
-#else
- "";
-#endif
-
- if (!updateBinary.isEmpty()) {
- updateBinaryAbsolutePath = QFileInfo(QCoreApplication::applicationDirPath() + "/" + updateBinary).absoluteFilePath();
- }
+ connect(this, SIGNAL(updatingError(QString)), this, SLOT(handleUpdatingError(QString)));
+ netManager = new QNetworkAccessManager(this);
}
UpdateManager::~UpdateManager()
@@ -42,54 +31,14 @@ void UpdateManager::checkForUpdates()
if (!CFG_CORE.General.CheckUpdatesOnStartup.get())
return;
- if (updateBinaryAbsolutePath.isEmpty()) {
- qDebug() << "Updater binary not defined. Skipping updates checking.";
- return;
- }
-
- if (!QFileInfo(updateBinaryAbsolutePath).exists()) {
- QString errorDetails = tr("Updates installer executable is missing.");
- emit updatingError(tr("Unable to check for updates (%1)").arg(errorDetails.trimmed()));
- qWarning() << "Error while checking for updates: " << errorDetails;
- return;
- }
-
- QtConcurrent::run(this, &UpdateManager::checkForUpdatesAsync);
-}
-
-void UpdateManager::checkForUpdatesAsync()
-{
- QProcess proc;
- proc.start(updateBinaryAbsolutePath, {"--checkupdates"});
- if (!waitForProcess(proc))
+ static_qstring(url, "https://sqlitestudio.pl/rest/updates");
+ QNetworkRequest request(url);
+ QNetworkReply* response = netManager->get(request);
+ connect(response, &QNetworkReply::finished, [this, response]()
{
- QString errorDetails = QString::fromLocal8Bit(proc.readAllStandardError());
-
- if (errorDetails.toLower().contains("no updates")) {
- emit noUpdatesAvailable();
- return;
- }
-
- if (errorDetails.isEmpty())
- errorDetails = tr("details are unknown");
-
- emit updatingError(tr("Unable to check for updates (%1)").arg(errorDetails.trimmed()));
- qWarning() << "Error while checking for updates: " << errorDetails;
- return;
- }
-
- processCheckResults(proc.readAllStandardOutput());
-}
-
-void UpdateManager::update()
-{
- bool success = QProcess::startDetached(updateBinaryAbsolutePath, {"--updater"});
- if (!success)
- {
- emit updatingError(tr("Unable to run updater application (%1). Please report this.").arg(updateBinaryAbsolutePath));
- return;
- }
- qApp->exit(0);
+ response->deleteLater();
+ handleUpdatesResponse(response);
+ });
}
bool UpdateManager::isPlatformEligibleForUpdate() const
@@ -97,56 +46,50 @@ bool UpdateManager::isPlatformEligibleForUpdate() const
return getDistributionType() != DistributionType::OS_MANAGED;
}
-bool UpdateManager::waitForProcess(QProcess& proc)
+void UpdateManager::handleUpdatesResponse(QNetworkReply* response)
{
- if (!proc.waitForFinished(-1))
+ if (response->error() != QNetworkReply::NoError)
{
- qDebug() << "Update QProcess timed out.";
- return false;
+ emit updatingError(response->errorString());
+ return;
}
- if (proc.exitStatus() == QProcess::CrashExit)
- {
- qDebug() << "Update QProcess finished by crashing.";
- return false;
- }
+ QJsonParseError parsingError;
+ QJsonDocument json = QJsonDocument::fromJson(response->readAll(), &parsingError);
- if (proc.exitCode() != 0)
+ if (parsingError.error != QJsonParseError::NoError)
{
- qDebug() << "Update QProcess finished with code:" << proc.exitCode();
- return false;
+ emit updatingError(parsingError.errorString());
+ return;
}
- return true;
-}
+ QString version = json["version"].toString();
+ QStringList versionParts = version.split(".");
+ QString alignedVersion = versionParts[0] + versionParts[1].rightJustified(2, '0') + versionParts[2].rightJustified(2, '0');
+ int versionNumber = alignedVersion.toInt();
-void UpdateManager::processCheckResults(const QByteArray &results)
-{
- if (results.trimmed().isEmpty()) {
+ if (SQLITESTUDIO->getVersion() >= versionNumber)
+ {
emit noUpdatesAvailable();
return;
}
- QRegularExpression re(R"(\<update\s+([^\>]+)\>)");
- QRegularExpression versionRe(R"(version\=\"([\d\.]+)\")");
- QRegularExpression nameRe(R"(name\=\"([^\"]+)\")");
+#if defined(Q_OS_WIN)
+ QString url = json["win"].toString();
+#elif defined(Q_OS_LINUX)
+ QString url = json["lin"].toString();
+#elif defined(Q_OS_OSX)
+ QString url = json["mac"].toString();
+#else
+ QString url = jsonQString();
+#endif
- QRegularExpressionMatchIterator reIter = re.globalMatch(results);
- QString updateNode;
- UpdateEntry theUpdate;
- QList<UpdateEntry> updates;
- while (reIter.hasNext())
- {
- updateNode = reIter.next().captured(1);
- theUpdate.version = versionRe.match(updateNode).captured(1);
- theUpdate.compontent = nameRe.match(updateNode).captured(1);
- updates << theUpdate;
- }
+ emit updateAvailable(version, url);
+}
- if (updates.isEmpty())
- emit noUpdatesAvailable();
- else
- emit updatesAvailable(updates);
+void UpdateManager::handleUpdatingError(const QString& errorMessage)
+{
+ NOTIFY_MANAGER->warn(tr("Could not check for updates (%1).").arg(errorMessage));
}
#endif // PORTABLE_CONFIG