2 Copyright (C) 2014 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.
22 #include <boost/algorithm/string.hpp>
23 #include <curl/curl.h>
24 #include <libcxml/cxml.h>
25 #include <libdcp/raw_convert.h>
28 #include "ui_signaller.h"
30 #define BUFFER_SIZE 1024
35 using std::stringstream;
36 using libdcp::raw_convert;
38 UpdateChecker* UpdateChecker::_instance = 0;
41 write_callback_wrapper (void* data, size_t size, size_t nmemb, void* user)
43 return reinterpret_cast<UpdateChecker*>(user)->write_callback (data, size, nmemb);
46 UpdateChecker::UpdateChecker ()
47 : _buffer (new char[BUFFER_SIZE])
54 curl_global_init (CURL_GLOBAL_ALL);
55 _curl = curl_easy_init ();
57 curl_easy_setopt (_curl, CURLOPT_URL, "http://dcpomatic.com/update");
58 curl_easy_setopt (_curl, CURLOPT_WRITEFUNCTION, write_callback_wrapper);
59 curl_easy_setopt (_curl, CURLOPT_WRITEDATA, this);
60 curl_easy_setopt (_curl, CURLOPT_TIMEOUT, 20);
62 string const agent = "dcpomatic/" + string (dcpomatic_version);
63 curl_easy_setopt (_curl, CURLOPT_USERAGENT, agent.c_str ());
65 _thread = new boost::thread (boost::bind (&UpdateChecker::thread, this));
68 UpdateChecker::~UpdateChecker ()
70 /* We are not cleaning up our thread, but hey well */
72 curl_easy_cleanup (_curl);
73 curl_global_cleanup ();
80 boost::mutex::scoped_lock lm (_process_mutex);
82 _condition.notify_one ();
86 UpdateChecker::thread ()
89 boost::mutex::scoped_lock lock (_process_mutex);
91 _condition.wait (lock);
99 int r = curl_easy_perform (_curl);
105 _buffer[_offset] = '\0';
108 cxml::Document doc ("Update");
112 boost::mutex::scoped_lock lm (_data_mutex);
113 _stable = doc.string_child ("Stable");
114 _test = doc.string_child ("Test");
117 string current = string (dcpomatic_version);
118 bool current_pre = false;
119 if (boost::algorithm::ends_with (current, "pre")) {
120 current = current.substr (0, current.length() - 3);
124 float current_float = raw_convert<float> (current);
126 current_float -= 0.005;
129 if (current_float < raw_convert<float> (_stable)) {
141 UpdateChecker::write_callback (void* data, size_t size, size_t nmemb)
143 size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset - 1));
144 memcpy (_buffer + _offset, data, t);
150 UpdateChecker::set_state (State s)
153 boost::mutex::scoped_lock lm (_data_mutex);
158 ui_signaller->emit (boost::bind (boost::ref (StateChanged)));
162 UpdateChecker::instance ()
165 _instance = new UpdateChecker ();