2 Copyright (C) 2013-2015 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 "exceptions.h"
22 #include "cinema_kdms.h"
28 #include "compose.hpp"
31 #include "dcpomatic_log.h"
32 #include "kdm_with_metadata.h"
33 #include <boost/foreach.hpp>
40 using std::runtime_error;
41 using boost::shared_ptr;
42 using boost::function;
43 using boost::optional;
46 make_zip_file (CinemaKDMs kdms, boost::filesystem::path zip_file, dcp::NameFormat name_format, dcp::NameFormat::Map name_values)
48 Zipper zipper (zip_file);
50 name_values['c'] = kdms.cinema->name;
52 BOOST_FOREACH (KDMWithMetadataPtr i, kdms.screen_kdms) {
53 name_values['i'] = i->kdm_id ();
54 string const name = careful_string_filter(name_format.get(name_values, ".xml"));
55 zipper.add (name, i->kdm_as_xml());
61 /** Collect a list of KDMWithMetadatas into a list of CinemaKDMs so that each
62 * CinemaKDM contains the KDMs for its cinema.
65 collect (list<KDMWithMetadataPtr> screen_kdms)
67 list<CinemaKDMs> cinema_kdms;
69 while (!screen_kdms.empty ()) {
71 /* Get all the screens from a single cinema */
75 list<KDMWithMetadataPtr>::iterator i = screen_kdms.begin ();
76 ck.cinema = (*i)->cinema();
77 ck.screen_kdms.push_back (*i);
78 list<KDMWithMetadataPtr>::iterator j = i;
80 screen_kdms.remove (*j);
82 while (i != screen_kdms.end ()) {
83 if ((*i)->cinema() == ck.cinema) {
84 ck.screen_kdms.push_back (*i);
85 list<KDMWithMetadataPtr>::iterator j = i;
87 screen_kdms.remove (*j);
93 cinema_kdms.push_back (ck);
99 /** Write one directory per cinema into another directory */
102 list<CinemaKDMs> cinema_kdms,
103 boost::filesystem::path directory,
104 dcp::NameFormat container_name_format,
105 dcp::NameFormat filename_format,
106 dcp::NameFormat::Map name_values,
107 function<bool (boost::filesystem::path)> confirm_overwrite
110 /* No specific screen */
111 name_values['s'] = "";
115 BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
116 boost::filesystem::path path = directory;
117 name_values['c'] = i.cinema->name;
118 path /= container_name_format.get(name_values, "");
119 if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
120 boost::filesystem::create_directories (path);
121 write_files (i.screen_kdms, path, filename_format, name_values, confirm_overwrite);
123 written += i.screen_kdms.size();
129 /** Write one ZIP file per cinema into a directory */
132 list<CinemaKDMs> cinema_kdms,
133 boost::filesystem::path directory,
134 dcp::NameFormat container_name_format,
135 dcp::NameFormat filename_format,
136 dcp::NameFormat::Map name_values,
137 function<bool (boost::filesystem::path)> confirm_overwrite
140 /* No specific screen */
141 name_values['s'] = "";
145 BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
146 boost::filesystem::path path = directory;
147 name_values['c'] = i.cinema->name;
148 path /= container_name_format.get(name_values, ".zip");
149 if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
150 if (boost::filesystem::exists (path)) {
151 /* Creating a new zip file over an existing one is an error */
152 boost::filesystem::remove (path);
154 make_zip_file (i, path, filename_format, name_values);
155 written += i.screen_kdms.size();
162 /** Email one ZIP file per cinema to the cinema.
163 * @param cinema_kdms KDMS to email.
164 * @param container_name_format Format of folder / ZIP to use.
165 * @param filename_format Format of filenames to use.
166 * @param name_values Values to substitute into \p container_name_format and \p filename_format.
167 * @param cpl_name Name of the CPL that the KDMs are for.
171 list<CinemaKDMs> cinema_kdms,
172 dcp::NameFormat container_name_format,
173 dcp::NameFormat filename_format,
174 dcp::NameFormat::Map name_values,
178 Config* config = Config::instance ();
180 if (config->mail_server().empty()) {
181 throw NetworkError (_("No mail server configured in preferences"));
184 /* No specific screen */
185 name_values['s'] = "";
187 BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
189 if (i.cinema->emails.empty()) {
193 name_values['c'] = i.cinema->name;
195 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
196 boost::filesystem::create_directories (zip_file);
197 zip_file /= container_name_format.get(name_values, ".zip");
198 make_zip_file (i, zip_file, filename_format, name_values);
200 string subject = config->kdm_subject();
201 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
202 boost::algorithm::replace_all (subject, "$START_TIME", name_values['b']);
203 boost::algorithm::replace_all (subject, "$END_TIME", name_values['e']);
204 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i.cinema->name);
206 string body = config->kdm_email().c_str();
207 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_name);
208 boost::algorithm::replace_all (body, "$START_TIME", name_values['b']);
209 boost::algorithm::replace_all (body, "$END_TIME", name_values['e']);
210 boost::algorithm::replace_all (body, "$CINEMA_NAME", i.cinema->name);
213 BOOST_FOREACH (KDMWithMetadataPtr j, i.screen_kdms) {
214 optional<string> screen_name = j->get('n');
216 screens += *screen_name + ", ";
219 boost::algorithm::replace_all (body, "$SCREENS", screens.substr (0, screens.length() - 2));
221 Emailer email (config->kdm_from(), i.cinema->emails, subject, body);
223 BOOST_FOREACH (string i, config->kdm_cc()) {
226 if (!config->kdm_bcc().empty ()) {
227 email.add_bcc (config->kdm_bcc ());
230 email.add_attachment (zip_file, container_name_format.get(name_values, ".zip"), "application/zip");
232 Config* c = Config::instance ();
235 email.send (c->mail_server(), c->mail_port(), c->mail_protocol(), c->mail_user(), c->mail_password());
237 boost::filesystem::remove (zip_file);
238 dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
239 dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
240 dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
241 dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
245 boost::filesystem::remove (zip_file);
247 dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
248 dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
249 dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
250 dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);