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>
32 #include <boost/make_shared.hpp>
39 using std::runtime_error;
40 using boost::shared_ptr;
41 using boost::make_shared;
44 CinemaKDMs::make_zip_file (string film_name, boost::filesystem::path zip_file) 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 BOOST_FOREACH (ScreenKDM const & i, screen_kdms) {
58 shared_ptr<string> kdm = boost::make_shared<string> (i.kdm.as_xml ());
59 kdm_strings.push_back (kdm);
61 struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
63 throw runtime_error ("could not create ZIP source");
66 if (zip_add (zip, i.filename(film_name).c_str(), source) == -1) {
67 throw runtime_error ("failed to add KDM to ZIP archive");
71 if (zip_close (zip) == -1) {
72 throw runtime_error ("failed to close ZIP archive");
77 CinemaKDMs::collect (list<ScreenKDM> screen_kdms)
79 list<CinemaKDMs> cinema_kdms;
81 while (!screen_kdms.empty ()) {
83 /* Get all the screens from a single cinema */
87 list<ScreenKDM>::iterator i = screen_kdms.begin ();
88 ck.cinema = i->screen->cinema;
89 ck.screen_kdms.push_back (*i);
90 list<ScreenKDM>::iterator j = i;
92 screen_kdms.remove (*j);
94 while (i != screen_kdms.end ()) {
95 if (i->screen->cinema == ck.cinema) {
96 ck.screen_kdms.push_back (*i);
97 list<ScreenKDM>::iterator j = i;
99 screen_kdms.remove (*j);
105 cinema_kdms.push_back (ck);
112 CinemaKDMs::write_zip_files (string film_name, list<CinemaKDMs> cinema_kdms, boost::filesystem::path directory)
114 BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
115 boost::filesystem::path path = directory;
116 path /= tidy_for_filename (i.cinema->name) + ".zip";
117 i.make_zip_file (film_name, path);
121 /** @param log Log to write email session transcript to, or 0 */
122 /* XXX: should probably get from/to from the KDMs themselves */
125 string film_name, string cpl_name, list<CinemaKDMs> cinema_kdms, dcp::LocalTime from, dcp::LocalTime to, shared_ptr<Log> log
128 Config* config = Config::instance ();
130 if (config->mail_server().empty()) {
131 throw NetworkError (_("No mail server configured in preferences"));
134 BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
136 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path ();
137 zip_file /= boost::filesystem::unique_path().string() + ".zip";
138 i.make_zip_file (film_name, zip_file);
140 string subject = config->kdm_subject();
141 SafeStringStream start;
142 start << from.date() << " " << from.time_of_day();
143 SafeStringStream end;
144 end << to.date() << " " << to.time_of_day();
145 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
146 boost::algorithm::replace_all (subject, "$START_TIME", start.str ());
147 boost::algorithm::replace_all (subject, "$END_TIME", end.str ());
148 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i.cinema->name);
150 string body = config->kdm_email().c_str();
151 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_name);
152 boost::algorithm::replace_all (body, "$START_TIME", start.str ());
153 boost::algorithm::replace_all (body, "$END_TIME", end.str ());
154 boost::algorithm::replace_all (body, "$CINEMA_NAME", i.cinema->name);
156 SafeStringStream screens;
157 BOOST_FOREACH (ScreenKDM const & j, i.screen_kdms) {
158 screens << j.screen->name << ", ";
160 boost::algorithm::replace_all (body, "$SCREENS", screens.str().substr (0, screens.str().length() - 2));
162 Emailer email (config->kdm_from(), i.cinema->emails, subject, body);
164 BOOST_FOREACH (string i, config->kdm_cc()) {
167 if (!config->kdm_bcc().empty ()) {
168 email.add_bcc (config->kdm_bcc ());
171 string const name = tidy_for_filename(i.cinema->name) + "_" + tidy_for_filename(film_name) + ".zip";
172 email.add_attachment (zip_file, name, "application/zip");
174 Config* c = Config::instance ();
177 email.send (c->mail_server(), c->mail_port(), c->mail_user(), c->mail_password());
180 log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
181 log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
182 log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
183 log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
189 log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
190 log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
191 log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
192 log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);