2 Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "update_checker.h"
22 #include "safe_stringstream.h"
25 #include "raw_convert.h"
26 #include <libcxml/cxml.h>
27 #include <curl/curl.h>
28 #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 ();
92 curl_easy_cleanup (_curl);
96 /** Start running the update check */
100 boost::mutex::scoped_lock lm (_process_mutex);
102 _condition.notify_one ();
106 UpdateChecker::thread ()
109 /* Block until there is something to do */
110 boost::mutex::scoped_lock lock (_process_mutex);
111 while (_to_do == 0 && !_terminate) {
112 _condition.wait (lock);
125 /* Perform the request */
127 int r = curl_easy_perform (_curl);
133 /* Parse the reply */
135 _buffer[_offset] = '\0';
137 cxml::Document doc ("Update");
140 /* Read the current stable and test version numbers */
146 boost::mutex::scoped_lock lm (_data_mutex);
147 stable = doc.string_child ("Stable");
148 test = doc.string_child ("Test");
151 if (version_less_than (dcpomatic_version, stable)) {
155 if (Config::instance()->check_for_test_updates() && version_less_than (dcpomatic_version, test)) {
159 if (_stable || _test) {
171 UpdateChecker::write_callback (void* data, size_t size, size_t nmemb)
173 size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset - 1));
174 memcpy (_buffer + _offset, data, t);
180 UpdateChecker::set_state (State s)
183 boost::mutex::scoped_lock lm (_data_mutex);
188 emit (boost::bind (boost::ref (StateChanged)));
192 UpdateChecker::instance ()
195 _instance = new UpdateChecker ();
203 UpdateChecker::version_less_than (string const & a, string const & b)
206 split (ap, a, is_any_of ("."));
208 split (bp, b, is_any_of ("."));
210 DCPOMATIC_ASSERT (ap.size() == 3 && bp.size() == 3);
212 if (ap[0] != bp[0]) {
213 return raw_convert<int> (ap[0]) < raw_convert<int> (bp[0]);
216 if (ap[1] != bp[1]) {
217 return raw_convert<int> (ap[1]) < raw_convert<int> (bp[1]);
220 if (ends_with (ap[2], "devel")) {
221 am = raw_convert<int> (ap[2].substr (0, ap[2].length() - 5)) + 0.5;
223 am = raw_convert<int> (ap[2]);
227 if (ends_with (bp[2], "devel")) {
228 bm = raw_convert<int> (bp[2].substr (0, bp[2].length() - 5)) + 0.5;
230 bm = raw_convert<int> (bp[2]);