Emit the state of the job with the Finished signals.
[dcpomatic.git] / src / lib / job.cc
index 6ec154c34b3804c7edc040367ce88ee1e0a7fdef..912c6a6ef3c961374a81bd4863509a94d0c524f2 100644 (file)
@@ -344,6 +344,10 @@ Job::set_state (State s)
 
        {
                boost::mutex::scoped_lock lm (_state_mutex);
+               if (_state == s) {
+                       return;
+               }
+
                _state = s;
 
                if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
@@ -354,12 +358,31 @@ Job::set_state (State s)
        }
 
        if (finished) {
-               emit (boost::bind (boost::ref (Finished)));
-               FinishedImmediate ();
+               auto const result = state_to_result(s);
+               emit(boost::bind(boost::ref(Finished), result));
+               FinishedImmediate(result);
        }
 }
 
 
+Job::Result
+Job::state_to_result(State state) const
+{
+       switch (state) {
+       case FINISHED_OK:
+               return Result::RESULT_OK;
+       case FINISHED_ERROR:
+               return Result::RESULT_ERROR;
+       case FINISHED_CANCELLED:
+               return Result::RESULT_CANCELLED;
+       default:
+               DCPOMATIC_ASSERT(false);
+       };
+
+       DCPOMATIC_ASSERT(false);
+}
+
+
 /** @return DCPTime (in seconds) that this sub-job has been running */
 int
 Job::elapsed_sub_time () const
@@ -673,11 +696,11 @@ Job::resume ()
 
 
 void
-Job::when_finished (boost::signals2::connection& connection, function<void()> finished)
+Job::when_finished(boost::signals2::connection& connection, function<void(Result)> finished)
 {
        boost::mutex::scoped_lock lm (_state_mutex);
        if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
-               finished ();
+               finished(state_to_result(_state));
        } else {
                connection = Finished.connect (finished);
        }