2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
2014-01-11 16:11:42 +01:00
* Copyright ( C ) 2010 - 2014 David Rosca < nowrep @ gmail . com >
2011-03-03 18:29:20 +01:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
2011-03-02 16:57:41 +01:00
# include "updater.h"
2014-02-19 22:07:21 +01:00
# include "browserwindow.h"
2013-02-12 16:52:30 +01:00
# include "qztools.h"
2012-01-21 23:19:38 +01:00
# include "mainapplication.h"
2011-03-02 16:57:41 +01:00
# include "tabwidget.h"
2011-04-24 22:40:35 +02:00
# include "desktopnotificationsfactory.h"
2015-10-06 09:46:23 +02:00
# include "networkmanager.h"
2011-03-02 16:57:41 +01:00
2012-02-29 18:33:50 +01:00
# include <QTimer>
# include <QNetworkRequest>
# include <QNetworkReply>
2013-03-17 10:26:47 +01:00
Updater : : Version : : Version ( const QString & s )
2014-02-01 19:21:49 +01:00
: isValid ( false )
, majorVersion ( - 1 )
, minorVersion ( - 1 )
, revisionNumber ( - 1 )
2011-03-02 16:57:41 +01:00
{
2013-03-17 10:26:47 +01:00
isValid = false ;
2011-03-02 16:57:41 +01:00
2013-03-17 10:26:47 +01:00
QStringList v = s . split ( QLatin1Char ( ' . ' ) ) ;
2011-11-06 17:01:23 +01:00
if ( v . count ( ) ! = 3 ) {
2013-03-17 10:26:47 +01:00
return ;
2011-11-06 17:01:23 +01:00
}
2011-10-05 16:48:23 +02:00
2013-03-12 14:09:12 +01:00
bool ok ;
2011-10-05 16:48:23 +02:00
2013-03-17 10:26:47 +01:00
majorVersion = v . at ( 0 ) . toInt ( & ok ) ;
2013-03-12 14:09:12 +01:00
if ( ! ok ) {
2013-03-17 10:26:47 +01:00
return ;
2011-11-06 17:01:23 +01:00
}
2011-10-18 14:30:17 +02:00
2013-03-17 10:26:47 +01:00
minorVersion = v . at ( 1 ) . toInt ( & ok ) ;
2013-03-12 14:09:12 +01:00
if ( ! ok ) {
2013-03-17 10:26:47 +01:00
return ;
2011-10-05 16:48:23 +02:00
}
2013-03-17 10:26:47 +01:00
revisionNumber = v . at ( 2 ) . toInt ( & ok ) ;
2013-03-12 14:09:12 +01:00
if ( ! ok ) {
2013-03-17 10:26:47 +01:00
return ;
}
isValid = majorVersion > = 0 & & minorVersion > = 0 & & revisionNumber > = 0 ;
}
bool Updater : : Version : : operator < ( const Updater : : Version & other ) const
{
if ( this - > majorVersion ! = other . majorVersion ) {
return this - > majorVersion < other . majorVersion ;
}
if ( this - > minorVersion ! = other . minorVersion ) {
return this - > minorVersion < other . minorVersion ;
}
if ( this - > revisionNumber ! = other . revisionNumber ) {
return this - > revisionNumber < other . revisionNumber ;
}
return false ;
}
bool Updater : : Version : : operator > ( const Updater : : Version & other ) const
{
if ( * this = = other ) {
return false ;
}
return ! operator < ( other ) ;
}
bool Updater : : Version : : operator = = ( const Updater : : Version & other ) const
{
return ( this - > majorVersion = = other . majorVersion & &
this - > minorVersion = = other . minorVersion & &
this - > revisionNumber = = other . revisionNumber ) ;
}
bool Updater : : Version : : operator > = ( const Updater : : Version & other ) const
{
if ( * this = = other ) {
return true ;
}
return * this > other ;
}
bool Updater : : Version : : operator < = ( const Updater : : Version & other ) const
{
if ( * this = = other ) {
return true ;
2011-10-05 16:48:23 +02:00
}
2013-03-17 10:26:47 +01:00
return * this < other ;
}
2011-10-05 16:48:23 +02:00
2014-02-04 23:03:21 +01:00
QString Updater : : Version : : versionString ( ) const
{
return QString ( " %1.%2.%3 " ) . arg ( majorVersion , minorVersion , revisionNumber ) ;
}
2014-02-19 22:07:21 +01:00
Updater : : Updater ( BrowserWindow * window , QObject * parent )
2013-03-17 10:26:47 +01:00
: QObject ( parent )
2014-02-19 22:07:21 +01:00
, m_window ( window )
2013-03-17 10:26:47 +01:00
{
QTimer : : singleShot ( 60 * 1000 , this , SLOT ( start ( ) ) ) ; // Start checking after 1 minute
2011-10-05 16:48:23 +02:00
}
2011-03-02 16:57:41 +01:00
void Updater : : start ( )
{
2014-02-19 22:07:21 +01:00
QUrl url = QUrl ( QString ( " %1/update.php?v=%2&os=%3 " ) . arg ( Qz : : WWWADDRESS ,
Qz : : VERSION ,
2013-02-24 23:02:55 +01:00
QzTools : : operatingSystem ( ) ) ) ;
2013-02-12 16:52:30 +01:00
startDownloadingUpdateInfo ( url ) ;
2011-03-02 16:57:41 +01:00
}
void Updater : : startDownloadingUpdateInfo ( const QUrl & url )
{
2015-10-06 09:46:23 +02:00
QNetworkReply * reply = mApp - > networkManager ( ) - > get ( QNetworkRequest ( QUrl ( url ) ) ) ;
2011-03-02 16:57:41 +01:00
2015-10-06 09:46:23 +02:00
connect ( reply , SIGNAL ( finished ( ) ) , this , SLOT ( downCompleted ( ) ) ) ;
2011-03-02 16:57:41 +01:00
}
2015-10-06 09:46:23 +02:00
void Updater : : downCompleted ( )
2011-03-02 16:57:41 +01:00
{
2015-10-06 09:46:23 +02:00
QNetworkReply * reply = qobject_cast < QNetworkReply * > ( sender ( ) ) ;
if ( ! reply )
return ;
2012-07-10 00:36:05 +02:00
QString html = reply - > readAll ( ) ;
2012-09-04 12:42:45 +02:00
if ( html . startsWith ( QLatin1String ( " Version: " ) ) ) {
html . remove ( QLatin1String ( " Version: " ) ) ;
2014-02-19 22:07:21 +01:00
Version current ( Qz : : VERSION ) ;
2013-03-17 10:26:47 +01:00
Version updated ( html ) ;
2012-07-10 00:36:05 +02:00
2013-03-12 14:09:12 +01:00
if ( current . isValid & & updated . isValid & & current < updated ) {
2012-07-08 14:03:50 +02:00
mApp - > desktopNotifications ( ) - > showNotification ( QPixmap ( " :icons/qupzillaupdate.png " ) , tr ( " Update available " ) , tr ( " New version of QupZilla is ready to download. " ) ) ;
2011-04-25 15:06:59 +02:00
}
2011-04-24 22:40:35 +02:00
}
2012-07-10 00:36:05 +02:00
2015-10-06 09:46:23 +02:00
reply - > deleteLater ( ) ;
2011-03-02 16:57:41 +01:00
}
2011-04-25 15:06:59 +02:00
void Updater : : downloadNewVersion ( )
{
2014-04-17 15:20:54 +02:00
m_window - > tabWidget ( ) - > addView ( QUrl : : fromEncoded ( QByteArray ( Qz : : WWWADDRESS ) + QByteArray ( " /download " ) ) , tr ( " Update " ) , Qz : : NT_NotSelectedTab ) ;
2011-04-25 15:06:59 +02:00
}