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.
20 #include "exceptions.h"
21 #include "cinema_kdms.h"
27 #include "compose.hpp"
29 #include <boost/foreach.hpp>
36 using boost::shared_ptr;
39 CinemaKDMs::make_zip_file (string film_name, boost::filesystem::path zip_file) const
42 struct zip* zip = zip_open (zip_file.string().c_str(), ZIP_CREATE | ZIP_EXCL, &error);
44 if (error == ZIP_ER_EXISTS) {
45 throw FileError ("ZIP file already exists", zip_file);
47 throw FileError ("could not create ZIP file", zip_file);
50 list<shared_ptr<string> > kdm_strings;
52 BOOST_FOREACH (ScreenKDM const & i, screen_kdms) {
53 shared_ptr<string> kdm (new string (i.kdm.as_xml ()));
54 kdm_strings.push_back (kdm);
56 struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
58 throw StringError ("could not create ZIP source");
61 if (zip_add (zip, i.filename(film_name).c_str(), source) == -1) {
62 throw StringError ("failed to add KDM to ZIP archive");
66 if (zip_close (zip) == -1) {
67 throw StringError ("failed to close ZIP archive");
72 CinemaKDMs::collect (list<ScreenKDM> screen_kdms)
74 list<CinemaKDMs> cinema_kdms;
76 while (!screen_kdms.empty ()) {
78 /* Get all the screens from a single cinema */
82 list<ScreenKDM>::iterator i = screen_kdms.begin ();
83 ck.cinema = i->screen->cinema;
84 ck.screen_kdms.push_back (*i);
85 list<ScreenKDM>::iterator j = i;
87 screen_kdms.remove (*j);
89 while (i != screen_kdms.end ()) {
90 if (i->screen->cinema == ck.cinema) {
91 ck.screen_kdms.push_back (*i);
92 list<ScreenKDM>::iterator j = i;
94 screen_kdms.remove (*j);
100 cinema_kdms.push_back (ck);
107 CinemaKDMs::write_zip_files (string film_name, list<CinemaKDMs> cinema_kdms, boost::filesystem::path directory)
109 BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
110 boost::filesystem::path path = directory;
111 path /= tidy_for_filename (i.cinema->name) + ".zip";
112 i.make_zip_file (film_name, path);
116 /* XXX: should probably get from/to from the KDMs themselves */
118 CinemaKDMs::email (string film_name, string cpl_name, list<CinemaKDMs> cinema_kdms, dcp::LocalTime from, dcp::LocalTime to, shared_ptr<Job> job)
120 Config* config = Config::instance ();
122 BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
124 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path ();
125 zip_file /= boost::filesystem::unique_path().string() + ".zip";
126 i.make_zip_file (film_name, zip_file);
128 string subject = config->kdm_subject();
129 SafeStringStream start;
130 start << from.date() << " " << from.time_of_day();
131 SafeStringStream end;
132 end << to.date() << " " << to.time_of_day();
133 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
134 boost::algorithm::replace_all (subject, "$START_TIME", start.str ());
135 boost::algorithm::replace_all (subject, "$END_TIME", end.str ());
136 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i.cinema->name);
138 string body = config->kdm_email().c_str();
139 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_name);
140 boost::algorithm::replace_all (body, "$START_TIME", start.str ());
141 boost::algorithm::replace_all (body, "$END_TIME", end.str ());
142 boost::algorithm::replace_all (body, "$CINEMA_NAME", i.cinema->name);
144 SafeStringStream screens;
145 BOOST_FOREACH (ScreenKDM const & j, i.screen_kdms) {
146 screens << j.screen->name << ", ";
148 boost::algorithm::replace_all (body, "$SCREENS", screens.str().substr (0, screens.str().length() - 2));
150 Emailer email (config->kdm_from(), i.cinema->email, subject, body);
152 if (!config->kdm_cc().empty ()) {
153 email.add_cc (config->kdm_cc ());
155 if (!config->kdm_bcc().empty ()) {
156 email.add_bcc (config->kdm_bcc ());
159 string const name = tidy_for_filename(i.cinema->name) + "_" + tidy_for_filename(film_name) + ".zip";
160 email.add_attachment (zip_file, name, "application/zip");