Merge.
[dcpomatic.git] / src / lib / kdm.cc
1 /*
2     Copyright (C) 2013 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 <list>
21 #include <boost/shared_ptr.hpp>
22 #include <quickmail.h>
23 #include <zip.h>
24 #include <libdcp/kdm.h>
25 #include "kdm.h"
26 #include "cinema.h"
27 #include "exceptions.h"
28 #include "util.h"
29 #include "film.h"
30 #include "config.h"
31
32 using std::list;
33 using std::string;
34 using std::stringstream;
35 using std::cout;
36 using boost::shared_ptr;
37
38 struct ScreenKDM
39 {
40         ScreenKDM (shared_ptr<Screen> s, libdcp::KDM k)
41                 : screen (s)
42                 , kdm (k)
43         {}
44         
45         shared_ptr<Screen> screen;
46         libdcp::KDM kdm;
47 };
48
49 static string
50 kdm_filename (shared_ptr<const Film> film, ScreenKDM kdm)
51 {
52         return tidy_for_filename (film->name()) + "_" + tidy_for_filename (kdm.screen->cinema->name) + "_" + tidy_for_filename (kdm.screen->name) + ".kdm.xml";
53 }
54
55 struct CinemaKDMs
56 {
57         shared_ptr<Cinema> cinema;
58         list<ScreenKDM> screen_kdms;
59
60         void make_zip_file (shared_ptr<const Film> film, boost::filesystem::path zip_file) const
61         {
62                 int error;
63                 struct zip* zip = zip_open (zip_file.string().c_str(), ZIP_CREATE | ZIP_EXCL, &error);
64                 if (!zip) {
65                         if (error == ZIP_ER_EXISTS) {
66                                 throw FileError ("ZIP file already exists", zip_file);
67                         }
68                         throw FileError ("could not create ZIP file", zip_file);
69                 }
70                 
71                 list<shared_ptr<string> > kdm_strings;
72                 
73                 for (list<ScreenKDM>::const_iterator i = screen_kdms.begin(); i != screen_kdms.end(); ++i) {
74                         shared_ptr<string> kdm (new string (i->kdm.as_xml ()));
75                         kdm_strings.push_back (kdm);
76                         
77                         struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
78                         if (!source) {
79                                 throw StringError ("could not create ZIP source");
80                         }
81                         
82                         if (zip_add (zip, kdm_filename (film, *i).c_str(), source) == -1) {
83                                 throw StringError ("failed to add KDM to ZIP archive");
84                         }
85                 }
86                 
87                 if (zip_close (zip) == -1) {
88                         throw StringError ("failed to close ZIP archive");
89                 }
90         }
91 };
92
93 /* Not complete but sufficient for our purposes (we're using
94    ScreenKDM in a list where all the screens will be unique).
95 */
96 bool
97 operator== (ScreenKDM const & a, ScreenKDM const & b)
98 {
99         return a.screen == b.screen;
100 }
101
102 static list<ScreenKDM>
103 make_screen_kdms (
104         shared_ptr<const Film> film,
105         list<shared_ptr<Screen> > screens,
106         boost::filesystem::path cpl,
107         boost::posix_time::ptime from,
108         boost::posix_time::ptime to,
109         libdcp::KDM::Formulation formulation
110         )
111 {
112         list<libdcp::KDM> kdms = film->make_kdms (screens, cpl, from, to, formulation);
113            
114         list<ScreenKDM> screen_kdms;
115         
116         list<shared_ptr<Screen> >::iterator i = screens.begin ();
117         list<libdcp::KDM>::iterator j = kdms.begin ();
118         while (i != screens.end() && j != kdms.end ()) {
119                 screen_kdms.push_back (ScreenKDM (*i, *j));
120                 ++i;
121                 ++j;
122         }
123
124         return screen_kdms;
125 }
126
127 static list<CinemaKDMs>
128 make_cinema_kdms (
129         shared_ptr<const Film> film,
130         list<shared_ptr<Screen> > screens,
131         boost::filesystem::path cpl,
132         boost::posix_time::ptime from,
133         boost::posix_time::ptime to,
134         libdcp::KDM::Formulation formulation
135         )
136 {
137         list<ScreenKDM> screen_kdms = make_screen_kdms (film, screens, cpl, from, to, formulation);
138         list<CinemaKDMs> cinema_kdms;
139
140         while (!screen_kdms.empty ()) {
141                 
142                 /* Get all the screens from a single cinema */
143
144                 CinemaKDMs ck;
145                 
146                 list<ScreenKDM>::iterator i = screen_kdms.begin ();
147                 ck.cinema = i->screen->cinema;
148                 ck.screen_kdms.push_back (*i);
149                 list<ScreenKDM>::iterator j = i;
150                 ++i;
151                 screen_kdms.remove (*j);
152                 
153                 while (i != screen_kdms.end ()) {
154                         if (i->screen->cinema == ck.cinema) {
155                                 ck.screen_kdms.push_back (*i);
156                                 list<ScreenKDM>::iterator j = i;
157                                 ++i;
158                                 screen_kdms.remove (*j);
159                         } else {
160                                 ++i;
161                         }
162                 }
163
164                 cinema_kdms.push_back (ck);
165         }
166
167         return cinema_kdms;
168 }
169
170 /** @param from KDM from time in local time.
171  *  @param to KDM to time in local time.
172  */
173 void
174 write_kdm_files (
175         shared_ptr<const Film> film,
176         list<shared_ptr<Screen> > screens,
177         boost::filesystem::path cpl,
178         boost::posix_time::ptime from,
179         boost::posix_time::ptime to,
180         libdcp::KDM::Formulation formulation,
181         boost::filesystem::path directory
182         )
183 {
184         list<ScreenKDM> screen_kdms = make_screen_kdms (film, screens, cpl, from, to, formulation);
185
186         /* Write KDMs to the specified directory */
187         for (list<ScreenKDM>::iterator i = screen_kdms.begin(); i != screen_kdms.end(); ++i) {
188                 boost::filesystem::path out = directory;
189                 out /= kdm_filename (film, *i);
190                 i->kdm.as_xml (out);
191         }
192 }
193
194 void
195 write_kdm_zip_files (
196         shared_ptr<const Film> film,
197         list<shared_ptr<Screen> > screens,
198         boost::filesystem::path cpl,
199         boost::posix_time::ptime from,
200         boost::posix_time::ptime to,
201         libdcp::KDM::Formulation formulation,
202         boost::filesystem::path directory
203         )
204 {
205         list<CinemaKDMs> cinema_kdms = make_cinema_kdms (film, screens, cpl, from, to, formulation);
206
207         for (list<CinemaKDMs>::const_iterator i = cinema_kdms.begin(); i != cinema_kdms.end(); ++i) {
208                 boost::filesystem::path path = directory;
209                 path /= tidy_for_filename (i->cinema->name) + ".zip";
210                 i->make_zip_file (film, path);
211         }
212 }
213
214 void
215 email_kdms (
216         shared_ptr<const Film> film,
217         list<shared_ptr<Screen> > screens,
218         boost::filesystem::path cpl,
219         boost::posix_time::ptime from,
220         boost::posix_time::ptime to,
221         libdcp::KDM::Formulation formulation
222         )
223 {
224         list<CinemaKDMs> cinema_kdms = make_cinema_kdms (film, screens, cpl, from, to, formulation);
225
226         for (list<CinemaKDMs>::const_iterator i = cinema_kdms.begin(); i != cinema_kdms.end(); ++i) {
227                 
228                 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path ();
229                 zip_file /= boost::filesystem::unique_path().string() + ".zip";
230                 i->make_zip_file (film, zip_file);
231                 
232                 /* Send email */
233                 
234                 quickmail_initialize ();
235                 quickmail mail = quickmail_create (Config::instance()->kdm_from().c_str(), "KDM delivery");
236                 quickmail_add_to (mail, i->cinema->email.c_str ());
237                 if (!Config::instance()->kdm_cc().empty ()) {
238                         quickmail_add_cc (mail, Config::instance()->kdm_cc().c_str ());
239                 }
240                 string body = Config::instance()->kdm_email().c_str();
241                 boost::algorithm::replace_all (body, "$CPL_NAME", film->dcp_name ());
242                 stringstream start;
243                 start << from.date() << " " << from.time_of_day();
244                 boost::algorithm::replace_all (body, "$START_TIME", start.str ());
245                 stringstream end;
246                 end << to.date() << " " << to.time_of_day();
247                 boost::algorithm::replace_all (body, "$END_TIME", end.str ());
248                 boost::algorithm::replace_all (body, "$CINEMA_NAME", i->cinema->name);
249                 stringstream screens;
250                 for (list<ScreenKDM>::const_iterator j = i->screen_kdms.begin(); j != i->screen_kdms.end(); ++j) {
251                         screens << j->screen->name << ", ";
252                 }
253                 boost::algorithm::replace_all (body, "$SCREENS", screens.str().substr (0, screens.str().length() - 2));
254
255                 quickmail_set_body (mail, body.c_str());
256                 quickmail_add_attachment_file (mail, zip_file.string().c_str(), "application/zip");
257
258                 int const port = Config::instance()->mail_user().empty() ? 25 : 587;
259
260                 char const* error = quickmail_send (
261                         mail,
262                         Config::instance()->mail_server().c_str(),
263                         port,
264                         Config::instance()->mail_user().c_str(),
265                         Config::instance()->mail_password().c_str()
266                         );
267                 
268                 if (error) {
269                         quickmail_destroy (mail);
270                         throw KDMError (String::compose ("Failed to send KDM email (%1)", error));
271                 }
272                 quickmail_destroy (mail);
273         }
274 }