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>
38 using std::runtime_error;
39 using boost::shared_ptr;
42 CinemaKDMs::make_zip_file (string film_name, boost::filesystem::path zip_file) const
45 struct zip* zip = zip_open (zip_file.string().c_str(), ZIP_CREATE | ZIP_EXCL, &error);
47 if (error == ZIP_ER_EXISTS) {
48 throw FileError ("ZIP file already exists", zip_file);
50 throw FileError ("could not create ZIP file", zip_file);
53 list<shared_ptr<string> > kdm_strings;
55 BOOST_FOREACH (ScreenKDM const & i, screen_kdms) {
56 shared_ptr<string> kdm (new string (i.kdm.as_xml ()));
57 kdm_strings.push_back (kdm);
59 struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
61 throw runtime_error ("could not create ZIP source");
64 if (zip_add (zip, i.filename(film_name).c_str(), source) == -1) {
65 throw runtime_error ("failed to add KDM to ZIP archive");
69 if (zip_close (zip) == -1) {
70 throw runtime_error ("failed to close ZIP archive");
75 CinemaKDMs::collect (list<ScreenKDM> screen_kdms)
77 list<CinemaKDMs> cinema_kdms;
79 while (!screen_kdms.empty ()) {
81 /* Get all the screens from a single cinema */
85 list<ScreenKDM>::iterator i = screen_kdms.begin ();
86 ck.cinema = i->screen->cinema;
87 ck.screen_kdms.push_back (*i);
88 list<ScreenKDM>::iterator j = i;
90 screen_kdms.remove (*j);
92 while (i != screen_kdms.end ()) {
93 if (i->screen->cinema == ck.cinema) {
94 ck.screen_kdms.push_back (*i);
95 list<ScreenKDM>::iterator j = i;
97 screen_kdms.remove (*j);
103 cinema_kdms.push_back (ck);
110 CinemaKDMs::write_zip_files (string film_name, list<CinemaKDMs> cinema_kdms, boost::filesystem::path directory)
112 BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
113 boost::filesystem::path path = directory;
114 path /= tidy_for_filename (i.cinema->name) + ".zip";
115 i.make_zip_file (film_name, path);
119 /** @param log Log to write email session transcript to, or 0 */
120 /* XXX: should probably get from/to from the KDMs themselves */
123 string film_name, string cpl_name, list<CinemaKDMs> cinema_kdms, dcp::LocalTime from, dcp::LocalTime to, shared_ptr<Log> log
126 Config* config = Config::instance ();
128 if (config->mail_server().empty()) {
129 throw NetworkError (_("No mail server configured in preferences"));
132 BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
134 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path ();
135 zip_file /= boost::filesystem::unique_path().string() + ".zip";
136 i.make_zip_file (film_name, zip_file);
138 string subject = config->kdm_subject();
139 SafeStringStream start;
140 start << from.date() << " " << from.time_of_day();
141 SafeStringStream end;
142 end << to.date() << " " << to.time_of_day();
143 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
144 boost::algorithm::replace_all (subject, "$START_TIME", start.str ());
145 boost::algorithm::replace_all (subject, "$END_TIME", end.str ());
146 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i.cinema->name);
148 string body = config->kdm_email().c_str();
149 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_name);
150 boost::algorithm::replace_all (body, "$START_TIME", start.str ());
151 boost::algorithm::replace_all (body, "$END_TIME", end.str ());
152 boost::algorithm::replace_all (body, "$CINEMA_NAME", i.cinema->name);
154 SafeStringStream screens;
155 BOOST_FOREACH (ScreenKDM const & j, i.screen_kdms) {
156 screens << j.screen->name << ", ";
158 boost::algorithm::replace_all (body, "$SCREENS", screens.str().substr (0, screens.str().length() - 2));
160 Emailer email (config->kdm_from(), i.cinema->emails, subject, body);
162 BOOST_FOREACH (string i, config->kdm_cc()) {
165 if (!config->kdm_bcc().empty ()) {
166 email.add_bcc (config->kdm_bcc ());
169 string const name = tidy_for_filename(i.cinema->name) + "_" + tidy_for_filename(film_name) + ".zip";
170 email.add_attachment (zip_file, name, "application/zip");
172 Config* c = Config::instance ();
175 email.send (c->mail_server(), c->mail_port(), c->mail_user(), c->mail_password());
178 log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
179 log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
180 log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
181 log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
187 log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
188 log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
189 log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
190 log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);