BOOST_FOREACH.
[dcpomatic.git] / src / lib / send_problem_report_job.cc
1 /*
2     Copyright (C) 2014-2017 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 "send_problem_report_job.h"
22 #include "compose.hpp"
23 #include "film.h"
24 #include "cross.h"
25 #include "film.h"
26 #include "log.h"
27 #include "version.h"
28 #include "emailer.h"
29 #include "environment_info.h"
30 #include <libxml++/libxml++.h>
31
32 #include "i18n.h"
33
34 using std::string;
35 using std::list;
36 using std::shared_ptr;
37
38 /** @param film Film thta the problem is with, or 0.
39  *  @param from Email address to use for From:
40  *  @param summary Summary of the problem.
41  */
42 SendProblemReportJob::SendProblemReportJob (
43         shared_ptr<const Film> film,
44         string from,
45         string summary
46         )
47         : Job (film)
48         , _from (from)
49         , _summary (summary)
50 {
51
52 }
53
54 SendProblemReportJob::~SendProblemReportJob ()
55 {
56         stop_thread ();
57 }
58
59 string
60 SendProblemReportJob::name () const
61 {
62         if (!_film) {
63                 return _("Email problem report");
64         }
65
66         return String::compose (_("Email problem report for %1"), _film->name());
67 }
68
69 string
70 SendProblemReportJob::json_name () const
71 {
72         return N_("send_problem_report");
73 }
74
75 void
76 SendProblemReportJob::run ()
77 {
78         sub (_("Sending email"));
79         set_progress_unknown ();
80
81         string body = _summary + "\n\n";
82
83         body += "Version: " + string (dcpomatic_version) + " " + string (dcpomatic_git_commit) + "\n\n";
84
85         for (auto i: environment_info ()) {
86                 body += i + "\n";
87         }
88
89         body += "\n";
90
91         if (_film) {
92                 body += "log head and tail:\n";
93                 body += "---<8----\n";
94                 body += _film->log()->head_and_tail (4096);
95                 body += "---<8----\n\n";
96
97                 add_file (body, "ffprobe.log");
98
99                 body += "---<8----\n";
100                 body += _film->metadata()->write_to_string_formatted("UTF-8");
101                 body += "---<8----\n";
102         }
103
104         list<string> to;
105         to.push_back ("carl@dcpomatic.com");
106
107         Emailer emailer (_from, to, "DCP-o-matic problem report", body);
108         emailer.send ("main.carlh.net", 2525, EMAIL_PROTOCOL_STARTTLS);
109
110         set_progress (1);
111         set_state (FINISHED_OK);
112 }
113
114 void
115 SendProblemReportJob::add_file (string& body, boost::filesystem::path file) const
116 {
117         FILE* f = fopen_boost (_film->file (file), "r");
118         if (!f) {
119                 return;
120         }
121
122         body += file.string() + ":\n";
123         body += "---<8----\n";
124         uintmax_t const size = boost::filesystem::file_size (_film->file (file));
125         char* buffer = new char[size + 1];
126         int const N = fread (buffer, 1, size, f);
127         buffer[N] = '\0';
128         body += buffer;
129         delete[] buffer;
130         body += "---<8----\n\n";
131         fclose (f);
132 }