2013-08-09 06:57:18 +02:00
# include "lc_global.h"
# include "lc_qupdatedialog.h"
# include "ui_lc_qupdatedialog.h"
# include "lc_application.h"
# include "lc_library.h"
2013-08-12 02:26:52 +02:00
# include "lc_profile.h"
2013-08-09 06:57:18 +02:00
2013-08-12 02:26:52 +02:00
void lcDoInitialUpdateCheck ( )
2013-08-09 06:57:18 +02:00
{
2013-08-12 02:26:52 +02:00
int updateFrequency = lcGetProfileInt ( LC_PROFILE_CHECK_UPDATES ) ;
2013-08-09 06:57:18 +02:00
2013-08-12 02:26:52 +02:00
if ( updateFrequency = = 0 )
return ;
2013-08-09 06:57:18 +02:00
2013-08-12 02:26:52 +02:00
QSettings settings ;
2013-08-20 03:23:41 +02:00
QDateTime checkTime = settings . value ( " Updates/LastCheck " , QDateTime ( ) ) . toDateTime ( ) ;
2013-08-09 06:57:18 +02:00
2013-08-12 02:26:52 +02:00
if ( ! checkTime . isNull ( ) )
2013-08-09 06:57:18 +02:00
{
2013-08-20 03:23:41 +02:00
checkTime . addDays ( updateFrequency = = 1 ? 1 : 7 ) ;
2013-08-12 02:26:52 +02:00
if ( checkTime > QDateTime : : currentDateTimeUtc ( ) )
return ;
2013-08-09 06:57:18 +02:00
}
2013-08-12 02:26:52 +02:00
new lcQUpdateDialog ( NULL , ( void * ) 1 ) ;
}
lcQUpdateDialog : : lcQUpdateDialog ( QWidget * parent , void * data ) :
QDialog ( parent ) ,
ui ( new Ui : : lcQUpdateDialog )
{
ui - > setupUi ( this ) ;
initialUpdate = ( bool ) data ;
connect ( this , SIGNAL ( finished ( int ) ) , this , SLOT ( finished ( int ) ) ) ;
ui - > status - > setText ( tr ( " Connecting to update server... " ) ) ;
manager = new QNetworkAccessManager ( this ) ;
connect ( manager , SIGNAL ( finished ( QNetworkReply * ) ) , this , SLOT ( replyFinished ( QNetworkReply * ) ) ) ;
updateReply = manager - > get ( QNetworkRequest ( QUrl ( " http://www.leocad.org/updates.txt " ) ) ) ;
2013-08-09 06:57:18 +02:00
}
lcQUpdateDialog : : ~ lcQUpdateDialog ( )
{
if ( updateReply )
{
updateReply - > abort ( ) ;
updateReply - > deleteLater ( ) ;
}
if ( manager )
manager - > deleteLater ( ) ;
delete ui ;
}
2013-08-20 03:23:41 +02:00
void lcQUpdateDialog : : accept ( )
{
QSettings settings ;
settings . setValue ( " Updates/IgnoreVersion " , versionData ) ;
QDialog : : accept ( ) ;
}
void lcQUpdateDialog : : reject ( )
2013-08-09 06:57:18 +02:00
{
if ( updateReply )
{
updateReply - > abort ( ) ;
updateReply - > deleteLater ( ) ;
updateReply = NULL ;
}
2013-08-20 03:23:41 +02:00
QDialog : : reject ( ) ;
2013-08-09 06:57:18 +02:00
}
2013-08-12 02:26:52 +02:00
void lcQUpdateDialog : : finished ( int result )
2013-08-09 06:57:18 +02:00
{
2013-08-12 02:26:52 +02:00
if ( initialUpdate )
deleteLater ( ) ;
2013-08-09 06:57:18 +02:00
}
2013-08-12 02:26:52 +02:00
void lcQUpdateDialog : : replyFinished ( QNetworkReply * reply )
2013-08-09 06:57:18 +02:00
{
2013-08-12 02:26:52 +02:00
bool updateAvailable = false ;
2013-08-09 06:57:18 +02:00
2013-08-12 02:26:52 +02:00
if ( reply - > error ( ) = = QNetworkReply : : NoError )
2013-08-09 06:57:18 +02:00
{
2013-08-12 02:26:52 +02:00
int majorVersion , minorVersion , patchVersion ;
int parts ;
2013-08-20 03:23:41 +02:00
versionData = reply - > readAll ( ) ;
const char * update = versionData ;
2013-08-09 06:57:18 +02:00
2013-08-20 03:23:41 +02:00
QSettings settings ;
QByteArray ignoreUpdate = settings . value ( " Updates/IgnoreVersion " , QByteArray ( ) ) . toByteArray ( ) ;
if ( initialUpdate & & ignoreUpdate = = versionData )
{
updateAvailable = false ;
}
else if ( sscanf ( update , " %d.%d.%d %d " , & majorVersion , & minorVersion , & patchVersion , & parts ) = = 4 )
2013-08-09 06:57:18 +02:00
{
2013-08-12 02:26:52 +02:00
QString status ;
if ( majorVersion > LC_VERSION_MAJOR )
2013-08-09 06:57:18 +02:00
updateAvailable = true ;
2013-08-12 02:26:52 +02:00
else if ( majorVersion = = LC_VERSION_MAJOR )
2013-08-09 06:57:18 +02:00
{
2013-08-12 02:26:52 +02:00
if ( minorVersion > LC_VERSION_MINOR )
2013-08-09 06:57:18 +02:00
updateAvailable = true ;
2013-08-12 02:26:52 +02:00
else if ( minorVersion = = LC_VERSION_MINOR )
{
if ( patchVersion > LC_VERSION_PATCH )
updateAvailable = true ;
}
2013-08-09 06:57:18 +02:00
}
2013-08-12 02:26:52 +02:00
if ( updateAvailable )
2013-10-21 23:49:26 +02:00
status = QString ( tr ( " <p>There's a newer version of LeoCAD available for download (%1.%2.%3).</p> " ) ) . arg ( QString : : number ( majorVersion ) , QString : : number ( minorVersion ) , QString : : number ( patchVersion ) ) ;
2013-08-12 02:26:52 +02:00
else
status = tr ( " <p>You are using the latest LeoCAD version.</p> " ) ;
2013-08-09 06:57:18 +02:00
2013-08-12 02:26:52 +02:00
lcPiecesLibrary * library = lcGetPiecesLibrary ( ) ;
2013-08-09 06:57:18 +02:00
2013-08-12 02:26:52 +02:00
if ( library - > mNumOfficialPieces )
2013-08-09 06:57:18 +02:00
{
2013-08-12 02:26:52 +02:00
if ( parts > library - > mNumOfficialPieces )
{
status + = tr ( " <p>There are new parts available.</p> " ) ;
updateAvailable = true ;
}
else
status + = tr ( " <p>There are no new parts available at this time.</p> " ) ;
2013-08-09 06:57:18 +02:00
}
2013-08-12 02:26:52 +02:00
if ( updateAvailable )
{
status + = tr ( " <p>Visit <a href= \" http://www.leocad.org/files/ \" >http://www.leocad.org/files/</a> to download.</p> " ) ;
}
ui - > status - > setText ( status ) ;
2013-08-09 06:57:18 +02:00
}
2013-08-12 02:26:52 +02:00
else
ui - > status - > setText ( tr ( " Error parsing update information. " ) ) ;
2013-08-09 06:57:18 +02:00
2013-08-20 03:23:41 +02:00
settings . setValue ( " Updates/LastCheck " , QDateTime : : currentDateTimeUtc ( ) ) ;
2013-08-12 02:26:52 +02:00
updateReply = NULL ;
reply - > deleteLater ( ) ;
2013-08-09 06:57:18 +02:00
}
else
2013-08-12 02:26:52 +02:00
ui - > status - > setText ( tr ( " Error connecting to the update server. " ) ) ;
if ( initialUpdate )
{
if ( updateAvailable )
show ( ) ;
else
deleteLater ( ) ;
}
2013-08-09 06:57:18 +02:00
2013-08-20 03:23:41 +02:00
if ( updateAvailable )
ui - > buttonBox - > setStandardButtons ( QDialogButtonBox : : Close | QDialogButtonBox : : Ignore ) ;
else
ui - > buttonBox - > setStandardButtons ( QDialogButtonBox : : Close ) ;
2013-08-09 06:57:18 +02:00
}