Try to clean up KDM creation code a bit.
[dcpomatic.git] / src / lib / cinema_kdms.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 "quickmail.h"
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 "film.h"
28 #include "compose.hpp"
29 #include <zip.h>
30 #include <boost/foreach.hpp>
31
32 using std::list;
33 using std::string;
34 using boost::shared_ptr;
35
36 void
37 CinemaKDMs::make_zip_file (shared_ptr<const Film> film, boost::filesystem::path zip_file) const
38 {
39         int error;
40         struct zip* zip = zip_open (zip_file.string().c_str(), ZIP_CREATE | ZIP_EXCL, &error);
41         if (!zip) {
42                 if (error == ZIP_ER_EXISTS) {
43                         throw FileError ("ZIP file already exists", zip_file);
44                 }
45                 throw FileError ("could not create ZIP file", zip_file);
46         }
47
48         list<shared_ptr<string> > kdm_strings;
49
50         BOOST_FOREACH (ScreenKDM const & i, screen_kdms) {
51                 shared_ptr<string> kdm (new string (i.kdm.as_xml ()));
52                 kdm_strings.push_back (kdm);
53
54                 struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
55                 if (!source) {
56                         throw StringError ("could not create ZIP source");
57                 }
58
59                 if (zip_add (zip, i.filename(film).c_str(), source) == -1) {
60                         throw StringError ("failed to add KDM to ZIP archive");
61                 }
62         }
63
64         if (zip_close (zip) == -1) {
65                 throw StringError ("failed to close ZIP archive");
66         }
67 }
68
69 list<CinemaKDMs>
70 CinemaKDMs::collect (list<ScreenKDM> screen_kdms)
71 {
72         list<CinemaKDMs> cinema_kdms;
73
74         while (!screen_kdms.empty ()) {
75
76                 /* Get all the screens from a single cinema */
77
78                 CinemaKDMs ck;
79
80                 list<ScreenKDM>::iterator i = screen_kdms.begin ();
81                 ck.cinema = i->screen->cinema;
82                 ck.screen_kdms.push_back (*i);
83                 list<ScreenKDM>::iterator j = i;
84                 ++i;
85                 screen_kdms.remove (*j);
86
87                 while (i != screen_kdms.end ()) {
88                         if (i->screen->cinema == ck.cinema) {
89                                 ck.screen_kdms.push_back (*i);
90                                 list<ScreenKDM>::iterator j = i;
91                                 ++i;
92                                 screen_kdms.remove (*j);
93                         } else {
94                                 ++i;
95                         }
96                 }
97
98                 cinema_kdms.push_back (ck);
99         }
100
101         return cinema_kdms;
102 }
103
104 void
105 CinemaKDMs::write_zip_files (shared_ptr<const Film> film, list<CinemaKDMs> cinema_kdms, boost::filesystem::path directory)
106 {
107         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
108                 boost::filesystem::path path = directory;
109                 path /= tidy_for_filename (i.cinema->name) + ".zip";
110                 i.make_zip_file (film, path);
111         }
112 }
113
114 /* XXX: should probably get from/to from the KDMs themselves */
115 void
116 CinemaKDMs::email (shared_ptr<const Film> film, list<CinemaKDMs> cinema_kdms, dcp::LocalTime from, dcp::LocalTime to)
117 {
118         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
119
120                 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path ();
121                 zip_file /= boost::filesystem::unique_path().string() + ".zip";
122                 i.make_zip_file (film, zip_file);
123
124                 /* Send email */
125
126                 quickmail_initialize ();
127
128                 SafeStringStream start;
129                 start << from.date() << " " << from.time_of_day();
130                 SafeStringStream end;
131                 end << to.date() << " " << to.time_of_day();
132
133                 string subject = Config::instance()->kdm_subject();
134                 boost::algorithm::replace_all (subject, "$CPL_NAME", film->dcp_name ());
135                 boost::algorithm::replace_all (subject, "$START_TIME", start.str ());
136                 boost::algorithm::replace_all (subject, "$END_TIME", end.str ());
137                 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i.cinema->name);
138                 quickmail mail = quickmail_create (Config::instance()->kdm_from().c_str(), subject.c_str ());
139
140                 quickmail_add_to (mail, i.cinema->email.c_str ());
141                 if (!Config::instance()->kdm_cc().empty ()) {
142                         quickmail_add_cc (mail, Config::instance()->kdm_cc().c_str ());
143                 }
144                 if (!Config::instance()->kdm_bcc().empty ()) {
145                         quickmail_add_bcc (mail, Config::instance()->kdm_bcc().c_str ());
146                 }
147
148                 string body = Config::instance()->kdm_email().c_str();
149                 boost::algorithm::replace_all (body, "$CPL_NAME", film->dcp_name ());
150                 boost::algorithm::replace_all (body, "$START_TIME", start.str ());
151                 boost::algorithm::replace_all (body, "$END_TIME", end.str ());
152                 boost::algorithm::replace_all (body, "$CINEMA_NAME", i.cinema->name);
153
154                 SafeStringStream screens;
155                 BOOST_FOREACH (ScreenKDM const & j, i.screen_kdms) {
156                         screens << j.screen->name << ", ";
157                 }
158                 boost::algorithm::replace_all (body, "$SCREENS", screens.str().substr (0, screens.str().length() - 2));
159
160                 quickmail_set_body (mail, body.c_str());
161                 quickmail_add_attachment_file (mail, zip_file.string().c_str(), "application/zip");
162
163                 char const* error = quickmail_send (
164                         mail,
165                         Config::instance()->mail_server().c_str(),
166                         Config::instance()->mail_port(),
167                         Config::instance()->mail_user().c_str(),
168                         Config::instance()->mail_password().c_str()
169                         );
170
171                 if (error) {
172                         quickmail_destroy (mail);
173                         throw KDMError (
174                                 String::compose (
175                                         "Failed to send KDM email to %1 (%2)",
176                                         Config::instance()->mail_server(),
177                                         error
178                                         )
179                                 );
180                 }
181                 quickmail_destroy (mail);
182         }
183 }