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