logic: Added update checker connecting to online API
* connects to https://www.sourcetrail.com/api/v1/versions/latest * sends information about OS, platform, license type, current version and a unique user token * Automatic update check is enabled via Startscreen and performed once every 24 hours on window focus * Update check can be forced by clicking on "check for new version" on start screen * renamed TimePoint to TimeStamp fortune cookie message = In Träumen und im Leben ist nichts unmöglich.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "QtNetworkFactory.h"
|
||||
|
||||
#include "QtIDECommunicationController.h"
|
||||
#include "QtUpdateChecker.h"
|
||||
|
||||
QtNetworkFactory::QtNetworkFactory()
|
||||
{
|
||||
@@ -11,6 +12,11 @@ QtNetworkFactory::~QtNetworkFactory()
|
||||
}
|
||||
|
||||
std::shared_ptr<IDECommunicationController> QtNetworkFactory::createIDECommunicationController(StorageAccess* storageAccess) const
|
||||
{
|
||||
{
|
||||
return std::make_shared<QtIDECommunicationController>(nullptr, storageAccess);
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<UpdateChecker> QtNetworkFactory::createUpdateChecker() const
|
||||
{
|
||||
return std::make_shared<QtUpdateChecker>();
|
||||
}
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
#ifndef QT_NETWORK_FACTORY_H
|
||||
#define QT_NETWORK_FACTORY_H
|
||||
|
||||
#include "component/controller/NetworkFactory.h"
|
||||
#include "component/NetworkFactory.h"
|
||||
|
||||
class QtNetworkFactory : public NetworkFactory
|
||||
class QtNetworkFactory
|
||||
: public NetworkFactory
|
||||
{
|
||||
public:
|
||||
QtNetworkFactory();
|
||||
virtual ~QtNetworkFactory();
|
||||
|
||||
virtual std::shared_ptr<IDECommunicationController> createIDECommunicationController(StorageAccess* storageAccess) const;
|
||||
virtual std::shared_ptr<IDECommunicationController>
|
||||
createIDECommunicationController(StorageAccess* storageAccess) const override;
|
||||
virtual std::shared_ptr<UpdateChecker> createUpdateChecker() const override;
|
||||
};
|
||||
|
||||
#endif // QT_NETWORK_FACTORY_H
|
||||
#endif // QT_NETWORK_FACTORY_H
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "qt/network/QtRequest.h"
|
||||
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
|
||||
#include "utility/logging/logging.h"
|
||||
|
||||
QtRequest::QtRequest()
|
||||
{
|
||||
m_networkManager = new QNetworkAccessManager(this);
|
||||
QObject::connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));
|
||||
}
|
||||
|
||||
void QtRequest::sendRequest(QString url)
|
||||
{
|
||||
LOG_INFO_STREAM(<< "send HTTP request: " << url.toStdString());
|
||||
|
||||
QNetworkRequest request;
|
||||
request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
|
||||
request.setUrl(QUrl(url));
|
||||
m_networkManager->get(request);
|
||||
}
|
||||
|
||||
void QtRequest::finished(QNetworkReply *reply)
|
||||
{
|
||||
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
||||
QVariant redirectionTargetUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
|
||||
|
||||
if (reply->error() != QNetworkReply::NoError)
|
||||
{
|
||||
LOG_ERROR_STREAM(<< "An error occured during http request. ERRORCODE: " << reply->error());
|
||||
}
|
||||
|
||||
QByteArray bytes = reply->readAll();
|
||||
LOG_INFO_STREAM(<< "received HTTP reply: " << bytes.toStdString());
|
||||
|
||||
delete reply;
|
||||
|
||||
emit receivedData(bytes);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef QT_REQUEST_H
|
||||
#define QT_REQUEST_H
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QObject>
|
||||
|
||||
class QNetworkAccessManager;
|
||||
class QNetworkReply;
|
||||
|
||||
class QtRequest
|
||||
: public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
QtRequest();
|
||||
void sendRequest(QString url);
|
||||
|
||||
signals:
|
||||
void receivedData(QByteArray bytes);
|
||||
|
||||
private slots:
|
||||
void finished(QNetworkReply* reply);
|
||||
|
||||
private:
|
||||
QNetworkAccessManager* m_networkManager;
|
||||
};
|
||||
|
||||
#endif // QT_REQUEST_H
|
||||
@@ -0,0 +1,148 @@
|
||||
#include "qt/network/QtUpdateChecker.h"
|
||||
|
||||
#include <QDesktopServices>
|
||||
#include <QMessageBox>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QUrl>
|
||||
|
||||
#include "LicenseChecker.h"
|
||||
#include "qt/network/QtRequest.h"
|
||||
#include "settings/ApplicationSettings.h"
|
||||
#include "utility/Version.h"
|
||||
#include "utility/utility.h"
|
||||
#include "utility/utilityUuid.h"
|
||||
|
||||
void QtUpdateChecker::check(bool force)
|
||||
{
|
||||
ApplicationSettings* appSettings = ApplicationSettings::getInstance().get();
|
||||
|
||||
if (!force && TimeStamp::now().deltaHours(appSettings->getLastUpdateCheck()) < 24)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
appSettings->setLastUpdateCheck(TimeStamp::now());
|
||||
appSettings->save();
|
||||
|
||||
QString urlString = "https://www.sourcetrail.com/api/v1/versions/latest";
|
||||
|
||||
// OS
|
||||
std::string osString;
|
||||
|
||||
#if defined(Q_OS_WIN)
|
||||
osString = "windows";
|
||||
#elif defined(Q_OS_MACOS)
|
||||
osString = "macOS";
|
||||
#elif defined(Q_OS_LINUX)
|
||||
osString = "linux";
|
||||
#endif
|
||||
|
||||
if (!osString.size())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
urlString += ("?os=" + osString).c_str();
|
||||
|
||||
// architecture
|
||||
std::string platformString = (utility::getApplicationArchitectureType() == APPLICATION_ARCHITECTURE_X86_64 ? "64" : "32");
|
||||
urlString += ("&platform=" + platformString + "bit").c_str();
|
||||
|
||||
// version
|
||||
urlString += ("&version=" + Version::getApplicationVersion().toDisplayString()).c_str();
|
||||
|
||||
// license
|
||||
std::string licenseString;
|
||||
switch (LicenseChecker::getInstance()->getCurrentLicenseType())
|
||||
{
|
||||
case MessageEnteredLicense::LICENSE_NONE:
|
||||
licenseString = "trial";
|
||||
break;
|
||||
case MessageEnteredLicense::LICENSE_TEST:
|
||||
licenseString = "test";
|
||||
break;
|
||||
case MessageEnteredLicense::LICENSE_NON_COMMERCIAL:
|
||||
licenseString = "private";
|
||||
break;
|
||||
case MessageEnteredLicense::LICENSE_COMMERCIAL:
|
||||
licenseString = "commercial";
|
||||
break;
|
||||
}
|
||||
|
||||
urlString += ("&license=" + licenseString).c_str();
|
||||
|
||||
// user token
|
||||
std::string token = appSettings->getUserToken();
|
||||
if (!token.size())
|
||||
{
|
||||
token = utility::getUuidString();
|
||||
appSettings->setUserToken(token);
|
||||
appSettings->save();
|
||||
}
|
||||
|
||||
urlString += ("&token=" + token).c_str();
|
||||
|
||||
// send request
|
||||
QtRequest* request = new QtRequest();
|
||||
QObject::connect(request, &QtRequest::receivedData,
|
||||
[force, request](QByteArray bytes)
|
||||
{
|
||||
do
|
||||
{
|
||||
QJsonDocument doc = QJsonDocument::fromJson(bytes);
|
||||
if (!doc.isObject())
|
||||
{
|
||||
LOG_ERROR_STREAM(<< "Update response couldn't be parsed as JSON");
|
||||
break;
|
||||
}
|
||||
|
||||
QString version = doc.object().find("version")->toString();
|
||||
QString url = doc.object().find("url")->toString();
|
||||
|
||||
Version updateVersion = Version::fromString(version.toStdString());
|
||||
if (!updateVersion.isValid())
|
||||
{
|
||||
LOG_ERROR_STREAM(<< "update version string is not valid: " << version.toStdString());
|
||||
break;
|
||||
}
|
||||
|
||||
if (updateVersion > Version::getApplicationVersion())
|
||||
{
|
||||
QMessageBox msgBox;
|
||||
msgBox.setText("Update Check");
|
||||
msgBox.setInformativeText(
|
||||
"Sourcetrial " + version + " is available for download: <a href=\"" + url + "\">" + url + "</a>");
|
||||
msgBox.addButton("Close", QMessageBox::ButtonRole::NoRole);
|
||||
QPushButton* but = msgBox.addButton("Download", QMessageBox::ButtonRole::YesRole);
|
||||
msgBox.setDefaultButton(but);
|
||||
|
||||
if (msgBox.exec() == 1)
|
||||
{
|
||||
QDesktopServices::openUrl(QUrl(url, QUrl::TolerantMode));
|
||||
}
|
||||
}
|
||||
else if (force)
|
||||
{
|
||||
QMessageBox msgBox;
|
||||
msgBox.setText("Update Check");
|
||||
msgBox.setInformativeText(
|
||||
("Sourcetrial " + Version::getApplicationVersion().toDisplayString() + " is still up-to-date.").c_str());
|
||||
msgBox.setStandardButtons(QMessageBox::Ok);
|
||||
msgBox.setDefaultButton(QMessageBox::Ok);
|
||||
msgBox.exec();
|
||||
}
|
||||
}
|
||||
while (false);
|
||||
|
||||
request->deleteLater();
|
||||
}
|
||||
);
|
||||
|
||||
request->sendRequest(urlString);
|
||||
}
|
||||
|
||||
void QtUpdateChecker::checkUpdate()
|
||||
{
|
||||
check();
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef QT_UPDATE_CHECKER_H
|
||||
#define QT_UPDATE_CHECKER_H
|
||||
|
||||
#include "UpdateChecker.h"
|
||||
|
||||
class QtUpdateChecker
|
||||
: public UpdateChecker
|
||||
{
|
||||
public:
|
||||
static void check(bool force = false);
|
||||
|
||||
virtual void checkUpdate() override;
|
||||
};
|
||||
|
||||
#endif // QT_UPDATE_CHECKER_H
|
||||
Reference in New Issue
Block a user