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