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 #include <boost/make_shared.hpp>
33
34 #include "i18n.h"
35
36 using std::list;
37 using std::cout;
38 using std::string;
39 using std::runtime_error;
40 using boost::shared_ptr;
41 using boost::make_shared;
42
43 void
44 CinemaKDMs::make_zip_file (string film_name, boost::filesystem::path zip_file) const
45 {
46         int error;
47         struct zip* zip = zip_open (zip_file.string().c_str(), ZIP_CREATE | ZIP_EXCL, &error);
48         if (!zip) {
49                 if (error == ZIP_ER_EXISTS) {
50                         throw FileError ("ZIP file already exists", zip_file);
51                 }
52                 throw FileError ("could not create ZIP file", zip_file);
53         }
54
55         list<shared_ptr<string> > kdm_strings;
56
57         BOOST_FOREACH (ScreenKDM const & i, screen_kdms) {
58                 shared_ptr<string> kdm = boost::make_shared<string> (i.kdm.as_xml ());
59                 kdm_strings.push_back (kdm);
60
61                 struct zip_source* source = zip_source_buffer (zip, kdm->c_str(), kdm->length(), 0);
62                 if (!source) {
63                         throw runtime_error ("could not create ZIP source");
64                 }
65
66                 if (zip_add (zip, i.filename(film_name).c_str(), source) == -1) {
67                         throw runtime_error ("failed to add KDM to ZIP archive");
68                 }
69         }
70
71         if (zip_close (zip) == -1) {
72                 throw runtime_error ("failed to close ZIP archive");
73         }
74 }
75
76 list<CinemaKDMs>
77 CinemaKDMs::collect (list<ScreenKDM> screen_kdms)
78 {
79         list<CinemaKDMs> cinema_kdms;
80
81         while (!screen_kdms.empty ()) {
82
83                 /* Get all the screens from a single cinema */
84
85                 CinemaKDMs ck;
86
87                 list<ScreenKDM>::iterator i = screen_kdms.begin ();
88                 ck.cinema = i->screen->cinema;
89                 ck.screen_kdms.push_back (*i);
90                 list<ScreenKDM>::iterator j = i;
91                 ++i;
92                 screen_kdms.remove (*j);
93
94                 while (i != screen_kdms.end ()) {
95                         if (i->screen->cinema == ck.cinema) {
96                                 ck.screen_kdms.push_back (*i);
97                                 list<ScreenKDM>::iterator j = i;
98                                 ++i;
99                                 screen_kdms.remove (*j);
100                         } else {
101                                 ++i;
102                         }
103                 }
104
105                 cinema_kdms.push_back (ck);
106         }
107
108         return cinema_kdms;
109 }
110
111 void
112 CinemaKDMs::write_zip_files (string film_name, list<CinemaKDMs> cinema_kdms, boost::filesystem::path directory)
113 {
114         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
115                 boost::filesystem::path path = directory;
116                 path /= tidy_for_filename (i.cinema->name) + ".zip";
117                 i.make_zip_file (film_name, path);
118         }
119 }
120
121 /** @param log Log to write email session transcript to, or 0 */
122 /* XXX: should probably get from/to from the KDMs themselves */
123 void
124 CinemaKDMs::email (
125         string film_name, string cpl_name, list<CinemaKDMs> cinema_kdms, dcp::LocalTime from, dcp::LocalTime to, shared_ptr<Log> log
126         )
127 {
128         Config* config = Config::instance ();
129
130         if (config->mail_server().empty()) {
131                 throw NetworkError (_("No mail server configured in preferences"));
132         }
133
134         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
135
136                 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path ();
137                 zip_file /= boost::filesystem::unique_path().string() + ".zip";
138                 i.make_zip_file (film_name, zip_file);
139
140                 string subject = config->kdm_subject();
141                 SafeStringStream start;
142                 start << from.date() << " " << from.time_of_day();
143                 SafeStringStream end;
144                 end << to.date() << " " << to.time_of_day();
145                 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
146                 boost::algorithm::replace_all (subject, "$START_TIME", start.str ());
147                 boost::algorithm::replace_all (subject, "$END_TIME", end.str ());
148                 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i.cinema->name);
149
150                 string body = config->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                 Emailer email (config->kdm_from(), i.cinema->emails, subject, body);
163
164                 BOOST_FOREACH (string i, config->kdm_cc()) {
165                         email.add_cc (i);
166                 }
167                 if (!config->kdm_bcc().empty ()) {
168                         email.add_bcc (config->kdm_bcc ());
169                 }
170
171                 string const name = tidy_for_filename(i.cinema->name) + "_" + tidy_for_filename(film_name) + ".zip";
172                 email.add_attachment (zip_file, name, "application/zip");
173
174                 Config* c = Config::instance ();
175
176                 try {
177                         email.send (c->mail_server(), c->mail_port(), c->mail_user(), c->mail_password());
178                 } catch (...) {
179                         if (log) {
180                                 log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
181                                 log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
182                                 log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
183                                 log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
184                         }
185                         throw;
186                 }
187
188                 if (log) {
189                         log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
190                         log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
191                         log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
192                         log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
193                 }
194         }
195 }