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 boost::function<bool (boost::filesystem::path)> confirm_overwrite
52 if (directory == "-") {
53 /* Write KDMs to the stdout */
54 BOOST_FOREACH (KDMWithMetadataPtr i, kdms) {
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 */
67 BOOST_FOREACH (KDMWithMetadataPtr i, kdms) {
68 boost::filesystem::path 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 dcp::NameFormat::Map::const_iterator i = _name_values.find (k);
83 if (i == _name_values.end()) {
84 return optional<string>();
92 make_zip_file (list<KDMWithMetadataPtr> kdms, boost::filesystem::path zip_file, dcp::NameFormat name_format)
94 Zipper zipper (zip_file);
96 BOOST_FOREACH (KDMWithMetadataPtr i, kdms) {
97 string 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;
113 BOOST_FOREACH (KDMWithMetadataPtr i, kdms) {
115 list<list<KDMWithMetadataPtr> >::iterator 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 BOOST_FOREACH (list<KDMWithMetadataPtr> 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 BOOST_FOREACH (list<KDMWithMetadataPtr> const & i, kdms) {
174 boost::filesystem::path 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 Config* config = Config::instance ();
207 if (config->mail_server().empty()) {
208 throw NetworkError (_("No mail server configured in preferences"));
211 BOOST_FOREACH (list<KDMWithMetadataPtr> const & i, kdms) {
213 if (i.front()->emails().empty()) {
217 boost::filesystem::path 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 string 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 string body = config->kdm_email().c_str();
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(""));
235 BOOST_FOREACH (KDMWithMetadataPtr j, i) {
236 optional<string> 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 BOOST_FOREACH (string 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");
254 Config* c = Config::instance ();
257 email.send (c->mail_server(), c->mail_port(), c->mail_protocol(), c->mail_user(), c->mail_password());
259 boost::filesystem::remove (zip_file);
260 dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
261 dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
262 dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
263 dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
267 boost::filesystem::remove (zip_file);
269 dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
270 dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
271 dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
272 dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);