Fix crashes on x-thread signal emission.
[dcpomatic.git] / src / lib / update.cc
index 44ecbb232c26b03214f23d3191c54fedd9a9071a..26944ecc32d2bc3503d2472f91d7feff49849533 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2015 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
 
 */
 
-#include <string>
-#include <sstream>
-#include <boost/algorithm/string.hpp>
-#include <curl/curl.h>
-#include <libcxml/cxml.h>
-#include <dcp/raw_convert.h>
 #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 <libcxml/cxml.h>
+#include <curl/curl.h>
+#include <boost/algorithm/string.hpp>
+#include <string>
 
 #define BUFFER_SIZE 1024
 
 using std::cout;
 using std::min;
 using std::string;
-using std::stringstream;
-using dcp::raw_convert;
+using std::vector;
+using boost::is_any_of;
+using boost::ends_with;
 
 /** Singleton instance */
 UpdateChecker* UpdateChecker::_instance = 0;
@@ -113,30 +116,30 @@ UpdateChecker::thread ()
                        /* Parse the reply */
                        
                        _buffer[_offset] = '\0';
-                       stringstream s;
-                       s << _buffer;
+                       string s (_buffer);
                        cxml::Document doc ("Update");
-                       doc.read_stream (s);
-                       
+                       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<float> (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<float> (_stable)) {
+
+                       if (_stable || _test) {
                                set_state (YES);
                        } else {
                                set_state (NO);
@@ -165,7 +168,7 @@ UpdateChecker::set_state (State s)
                _emits++;
        }
 
-       ui_signaller->emit (boost::bind (boost::ref (StateChanged)));
+       emit (boost::bind (boost::ref (StateChanged)));
 }
 
 UpdateChecker *
@@ -178,4 +181,36 @@ UpdateChecker::instance ()
        return _instance;
 }
 
+bool
+UpdateChecker::version_less_than (string const & a, string const & b)
+{
+       vector<string> ap;
+       split (ap, a, is_any_of ("."));
+       vector<string> bp;
+       split (bp, b, is_any_of ("."));
+
+       DCPOMATIC_ASSERT (ap.size() == 3 && bp.size() == 3);
+
+       if (ap[0] != bp[0]) {
+               return raw_convert<int> (ap[0]) < raw_convert<int> (bp[0]);
+       }
+
+       if (ap[1] != bp[1]) {
+               return raw_convert<int> (ap[1]) < raw_convert<int> (bp[1]);
+       }
+       float am;
+       if (ends_with (ap[2], "devel")) {
+               am = raw_convert<int> (ap[2].substr (0, ap[2].length() - 5)) + 0.5;
+       } else {
+               am = raw_convert<int> (ap[2]);
+       }
        
+       float bm;
+       if (ends_with (bp[2], "devel")) {
+               bm = raw_convert<int> (bp[2].substr (0, bp[2].length() - 5)) + 0.5;
+       } else {
+               bm = raw_convert<int> (bp[2]);
+       }
+       
+       return am < bm;
+}