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"
32 #include "safe_stringstream.h"
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) {
71 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
74 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
75 if (s.available < pow (1024, 3)) {
77 m += _("The drive that the film is stored on is low in disc space. Free some more space and try again.");
83 set_error (e.what(), m);
85 set_state (FINISHED_ERROR);
87 } catch (OpenFileError& e) {
90 String::compose (_("Could not open %1"), e.file().string()),
91 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())
95 set_state (FINISHED_ERROR);
97 } catch (boost::thread_interrupted &) {
99 set_state (FINISHED_CANCELLED);
101 } catch (std::bad_alloc& e) {
103 set_error (_("Out of memory"), _("There was not enough memory to do this."));
105 set_state (FINISHED_ERROR);
107 } catch (std::exception& e) {
111 _("It is not known what caused this error. Please report the problem to the DCP-o-matic author (carl@dcpomatic.com).")
115 set_state (FINISHED_ERROR);
121 _("It is not known what caused this error. Please report the problem to the DCP-o-matic author (carl@dcpomatic.com).")
125 set_state (FINISHED_ERROR);
129 /** @return true if this job is new (ie has not started running) */
133 boost::mutex::scoped_lock lm (_state_mutex);
134 return _state == NEW;
137 /** @return true if the job is running */
139 Job::running () const
141 boost::mutex::scoped_lock lm (_state_mutex);
142 return _state == RUNNING;
145 /** @return true if the job has finished (either successfully or unsuccessfully) */
147 Job::finished () const
149 boost::mutex::scoped_lock lm (_state_mutex);
150 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
153 /** @return true if the job has finished successfully */
155 Job::finished_ok () const
157 boost::mutex::scoped_lock lm (_state_mutex);
158 return _state == FINISHED_OK;
161 /** @return true if the job has finished unsuccessfully */
163 Job::finished_in_error () const
165 boost::mutex::scoped_lock lm (_state_mutex);
166 return _state == FINISHED_ERROR;
170 Job::finished_cancelled () const
172 boost::mutex::scoped_lock lm (_state_mutex);
173 return _state == FINISHED_CANCELLED;
179 boost::mutex::scoped_lock lm (_state_mutex);
180 return _state == PAUSED;
183 /** Set the state of this job.
184 * @param s New state.
187 Job::set_state (State s)
189 bool finished = false;
192 boost::mutex::scoped_lock lm (_state_mutex);
195 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
196 _ran_for = elapsed_time ();
202 if (finished && ui_signaller) {
203 ui_signaller->emit (boost::bind (boost::ref (Finished)));
207 /** @return Time (in seconds) that this sub-job has been running */
209 Job::elapsed_time () const
211 if (_start_time == 0) {
215 return time (0) - _start_time;
218 /** Set the progress of the current part of the job.
219 * @param p Progress (from 0 to 1)
222 Job::set_progress (float p, bool force)
224 if (!force && fabs (p - progress()) < 0.01) {
225 /* Calm excessive progress reporting */
229 boost::mutex::scoped_lock lm (_progress_mutex);
231 boost::this_thread::interruption_point ();
233 boost::mutex::scoped_lock lm2 (_state_mutex);
234 while (_state == PAUSED) {
235 _pause_changed.wait (lm2);
239 ui_signaller->emit (boost::bind (boost::ref (Progress)));
243 /** @return fractional progress of the current sub-job, or -1 if not known */
245 Job::progress () const
247 boost::mutex::scoped_lock lm (_progress_mutex);
248 return _progress.get_value_or (-1);
255 boost::mutex::scoped_lock lm (_progress_mutex);
259 set_progress (0, true);
263 Job::error_details () const
265 boost::mutex::scoped_lock lm (_state_mutex);
266 return _error_details;
269 /** @return A summary of any error that the job has generated */
271 Job::error_summary () const
273 boost::mutex::scoped_lock lm (_state_mutex);
274 return _error_summary;
277 /** Set the current error string.
278 * @param e New error string.
281 Job::set_error (string s, string d)
283 boost::mutex::scoped_lock lm (_state_mutex);
288 /** Say that this job's progress will be unknown until further notice */
290 Job::set_progress_unknown ()
292 boost::mutex::scoped_lock lm (_progress_mutex);
296 /** @return Human-readable status of this job */
300 float const p = progress ();
301 int const t = elapsed_time ();
302 int const r = remaining_time ();
304 int pc = rint (p * 100);
306 /* 100% makes it sound like we've finished when we haven't */
313 if (p >= 0 && t > 10 && r > 0) {
314 /// TRANSLATORS: remaining here follows an amount of time that is remaining
316 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
318 } else if (finished_ok ()) {
319 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
320 } else if (finished_in_error ()) {
321 s << String::compose (_("Error (%1)"), error_summary());
322 } else if (finished_cancelled ()) {
329 /** @return An estimate of the remaining time for this sub-job, in seconds */
331 Job::remaining_time () const
333 return elapsed_time() / progress() - elapsed_time();
343 _thread->interrupt ();
352 _pause_changed.notify_all ();
361 _pause_changed.notify_all ();