Fix various misbehaviours with update checking (#749).
[dcpomatic.git] / src / lib / update_checker.h
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/lib/update.h
21  *  @brief UpdateChecker class.
22  */
23
24 #include "signaller.h"
25 #include <curl/curl.h>
26 #include <boost/signals2.hpp>
27 #include <boost/thread/mutex.hpp>
28 #include <boost/thread/condition.hpp>
29 #include <boost/thread.hpp>
30
31 struct update_checker_test;
32
33 /** Class to check for the existance of an update for DCP-o-matic on a remote server */
34 class UpdateChecker : public Signaller, public boost::noncopyable
35 {
36 public:
37         ~UpdateChecker ();
38
39         void run ();
40
41         enum State {
42                 YES,    ///< there is an update
43                 FAILED, ///< the check failed, so we don't know
44                 NO,     ///< there is no update
45                 NOT_RUN ///< the check has not been run (yet)
46         };
47
48         /** @return state of the checker */
49         State state () {
50                 boost::mutex::scoped_lock lm (_data_mutex);
51                 return _state;
52         }
53
54         /** @return new stable version, if there is one */
55         boost::optional<std::string> stable () {
56                 boost::mutex::scoped_lock lm (_data_mutex);
57                 return _stable;
58         }
59
60         /** @return new test version, if there is one */
61         boost::optional<std::string> test () {
62                 boost::mutex::scoped_lock lm (_data_mutex);
63                 return _test;
64         }
65
66         size_t write_callback (void *, size_t, size_t);
67
68         boost::signals2::signal<void (void)> StateChanged;
69
70         static UpdateChecker* instance ();
71
72 private:
73         friend struct update_checker_test;
74
75         static UpdateChecker* _instance;
76
77         static bool version_less_than (std::string const & a, std::string const & b);
78
79         UpdateChecker ();
80         void start ();
81         void set_state (State);
82         void thread ();
83
84         char* _buffer;
85         int _offset;
86         CURL* _curl;
87
88         /** mutex to protect _state, _stable, _test and _emits */
89         mutable boost::mutex _data_mutex;
90         State _state;
91         boost::optional<std::string> _stable;
92         boost::optional<std::string> _test;
93         int _emits;
94
95         boost::thread* _thread;
96         boost::mutex _process_mutex;
97         boost::condition _condition;
98         int _to_do;
99         bool _terminate;
100 };