2 Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "recipient_with_dkdm.h"
23 #include "dcpomatic_log.h"
25 #include "exceptions.h"
28 #include <boost/foreach.hpp>
35 using boost::shared_ptr;
38 RecipientWithDKDM::make_zip_file (boost::filesystem::path zip_file, dcp::NameFormat name_format, dcp::NameFormat::Map name_values) const
40 Zipper zipper (zip_file);
42 name_values['i'] = kdm.cpl_id ();
43 string const name = careful_string_filter(name_format.get(name_values, ".xml"));
44 zipper.add (name, kdm.as_xml());
50 /** Email one ZIP file per recipient.
51 * @param kdms KDMs to email.
52 * @param filename_format Format of filenames to use.
53 * @param name_values Values to substitute into \p container_name_format and \p filename_format.
54 * @param cpl_name Name of the CPL that the KDMs are for.
57 RecipientWithDKDM::email (
58 vector<RecipientWithDKDM> kdms,
59 dcp::NameFormat filename_format,
60 dcp::NameFormat::Map name_values,
64 Config* config = Config::instance ();
66 if (config->mail_server().empty()) {
67 throw NetworkError (_("No mail server configured in preferences"));
70 BOOST_FOREACH (RecipientWithDKDM const & i, kdms) {
72 if (i.recipient->emails.empty()) {
76 boost::filesystem::path zip_file = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
77 boost::filesystem::create_directories (zip_file);
78 zip_file /= filename_format.get(name_values, ".zip");
79 i.make_zip_file (zip_file, filename_format, name_values);
81 string subject = config->kdm_subject();
82 boost::algorithm::replace_all (subject, "$CPL_NAME", cpl_name);
83 boost::algorithm::replace_all (subject, "$START_TIME", name_values['b']);
84 boost::algorithm::replace_all (subject, "$END_TIME", name_values['e']);
86 string body = config->kdm_email().c_str();
87 boost::algorithm::replace_all (body, "$CPL_NAME", cpl_name);
88 boost::algorithm::replace_all (body, "$START_TIME", name_values['b']);
89 boost::algorithm::replace_all (body, "$END_TIME", name_values['e']);
91 Emailer email (config->kdm_from(), i.recipient->emails, subject, body);
93 BOOST_FOREACH (string i, config->kdm_cc()) {
96 if (!config->kdm_bcc().empty ()) {
97 email.add_bcc (config->kdm_bcc ());
100 email.add_attachment (zip_file, filename_format.get(name_values, ".zip"), "application/zip");
103 email.send (config->mail_server(), config->mail_port(), config->mail_protocol(), config->mail_user(), config->mail_password());
105 boost::filesystem::remove (zip_file);
106 dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
107 dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
108 dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
109 dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
113 boost::filesystem::remove (zip_file);
115 dcpomatic_log->log ("Email content follows", LogEntry::TYPE_DEBUG_EMAIL);
116 dcpomatic_log->log (email.email(), LogEntry::TYPE_DEBUG_EMAIL);
117 dcpomatic_log->log ("Email session follows", LogEntry::TYPE_DEBUG_EMAIL);
118 dcpomatic_log->log (email.notes(), LogEntry::TYPE_DEBUG_EMAIL);
124 RecipientWithDKDM::write_files (
125 vector<RecipientWithDKDM> kdms,
126 boost::filesystem::path directory,
127 dcp::NameFormat name_format,
128 dcp::NameFormat::Map name_values,
129 boost::function<bool (boost::filesystem::path)> confirm_overwrite
134 if (!boost::filesystem::exists (directory)) {
135 boost::filesystem::create_directories (directory);
138 /* Write KDMs to the specified directory */
139 BOOST_FOREACH (RecipientWithDKDM const& i, kdms) {
140 name_values['i'] = i.kdm.cpl_id ();
141 boost::filesystem::path out = directory / careful_string_filter(name_format.get(name_values, ".xml"));
142 if (!boost::filesystem::exists (out) || confirm_overwrite (out)) {