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