Fix the build for older macOS.
[dcpomatic.git] / src / lib / update_checker.h
1 /*
2     Copyright (C) 2014-2021 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
22 /** @file  src/lib/update_checker.h
23  *  @brief UpdateChecker class.
24  */
25
26
27 #include "signaller.h"
28 #include <curl/curl.h>
29 #include <boost/signals2.hpp>
30 #include <boost/thread/mutex.hpp>
31 #include <boost/thread/condition.hpp>
32 #include <boost/thread.hpp>
33
34
35 struct update_checker_test;
36
37
38 /** Class to check for the existance of an update for DCP-o-matic on a remote server */
39 class UpdateChecker : public Signaller
40 {
41 public:
42         ~UpdateChecker ();
43
44         UpdateChecker (UpdateChecker const &);
45         UpdateChecker& operator= (UpdateChecker const &);
46
47         void run ();
48
49         enum class State {
50                 YES,    ///< there is an update
51                 FAILED, ///< the check failed, so we don't know
52                 NO,     ///< there is no update
53                 NOT_RUN ///< the check has not been run (yet)
54         };
55
56         /** @return state of the checker */
57         State state () {
58                 boost::mutex::scoped_lock lm (_data_mutex);
59                 return _state;
60         }
61
62         /** @return new stable version, if there is one */
63         boost::optional<std::string> stable () {
64                 boost::mutex::scoped_lock lm (_data_mutex);
65                 return _stable;
66         }
67
68         /** @return new test version, if there is one */
69         boost::optional<std::string> test () {
70                 boost::mutex::scoped_lock lm (_data_mutex);
71                 return _test;
72         }
73
74         size_t write_callback (void *, size_t, size_t);
75
76         boost::signals2::signal<void (void)> StateChanged;
77
78         static UpdateChecker* instance ();
79
80 private:
81         friend struct update_checker_test;
82
83         static UpdateChecker* _instance;
84
85         static bool version_less_than (std::string const & a, std::string const & b);
86
87         UpdateChecker ();
88         void start ();
89         void set_state (State);
90         void thread ();
91
92         char* _buffer;
93         int _offset = 0;
94         CURL* _curl = nullptr;
95
96         /** mutex to protect _state, _stable, _test and _emits */
97         mutable boost::mutex _data_mutex;
98         State _state;
99         boost::optional<std::string> _stable;
100         boost::optional<std::string> _test;
101
102         boost::thread _thread;
103         boost::mutex _process_mutex;
104         boost::condition _condition;
105         int _to_do = 0;
106         bool _terminate = false;
107 };