Various update bits.
[dcpomatic.git] / src / lib / update.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 #include <boost/signals2.hpp>
21 #include <boost/thread/mutex.hpp>
22 #include <boost/thread/condition.hpp>
23 #include <curl/curl.h>
24
25 class UpdateChecker
26 {
27 public:
28         UpdateChecker ();
29         ~UpdateChecker ();
30
31         void run ();
32
33         enum State {
34                 YES,
35                 FAILED,
36                 NO,
37                 NOT_RUN
38         };
39
40         State state () {
41                 boost::mutex::scoped_lock lm (_data_mutex);
42                 return _state;
43         }
44         
45         std::string stable () {
46                 boost::mutex::scoped_lock lm (_data_mutex);
47                 return _stable;
48         }
49
50         std::string test () {
51                 boost::mutex::scoped_lock lm (_data_mutex);
52                 return _test;
53         }
54         
55         /** @return true if the list signal emission was the first */
56         bool last_emit_was_first () const {
57                 boost::mutex::scoped_lock lm (_data_mutex);
58                 return _emits == 1;
59         }
60
61         size_t write_callback (void *, size_t, size_t);
62
63         boost::signals2::signal<void (void)> StateChanged;
64
65         static UpdateChecker* instance ();
66
67 private:        
68         static UpdateChecker* _instance;
69
70         void set_state (State);
71         void thread ();
72
73         char* _buffer;
74         int _offset;
75         CURL* _curl;
76
77         /** mutex to protect _state, _stable, _test and _emits */
78         mutable boost::mutex _data_mutex;
79         State _state;
80         std::string _stable;
81         std::string _test;
82         int _emits;
83
84         boost::thread* _thread;
85         boost::mutex _process_mutex;
86         boost::condition _condition;
87 };