Remove all use of stringstream in an attempt to fix
[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 (boost::filesystem::path zip_file, dcp::NameFormat name_format, dcp::NameFormat::Map name_values) 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         name_values['c'] = cinema->name;
56
57         BOOST_FOREACH (ScreenKDM const & i, screen_kdms) {
58                 shared_ptr<string> kdm (new 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                 name_values['s'] = i.screen->name;
67                 string const name = name_format.get(name_values) + ".xml";
68                 if (zip_add (zip, name.c_str(), source) == -1) {
69                         throw runtime_error ("failed to add KDM to ZIP archive");
70                 }
71         }
72
73         if (zip_close (zip) == -1) {
74                 throw runtime_error ("failed to close ZIP archive");
75         }
76 }
77
78 /** Collect a list of ScreenKDMs into a list of CinemaKDMs so that each
79  *  CinemaKDM contains the KDMs for its cinema.
80  */
81 list<CinemaKDMs>
82 CinemaKDMs::collect (list<ScreenKDM> screen_kdms)
83 {
84         list<CinemaKDMs> cinema_kdms;
85
86         while (!screen_kdms.empty ()) {
87
88                 /* Get all the screens from a single cinema */
89
90                 CinemaKDMs ck;
91
92                 list<ScreenKDM>::iterator i = screen_kdms.begin ();
93                 ck.cinema = i->screen->cinema;
94                 ck.screen_kdms.push_back (*i);
95                 list<ScreenKDM>::iterator j = i;
96                 ++i;
97                 screen_kdms.remove (*j);
98
99                 while (i != screen_kdms.end ()) {
100                         if (i->screen->cinema == ck.cinema) {
101                                 ck.screen_kdms.push_back (*i);
102                                 list<ScreenKDM>::iterator j = i;
103                                 ++i;
104                                 screen_kdms.remove (*j);
105                         } else {
106                                 ++i;
107                         }
108                 }
109
110                 cinema_kdms.push_back (ck);
111         }
112
113         return cinema_kdms;
114 }
115
116 /** Write one ZIP file per cinema into a directory */
117 void
118 CinemaKDMs::write_zip_files (
119         list<CinemaKDMs> cinema_kdms,
120         boost::filesystem::path directory,
121         dcp::NameFormat name_format,
122         dcp::NameFormat::Map name_values
123         )
124 {
125         /* No specific screen */
126         name_values['s'] = "";
127
128         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
129                 boost::filesystem::path path = directory;
130                 name_values['c'] = i.cinema->name;
131                 path /= name_format.get(name_values) + ".zip";
132                 i.make_zip_file (path, name_format, name_values);
133         }
134 }
135
136 /** Email one ZIP file per cinema to the cinema.
137  *  @param log Log to write email session transcript to, or 0.
138  */
139 void
140 CinemaKDMs::email (
141         list<CinemaKDMs> cinema_kdms,
142         dcp::NameFormat name_format,
143         dcp::NameFormat::Map name_values,
144         string cpl_name,
145         shared_ptr<Log> log
146         )
147 {
148         Config* config = Config::instance ();
149
150         if (config->mail_server().empty()) {
151                 throw NetworkError (_("No mail server configured in preferences"));
152         }
153
154         /* No specific screen */
155         name_values['s'] = "";
156
157         BOOST_FOREACH (CinemaKDMs const & i, cinema_kdms) {
158
159                 name_values['c'] = i.cinema->name;
160
161                 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path ();
162                 zip_file /= boost::filesystem::unique_path().string() + ".zip";
163                 i.make_zip_file (zip_file, name_format, name_values);
164
165                 string subject = config->kdm_subject();
166                 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
167                 boost::algorithm::replace_all (subject, "$START_TIME", name_values['f']);
168                 boost::algorithm::replace_all (subject, "$END_TIME", name_values['t']);
169                 boost::algorithm::replace_all (subject, "$CINEMA_NAME", i.cinema->name);
170
171                 string body = config->kdm_email().c_str();
172                 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_name);
173                 boost::algorithm::replace_all (body, "$START_TIME", name_values['f']);
174                 boost::algorithm::replace_all (body, "$END_TIME", name_values['t']);
175                 boost::algorithm::replace_all (body, "$CINEMA_NAME", i.cinema->name);
176
177                 string screens;
178                 BOOST_FOREACH (ScreenKDM const & j, i.screen_kdms) {
179                         screens += j.screen->name + ", ";
180                 }
181                 boost::algorithm::replace_all (body, "$SCREENS", screens.substr (0, screens.length() - 2));
182
183                 Emailer email (config->kdm_from(), i.cinema->emails, subject, body);
184
185                 BOOST_FOREACH (string i, config->kdm_cc()) {
186                         email.add_cc (i);
187                 }
188                 if (!config->kdm_bcc().empty ()) {
189                         email.add_bcc (config->kdm_bcc ());
190                 }
191
192                 email.add_attachment (zip_file, name_format.get(name_values) + ".zip", "application/zip");
193
194                 Config* c = Config::instance ();
195
196                 try {
197                         email.send (c->mail_server(), c->mail_port(), c->mail_user(), c->mail_password());
198                 } catch (...) {
199                         if (log) {
200                                 log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
201                                 log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
202                                 log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
203                                 log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
204                         }
205                         throw;
206                 }
207
208                 if (log) {
209                         log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
210                         log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
211                         log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
212                         log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
213                 }
214         }
215 }