2 Copyright (C) 2013-2016 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/>.
21 #include "kdm_with_metadata.h"
27 #include "dcpomatic_log.h"
29 #include <boost/foreach.hpp>
30 #include <boost/function.hpp>
31 #include <boost/function.hpp>
38 using boost::shared_ptr;
39 using boost::optional;
40 using boost::function;
44 list<KDMWithMetadataPtr> screen_kdms,
45 boost::filesystem::path directory,
46 dcp::NameFormat name_format,
47 dcp::NameFormat::Map name_values,
48 boost::function<bool (boost::filesystem::path)> confirm_overwrite
53 if (directory == "-") {
54 /* Write KDMs to the stdout */
55 BOOST_FOREACH (KDMWithMetadataPtr i, screen_kdms) {
56 cout << i->kdm_as_xml ();
63 if (!boost::filesystem::exists (directory)) {
64 boost::filesystem::create_directories (directory);
67 /* Write KDMs to the specified directory */
68 BOOST_FOREACH (KDMWithMetadataPtr i, screen_kdms) {
69 name_values['i'] = i->kdm_id ();
70 boost::filesystem::path out = directory / careful_string_filter(name_format.get(name_values, ".xml"));
71 if (!boost::filesystem::exists (out) || confirm_overwrite (out)) {
82 KDMWithMetadata::get (char k) const
84 dcp::NameFormat::Map::const_iterator i = _name_values.find (k);
85 if (i == _name_values.end()) {
86 return optional<string>();
94 make_zip_file (list<KDMWithMetadataPtr> kdms, boost::filesystem::path zip_file, dcp::NameFormat name_format, dcp::NameFormat::Map name_values)
96 Zipper zipper (zip_file);
98 BOOST_FOREACH (KDMWithMetadataPtr i, kdms) {
99 name_values['i'] = i->kdm_id ();
100 string const name = careful_string_filter(name_format.get(name_values, ".xml"));
101 zipper.add (name, i->kdm_as_xml());
108 /** Collect a list of KDMWithMetadatas into a list of list<KDMWithMetadata> so that each
109 * CinemaKDM contains the KDMs for its cinema.
111 list<list<KDMWithMetadataPtr> >
112 collect (list<KDMWithMetadataPtr> screen_kdms)
114 list<list<KDMWithMetadataPtr> > cinema_kdms;
116 while (!screen_kdms.empty ()) {
118 /* Get all the screens from a single cinema */
120 list<KDMWithMetadataPtr> ck;
122 list<KDMWithMetadataPtr>::iterator i = screen_kdms.begin ();
124 list<KDMWithMetadataPtr>::iterator j = i;
126 screen_kdms.remove (*j);
128 while (i != screen_kdms.end ()) {
129 if ((*i)->cinema() == ck.front()->cinema()) {
131 list<KDMWithMetadataPtr>::iterator j = i;
133 screen_kdms.remove (*j);
139 cinema_kdms.push_back (ck);
146 /** Write one directory per cinema into another directory */
149 list<list<KDMWithMetadataPtr> > cinema_kdms,
150 boost::filesystem::path directory,
151 dcp::NameFormat container_name_format,
152 dcp::NameFormat filename_format,
153 dcp::NameFormat::Map name_values,
154 function<bool (boost::filesystem::path)> confirm_overwrite
157 /* No specific screen */
158 name_values['s'] = "";
162 BOOST_FOREACH (list<KDMWithMetadataPtr> const & i, cinema_kdms) {
163 boost::filesystem::path path = directory;
164 path /= container_name_format.get(name_values, "");
165 if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
166 boost::filesystem::create_directories (path);
167 write_files (i, path, filename_format, name_values, confirm_overwrite);
176 /** Write one ZIP file per cinema into a directory */
179 list<list<KDMWithMetadataPtr> > cinema_kdms,
180 boost::filesystem::path directory,
181 dcp::NameFormat container_name_format,
182 dcp::NameFormat filename_format,
183 dcp::NameFormat::Map name_values,
184 function<bool (boost::filesystem::path)> confirm_overwrite
187 /* No specific screen */
188 name_values['s'] = "";
192 BOOST_FOREACH (list<KDMWithMetadataPtr> const & i, cinema_kdms) {
193 boost::filesystem::path path = directory;
194 path /= container_name_format.get(name_values, ".zip");
195 if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
196 if (boost::filesystem::exists (path)) {
197 /* Creating a new zip file over an existing one is an error */
198 boost::filesystem::remove (path);
200 make_zip_file (i, path, filename_format, name_values);
209 /** Email one ZIP file per cinema to the cinema.
210 * @param cinema_kdms KDMS to email.
211 * @param container_name_format Format of folder / ZIP to use.
212 * @param filename_format Format of filenames to use.
213 * @param name_values Values to substitute into \p container_name_format and \p filename_format.
214 * @param cpl_name Name of the CPL that the KDMs are for.
218 list<list<KDMWithMetadataPtr> > cinema_kdms,
219 dcp::NameFormat container_name_format,
220 dcp::NameFormat filename_format,
221 dcp::NameFormat::Map name_values,
225 Config* config = Config::instance ();
227 if (config->mail_server().empty()) {
228 throw NetworkError (_("No mail server configured in preferences"));
231 /* No specific screen */
232 name_values['s'] = "";
234 BOOST_FOREACH (list<KDMWithMetadataPtr> const & i, cinema_kdms) {
236 if (i.front()->cinema()->emails.empty()) {
240 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
241 boost::filesystem::create_directories (zip_file);
242 zip_file /= container_name_format.get(name_values, ".zip");
243 make_zip_file (i, zip_file, filename_format, name_values);
245 string subject = config->kdm_subject();
246 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
247 boost::algorithm::replace_all (subject, "$START_TIME", name_values['b']);
248 boost::algorithm::replace_all (subject, "$END_TIME", name_values['e']);
249 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i.front()->cinema()->name);
251 string body = config->kdm_email().c_str();
252 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_name);
253 boost::algorithm::replace_all (body, "$START_TIME", name_values['b']);
254 boost::algorithm::replace_all (body, "$END_TIME", name_values['e']);
255 boost::algorithm::replace_all (body, "$CINEMA_NAME", i.front()->cinema()->name);
258 BOOST_FOREACH (KDMWithMetadataPtr j, i) {
259 optional<string> screen_name = j->get('n');
261 screens += *screen_name + ", ";
264 boost::algorithm::replace_all (body, "$SCREENS", screens.substr (0, screens.length() - 2));
266 Emailer email (config->kdm_from(), i.front()->cinema()->emails, subject, body);
268 BOOST_FOREACH (string i, config->kdm_cc()) {
271 if (!config->kdm_bcc().empty ()) {
272 email.add_bcc (config->kdm_bcc ());
275 email.add_attachment (zip_file, container_name_format.get(name_values, ".zip"), "application/zip");
277 Config* c = Config::instance ();
280 email.send (c->mail_server(), c->mail_port(), c->mail_protocol(), c->mail_user(), c->mail_password());
282 boost::filesystem::remove (zip_file);
283 dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
284 dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
285 dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
286 dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
290 boost::filesystem::remove (zip_file);
292 dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
293 dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
294 dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
295 dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);