Automatic update check with an opt out

Store the last version used, the next time we can check, and the decision
if the user does or does not want these checks in the settings.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2015-01-01 20:49:24 -08:00
parent 4ce3c1e8b8
commit 864c4ce94d
3 changed files with 56 additions and 10 deletions

View file

@ -64,7 +64,6 @@ MainWindow::MainWindow() : QMainWindow(),
actionPreviousDive(0), actionPreviousDive(0),
helpView(0), helpView(0),
state(VIEWALL), state(VIEWALL),
updateManager(0),
survey(0) survey(0)
{ {
Q_ASSERT_X(m_Instance == NULL, "MainWindow", "MainWindow recreated!"); Q_ASSERT_X(m_Instance == NULL, "MainWindow", "MainWindow recreated!");
@ -136,6 +135,7 @@ MainWindow::MainWindow() : QMainWindow(),
QLayoutItem *p = ui.gridLayout->takeAt(0); QLayoutItem *p = ui.gridLayout->takeAt(0);
ui.gridLayout->addWidget(toolBar, 0, 0); ui.gridLayout->addWidget(toolBar, 0, 0);
ui.gridLayout->addItem(p, 0, 1); ui.gridLayout->addItem(p, 0, 1);
updateManager = new UpdateManager(this);
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()

View file

@ -2,14 +2,31 @@
#include "usersurvey.h" #include "usersurvey.h"
#include <QtNetwork> #include <QtNetwork>
#include <QMessageBox> #include <QMessageBox>
#include <QDateTime>
#include "subsurfacewebservices.h" #include "subsurfacewebservices.h"
#include "ssrf-version.h" #include "ssrf-version.h"
#include "mainwindow.h"
UpdateManager::UpdateManager(QObject *parent) : QObject(parent) UpdateManager::UpdateManager(QObject *parent) : QObject(parent)
{ {
// is this the first time this version was run?
QSettings settings;
settings.beginGroup("UpdateManager");
if (settings.contains("LastVersionUsed") && settings.value("LastVersionUsed").toString() == GIT_VERSION_STRING) {
// this is the version we've been using
QString nextCheckString = settings.value("NextCheck").toString();
QDateTime nextCheck = QDateTime::fromString(nextCheckString, Qt::ISODate);
if (nextCheck > QDateTime::currentDateTime())
return;
}
settings.setValue("LastVersionUsed", QString(GIT_VERSION_STRING));
if (settings.contains("DontCheckForUpdates") && settings.value("DontCheckForUpdates") == "TRUE")
return;
checkForUpdates(true);
settings.setValue("NextCheck", QDateTime::currentDateTime().addDays(14).toString(Qt::ISODate));
} }
void UpdateManager::checkForUpdates() void UpdateManager::checkForUpdates(bool automatic)
{ {
QString os; QString os;
@ -22,7 +39,8 @@ void UpdateManager::checkForUpdates()
#else #else
os = "unknown"; os = "unknown";
#endif #endif
qDebug() << "checking for update";
isAutomaticCheck = automatic;
QString version = CANONICAL_VERSION_STRING; QString version = CANONICAL_VERSION_STRING;
QString url = QString("http://subsurface-divelog.org/updatecheck.html?os=%1&version=%2").arg(os, version); QString url = QString("http://subsurface-divelog.org/updatecheck.html?os=%1&version=%2").arg(os, version);
QNetworkRequest request; QNetworkRequest request;
@ -35,6 +53,7 @@ void UpdateManager::checkForUpdates()
void UpdateManager::requestReceived() void UpdateManager::requestReceived()
{ {
bool haveNewVersion = false;
QMessageBox msgbox; QMessageBox msgbox;
QString msgTitle = tr("Check for updates."); QString msgTitle = tr("Check for updates.");
QString msgText = "<h3>" + tr("Subsurface was unable to check for updates.") + "</h3>"; QString msgText = "<h3>" + tr("Subsurface was unable to check for updates.") + "</h3>";
@ -58,11 +77,13 @@ void UpdateManager::requestReceived()
if (responseBody == "OK") { if (responseBody == "OK") {
msgText = tr("You are using the latest version of Subsurface."); msgText = tr("You are using the latest version of Subsurface.");
} else if (responseBody.startsWith("http")) { } else if (responseBody.startsWith("http")) {
haveNewVersion = true;
msgText = tr("A new version of Subsurface is available.<br/>Click on:<br/><a href=\"%1\">%1</a><br/> to download it.") msgText = tr("A new version of Subsurface is available.<br/>Click on:<br/><a href=\"%1\">%1</a><br/> to download it.")
.arg(responseBody); .arg(responseBody);
} else if (responseBody.startsWith("Latest version")) { } else if (responseBody.startsWith("Latest version")) {
// the webservice backend doesn't localize - but it's easy enough to just replace the // the webservice backend doesn't localize - but it's easy enough to just replace the
// strings that it is likely to send back // strings that it is likely to send back
haveNewVersion = true;
responseBody.replace("Latest version is ", ""); responseBody.replace("Latest version is ", "");
responseBody.replace(". please check with your OS vendor for updates.", ""); responseBody.replace(". please check with your OS vendor for updates.", "");
msgText = QString("<b>") + tr("A new version of Subsurface is available.") + QString("</b><br/><br/>") + msgText = QString("<b>") + tr("A new version of Subsurface is available.") + QString("</b><br/><br/>") +
@ -71,16 +92,38 @@ void UpdateManager::requestReceived()
} else { } else {
// the webservice backend doesn't localize - but it's easy enough to just replace the // the webservice backend doesn't localize - but it's easy enough to just replace the
// strings that it is likely to send back // strings that it is likely to send back
haveNewVersion = true;
if (responseBody.contains("Newest release version is ")) if (responseBody.contains("Newest release version is "))
responseBody.replace("Newest release version is ", tr("Newest release version is ")); responseBody.replace("Newest release version is ", tr("Newest release version is "));
msgText = tr("The server returned the following information:").append("<br/><br/>").append(responseBody); msgText = tr("The server returned the following information:").append("<br/><br/>").append(responseBody);
msgbox.setIcon(QMessageBox::Warning); msgbox.setIcon(QMessageBox::Warning);
} }
} }
if (haveNewVersion || !isAutomaticCheck) {
msgbox.setWindowTitle(msgTitle); msgbox.setWindowTitle(msgTitle);
msgbox.setWindowIcon(QIcon(":/subsurface-icon")); msgbox.setWindowIcon(QIcon(":/subsurface-icon"));
msgbox.setText(msgText); msgbox.setText(msgText);
msgbox.setTextFormat(Qt::RichText); msgbox.setTextFormat(Qt::RichText);
msgbox.exec(); msgbox.exec();
}
if (isAutomaticCheck) {
QSettings settings;
settings.beginGroup("UpdateManager");
if (!settings.contains("DontCheckForUpdates")) {
// we allow an opt out of future checks
QMessageBox response(MainWindow::instance());
QString message = tr("Subsurface is checking every two weeks if a new version is available. If you don't want Subsurface to continue checking, please click Decline.");
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);
int ret = response.exec();
if (ret == QMessageBox::Accepted)
settings.setValue("DontCheckForUpdates", "FALSE");
else
settings.setValue("DontCheckForUpdates", "TRUE");
}
}
} }

View file

@ -10,11 +10,14 @@ class UpdateManager : public QObject {
Q_OBJECT Q_OBJECT
public: public:
explicit UpdateManager(QObject *parent = 0); explicit UpdateManager(QObject *parent = 0);
void checkForUpdates(); void checkForUpdates(bool automatic = false);
public public
slots: slots:
void requestReceived(); void requestReceived();
private:
bool isAutomaticCheck;
}; };
#endif // UPDATEMANAGER_H #endif // UPDATEMANAGER_H