xref: /glogg/src/versionchecker.h (revision a3f81147fefbe67ae6fc1db73f512393192a3595)
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 #ifndef VERSIONCHECKER_H
21431d01deSNicolas Bonnefon #define VERSIONCHECKER_H
22431d01deSNicolas Bonnefon 
23*a3f81147SNicolas Bonnefon #include <ctime>
24*a3f81147SNicolas Bonnefon 
25431d01deSNicolas Bonnefon #include <QObject>
26431d01deSNicolas Bonnefon #include <QtNetwork>
27431d01deSNicolas Bonnefon 
28*a3f81147SNicolas Bonnefon #include "persistable.h"
29*a3f81147SNicolas Bonnefon 
30*a3f81147SNicolas Bonnefon // This class holds the configuration options and persistent
31*a3f81147SNicolas Bonnefon // data for the version checker
32*a3f81147SNicolas Bonnefon class VersionCheckerConfig : public Persistable
33*a3f81147SNicolas Bonnefon {
34*a3f81147SNicolas Bonnefon   public:
35*a3f81147SNicolas Bonnefon     VersionCheckerConfig();
36*a3f81147SNicolas Bonnefon 
37*a3f81147SNicolas Bonnefon     // Accessors
versionCheckingEnabled()38*a3f81147SNicolas Bonnefon     bool versionCheckingEnabled() const
39*a3f81147SNicolas Bonnefon     { return enabled_; }
setVersionCheckingEnabled(bool enabled)40*a3f81147SNicolas Bonnefon     void setVersionCheckingEnabled( bool enabled )
41*a3f81147SNicolas Bonnefon     { enabled_ = enabled; }
nextDeadline()42*a3f81147SNicolas Bonnefon     std::time_t nextDeadline() const
43*a3f81147SNicolas Bonnefon     { return next_deadline_; }
setNextDeadline(std::time_t deadline)44*a3f81147SNicolas Bonnefon     void setNextDeadline( std::time_t deadline )
45*a3f81147SNicolas Bonnefon     { next_deadline_ = deadline; }
46*a3f81147SNicolas Bonnefon 
47*a3f81147SNicolas Bonnefon     // Reads/writes the current config in the QSettings object passed
48*a3f81147SNicolas Bonnefon     virtual void saveToStorage( QSettings& settings ) const;
49*a3f81147SNicolas Bonnefon     virtual void retrieveFromStorage( QSettings& settings );
50*a3f81147SNicolas Bonnefon 
51*a3f81147SNicolas Bonnefon   private:
52*a3f81147SNicolas Bonnefon     bool enabled_;
53*a3f81147SNicolas Bonnefon     std::time_t next_deadline_;
54*a3f81147SNicolas Bonnefon };
55*a3f81147SNicolas Bonnefon 
56431d01deSNicolas Bonnefon // This class compares the current version number with the latest
57431d01deSNicolas Bonnefon // stored on a central server
58431d01deSNicolas Bonnefon class VersionChecker : public QObject
59431d01deSNicolas Bonnefon {
60431d01deSNicolas Bonnefon   Q_OBJECT
61431d01deSNicolas Bonnefon 
62431d01deSNicolas Bonnefon   public:
63431d01deSNicolas Bonnefon     VersionChecker();
64431d01deSNicolas Bonnefon     ~VersionChecker();
65431d01deSNicolas Bonnefon 
66431d01deSNicolas Bonnefon     // Starts an asynchronous check for a newer version if it is needed.
67431d01deSNicolas Bonnefon     // A newVersionFound signal is sent if one is found.
68431d01deSNicolas Bonnefon     // In case of error or if no new version is found, no signal is emitted.
69431d01deSNicolas Bonnefon     void startCheck();
70431d01deSNicolas Bonnefon 
71431d01deSNicolas Bonnefon   signals:
72431d01deSNicolas Bonnefon     // New version "version" is available
73431d01deSNicolas Bonnefon     void newVersionFound( const QString& version );
74431d01deSNicolas Bonnefon 
75431d01deSNicolas Bonnefon   private slots:
76431d01deSNicolas Bonnefon     // Called when download is finished
77431d01deSNicolas Bonnefon     void downloadFinished( QNetworkReply* );
78431d01deSNicolas Bonnefon 
79431d01deSNicolas Bonnefon   private:
80431d01deSNicolas Bonnefon     static const char* VERSION_URL;
81*a3f81147SNicolas Bonnefon     static const uint64_t CHECK_INTERVAL_S;
82431d01deSNicolas Bonnefon 
83431d01deSNicolas Bonnefon     QNetworkAccessManager manager_;
84431d01deSNicolas Bonnefon };
85431d01deSNicolas Bonnefon 
86431d01deSNicolas Bonnefon #endif
87