xref: /glogg/src/versionchecker.cpp (revision 92bcda3be1ade91702f2f9691c82341dab1552ce)
1 /*
2  * Copyright (C) 2014 Nicolas Bonnefon and other contributors
3  *
4  * This file is part of glogg.
5  *
6  * glogg is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * glogg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "versionchecker.h"
21 
22 #include "log.h"
23 
24 const char* VersionChecker::VERSION_URL =
25     "http://gloggversion.bonnefon.org/latest";
26 
27 namespace {
28     bool isVersionNewer( const QString& current, const QString& new_version );
29 };
30 
31 VersionChecker::VersionChecker() : QObject(), manager_( this )
32 {
33 }
34 
35 VersionChecker::~VersionChecker()
36 {
37 }
38 
39 void VersionChecker::startCheck()
40 {
41     LOG(logDEBUG) << "VersionChecker::startCheck()";
42 
43     connect( &manager_, SIGNAL( finished( QNetworkReply* ) ),
44             this, SLOT( downloadFinished( QNetworkReply* ) ) );
45 
46     QNetworkRequest request;
47     request.setUrl( QUrl( VERSION_URL ) );
48     request.setRawHeader( "User-Agent", "glogg-" GLOGG_VERSION );
49     manager_.get( request );
50 }
51 
52 void VersionChecker::downloadFinished( QNetworkReply* reply )
53 {
54     LOG(logDEBUG) << "VersionChecker::downloadFinished()";
55 
56     if ( reply->error() == QNetworkReply::NoError )
57     {
58         QString new_version = { reply->read( 256 ) };
59         LOG(logDEBUG) << "Latest version is " << new_version.toStdString();
60         if ( isVersionNewer( QString( GLOGG_VERSION ), new_version ) )
61         {
62             emit newVersionFound( new_version );
63         }
64     }
65     else
66     {
67         LOG(logWARNING) << "Download failed: err " << reply->error();
68     }
69 
70     reply->deleteLater();
71 }
72 
73 namespace {
74     bool isVersionNewer( const QString& current, const QString& new_version )
75     {
76         return false;
77     }
78 };
79