2 Copyright (C) 2013 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.
21 #include <boost/shared_ptr.hpp>
22 #include <quickmail.h>
24 #include <libdcp/kdm.h>
27 #include "exceptions.h"
34 using std::stringstream;
36 using boost::shared_ptr;
40 ScreenKDM (shared_ptr<Screen> s, libdcp::KDM k)
45 shared_ptr<Screen> screen;
50 kdm_filename (shared_ptr<const Film> film, ScreenKDM kdm)
52 return tidy_for_filename (film->name()) + "_" + tidy_for_filename (kdm.screen->cinema->name) + "_" + tidy_for_filename (kdm.screen->name) + ".kdm.xml";
57 shared_ptr<Cinema> cinema;
58 list<ScreenKDM> screen_kdms;
60 void make_zip_file (shared_ptr<const Film> film, boost::filesystem::path zip_file) const
63 struct zip* zip = zip_open (zip_file.string().c_str(), ZIP_CREATE | ZIP_EXCL, &error);
65 if (error == ZIP_ER_EXISTS) {
66 throw FileError ("ZIP file already exists", zip_file);
68 throw FileError ("could not create ZIP file", zip_file);
71 list<shared_ptr<string> > kdm_strings;
73 for (list<ScreenKDM>::const_iterator i = screen_kdms.begin(); i != screen_kdms.end(); ++i) {
74 shared_ptr<string> kdm (new string (i->kdm.as_xml ()));
75 kdm_strings.push_back (kdm);
77 struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
79 throw StringError ("could not create ZIP source");
82 if (zip_add (zip, kdm_filename (film, *i).c_str(), source) == -1) {
83 throw StringError ("failed to add KDM to ZIP archive");
87 if (zip_close (zip) == -1) {
88 throw StringError ("failed to close ZIP archive");
93 /* Not complete but sufficient for our purposes (we're using
94 ScreenKDM in a list where all the screens will be unique).
97 operator== (ScreenKDM const & a, ScreenKDM const & b)
99 return a.screen == b.screen;
102 static list<ScreenKDM>
104 shared_ptr<const Film> film,
105 list<shared_ptr<Screen> > screens,
106 boost::filesystem::path cpl,
107 boost::posix_time::ptime from,
108 boost::posix_time::ptime to,
109 libdcp::KDM::Formulation formulation
112 list<libdcp::KDM> kdms = film->make_kdms (screens, cpl, from, to, formulation);
114 list<ScreenKDM> screen_kdms;
116 list<shared_ptr<Screen> >::iterator i = screens.begin ();
117 list<libdcp::KDM>::iterator j = kdms.begin ();
118 while (i != screens.end() && j != kdms.end ()) {
119 screen_kdms.push_back (ScreenKDM (*i, *j));
127 static list<CinemaKDMs>
129 shared_ptr<const Film> film,
130 list<shared_ptr<Screen> > screens,
131 boost::filesystem::path cpl,
132 boost::posix_time::ptime from,
133 boost::posix_time::ptime to,
134 libdcp::KDM::Formulation formulation
137 list<ScreenKDM> screen_kdms = make_screen_kdms (film, screens, cpl, from, to, formulation);
138 list<CinemaKDMs> cinema_kdms;
140 while (!screen_kdms.empty ()) {
142 /* Get all the screens from a single cinema */
146 list<ScreenKDM>::iterator i = screen_kdms.begin ();
147 ck.cinema = i->screen->cinema;
148 ck.screen_kdms.push_back (*i);
149 list<ScreenKDM>::iterator j = i;
151 screen_kdms.remove (*j);
153 while (i != screen_kdms.end ()) {
154 if (i->screen->cinema == ck.cinema) {
155 ck.screen_kdms.push_back (*i);
156 list<ScreenKDM>::iterator j = i;
158 screen_kdms.remove (*j);
164 cinema_kdms.push_back (ck);
170 /** @param from KDM from time in local time.
171 * @param to KDM to time in local time.
175 shared_ptr<const Film> film,
176 list<shared_ptr<Screen> > screens,
177 boost::filesystem::path cpl,
178 boost::posix_time::ptime from,
179 boost::posix_time::ptime to,
180 libdcp::KDM::Formulation formulation,
181 boost::filesystem::path directory
184 list<ScreenKDM> screen_kdms = make_screen_kdms (film, screens, cpl, from, to, formulation);
186 /* Write KDMs to the specified directory */
187 for (list<ScreenKDM>::iterator i = screen_kdms.begin(); i != screen_kdms.end(); ++i) {
188 boost::filesystem::path out = directory;
189 out /= kdm_filename (film, *i);
195 write_kdm_zip_files (
196 shared_ptr<const Film> film,
197 list<shared_ptr<Screen> > screens,
198 boost::filesystem::path cpl,
199 boost::posix_time::ptime from,
200 boost::posix_time::ptime to,
201 libdcp::KDM::Formulation formulation,
202 boost::filesystem::path directory
205 list<CinemaKDMs> cinema_kdms = make_cinema_kdms (film, screens, cpl, from, to, formulation);
207 for (list<CinemaKDMs>::const_iterator i = cinema_kdms.begin(); i != cinema_kdms.end(); ++i) {
208 boost::filesystem::path path = directory;
209 path /= tidy_for_filename (i->cinema->name) + ".zip";
210 i->make_zip_file (film, path);
216 shared_ptr<const Film> film,
217 list<shared_ptr<Screen> > screens,
218 boost::filesystem::path cpl,
219 boost::posix_time::ptime from,
220 boost::posix_time::ptime to,
221 libdcp::KDM::Formulation formulation
224 list<CinemaKDMs> cinema_kdms = make_cinema_kdms (film, screens, cpl, from, to, formulation);
226 for (list<CinemaKDMs>::const_iterator i = cinema_kdms.begin(); i != cinema_kdms.end(); ++i) {
228 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path ();
229 zip_file /= boost::filesystem::unique_path().string() + ".zip";
230 i->make_zip_file (film, zip_file);
234 quickmail_initialize ();
237 start << from.date() << " " << from.time_of_day();
239 end << to.date() << " " << to.time_of_day();
241 string subject = Config::instance()->kdm_subject();
242 boost::algorithm::replace_all (subject, "$CPL_NAME", film->dcp_name ());
243 boost::algorithm::replace_all (subject, "$START_TIME", start.str ());
244 boost::algorithm::replace_all (subject, "$END_TIME", end.str ());
245 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i->cinema->name);
246 quickmail mail = quickmail_create (Config::instance()->kdm_from().c_str(), subject.c_str ());
248 quickmail_add_to (mail, i->cinema->email.c_str ());
249 if (!Config::instance()->kdm_cc().empty ()) {
250 quickmail_add_cc (mail, Config::instance()->kdm_cc().c_str ());
252 if (!Config::instance()->kdm_bcc().empty ()) {
253 quickmail_add_bcc (mail, Config::instance()->kdm_bcc().c_str ());
256 string body = Config::instance()->kdm_email().c_str();
257 boost::algorithm::replace_all (body, "$CPL_NAME", film->dcp_name ());
258 boost::algorithm::replace_all (body, "$START_TIME", start.str ());
259 boost::algorithm::replace_all (body, "$END_TIME", end.str ());
260 boost::algorithm::replace_all (body, "$CINEMA_NAME", i->cinema->name);
262 stringstream screens;
263 for (list<ScreenKDM>::const_iterator j = i->screen_kdms.begin(); j != i->screen_kdms.end(); ++j) {
264 screens << j->screen->name << ", ";
266 boost::algorithm::replace_all (body, "$SCREENS", screens.str().substr (0, screens.str().length() - 2));
268 quickmail_set_body (mail, body.c_str());
269 quickmail_add_attachment_file (mail, zip_file.string().c_str(), "application/zip");
271 int const port = Config::instance()->mail_user().empty() ? 25 : 587;
273 char const* error = quickmail_send (
275 Config::instance()->mail_server().c_str(),
277 Config::instance()->mail_user().c_str(),
278 Config::instance()->mail_password().c_str()
282 quickmail_destroy (mail);
283 throw KDMError (String::compose ("Failed to send KDM email (%1)", error));
285 quickmail_destroy (mail);