summaryrefslogtreecommitdiff
path: root/src/lib/update.cc
blob: 6e9d1169acbfc968c9f28bb1f0196ed7f8291d00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <string>
#include <sstream>
#include <curl/curl.h>
#include <libcxml/cxml.h>
#include "update.h"
#include "version.h"

#define BUFFER_SIZE 1024

using std::cout;
using std::min;
using std::string;
using std::stringstream;

UpdateChecker::UpdateChecker ()
	: _buffer (new char[BUFFER_SIZE])
	, _offset (0)
{
	
}

UpdateChecker::~UpdateChecker ()
{
	delete[] _buffer;
}

static size_t
write_callback_wrapper (void* data, size_t size, size_t nmemb, void* user)
{
	return reinterpret_cast<UpdateChecker*>(user)->write_callback (data, size, nmemb);
}

UpdateChecker::Result
UpdateChecker::run ()
{
	curl_global_init (CURL_GLOBAL_ALL);
	CURL* curl = curl_easy_init ();
	if (!curl) {
		return MAYBE;
	}

	curl_easy_setopt (curl, CURLOPT_URL, "http://dcpomatic.com/update.php");
	curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, write_callback_wrapper);
	curl_easy_setopt (curl, CURLOPT_WRITEDATA, this);
	string const agent = "dcpomatic/" + string (dcpomatic_version);
	curl_easy_setopt (curl, CURLOPT_USERAGENT, agent.c_str ());
	int r = curl_easy_perform (curl);
	if (r != CURLE_OK) {
		return MAYBE;
	}

	_buffer[BUFFER_SIZE-1] = '\0';
	stringstream s;
	s << _buffer;
	cxml::Document doc ("Update");
	doc.read_stream (s);

	cout << doc.string_child ("Stable") << "\n";
	return YES;
}

size_t
UpdateChecker::write_callback (void* data, size_t size, size_t nmemb)
{
	size_t const t = min (size * nmemb, size_t (BUFFER_SIZE - _offset));
	memcpy (_buffer + _offset, data, t);
	_offset += t;
	return t;
}