Fix ran-for display in makedcp.
[dcpomatic.git] / src / lib / job.cc
index 0feb73d31b22b00f59d6c5cd6c3dea12c8d38a2e..e78a9131581e6183ceefc2fb96ac20a4b28f421e 100644 (file)
@@ -41,6 +41,7 @@ Job::Job (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l)
        , _state (NEW)
        , _start_time (0)
        , _progress_unknown (false)
+       , _ran_for (0)
 {
        assert (_log);
        
@@ -121,6 +122,10 @@ Job::set_state (State s)
 {
        boost::mutex::scoped_lock lm (_state_mutex);
        _state = s;
+
+       if (_state == FINISHED_OK || _state == FINISHED_ERROR) {
+               _ran_for = elapsed_time ();
+       }
 }
 
 /** A hack to work around our lack of cross-thread
@@ -223,7 +228,7 @@ Job::set_error (string e)
        _error = e;
 }
 
-/** Set that this job's progress will always be unknown */
+/** Say that this job's progress will always be unknown */
 void
 Job::set_progress_unknown ()
 {
@@ -231,22 +236,31 @@ Job::set_progress_unknown ()
        _progress_unknown = true;
 }
 
+/** @return Human-readable status of this job */
 string
 Job::status () const
 {
        float const p = overall_progress ();
        int const t = elapsed_time ();
+       int const r = remaining_time ();
        
        stringstream s;
-       if (!finished () && p >= 0 && t > 10) {
-               s << rint (p * 100) << "%; about " << seconds_to_approximate_hms (t / p - t) << " remaining";
-       } else if (!finished () && t <= 10) {
+       if (!finished () && p >= 0 && t > 10 && r > 0) {
+               s << rint (p * 100) << "%; " << seconds_to_approximate_hms (r) << " remaining";
+       } else if (!finished () && (t <= 10 || r == 0)) {
                s << rint (p * 100) << "%";
        } else if (finished_ok ()) {
-               s << "OK (ran for " << seconds_to_hms (t) << ")";
+               s << "OK (ran for " << seconds_to_hms (_ran_for) << ")";
        } else if (finished_in_error ()) {
                s << "Error (" << error() << ")";
        }
 
        return s.str ();
 }
+
+/** @return An estimate of the remaining time for this job, in seconds */
+int
+Job::remaining_time () const
+{
+       return elapsed_time() / overall_progress() - elapsed_time();
+}