X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fupdate.cc;h=24cd28b167bde800aeb419be236ea85d69036da7;hb=e60bb3e51bd1508b149e6b8f6608f09b5196ae26;hp=c50022091fbe3ab6abb0169e86fa00e7444c4cb3;hpb=12efbd5938f08eb445b43f539fa4f27aa5caccfb;p=dcpomatic.git diff --git a/src/lib/update.cc b/src/lib/update.cc index c50022091..24cd28b16 100644 --- a/src/lib/update.cc +++ b/src/lib/update.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2014 Carl Hetherington + Copyright (C) 2014-2015 Carl Hetherington 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 @@ -17,22 +17,25 @@ */ -#include -#include -#include -#include -#include #include "update.h" #include "version.h" -#include "ui_signaller.h" #include "safe_stringstream.h" +#include "config.h" +#include "util.h" +#include "raw_convert.h" +#include +#include +#include +#include #define BUFFER_SIZE 1024 using std::cout; using std::min; using std::string; -using dcp::raw_convert; +using std::vector; +using boost::is_any_of; +using boost::ends_with; /** Singleton instance */ UpdateChecker* UpdateChecker::_instance = 0; @@ -61,7 +64,7 @@ UpdateChecker::UpdateChecker () curl_easy_setopt (_curl, CURLOPT_WRITEFUNCTION, write_callback_wrapper); curl_easy_setopt (_curl, CURLOPT_WRITEDATA, this); curl_easy_setopt (_curl, CURLOPT_TIMEOUT, 20); - + string const agent = "dcpomatic/" + string (dcpomatic_version); curl_easy_setopt (_curl, CURLOPT_USERAGENT, agent.c_str ()); @@ -71,7 +74,7 @@ UpdateChecker::UpdateChecker () UpdateChecker::~UpdateChecker () { /* We are not cleaning up our thread, but hey well */ - + curl_easy_cleanup (_curl); curl_global_cleanup (); delete[] _buffer; @@ -97,12 +100,12 @@ UpdateChecker::thread () } --_to_do; lock.unlock (); - + try { _offset = 0; /* Perform the request */ - + int r = curl_easy_perform (_curl); if (r != CURLE_OK) { set_state (FAILED); @@ -110,31 +113,32 @@ UpdateChecker::thread () } /* Parse the reply */ - + _buffer[_offset] = '\0'; string s (_buffer); cxml::Document doc ("Update"); doc.read_string (s); - + + /* Read the current stable and test version numbers */ + + string stable; + string test; + { boost::mutex::scoped_lock lm (_data_mutex); - _stable = doc.string_child ("Stable"); - _test = doc.string_child ("Test"); + stable = doc.string_child ("Stable"); + test = doc.string_child ("Test"); } - - string current = string (dcpomatic_version); - bool current_pre = false; - if (boost::algorithm::ends_with (current, "pre")) { - current = current.substr (0, current.length() - 3); - current_pre = true; + + if (version_less_than (dcpomatic_version, stable)) { + _stable = stable; } - - float current_float = raw_convert (current); - if (current_pre) { - current_float -= 0.005; + + if (Config::instance()->check_for_test_updates() && version_less_than (dcpomatic_version, test)) { + _test = test; } - - if (current_float < raw_convert (_stable)) { + + if (_stable || _test) { set_state (YES); } else { set_state (NO); @@ -144,7 +148,7 @@ UpdateChecker::thread () } } } - + size_t UpdateChecker::write_callback (void* data, size_t size, size_t nmemb) { @@ -163,7 +167,7 @@ UpdateChecker::set_state (State s) _emits++; } - ui_signaller->emit (boost::bind (boost::ref (StateChanged))); + emit (boost::bind (boost::ref (StateChanged))); } UpdateChecker * @@ -176,4 +180,36 @@ UpdateChecker::instance () return _instance; } - +bool +UpdateChecker::version_less_than (string const & a, string const & b) +{ + vector ap; + split (ap, a, is_any_of (".")); + vector bp; + split (bp, b, is_any_of (".")); + + DCPOMATIC_ASSERT (ap.size() == 3 && bp.size() == 3); + + if (ap[0] != bp[0]) { + return raw_convert (ap[0]) < raw_convert (bp[0]); + } + + if (ap[1] != bp[1]) { + return raw_convert (ap[1]) < raw_convert (bp[1]); + } + float am; + if (ends_with (ap[2], "devel")) { + am = raw_convert (ap[2].substr (0, ap[2].length() - 5)) + 0.5; + } else { + am = raw_convert (ap[2]); + } + + float bm; + if (ends_with (bp[2], "devel")) { + bm = raw_convert (bp[2].substr (0, bp[2].length() - 5)) + 0.5; + } else { + bm = raw_convert (bp[2]); + } + + return am < bm; +}