Emit the state of the job with the Finished signals.
[dcpomatic.git] / src / lib / job.cc
index 50dcc336e2fd9029b3d659246165e06d3e303f10..912c6a6ef3c961374a81bd4863509a94d0c524f2 100644 (file)
@@ -25,6 +25,7 @@
 
 
 #include "compose.hpp"
+#include "constants.h"
 #include "cross.h"
 #include "dcpomatic_log.h"
 #include "exceptions.h"
@@ -199,9 +200,7 @@ Job::run_wrapper ()
                set_state (FINISHED_ERROR);
 
        } catch (boost::thread_interrupted &) {
-
-               set_state (FINISHED_CANCELLED);
-
+               /* The job was cancelled; there's nothing else we need to do here */
        } catch (sub::SubripError& e) {
 
                string extra = "Error is near:\n";
@@ -237,6 +236,12 @@ Job::run_wrapper ()
                set_progress (1);
                set_state (FINISHED_ERROR);
 
+       } catch (CPLNotFoundError& e) {
+
+               set_error(e.what());
+               set_progress(1);
+               set_state(FINISHED_ERROR);
+
        } catch (std::exception& e) {
 
                set_error (
@@ -339,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) {
@@ -349,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
@@ -509,6 +537,27 @@ Job::status () const
        int const t = elapsed_sub_time ();
        int const r = remaining_time ();
 
+       auto day_of_week_to_string = [](boost::gregorian::greg_weekday d) -> std::string {
+               switch (d.as_enum()) {
+               case boost::date_time::Sunday:
+                       return _("Sunday");
+               case boost::date_time::Monday:
+                       return _("Monday");
+               case boost::date_time::Tuesday:
+                       return _("Tuesday");
+               case boost::date_time::Wednesday:
+                       return _("Wednesday");
+               case boost::date_time::Thursday:
+                       return _("Thursday");
+               case boost::date_time::Friday:
+                       return _("Friday");
+               case boost::date_time::Saturday:
+                       return _("Saturday");
+               }
+
+               return d.as_long_string();
+       };
+
        string s;
        if (!finished () && p) {
                int pc = lrintf (p.get() * 100);
@@ -591,16 +640,14 @@ Job::remaining_time () const
 void
 Job::cancel ()
 {
-       if (!_thread.joinable()) {
-               return;
-       }
+       if (_thread.joinable()) {
+               resume();
 
-       if (paused_by_user() || paused_by_priority()) {
-               resume ();
+               _thread.interrupt ();
+               _thread.join ();
        }
 
-       _thread.interrupt ();
-       _thread.join ();
+       set_state (FINISHED_CANCELLED);
 }
 
 
@@ -649,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);
        }