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