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