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