Merge master.
[dcpomatic.git] / src / lib / config.cc
index 0d6a550549bf544ddc0c6d73a879116cc6265d0d..7e37625878191a87db0e7bf1cf3959e5b3fb68e3 100644 (file)
@@ -17,7 +17,6 @@
 
 */
 
-#include <sstream>
 #include <cstdlib>
 #include <fstream>
 #include <glib.h>
 #include "i18n.h"
 
 using std::vector;
+using std::cout;
 using std::ifstream;
 using std::string;
 using std::list;
 using std::max;
+using std::remove;
 using std::exception;
 using std::cerr;
 using boost::shared_ptr;
@@ -94,6 +95,10 @@ void
 Config::read ()
 {
        if (!boost::filesystem::exists (file (false))) {
+               /* Make a new set of signing certificates and key */
+               _signer.reset (new dcp::Signer (openssl_path ()));
+               /* And decryption keys */
+               make_decryption_keys ();
                return;
        }
 
@@ -201,6 +206,7 @@ Config::read ()
        _kdm_subject = f.optional_string_child ("KDMSubject").get_value_or (_("KDM delivery: $CPL_NAME"));
        _kdm_from = f.string_child ("KDMFrom");
        _kdm_cc = f.optional_string_child ("KDMCC").get_value_or ("");
+       _kdm_bcc = f.optional_string_child ("KDMBCC").get_value_or ("");
        _kdm_email = f.string_child ("KDMEmail");
 
        _check_for_updates = f.optional_bool_child("CheckForUpdates").get_value_or (false);
@@ -211,13 +217,18 @@ Config::read ()
 
        _log_types = f.optional_number_child<int> ("LogTypes").get_value_or (Log::TYPE_GENERAL | Log::TYPE_WARNING | Log::TYPE_ERROR);
 
+       list<cxml::NodePtr> his = f.node_children ("History");
+       for (list<cxml::NodePtr>::const_iterator i = his.begin(); i != his.end(); ++i) {
+               _history.push_back ((*i)->content ());
+       }
+
        cxml::NodePtr signer = f.optional_node_child ("Signer");
        dcp::CertificateChain signer_chain;
        if (signer) {
                /* Read the signing certificates and private key in from the config file */
                list<cxml::NodePtr> certificates = signer->node_children ("Certificate");
                for (list<cxml::NodePtr>::const_iterator i = certificates.begin(); i != certificates.end(); ++i) {
-                       signer_chain.add (shared_ptr<dcp::Certificate> (new dcp::Certificate ((*i)->content ())));
+                       signer_chain.add (dcp::Certificate ((*i)->content ()));
                }
 
                _signer.reset (new dcp::Signer (signer_chain, signer->string_child ("PrivateKey")));
@@ -236,13 +247,19 @@ Config::read ()
 
        if (!f.optional_string_child ("DecryptionCertificate") || !f.optional_string_child ("DecryptionPrivateKey")) {
                /* Generate our own decryption certificate and key if either is not present in config */
-               boost::filesystem::path p = dcp::make_certificate_chain (openssl_path ());
-               _decryption_certificate = dcp::Certificate (dcp::file_to_string (p / "leaf.signed.pem"));
-               _decryption_private_key = dcp::file_to_string (p / "leaf.key");
-               boost::filesystem::remove_all (p);
+               make_decryption_keys ();
        }
 }
 
+void
+Config::make_decryption_keys ()
+{
+       boost::filesystem::path p = dcp::make_certificate_chain (openssl_path ());
+       _decryption_certificate = dcp::Certificate (dcp::file_to_string (p / "leaf.signed.pem"));
+       _decryption_private_key = dcp::file_to_string (p / "leaf.key");
+       boost::filesystem::remove_all (p);
+}
+
 /** @return Filename to write configuration to */
 boost::filesystem::path
 Config::file (bool old) const
@@ -341,6 +358,7 @@ Config::write () const
        root->add_child("KDMSubject")->add_child_text (_kdm_subject);
        root->add_child("KDMFrom")->add_child_text (_kdm_from);
        root->add_child("KDMCC")->add_child_text (_kdm_cc);
+       root->add_child("KDMBCC")->add_child_text (_kdm_bcc);
        root->add_child("KDMEmail")->add_child_text (_kdm_email);
 
        root->add_child("CheckForUpdates")->add_child_text (_check_for_updates ? "1" : "0");
@@ -353,13 +371,17 @@ Config::write () const
        xmlpp::Element* signer = root->add_child ("Signer");
        dcp::CertificateChain::List certs = _signer->certificates().root_to_leaf ();
        for (dcp::CertificateChain::List::const_iterator i = certs.begin(); i != certs.end(); ++i) {
-               signer->add_child("Certificate")->add_child_text ((*i)->certificate (true));
+               signer->add_child("Certificate")->add_child_text (i->certificate (true));
        }
        signer->add_child("PrivateKey")->add_child_text (_signer->key ());
 
        root->add_child("DecryptionCertificate")->add_child_text (_decryption_certificate.certificate (true));
        root->add_child("DecryptionPrivateKey")->add_child_text (_decryption_private_key);
 
+       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(false).string ());
 }
 
@@ -405,3 +427,17 @@ Config::reset_kdm_email ()
                "Best regards,\nDCP-o-matic"
                );
 }
+
+void
+Config::add_to_history (boost::filesystem::path p)
+{
+       /* Remove existing instances of this path in the history */
+       _history.erase (remove (_history.begin(), _history.end(), p), _history.end ());
+       
+       _history.insert (_history.begin (), p);
+       if (_history.size() > HISTORY_SIZE) {
+               _history.pop_back ();
+       }
+
+       changed ();
+}