X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fjob.cc;h=080d1eaf6018a5588c9d9f0c31a63c894585a4a1;hb=89115db77729a2c99f1a09ff6a461720e16f889e;hp=896862d143f7c9c7c26514d4e17d1e7d966652f5;hpb=643519230e7060ada52a352b280b289793abf115;p=dcpomatic.git diff --git a/src/lib/job.cc b/src/lib/job.cc index 896862d14..080d1eaf6 100644 --- a/src/lib/job.cc +++ b/src/lib/job.cc @@ -26,18 +26,19 @@ #include #include "job.h" #include "util.h" +#include "cross.h" +#include "ui_signaller.h" + +#include "i18n.h" using std::string; using std::list; using std::stringstream; using boost::shared_ptr; -/** @param s Film that we are operating on. - * @param req Job that must be completed before this job is run. - */ -Job::Job (shared_ptr f, shared_ptr req) +Job::Job (shared_ptr f) : _film (f) - , _required (req) + , _thread (0) , _state (NEW) , _start_time (0) , _progress_unknown (false) @@ -52,7 +53,7 @@ Job::start () { set_state (RUNNING); _start_time = time (0); - boost::thread (boost::bind (&Job::run_wrapper, this)); + _thread = new boost::thread (boost::bind (&Job::run_wrapper, this)); } /** A wrapper for the ::run() method to catch exceptions */ @@ -67,19 +68,42 @@ Job::run_wrapper () set_progress (1); set_state (FINISHED_ERROR); - set_error (String::compose ("%1 (%2)", e.what(), boost::filesystem::path (e.filename()).leaf())); + string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf()); + + try { + boost::filesystem::space_info const s = boost::filesystem::space (e.filename()); + if (s.available < pow (1024, 3)) { + m += N_("\n\n"); + m += _("The drive that the film is stored on is low in disc space. Free some more space and try again."); + } + } catch (...) { + + } + + set_error (e.what(), m); + + } catch (boost::thread_interrupted &) { + + set_state (FINISHED_CANCELLED); + } catch (std::exception& e) { set_progress (1); set_state (FINISHED_ERROR); - set_error (e.what ()); + set_error ( + e.what (), + _("It is not known what caused this error. The best idea is to report the problem to the DCP-o-matic mailing list (dcpomatic@carlh.net)") + ); } catch (...) { set_progress (1); set_state (FINISHED_ERROR); - set_error ("unknown exception"); + set_error ( + _("Unknown error"), + _("It is not known what caused this error. The best idea is to report the problem to the DCP-o-matic mailing list (dcpomatic@carlh.net)") + ); } } @@ -105,7 +129,7 @@ bool Job::finished () const { boost::mutex::scoped_lock lm (_state_mutex); - return _state == FINISHED_OK || _state == FINISHED_ERROR; + return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED; } /** @return true if the job has finished successfully */ @@ -124,6 +148,20 @@ Job::finished_in_error () const return _state == FINISHED_ERROR; } +bool +Job::finished_cancelled () const +{ + boost::mutex::scoped_lock lm (_state_mutex); + return _state == FINISHED_CANCELLED; +} + +bool +Job::paused () const +{ + boost::mutex::scoped_lock lm (_state_mutex); + return _state == PAUSED; +} + /** Set the state of this job. * @param s New state. */ @@ -133,9 +171,11 @@ Job::set_state (State s) boost::mutex::scoped_lock lm (_state_mutex); _state = s; - if (_state == FINISHED_OK || _state == FINISHED_ERROR) { + if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) { _ran_for = elapsed_time (); - Finished (); + if (ui_signaller) { + ui_signaller->emit (boost::bind (boost::ref (Finished))); + } } } @@ -159,6 +199,11 @@ Job::set_progress (float p) boost::mutex::scoped_lock lm (_progress_mutex); _progress_unknown = false; _stack.back().normalised = p; + boost::this_thread::interruption_point (); + + if (paused ()) { + dcpomatic_sleep (1); + } } /** @return fractional overall progress, or -1 if not known */ @@ -211,22 +256,30 @@ Job::descend (float a) _stack.push_back (Level (a)); } -/** @return Any error string that the job has generated */ string -Job::error () const +Job::error_details () const +{ + boost::mutex::scoped_lock lm (_state_mutex); + return _error_details; +} + +/** @return A summary of any error that the job has generated */ +string +Job::error_summary () const { boost::mutex::scoped_lock lm (_state_mutex); - return _error; + return _error_summary; } /** Set the current error string. * @param e New error string. */ void -Job::set_error (string e) +Job::set_error (string s, string d) { boost::mutex::scoped_lock lm (_state_mutex); - _error = e; + _error_summary = s; + _error_details = d; } /** Say that this job's progress will be unknown until further notice */ @@ -245,15 +298,26 @@ Job::status () const int const t = elapsed_time (); int const r = remaining_time (); + int pc = rint (p * 100); + if (pc == 100) { + /* 100% makes it sound like we've finished when we haven't */ + pc = 99; + } + stringstream s; - 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) << "%"; + if (!finished ()) { + s << pc << N_("%"); + if (p >= 0 && t > 10 && r > 0) { + /// TRANSLATORS: remaining here follows an amount of time that is remaining + /// on an operation. + s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining"); + } } else if (finished_ok ()) { - s << "OK (ran for " << seconds_to_hms (_ran_for) << ")"; + s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for)); } else if (finished_in_error ()) { - s << "Error (" << error() << ")"; + s << String::compose (_("Error (%1)"), error_summary()); + } else if (finished_cancelled ()) { + s << _("Cancelled"); } return s.str (); @@ -265,3 +329,30 @@ Job::remaining_time () const { return elapsed_time() / overall_progress() - elapsed_time(); } + +void +Job::cancel () +{ + if (!_thread) { + return; + } + + _thread->interrupt (); + _thread->join (); +} + +void +Job::pause () +{ + if (running ()) { + set_state (PAUSED); + } +} + +void +Job::resume () +{ + if (paused ()) { + set_state (RUNNING); + } +}