32879cf6b5763355378e6ddc58432b8095d8ef52
[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 (shared_ptr<ScreenKDM> 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                 throw runtime_error ("failed to close ZIP archive");
78         }
79 }
80
81 /** Collect a list of ScreenKDMs into a list of CinemaKDMs so that each
82  *  CinemaKDM contains the KDMs for its cinema.
83  */
84 list<CinemaKDMs>
85 CinemaKDMs::collect (list<shared_ptr<ScreenKDM> > screen_kdms)
86 {
87         list<CinemaKDMs> cinema_kdms;
88
89         while (!screen_kdms.empty ()) {
90
91                 /* Get all the screens from a single cinema */
92
93                 CinemaKDMs ck;
94
95                 list<shared_ptr<ScreenKDM> >::iterator i = screen_kdms.begin ();
96                 ck.cinema = (*i)->screen->cinema;
97                 ck.screen_kdms.push_back (*i);
98                 list<shared_ptr<ScreenKDM> >::iterator j = i;
99                 ++i;
100                 screen_kdms.remove (*j);
101
102                 while (i != screen_kdms.end ()) {
103                         if ((*i)->screen->cinema == ck.cinema) {
104                                 ck.screen_kdms.push_back (*i);
105                                 list<shared_ptr<ScreenKDM> >::iterator j = i;
106                                 ++i;
107                                 screen_kdms.remove (*j);
108                         } else {
109                                 ++i;
110                         }
111                 }
112
113                 cinema_kdms.push_back (ck);
114         }
115
116         return cinema_kdms;
117 }
118
119 /** Write one directory per cinema into another directory */
120 int
121 CinemaKDMs::write_directories (
122         list<CinemaKDMs> cinema_kdms,
123         boost::filesystem::path directory,
124         dcp::NameFormat container_name_format,
125         dcp::NameFormat filename_format,
126         dcp::NameFormat::Map name_values,
127         function<bool (boost::filesystem::path)> confirm_overwrite
128         )
129 {
130         /* No specific screen */
131         name_values['s'] = "";
132
133         int written = 0;
134
135         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
136                 boost::filesystem::path path = directory;
137                 name_values['c'] = i.cinema->name;
138                 path /= container_name_format.get(name_values, "");
139                 if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
140                         boost::filesystem::create_directories (path);
141                         ScreenKDM::write_files (i.screen_kdms, path, filename_format, name_values, confirm_overwrite);
142                 }
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         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
166                 boost::filesystem::path path = directory;
167                 name_values['c'] = i.cinema->name;
168                 path /= container_name_format.get(name_values, ".zip");
169                 if (!boost::filesystem::exists (path) || confirm_overwrite (path)) {
170                         if (boost::filesystem::exists (path)) {
171                                 /* Creating a new zip file over an existing one is an error */
172                                 boost::filesystem::remove (path);
173                         }
174                         i.make_zip_file (path, filename_format, name_values);
175                         written += i.screen_kdms.size();
176                 }
177         }
178
179         return written;
180 }
181
182 /** Email one ZIP file per cinema to the cinema.
183  *  @param cinema_kdms KDMS to email.
184  *  @param container_name_format Format of folder / ZIP to use.
185  *  @param filename_format Format of filenames to use.
186  *  @param name_values Values to substitute into \p container_name_format and \p filename_format.
187  *  @param cpl_name Name of the CPL that the KDMs are for.
188  */
189 void
190 CinemaKDMs::email (
191         list<CinemaKDMs> cinema_kdms,
192         dcp::NameFormat container_name_format,
193         dcp::NameFormat filename_format,
194         dcp::NameFormat::Map name_values,
195         string cpl_name
196         )
197 {
198         Config* config = Config::instance ();
199
200         if (config->mail_server().empty()) {
201                 throw NetworkError (_("No mail server configured in preferences"));
202         }
203
204         /* No specific screen */
205         name_values['s'] = "";
206
207         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
208
209                 if (i.cinema->emails.empty()) {
210                         continue;
211                 }
212
213                 name_values['c'] = i.cinema->name;
214
215                 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
216                 boost::filesystem::create_directories (zip_file);
217                 zip_file /= container_name_format.get(name_values, ".zip");
218                 i.make_zip_file (zip_file, filename_format, name_values);
219
220                 string subject = config->kdm_subject();
221                 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
222                 boost::algorithm::replace_all (subject, "$START_TIME", name_values['b']);
223                 boost::algorithm::replace_all (subject, "$END_TIME", name_values['e']);
224                 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i.cinema->name);
225
226                 string body = config->kdm_email().c_str();
227                 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_name);
228                 boost::algorithm::replace_all (body, "$START_TIME", name_values['b']);
229                 boost::algorithm::replace_all (body, "$END_TIME", name_values['e']);
230                 boost::algorithm::replace_all (body, "$CINEMA_NAME", i.cinema->name);
231
232                 string screens;
233                 BOOST_FOREACH (shared_ptr<ScreenKDM> j, i.screen_kdms) {
234                         screens += j->screen->name + ", ";
235                 }
236                 boost::algorithm::replace_all (body, "$SCREENS", screens.substr (0, screens.length() - 2));
237
238                 Emailer email (config->kdm_from(), i.cinema->emails, subject, body);
239
240                 BOOST_FOREACH (string i, config->kdm_cc()) {
241                         email.add_cc (i);
242                 }
243                 if (!config->kdm_bcc().empty ()) {
244                         email.add_bcc (config->kdm_bcc ());
245                 }
246
247                 email.add_attachment (zip_file, container_name_format.get(name_values, ".zip"), "application/zip");
248
249                 Config* c = Config::instance ();
250
251                 try {
252                         email.send (c->mail_server(), c->mail_port(), c->mail_protocol(), c->mail_user(), c->mail_password());
253                 } catch (...) {
254                         boost::filesystem::remove (zip_file);
255                         dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
256                         dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
257                         dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
258                         dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
259                         throw;
260                 }
261
262                 boost::filesystem::remove (zip_file);
263
264                 dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
265                 dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
266                 dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
267                 dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
268         }
269 }