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