Merge remote-tracking branch 'origin/master' into 2.0
[dcpomatic.git] / src / lib / update.cc
1 /*
2     Copyright (C) 2014 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
30 #define BUFFER_SIZE 1024
31
32 using std::cout;
33 using std::min;
34 using std::string;
35 using dcp::raw_convert;
36
37 /** Singleton instance */
38 UpdateChecker* UpdateChecker::_instance = 0;
39
40 static size_t
41 write_callback_wrapper (void* data, size_t size, size_t nmemb, void* user)
42 {
43         return reinterpret_cast<UpdateChecker*>(user)->write_callback (data, size, nmemb);
44 }
45
46 /** Construct an UpdateChecker.  This sets things up and starts a thread to
47  *  do the work.
48  */
49 UpdateChecker::UpdateChecker ()
50         : _buffer (new char[BUFFER_SIZE])
51         , _offset (0)
52         , _curl (0)
53         , _state (NOT_RUN)
54         , _emits (0)
55         , _to_do (0)
56 {
57         curl_global_init (CURL_GLOBAL_ALL);
58         _curl = curl_easy_init ();
59
60         curl_easy_setopt (_curl, CURLOPT_URL, "http://dcpomatic.com/update");
61         curl_easy_setopt (_curl, CURLOPT_WRITEFUNCTION, write_callback_wrapper);
62         curl_easy_setopt (_curl, CURLOPT_WRITEDATA, this);
63         curl_easy_setopt (_curl, CURLOPT_TIMEOUT, 20);
64         
65         string const agent = "dcpomatic/" + string (dcpomatic_version);
66         curl_easy_setopt (_curl, CURLOPT_USERAGENT, agent.c_str ());
67
68         _thread = new boost::thread (boost::bind (&UpdateChecker::thread, this));
69 }
70
71 UpdateChecker::~UpdateChecker ()
72 {
73         /* We are not cleaning up our thread, but hey well */
74         
75         curl_easy_cleanup (_curl);
76         curl_global_cleanup ();
77         delete[] _buffer;
78 }
79
80 /** Start running the update check */
81 void
82 UpdateChecker::run ()
83 {
84         boost::mutex::scoped_lock lm (_process_mutex);
85         _to_do++;
86         _condition.notify_one ();
87 }
88
89 void
90 UpdateChecker::thread ()
91 {
92         while (true) {
93                 /* Block until there is something to do */
94                 boost::mutex::scoped_lock lock (_process_mutex);
95                 while (_to_do == 0) {
96                         _condition.wait (lock);
97                 }
98                 --_to_do;
99                 lock.unlock ();
100                 
101                 try {
102                         _offset = 0;
103
104                         /* Perform the request */
105                         
106                         int r = curl_easy_perform (_curl);
107                         if (r != CURLE_OK) {
108                                 set_state (FAILED);
109                                 return;
110                         }
111
112                         /* Parse the reply */
113                         
114                         _buffer[_offset] = '\0';
115                         string s (_buffer);
116                         cxml::Document doc ("Update");
117                         doc.read_string (s);
118                         
119                         {
120                                 boost::mutex::scoped_lock lm (_data_mutex);
121                                 _stable = doc.string_child ("Stable");
122                                 _test = doc.string_child ("Test");
123                         }
124                         
125                         string current = string (dcpomatic_version);
126                         bool current_pre = false;
127                         if (boost::algorithm::ends_with (current, "pre")) {
128                                 current = current.substr (0, current.length() - 3);
129                                 current_pre = true;
130                         }
131                         
132                         float current_float = raw_convert<float> (current);
133                         if (current_pre) {
134                                 current_float -= 0.005;
135                         }
136                         
137                         if (current_float < raw_convert<float> (_stable)) {
138                                 set_state (YES);
139                         } else {
140                                 set_state (NO);
141                         }
142                 } catch (...) {
143                         set_state (FAILED);
144                 }
145         }
146 }
147         
148 size_t
149 UpdateChecker::write_callback (void* data, size_t size, size_t nmemb)
150 {
151         size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset - 1));
152         memcpy (_buffer + _offset, data, t);
153         _offset += t;
154         return t;
155 }
156
157 void
158 UpdateChecker::set_state (State s)
159 {
160         {
161                 boost::mutex::scoped_lock lm (_data_mutex);
162                 _state = s;
163                 _emits++;
164         }
165
166         ui_signaller->emit (boost::bind (boost::ref (StateChanged)));
167 }
168
169 UpdateChecker *
170 UpdateChecker::instance ()
171 {
172         if (!_instance) {
173                 _instance = new UpdateChecker ();
174         }
175
176         return _instance;
177 }
178
179