Revert "Use make_shared<>."
[dcpomatic.git] / src / lib / cinema_kdms.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
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 "emailer.h"
28 #include "compose.hpp"
29 #include "log.h"
30 #include <zip.h>
31 #include <boost/foreach.hpp>
32
33 #include "i18n.h"
34
35 using std::list;
36 using std::cout;
37 using std::string;
38 using std::runtime_error;
39 using boost::shared_ptr;
40
41 void
42 CinemaKDMs::make_zip_file (string film_name, boost::filesystem::path zip_file) const
43 {
44         int error;
45         struct zip* zip = zip_open (zip_file.string().c_str(), ZIP_CREATE | ZIP_EXCL, &error);
46         if (!zip) {
47                 if (error == ZIP_ER_EXISTS) {
48                         throw FileError ("ZIP file already exists", zip_file);
49                 }
50                 throw FileError ("could not create ZIP file", zip_file);
51         }
52
53         list<shared_ptr<string> > kdm_strings;
54
55         BOOST_FOREACH (ScreenKDM const & i, screen_kdms) {
56                 shared_ptr<string> kdm (new string (i.kdm.as_xml ()));
57                 kdm_strings.push_back (kdm);
58
59                 struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
60                 if (!source) {
61                         throw runtime_error ("could not create ZIP source");
62                 }
63
64                 if (zip_add (zip, i.filename(film_name).c_str(), source) == -1) {
65                         throw runtime_error ("failed to add KDM to ZIP archive");
66                 }
67         }
68
69         if (zip_close (zip) == -1) {
70                 throw runtime_error ("failed to close ZIP archive");
71         }
72 }
73
74 list<CinemaKDMs>
75 CinemaKDMs::collect (list<ScreenKDM> screen_kdms)
76 {
77         list<CinemaKDMs> cinema_kdms;
78
79         while (!screen_kdms.empty ()) {
80
81                 /* Get all the screens from a single cinema */
82
83                 CinemaKDMs ck;
84
85                 list<ScreenKDM>::iterator i = screen_kdms.begin ();
86                 ck.cinema = i->screen->cinema;
87                 ck.screen_kdms.push_back (*i);
88                 list<ScreenKDM>::iterator j = i;
89                 ++i;
90                 screen_kdms.remove (*j);
91
92                 while (i != screen_kdms.end ()) {
93                         if (i->screen->cinema == ck.cinema) {
94                                 ck.screen_kdms.push_back (*i);
95                                 list<ScreenKDM>::iterator j = i;
96                                 ++i;
97                                 screen_kdms.remove (*j);
98                         } else {
99                                 ++i;
100                         }
101                 }
102
103                 cinema_kdms.push_back (ck);
104         }
105
106         return cinema_kdms;
107 }
108
109 void
110 CinemaKDMs::write_zip_files (string film_name, list<CinemaKDMs> cinema_kdms, boost::filesystem::path directory)
111 {
112         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
113                 boost::filesystem::path path = directory;
114                 path /= tidy_for_filename (i.cinema->name) + ".zip";
115                 i.make_zip_file (film_name, path);
116         }
117 }
118
119 /** @param log Log to write email session transcript to, or 0 */
120 /* XXX: should probably get from/to from the KDMs themselves */
121 void
122 CinemaKDMs::email (
123         string film_name, string cpl_name, list<CinemaKDMs> cinema_kdms, dcp::LocalTime from, dcp::LocalTime to, shared_ptr<Log> log
124         )
125 {
126         Config* config = Config::instance ();
127
128         if (config->mail_server().empty()) {
129                 throw NetworkError (_("No mail server configured in preferences"));
130         }
131
132         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
133
134                 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path ();
135                 zip_file /= boost::filesystem::unique_path().string() + ".zip";
136                 i.make_zip_file (film_name, zip_file);
137
138                 string subject = config->kdm_subject();
139                 SafeStringStream start;
140                 start << from.date() << " " << from.time_of_day();
141                 SafeStringStream end;
142                 end << to.date() << " " << to.time_of_day();
143                 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
144                 boost::algorithm::replace_all (subject, "$START_TIME", start.str ());
145                 boost::algorithm::replace_all (subject, "$END_TIME", end.str ());
146                 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i.cinema->name);
147
148                 string body = config->kdm_email().c_str();
149                 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_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                 Emailer email (config->kdm_from(), i.cinema->emails, subject, body);
161
162                 BOOST_FOREACH (string i, config->kdm_cc()) {
163                         email.add_cc (i);
164                 }
165                 if (!config->kdm_bcc().empty ()) {
166                         email.add_bcc (config->kdm_bcc ());
167                 }
168
169                 string const name = tidy_for_filename(i.cinema->name) + "_" + tidy_for_filename(film_name) + ".zip";
170                 email.add_attachment (zip_file, name, "application/zip");
171
172                 Config* c = Config::instance ();
173
174                 try {
175                         email.send (c->mail_server(), c->mail_port(), c->mail_user(), c->mail_password());
176                 } catch (...) {
177                         if (log) {
178                                 log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
179                                 log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
180                                 log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
181                                 log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
182                         }
183                         throw;
184                 }
185
186                 if (log) {
187                         log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
188                         log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
189                         log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
190                         log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
191                 }
192         }
193 }