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