Increase maximum allowable KDM file size.
[dcpomatic.git] / src / lib / job.cc
index b316cddf7fa2740af8ff0de774d5a425af66107d..1676928632164410582e179c97d5e3c5e7842c33 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
 #include "log.h"
 #include "compose.hpp"
 #include <dcp/exceptions.h>
+#include <sub/exceptions.h>
 #include <boost/thread.hpp>
 #include <boost/filesystem.hpp>
+#include <boost/foreach.hpp>
 #include <iostream>
 
 #include "i18n.h"
@@ -43,8 +45,8 @@ using boost::shared_ptr;
 using boost::optional;
 using boost::function;
 
-#define LOG_ERROR_NC(...) _film->log()->log (__VA_ARGS__, LogEntry::TYPE_ERROR);
-#define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
+#define LOG_ERROR_NC(...) if (_film) { _film->log()->log (__VA_ARGS__, LogEntry::TYPE_ERROR); }
+#define LOG_GENERAL(...) if (_film) { _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL); }
 
 /** @param film Associated film, or 0 */
 Job::Job (shared_ptr<const Film> film)
@@ -149,6 +151,17 @@ Job::run_wrapper ()
 
                set_state (FINISHED_CANCELLED);
 
+       } catch (sub::SubripError& e) {
+
+               string extra = "Error is near:\n";
+               BOOST_FOREACH (string i, e.context()) {
+                       extra += i + "\n";
+               }
+
+               set_error (e.what (), extra);
+               set_progress (1);
+               set_state (FINISHED_ERROR);
+
        } catch (std::bad_alloc& e) {
 
                set_error (_("Out of memory"), _("There was not enough memory to do this.  If you are running a 32-bit operating system try reducing the number of encoding threads in the General tab of Preferences."));
@@ -266,12 +279,27 @@ Job::elapsed_sub_time () const
        return time (0) - _sub_start_time;
 }
 
+/** Check to see if this job has been interrupted or paused */
+void
+Job::check_for_interruption_or_pause ()
+{
+       boost::this_thread::interruption_point ();
+
+       boost::mutex::scoped_lock lm (_state_mutex);
+       while (_state == PAUSED) {
+               emit (boost::bind (boost::ref (Progress)));
+               _pause_changed.wait (lm);
+       }
+}
+
 /** Set the progress of the current part of the job.
  *  @param p Progress (from 0 to 1)
  */
 void
 Job::set_progress (float p, bool force)
 {
+       check_for_interruption_or_pause ();
+
        if (!force) {
                /* Check for excessively frequent progress reporting */
                boost::mutex::scoped_lock lm (_progress_mutex);
@@ -293,18 +321,11 @@ Job::set_progress (float p, bool force)
 void
 Job::set_progress_common (optional<float> p)
 {
-       boost::mutex::scoped_lock lm (_progress_mutex);
-       _progress = p;
-       boost::this_thread::interruption_point ();
-
-       boost::mutex::scoped_lock lm2 (_state_mutex);
-       while (_state == PAUSED) {
-               _pause_changed.wait (lm2);
+       {
+               boost::mutex::scoped_lock lm (_progress_mutex);
+               _progress = p;
        }
 
-       lm.unlock ();
-       lm2.unlock ();
-
        emit (boost::bind (boost::ref (Progress)));
 }
 
@@ -365,6 +386,7 @@ Job::set_error (string s, string d)
 void
 Job::set_progress_unknown ()
 {
+       check_for_interruption_or_pause ();
        set_progress_common (optional<float> ());
 }
 
@@ -376,7 +398,7 @@ Job::status () const
        int const t = elapsed_sub_time ();
        int const r = remaining_time ();
 
-       SafeStringStream s;
+       string s;
        if (!finished () && p) {
                int pc = lrintf (p.get() * 100);
                if (pc == 100) {
@@ -384,22 +406,24 @@ Job::status () const
                        pc = 99;
                }
 
-               s << pc << N_("%");
+               char buffer[64];
+               snprintf (buffer, sizeof(buffer), "%d%%", pc);
+               s += buffer;
 
                if (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");
+                       s += "; " + seconds_to_approximate_hms (r) + " " + _("remaining");
                }
        } else if (finished_ok ()) {
-               s << String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
+               s = String::compose (_("OK (ran for %1)"), seconds_to_hms (_ran_for));
        } else if (finished_in_error ()) {
-               s << String::compose (_("Error: %1"), error_summary ());
+               s = String::compose (_("Error: %1"), error_summary ());
        } else if (finished_cancelled ()) {
-               s << _("Cancelled");
+               s = _("Cancelled");
        }
 
-       return s.str ();
+       return s;
 }
 
 string