6c1217f860b318266e84beb7208e13c07375438a
[dcpomatic.git] / src / lib / update_checker.cc
1 /*
2     Copyright (C) 2014-2015 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 #include "update_checker.h"
22 #include "version.h"
23 #include "util.h"
24 #include "raw_convert.h"
25 #include <libcxml/cxml.h>
26 #include <curl/curl.h>
27 #include <boost/algorithm/string.hpp>
28 #include <string>
29 #include <iostream>
30
31 #define BUFFER_SIZE 1024
32
33 using std::cout;
34 using std::min;
35 using std::string;
36 using std::vector;
37 using boost::is_any_of;
38 using boost::ends_with;
39
40 /** Singleton instance */
41 UpdateChecker* UpdateChecker::_instance = 0;
42
43 static size_t
44 write_callback_wrapper (void* data, size_t size, size_t nmemb, void* user)
45 {
46         return reinterpret_cast<UpdateChecker*>(user)->write_callback (data, size, nmemb);
47 }
48
49 /** Construct an UpdateChecker.  This sets things up and starts a thread to
50  *  do the work.
51  */
52 UpdateChecker::UpdateChecker ()
53         : _buffer (new char[BUFFER_SIZE])
54         , _offset (0)
55         , _curl (0)
56         , _state (NOT_RUN)
57         , _emits (0)
58         , _thread (0)
59         , _to_do (0)
60         , _terminate (false)
61 {
62         _curl = curl_easy_init ();
63
64         curl_easy_setopt (_curl, CURLOPT_URL, "http://dcpomatic.com/update");
65         curl_easy_setopt (_curl, CURLOPT_WRITEFUNCTION, write_callback_wrapper);
66         curl_easy_setopt (_curl, CURLOPT_WRITEDATA, this);
67         curl_easy_setopt (_curl, CURLOPT_TIMEOUT, 20);
68
69         string const agent = "dcpomatic/" + string (dcpomatic_version);
70         curl_easy_setopt (_curl, CURLOPT_USERAGENT, agent.c_str ());
71 }
72
73 void
74 UpdateChecker::start ()
75 {
76         _thread = new boost::thread (boost::bind (&UpdateChecker::thread, this));
77 }
78
79 UpdateChecker::~UpdateChecker ()
80 {
81         {
82                 boost::mutex::scoped_lock lm (_process_mutex);
83                 _terminate = true;
84         }
85
86         _condition.notify_all ();
87         if (_thread) {
88                 /* Ideally this would be a DCPOMATIC_ASSERT(_thread->joinable()) but we
89                    can't throw exceptions from a destructor.
90                 */
91                 if (_thread->joinable ()) {
92                         _thread->join ();
93                 }
94         }
95         delete _thread;
96
97         curl_easy_cleanup (_curl);
98         delete[] _buffer;
99 }
100
101 /** Start running the update check */
102 void
103 UpdateChecker::run ()
104 {
105         boost::mutex::scoped_lock lm (_process_mutex);
106         _to_do++;
107         _condition.notify_one ();
108 }
109
110 void
111 UpdateChecker::thread ()
112 {
113         while (true) {
114                 /* Block until there is something to do */
115                 boost::mutex::scoped_lock lock (_process_mutex);
116                 while (_to_do == 0 && !_terminate) {
117                         _condition.wait (lock);
118                 }
119
120                 if (_terminate) {
121                         return;
122                 }
123
124                 --_to_do;
125                 lock.unlock ();
126
127                 try {
128                         _offset = 0;
129
130                         /* Perform the request */
131
132                         int r = curl_easy_perform (_curl);
133                         if (r != CURLE_OK) {
134                                 set_state (FAILED);
135                                 return;
136                         }
137
138                         /* Parse the reply */
139
140                         _buffer[_offset] = '\0';
141                         string s (_buffer);
142                         cxml::Document doc ("Update");
143                         doc.read_string (s);
144
145                         /* Read the current stable and test version numbers */
146
147                         string stable;
148                         string test;
149
150                         {
151                                 boost::mutex::scoped_lock lm (_data_mutex);
152                                 stable = doc.string_child ("Stable");
153                                 test = doc.string_child ("Test");
154                         }
155
156                         if (version_less_than (dcpomatic_version, stable)) {
157                                 _stable = stable;
158                         }
159
160                         if (version_less_than (dcpomatic_version, test)) {
161                                 _test = test;
162                         }
163
164                         if (_stable || _test) {
165                                 set_state (YES);
166                         } else {
167                                 set_state (NO);
168                         }
169                 } catch (...) {
170                         set_state (FAILED);
171                 }
172         }
173 }
174
175 size_t
176 UpdateChecker::write_callback (void* data, size_t size, size_t nmemb)
177 {
178         size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset - 1));
179         memcpy (_buffer + _offset, data, t);
180         _offset += t;
181         return t;
182 }
183
184 void
185 UpdateChecker::set_state (State s)
186 {
187         {
188                 boost::mutex::scoped_lock lm (_data_mutex);
189                 _state = s;
190                 _emits++;
191         }
192
193         emit (boost::bind (boost::ref (StateChanged)));
194 }
195
196 UpdateChecker *
197 UpdateChecker::instance ()
198 {
199         if (!_instance) {
200                 _instance = new UpdateChecker ();
201                 _instance->start ();
202         }
203
204         return _instance;
205 }
206
207 bool
208 UpdateChecker::version_less_than (string const & a, string const & b)
209 {
210         vector<string> ap;
211         split (ap, a, is_any_of ("."));
212         vector<string> bp;
213         split (bp, b, is_any_of ("."));
214
215         DCPOMATIC_ASSERT (ap.size() == 3 && bp.size() == 3);
216
217         if (ap[0] != bp[0]) {
218                 return raw_convert<int> (ap[0]) < raw_convert<int> (bp[0]);
219         }
220
221         if (ap[1] != bp[1]) {
222                 return raw_convert<int> (ap[1]) < raw_convert<int> (bp[1]);
223         }
224         float am;
225         if (ends_with (ap[2], "devel")) {
226                 am = raw_convert<int> (ap[2].substr (0, ap[2].length() - 5)) + 0.5;
227         } else {
228                 am = raw_convert<int> (ap[2]);
229         }
230
231         float bm;
232         if (ends_with (bp[2], "devel")) {
233                 bm = raw_convert<int> (bp[2].substr (0, bp[2].length() - 5)) + 0.5;
234         } else {
235                 bm = raw_convert<int> (bp[2]);
236         }
237
238         return am < bm;
239 }