Make the KDM's id available to the filename format.
[dcpomatic.git] / src / lib / cinema_kdms.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "exceptions.h"
22 #include "cinema_kdms.h"
23 #include "cinema.h"
24 #include "screen.h"
25 #include "config.h"
26 #include "util.h"
27 #include "emailer.h"
28 #include "compose.hpp"
29 #include "log.h"
30 #include <zip.h>
31 #include <boost/foreach.hpp>
32
33 #include "i18n.h"
34
35 using std::list;
36 using std::cout;
37 using std::string;
38 using std::runtime_error;
39 using boost::shared_ptr;
40 using boost::function;
41
42 void
43 CinemaKDMs::make_zip_file (boost::filesystem::path zip_file, dcp::NameFormat name_format, dcp::NameFormat::Map name_values) const
44 {
45         int error;
46         struct zip* zip = zip_open (zip_file.string().c_str(), ZIP_CREATE | ZIP_EXCL, &error);
47         if (!zip) {
48                 if (error == ZIP_ER_EXISTS) {
49                         throw FileError ("ZIP file already exists", zip_file);
50                 }
51                 throw FileError ("could not create ZIP file", zip_file);
52         }
53
54         list<shared_ptr<string> > kdm_strings;
55
56         name_values['c'] = cinema->name;
57
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);
61
62                 struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
63                 if (!source) {
64                         throw runtime_error ("could not create ZIP source");
65                 }
66
67                 name_values['s'] = i.screen->name;
68                 name_values['i'] = i.kdm.id ();
69                 string const name = name_format.get(name_values, ".xml");
70                 if (zip_add (zip, name.c_str(), source) == -1) {
71                         throw runtime_error ("failed to add KDM to ZIP archive");
72                 }
73         }
74
75         if (zip_close (zip) == -1) {
76                 throw runtime_error ("failed to close ZIP archive");
77         }
78 }
79
80 /** Collect a list of ScreenKDMs into a list of CinemaKDMs so that each
81  *  CinemaKDM contains the KDMs for its cinema.
82  */
83 list<CinemaKDMs>
84 CinemaKDMs::collect (list<ScreenKDM> screen_kdms)
85 {
86         list<CinemaKDMs> cinema_kdms;
87
88         while (!screen_kdms.empty ()) {
89
90                 /* Get all the screens from a single cinema */
91
92                 CinemaKDMs ck;
93
94                 list<ScreenKDM>::iterator i = screen_kdms.begin ();
95                 ck.cinema = i->screen->cinema;
96                 ck.screen_kdms.push_back (*i);
97                 list<ScreenKDM>::iterator j = i;
98                 ++i;
99                 screen_kdms.remove (*j);
100
101                 while (i != screen_kdms.end ()) {
102                         if (i->screen->cinema == ck.cinema) {
103                                 ck.screen_kdms.push_back (*i);
104                                 list<ScreenKDM>::iterator j = i;
105                                 ++i;
106                                 screen_kdms.remove (*j);
107                         } else {
108                                 ++i;
109                         }
110                 }
111
112                 cinema_kdms.push_back (ck);
113         }
114
115         return cinema_kdms;
116 }
117
118 /** Write one directory per cinema into another directory */
119 int
120 CinemaKDMs::write_directories (
121         list<CinemaKDMs> cinema_kdms,
122         boost::filesystem::path directory,
123         dcp::NameFormat container_name_format,
124         dcp::NameFormat filename_format,
125         dcp::NameFormat::Map name_values,
126         function<bool (boost::filesystem::path)> confirm_overwrite
127         )
128 {
129         /* No specific screen */
130         name_values['s'] = "";
131
132         int written = 0;
133
134         if (!boost::filesystem::exists (directory)) {
135                 boost::filesystem::create_directories (directory);
136         }
137
138         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
139                 boost::filesystem::path path = directory;
140                 name_values['c'] = i.cinema->name;
141                 path /= container_name_format.get(name_values, "");
142                 ScreenKDM::write_files (i.screen_kdms, path, filename_format, name_values, confirm_overwrite);
143                 written += i.screen_kdms.size();
144         }
145
146         return written;
147 }
148
149 /** Write one ZIP file per cinema into a directory */
150 int
151 CinemaKDMs::write_zip_files (
152         list<CinemaKDMs> cinema_kdms,
153         boost::filesystem::path directory,
154         dcp::NameFormat container_name_format,
155         dcp::NameFormat filename_format,
156         dcp::NameFormat::Map name_values,
157         function<bool (boost::filesystem::path)> confirm_overwrite
158         )
159 {
160         /* No specific screen */
161         name_values['s'] = "";
162
163         int written = 0;
164
165         if (!boost::filesystem::exists (directory)) {
166                 boost::filesystem::create_directories (directory);
167         }
168
169         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
170                 boost::filesystem::path path = directory;
171                 name_values['c'] = i.cinema->name;
172                 path /= container_name_format.get(name_values, ".zip");
173                 if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
174                         if (boost::filesystem::exists (path)) {
175                                 /* Creating a new zip file over an existing one is an error */
176                                 boost::filesystem::remove (path);
177                         }
178                         i.make_zip_file (path, filename_format, name_values);
179                         written += i.screen_kdms.size();
180                 }
181         }
182
183         return written;
184 }
185
186 /** Email one ZIP file per cinema to the cinema.
187  *  @param cinema_kdms KDMS to email.
188  *  @param container_name_format Format of folder / ZIP to use.
189  *  @param filename_format Format of filenames to use.
190  *  @param name_values Values to substitute into \p container_name_format and \p filename_format.
191  *  @param cpl_name Name of the CPL that the KDMs are for.
192  *  @param log Log to write email session transcript to, or 0.
193  */
194 void
195 CinemaKDMs::email (
196         list<CinemaKDMs> cinema_kdms,
197         dcp::NameFormat container_name_format,
198         dcp::NameFormat filename_format,
199         dcp::NameFormat::Map name_values,
200         string cpl_name,
201         shared_ptr<Log> log
202         )
203 {
204         Config* config = Config::instance ();
205
206         if (config->mail_server().empty()) {
207                 throw NetworkError (_("No mail server configured in preferences"));
208         }
209
210         /* No specific screen */
211         name_values['s'] = "";
212
213         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
214
215                 name_values['c'] = i.cinema->name;
216
217                 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
218                 boost::filesystem::create_directories (zip_file);
219                 zip_file /= container_name_format.get(name_values, ".zip");
220                 i.make_zip_file (zip_file, filename_format, name_values);
221
222                 string subject = config->kdm_subject();
223                 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
224                 boost::algorithm::replace_all (subject, "$START_TIME", name_values['b']);
225                 boost::algorithm::replace_all (subject, "$END_TIME", name_values['e']);
226                 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i.cinema->name);
227
228                 string body = config->kdm_email().c_str();
229                 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_name);
230                 boost::algorithm::replace_all (body, "$START_TIME", name_values['b']);
231                 boost::algorithm::replace_all (body, "$END_TIME", name_values['e']);
232                 boost::algorithm::replace_all (body, "$CINEMA_NAME", i.cinema->name);
233
234                 string screens;
235                 BOOST_FOREACH (ScreenKDM const & j, i.screen_kdms) {
236                         screens += j.screen->name + ", ";
237                 }
238                 boost::algorithm::replace_all (body, "$SCREENS", screens.substr (0, screens.length() - 2));
239
240                 Emailer email (config->kdm_from(), i.cinema->emails, subject, body);
241
242                 BOOST_FOREACH (string i, config->kdm_cc()) {
243                         email.add_cc (i);
244                 }
245                 if (!config->kdm_bcc().empty ()) {
246                         email.add_bcc (config->kdm_bcc ());
247                 }
248
249                 email.add_attachment (zip_file, container_name_format.get(name_values, ".zip"), "application/zip");
250
251                 Config* c = Config::instance ();
252
253                 try {
254                         email.send (c->mail_server(), c->mail_port(), c->mail_user(), c->mail_password());
255                 } catch (...) {
256                         boost::filesystem::remove (zip_file);
257                         if (log) {
258                                 log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
259                                 log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
260                                 log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
261                                 log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
262                         }
263                         throw;
264                 }
265
266                 boost::filesystem::remove (zip_file);
267
268                 if (log) {
269                         log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
270                         log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
271                         log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
272                         log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
273                 }
274         }
275 }