2016-04-05 05:02:03 +00:00
# include "desktop-widgets/updatemanager.h"
# include "core/helpers.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"
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
{
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
2015-02-15 18:25:18 +00:00
if ( settings . value ( " LastVersionUsed " ) . toString ( ) ! = subsurface_git_version ( ) ) {
2015-01-03 21:47:05 +00:00
// we have just updated - wait two weeks before you check again
2015-02-15 18:25:18 +00:00
settings . setValue ( " LastVersionUsed " , QString ( subsurface_git_version ( ) ) ) ;
2015-01-03 21:47:05 +00:00
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
}
2015-02-15 18:25:18 +00:00
settings . setValue ( " LastVersionUsed " , QString ( subsurface_git_version ( ) ) ) ;
2015-01-02 04:49:24 +00:00
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-02-15 18:25:18 +00:00
QString version = subsurface_canonical_version ( ) ;
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 " ) ;
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-07-10 20:28:28 +00:00
# ifndef SUBSURFACE_MOBILE
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 " ) ;
}
}
2015-07-10 20:28:28 +00:00
# endif
2014-04-02 19:41:39 +00:00
}