Merge branch 'master' of ssh://git.carlh.net/home/carl/git/dcpomatic
[dcpomatic.git] / src / lib / update_checker.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_checker.h"
21 #include "version.h"
22 #include "safe_stringstream.h"
23 #include "config.h"
24 #include "util.h"
25 #include "raw_convert.h"
26 #include <libcxml/cxml.h>
27 #include <curl/curl.h>
28 #include <boost/algorithm/string.hpp>
29 #include <string>
30 #include <iostream>
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         , _thread (0)
60         , _to_do (0)
61         , _terminate (false)
62 {
63         _curl = curl_easy_init ();
64
65         curl_easy_setopt (_curl, CURLOPT_URL, "http://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);
69
70         string const agent = "dcpomatic/" + string (dcpomatic_version);
71         curl_easy_setopt (_curl, CURLOPT_USERAGENT, agent.c_str ());
72 }
73
74 void
75 UpdateChecker::start ()
76 {
77         _thread = new boost::thread (boost::bind (&UpdateChecker::thread, this));
78 }
79
80 UpdateChecker::~UpdateChecker ()
81 {
82         {
83                 boost::mutex::scoped_lock lm (_process_mutex);
84                 _terminate = true;
85         }
86
87         _condition.notify_all ();
88         if (_thread) {
89                 DCPOMATIC_ASSERT (_thread->joinable ());
90                 _thread->join ();
91         }
92         delete _thread;
93
94         curl_easy_cleanup (_curl);
95         delete[] _buffer;
96 }
97
98 /** Start running the update check */
99 void
100 UpdateChecker::run ()
101 {
102         boost::mutex::scoped_lock lm (_process_mutex);
103         _to_do++;
104         _condition.notify_one ();
105 }
106
107 void
108 UpdateChecker::thread ()
109 {
110         while (true) {
111                 /* Block until there is something to do */
112                 boost::mutex::scoped_lock lock (_process_mutex);
113                 while (_to_do == 0 && !_terminate) {
114                         _condition.wait (lock);
115                 }
116
117                 if (_terminate) {
118                         return;
119                 }
120
121                 --_to_do;
122                 lock.unlock ();
123
124                 try {
125                         _offset = 0;
126
127                         /* Perform the request */
128
129                         int r = curl_easy_perform (_curl);
130                         if (r != CURLE_OK) {
131                                 set_state (FAILED);
132                                 return;
133                         }
134
135                         /* Parse the reply */
136
137                         _buffer[_offset] = '\0';
138                         string s (_buffer);
139                         cxml::Document doc ("Update");
140                         doc.read_string (s);
141
142                         /* Read the current stable and test version numbers */
143
144                         string stable;
145                         string test;
146
147                         {
148                                 boost::mutex::scoped_lock lm (_data_mutex);
149                                 stable = doc.string_child ("Stable");
150                                 test = doc.string_child ("Test");
151                         }
152
153                         if (version_less_than (dcpomatic_version, stable)) {
154                                 _stable = stable;
155                         }
156
157                         if (Config::instance()->check_for_test_updates() && version_less_than (dcpomatic_version, test)) {
158                                 _test = test;
159                         }
160
161                         if (_stable || _test) {
162                                 set_state (YES);
163                         } else {
164                                 set_state (NO);
165                         }
166                 } catch (...) {
167                         set_state (FAILED);
168                 }
169         }
170 }
171
172 size_t
173 UpdateChecker::write_callback (void* data, size_t size, size_t nmemb)
174 {
175         size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset - 1));
176         memcpy (_buffer + _offset, data, t);
177         _offset += t;
178         return t;
179 }
180
181 void
182 UpdateChecker::set_state (State s)
183 {
184         {
185                 boost::mutex::scoped_lock lm (_data_mutex);
186                 _state = s;
187                 _emits++;
188         }
189
190         emit (boost::bind (boost::ref (StateChanged)));
191 }
192
193 UpdateChecker *
194 UpdateChecker::instance ()
195 {
196         if (!_instance) {
197                 _instance = new UpdateChecker ();
198                 _instance->start ();
199         }
200
201         return _instance;
202 }
203
204 bool
205 UpdateChecker::version_less_than (string const & a, string const & b)
206 {
207         vector<string> ap;
208         split (ap, a, is_any_of ("."));
209         vector<string> bp;
210         split (bp, b, is_any_of ("."));
211
212         DCPOMATIC_ASSERT (ap.size() == 3 && bp.size() == 3);
213
214         if (ap[0] != bp[0]) {
215                 return raw_convert<int> (ap[0]) < raw_convert<int> (bp[0]);
216         }
217
218         if (ap[1] != bp[1]) {
219                 return raw_convert<int> (ap[1]) < raw_convert<int> (bp[1]);
220         }
221         float am;
222         if (ends_with (ap[2], "devel")) {
223                 am = raw_convert<int> (ap[2].substr (0, ap[2].length() - 5)) + 0.5;
224         } else {
225                 am = raw_convert<int> (ap[2]);
226         }
227
228         float bm;
229         if (ends_with (bp[2], "devel")) {
230                 bm = raw_convert<int> (bp[2].substr (0, bp[2].length() - 5)) + 0.5;
231         } else {
232                 bm = raw_convert<int> (bp[2]);
233         }
234
235         return am < bm;
236 }