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