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