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