2014-04-02 19:41:39 +00:00
# include "updatemanager.h"
2014-07-31 18:20:11 +00:00
# include "usersurvey.h"
2014-04-02 19:41:39 +00:00
# include <QtNetwork>
# include <QMessageBox>
2015-01-25 19:27:42 +00:00
# include <QUuid>
2014-04-02 19:41:39 +00:00
# include "subsurfacewebservices.h"
# include "ssrf-version.h"
2015-01-02 04:49:24 +00:00
# include "mainwindow.h"
2014-04-02 19:41:39 +00:00
2014-05-22 18:40:22 +00:00
UpdateManager : : UpdateManager ( QObject * parent ) : QObject ( parent )
2014-04-02 19:41:39 +00:00
{
2015-01-02 04:49:24 +00:00
// is this the first time this version was run?
QSettings settings ;
settings . beginGroup ( " UpdateManager " ) ;
2015-01-03 21:47:05 +00:00
if ( settings . contains ( " DontCheckForUpdates " ) & & settings . value ( " DontCheckForUpdates " ) = = " TRUE " )
return ;
if ( settings . contains ( " LastVersionUsed " ) ) {
// we have checked at least once before
if ( settings . value ( " LastVersionUsed " ) . toString ( ) ! = GIT_VERSION_STRING ) {
// we have just updated - wait two weeks before you check again
settings . setValue ( " LastVersionUsed " , QString ( GIT_VERSION_STRING ) ) ;
settings . setValue ( " NextCheck " , QDateTime : : currentDateTime ( ) . addDays ( 14 ) . toString ( Qt : : ISODate ) ) ;
2015-01-25 16:13:41 +00:00
} else {
// is it time to check again?
QString nextCheckString = settings . value ( " NextCheck " ) . toString ( ) ;
QDateTime nextCheck = QDateTime : : fromString ( nextCheckString , Qt : : ISODate ) ;
if ( nextCheck > QDateTime : : currentDateTime ( ) )
return ;
2015-01-03 21:47:05 +00:00
}
2015-01-02 04:49:24 +00:00
}
settings . setValue ( " LastVersionUsed " , QString ( GIT_VERSION_STRING ) ) ;
settings . setValue ( " NextCheck " , QDateTime : : currentDateTime ( ) . addDays ( 14 ) . toString ( Qt : : ISODate ) ) ;
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-01-01 20:02:27 +00:00
QString version = CANONICAL_VERSION_STRING ;
2015-01-25 19:27:42 +00:00
QString uuidString = getUUID ( ) ;
QString url = QString ( " http://subsurface-divelog.org/updatecheck.html?os=%1&version=%2&uuid=%3 " ) . arg ( os , version , uuidString ) ;
2014-07-31 18:20:11 +00:00
QNetworkRequest request ;
request . setUrl ( url ) ;
request . setRawHeader ( " Accept " , " text/xml " ) ;
2014-08-08 18:13:05 +00:00
QString userAgent = UserSurvey : : getUserAgent ( ) ;
2014-07-31 18:20:11 +00:00
request . setRawHeader ( " User-Agent " , userAgent . toUtf8 ( ) ) ;
2015-01-25 16:13:41 +00:00
connect ( SubsurfaceWebServices : : manager ( ) - > get ( request ) , SIGNAL ( finished ( ) ) , this , SLOT ( requestReceived ( ) ) , Qt : : UniqueConnection ) ;
2014-04-02 19:41:39 +00:00
}
2015-01-25 19:27:42 +00:00
QString UpdateManager : : getUUID ( )
{
QString uuidString ;
QSettings settings ;
settings . beginGroup ( " UpdateManager " ) ;
if ( settings . contains ( " UUID " ) ) {
uuidString = settings . value ( " UUID " ) . toString ( ) ;
} else {
QUuid uuid = QUuid : : createUuid ( ) ;
uuidString = uuid . toString ( ) ;
settings . setValue ( " UUID " , uuidString ) ;
}
uuidString . replace ( " { " , " " ) . replace ( " } " , " " ) ;
return uuidString ;
}
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-25 20:19:16 +00:00
if ( ! 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 ) ;
msgbox . setWindowIcon ( QIcon ( " :/subsurface-icon " ) ) ;
msgbox . setText ( msgText ) ;
msgbox . setTextFormat ( Qt : : RichText ) ;
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 " ) ;
}
}
2014-04-02 19:41:39 +00:00
}