Fix crashes on x-thread signal emission.
[dcpomatic.git] / src / lib / update.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "update.h"
21 #include "version.h"
22 #include "ui_signaller.h"
23 #include "safe_stringstream.h"
24 #include "config.h"
25 #include "util.h"
26 #include "raw_convert.h"
27 #include <libcxml/cxml.h>
28 #include <curl/curl.h>
29 #include <boost/algorithm/string.hpp>
30 #include <string>
31
32 #define BUFFER_SIZE 1024
33
34 using std::cout;
35 using std::min;
36 using std::string;
37 using std::vector;
38 using boost::is_any_of;
39 using boost::ends_with;
40
41 /** Singleton instance */
42 UpdateChecker* UpdateChecker::_instance = 0;
43
44 static size_t
45 write_callback_wrapper (void* data, size_t size, size_t nmemb, void* user)
46 {
47         return reinterpret_cast<UpdateChecker*>(user)->write_callback (data, size, nmemb);
48 }
49
50 /** Construct an UpdateChecker.  This sets things up and starts a thread to
51  *  do the work.
52  */
53 UpdateChecker::UpdateChecker ()
54         : _buffer (new char[BUFFER_SIZE])
55         , _offset (0)
56         , _curl (0)
57         , _state (NOT_RUN)
58         , _emits (0)
59         , _to_do (0)
60 {
61         curl_global_init (CURL_GLOBAL_ALL);
62         _curl = curl_easy_init ();
63
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);
68         
69         string const agent = "dcpomatic/" + string (dcpomatic_version);
70         curl_easy_setopt (_curl, CURLOPT_USERAGENT, agent.c_str ());
71
72         _thread = new boost::thread (boost::bind (&UpdateChecker::thread, this));
73 }
74
75 UpdateChecker::~UpdateChecker ()
76 {
77         /* We are not cleaning up our thread, but hey well */
78         
79         curl_easy_cleanup (_curl);
80         curl_global_cleanup ();
81         delete[] _buffer;
82 }
83
84 /** Start running the update check */
85 void
86 UpdateChecker::run ()
87 {
88         boost::mutex::scoped_lock lm (_process_mutex);
89         _to_do++;
90         _condition.notify_one ();
91 }
92
93 void
94 UpdateChecker::thread ()
95 {
96         while (true) {
97                 /* Block until there is something to do */
98                 boost::mutex::scoped_lock lock (_process_mutex);
99                 while (_to_do == 0) {
100                         _condition.wait (lock);
101                 }
102                 --_to_do;
103                 lock.unlock ();
104                 
105                 try {
106                         _offset = 0;
107
108                         /* Perform the request */
109                         
110                         int r = curl_easy_perform (_curl);
111                         if (r != CURLE_OK) {
112                                 set_state (FAILED);
113                                 return;
114                         }
115
116                         /* Parse the reply */
117                         
118                         _buffer[_offset] = '\0';
119                         string s (_buffer);
120                         cxml::Document doc ("Update");
121                         doc.read_string (s);
122
123                         /* Read the current stable and test version numbers */
124
125                         string stable;
126                         string test;
127
128                         {
129                                 boost::mutex::scoped_lock lm (_data_mutex);
130                                 stable = doc.string_child ("Stable");
131                                 test = doc.string_child ("Test");
132                         }
133
134                         if (version_less_than (dcpomatic_version, stable)) {
135                                 _stable = stable;
136                         }
137                         
138                         if (Config::instance()->check_for_test_updates() && version_less_than (dcpomatic_version, test)) {
139                                 _test = test;
140                         }
141
142                         if (_stable || _test) {
143                                 set_state (YES);
144                         } else {
145                                 set_state (NO);
146                         }
147                 } catch (...) {
148                         set_state (FAILED);
149                 }
150         }
151 }
152         
153 size_t
154 UpdateChecker::write_callback (void* data, size_t size, size_t nmemb)
155 {
156         size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset - 1));
157         memcpy (_buffer + _offset, data, t);
158         _offset += t;
159         return t;
160 }
161
162 void
163 UpdateChecker::set_state (State s)
164 {
165         {
166                 boost::mutex::scoped_lock lm (_data_mutex);
167                 _state = s;
168                 _emits++;
169         }
170
171         emit (boost::bind (boost::ref (StateChanged)));
172 }
173
174 UpdateChecker *
175 UpdateChecker::instance ()
176 {
177         if (!_instance) {
178                 _instance = new UpdateChecker ();
179         }
180
181         return _instance;
182 }
183
184 bool
185 UpdateChecker::version_less_than (string const & a, string const & b)
186 {
187         vector<string> ap;
188         split (ap, a, is_any_of ("."));
189         vector<string> bp;
190         split (bp, b, is_any_of ("."));
191
192         DCPOMATIC_ASSERT (ap.size() == 3 && bp.size() == 3);
193
194         if (ap[0] != bp[0]) {
195                 return raw_convert<int> (ap[0]) < raw_convert<int> (bp[0]);
196         }
197
198         if (ap[1] != bp[1]) {
199                 return raw_convert<int> (ap[1]) < raw_convert<int> (bp[1]);
200         }
201         float am;
202         if (ends_with (ap[2], "devel")) {
203                 am = raw_convert<int> (ap[2].substr (0, ap[2].length() - 5)) + 0.5;
204         } else {
205                 am = raw_convert<int> (ap[2]);
206         }
207         
208         float bm;
209         if (ends_with (bp[2], "devel")) {
210                 bm = raw_convert<int> (bp[2].substr (0, bp[2].length() - 5)) + 0.5;
211         } else {
212                 bm = raw_convert<int> (bp[2]);
213         }
214         
215         return am < bm;
216 }