Hand-apply 2ee558ec90623b570360e8e55c918b5bb37f9aac from master; various snapping...
[dcpomatic.git] / src / lib / config.cc
index a15825dc7e0f56fb3b511afe37d43d8d1ca3e0e8..ab57c1afcc89aea6674b929b0727d13a2b9ad7b7 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -19,7 +19,6 @@
 
 #include "config.h"
 #include "server.h"
-#include "scaler.h"
 #include "filter.h"
 #include "ratio.h"
 #include "dcp_content_type.h"
@@ -28,8 +27,8 @@
 #include "cinema.h"
 #include "util.h"
 #include "cross.h"
+#include "raw_convert.h"
 #include <dcp/colour_matrix.h>
-#include <dcp/raw_convert.h>
 #include <dcp/signer.h>
 #include <dcp/certificate_chain.h>
 #include <libcxml/cxml.h>
@@ -52,33 +51,39 @@ using std::exception;
 using std::cerr;
 using boost::shared_ptr;
 using boost::optional;
-using boost::algorithm::is_any_of;
-using boost::algorithm::split;
-using dcp::raw_convert;
+using boost::algorithm::trim;
 
 Config* Config::_instance = 0;
 
 /** Construct default configuration */
 Config::Config ()
-       : _num_local_encoding_threads (max (2U, boost::thread::hardware_concurrency()))
-       , _server_port_base (6192)
-       , _use_any_servers (true)
-       , _tms_path (".")
-       , _cinema_sound_processor (CinemaSoundProcessor::from_id (N_("dolby_cp750")))
-       , _allow_any_dcp_frame_rate (false)
-       , _default_still_length (10)
-       , _default_container (Ratio::from_id ("185"))
-       , _default_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"))
-       , _default_j2k_bandwidth (100000000)
-       , _default_audio_delay (0)
-       , _check_for_updates (false)
-       , _check_for_test_updates (false)
-       , _maximum_j2k_bandwidth (250000000)
-       , _log_types (Log::TYPE_GENERAL | Log::TYPE_WARNING | Log::TYPE_ERROR)
+{
+       set_defaults ();
+}
+
+void
+Config::set_defaults ()
+{
+       _num_local_encoding_threads = max (2U, boost::thread::hardware_concurrency ());
+       _server_port_base = 6192;
+       _use_any_servers = true;
+       _tms_path = ".";
+       _cinema_sound_processor = CinemaSoundProcessor::from_id (N_("dolby_cp750"));
+       _allow_any_dcp_frame_rate = false;
+       _default_still_length = 10;
+       _default_container = Ratio::from_id ("185");
+       _default_dcp_content_type = DCPContentType::from_isdcf_name ("FTR");
+       _default_j2k_bandwidth = 100000000;
+       _default_audio_delay = 0;
+       _check_for_updates = false;
+       _check_for_test_updates = false;
+       _maximum_j2k_bandwidth = 250000000;
+       _log_types = Log::TYPE_GENERAL | Log::TYPE_WARNING | Log::TYPE_ERROR | Log::TYPE_DEBUG;
 #ifdef DCPOMATIC_WINDOWS         
-       , _win32_console (false)
+       _win32_console = false;
 #endif   
-{
+
+       _allowed_dcp_frame_rates.clear ();
        _allowed_dcp_frame_rates.push_back (24);
        _allowed_dcp_frame_rates.push_back (25);
        _allowed_dcp_frame_rates.push_back (30);
@@ -86,11 +91,20 @@ Config::Config ()
        _allowed_dcp_frame_rates.push_back (50);
        _allowed_dcp_frame_rates.push_back (60);
 
-       _colour_conversions.push_back (PresetColourConversion (_("sRGB"), 2.4, true, dcp::colour_matrix::srgb_to_xyz, 2.6));
-       _colour_conversions.push_back (PresetColourConversion (_("sRGB non-linearised"), 2.4, false, dcp::colour_matrix::srgb_to_xyz, 2.6));
-       _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), 2.2, false, dcp::colour_matrix::rec709_to_xyz, 2.6));
+       _colour_conversions.clear ();
+       _colour_conversions.push_back (PresetColourConversion (_("sRGB"), dcp::ColourConversion::srgb_to_xyz ()));
+       _colour_conversions.push_back (PresetColourConversion (_("Rec. 601"), dcp::ColourConversion::rec601_to_xyz ()));
+       _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), dcp::ColourConversion::rec709_to_xyz ()));
+       _colour_conversions.push_back (PresetColourConversion (_("P3 (from SMPTE RP 431-2)"), dcp::ColourConversion::p3_to_xyz ()));
 
-       reset_kdm_email ();
+       set_kdm_email_to_default ();
+}
+
+void
+Config::restore_defaults ()
+{
+       Config::instance()->set_defaults ();
+       Config::instance()->changed ();
 }
 
 void
@@ -178,16 +192,18 @@ Config::read ()
        if (!cc.empty ()) {
                _colour_conversions.clear ();
        }
-       
-       for (list<cxml::NodePtr>::iterator i = cc.begin(); i != cc.end(); ++i) {
-               _colour_conversions.push_back (PresetColourConversion (*i));
-       }
 
-       if (!version) {
-               /* Loading version 0 (before Rec. 709 was added as a preset).
-                  Add it in.
-               */
-               _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), 2.2, false, dcp::colour_matrix::rec709_to_xyz, 2.6));
+       try {
+               for (list<cxml::NodePtr>::iterator i = cc.begin(); i != cc.end(); ++i) {
+                       /* This is a bit of a hack; use 32 (the first Film state file version for the 2.x branch)
+                          for version 2 and 10 (the current Film state version for the 1.x branch) for version 1.
+                       */
+                       _colour_conversions.push_back (PresetColourConversion (*i, version == 2 ? 32 : 10));
+               }
+       } catch (cxml::Error) {
+               /* Probably failed to load an old-style ColourConversion tag; just give up */
+               _colour_conversions.push_back (PresetColourConversion (_("sRGB"), dcp::ColourConversion::srgb_to_xyz ()));
+               _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), dcp::ColourConversion::rec709_to_xyz ()));
        }
 
        list<cxml::NodePtr> cin = f.node_children ("Cinema");
@@ -379,8 +395,14 @@ Config::write () const
        for (vector<boost::filesystem::path>::const_iterator i = _history.begin(); i != _history.end(); ++i) {
                root->add_child("History")->add_child_text (i->string ());
        }
-       
-       doc.write_to_file_formatted (file().string ());
+
+       try {
+               doc.write_to_file_formatted (file().string ());
+       } catch (xmlpp::exception& e) {
+               string s = e.what ();
+               trim (s);
+               throw FileError (s, file ());
+       }
 }
 
 boost::filesystem::path
@@ -414,7 +436,7 @@ Config::changed ()
 }
 
 void
-Config::reset_kdm_email ()
+Config::set_kdm_email_to_default ()
 {
        _kdm_email = _(
                "Dear Projectionist\n\n"
@@ -426,6 +448,13 @@ Config::reset_kdm_email ()
                );
 }
 
+void
+Config::reset_kdm_email ()
+{
+       set_kdm_email_to_default ();
+       changed ();
+}
+
 void
 Config::add_to_history (boost::filesystem::path p)
 {