2 Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "update_checker.h"
25 #include <dcp/raw_convert.h>
26 #include <libcxml/cxml.h>
27 #include <curl/curl.h>
28 #include <boost/algorithm/string.hpp>
33 #define BUFFER_SIZE 1024
40 using boost::is_any_of;
41 using boost::ends_with;
42 using dcp::raw_convert;
45 /** Singleton instance */
46 UpdateChecker* UpdateChecker::_instance = nullptr;
50 write_callback_wrapper (void* data, size_t size, size_t nmemb, void* user)
52 return reinterpret_cast<UpdateChecker*>(user)->write_callback (data, size, nmemb);
56 /** Construct an UpdateChecker. This sets things up and starts a thread to
59 UpdateChecker::UpdateChecker ()
60 : _buffer (BUFFER_SIZE)
61 , _state (State::NOT_RUN)
63 _curl = curl_easy_init ();
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);
70 string const agent = "dcpomatic/" + string (dcpomatic_version);
71 curl_easy_setopt (_curl, CURLOPT_USERAGENT, agent.c_str ());
76 UpdateChecker::start ()
78 _thread = boost::thread (boost::bind (&UpdateChecker::thread, this));
79 #ifdef DCPOMATIC_LINUX
80 pthread_setname_np (_thread.native_handle(), "update-checker");
85 UpdateChecker::~UpdateChecker ()
87 boost::this_thread::disable_interruption dis;
90 boost::mutex::scoped_lock lm (_process_mutex);
94 _condition.notify_all ();
99 curl_easy_cleanup (_curl);
103 /** Start running the update check */
105 UpdateChecker::run ()
107 boost::mutex::scoped_lock lm (_process_mutex);
109 _condition.notify_one ();
114 UpdateChecker::thread ()
117 /* Block until there is something to do */
118 boost::mutex::scoped_lock lock (_process_mutex);
119 while (_to_do == 0 && !_terminate) {
120 _condition.wait (lock);
133 /* Perform the request */
135 int r = curl_easy_perform (_curl);
137 set_state (State::FAILED);
141 /* Parse the reply */
143 _buffer[_offset] = '\0';
144 string s (_buffer.data());
145 cxml::Document doc ("Update");
148 /* Read the current stable and test version numbers */
154 boost::mutex::scoped_lock lm (_data_mutex);
155 stable = doc.string_child ("Stable");
156 test = doc.string_child ("Test");
159 if (version_less_than (dcpomatic_version, stable)) {
163 if (!test.empty() && version_less_than (dcpomatic_version, test)) {
167 if (_stable || _test) {
168 set_state (State::YES);
170 set_state (State::NO);
173 set_state (State::FAILED);
180 UpdateChecker::write_callback (void* data, size_t size, size_t nmemb)
182 size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset - 1));
183 memcpy (_buffer.data() + _offset, data, t);
190 UpdateChecker::set_state (State s)
193 boost::mutex::scoped_lock lm (_data_mutex);
197 emit (boost::bind(boost::ref(StateChanged)));
202 UpdateChecker::instance ()
205 _instance = new UpdateChecker ();
214 UpdateChecker::version_less_than (string const & a, string const & b)
217 split (ap, a, is_any_of ("."));
219 split (bp, b, is_any_of ("."));
221 DCPOMATIC_ASSERT (ap.size() == 3 && bp.size() == 3);
223 if (ap[0] != bp[0]) {
224 return raw_convert<int>(ap[0]) < raw_convert<int>(bp[0]);
227 if (ap[1] != bp[1]) {
228 return raw_convert<int>(ap[1]) < raw_convert<int>(bp[1]);
231 if (ends_with (ap[2], "devel")) {
232 am = raw_convert<int>(ap[2].substr(0, ap[2].length() - 5)) + 0.5;
234 am = raw_convert<int>(ap[2]);
238 if (ends_with (bp[2], "devel")) {
239 bm = raw_convert<int>(bp[2].substr(0, bp[2].length() - 5)) + 0.5;
241 bm = raw_convert<int>(bp[2]);