Merge .po files.
[dcpomatic.git] / src / lib / job.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file src/job.cc
21  *  @brief A parent class to represent long-running tasks which are run in their own thread.
22  */
23
24 #include <boost/thread.hpp>
25 #include <boost/filesystem.hpp>
26 #include <libdcp/exceptions.h>
27 #include "job.h"
28 #include "util.h"
29
30 #include "i18n.h"
31
32 using std::string;
33 using std::list;
34 using std::stringstream;
35 using boost::shared_ptr;
36
37 /** @param s Film that we are operating on.
38  */
39 Job::Job (shared_ptr<Film> f)
40         : _film (f)
41         , _thread (0)
42         , _state (NEW)
43         , _start_time (0)
44         , _progress_unknown (false)
45         , _ran_for (0)
46 {
47         descend (1);
48 }
49
50 /** Start the job in a separate thread, returning immediately */
51 void
52 Job::start ()
53 {
54         set_state (RUNNING);
55         _start_time = time (0);
56         _thread = new boost::thread (boost::bind (&Job::run_wrapper, this));
57 }
58
59 /** A wrapper for the ::run() method to catch exceptions */
60 void
61 Job::run_wrapper ()
62 {
63         try {
64
65                 run ();
66
67         } catch (libdcp::FileError& e) {
68                 
69                 set_progress (1);
70                 set_state (FINISHED_ERROR);
71                 
72                 string m = String::compose (_("An error occurred whilst handling the file %1."), boost::filesystem::path (e.filename()).leaf());
73                 
74                 boost::filesystem::space_info const s = boost::filesystem::space (e.filename());
75                 if (s.available < pow (1024, 3)) {
76                         m += N_("\n\n");
77                         m += _("The drive that the film is stored on is low in disc space.  Free some more space and try again.");
78                 }
79
80                 set_error (e.what(), m);
81
82         } catch (boost::thread_interrupted &) {
83
84                 set_state (FINISHED_CANCELLED);
85                 
86         } catch (std::exception& e) {
87
88                 set_progress (1);
89                 set_state (FINISHED_ERROR);
90                 set_error (
91                         e.what (),
92                         _("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)")
93                         );
94
95         } catch (...) {
96
97                 set_progress (1);
98                 set_state (FINISHED_ERROR);
99                 set_error (
100                         _("Unknown error"),
101                         _("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)")
102                         );
103
104         }
105 }
106
107 /** @return true if this job is new (ie has not started running) */
108 bool
109 Job::is_new () const
110 {
111         boost::mutex::scoped_lock lm (_state_mutex);
112         return _state == NEW;
113 }
114
115 /** @return true if the job is running */
116 bool
117 Job::running () const
118 {
119         boost::mutex::scoped_lock lm (_state_mutex);
120         return _state == RUNNING;
121 }
122
123 /** @return true if the job has finished (either successfully or unsuccessfully) */
124 bool
125 Job::finished () const
126 {
127         boost::mutex::scoped_lock lm (_state_mutex);
128         return _state == FINISHED_OK || _state == FINISHED_ERROR || _state == FINISHED_CANCELLED;
129 }
130
131 /** @return true if the job has finished successfully */
132 bool
133 Job::finished_ok () const
134 {
135         boost::mutex::scoped_lock lm (_state_mutex);
136         return _state == FINISHED_OK;
137 }
138
139 /** @return true if the job has finished unsuccessfully */
140 bool
141 Job::finished_in_error () const
142 {
143         boost::mutex::scoped_lock lm (_state_mutex);
144         return _state == FINISHED_ERROR;
145 }
146
147 bool
148 Job::finished_cancelled () const
149 {
150         boost::mutex::scoped_lock lm (_state_mutex);
151         return _state == FINISHED_CANCELLED;
152 }
153
154 /** Set the state of this job.
155  *  @param s New state.
156  */
157 void
158 Job::set_state (State s)
159 {
160         boost::mutex::scoped_lock lm (_state_mutex);
161         _state = s;
162
163         if (_state == FINISHED_OK || _state == FINISHED_ERROR) {
164                 _ran_for = elapsed_time ();
165         }
166 }
167
168 /** @return Time (in seconds) that this job has been running */
169 int
170 Job::elapsed_time () const
171 {
172         if (_start_time == 0) {
173                 return 0;
174         }
175         
176         return time (0) - _start_time;
177 }
178
179 /** Set the progress of the current part of the job.
180  *  @param p Progress (from 0 to 1)
181  */
182 void
183 Job::set_progress (float p)
184 {
185         boost::mutex::scoped_lock lm (_progress_mutex);
186         _progress_unknown = false;
187         _stack.back().normalised = p;
188         boost::this_thread::interruption_point ();
189 }
190
191 /** @return fractional overall progress, or -1 if not known */
192 float
193 Job::overall_progress () const
194 {
195         boost::mutex::scoped_lock lm (_progress_mutex);
196         if (_progress_unknown) {
197                 return -1;
198         }
199
200         float overall = 0;
201         float factor = 1;
202         for (list<Level>::const_iterator i = _stack.begin(); i != _stack.end(); ++i) {
203                 factor *= i->allocation;
204                 overall += i->normalised * factor;
205         }
206
207         if (overall > 1) {
208                 overall = 1;
209         }
210         
211         return overall;
212 }
213
214 /** Ascend up one level in terms of progress reporting; see descend() */
215 void
216 Job::ascend ()
217 {
218         boost::mutex::scoped_lock lm (_progress_mutex);
219         
220         assert (!_stack.empty ());
221         float const a = _stack.back().allocation;
222         _stack.pop_back ();
223         _stack.back().normalised += a;
224 }
225
226 /** Descend down one level in terms of progress reporting; e.g. if
227  *  there is a task which is split up into N subtasks, each of which
228  *  report their progress from 0 to 100%, call descend() before executing
229  *  each subtask, and ascend() afterwards to ensure that overall progress
230  *  is reported correctly.
231  *
232  *  @param a Fraction (from 0 to 1) of the current task to allocate to the subtask.
233  */
234 void
235 Job::descend (float a)
236 {
237         boost::mutex::scoped_lock lm (_progress_mutex);
238         _stack.push_back (Level (a));
239 }
240
241 string
242 Job::error_details () const
243 {
244         boost::mutex::scoped_lock lm (_state_mutex);
245         return _error_details;
246 }
247
248 /** @return A summary of any error that the job has generated */
249 string
250 Job::error_summary () const
251 {
252         boost::mutex::scoped_lock lm (_state_mutex);
253         return _error_summary;
254 }
255
256 /** Set the current error string.
257  *  @param e New error string.
258  */
259 void
260 Job::set_error (string s, string d)
261 {
262         boost::mutex::scoped_lock lm (_state_mutex);
263         _error_summary = s;
264         _error_details = d;
265 }
266
267 /** Say that this job's progress will be unknown until further notice */
268 void
269 Job::set_progress_unknown ()
270 {
271         boost::mutex::scoped_lock lm (_progress_mutex);
272         _progress_unknown = true;
273 }
274
275 /** @return Human-readable status of this job */
276 string
277 Job::status () const
278 {
279         float const p = overall_progress ();
280         int const t = elapsed_time ();
281         int const r = remaining_time ();
282
283         int pc = rint (p * 100);
284         if (pc == 100) {
285                 /* 100% makes it sound like we've finished when we haven't */
286                 pc = 99;
287         }
288
289         stringstream s;
290         if (!finished ()) {
291                 s << pc << N_("%");
292                 if (p >= 0 && t > 10 && r > 0) {
293                         /// TRANSLATORS: remaining here follows an amount of time that is remaining
294                         /// on an operation.
295                         s << "; " << seconds_to_approximate_hms (r) << " " << _("remaining");
296                 }
297         } else if (finished_ok ()) {
298                 s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
299         } else if (finished_in_error ()) {
300                 s << String::compose (_("Error (%1)"), error_summary());
301         } else if (finished_cancelled ()) {
302                 s << _("Cancelled");
303         }
304
305         return s.str ();
306 }
307
308 /** @return An estimate of the remaining time for this job, in seconds */
309 int
310 Job::remaining_time () const
311 {
312         return elapsed_time() / overall_progress() - elapsed_time();
313 }
314
315 void
316 Job::cancel ()
317 {
318         if (!_thread) {
319                 return;
320         }
321
322         _thread->interrupt ();
323         _thread->join ();
324 }