2 Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "kdm_with_metadata.h"
28 #include "dcpomatic_log.h"
37 using std::shared_ptr;
38 using boost::optional;
44 list<KDMWithMetadataPtr> kdms,
45 boost::filesystem::path directory,
46 dcp::NameFormat name_format,
47 std::function<bool (boost::filesystem::path)> confirm_overwrite
52 if (directory == "-") {
53 /* Write KDMs to the stdout */
55 cout << i->kdm_as_xml ();
62 if (!boost::filesystem::exists (directory)) {
63 boost::filesystem::create_directories (directory);
66 /* Write KDMs to the specified directory */
68 auto out = directory / careful_string_filter(name_format.get(i->name_values(), ".xml"));
69 if (!boost::filesystem::exists (out) || confirm_overwrite (out)) {
80 KDMWithMetadata::get (char k) const
82 auto i = _name_values.find (k);
83 if (i == _name_values.end()) {
92 make_zip_file (list<KDMWithMetadataPtr> kdms, boost::filesystem::path zip_file, dcp::NameFormat name_format)
94 Zipper zipper (zip_file);
97 auto const name = careful_string_filter(name_format.get(i->name_values(), ".xml"));
98 zipper.add (name, i->kdm_as_xml());
105 /** Collect a list of KDMWithMetadatas into a list of lists so that
106 * each list contains the KDMs for one list.
108 list<list<KDMWithMetadataPtr>>
109 collect (list<KDMWithMetadataPtr> kdms)
111 list<list<KDMWithMetadataPtr>> grouped;
115 auto j = grouped.begin ();
117 while (j != grouped.end()) {
118 if (j->front()->group() == i->group()) {
125 if (j == grouped.end()) {
126 grouped.push_back (list<KDMWithMetadataPtr>());
127 grouped.back().push_back (i);
135 /** Write one directory per list into another directory */
138 list<list<KDMWithMetadataPtr>> kdms,
139 boost::filesystem::path directory,
140 dcp::NameFormat container_name_format,
141 dcp::NameFormat filename_format,
142 function<bool (boost::filesystem::path)> confirm_overwrite
147 for (auto const& i: kdms) {
148 boost::filesystem::path path = directory;
149 path /= container_name_format.get(i.front()->name_values(), "", "s");
150 if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
151 boost::filesystem::create_directories (path);
152 write_files (i, path, filename_format, confirm_overwrite);
161 /** Write one ZIP file per cinema into a directory */
164 list<list<KDMWithMetadataPtr>> kdms,
165 boost::filesystem::path directory,
166 dcp::NameFormat container_name_format,
167 dcp::NameFormat filename_format,
168 function<bool (boost::filesystem::path)> confirm_overwrite
173 for (auto const& i: kdms) {
174 auto path = directory;
175 path /= container_name_format.get(i.front()->name_values(), ".zip", "s");
176 if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
177 if (boost::filesystem::exists (path)) {
178 /* Creating a new zip file over an existing one is an error */
179 boost::filesystem::remove (path);
181 make_zip_file (i, path, filename_format);
190 /** Email one ZIP file per cinema to the cinema.
191 * @param kdms KDMs to email.
192 * @param container_name_format Format of folder / ZIP to use.
193 * @param filename_format Format of filenames to use.
194 * @param name_values Values to substitute into \p container_name_format and \p filename_format.
195 * @param cpl_name Name of the CPL that the KDMs are for.
199 list<list<KDMWithMetadataPtr>> kdms,
200 dcp::NameFormat container_name_format,
201 dcp::NameFormat filename_format,
205 auto config = Config::instance ();
207 if (config->mail_server().empty()) {
208 throw NetworkError (_("No mail server configured in preferences"));
211 for (auto const& i: kdms) {
213 if (i.front()->emails().empty()) {
217 auto zip_file = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
218 boost::filesystem::create_directories (zip_file);
219 zip_file /= container_name_format.get(i.front()->name_values(), ".zip");
220 make_zip_file (i, zip_file, filename_format);
222 auto subject = config->kdm_subject();
223 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
224 boost::algorithm::replace_all (subject, "$START_TIME", i.front()->get('b').get_value_or(""));
225 boost::algorithm::replace_all (subject, "$END_TIME", i.front()->get('e').get_value_or(""));
226 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i.front()->get('c').get_value_or(""));
228 auto body = config->kdm_email();
229 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_name);
230 boost::algorithm::replace_all (body, "$START_TIME", i.front()->get('b').get_value_or(""));
231 boost::algorithm::replace_all (body, "$END_TIME", i.front()->get('e').get_value_or(""));
232 boost::algorithm::replace_all (body, "$CINEMA_NAME", i.front()->get('c').get_value_or(""));
236 auto screen_name = j->get('n');
238 screens += *screen_name + ", ";
241 boost::algorithm::replace_all (body, "$SCREENS", screens.substr (0, screens.length() - 2));
243 Emailer email (config->kdm_from(), i.front()->emails(), subject, body);
245 for (auto i: config->kdm_cc()) {
248 if (!config->kdm_bcc().empty()) {
249 email.add_bcc (config->kdm_bcc());
252 email.add_attachment (zip_file, container_name_format.get(i.front()->name_values(), ".zip"), "application/zip");
255 email.send (config->mail_server(), config->mail_port(), config->mail_protocol(), config->mail_user(), config->mail_password());
257 boost::filesystem::remove (zip_file);
258 dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
259 dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
260 dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
261 dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
265 boost::filesystem::remove (zip_file);
267 dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
268 dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
269 dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
270 dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);