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"
36 using std::stringstream;
37 using boost::shared_ptr;
39 /** @param s Film that we are operating on.
41 Job::Job (shared_ptr<Film> f)
46 , _progress_unknown (false)
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 (boost::thread_interrupted &) {
90 set_state (FINISHED_CANCELLED);
92 } catch (std::exception& e) {
95 set_state (FINISHED_ERROR);
98 _("It is not known what caused this error. The best idea is to report the problem to the DVD-o-matic mailing list (dvdomatic@carlh.net)")
104 set_state (FINISHED_ERROR);
107 _("It is not known what caused this error. The best idea is to report the problem to the DVD-o-matic mailing list (dvdomatic@carlh.net)")
113 /** @return true if this job is new (ie has not started running) */
117 boost::mutex::scoped_lock lm (_state_mutex);
118 return _state == NEW;
121 /** @return true if the job is running */
123 Job::running () const
125 boost::mutex::scoped_lock lm (_state_mutex);
126 return _state == RUNNING;
129 /** @return true if the job has finished (either successfully or unsuccessfully) */
131 Job::finished () const
133 boost::mutex::scoped_lock lm (_state_mutex);
134 return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
137 /** @return true if the job has finished successfully */
139 Job::finished_ok () const
141 boost::mutex::scoped_lock lm (_state_mutex);
142 return _state == FINISHED_OK;
145 /** @return true if the job has finished unsuccessfully */
147 Job::finished_in_error () const
149 boost::mutex::scoped_lock lm (_state_mutex);
150 return _state == FINISHED_ERROR;
154 Job::finished_cancelled () const
156 boost::mutex::scoped_lock lm (_state_mutex);
157 return _state == FINISHED_CANCELLED;
163 boost::mutex::scoped_lock lm (_state_mutex);
164 return _state == PAUSED;
167 /** Set the state of this job.
168 * @param s New state.
171 Job::set_state (State s)
173 boost::mutex::scoped_lock lm (_state_mutex);
176 if (_state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED) {
177 _ran_for = elapsed_time ();
179 ui_signaller->emit (boost::bind (boost::ref (Finished)));
184 /** @return Time (in seconds) that this job has been running */
186 Job::elapsed_time () const
188 if (_start_time == 0) {
192 return time (0) - _start_time;
195 /** Set the progress of the current part of the job.
196 * @param p Progress (from 0 to 1)
199 Job::set_progress (float p)
201 boost::mutex::scoped_lock lm (_progress_mutex);
202 _progress_unknown = false;
203 _stack.back().normalised = p;
204 boost::this_thread::interruption_point ();
211 /** @return fractional overall progress, or -1 if not known */
213 Job::overall_progress () const
215 boost::mutex::scoped_lock lm (_progress_mutex);
216 if (_progress_unknown) {
222 for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
223 factor *= i->allocation;
224 overall += i->normalised * factor;
234 /** Ascend up one level in terms of progress reporting; see descend() */
238 boost::mutex::scoped_lock lm (_progress_mutex);
240 assert (!_stack.empty ());
241 float const a = _stack.back().allocation;
243 _stack.back().normalised += a;
246 /** Descend down one level in terms of progress reporting; e.g. if
247 * there is a task which is split up into N subtasks, each of which
248 * report their progress from 0 to 100%, call descend() before executing
249 * each subtask, and ascend() afterwards to ensure that overall progress
250 * is reported correctly.
252 * @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
255 Job::descend (float a)
257 boost::mutex::scoped_lock lm (_progress_mutex);
258 _stack.push_back (Level (a));
262 Job::error_details () const
264 boost::mutex::scoped_lock lm (_state_mutex);
265 return _error_details;
268 /** @return A summary of any error that the job has generated */
270 Job::error_summary () const
272 boost::mutex::scoped_lock lm (_state_mutex);
273 return _error_summary;
276 /** Set the current error string.
277 * @param e New error string.
280 Job::set_error (string s, string d)
282 boost::mutex::scoped_lock lm (_state_mutex);
287 /** Say that this job's progress will be unknown until further notice */
289 Job::set_progress_unknown ()
291 boost::mutex::scoped_lock lm (_progress_mutex);
292 _progress_unknown = true;
295 /** @return Human-readable status of this job */
299 float const p = overall_progress ();
300 int const t = elapsed_time ();
301 int const r = remaining_time ();
303 int pc = rint (p * 100);
305 /* 100% makes it sound like we've finished when we haven't */
312 if (p >= 0 && t > 10 && r > 0) {
313 /// TRANSLATORS: remaining here follows an amount of time that is remaining
315 s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
317 } else if (finished_ok ()) {
318 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
319 } else if (finished_in_error ()) {
320 s << String::compose (_("Error (%1)"), error_summary());
321 } else if (finished_cancelled ()) {
328 /** @return An estimate of the remaining time for this job, in seconds */
330 Job::remaining_time () const
332 return elapsed_time() / overall_progress() - elapsed_time();
342 _thread->interrupt ();