d68979e60d7e44a8da0b3d58e252539802bb1720
[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 "update.h"
26 #include "version.h"
27 #include "ui_signaller.h"
28
29 #define BUFFER_SIZE 1024
30
31 using std::cout;
32 using std::min;
33 using std::string;
34 using std::stringstream;
35 using boost::lexical_cast;
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         , _startup (true)
51 {
52         curl_global_init (CURL_GLOBAL_ALL);
53         _curl = curl_easy_init ();
54
55         curl_easy_setopt (_curl, CURLOPT_URL, "http://dcpomatic.com/update");
56         curl_easy_setopt (_curl, CURLOPT_WRITEFUNCTION, write_callback_wrapper);
57         curl_easy_setopt (_curl, CURLOPT_WRITEDATA, this);
58         curl_easy_setopt (_curl, CURLOPT_TIMEOUT, 20);
59         
60         string const agent = "dcpomatic/" + string (dcpomatic_version);
61         curl_easy_setopt (_curl, CURLOPT_USERAGENT, agent.c_str ());
62 }
63
64 UpdateChecker::~UpdateChecker ()
65 {
66         curl_easy_cleanup (_curl);
67         curl_global_cleanup ();
68         delete[] _buffer;
69 }
70
71 void
72 UpdateChecker::run (bool startup)
73 try
74 {
75         boost::mutex::scoped_lock lm (_single_thread_mutex);
76
77         {
78                 boost::mutex::scoped_lock lm (_data_mutex);
79                 _startup = startup;
80         }
81         
82         _offset = 0;
83         
84         int r = curl_easy_perform (_curl);
85         if (r != CURLE_OK) {
86                 set_state (FAILED);
87                 return;
88         }
89         
90         _buffer[_offset] = '\0';
91         stringstream s;
92         s << _buffer;
93         cxml::Document doc ("Update");
94         doc.read_stream (s);
95
96         {
97                 boost::mutex::scoped_lock lm (_data_mutex);
98                 _stable = doc.string_child ("Stable");
99         }
100         
101         string current = string (dcpomatic_version);
102         bool current_pre = false;
103         if (boost::algorithm::ends_with (current, "pre")) {
104                 current = current.substr (0, current.length() - 3);
105                 current_pre = true;
106         }
107
108         float current_float = lexical_cast<float> (current);
109         if (current_pre) {
110                 current_float -= 0.005;
111         }
112
113         if (current_float < lexical_cast<float> (_stable)) {
114                 set_state (YES);
115         } else {
116                 set_state (NO);
117         }
118 } catch (...) {
119         set_state (FAILED);
120 }
121
122 size_t
123 UpdateChecker::write_callback (void* data, size_t size, size_t nmemb)
124 {
125         size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset - 1));
126         memcpy (_buffer + _offset, data, t);
127         _offset += t;
128         return t;
129 }
130
131 void
132 UpdateChecker::set_state (State s)
133 {
134         {
135                 boost::mutex::scoped_lock lm (_data_mutex);
136                 _state = s;
137         }
138         
139         ui_signaller->emit (boost::bind (boost::ref (StateChanged)));
140 }
141
142 UpdateChecker *
143 UpdateChecker::instance ()
144 {
145         if (!_instance) {
146                 _instance = new UpdateChecker ();
147         }
148
149         return _instance;
150 }
151
152