Ensure basic system information is in problem reports.
[dcpomatic.git] / src / lib / send_problem_report_job.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "send_problem_report_job.h"
21 #include "compose.hpp"
22 #include "film.h"
23 #include "cross.h"
24 #include "film.h"
25 #include "log.h"
26 #include "version.h"
27 #include "emailer.h"
28 #include "environment_info.h"
29 #include <boost/foreach.hpp>
30
31 #include "i18n.h"
32
33 using std::string;
34 using std::list;
35 using boost::shared_ptr;
36
37 /** @param film Film thta the problem is with, or 0 */
38 SendProblemReportJob::SendProblemReportJob (
39         shared_ptr<const Film> film,
40         string from,
41         string summary
42         )
43         : Job (film)
44         , _from (from)
45         , _summary (summary)
46 {
47
48 }
49
50 string
51 SendProblemReportJob::name () const
52 {
53         if (!_film) {
54                 return _("Email problem report");
55         }
56
57         return String::compose (_("Email problem report for %1"), _film->name());
58 }
59
60 string
61 SendProblemReportJob::json_name () const
62 {
63         return N_("send_problem_report");
64 }
65
66 void
67 SendProblemReportJob::run ()
68 {
69         sub (_("Sending email"));
70         set_progress_unknown ();
71
72         string body = _summary + "\n\n";
73
74         body += "Version: " + string (dcpomatic_version) + " " + string (dcpomatic_git_commit) + "\n\n";
75
76         BOOST_FOREACH (string i, environment_info ()) {
77                 body += i + "\n";
78         }
79
80         body += "\n";
81
82         if (_film) {
83                 body += "log head and tail:\n";
84                 body += "---<8----\n";
85                 body += _film->log()->head_and_tail (4096);
86                 body += "---<8----\n\n";
87
88                 add_file (body, "ffprobe.log");
89                 add_file (body, "metadata.xml");
90         }
91
92         list<string> to;
93         to.push_back ("carl@dcpomatic.com");
94
95         Emailer emailer (_from, to, "DCP-o-matic problem report", body);
96         emailer.send ("main.carlh.net", 2525);
97
98         set_progress (1);
99         set_state (FINISHED_OK);
100 }
101
102 void
103 SendProblemReportJob::add_file (string& body, boost::filesystem::path file) const
104 {
105         FILE* f = fopen_boost (_film->file (file), "r");
106         if (!f) {
107                 return;
108         }
109
110         body += file.string() + ":\n";
111         body += "---<8----\n";
112         uintmax_t const size = boost::filesystem::file_size (_film->file (file));
113         char* buffer = new char[size + 1];
114         int const N = fread (buffer, 1, size, f);
115         buffer[N] = '\0';
116         body += buffer;
117         delete[] buffer;
118         body += "---<8----\n\n";
119         fclose (f);
120 }