Shell of KDM GUI tool.
[dcpomatic.git] / src / lib / kdm.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "kdm.h"
21 #include "cinema.h"
22 #include "screen.h"
23 #include "exceptions.h"
24 #include "util.h"
25 #include "film.h"
26 #include "config.h"
27 #include "safe_stringstream.h"
28 #include "quickmail.h"
29 #include "compose.hpp"
30 #include <zip.h>
31 #include <dcp/encrypted_kdm.h>
32 #include <dcp/types.h>
33 #include <boost/shared_ptr.hpp>
34 #include <list>
35 #include <iostream>
36
37 using std::list;
38 using std::string;
39 using std::cout;
40 using boost::shared_ptr;
41
42 struct ScreenKDM
43 {
44         ScreenKDM (shared_ptr<Screen> s, dcp::EncryptedKDM k)
45                 : screen (s)
46                 , kdm (k)
47         {}
48
49         shared_ptr<Screen> screen;
50         dcp::EncryptedKDM kdm;
51 };
52
53 static string
54 kdm_filename (shared_ptr<const Film> film, ScreenKDM kdm)
55 {
56         return tidy_for_filename (film->name()) + "_" + tidy_for_filename (kdm.screen->cinema->name) + "_" + tidy_for_filename (kdm.screen->name) + ".kdm.xml";
57 }
58
59 struct CinemaKDMs
60 {
61         shared_ptr<Cinema> cinema;
62         list<ScreenKDM> screen_kdms;
63
64         void make_zip_file (shared_ptr<const Film> film, boost::filesystem::path zip_file) const
65         {
66                 int error;
67                 struct zip* zip = zip_open (zip_file.string().c_str(), ZIP_CREATE | ZIP_EXCL, &error);
68                 if (!zip) {
69                         if (error == ZIP_ER_EXISTS) {
70                                 throw FileError ("ZIP file already exists", zip_file);
71                         }
72                         throw FileError ("could not create ZIP file", zip_file);
73                 }
74
75                 list<shared_ptr<string> > kdm_strings;
76
77                 for (list<ScreenKDM>::const_iterator i = screen_kdms.begin(); i != screen_kdms.end(); ++i) {
78                         shared_ptr<string> kdm (new string (i->kdm.as_xml ()));
79                         kdm_strings.push_back (kdm);
80
81                         struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
82                         if (!source) {
83                                 throw StringError ("could not create ZIP source");
84                         }
85
86                         if (zip_add (zip, kdm_filename (film, *i).c_str(), source) == -1) {
87                                 throw StringError ("failed to add KDM to ZIP archive");
88                         }
89                 }
90
91                 if (zip_close (zip) == -1) {
92                         throw StringError ("failed to close ZIP archive");
93                 }
94         }
95 };
96
97 /* Not complete but sufficient for our purposes (we're using
98    ScreenKDM in a list where all the screens will be unique).
99 */
100 bool
101 operator== (ScreenKDM const & a, ScreenKDM const & b)
102 {
103         return a.screen == b.screen;
104 }
105
106 static list<ScreenKDM>
107 make_screen_kdms (
108         shared_ptr<const Film> film,
109         list<shared_ptr<Screen> > screens,
110         boost::filesystem::path cpl,
111         dcp::LocalTime from,
112         dcp::LocalTime to,
113         dcp::Formulation formulation
114         )
115 {
116         list<dcp::EncryptedKDM> kdms = film->make_kdms (screens, cpl, from, to, formulation);
117
118         list<ScreenKDM> screen_kdms;
119
120         list<shared_ptr<Screen> >::iterator i = screens.begin ();
121         list<dcp::EncryptedKDM>::iterator j = kdms.begin ();
122         while (i != screens.end() && j != kdms.end ()) {
123                 screen_kdms.push_back (ScreenKDM (*i, *j));
124                 ++i;
125                 ++j;
126         }
127
128         return screen_kdms;
129 }
130
131 static list<CinemaKDMs>
132 make_cinema_kdms (
133         shared_ptr<const Film> film,
134         list<shared_ptr<Screen> > screens,
135         boost::filesystem::path cpl,
136         dcp::LocalTime from,
137         dcp::LocalTime to,
138         dcp::Formulation formulation
139         )
140 {
141         list<ScreenKDM> screen_kdms = make_screen_kdms (film, screens, cpl, from, to, formulation);
142         list<CinemaKDMs> cinema_kdms;
143
144         while (!screen_kdms.empty ()) {
145
146                 /* Get all the screens from a single cinema */
147
148                 CinemaKDMs ck;
149
150                 list<ScreenKDM>::iterator i = screen_kdms.begin ();
151                 ck.cinema = i->screen->cinema;
152                 ck.screen_kdms.push_back (*i);
153                 list<ScreenKDM>::iterator j = i;
154                 ++i;
155                 screen_kdms.remove (*j);
156
157                 while (i != screen_kdms.end ()) {
158                         if (i->screen->cinema == ck.cinema) {
159                                 ck.screen_kdms.push_back (*i);
160                                 list<ScreenKDM>::iterator j = i;
161                                 ++i;
162                                 screen_kdms.remove (*j);
163                         } else {
164                                 ++i;
165                         }
166                 }
167
168                 cinema_kdms.push_back (ck);
169         }
170
171         return cinema_kdms;
172 }
173
174 /** @param from KDM from time in local time.
175  *  @param to KDM to time in local time.
176  */
177 void
178 write_kdm_files (
179         shared_ptr<const Film> film,
180         list<shared_ptr<Screen> > screens,
181         boost::filesystem::path cpl,
182         dcp::LocalTime from,
183         dcp::LocalTime to,
184         dcp::Formulation formulation,
185         boost::filesystem::path directory
186         )
187 {
188         list<ScreenKDM> screen_kdms = make_screen_kdms (film, screens, cpl, from, to, formulation);
189
190         /* Write KDMs to the specified directory */
191         for (list<ScreenKDM>::iterator i = screen_kdms.begin(); i != screen_kdms.end(); ++i) {
192                 boost::filesystem::path out = directory;
193                 out /= kdm_filename (film, *i);
194                 i->kdm.as_xml (out);
195         }
196 }
197
198 void
199 write_kdm_zip_files (
200         shared_ptr<const Film> film,
201         list<shared_ptr<Screen> > screens,
202         boost::filesystem::path cpl,
203         dcp::LocalTime from,
204         dcp::LocalTime to,
205         dcp::Formulation formulation,
206         boost::filesystem::path directory
207         )
208 {
209         list<CinemaKDMs> cinema_kdms = make_cinema_kdms (film, screens, cpl, from, to, formulation);
210
211         for (list<CinemaKDMs>::const_iterator i = cinema_kdms.begin(); i != cinema_kdms.end(); ++i) {
212                 boost::filesystem::path path = directory;
213                 path /= tidy_for_filename (i->cinema->name) + ".zip";
214                 i->make_zip_file (film, path);
215         }
216 }
217
218 void
219 email_kdms (
220         shared_ptr<const Film> film,
221         list<shared_ptr<Screen> > screens,
222         boost::filesystem::path cpl,
223         dcp::LocalTime from,
224         dcp::LocalTime to,
225         dcp::Formulation formulation
226         )
227 {
228         list<CinemaKDMs> cinema_kdms = make_cinema_kdms (film, screens, cpl, from, to, formulation);
229
230         for (list<CinemaKDMs>::const_iterator i = cinema_kdms.begin(); i != cinema_kdms.end(); ++i) {
231
232                 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path ();
233                 zip_file /= boost::filesystem::unique_path().string() + ".zip";
234                 i->make_zip_file (film, zip_file);
235
236                 /* Send email */
237
238                 quickmail_initialize ();
239
240                 SafeStringStream start;
241                 start << from.date() << " " << from.time_of_day();
242                 SafeStringStream end;
243                 end << to.date() << " " << to.time_of_day();
244
245                 string subject = Config::instance()->kdm_subject();
246                 boost::algorithm::replace_all (subject, "$CPL_NAME", film->dcp_name ());
247                 boost::algorithm::replace_all (subject, "$START_TIME", start.str ());
248                 boost::algorithm::replace_all (subject, "$END_TIME", end.str ());
249                 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i->cinema->name);
250                 quickmail mail = quickmail_create (Config::instance()->kdm_from().c_str(), subject.c_str ());
251
252                 quickmail_add_to (mail, i->cinema->email.c_str ());
253                 if (!Config::instance()->kdm_cc().empty ()) {
254                         quickmail_add_cc (mail, Config::instance()->kdm_cc().c_str ());
255                 }
256                 if (!Config::instance()->kdm_bcc().empty ()) {
257                         quickmail_add_bcc (mail, Config::instance()->kdm_bcc().c_str ());
258                 }
259
260                 string body = Config::instance()->kdm_email().c_str();
261                 boost::algorithm::replace_all (body, "$CPL_NAME", film->dcp_name ());
262                 boost::algorithm::replace_all (body, "$START_TIME", start.str ());
263                 boost::algorithm::replace_all (body, "$END_TIME", end.str ());
264                 boost::algorithm::replace_all (body, "$CINEMA_NAME", i->cinema->name);
265
266                 SafeStringStream screens;
267                 for (list<ScreenKDM>::const_iterator j = i->screen_kdms.begin(); j != i->screen_kdms.end(); ++j) {
268                         screens << j->screen->name << ", ";
269                 }
270                 boost::algorithm::replace_all (body, "$SCREENS", screens.str().substr (0, screens.str().length() - 2));
271
272                 quickmail_set_body (mail, body.c_str());
273                 quickmail_add_attachment_file (mail, zip_file.string().c_str(), "application/zip");
274
275                 char const* error = quickmail_send (
276                         mail,
277                         Config::instance()->mail_server().c_str(),
278                         Config::instance()->mail_port(),
279                         Config::instance()->mail_user().c_str(),
280                         Config::instance()->mail_password().c_str()
281                         );
282
283                 if (error) {
284                         quickmail_destroy (mail);
285                         throw KDMError (
286                                 String::compose (
287                                         "Failed to send KDM email to %1 (%2)",
288                                         Config::instance()->mail_server(),
289                                         error
290                                         )
291                                 );
292                 }
293                 quickmail_destroy (mail);
294         }
295 }