Add unfinished check-for-updates. Bump ffmpeg to get windows build fix.
[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 <curl/curl.h>
23 #include <libcxml/cxml.h>
24 #include "update.h"
25 #include "version.h"
26
27 #define BUFFER_SIZE 1024
28
29 using std::cout;
30 using std::min;
31 using std::string;
32 using std::stringstream;
33
34 UpdateChecker::UpdateChecker ()
35         : _buffer (new char[BUFFER_SIZE])
36         , _offset (0)
37 {
38         
39 }
40
41 UpdateChecker::~UpdateChecker ()
42 {
43         delete[] _buffer;
44 }
45
46 static size_t
47 write_callback_wrapper (void* data, size_t size, size_t nmemb, void* user)
48 {
49         return reinterpret_cast<UpdateChecker*>(user)->write_callback (data, size, nmemb);
50 }
51
52 UpdateChecker::Result
53 UpdateChecker::run ()
54 {
55         curl_global_init (CURL_GLOBAL_ALL);
56         CURL* curl = curl_easy_init ();
57         if (!curl) {
58                 return MAYBE;
59         }
60
61         curl_easy_setopt (curl, CURLOPT_URL, "http://dcpomatic.com/update.php");
62         curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_callback_wrapper);
63         curl_easy_setopt (curl, CURLOPT_WRITEDATA, this);
64         string const agent = "dcpomatic/" + string (dcpomatic_version);
65         curl_easy_setopt (curl, CURLOPT_USERAGENT, agent.c_str ());
66         int r = curl_easy_perform (curl);
67         if (r != CURLE_OK) {
68                 return MAYBE;
69         }
70
71         _buffer[BUFFER_SIZE-1] = '\0';
72         stringstream s;
73         s << _buffer;
74         cxml::Document doc ("Update");
75         doc.read_stream (s);
76
77         cout << doc.string_child ("Stable") << "\n";
78         return YES;
79 }
80
81 size_t
82 UpdateChecker::write_callback (void* data, size_t size, size_t nmemb)
83 {
84         size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset));
85         memcpy (_buffer + _offset, data, t);
86         _offset += t;
87         return t;
88 }