2 Copyright (C) 2014-2015 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/>.
21 #include "update_checker.h"
24 #include "raw_convert.h"
25 #include <libcxml/cxml.h>
26 #include <curl/curl.h>
27 #include <boost/algorithm/string.hpp>
31 #define BUFFER_SIZE 1024
37 using boost::is_any_of;
38 using boost::ends_with;
40 /** Singleton instance */
41 UpdateChecker* UpdateChecker::_instance = 0;
44 write_callback_wrapper (void* data, size_t size, size_t nmemb, void* user)
46 return reinterpret_cast<UpdateChecker*>(user)->write_callback (data, size, nmemb);
49 /** Construct an UpdateChecker. This sets things up and starts a thread to
52 UpdateChecker::UpdateChecker ()
53 : _buffer (new char[BUFFER_SIZE])
62 _curl = curl_easy_init ();
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);
69 string const agent = "dcpomatic/" + string (dcpomatic_version);
70 curl_easy_setopt (_curl, CURLOPT_USERAGENT, agent.c_str ());
74 UpdateChecker::start ()
76 _thread = new boost::thread (boost::bind (&UpdateChecker::thread, this));
79 UpdateChecker::~UpdateChecker ()
82 boost::mutex::scoped_lock lm (_process_mutex);
86 _condition.notify_all ();
88 /* Ideally this would be a DCPOMATIC_ASSERT(_thread->joinable()) but we
89 can't throw exceptions from a destructor.
91 if (_thread->joinable ()) {
97 curl_easy_cleanup (_curl);
101 /** Start running the update check */
103 UpdateChecker::run ()
105 boost::mutex::scoped_lock lm (_process_mutex);
107 _condition.notify_one ();
111 UpdateChecker::thread ()
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);
130 /* Perform the request */
132 int r = curl_easy_perform (_curl);
138 /* Parse the reply */
140 _buffer[_offset] = '\0';
142 cxml::Document doc ("Update");
145 /* Read the current stable and test version numbers */
151 boost::mutex::scoped_lock lm (_data_mutex);
152 stable = doc.string_child ("Stable");
153 test = doc.string_child ("Test");
156 if (version_less_than (dcpomatic_version, stable)) {
160 if (version_less_than (dcpomatic_version, test)) {
164 if (_stable || _test) {
176 UpdateChecker::write_callback (void* data, size_t size, size_t nmemb)
178 size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset - 1));
179 memcpy (_buffer + _offset, data, t);
185 UpdateChecker::set_state (State s)
188 boost::mutex::scoped_lock lm (_data_mutex);
193 emit (boost::bind (boost::ref (StateChanged)));
197 UpdateChecker::instance ()
200 _instance = new UpdateChecker ();
208 UpdateChecker::version_less_than (string const & a, string const & b)
211 split (ap, a, is_any_of ("."));
213 split (bp, b, is_any_of ("."));
215 DCPOMATIC_ASSERT (ap.size() == 3 && bp.size() == 3);
217 if (ap[0] != bp[0]) {
218 return raw_convert<int> (ap[0]) < raw_convert<int> (bp[0]);
221 if (ap[1] != bp[1]) {
222 return raw_convert<int> (ap[1]) < raw_convert<int> (bp[1]);
225 if (ends_with (ap[2], "devel")) {
226 am = raw_convert<int> (ap[2].substr (0, ap[2].length() - 5)) + 0.5;
228 am = raw_convert<int> (ap[2]);
232 if (ends_with (bp[2], "devel")) {
233 bm = raw_convert<int> (bp[2].substr (0, bp[2].length() - 5)) + 0.5;
235 bm = raw_convert<int> (bp[2]);