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