Allow specification of DCP to build KDMs for (#235).
[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 boost::shared_ptr;
35
36 struct ScreenKDM
37 {
38         ScreenKDM (shared_ptr<Screen> s, libdcp::KDM k)
39                 : screen (s)
40                 , kdm (k)
41         {}
42         
43         shared_ptr<Screen> screen;
44         libdcp::KDM kdm;
45 };
46
47 static string
48 kdm_filename (shared_ptr<Film> film, ScreenKDM kdm)
49 {
50         return tidy_for_filename (film->name()) + "_" + tidy_for_filename (kdm.screen->cinema->name) + "_" + tidy_for_filename (kdm.screen->name) + ".kdm.xml";
51 }
52
53 struct CinemaKDMs
54 {
55         shared_ptr<Cinema> cinema;
56         list<ScreenKDM> screen_kdms;
57
58         void make_zip_file (shared_ptr<Film> film, boost::filesystem::path zip_file) const
59         {
60                 int error;
61                 struct zip* zip = zip_open (zip_file.string().c_str(), ZIP_CREATE | ZIP_EXCL, &error);
62                 if (!zip) {
63                         if (error == ZIP_ER_EXISTS) {
64                                 throw FileError ("ZIP file already exists", zip_file);
65                         }
66                         throw FileError ("could not create ZIP file", zip_file);
67                 }
68                 
69                 list<shared_ptr<string> > kdm_strings;
70                 
71                 for (list<ScreenKDM>::const_iterator i = screen_kdms.begin(); i != screen_kdms.end(); ++i) {
72                         shared_ptr<string> kdm (new string (i->kdm.as_xml ()));
73                         kdm_strings.push_back (kdm);
74                         
75                         struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
76                         if (!source) {
77                                 throw StringError ("could not create ZIP source");
78                         }
79                         
80                         if (zip_add (zip, kdm_filename (film, *i).c_str(), source) == -1) {
81                                 throw StringError ("failed to add KDM to ZIP archive");
82                         }
83                 }
84                 
85                 if (zip_close (zip) == -1) {
86                         throw StringError ("failed to close ZIP archive");
87                 }
88         }
89 };
90
91 /* Not complete but sufficient for our purposes (we're using
92    ScreenKDM in a list where all the screens will be unique).
93 */
94 bool
95 operator== (ScreenKDM const & a, ScreenKDM const & b)
96 {
97         return a.screen == b.screen;
98 }
99
100 static list<ScreenKDM>
101 make_screen_kdms (
102         shared_ptr<Film> film,
103         list<shared_ptr<Screen> > screens,
104         boost::filesystem::path dcp,
105         boost::posix_time::ptime from,
106         boost::posix_time::ptime to
107         )
108 {
109         list<libdcp::KDM> kdms = film->make_kdms (screens, dcp, from, to);
110            
111         list<ScreenKDM> screen_kdms;
112         
113         list<shared_ptr<Screen> >::iterator i = screens.begin ();
114         list<libdcp::KDM>::iterator j = kdms.begin ();
115         while (i != screens.end() && j != kdms.end ()) {
116                 screen_kdms.push_back (ScreenKDM (*i, *j));
117                 ++i;
118                 ++j;
119         }
120
121         return screen_kdms;
122 }
123
124 static list<CinemaKDMs>
125 make_cinema_kdms (
126         shared_ptr<Film> film,
127         list<shared_ptr<Screen> > screens,
128         boost::filesystem::path dcp,
129         boost::posix_time::ptime from,
130         boost::posix_time::ptime to
131         )
132 {
133         list<ScreenKDM> screen_kdms = make_screen_kdms (film, screens, dcp, from, to);
134         list<CinemaKDMs> cinema_kdms;
135
136         while (!screen_kdms.empty ()) {
137                 
138                 /* Get all the screens from a single cinema */
139
140                 CinemaKDMs ck;
141                 
142                 list<ScreenKDM>::iterator i = screen_kdms.begin ();
143                 ck.cinema = i->screen->cinema;
144                 ck.screen_kdms.push_back (*i);
145                 list<ScreenKDM>::iterator j = i;
146                 ++i;
147                 screen_kdms.remove (*j);
148                 
149                 while (i != screen_kdms.end ()) {
150                         if (i->screen->cinema == ck.cinema) {
151                                 ck.screen_kdms.push_back (*i);
152                                 list<ScreenKDM>::iterator j = i;
153                                 ++i;
154                                 screen_kdms.remove (*j);
155                         } else {
156                                 ++i;
157                         }
158                 }
159
160                 cinema_kdms.push_back (ck);
161         }
162
163         return cinema_kdms;
164 }
165
166 void
167 write_kdm_files (
168         shared_ptr<Film> film,
169         list<shared_ptr<Screen> > screens,
170         boost::filesystem::path dcp,
171         boost::posix_time::ptime from,
172         boost::posix_time::ptime to,
173         boost::filesystem::path directory
174         )
175 {
176         list<ScreenKDM> screen_kdms = make_screen_kdms (film, screens, dcp, from, to);
177
178         /* Write KDMs to the specified directory */
179         for (list<ScreenKDM>::iterator i = screen_kdms.begin(); i != screen_kdms.end(); ++i) {
180                 boost::filesystem::path out = directory;
181                 out /= kdm_filename (film, *i);
182                 i->kdm.as_xml (out);
183         }
184 }
185
186 void
187 write_kdm_zip_files (
188         shared_ptr<Film> film,
189         list<shared_ptr<Screen> > screens,
190         boost::filesystem::path dcp,
191         boost::posix_time::ptime from,
192         boost::posix_time::ptime to,
193         boost::filesystem::path directory
194         )
195 {
196         list<CinemaKDMs> cinema_kdms = make_cinema_kdms (film, screens, dcp, from, to);
197
198         for (list<CinemaKDMs>::const_iterator i = cinema_kdms.begin(); i != cinema_kdms.end(); ++i) {
199                 boost::filesystem::path path = directory;
200                 path /= tidy_for_filename (i->cinema->name) + ".zip";
201                 i->make_zip_file (film, path);
202         }
203 }
204
205 void
206 email_kdms (
207         shared_ptr<Film> film,
208         list<shared_ptr<Screen> > screens,
209         boost::filesystem::path dcp,
210         boost::posix_time::ptime from,
211         boost::posix_time::ptime to
212         )
213 {
214         list<CinemaKDMs> cinema_kdms = make_cinema_kdms (film, screens, dcp, from, to);
215
216         for (list<CinemaKDMs>::const_iterator i = cinema_kdms.begin(); i != cinema_kdms.end(); ++i) {
217                 
218                 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path ();
219                 zip_file /= boost::filesystem::unique_path().string() + ".zip";
220                 i->make_zip_file (film, zip_file);
221                 
222                 /* Send email */
223                 
224                 quickmail_initialize ();
225                 quickmail mail = quickmail_create (Config::instance()->kdm_from().c_str(), "KDM delivery");
226                 quickmail_add_to (mail, i->cinema->email.c_str ());
227                 
228                 string body = Config::instance()->kdm_email().c_str();
229                 boost::algorithm::replace_all (body, "$DCP_NAME", film->dcp_name ());
230                 
231                 quickmail_set_body (mail, body.c_str());
232                 quickmail_add_attachment_file (mail, zip_file.string().c_str());
233                 char const* error = quickmail_send (mail, Config::instance()->mail_server().c_str(), 25, "", "");
234                 if (error) {
235                         quickmail_destroy (mail);
236                         throw StringError (String::compose ("Failed to send KDM email (%1)", error));
237                 }
238                 quickmail_destroy (mail);
239         }
240 }