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 <boost/foreach.hpp>
38 using std::runtime_error;
39 using boost::shared_ptr;
40 using boost::function;
43 CinemaKDMs::make_zip_file (boost::filesystem::path zip_file, dcp::NameFormat name_format, dcp::NameFormat::Map name_values) const
46 struct zip* zip = zip_open (zip_file.string().c_str(), ZIP_CREATE | ZIP_EXCL, &error);
48 if (error == ZIP_ER_EXISTS) {
49 throw FileError ("ZIP file already exists", zip_file);
51 throw FileError ("could not create ZIP file", zip_file);
54 list<shared_ptr<string> > kdm_strings;
56 name_values['c'] = cinema->name;
58 BOOST_FOREACH (ScreenKDM const & i, screen_kdms) {
59 shared_ptr<string> kdm (new string (i.kdm.as_xml ()));
60 kdm_strings.push_back (kdm);
62 struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
64 throw runtime_error ("could not create ZIP source");
67 name_values['s'] = i.screen->name;
68 name_values['i'] = i.kdm.id ();
69 string const name = name_format.get(name_values, ".xml");
70 if (zip_add (zip, name.c_str(), source) == -1) {
71 throw runtime_error ("failed to add KDM to ZIP archive");
75 if (zip_close (zip) == -1) {
76 throw runtime_error ("failed to close ZIP archive");
80 /** Collect a list of ScreenKDMs into a list of CinemaKDMs so that each
81 * CinemaKDM contains the KDMs for its cinema.
84 CinemaKDMs::collect (list<ScreenKDM> screen_kdms)
86 list<CinemaKDMs> cinema_kdms;
88 while (!screen_kdms.empty ()) {
90 /* Get all the screens from a single cinema */
94 list<ScreenKDM>::iterator i = screen_kdms.begin ();
95 ck.cinema = i->screen->cinema;
96 ck.screen_kdms.push_back (*i);
97 list<ScreenKDM>::iterator j = i;
99 screen_kdms.remove (*j);
101 while (i != screen_kdms.end ()) {
102 if (i->screen->cinema == ck.cinema) {
103 ck.screen_kdms.push_back (*i);
104 list<ScreenKDM>::iterator j = i;
106 screen_kdms.remove (*j);
112 cinema_kdms.push_back (ck);
118 /** Write one directory per cinema into another directory */
120 CinemaKDMs::write_directories (
121 list<CinemaKDMs> cinema_kdms,
122 boost::filesystem::path directory,
123 dcp::NameFormat container_name_format,
124 dcp::NameFormat filename_format,
125 dcp::NameFormat::Map name_values,
126 function<bool (boost::filesystem::path)> confirm_overwrite
129 /* No specific screen */
130 name_values['s'] = "";
134 BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
135 boost::filesystem::path path = directory;
136 name_values['c'] = i.cinema->name;
137 path /= container_name_format.get(name_values, "");
138 if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
139 boost::filesystem::create_directories (path);
140 ScreenKDM::write_files (i.screen_kdms, path, filename_format, name_values, confirm_overwrite);
142 written += i.screen_kdms.size();
148 /** Write one ZIP file per cinema into a directory */
150 CinemaKDMs::write_zip_files (
151 list<CinemaKDMs> cinema_kdms,
152 boost::filesystem::path directory,
153 dcp::NameFormat container_name_format,
154 dcp::NameFormat filename_format,
155 dcp::NameFormat::Map name_values,
156 function<bool (boost::filesystem::path)> confirm_overwrite
159 /* No specific screen */
160 name_values['s'] = "";
164 BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
165 boost::filesystem::path path = directory;
166 name_values['c'] = i.cinema->name;
167 path /= container_name_format.get(name_values, ".zip");
168 if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
169 if (boost::filesystem::exists (path)) {
170 /* Creating a new zip file over an existing one is an error */
171 boost::filesystem::remove (path);
173 i.make_zip_file (path, filename_format, name_values);
174 written += i.screen_kdms.size();
181 /** Email one ZIP file per cinema to the cinema.
182 * @param cinema_kdms KDMS to email.
183 * @param container_name_format Format of folder / ZIP to use.
184 * @param filename_format Format of filenames to use.
185 * @param name_values Values to substitute into \p container_name_format and \p filename_format.
186 * @param cpl_name Name of the CPL that the KDMs are for.
187 * @param log Log to write email session transcript to, or 0.
191 list<CinemaKDMs> cinema_kdms,
192 dcp::NameFormat container_name_format,
193 dcp::NameFormat filename_format,
194 dcp::NameFormat::Map name_values,
199 Config* config = Config::instance ();
201 if (config->mail_server().empty()) {
202 throw NetworkError (_("No mail server configured in preferences"));
205 /* No specific screen */
206 name_values['s'] = "";
208 BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
210 name_values['c'] = i.cinema->name;
212 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
213 boost::filesystem::create_directories (zip_file);
214 zip_file /= container_name_format.get(name_values, ".zip");
215 i.make_zip_file (zip_file, filename_format, name_values);
217 string subject = config->kdm_subject();
218 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
219 boost::algorithm::replace_all (subject, "$START_TIME", name_values['b']);
220 boost::algorithm::replace_all (subject, "$END_TIME", name_values['e']);
221 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i.cinema->name);
223 string body = config->kdm_email().c_str();
224 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_name);
225 boost::algorithm::replace_all (body, "$START_TIME", name_values['b']);
226 boost::algorithm::replace_all (body, "$END_TIME", name_values['e']);
227 boost::algorithm::replace_all (body, "$CINEMA_NAME", i.cinema->name);
230 BOOST_FOREACH (ScreenKDM const & j, i.screen_kdms) {
231 screens += j.screen->name + ", ";
233 boost::algorithm::replace_all (body, "$SCREENS", screens.substr (0, screens.length() - 2));
235 Emailer email (config->kdm_from(), i.cinema->emails, subject, body);
237 BOOST_FOREACH (string i, config->kdm_cc()) {
240 if (!config->kdm_bcc().empty ()) {
241 email.add_bcc (config->kdm_bcc ());
244 email.add_attachment (zip_file, container_name_format.get(name_values, ".zip"), "application/zip");
246 Config* c = Config::instance ();
249 email.send (c->mail_server(), c->mail_port(), c->mail_user(), c->mail_password());
251 boost::filesystem::remove (zip_file);
253 log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
254 log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
255 log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
256 log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
261 boost::filesystem::remove (zip_file);
264 log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
265 log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
266 log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
267 log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);