xref: /glogg/src/versionchecker.h (revision a3f81147fefbe67ae6fc1db73f512393192a3595)
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 #ifndef VERSIONCHECKER_H
21 #define VERSIONCHECKER_H
22 
23 #include <ctime>
24 
25 #include <QObject>
26 #include <QtNetwork>
27 
28 #include "persistable.h"
29 
30 // This class holds the configuration options and persistent
31 // data for the version checker
32 class VersionCheckerConfig : public Persistable
33 {
34   public:
35     VersionCheckerConfig();
36 
37     // Accessors
versionCheckingEnabled()38     bool versionCheckingEnabled() const
39     { return enabled_; }
setVersionCheckingEnabled(bool enabled)40     void setVersionCheckingEnabled( bool enabled )
41     { enabled_ = enabled; }
nextDeadline()42     std::time_t nextDeadline() const
43     { return next_deadline_; }
setNextDeadline(std::time_t deadline)44     void setNextDeadline( std::time_t deadline )
45     { next_deadline_ = deadline; }
46 
47     // Reads/writes the current config in the QSettings object passed
48     virtual void saveToStorage( QSettings& settings ) const;
49     virtual void retrieveFromStorage( QSettings& settings );
50 
51   private:
52     bool enabled_;
53     std::time_t next_deadline_;
54 };
55 
56 // This class compares the current version number with the latest
57 // stored on a central server
58 class VersionChecker : public QObject
59 {
60   Q_OBJECT
61 
62   public:
63     VersionChecker();
64     ~VersionChecker();
65 
66     // Starts an asynchronous check for a newer version if it is needed.
67     // A newVersionFound signal is sent if one is found.
68     // In case of error or if no new version is found, no signal is emitted.
69     void startCheck();
70 
71   signals:
72     // New version "version" is available
73     void newVersionFound( const QString& version );
74 
75   private slots:
76     // Called when download is finished
77     void downloadFinished( QNetworkReply* );
78 
79   private:
80     static const char* VERSION_URL;
81     static const uint64_t CHECK_INTERVAL_S;
82 
83     QNetworkAccessManager manager_;
84 };
85 
86 #endif
87