2017-04-27 18:26:05 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "desktop-widgets/updatemanager.h"
|
|
|
|
#include "core/qthelper.h"
|
2014-04-02 19:41:39 +00:00
|
|
|
#include <QtNetwork>
|
|
|
|
#include <QMessageBox>
|
2015-01-25 19:27:42 +00:00
|
|
|
#include <QUuid>
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "desktop-widgets/subsurfacewebservices.h"
|
|
|
|
#include "core/version.h"
|
|
|
|
#include "desktop-widgets/mainwindow.h"
|
|
|
|
#include "core/cloudstorage.h"
|
2018-08-15 09:56:17 +00:00
|
|
|
#include "core/settings/qPrefUpdateManager.h"
|
2014-04-02 19:41:39 +00:00
|
|
|
|
2015-06-22 13:42:02 +00:00
|
|
|
UpdateManager::UpdateManager(QObject *parent) :
|
|
|
|
QObject(parent),
|
|
|
|
isAutomaticCheck(false)
|
2014-04-02 19:41:39 +00:00
|
|
|
{
|
2018-08-15 09:56:17 +00:00
|
|
|
if (qPrefUpdateManager::dont_check_for_updates())
|
2015-01-03 21:47:05 +00:00
|
|
|
return;
|
2016-08-10 19:40:14 +00:00
|
|
|
|
2018-08-15 09:56:17 +00:00
|
|
|
if (qPrefUpdateManager::last_version_used() == subsurface_git_version() &&
|
|
|
|
qPrefUpdateManager::next_check() > QDate::currentDate())
|
2016-08-10 19:40:14 +00:00
|
|
|
return;
|
|
|
|
|
2018-08-15 09:56:17 +00:00
|
|
|
qPrefUpdateManager::set_last_version_used(subsurface_git_version());
|
2016-08-10 19:40:14 +00:00
|
|
|
|
2015-01-03 21:47:05 +00:00
|
|
|
checkForUpdates(true);
|
2014-04-02 19:41:39 +00:00
|
|
|
}
|
|
|
|
|
2015-01-02 04:49:24 +00:00
|
|
|
void UpdateManager::checkForUpdates(bool automatic)
|
2014-04-02 19:41:39 +00:00
|
|
|
{
|
|
|
|
QString os;
|
|
|
|
|
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
os = "win";
|
|
|
|
#elif defined(Q_OS_MAC)
|
|
|
|
os = "osx";
|
2014-04-02 19:55:33 +00:00
|
|
|
#elif defined(Q_OS_LINUX)
|
|
|
|
os = "linux";
|
2014-04-02 19:41:39 +00:00
|
|
|
#else
|
|
|
|
os = "unknown";
|
|
|
|
#endif
|
2015-01-02 04:49:24 +00:00
|
|
|
isAutomaticCheck = automatic;
|
2015-02-15 18:25:18 +00:00
|
|
|
QString version = subsurface_canonical_version();
|
2024-01-07 22:35:56 +00:00
|
|
|
QString url = QString("http://updatecheck.subsurface-divelog.org/updatecheck.html?os=%1&version=%2").arg(os, version);
|
2014-07-31 18:20:11 +00:00
|
|
|
QNetworkRequest request;
|
|
|
|
request.setUrl(url);
|
|
|
|
request.setRawHeader("Accept", "text/xml");
|
2015-02-23 17:09:48 +00:00
|
|
|
QString userAgent = getUserAgent();
|
2014-07-31 18:20:11 +00:00
|
|
|
request.setRawHeader("User-Agent", userAgent.toUtf8());
|
2016-01-26 17:49:47 +00:00
|
|
|
connect(manager()->get(request), SIGNAL(finished()), this, SLOT(requestReceived()), Qt::UniqueConnection);
|
2014-04-02 19:41:39 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 20:20:21 +00:00
|
|
|
void UpdateManager::requestReceived()
|
2014-04-02 19:41:39 +00:00
|
|
|
{
|
2015-01-02 04:49:24 +00:00
|
|
|
bool haveNewVersion = false;
|
2014-04-02 19:41:39 +00:00
|
|
|
QMessageBox msgbox;
|
|
|
|
QString msgTitle = tr("Check for updates.");
|
2014-07-09 17:26:43 +00:00
|
|
|
QString msgText = "<h3>" + tr("Subsurface was unable to check for updates.") + "</h3>";
|
2014-04-02 19:41:39 +00:00
|
|
|
|
2014-07-16 20:20:21 +00:00
|
|
|
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
|
2014-04-02 19:41:39 +00:00
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
//Network Error
|
2014-07-09 17:26:43 +00:00
|
|
|
msgText = msgText + "<br/><b>" + tr("The following error occurred:") + "</b><br/>" + reply->errorString()
|
|
|
|
+ "<br/><br/><b>" + tr("Please check your internet connection.") + "</b>";
|
2014-04-02 19:41:39 +00:00
|
|
|
} else {
|
|
|
|
//No network error
|
2015-01-26 06:19:05 +00:00
|
|
|
QString responseBody(reply->readAll());
|
|
|
|
QString responseLink;
|
|
|
|
if (responseBody.contains('"'))
|
|
|
|
responseLink = responseBody.split("\"").at(1);
|
2014-04-02 19:41:39 +00:00
|
|
|
|
|
|
|
msgbox.setIcon(QMessageBox::Information);
|
|
|
|
if (responseBody == "OK") {
|
2014-11-25 15:47:24 +00:00
|
|
|
msgText = tr("You are using the latest version of Subsurface.");
|
2015-01-26 06:19:05 +00:00
|
|
|
} else if (responseBody.startsWith("[\"http")) {
|
2015-01-02 04:49:24 +00:00
|
|
|
haveNewVersion = true;
|
2014-11-25 15:47:24 +00:00
|
|
|
msgText = tr("A new version of Subsurface is available.<br/>Click on:<br/><a href=\"%1\">%1</a><br/> to download it.")
|
2015-01-26 06:19:05 +00:00
|
|
|
.arg(responseLink);
|
2014-04-02 19:41:39 +00:00
|
|
|
} else if (responseBody.startsWith("Latest version")) {
|
2014-08-26 17:30:41 +00:00
|
|
|
// the webservice backend doesn't localize - but it's easy enough to just replace the
|
|
|
|
// strings that it is likely to send back
|
2015-01-02 04:49:24 +00:00
|
|
|
haveNewVersion = true;
|
2014-11-25 15:47:24 +00:00
|
|
|
msgText = QString("<b>") + tr("A new version of Subsurface is available.") + QString("</b><br/><br/>") +
|
2015-01-26 06:19:05 +00:00
|
|
|
tr("Latest version is %1, please check %2 our download page %3 for information in how to update.")
|
|
|
|
.arg(responseLink).arg("<a href=\"http://subsurface-divelog.org/download\">").arg("</a>");
|
2014-04-02 19:41:39 +00:00
|
|
|
} else {
|
2014-08-26 17:30:41 +00:00
|
|
|
// the webservice backend doesn't localize - but it's easy enough to just replace the
|
|
|
|
// strings that it is likely to send back
|
2015-01-31 06:07:32 +00:00
|
|
|
if (!responseBody.contains("latest development") &&
|
|
|
|
!responseBody.contains("newer") &&
|
|
|
|
!responseBody.contains("beta", Qt::CaseInsensitive))
|
2015-01-25 16:13:41 +00:00
|
|
|
haveNewVersion = true;
|
2014-08-26 17:30:41 +00:00
|
|
|
if (responseBody.contains("Newest release version is "))
|
|
|
|
responseBody.replace("Newest release version is ", tr("Newest release version is "));
|
2015-01-01 20:13:27 +00:00
|
|
|
msgText = tr("The server returned the following information:").append("<br/><br/>").append(responseBody);
|
2014-04-02 19:41:39 +00:00
|
|
|
msgbox.setIcon(QMessageBox::Warning);
|
|
|
|
}
|
|
|
|
}
|
2015-01-02 04:49:24 +00:00
|
|
|
if (haveNewVersion || !isAutomaticCheck) {
|
|
|
|
msgbox.setWindowTitle(msgTitle);
|
2017-11-28 07:37:01 +00:00
|
|
|
msgbox.setWindowIcon(QIcon(":subsurface-icon"));
|
2015-01-02 04:49:24 +00:00
|
|
|
msgbox.setText(msgText);
|
|
|
|
msgbox.setTextFormat(Qt::RichText);
|
|
|
|
msgbox.exec();
|
|
|
|
}
|
|
|
|
if (isAutomaticCheck) {
|
2018-08-03 18:25:02 +00:00
|
|
|
auto update_settings = qPrefUpdateManager::instance();
|
2021-01-05 20:07:41 +00:00
|
|
|
if (!update_settings->dont_check_for_updates() && update_settings->next_check() == QDate::fromJulianDay(0)) {
|
2015-01-02 04:49:24 +00:00
|
|
|
// we allow an opt out of future checks
|
|
|
|
QMessageBox response(MainWindow::instance());
|
2016-08-10 19:40:14 +00:00
|
|
|
QString message = tr("Subsurface is checking every two weeks if a new version is available. "
|
2017-02-20 07:46:40 +00:00
|
|
|
"\nIf you don't want Subsurface to continue checking, please click Decline.");
|
2015-01-02 04:49:24 +00:00
|
|
|
response.addButton(tr("Decline"), QMessageBox::RejectRole);
|
|
|
|
response.addButton(tr("Accept"), QMessageBox::AcceptRole);
|
|
|
|
response.setText(message);
|
|
|
|
response.setWindowTitle(tr("Automatic check for updates"));
|
|
|
|
response.setIcon(QMessageBox::Question);
|
|
|
|
response.setWindowModality(Qt::WindowModal);
|
2018-08-03 18:25:02 +00:00
|
|
|
update_settings->set_dont_check_for_updates(response.exec() != QMessageBox::Accepted);
|
2015-01-02 04:49:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-05 20:07:41 +00:00
|
|
|
qPrefUpdateManager::set_next_check(QDate::currentDate().addDays(14));
|
2014-04-02 19:41:39 +00:00
|
|
|
}
|