Merge master.
[dcpomatic.git] / src / lib / update.cc
index 6e5f92edc9eee06d9ea86a56502312bcfd1e6d08..44ecbb232c26b03214f23d3191c54fedd9a9071a 100644 (file)
@@ -22,6 +22,7 @@
 #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"
@@ -32,8 +33,9 @@ using std::cout;
 using std::min;
 using std::string;
 using std::stringstream;
-using boost::lexical_cast;
+using dcp::raw_convert;
 
+/** Singleton instance */
 UpdateChecker* UpdateChecker::_instance = 0;
 
 static size_t
@@ -42,12 +44,16 @@ write_callback_wrapper (void* data, size_t size, size_t nmemb, void* user)
        return reinterpret_cast<UpdateChecker*>(user)->write_callback (data, size, nmemb);
 }
 
+/** Construct an UpdateChecker.  This sets things up and starts a thread to
+ *  do the work.
+ */
 UpdateChecker::UpdateChecker ()
        : _buffer (new char[BUFFER_SIZE])
        , _offset (0)
        , _curl (0)
        , _state (NOT_RUN)
        , _emits (0)
+       , _to_do (0)
 {
        curl_global_init (CURL_GLOBAL_ALL);
        _curl = curl_easy_init ();
@@ -72,29 +78,39 @@ UpdateChecker::~UpdateChecker ()
        delete[] _buffer;
 }
 
+/** Start running the update check */
 void
 UpdateChecker::run ()
 {
        boost::mutex::scoped_lock lm (_process_mutex);
+       _to_do++;
        _condition.notify_one ();
 }
 
 void
 UpdateChecker::thread ()
 {
-       while (1) {
+       while (true) {
+               /* Block until there is something to do */
                boost::mutex::scoped_lock lock (_process_mutex);
-               _condition.wait (lock);
+               while (_to_do == 0) {
+                       _condition.wait (lock);
+               }
+               --_to_do;
                lock.unlock ();
                
                try {
                        _offset = 0;
+
+                       /* Perform the request */
                        
                        int r = curl_easy_perform (_curl);
                        if (r != CURLE_OK) {
                                set_state (FAILED);
                                return;
                        }
+
+                       /* Parse the reply */
                        
                        _buffer[_offset] = '\0';
                        stringstream s;
@@ -115,12 +131,12 @@ UpdateChecker::thread ()
                                current_pre = true;
                        }
                        
-                       float current_float = lexical_cast<float> (current);
+                       float current_float = raw_convert<float> (current);
                        if (current_pre) {
                                current_float -= 0.005;
                        }
                        
-                       if (current_float < lexical_cast<float> (_stable)) {
+                       if (current_float < raw_convert<float> (_stable)) {
                                set_state (YES);
                        } else {
                                set_state (NO);