Add configuration options for DKDMs.
[dcpomatic.git] / src / lib / config.cc
index 7364a122c4998c877a7970331840e240c50ec146..ebb6ece931f0bed3899e734f17c0baefdb2f4dad 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2019 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
@@ -32,6 +32,7 @@
 #include "dkdm_wrapper.h"
 #include "compose.hpp"
 #include "crypto.h"
+#include "dkdm_recipient.h"
 #include <dcp/raw_convert.h>
 #include <dcp/name_format.h>
 #include <dcp/certificate_chain.h>
@@ -127,10 +128,12 @@ Config::set_defaults ()
        _win32_console = false;
 #endif
        _cinemas_file = path ("cinemas.xml");
+       _dkdm_recipients_file = path ("dkdm_recipients.xml");
        _show_hints_before_make_dcp = true;
        _confirm_kdm_email = true;
        _kdm_container_name_format = dcp::NameFormat ("KDM %f %c");
        _kdm_filename_format = dcp::NameFormat ("KDM %f %c %s");
+       _dkdm_filename_format = dcp::NameFormat ("DKDM %f %c %s");
        _dcp_metadata_filename_format = dcp::NameFormat ("%t");
        _dcp_asset_filename_format = dcp::NameFormat ("%t");
        _jump_to_selected = true;
@@ -232,6 +235,7 @@ Config::backup ()
 
                boost::filesystem::copy_file(path("config.xml", false), path(String::compose("config.xml.%1", n), false));
                boost::filesystem::copy_file(path("cinemas.xml", false), path(String::compose("cinemas.xml.%1", n), false));
+               boost::filesystem::copy_file(path("dkdm_recipients.xml", false), path(String::compose("dkdm_recipients.xml.%1", n), false));
        } catch (...) {}
 }
 
@@ -334,7 +338,9 @@ try
        _default_interop = f.optional_bool_child("DefaultInterop").get_value_or (false);
        _default_kdm_directory = f.optional_string_child("DefaultKDMDirectory");
 
-       /* Load any cinemas from config.xml */
+       /* Read any cinemas that are still lying around in the config file
+        * from an old version.
+        */
        read_cinemas (f);
 
        _mail_server = f.string_child ("MailServer");
@@ -513,10 +519,12 @@ try
                }
        }
        _cinemas_file = f.optional_string_child("CinemasFile").get_value_or (path ("cinemas.xml").string ());
+       _dkdm_recipients_file = f.optional_string_child("DKDMRecipientsFile").get_value_or (path("dkdm_recipients.xml").string());
        _show_hints_before_make_dcp = f.optional_bool_child("ShowHintsBeforeMakeDCP").get_value_or (true);
        _confirm_kdm_email = f.optional_bool_child("ConfirmKDMEmail").get_value_or (true);
        _kdm_container_name_format = dcp::NameFormat (f.optional_string_child("KDMContainerNameFormat").get_value_or ("KDM %f %c"));
        _kdm_filename_format = dcp::NameFormat (f.optional_string_child("KDMFilenameFormat").get_value_or ("KDM %f %c %s"));
+       _dkdm_filename_format = dcp::NameFormat (f.optional_string_child("DKDMFilenameFormat").get_value_or("DKDM %f %c %s"));
        _dcp_metadata_filename_format = dcp::NameFormat (f.optional_string_child("DCPMetadataFilenameFormat").get_value_or ("%t"));
        _dcp_asset_filename_format = dcp::NameFormat (f.optional_string_child("DCPAssetFilenameFormat").get_value_or ("%t"));
        _jump_to_selected = f.optional_bool_child("JumpToSelected").get_value_or (true);
@@ -608,12 +616,17 @@ try
        _player_lock_file = f.optional_string_child("PlayerLockFile");
 #endif
 
-       /* Replace any cinemas from config.xml with those from the configured file */
        if (boost::filesystem::exists (_cinemas_file)) {
                cxml::Document f ("Cinemas");
                f.read_file (_cinemas_file);
                read_cinemas (f);
        }
+
+       if (boost::filesystem::exists (_dkdm_recipients_file)) {
+               cxml::Document f ("DKDMRecipients");
+               f.read_file (_dkdm_recipients_file);
+               read_dkdm_recipients (f);
+       }
 }
 catch (...) {
        if (have_existing ("config.xml")) {
@@ -647,6 +660,7 @@ Config::write () const
 {
        write_config ();
        write_cinemas ();
+       write_dkdm_recipients ();
 }
 
 void
@@ -883,12 +897,16 @@ Config::write_config () const
 
        /* [XML] CinemasFile Filename of cinemas list file. */
        root->add_child("CinemasFile")->add_child_text (_cinemas_file.string());
+       /* [XML] DKDMRecipientsFile Filename of DKDM recipients list file. */
+       root->add_child("DKDMRecipientsFile")->add_child_text (_dkdm_recipients_file.string());
        /* [XML] ShowHintsBeforeMakeDCP 1 to show hints in the GUI before making a DCP, otherwise 0. */
        root->add_child("ShowHintsBeforeMakeDCP")->add_child_text (_show_hints_before_make_dcp ? "1" : "0");
        /* [XML] ConfirmKDMEmail 1 to confirm before sending KDM emails in the GUI, otherwise 0. */
        root->add_child("ConfirmKDMEmail")->add_child_text (_confirm_kdm_email ? "1" : "0");
        /* [XML] KDMFilenameFormat Format for KDM filenames. */
        root->add_child("KDMFilenameFormat")->add_child_text (_kdm_filename_format.specification ());
+       /* [XML] KDMFilenameFormat Format for DKDM filenames. */
+       root->add_child("DKDMFilenameFormat")->add_child_text(_dkdm_filename_format.specification());
        /* [XML] KDMContainerNameFormat Format for KDM containers (directories or ZIP files). */
        root->add_child("KDMContainerNameFormat")->add_child_text (_kdm_container_name_format.specification ());
        /* [XML] DCPMetadataFilenameFormat Format for DCP metadata filenames. */
@@ -1080,28 +1098,45 @@ Config::write_config () const
        }
 }
 
+
+template <class T>
 void
-Config::write_cinemas () const
+write_file (string root_node, string node, string version, list<shared_ptr<T> > things, boost::filesystem::path file)
 {
        xmlpp::Document doc;
-       xmlpp::Element* root = doc.create_root_node ("Cinemas");
-       root->add_child("Version")->add_child_text ("1");
+       xmlpp::Element* root = doc.create_root_node (root_node);
+       root->add_child("Version")->add_child_text(version);
 
-       BOOST_FOREACH (shared_ptr<Cinema> i, _cinemas) {
-               i->as_xml (root->add_child ("Cinema"));
+       BOOST_FOREACH (shared_ptr<T> i, things) {
+               i->as_xml (root->add_child(node));
        }
 
        try {
-               doc.write_to_file_formatted (_cinemas_file.string() + ".tmp");
-               boost::filesystem::remove (_cinemas_file);
-               boost::filesystem::rename (_cinemas_file.string() + ".tmp", _cinemas_file);
+               doc.write_to_file_formatted (file.string() + ".tmp");
+               boost::filesystem::remove (file);
+               boost::filesystem::rename (file.string() + ".tmp", file);
        } catch (xmlpp::exception& e) {
                string s = e.what ();
                trim (s);
-               throw FileError (s, _cinemas_file);
+               throw FileError (s, file);
        }
 }
 
+
+void
+Config::write_cinemas () const
+{
+       write_file ("Cinemas", "Cinema", "1", _cinemas, _cinemas_file);
+}
+
+
+void
+Config::write_dkdm_recipients () const
+{
+       write_file ("DKDMRecipients", "DKDMRecipient", "1", _dkdm_recipients, _dkdm_recipients_file);
+}
+
+
 boost::filesystem::path
 Config::default_directory_or (boost::filesystem::path a) const
 {
@@ -1293,6 +1328,37 @@ Config::set_cinemas_file (boost::filesystem::path file)
        changed (OTHER);
 }
 
+
+void
+Config::read_dkdm_recipients (cxml::Document const & f)
+{
+       _dkdm_recipients.clear ();
+       list<cxml::NodePtr> cin = f.node_children ("DKDMRecipient");
+       BOOST_FOREACH (cxml::ConstNodePtr i, f.node_children("DKDMRecipient")) {
+               _dkdm_recipients.push_back (shared_ptr<DKDMRecipient>(new DKDMRecipient(i)));
+       }
+}
+
+void
+Config::set_dkdm_recipients_file (boost::filesystem::path file)
+{
+       if (file == _dkdm_recipients_file) {
+               return;
+       }
+
+       _dkdm_recipients_file = file;
+
+       if (boost::filesystem::exists (_dkdm_recipients_file)) {
+               /* Existing file; read it in */
+               cxml::Document f ("DKDMRecipients");
+               f.read_file (_dkdm_recipients_file);
+               read_dkdm_recipients (f);
+       }
+
+       changed (OTHER);
+}
+
+
 void
 Config::save_template (shared_ptr<const Film> film, string name) const
 {