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> 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, 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, 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 lists so that
109 * each list contains the KDMs for one cinema.
111 list<list<KDMWithMetadataPtr> >
112 collect (list<KDMWithMetadataPtr> kdms)
114 list<list<KDMWithMetadataPtr> > grouped;
116 BOOST_FOREACH (KDMWithMetadataPtr i, kdms) {
118 list<list<KDMWithMetadataPtr> >::iterator j = grouped.begin ();
120 while (j != grouped.end()) {
121 if (j->front()->cinema() == i->cinema()) {
128 if (j == grouped.end()) {
129 grouped.push_back (list<KDMWithMetadataPtr>());
130 grouped.back().push_back (i);
138 /** Write one directory per cinema into another directory */
141 list<list<KDMWithMetadataPtr> > cinema_kdms,
142 boost::filesystem::path directory,
143 dcp::NameFormat container_name_format,
144 dcp::NameFormat filename_format,
145 dcp::NameFormat::Map name_values,
146 function<bool (boost::filesystem::path)> confirm_overwrite
149 /* No specific screen */
150 name_values['s'] = "";
154 BOOST_FOREACH (list<KDMWithMetadataPtr> const & i, cinema_kdms) {
155 boost::filesystem::path path = directory;
156 path /= container_name_format.get(name_values, "");
157 if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
158 boost::filesystem::create_directories (path);
159 write_files (i, path, filename_format, name_values, confirm_overwrite);
168 /** Write one ZIP file per cinema into a directory */
171 list<list<KDMWithMetadataPtr> > cinema_kdms,
172 boost::filesystem::path directory,
173 dcp::NameFormat container_name_format,
174 dcp::NameFormat filename_format,
175 dcp::NameFormat::Map name_values,
176 function<bool (boost::filesystem::path)> confirm_overwrite
179 /* No specific screen */
180 name_values['s'] = "";
184 BOOST_FOREACH (list<KDMWithMetadataPtr> const & i, cinema_kdms) {
185 boost::filesystem::path path = directory;
186 path /= container_name_format.get(name_values, ".zip");
187 if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
188 if (boost::filesystem::exists (path)) {
189 /* Creating a new zip file over an existing one is an error */
190 boost::filesystem::remove (path);
192 make_zip_file (i, path, filename_format, name_values);
201 /** Email one ZIP file per cinema to the cinema.
202 * @param cinema_kdms KDMS to email.
203 * @param container_name_format Format of folder / ZIP to use.
204 * @param filename_format Format of filenames to use.
205 * @param name_values Values to substitute into \p container_name_format and \p filename_format.
206 * @param cpl_name Name of the CPL that the KDMs are for.
210 list<list<KDMWithMetadataPtr> > cinema_kdms,
211 dcp::NameFormat container_name_format,
212 dcp::NameFormat filename_format,
213 dcp::NameFormat::Map name_values,
217 Config* config = Config::instance ();
219 if (config->mail_server().empty()) {
220 throw NetworkError (_("No mail server configured in preferences"));
223 /* No specific screen */
224 name_values['s'] = "";
226 BOOST_FOREACH (list<KDMWithMetadataPtr> const & i, cinema_kdms) {
228 if (i.front()->cinema()->emails.empty()) {
232 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
233 boost::filesystem::create_directories (zip_file);
234 zip_file /= container_name_format.get(name_values, ".zip");
235 make_zip_file (i, zip_file, filename_format, name_values);
237 string subject = config->kdm_subject();
238 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
239 boost::algorithm::replace_all (subject, "$START_TIME", name_values['b']);
240 boost::algorithm::replace_all (subject, "$END_TIME", name_values['e']);
241 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i.front()->cinema()->name);
243 string body = config->kdm_email().c_str();
244 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_name);
245 boost::algorithm::replace_all (body, "$START_TIME", name_values['b']);
246 boost::algorithm::replace_all (body, "$END_TIME", name_values['e']);
247 boost::algorithm::replace_all (body, "$CINEMA_NAME", i.front()->cinema()->name);
250 BOOST_FOREACH (KDMWithMetadataPtr j, i) {
251 optional<string> screen_name = j->get('n');
253 screens += *screen_name + ", ";
256 boost::algorithm::replace_all (body, "$SCREENS", screens.substr (0, screens.length() - 2));
258 Emailer email (config->kdm_from(), i.front()->cinema()->emails, subject, body);
260 BOOST_FOREACH (string i, config->kdm_cc()) {
263 if (!config->kdm_bcc().empty ()) {
264 email.add_bcc (config->kdm_bcc ());
267 email.add_attachment (zip_file, container_name_format.get(name_values, ".zip"), "application/zip");
269 Config* c = Config::instance ();
272 email.send (c->mail_server(), c->mail_port(), c->mail_protocol(), c->mail_user(), c->mail_password());
274 boost::filesystem::remove (zip_file);
275 dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
276 dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
277 dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
278 dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
282 boost::filesystem::remove (zip_file);
284 dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
285 dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
286 dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
287 dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);