Fix exception on adding an empty folder as content (#691).
[dcpomatic.git] / src / lib / job.cc
index d9715aa18535603d6040b6c8287b2788e385f086..c4632a83c6e9e9dbb036de90a3598d2abe5cd02e 100644 (file)
@@ -31,6 +31,7 @@
 #include <dcp/exceptions.h>
 #include <boost/thread.hpp>
 #include <boost/filesystem.hpp>
+#include <iostream>
 
 #include "i18n.h"
 
@@ -39,6 +40,7 @@ using std::list;
 using std::cout;
 using boost::shared_ptr;
 using boost::optional;
+using boost::function;
 
 #define LOG_ERROR_NC(...) _film->log()->log (__VA_ARGS__, Log::TYPE_ERROR);
 
@@ -53,6 +55,16 @@ Job::Job (shared_ptr<const Film> film)
 
 }
 
+Job::~Job ()
+{
+       if (_thread) {
+               _thread->interrupt ();
+               _thread->join ();
+       }
+
+       delete _thread;
+}
+
 /** Start the job in a separate thread, returning immediately */
 void
 Job::start ()
@@ -247,6 +259,12 @@ Job::set_progress (float p, bool force)
                return;
        }
 
+       set_progress_common (p);
+}
+
+void
+Job::set_progress_common (optional<float> p)
+{
        boost::mutex::scoped_lock lm (_progress_mutex);
        _progress = p;
        boost::this_thread::interruption_point ();
@@ -315,11 +333,7 @@ Job::set_error (string s, string d)
 void
 Job::set_progress_unknown ()
 {
-       boost::mutex::scoped_lock lm (_progress_mutex);
-       _progress.reset ();
-       lm.unlock ();
-
-       emit (boost::bind (boost::ref (Progress)));
+       set_progress_common (optional<float> ());
 }
 
 /** @return Human-readable status of this job */
@@ -422,3 +436,15 @@ Job::resume ()
                _pause_changed.notify_all ();
        }
 }
+
+void
+Job::when_finished (boost::signals2::connection& connection, function<void()> finished)
+{
+       boost::mutex::scoped_lock lm (_state_mutex);
+       if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
+               finished ();
+       } else {
+               connection = Finished.connect (finished);
+       }
+}
+