Lots of #include <iostream>s for Arch.
[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                 _thread->join ();
90         }
91         delete _thread;
92
93         curl_easy_cleanup (_curl);
94         delete[] _buffer;
95 }
96
97 /** Start running the update check */
98 void
99 UpdateChecker::run ()
100 {
101         boost::mutex::scoped_lock lm (_process_mutex);
102         _to_do++;
103         _condition.notify_one ();
104 }
105
106 void
107 UpdateChecker::thread ()
108 {
109         while (true) {
110                 /* Block until there is something to do */
111                 boost::mutex::scoped_lock lock (_process_mutex);
112                 while (_to_do == 0 && !_terminate) {
113                         _condition.wait (lock);
114                 }
115
116                 if (_terminate) {
117                         return;
118                 }
119
120                 --_to_do;
121                 lock.unlock ();
122
123                 try {
124                         _offset = 0;
125
126                         /* Perform the request */
127
128                         int r = curl_easy_perform (_curl);
129                         if (r != CURLE_OK) {
130                                 set_state (FAILED);
131                                 return;
132                         }
133
134                         /* Parse the reply */
135
136                         _buffer[_offset] = '\0';
137                         string s (_buffer);
138                         cxml::Document doc ("Update");
139                         doc.read_string (s);
140
141                         /* Read the current stable and test version numbers */
142
143                         string stable;
144                         string test;
145
146                         {
147                                 boost::mutex::scoped_lock lm (_data_mutex);
148                                 stable = doc.string_child ("Stable");
149                                 test = doc.string_child ("Test");
150                         }
151
152                         if (version_less_than (dcpomatic_version, stable)) {
153                                 _stable = stable;
154                         }
155
156                         if (Config::instance()->check_for_test_updates() && version_less_than (dcpomatic_version, test)) {
157                                 _test = test;
158                         }
159
160                         if (_stable || _test) {
161                                 set_state (YES);
162                         } else {
163                                 set_state (NO);
164                         }
165                 } catch (...) {
166                         set_state (FAILED);
167                 }
168         }
169 }
170
171 size_t
172 UpdateChecker::write_callback (void* data, size_t size, size_t nmemb)
173 {
174         size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset - 1));
175         memcpy (_buffer + _offset, data, t);
176         _offset += t;
177         return t;
178 }
179
180 void
181 UpdateChecker::set_state (State s)
182 {
183         {
184                 boost::mutex::scoped_lock lm (_data_mutex);
185                 _state = s;
186                 _emits++;
187         }
188
189         emit (boost::bind (boost::ref (StateChanged)));
190 }
191
192 UpdateChecker *
193 UpdateChecker::instance ()
194 {
195         if (!_instance) {
196                 _instance = new UpdateChecker ();
197                 _instance->start ();
198         }
199
200         return _instance;
201 }
202
203 bool
204 UpdateChecker::version_less_than (string const & a, string const & b)
205 {
206         vector<string> ap;
207         split (ap, a, is_any_of ("."));
208         vector<string> bp;
209         split (bp, b, is_any_of ("."));
210
211         DCPOMATIC_ASSERT (ap.size() == 3 && bp.size() == 3);
212
213         if (ap[0] != bp[0]) {
214                 return raw_convert<int> (ap[0]) < raw_convert<int> (bp[0]);
215         }
216
217         if (ap[1] != bp[1]) {
218                 return raw_convert<int> (ap[1]) < raw_convert<int> (bp[1]);
219         }
220         float am;
221         if (ends_with (ap[2], "devel")) {
222                 am = raw_convert<int> (ap[2].substr (0, ap[2].length() - 5)) + 0.5;
223         } else {
224                 am = raw_convert<int> (ap[2]);
225         }
226
227         float bm;
228         if (ends_with (bp[2], "devel")) {
229                 bm = raw_convert<int> (bp[2].substr (0, bp[2].length() - 5)) + 0.5;
230         } else {
231                 bm = raw_convert<int> (bp[2]);
232         }
233
234         return am < bm;
235 }