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"
30 #include "dcpomatic_log.h"
32 #include <boost/foreach.hpp>
39 using std::runtime_error;
40 using boost::shared_ptr;
41 using boost::function;
44 CinemaKDMs::make_zip_file (boost::filesystem::path zip_file, dcp::NameFormat name_format, dcp::NameFormat::Map name_values) const
47 struct zip* zip = zip_open (zip_file.string().c_str(), ZIP_CREATE | ZIP_EXCL, &error);
49 if (error == ZIP_ER_EXISTS) {
50 throw FileError ("ZIP file already exists", zip_file);
52 throw FileError ("could not create ZIP file", zip_file);
55 list<shared_ptr<string> > kdm_strings;
57 name_values['c'] = cinema->name;
59 BOOST_FOREACH (ScreenKDM const & i, screen_kdms) {
60 shared_ptr<string> kdm (new string (i.kdm.as_xml ()));
61 kdm_strings.push_back (kdm);
63 struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
65 throw runtime_error ("could not create ZIP source");
68 name_values['s'] = i.screen->name;
69 name_values['i'] = i.kdm.id ();
70 string const name = careful_string_filter(name_format.get(name_values, ".xml"));
71 if (zip_add (zip, name.c_str(), source) == -1) {
72 throw runtime_error ("failed to add KDM to ZIP archive");
76 if (zip_close (zip) == -1) {
79 zip_error_get (zip, &zep, &sep);
80 throw runtime_error (String::compose("failed to close ZIP archive (%1, %2)", zep, sep));
84 /** Collect a list of ScreenKDMs into a list of CinemaKDMs so that each
85 * CinemaKDM contains the KDMs for its cinema.
88 CinemaKDMs::collect (list<ScreenKDM> screen_kdms)
90 list<CinemaKDMs> cinema_kdms;
92 while (!screen_kdms.empty ()) {
94 /* Get all the screens from a single cinema */
98 list<ScreenKDM>::iterator i = screen_kdms.begin ();
99 ck.cinema = i->screen->cinema;
100 ck.screen_kdms.push_back (*i);
101 list<ScreenKDM>::iterator j = i;
103 screen_kdms.remove (*j);
105 while (i != screen_kdms.end ()) {
106 if (i->screen->cinema == ck.cinema) {
107 ck.screen_kdms.push_back (*i);
108 list<ScreenKDM>::iterator j = i;
110 screen_kdms.remove (*j);
116 cinema_kdms.push_back (ck);
122 /** Write one directory per cinema into another directory */
124 CinemaKDMs::write_directories (
125 list<CinemaKDMs> cinema_kdms,
126 boost::filesystem::path directory,
127 dcp::NameFormat container_name_format,
128 dcp::NameFormat filename_format,
129 dcp::NameFormat::Map name_values,
130 function<bool (boost::filesystem::path)> confirm_overwrite
133 /* No specific screen */
134 name_values['s'] = "";
138 BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
139 boost::filesystem::path path = directory;
140 name_values['c'] = i.cinema->name;
141 path /= container_name_format.get(name_values, "");
142 if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
143 boost::filesystem::create_directories (path);
144 ScreenKDM::write_files (i.screen_kdms, path, filename_format, name_values, confirm_overwrite);
146 written += i.screen_kdms.size();
152 /** Write one ZIP file per cinema into a directory */
154 CinemaKDMs::write_zip_files (
155 list<CinemaKDMs> cinema_kdms,
156 boost::filesystem::path directory,
157 dcp::NameFormat container_name_format,
158 dcp::NameFormat filename_format,
159 dcp::NameFormat::Map name_values,
160 function<bool (boost::filesystem::path)> confirm_overwrite
163 /* No specific screen */
164 name_values['s'] = "";
168 BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
169 boost::filesystem::path path = directory;
170 name_values['c'] = i.cinema->name;
171 path /= container_name_format.get(name_values, ".zip");
172 if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
173 if (boost::filesystem::exists (path)) {
174 /* Creating a new zip file over an existing one is an error */
175 boost::filesystem::remove (path);
177 i.make_zip_file (path, filename_format, name_values);
178 written += i.screen_kdms.size();
185 /** Email one ZIP file per cinema to the cinema.
186 * @param cinema_kdms KDMS to email.
187 * @param container_name_format Format of folder / ZIP to use.
188 * @param filename_format Format of filenames to use.
189 * @param name_values Values to substitute into \p container_name_format and \p filename_format.
190 * @param cpl_name Name of the CPL that the KDMs are for.
194 list<CinemaKDMs> cinema_kdms,
195 dcp::NameFormat container_name_format,
196 dcp::NameFormat filename_format,
197 dcp::NameFormat::Map name_values,
201 Config* config = Config::instance ();
203 if (config->mail_server().empty()) {
204 throw NetworkError (_("No mail server configured in preferences"));
207 /* No specific screen */
208 name_values['s'] = "";
210 BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
212 if (i.cinema->emails.empty()) {
216 name_values['c'] = i.cinema->name;
218 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
219 boost::filesystem::create_directories (zip_file);
220 zip_file /= container_name_format.get(name_values, ".zip");
221 i.make_zip_file (zip_file, filename_format, name_values);
223 string subject = config->kdm_subject();
224 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
225 boost::algorithm::replace_all (subject, "$START_TIME", name_values['b']);
226 boost::algorithm::replace_all (subject, "$END_TIME", name_values['e']);
227 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i.cinema->name);
229 string body = config->kdm_email().c_str();
230 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_name);
231 boost::algorithm::replace_all (body, "$START_TIME", name_values['b']);
232 boost::algorithm::replace_all (body, "$END_TIME", name_values['e']);
233 boost::algorithm::replace_all (body, "$CINEMA_NAME", i.cinema->name);
236 BOOST_FOREACH (ScreenKDM const & j, i.screen_kdms) {
237 screens += j.screen->name + ", ";
239 boost::algorithm::replace_all (body, "$SCREENS", screens.substr (0, screens.length() - 2));
241 Emailer email (config->kdm_from(), i.cinema->emails, subject, body);
243 BOOST_FOREACH (string i, config->kdm_cc()) {
246 if (!config->kdm_bcc().empty ()) {
247 email.add_bcc (config->kdm_bcc ());
250 email.add_attachment (zip_file, container_name_format.get(name_values, ".zip"), "application/zip");
252 Config* c = Config::instance ();
255 email.send (c->mail_server(), c->mail_port(), c->mail_protocol(), c->mail_user(), c->mail_password());
257 boost::filesystem::remove (zip_file);
258 dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
259 dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
260 dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
261 dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
265 boost::filesystem::remove (zip_file);
267 dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
268 dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
269 dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
270 dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);