+static bool
+always_overwrite ()
+{
+ return true;
+}
+
+void
+write_files (
+ list<shared_ptr<ScreenKDM> > screen_kdms,
+ bool zip,
+ boost::filesystem::path output,
+ dcp::NameFormat container_name_format,
+ dcp::NameFormat filename_format,
+ dcp::NameFormat::Map values,
+ bool verbose
+ )
+{
+ if (zip) {
+ int const N = CinemaKDMs::write_zip_files (
+ CinemaKDMs::collect (screen_kdms),
+ output,
+ container_name_format,
+ filename_format,
+ values,
+ bind (&always_overwrite)
+ );
+
+ if (verbose) {
+ cout << "Wrote " << N << " ZIP files to " << output << "\n";
+ }
+ } else {
+ int const N = ScreenKDM::write_files (
+ screen_kdms, output, filename_format, values,
+ bind (&always_overwrite)
+ );
+
+ if (verbose) {
+ cout << "Wrote " << N << " KDM files to " << output << "\n";
+ }
+ }
+}
+
+shared_ptr<Cinema>
+find_cinema (string cinema_name)
+{
+ list<shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
+ list<shared_ptr<Cinema> >::const_iterator i = cinemas.begin();
+ while (
+ i != cinemas.end() &&
+ (*i)->name != cinema_name &&
+ find ((*i)->emails.begin(), (*i)->emails.end(), cinema_name) == (*i)->emails.end()) {
+
+ ++i;
+ }
+
+ if (i == cinemas.end ()) {
+ cerr << program_name << ": could not find cinema \"" << cinema_name << "\"\n";
+ exit (EXIT_FAILURE);
+ }
+
+ return *i;
+}
+
+void
+from_film (
+ list<shared_ptr<Screen> > screens,
+ boost::filesystem::path film_dir,
+ bool verbose,
+ boost::filesystem::path output,
+ dcp::NameFormat container_name_format,
+ dcp::NameFormat filename_format,
+ boost::posix_time::ptime valid_from,
+ boost::posix_time::ptime valid_to,
+ dcp::Formulation formulation,
+ bool disable_forensic_marking_picture,
+ optional<int> disable_forensic_marking_audio,
+ bool zip
+ )
+{
+ shared_ptr<Film> film;
+ try {
+ film.reset (new Film (film_dir));
+ film->read_metadata ();
+ if (verbose) {
+ cout << "Read film " << film->name () << "\n";
+ }
+ } catch (std::exception& e) {
+ cerr << program_name << ": error reading film `" << film_dir.string() << "' (" << e.what() << ")\n";
+ exit (EXIT_FAILURE);
+ }
+
+ /* XXX: allow specification of this */
+ vector<CPLSummary> cpls = film->cpls ();
+ if (cpls.empty ()) {
+ error ("no CPLs found in film");
+ } else if (cpls.size() > 1) {
+ error ("more than one CPL found in film");
+ }
+
+ boost::filesystem::path cpl = cpls.front().cpl_file;
+
+ dcp::NameFormat::Map values;
+ values['f'] = film->name();
+ values['b'] = dcp::LocalTime(valid_from).date() + " " + dcp::LocalTime(valid_from).time_of_day(true, false);
+ values['e'] = dcp::LocalTime(valid_to).date() + " " + dcp::LocalTime(valid_to).time_of_day(true, false);
+
+ try {
+ list<shared_ptr<ScreenKDM> > screen_kdms = film->make_kdms (
+ screens, cpl, valid_from, valid_to, formulation, disable_forensic_marking_picture, disable_forensic_marking_audio
+ );
+
+ write_files (screen_kdms, zip, output, container_name_format, filename_format, values, verbose);
+ } catch (FileError& e) {
+ cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
+ exit (EXIT_FAILURE);
+ } catch (KDMError& e) {
+ cerr << program_name << ": " << e.what() << "\n";
+ exit (EXIT_FAILURE);
+ } catch (runtime_error& e) {
+ cerr << program_name << ": " << e.what() << "\n";
+ exit (EXIT_FAILURE);
+ }
+}
+
+optional<dcp::EncryptedKDM>
+sub_find_dkdm (shared_ptr<DKDMGroup> group, string cpl_id)
+{
+ BOOST_FOREACH (shared_ptr<DKDMBase> i, group->children()) {
+ shared_ptr<DKDMGroup> g = dynamic_pointer_cast<DKDMGroup>(i);
+ if (g) {
+ optional<dcp::EncryptedKDM> dkdm = sub_find_dkdm (g, cpl_id);
+ if (dkdm) {
+ return dkdm;
+ }
+ } else {
+ shared_ptr<DKDM> d = dynamic_pointer_cast<DKDM>(i);
+ assert (d);
+ if (d->dkdm().cpl_id() == cpl_id) {
+ return d->dkdm();
+ }
+ }
+ }
+
+ return optional<dcp::EncryptedKDM>();
+}
+
+optional<dcp::EncryptedKDM>
+find_dkdm (string cpl_id)
+{
+ return sub_find_dkdm (Config::instance()->dkdms(), cpl_id);
+}
+
+dcp::EncryptedKDM
+kdm_from_dkdm (
+ dcp::DecryptedKDM dkdm,
+ dcp::Certificate target,
+ vector<string> trusted_devices,
+ dcp::LocalTime valid_from,
+ dcp::LocalTime valid_to,
+ dcp::Formulation formulation,
+ bool disable_forensic_marking_picture,
+ optional<int> disable_forensic_marking_audio
+ )
+{
+ /* Signer for new KDM */
+ shared_ptr<const dcp::CertificateChain> signer = Config::instance()->signer_chain ();
+ if (!signer->valid ()) {
+ error ("signing certificate chain is invalid.");
+ }
+
+ /* Make a new empty KDM and add the keys from the DKDM to it */
+ dcp::DecryptedKDM kdm (
+ valid_from,
+ valid_to,
+ dkdm.annotation_text().get_value_or(""),
+ dkdm.content_title_text(),
+ dcp::LocalTime().as_string()
+ );
+
+ BOOST_FOREACH (dcp::DecryptedKDMKey const & j, dkdm.keys()) {
+ kdm.add_key(j);
+ }
+
+ return kdm.encrypt (signer, target, trusted_devices, formulation, disable_forensic_marking_picture, disable_forensic_marking_audio);
+}
+
+void
+from_dkdm (
+ list<shared_ptr<Screen> > screens,
+ dcp::DecryptedKDM dkdm,
+ bool verbose,
+ boost::filesystem::path output,
+ dcp::NameFormat container_name_format,
+ dcp::NameFormat filename_format,
+ boost::posix_time::ptime valid_from,
+ boost::posix_time::ptime valid_to,
+ dcp::Formulation formulation,
+ bool disable_forensic_marking_picture,
+ optional<int> disable_forensic_marking_audio,
+ bool zip
+ )
+{
+ dcp::NameFormat::Map values;
+ values['f'] = dkdm.annotation_text().get_value_or("");
+ values['b'] = dcp::LocalTime(valid_from).date() + " " + dcp::LocalTime(valid_from).time_of_day(true, false);
+ values['e'] = dcp::LocalTime(valid_to).date() + " " + dcp::LocalTime(valid_to).time_of_day(true, false);
+
+ try {
+ list<shared_ptr<ScreenKDM> > screen_kdms;
+ BOOST_FOREACH (shared_ptr<Screen> i, screens) {
+ if (!i->recipient) {
+ continue;
+ }
+
+ screen_kdms.push_back (
+ shared_ptr<ScreenKDM>(
+ new DCPScreenKDM(
+ i,
+ kdm_from_dkdm(
+ dkdm,
+ i->recipient.get(),
+ i->trusted_device_thumbprints(),
+ dcp::LocalTime(valid_from, i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute()),
+ dcp::LocalTime(valid_to, i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute()),
+ formulation,
+ disable_forensic_marking_picture,
+ disable_forensic_marking_audio
+ )
+ )
+ )
+ );
+ }
+ write_files (screen_kdms, zip, output, container_name_format, filename_format, values, verbose);
+ } catch (FileError& e) {
+ cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
+ exit (EXIT_FAILURE);
+ } catch (KDMError& e) {
+ cerr << program_name << ": " << e.what() << "\n";
+ exit (EXIT_FAILURE);
+ }
+}
+
+void
+dump_dkdm_group (shared_ptr<DKDMGroup> group, int indent)
+{
+ if (indent > 0) {
+ for (int i = 0; i < indent; ++i) {
+ cout << " ";
+ }
+ cout << group->name() << "\n";
+ }
+ BOOST_FOREACH (shared_ptr<DKDMBase> i, group->children()) {
+ shared_ptr<DKDMGroup> g = dynamic_pointer_cast<DKDMGroup>(i);
+ if (g) {
+ dump_dkdm_group (g, indent + 2);
+ } else {
+ for (int j = 0; j < indent; ++j) {
+ cout << " ";
+ }
+ shared_ptr<DKDM> d = dynamic_pointer_cast<DKDM>(i);
+ assert(d);
+ cout << d->dkdm().cpl_id() << "\n";
+ }
+ }
+}
+