Merge master.
[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 /** @file  src/lib/update.h
21  *  @brief UpdateChecker class.
22  */
23
24 #include <boost/signals2.hpp>
25 #include <boost/thread/mutex.hpp>
26 #include <boost/thread/condition.hpp>
27 #include <boost/thread.hpp>
28 #include <curl/curl.h>
29
30 /** Class to check for the existance of an update for DCP-o-matic on a remote server */
31 class UpdateChecker
32 {
33 public:
34         UpdateChecker ();
35         ~UpdateChecker ();
36
37         void run ();
38
39         enum State {
40                 YES,    ///< there is an update
41                 FAILED, ///< the check failed, so we don't know
42                 NO,     ///< there is no update
43                 NOT_RUN ///< the check has not been run (yet)
44         };
45
46         /** @return state of the checker */
47         State state () {
48                 boost::mutex::scoped_lock lm (_data_mutex);
49                 return _state;
50         }
51         
52         /** @return the version string of the latest stable version (if _state == YES or NO) */
53         std::string stable () {
54                 boost::mutex::scoped_lock lm (_data_mutex);
55                 return _stable;
56         }
57
58         /** @return the version string of the latest test version (if _state == YES or NO) */
59         std::string test () {
60                 boost::mutex::scoped_lock lm (_data_mutex);
61                 return _test;
62         }
63         
64         /** @return true if the last signal emission was the first */
65         bool last_emit_was_first () const {
66                 boost::mutex::scoped_lock lm (_data_mutex);
67                 return _emits == 1;
68         }
69
70         size_t write_callback (void *, size_t, size_t);
71
72         boost::signals2::signal<void (void)> StateChanged;
73
74         static UpdateChecker* instance ();
75
76 private:        
77         static UpdateChecker* _instance;
78
79         void set_state (State);
80         void thread ();
81
82         char* _buffer;
83         int _offset;
84         CURL* _curl;
85
86         /** mutex to protect _state, _stable, _test and _emits */
87         mutable boost::mutex _data_mutex;
88         State _state;
89         std::string _stable;
90         std::string _test;
91         int _emits;
92
93         boost::thread* _thread;
94         boost::mutex _process_mutex;
95         boost::condition _condition;
96         int _to_do;
97 };