Make sure program version gets into a problem report email.
[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 "quickmail.h"
27 #include "version.h"
28
29 #include "i18n.h"
30
31 using std::string;
32 using std::list;
33 using boost::shared_ptr;
34
35 SendProblemReportJob::SendProblemReportJob (
36         shared_ptr<const Film> film,
37         string from,
38         string summary
39         )
40         : Job (film)
41         , _from (from)
42         , _summary (summary)
43 {
44
45 }
46
47 string
48 SendProblemReportJob::name () const
49 {
50         return String::compose (_("Email problem report for %1"), _film->name());
51 }
52
53 string
54 SendProblemReportJob::json_name () const
55 {
56         return N_("send_problem_report");
57 }
58
59 void
60 SendProblemReportJob::run ()
61 {
62         sub (_("Sending email"));
63         set_progress_unknown ();
64
65         quickmail mail = quickmail_create (_from.c_str(), "DCP-o-matic problem report");
66
67         quickmail_add_to (mail, "carl@dcpomatic.com");
68
69         string body = _summary + "\n\n";
70
71         body += string (dcpomatic_version) + " " + string (dcpomatic_git_commit) + "\n\n";
72
73         body += "log head and tail:\n";
74         body += "---<8----\n";
75         body += _film->log()->head_and_tail ();
76         body += "---<8----\n\n";
77
78         add_file (body, "ffprobe.log");
79         add_file (body, "metadata.xml");
80
81         quickmail_set_body (mail, body.c_str());
82
83         char const* error = quickmail_send (mail, "main.carlh.net", 2525, 0, 0);
84
85         if (error) {
86                 set_state (FINISHED_ERROR);
87                 set_error (error, "");
88         } else {
89                 set_state (FINISHED_OK);
90         }
91
92         quickmail_destroy (mail);
93
94         set_progress (1);
95 }
96
97 void
98 SendProblemReportJob::add_file (string& body, boost::filesystem::path file) const
99 {
100         FILE* f = fopen_boost (_film->file (file), "r");
101         if (!f) {
102                 return;
103         }
104
105         body += file.string() + ":\n";
106         body += "---<8----\n";
107         uintmax_t const size = boost::filesystem::file_size (_film->file (file));
108         char* buffer = new char[size + 1];
109         int const N = fread (buffer, 1, size, f);
110         buffer[N] = '\0';
111         body += buffer;
112         delete[] buffer;
113         body += "---<8----\n\n";
114         fclose (f);
115 }