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