2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * @brief A parent class to represent long-running tasks which are run in their own thread.
24 #include <boost/thread.hpp>
25 #include <boost/filesystem.hpp>
26 #include <libdcp/exceptions.h>
30 #include "ui_signaller.h"
31 #include "exceptions.h"
38 using std::stringstream;
39 using boost::shared_ptr;
41 Job::Job (shared_ptr<const Film> f)
52 /** Start the job in a separate thread, returning immediately */
57 _start_time = time (0);
58 _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
61 /** A wrapper for the ::run() method to catch exceptions */
69 } catch (libdcp::FileError& e) {
72 set_state (FINISHED_ERROR);
74 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
77 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
78 if (s.available < pow (1024, 3)) {
80 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
86 set_error (e.what(), m);
88 } catch (OpenFileError& e) {
91 set_state (FINISHED_ERROR);
94 String::compose (_("Could not open %1"), e.file().string()),
95 String::compose (_("DCP-o-matic could not open the file %1. Perhaps it does not exist or is in an unexpected format."), e.file().string())
98 } catch (boost::thread_interrupted &) {
100 set_state (FINISHED_CANCELLED);
102 } catch (std::exception& e) {
105 set_state (FINISHED_ERROR);
108 _("It is not known what caused this error. The best idea is to report the problem to the DCP-o-matic mailing list (carl@dcpomatic.com)")
114 set_state (FINISHED_ERROR);
117 _("It is not known what caused this error. The best idea is to report the problem to the DCP-o-matic mailing list (carl@dcpomatic.com)")
123 /** @return true if this job is new (ie has not started running) */
127 boost::mutex::scoped_lock lm (_state_mutex);
128 return _state == NEW;
131 /** @return true if the job is running */
133 Job::running () const
135 boost::mutex::scoped_lock lm (_state_mutex);
136 return _state == RUNNING;
139 /** @return true if the job has finished (either successfully or unsuccessfully) */
141 Job::finished () const
143 boost::mutex::scoped_lock lm (_state_mutex);
144 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
147 /** @return true if the job has finished successfully */
149 Job::finished_ok () const
151 boost::mutex::scoped_lock lm (_state_mutex);
152 return _state == FINISHED_OK;
155 /** @return true if the job has finished unsuccessfully */
157 Job::finished_in_error () const
159 boost::mutex::scoped_lock lm (_state_mutex);
160 return _state == FINISHED_ERROR;
164 Job::finished_cancelled () const
166 boost::mutex::scoped_lock lm (_state_mutex);
167 return _state == FINISHED_CANCELLED;
173 boost::mutex::scoped_lock lm (_state_mutex);
174 return _state == PAUSED;
177 /** Set the state of this job.
178 * @param s New state.
181 Job::set_state (State s)
183 bool finished = false;
186 boost::mutex::scoped_lock lm (_state_mutex);
189 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
190 _ran_for = elapsed_time ();
196 if (finished && ui_signaller) {
197 ui_signaller->emit (boost::bind (boost::ref (Finished)));
201 /** @return Time (in seconds) that this sub-job has been running */
203 Job::elapsed_time () const
205 if (_start_time == 0) {
209 return time (0) - _start_time;
212 /** Set the progress of the current part of the job.
213 * @param p Progress (from 0 to 1)
216 Job::set_progress (float p, bool force)
218 if (!force && fabs (p - progress()) < 0.01) {
219 /* Calm excessive progress reporting */
223 boost::mutex::scoped_lock lm (_progress_mutex);
225 boost::this_thread::interruption_point ();
232 ui_signaller->emit (boost::bind (boost::ref (Progress)));
236 /** @return fractional progress of this sub-job, or -1 if not known */
238 Job::progress () const
240 boost::mutex::scoped_lock lm (_progress_mutex);
241 return _progress.get_value_or (-1);
248 boost::mutex::scoped_lock lm (_progress_mutex);
252 set_progress (0, true);
256 Job::error_details () const
258 boost::mutex::scoped_lock lm (_state_mutex);
259 return _error_details;
262 /** @return A summary of any error that the job has generated */
264 Job::error_summary () const
266 boost::mutex::scoped_lock lm (_state_mutex);
267 return _error_summary;
270 /** Set the current error string.
271 * @param e New error string.
274 Job::set_error (string s, string d)
276 boost::mutex::scoped_lock lm (_state_mutex);
281 /** Say that this job's progress will be unknown until further notice */
283 Job::set_progress_unknown ()
285 boost::mutex::scoped_lock lm (_progress_mutex);
289 /** @return Human-readable status of this job */
293 float const p = progress ();
294 int const t = elapsed_time ();
295 int const r = remaining_time ();
297 int pc = rint (p * 100);
299 /* 100% makes it sound like we've finished when we haven't */
306 if (p >= 0 && t > 10 && r > 0) {
307 /// TRANSLATORS: remaining here follows an amount of time that is remaining
309 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
311 } else if (finished_ok ()) {
312 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
313 } else if (finished_in_error ()) {
314 s << String::compose (_("Error (%1)"), error_summary());
315 } else if (finished_cancelled ()) {
322 /** @return An estimate of the remaining time for this sub-job, in seconds */
324 Job::remaining_time () const
326 return elapsed_time() / progress() - elapsed_time();
336 _thread->interrupt ();