1431d01deSNicolas Bonnefon /* 2431d01deSNicolas Bonnefon * Copyright (C) 2014 Nicolas Bonnefon and other contributors 3431d01deSNicolas Bonnefon * 4431d01deSNicolas Bonnefon * This file is part of glogg. 5431d01deSNicolas Bonnefon * 6431d01deSNicolas Bonnefon * glogg is free software: you can redistribute it and/or modify 7431d01deSNicolas Bonnefon * it under the terms of the GNU General Public License as published by 8431d01deSNicolas Bonnefon * the Free Software Foundation, either version 3 of the License, or 9431d01deSNicolas Bonnefon * (at your option) any later version. 10431d01deSNicolas Bonnefon * 11431d01deSNicolas Bonnefon * glogg is distributed in the hope that it will be useful, 12431d01deSNicolas Bonnefon * but WITHOUT ANY WARRANTY; without even the implied warranty of 13431d01deSNicolas Bonnefon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14431d01deSNicolas Bonnefon * GNU General Public License for more details. 15431d01deSNicolas Bonnefon * 16431d01deSNicolas Bonnefon * You should have received a copy of the GNU General Public License 17431d01deSNicolas Bonnefon * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18431d01deSNicolas Bonnefon */ 19431d01deSNicolas Bonnefon 20431d01deSNicolas Bonnefon #include "versionchecker.h" 21431d01deSNicolas Bonnefon 22431d01deSNicolas Bonnefon #include "log.h" 23431d01deSNicolas Bonnefon 24431d01deSNicolas Bonnefon const char* VersionChecker::VERSION_URL = 25431d01deSNicolas Bonnefon "http://gloggversion.bonnefon.org/latest"; 26431d01deSNicolas Bonnefon 2792bcda3bSNicolas Bonnefon namespace { 2892bcda3bSNicolas Bonnefon bool isVersionNewer( const QString& current, const QString& new_version ); 2992bcda3bSNicolas Bonnefon }; 3092bcda3bSNicolas Bonnefon 31431d01deSNicolas Bonnefon VersionChecker::VersionChecker() : QObject(), manager_( this ) 32431d01deSNicolas Bonnefon { 33431d01deSNicolas Bonnefon } 34431d01deSNicolas Bonnefon 35431d01deSNicolas Bonnefon VersionChecker::~VersionChecker() 36431d01deSNicolas Bonnefon { 37431d01deSNicolas Bonnefon } 38431d01deSNicolas Bonnefon 39431d01deSNicolas Bonnefon void VersionChecker::startCheck() 40431d01deSNicolas Bonnefon { 41431d01deSNicolas Bonnefon LOG(logDEBUG) << "VersionChecker::startCheck()"; 42431d01deSNicolas Bonnefon 43431d01deSNicolas Bonnefon connect( &manager_, SIGNAL( finished( QNetworkReply* ) ), 44431d01deSNicolas Bonnefon this, SLOT( downloadFinished( QNetworkReply* ) ) ); 45431d01deSNicolas Bonnefon 46431d01deSNicolas Bonnefon QNetworkRequest request; 47431d01deSNicolas Bonnefon request.setUrl( QUrl( VERSION_URL ) ); 48431d01deSNicolas Bonnefon request.setRawHeader( "User-Agent", "glogg-" GLOGG_VERSION ); 49431d01deSNicolas Bonnefon manager_.get( request ); 50431d01deSNicolas Bonnefon } 51431d01deSNicolas Bonnefon 52431d01deSNicolas Bonnefon void VersionChecker::downloadFinished( QNetworkReply* reply ) 53431d01deSNicolas Bonnefon { 54431d01deSNicolas Bonnefon LOG(logDEBUG) << "VersionChecker::downloadFinished()"; 5592bcda3bSNicolas Bonnefon 5692bcda3bSNicolas Bonnefon if ( reply->error() == QNetworkReply::NoError ) 5792bcda3bSNicolas Bonnefon { 58*460de700SNicolas Bonnefon QString new_version = QString( reply->read( 256 ) ).remove( '\n' ); 59*460de700SNicolas Bonnefon 6092bcda3bSNicolas Bonnefon LOG(logDEBUG) << "Latest version is " << new_version.toStdString(); 6192bcda3bSNicolas Bonnefon if ( isVersionNewer( QString( GLOGG_VERSION ), new_version ) ) 6292bcda3bSNicolas Bonnefon { 63*460de700SNicolas Bonnefon LOG(logDEBUG) << "Sending new version notification"; 6492bcda3bSNicolas Bonnefon emit newVersionFound( new_version ); 65431d01deSNicolas Bonnefon } 6692bcda3bSNicolas Bonnefon } 6792bcda3bSNicolas Bonnefon else 6892bcda3bSNicolas Bonnefon { 6992bcda3bSNicolas Bonnefon LOG(logWARNING) << "Download failed: err " << reply->error(); 7092bcda3bSNicolas Bonnefon } 7192bcda3bSNicolas Bonnefon 7292bcda3bSNicolas Bonnefon reply->deleteLater(); 7392bcda3bSNicolas Bonnefon } 7492bcda3bSNicolas Bonnefon 7592bcda3bSNicolas Bonnefon namespace { 7692bcda3bSNicolas Bonnefon bool isVersionNewer( const QString& current, const QString& new_version ) 7792bcda3bSNicolas Bonnefon { 78*460de700SNicolas Bonnefon QRegExp version_regex( "(\\d+)\\.(\\d+)\\.(\\d+)(-(\\S+))?", 79*460de700SNicolas Bonnefon Qt::CaseSensitive, 80*460de700SNicolas Bonnefon QRegExp::RegExp2 ); 81*460de700SNicolas Bonnefon 82*460de700SNicolas Bonnefon // Main version is the three first digits 83*460de700SNicolas Bonnefon // Add is the part after '-' if there 84*460de700SNicolas Bonnefon unsigned current_main_version = 0; 85*460de700SNicolas Bonnefon unsigned current_add_version = 0; 86*460de700SNicolas Bonnefon unsigned new_main_version = 0; 87*460de700SNicolas Bonnefon unsigned new_add_version = 0; 88*460de700SNicolas Bonnefon 89*460de700SNicolas Bonnefon if ( version_regex.indexIn( current ) != -1 ) 90*460de700SNicolas Bonnefon { 91*460de700SNicolas Bonnefon current_main_version = version_regex.cap(3).toInt() 92*460de700SNicolas Bonnefon + version_regex.cap(2).toInt() * 100 93*460de700SNicolas Bonnefon + version_regex.cap(1).toInt() * 10000; 94*460de700SNicolas Bonnefon current_add_version = version_regex.cap(5).isEmpty() ? 0 : 1; 95*460de700SNicolas Bonnefon } 96*460de700SNicolas Bonnefon 97*460de700SNicolas Bonnefon if ( version_regex.indexIn( new_version ) != -1 ) 98*460de700SNicolas Bonnefon { 99*460de700SNicolas Bonnefon new_main_version = version_regex.cap(3).toInt() 100*460de700SNicolas Bonnefon + version_regex.cap(2).toInt() * 100 101*460de700SNicolas Bonnefon + version_regex.cap(1).toInt() * 10000; 102*460de700SNicolas Bonnefon new_add_version = version_regex.cap(5).isEmpty() ? 0 : 1; 103*460de700SNicolas Bonnefon } 104*460de700SNicolas Bonnefon 105*460de700SNicolas Bonnefon LOG(logDEBUG) << "Current version: " << current_main_version; 106*460de700SNicolas Bonnefon LOG(logDEBUG) << "New version: " << new_main_version; 107*460de700SNicolas Bonnefon 108*460de700SNicolas Bonnefon // We only consider the main part for testing for now 109*460de700SNicolas Bonnefon return new_main_version > current_main_version; 11092bcda3bSNicolas Bonnefon } 11192bcda3bSNicolas Bonnefon }; 112