Extract common code out into kdm_for_screen()
[dcpomatic.git] / src / lib / update_checker.h
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  src/lib/update.h
22  *  @brief UpdateChecker class.
23  */
24
25 #include "signaller.h"
26 #include <curl/curl.h>
27 #include <boost/signals2.hpp>
28 #include <boost/thread/mutex.hpp>
29 #include <boost/thread/condition.hpp>
30 #include <boost/thread.hpp>
31
32 struct update_checker_test;
33
34 /** Class to check for the existance of an update for DCP-o-matic on a remote server */
35 class UpdateChecker : public Signaller, public boost::noncopyable
36 {
37 public:
38         ~UpdateChecker ();
39
40         void run ();
41
42         enum State {
43                 YES,    ///< there is an update
44                 FAILED, ///< the check failed, so we don't know
45                 NO,     ///< there is no update
46                 NOT_RUN ///< the check has not been run (yet)
47         };
48
49         /** @return state of the checker */
50         State state () {
51                 boost::mutex::scoped_lock lm (_data_mutex);
52                 return _state;
53         }
54
55         /** @return new stable version, if there is one */
56         boost::optional<std::string> stable () {
57                 boost::mutex::scoped_lock lm (_data_mutex);
58                 return _stable;
59         }
60
61         /** @return new test version, if there is one */
62         boost::optional<std::string> test () {
63                 boost::mutex::scoped_lock lm (_data_mutex);
64                 return _test;
65         }
66
67         size_t write_callback (void *, size_t, size_t);
68
69         boost::signals2::signal<void (void)> StateChanged;
70
71         static UpdateChecker* instance ();
72
73 private:
74         friend struct update_checker_test;
75
76         static UpdateChecker* _instance;
77
78         static bool version_less_than (std::string const & a, std::string const & b);
79
80         UpdateChecker ();
81         void start ();
82         void set_state (State);
83         void thread ();
84
85         char* _buffer;
86         int _offset;
87         CURL* _curl;
88
89         /** mutex to protect _state, _stable, _test and _emits */
90         mutable boost::mutex _data_mutex;
91         State _state;
92         boost::optional<std::string> _stable;
93         boost::optional<std::string> _test;
94         int _emits;
95
96         boost::thread _thread;
97         boost::mutex _process_mutex;
98         boost::condition _condition;
99         int _to_do;
100         bool _terminate;
101 };