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 "kdm_filename_format.h"
32 #include <boost/foreach.hpp>
39 using std::runtime_error;
40 using boost::shared_ptr;
43 CinemaKDMs::make_zip_file (boost::filesystem::path zip_file, KDMFilenameFormat name_format, dcp::NameFormat::Map name_values) const
46 struct zip* zip = zip_open (zip_file.string().c_str(), ZIP_CREATE | ZIP_EXCL, &error);
48 if (error == ZIP_ER_EXISTS) {
49 throw FileError ("ZIP file already exists", zip_file);
51 throw FileError ("could not create ZIP file", zip_file);
54 list<shared_ptr<string> > kdm_strings;
56 name_values['c'] = cinema->name;
58 BOOST_FOREACH (ScreenKDM const & i, screen_kdms) {
59 shared_ptr<string> kdm (new string (i.kdm.as_xml ()));
60 kdm_strings.push_back (kdm);
62 struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
64 throw runtime_error ("could not create ZIP source");
67 name_values['s'] = i.screen->name;
68 string const name = name_format.get(name_values) + ".xml";
69 if (zip_add (zip, name.c_str(), source) == -1) {
70 throw runtime_error ("failed to add KDM to ZIP archive");
74 if (zip_close (zip) == -1) {
75 throw runtime_error ("failed to close ZIP archive");
79 /** Collect a list of ScreenKDMs into a list of CinemaKDMs so that each
80 * CinemaKDM contains the KDMs for its cinema.
83 CinemaKDMs::collect (list<ScreenKDM> screen_kdms)
85 list<CinemaKDMs> cinema_kdms;
87 while (!screen_kdms.empty ()) {
89 /* Get all the screens from a single cinema */
93 list<ScreenKDM>::iterator i = screen_kdms.begin ();
94 ck.cinema = i->screen->cinema;
95 ck.screen_kdms.push_back (*i);
96 list<ScreenKDM>::iterator j = i;
98 screen_kdms.remove (*j);
100 while (i != screen_kdms.end ()) {
101 if (i->screen->cinema == ck.cinema) {
102 ck.screen_kdms.push_back (*i);
103 list<ScreenKDM>::iterator j = i;
105 screen_kdms.remove (*j);
111 cinema_kdms.push_back (ck);
117 /** Write one ZIP file per cinema into a directory */
119 CinemaKDMs::write_zip_files (
120 list<CinemaKDMs> cinema_kdms,
121 boost::filesystem::path directory,
122 KDMFilenameFormat name_format,
123 dcp::NameFormat::Map name_values
126 /* No specific screen */
127 name_values['s'] = "";
129 BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
130 boost::filesystem::path path = directory;
131 name_values['c'] = i.cinema->name;
132 path /= name_format.get(name_values) + ".zip";
133 i.make_zip_file (path, name_format, name_values);
137 /** Email one ZIP file per cinema to the cinema.
138 * @param log Log to write email session transcript to, or 0.
142 list<CinemaKDMs> cinema_kdms,
143 KDMFilenameFormat name_format,
144 dcp::NameFormat::Map name_values,
149 Config* config = Config::instance ();
151 if (config->mail_server().empty()) {
152 throw NetworkError (_("No mail server configured in preferences"));
155 /* No specific screen */
156 name_values['s'] = "";
158 BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
160 name_values['c'] = i.cinema->name;
162 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path ();
163 zip_file /= boost::filesystem::unique_path().string() + ".zip";
164 i.make_zip_file (zip_file, name_format, name_values);
166 string subject = config->kdm_subject();
167 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
168 boost::algorithm::replace_all (subject, "$START_TIME", name_values['f']);
169 boost::algorithm::replace_all (subject, "$END_TIME", name_values['t']);
170 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i.cinema->name);
172 string body = config->kdm_email().c_str();
173 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_name);
174 boost::algorithm::replace_all (body, "$START_TIME", name_values['f']);
175 boost::algorithm::replace_all (body, "$END_TIME", name_values['t']);
176 boost::algorithm::replace_all (body, "$CINEMA_NAME", i.cinema->name);
178 locked_stringstream screens;
179 BOOST_FOREACH (ScreenKDM const & j, i.screen_kdms) {
180 screens << j.screen->name << ", ";
182 boost::algorithm::replace_all (body, "$SCREENS", screens.str().substr (0, screens.str().length() - 2));
184 Emailer email (config->kdm_from(), i.cinema->emails, subject, body);
186 BOOST_FOREACH (string i, config->kdm_cc()) {
189 if (!config->kdm_bcc().empty ()) {
190 email.add_bcc (config->kdm_bcc ());
193 email.add_attachment (zip_file, name_format.get(name_values) + ".zip", "application/zip");
195 Config* c = Config::instance ();
198 email.send (c->mail_server(), c->mail_port(), c->mail_user(), c->mail_password());
201 log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
202 log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
203 log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
204 log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
210 log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
211 log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
212 log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
213 log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);