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-02 04:49:24 +00:00
# include <QDateTime>
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 " ) ;
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 ) ) ;
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
qDebug ( ) < < " checking for update " ;
isAutomaticCheck = automatic ;
2015-01-01 20:02:27 +00:00
QString version = CANONICAL_VERSION_STRING ;
QString url = QString ( " http://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 " ) ;
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 ( ) ) ;
connect ( SubsurfaceWebServices : : manager ( ) - > get ( request ) , SIGNAL ( finished ( ) ) , this , SLOT ( requestReceived ( ) ) ) ;
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
QString response ( reply - > readAll ( ) ) ;
2014-08-08 17:39:46 +00:00
QString responseBody ;
if ( response . contains ( ' " ' ) )
responseBody = response . split ( " \" " ) . at ( 1 ) ;
else
responseBody = response ;
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. " ) ;
2014-04-02 19:41:39 +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. " )
2014-04-02 19:41:39 +00:00
. arg ( responseBody ) ;
} 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-08-26 17:30:41 +00:00
responseBody . replace ( " Latest version is " , " " ) ;
responseBody . replace ( " . please check with your OS vendor for updates. " , " " ) ;
2014-11-25 15:47:24 +00:00
msgText = QString ( " <b> " ) + tr ( " A new version of Subsurface is available. " ) + QString ( " </b><br/><br/> " ) +
2014-08-26 17:30:41 +00:00
tr ( " Latest version is %1, please check with your OS vendor for updates. " )
2014-04-02 19:41:39 +00:00
. arg ( responseBody ) ;
} 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-02 04:49:24 +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
}