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