Merge master.
[dcpomatic.git] / src / lib / config.cc
index 0e2e33a16deaabf75db58318fc187561f6e9078a..ca8d0bc53c407182c8b39dc0770648574f1d9e51 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2014 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
 #include <fstream>
 #include <glib.h>
 #include <boost/filesystem.hpp>
+#include <boost/algorithm/string.hpp>
+#include <dcp/colour_matrix.h>
+#include <libcxml/cxml.h>
 #include "config.h"
 #include "server.h"
 #include "scaler.h"
 #include "filter.h"
-#include "format.h"
+#include "ratio.h"
 #include "dcp_content_type.h"
 #include "sound_processor.h"
+#include "colour_conversion.h"
+#include "cinema.h"
+#include "util.h"
 
 #include "i18n.h"
 
 using std::vector;
 using std::ifstream;
 using std::string;
-using std::ofstream;
+using std::list;
+using std::max;
+using std::exception;
+using std::cerr;
 using boost::shared_ptr;
+using boost::lexical_cast;
+using boost::optional;
+using boost::algorithm::is_any_of;
+using boost::algorithm::split;
 
 Config* Config::_instance = 0;
 
 /** Construct default configuration */
 Config::Config ()
-       : _num_local_encoding_threads (2)
-       , _server_port (6192)
-       , _reference_scaler (Scaler::from_id (N_("bicubic")))
-       , _tms_path (N_("."))
+       : _num_local_encoding_threads (max (2U, boost::thread::hardware_concurrency()))
+       , _server_port_base (6192)
+       , _use_any_servers (true)
+       , _tms_path (".")
        , _sound_processor (SoundProcessor::from_id (N_("dolby_cp750")))
-       , _default_format (0)
-       , _default_dcp_content_type (0)
+       , _default_still_length (10)
+       , _default_container (Ratio::from_id ("185"))
+       , _default_dcp_content_type (DCPContentType::from_dci_name ("TST"))
+       , _default_j2k_bandwidth (100000000)
+       , _default_audio_delay (0)
+       , _kdm_email (
+               _("Dear Projectionist\n\nPlease find attached KDMs for $CPL_NAME.\n\nThe KDMs are valid from $START_TIME until $END_TIME.\n\nBest regards,\nDCP-o-matic")
+               )
+       , _check_for_updates (false)
+       , _check_for_test_updates (false)
+       , _maximum_j2k_bandwidth (250000000)
 {
        _allowed_dcp_frame_rates.push_back (24);
        _allowed_dcp_frame_rates.push_back (25);
@@ -56,9 +78,125 @@ Config::Config ()
        _allowed_dcp_frame_rates.push_back (48);
        _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));
+}
+
+void
+Config::read ()
+{
+       LocaleGuard lg;
+       
+       if (!boost::filesystem::exists (file (false))) {
+               read_old_metadata ();
+               return;
+       }
+
+       cxml::Document f ("Config");
+       f.read_file (file (false));
+       optional<string> c;
+
+       optional<int> version = f.optional_number_child<int> ("Version");
+
+       _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
+       _default_directory = f.string_child ("DefaultDirectory");
+
+       boost::optional<int> b = f.optional_number_child<int> ("ServerPort");
+       if (!b) {
+               b = f.optional_number_child<int> ("ServerPortBase");
+       }
+       _server_port_base = b.get ();
+
+       boost::optional<bool> u = f.optional_bool_child ("UseAnyServers");
+       _use_any_servers = u.get_value_or (true);
+
+       list<cxml::NodePtr> servers = f.node_children ("Server");
+       for (list<cxml::NodePtr>::iterator i = servers.begin(); i != servers.end(); ++i) {
+               if ((*i)->node_children("HostName").size() == 1) {
+                       _servers.push_back ((*i)->string_child ("HostName"));
+               } else {
+                       _servers.push_back ((*i)->content ());
+               }
+       }
        
-       ifstream f (file().c_str ());
+       _tms_ip = f.string_child ("TMSIP");
+       _tms_path = f.string_child ("TMSPath");
+       _tms_user = f.string_child ("TMSUser");
+       _tms_password = f.string_child ("TMSPassword");
+
+       c = f.optional_string_child ("SoundProcessor");
+       if (c) {
+               _sound_processor = SoundProcessor::from_id (c.get ());
+       }
+
+       _language = f.optional_string_child ("Language");
+
+       c = f.optional_string_child ("DefaultContainer");
+       if (c) {
+               _default_container = Ratio::from_id (c.get ());
+       }
+
+       c = f.optional_string_child ("DefaultDCPContentType");
+       if (c) {
+               _default_dcp_content_type = DCPContentType::from_dci_name (c.get ());
+       }
+
+       _dcp_metadata.issuer = f.optional_string_child ("DCPMetadataIssuer").get_value_or ("");
+       _dcp_metadata.creator = f.optional_string_child ("DCPMetadataCreator").get_value_or ("");
+
+       _default_dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
+       _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
+       _default_j2k_bandwidth = f.optional_number_child<int>("DefaultJ2KBandwidth").get_value_or (200000000);
+       _default_audio_delay = f.optional_number_child<int>("DefaultAudioDelay").get_value_or (0);
+
+       list<cxml::NodePtr> cc = f.node_children ("ColourConversion");
+
+       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));
+       }
+
+       list<cxml::NodePtr> cin = f.node_children ("Cinema");
+       for (list<cxml::NodePtr>::iterator i = cin.begin(); i != cin.end(); ++i) {
+               /* Slightly grotty two-part construction of Cinema here so that we can use
+                  shared_from_this.
+               */
+               shared_ptr<Cinema> cinema (new Cinema (*i));
+               cinema->read_screens (*i);
+               _cinemas.push_back (cinema);
+       }
+
+       _mail_server = f.string_child ("MailServer");
+       _mail_user = f.optional_string_child("MailUser").get_value_or ("");
+       _mail_password = f.optional_string_child("MailPassword").get_value_or ("");
+       _kdm_from = f.string_child ("KDMFrom");
+       _kdm_email = f.string_child ("KDMEmail");
+
+       _check_for_updates = f.optional_bool_child("CheckForUpdates").get_value_or (false);
+       _check_for_test_updates = f.optional_bool_child("CheckForTestUpdates").get_value_or (false);
+
+       _maximum_j2k_bandwidth = f.optional_number_child<int> ("MaximumJ2KBandwidth").get_value_or (250000000);
+}
+
+void
+Config::read_old_metadata ()
+{
+       /* XXX: this won't work with non-Latin filenames */
+       ifstream f (file(true).string().c_str ());
        string line;
+
        while (getline (f, line)) {
                if (line.empty ()) {
                        continue;
@@ -81,13 +219,13 @@ Config::Config ()
                } else if (k == N_("default_directory")) {
                        _default_directory = v;
                } else if (k == N_("server_port")) {
-                       _server_port = atoi (v.c_str ());
-               } else if (k == N_("reference_scaler")) {
-                       _reference_scaler = Scaler::from_id (v);
-               } else if (k == N_("reference_filter")) {
-                       _reference_filters.push_back (Filter::from_id (v));
+                       _server_port_base = atoi (v.c_str ());
                } else if (k == N_("server")) {
-                       _servers.push_back (ServerDescription::create_from_metadata (v));
+                       vector<string> b;
+                       split (b, v, is_any_of (" "));
+                       if (b.size() == 2) {
+                               _servers.push_back (b[0]);
+                       }
                } else if (k == N_("tms_ip")) {
                        _tms_ip = v;
                } else if (k == N_("tms_path")) {
@@ -100,24 +238,49 @@ Config::Config ()
                        _sound_processor = SoundProcessor::from_id (v);
                } else if (k == "language") {
                        _language = v;
-               } else if (k == "default_format") {
-                       _default_format = Format::from_metadata (v);
+               } else if (k == "default_container") {
+                       _default_container = Ratio::from_id (v);
                } else if (k == "default_dcp_content_type") {
                        _default_dcp_content_type = DCPContentType::from_dci_name (v);
+               } else if (k == "dcp_metadata_issuer") {
+                       _dcp_metadata.issuer = v;
+               } else if (k == "dcp_metadata_creator") {
+                       _dcp_metadata.creator = v;
+               } else if (k == "dcp_metadata_issue_date") {
+                       _dcp_metadata.issue_date = v;
                }
 
-               _default_dci_metadata.read (k, v);
+               _default_dci_metadata.read_old_metadata (k, v);
        }
 }
 
 /** @return Filename to write configuration to */
-string
-Config::file () const
+boost::filesystem::path
+Config::file (bool old) const
 {
        boost::filesystem::path p;
        p /= g_get_user_config_dir ();
-       p /= N_(".dvdomatic");
-       return p.string ();
+       boost::system::error_code ec;
+       boost::filesystem::create_directory (p, ec);
+       if (old) {
+               p /= ".dvdomatic";
+       } else {
+               p /= "dcpomatic";
+               boost::filesystem::create_directory (p, ec);
+               p /= "config.xml";
+       }
+       return p;
+}
+
+boost::filesystem::path
+Config::signer_chain_directory () const
+{
+       boost::filesystem::path p;
+       p /= g_get_user_config_dir ();
+       p /= "dcpomatic";
+       p /= "crypt";
+       boost::filesystem::create_directories (p);
+       return p;
 }
 
 /** @return Singleton instance */
@@ -126,6 +289,16 @@ Config::instance ()
 {
        if (_instance == 0) {
                _instance = new Config;
+               try {
+                       _instance->read ();
+               } catch (exception& e) {
+                       /* configuration load failed; never mind, just
+                          stick with the default.
+                       */
+                       cerr << "dcpomatic: failed to load configuration (" << e.what() << ")\n";
+               } catch (...) {
+                       cerr << "dcpomatic: failed to load configuration\n";
+               }
        }
 
        return _instance;
@@ -135,47 +308,78 @@ Config::instance ()
 void
 Config::write () const
 {
-       ofstream f (file().c_str ());
-       f << N_("num_local_encoding_threads ") << _num_local_encoding_threads << N_("\n")
-         << N_("default_directory ") << _default_directory << N_("\n")
-         << N_("server_port ") << _server_port << N_("\n");
-
-       if (_reference_scaler) {
-               f << "reference_scaler " << _reference_scaler->id () << "\n";
-       }
+       LocaleGuard lg;
+       
+       xmlpp::Document doc;
+       xmlpp::Element* root = doc.create_root_node ("Config");
 
-       for (vector<Filter const *>::const_iterator i = _reference_filters.begin(); i != _reference_filters.end(); ++i) {
-               f << N_("reference_filter ") << (*i)->id () << N_("\n");
-       }
+       root->add_child("Version")->add_child_text ("1");
+       root->add_child("NumLocalEncodingThreads")->add_child_text (lexical_cast<string> (_num_local_encoding_threads));
+       root->add_child("DefaultDirectory")->add_child_text (_default_directory.string ());
+       root->add_child("ServerPortBase")->add_child_text (lexical_cast<string> (_server_port_base));
+       root->add_child("UseAnyServers")->add_child_text (_use_any_servers ? "1" : "0");
        
-       for (vector<ServerDescription*>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
-               f << N_("server ") << (*i)->as_metadata () << N_("\n");
+       for (vector<string>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
+               root->add_child("Server")->add_child_text (*i);
        }
 
-       f << N_("tms_ip ") << _tms_ip << N_("\n");
-       f << N_("tms_path ") << _tms_path << N_("\n");
-       f << N_("tms_user ") << _tms_user << N_("\n");
-       f << N_("tms_password ") << _tms_password << N_("\n");
+       root->add_child("TMSIP")->add_child_text (_tms_ip);
+       root->add_child("TMSPath")->add_child_text (_tms_path);
+       root->add_child("TMSUser")->add_child_text (_tms_user);
+       root->add_child("TMSPassword")->add_child_text (_tms_password);
        if (_sound_processor) {
-               f << "sound_processor " << _sound_processor->id () << "\n";
+               root->add_child("SoundProcessor")->add_child_text (_sound_processor->id ());
        }
        if (_language) {
-               f << "language " << _language.get() << "\n";
+               root->add_child("Language")->add_child_text (_language.get());
        }
-       if (_default_format) {
-               f << "default_format " << _default_format->as_metadata() << "\n";
+       if (_default_container) {
+               root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
        }
        if (_default_dcp_content_type) {
-               f << "default_dcp_content_type " << _default_dcp_content_type->dci_name() << "\n";
+               root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->dci_name ());
+       }
+       root->add_child("DCPMetadataIssuer")->add_child_text (_dcp_metadata.issuer);
+       root->add_child("DCPMetadataCreator")->add_child_text (_dcp_metadata.creator);
+
+       _default_dci_metadata.as_xml (root->add_child ("DCIMetadata"));
+
+       root->add_child("DefaultStillLength")->add_child_text (lexical_cast<string> (_default_still_length));
+       root->add_child("DefaultJ2KBandwidth")->add_child_text (lexical_cast<string> (_default_j2k_bandwidth));
+       root->add_child("DefaultAudioDelay")->add_child_text (lexical_cast<string> (_default_audio_delay));
+
+       for (vector<PresetColourConversion>::const_iterator i = _colour_conversions.begin(); i != _colour_conversions.end(); ++i) {
+               i->as_xml (root->add_child ("ColourConversion"));
        }
 
-       _default_dci_metadata.write (f);
+       for (list<shared_ptr<Cinema> >::const_iterator i = _cinemas.begin(); i != _cinemas.end(); ++i) {
+               (*i)->as_xml (root->add_child ("Cinema"));
+       }
+
+       root->add_child("MailServer")->add_child_text (_mail_server);
+       root->add_child("MailUser")->add_child_text (_mail_user);
+       root->add_child("MailPassword")->add_child_text (_mail_password);
+       root->add_child("KDMFrom")->add_child_text (_kdm_from);
+       root->add_child("KDMEmail")->add_child_text (_kdm_email);
+
+       root->add_child("CheckForUpdates")->add_child_text (_check_for_updates ? "1" : "0");
+       root->add_child("CheckForTestUpdates")->add_child_text (_check_for_test_updates ? "1" : "0");
+
+       root->add_child("MaximumJ2KBandwidth")->add_child_text (lexical_cast<string> (_maximum_j2k_bandwidth));
+
+       doc.write_to_file_formatted (file(false).string ());
 }
 
-string
-Config::default_directory_or (string a) const
+boost::filesystem::path
+Config::default_directory_or (boost::filesystem::path a) const
 {
-       if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
+       if (_default_directory.empty()) {
+               return a;
+       }
+
+       boost::system::error_code ec;
+       bool const e = boost::filesystem::exists (_default_directory, ec);
+       if (ec || !e) {
                return a;
        }
 
@@ -188,3 +392,10 @@ Config::drop ()
        delete _instance;
        _instance = 0;
 }
+
+void
+Config::changed ()
+{
+       write ();
+       Changed ();
+}