Merge master.
[dcpomatic.git] / src / lib / config.cc
index 8c65e371a229aef9ff090f50f7e9deb57170d6c6..b6f464717b48c7f6ca3f5a590c58ca5480d01e22 100644 (file)
@@ -22,6 +22,7 @@
 #include <fstream>
 #include <glib.h>
 #include <boost/filesystem.hpp>
+#include <libcxml/cxml.h>
 #include "config.h"
 #include "server.h"
 #include "scaler.h"
@@ -36,7 +37,10 @@ using std::vector;
 using std::ifstream;
 using std::string;
 using std::ofstream;
+using std::list;
 using boost::shared_ptr;
+using boost::lexical_cast;
+using boost::optional;
 
 Config* Config::_instance = 0;
 
@@ -47,6 +51,7 @@ Config::Config ()
        , _reference_scaler (Scaler::from_id (N_("bicubic")))
        , _tms_path (N_("."))
        , _sound_processor (SoundProcessor::from_id (N_("dolby_cp750")))
+       , _default_still_length (10)
        , _default_format (0)
        , _default_dcp_content_type (0)
 {
@@ -56,8 +61,66 @@ Config::Config ()
        _allowed_dcp_frame_rates.push_back (48);
        _allowed_dcp_frame_rates.push_back (50);
        _allowed_dcp_frame_rates.push_back (60);
+
+       if (!boost::filesystem::exists (file (false))) {
+               read_old_metadata ();
+               return;
+       }
+
+       cxml::File f (file (false), "Config");
+       optional<string> c;
+
+       _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
+       _default_directory = f.string_child ("DefaultDirectory");
+       _server_port = f.number_child<int> ("ServerPort");
+       c = f.optional_string_child ("ReferenceScaler");
+       if (c) {
+               _reference_scaler = Scaler::from_id (c.get ());
+       }
+
+       list<shared_ptr<cxml::Node> > filters = f.node_children ("ReferenceFilter");
+       for (list<shared_ptr<cxml::Node> >::iterator i = filters.begin(); i != filters.end(); ++i) {
+               _reference_filters.push_back (Filter::from_id ((*i)->content ()));
+       }
        
-       ifstream f (file().c_str ());
+       list<shared_ptr<cxml::Node> > servers = f.node_children ("Server");
+       for (list<shared_ptr<cxml::Node> >::iterator i = servers.begin(); i != servers.end(); ++i) {
+               _servers.push_back (new ServerDescription (*i));
+       }
+
+       _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 ("DefaultFormat");
+       if (c) {
+               _default_format = Format::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);
+}
+
+void
+Config::read_old_metadata ()
+{
+       ifstream f (file(true).c_str ());
        string line;
        while (getline (f, line)) {
                if (line.empty ()) {
@@ -101,7 +164,7 @@ Config::Config ()
                } else if (k == "language") {
                        _language = v;
                } else if (k == "default_format") {
-                       _default_format = Format::from_metadata (v);
+                       _default_format = Format::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") {
@@ -112,17 +175,21 @@ Config::Config ()
                        _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
+Config::file (bool old) const
 {
        boost::filesystem::path p;
        p /= g_get_user_config_dir ();
-       p /= N_(".dvdomatic");
+       if (old) {
+               p /= ".dvdomatic";
+       } else {
+               p /= ".dcpomatic.xml";
+       }
        return p.string ();
 }
 
@@ -141,44 +208,48 @@ Config::instance ()
 void
 Config::write () const
 {
-       ofstream f (file().c_str ());
-       f << "num_local_encoding_threads " << _num_local_encoding_threads << "\n"
-         << "default_directory " << _default_directory << "\n"
-         << "server_port " << _server_port << "\n";
+       xmlpp::Document doc;
+       xmlpp::Element* root = doc.create_root_node ("Config");
 
+       root->add_child("NumLocalEncodingThreads")->add_child_text (lexical_cast<string> (_num_local_encoding_threads));
+       root->add_child("DefaultDirectory")->add_child_text (_default_directory);
+       root->add_child("ServerPort")->add_child_text (lexical_cast<string> (_server_port));
        if (_reference_scaler) {
-               f << "reference_scaler " << _reference_scaler->id () << "\n";
+               root->add_child("ReferenceScaler")->add_child_text (_reference_scaler->id ());
        }
 
        for (vector<Filter const *>::const_iterator i = _reference_filters.begin(); i != _reference_filters.end(); ++i) {
-               f << "reference_filter " << (*i)->id () << "\n";
+               root->add_child("ReferenceFilter")->add_child_text ((*i)->id ());
        }
        
        for (vector<ServerDescription*>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
-               f << "server " << (*i)->as_metadata () << "\n";
+               (*i)->as_xml (root->add_child ("Server"));
        }
 
-       f << "tms_ip " << _tms_ip << "\n";
-       f << "tms_path " << _tms_path << "\n";
-       f << "tms_user " << _tms_user << "\n";
-       f << "tms_password " << _tms_password << "\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";
+               root->add_child("DefaultFormat")->add_child_text (_default_format->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 ());
        }
-       f << "dcp_metadata_issuer " << _dcp_metadata.issuer << "\n";
-       f << "dcp_metadata_creator " << _dcp_metadata.creator << "\n";
-       f << "dcp_metadata_issue_date " << _dcp_metadata.issue_date << "\n";
+       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));
 
-       _default_dci_metadata.write (f);
+       doc.write_to_file_formatted (file (false));
 }
 
 string