Merge.
[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 <libdcp/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 libdcp::raw_convert;
37
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 UpdateChecker::UpdateChecker ()
47         : _buffer (new char[BUFFER_SIZE])
48         , _offset (0)
49         , _curl (0)
50         , _state (NOT_RUN)
51         , _emits (0)
52         , _to_do (0)
53 {
54         curl_global_init (CURL_GLOBAL_ALL);
55         _curl = curl_easy_init ();
56
57         curl_easy_setopt (_curl, CURLOPT_URL, "http://dcpomatic.com/update");
58         curl_easy_setopt (_curl, CURLOPT_WRITEFUNCTION, write_callback_wrapper);
59         curl_easy_setopt (_curl, CURLOPT_WRITEDATA, this);
60         curl_easy_setopt (_curl, CURLOPT_TIMEOUT, 20);
61         
62         string const agent = "dcpomatic/" + string (dcpomatic_version);
63         curl_easy_setopt (_curl, CURLOPT_USERAGENT, agent.c_str ());
64
65         _thread = new boost::thread (boost::bind (&UpdateChecker::thread, this));
66 }
67
68 UpdateChecker::~UpdateChecker ()
69 {
70         /* We are not cleaning up our thread, but hey well */
71         
72         curl_easy_cleanup (_curl);
73         curl_global_cleanup ();
74         delete[] _buffer;
75 }
76
77 void
78 UpdateChecker::run ()
79 {
80         boost::mutex::scoped_lock lm (_process_mutex);
81         _to_do++;
82         _condition.notify_one ();
83 }
84
85 void
86 UpdateChecker::thread ()
87 {
88         while (true) {
89                 boost::mutex::scoped_lock lock (_process_mutex);
90                 while (_to_do == 0) {
91                         _condition.wait (lock);
92                 }
93                 --_to_do;
94                 lock.unlock ();
95                 
96                 try {
97                         _offset = 0;
98                         
99                         int r = curl_easy_perform (_curl);
100                         if (r != CURLE_OK) {
101                                 set_state (FAILED);
102                                 return;
103                         }
104                         
105                         _buffer[_offset] = '\0';
106                         stringstream s;
107                         s << _buffer;
108                         cxml::Document doc ("Update");
109                         doc.read_stream (s);
110                         
111                         {
112                                 boost::mutex::scoped_lock lm (_data_mutex);
113                                 _stable = doc.string_child ("Stable");
114                                 _test = doc.string_child ("Test");
115                         }
116                         
117                         string current = string (dcpomatic_version);
118                         bool current_pre = false;
119                         if (boost::algorithm::ends_with (current, "pre")) {
120                                 current = current.substr (0, current.length() - 3);
121                                 current_pre = true;
122                         }
123                         
124                         float current_float = raw_convert<float> (current);
125                         if (current_pre) {
126                                 current_float -= 0.005;
127                         }
128                         
129                         if (current_float < raw_convert<float> (_stable)) {
130                                 set_state (YES);
131                         } else {
132                                 set_state (NO);
133                         }
134                 } catch (...) {
135                         set_state (FAILED);
136                 }
137         }
138 }
139         
140 size_t
141 UpdateChecker::write_callback (void* data, size_t size, size_t nmemb)
142 {
143         size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset - 1));
144         memcpy (_buffer + _offset, data, t);
145         _offset += t;
146         return t;
147 }
148
149 void
150 UpdateChecker::set_state (State s)
151 {
152         {
153                 boost::mutex::scoped_lock lm (_data_mutex);
154                 _state = s;
155                 _emits++;
156         }
157
158         ui_signaller->emit (boost::bind (boost::ref (StateChanged)));
159 }
160
161 UpdateChecker *
162 UpdateChecker::instance ()
163 {
164         if (!_instance) {
165                 _instance = new UpdateChecker ();
166         }
167
168         return _instance;
169 }
170
171