2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "exceptions.h"
27 #include "safe_stringstream.h"
28 #include "quickmail.h"
29 #include "compose.hpp"
31 #include <dcp/encrypted_kdm.h>
32 #include <dcp/types.h>
33 #include <boost/shared_ptr.hpp>
40 using boost::shared_ptr;
44 ScreenKDM (shared_ptr<Screen> s, dcp::EncryptedKDM k)
49 shared_ptr<Screen> screen;
50 dcp::EncryptedKDM kdm;
54 kdm_filename (shared_ptr<const Film> film, ScreenKDM kdm)
56 return tidy_for_filename (film->name()) + "_" + tidy_for_filename (kdm.screen->cinema->name) + "_" + tidy_for_filename (kdm.screen->name) + ".kdm.xml";
61 shared_ptr<Cinema> cinema;
62 list<ScreenKDM> screen_kdms;
64 void make_zip_file (shared_ptr<const Film> film, boost::filesystem::path zip_file) const
67 struct zip* zip = zip_open (zip_file.string().c_str(), ZIP_CREATE | ZIP_EXCL, &error);
69 if (error == ZIP_ER_EXISTS) {
70 throw FileError ("ZIP file already exists", zip_file);
72 throw FileError ("could not create ZIP file", zip_file);
75 list<shared_ptr<string> > kdm_strings;
77 for (list<ScreenKDM>::const_iterator i = screen_kdms.begin(); i != screen_kdms.end(); ++i) {
78 shared_ptr<string> kdm (new string (i->kdm.as_xml ()));
79 kdm_strings.push_back (kdm);
81 struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
83 throw StringError ("could not create ZIP source");
86 if (zip_add (zip, kdm_filename (film, *i).c_str(), source) == -1) {
87 throw StringError ("failed to add KDM to ZIP archive");
91 if (zip_close (zip) == -1) {
92 throw StringError ("failed to close ZIP archive");
97 /* Not complete but sufficient for our purposes (we're using
98 ScreenKDM in a list where all the screens will be unique).
101 operator== (ScreenKDM const & a, ScreenKDM const & b)
103 return a.screen == b.screen;
106 static list<ScreenKDM>
108 shared_ptr<const Film> film,
109 list<shared_ptr<Screen> > screens,
110 boost::filesystem::path cpl,
113 dcp::Formulation formulation
116 list<dcp::EncryptedKDM> kdms = film->make_kdms (screens, cpl, from, to, formulation);
118 list<ScreenKDM> screen_kdms;
120 list<shared_ptr<Screen> >::iterator i = screens.begin ();
121 list<dcp::EncryptedKDM>::iterator j = kdms.begin ();
122 while (i != screens.end() && j != kdms.end ()) {
123 screen_kdms.push_back (ScreenKDM (*i, *j));
131 static list<CinemaKDMs>
133 shared_ptr<const Film> film,
134 list<shared_ptr<Screen> > screens,
135 boost::filesystem::path cpl,
138 dcp::Formulation formulation
141 list<ScreenKDM> screen_kdms = make_screen_kdms (film, screens, cpl, from, to, formulation);
142 list<CinemaKDMs> cinema_kdms;
144 while (!screen_kdms.empty ()) {
146 /* Get all the screens from a single cinema */
150 list<ScreenKDM>::iterator i = screen_kdms.begin ();
151 ck.cinema = i->screen->cinema;
152 ck.screen_kdms.push_back (*i);
153 list<ScreenKDM>::iterator j = i;
155 screen_kdms.remove (*j);
157 while (i != screen_kdms.end ()) {
158 if (i->screen->cinema == ck.cinema) {
159 ck.screen_kdms.push_back (*i);
160 list<ScreenKDM>::iterator j = i;
162 screen_kdms.remove (*j);
168 cinema_kdms.push_back (ck);
174 /** @param from KDM from time in local time.
175 * @param to KDM to time in local time.
179 shared_ptr<const Film> film,
180 list<shared_ptr<Screen> > screens,
181 boost::filesystem::path cpl,
184 dcp::Formulation formulation,
185 boost::filesystem::path directory
188 list<ScreenKDM> screen_kdms = make_screen_kdms (film, screens, cpl, from, to, formulation);
190 /* Write KDMs to the specified directory */
191 for (list<ScreenKDM>::iterator i = screen_kdms.begin(); i != screen_kdms.end(); ++i) {
192 boost::filesystem::path out = directory;
193 out /= kdm_filename (film, *i);
199 write_kdm_zip_files (
200 shared_ptr<const Film> film,
201 list<shared_ptr<Screen> > screens,
202 boost::filesystem::path cpl,
205 dcp::Formulation formulation,
206 boost::filesystem::path directory
209 list<CinemaKDMs> cinema_kdms = make_cinema_kdms (film, screens, cpl, from, to, formulation);
211 for (list<CinemaKDMs>::const_iterator i = cinema_kdms.begin(); i != cinema_kdms.end(); ++i) {
212 boost::filesystem::path path = directory;
213 path /= tidy_for_filename (i->cinema->name) + ".zip";
214 i->make_zip_file (film, path);
220 shared_ptr<const Film> film,
221 list<shared_ptr<Screen> > screens,
222 boost::filesystem::path cpl,
225 dcp::Formulation formulation
228 list<CinemaKDMs> cinema_kdms = make_cinema_kdms (film, screens, cpl, from, to, formulation);
230 for (list<CinemaKDMs>::const_iterator i = cinema_kdms.begin(); i != cinema_kdms.end(); ++i) {
232 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path ();
233 zip_file /= boost::filesystem::unique_path().string() + ".zip";
234 i->make_zip_file (film, zip_file);
238 quickmail_initialize ();
240 SafeStringStream start;
241 start << from.date() << " " << from.time_of_day();
242 SafeStringStream end;
243 end << to.date() << " " << to.time_of_day();
245 string subject = Config::instance()->kdm_subject();
246 boost::algorithm::replace_all (subject, "$CPL_NAME", film->dcp_name ());
247 boost::algorithm::replace_all (subject, "$START_TIME", start.str ());
248 boost::algorithm::replace_all (subject, "$END_TIME", end.str ());
249 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i->cinema->name);
250 quickmail mail = quickmail_create (Config::instance()->kdm_from().c_str(), subject.c_str ());
252 quickmail_add_to (mail, i->cinema->email.c_str ());
253 if (!Config::instance()->kdm_cc().empty ()) {
254 quickmail_add_cc (mail, Config::instance()->kdm_cc().c_str ());
256 if (!Config::instance()->kdm_bcc().empty ()) {
257 quickmail_add_bcc (mail, Config::instance()->kdm_bcc().c_str ());
260 string body = Config::instance()->kdm_email().c_str();
261 boost::algorithm::replace_all (body, "$CPL_NAME", film->dcp_name ());
262 boost::algorithm::replace_all (body, "$START_TIME", start.str ());
263 boost::algorithm::replace_all (body, "$END_TIME", end.str ());
264 boost::algorithm::replace_all (body, "$CINEMA_NAME", i->cinema->name);
266 SafeStringStream screens;
267 for (list<ScreenKDM>::const_iterator j = i->screen_kdms.begin(); j != i->screen_kdms.end(); ++j) {
268 screens << j->screen->name << ", ";
270 boost::algorithm::replace_all (body, "$SCREENS", screens.str().substr (0, screens.str().length() - 2));
272 quickmail_set_body (mail, body.c_str());
273 quickmail_add_attachment_file (mail, zip_file.string().c_str(), "application/zip");
275 char const* error = quickmail_send (
277 Config::instance()->mail_server().c_str(),
278 Config::instance()->mail_port(),
279 Config::instance()->mail_user().c_str(),
280 Config::instance()->mail_password().c_str()
284 quickmail_destroy (mail);
287 "Failed to send KDM email to %1 (%2)",
288 Config::instance()->mail_server(),
293 quickmail_destroy (mail);