Missed update to private test repo version.
[dcpomatic.git] / src / wx / job_view.cc
1 /*
2     Copyright (C) 2012-2019 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
22 #include "check_box.h"
23 #include "dcpomatic_button.h"
24 #include "job_view.h"
25 #include "message_dialog.h"
26 #include "static_text.h"
27 #include "wx_util.h"
28 #include "lib/analyse_audio_job.h"
29 #include "lib/compose.hpp"
30 #include "lib/config.h"
31 #include "lib/job.h"
32 #include "lib/job_manager.h"
33 #include "lib/send_notification_email_job.h"
34 #include "lib/transcode_job.h"
35 #include <dcp/warnings.h>
36 LIBDCP_DISABLE_WARNINGS
37 #include <wx/wx.h>
38 LIBDCP_ENABLE_WARNINGS
39 #include <boost/algorithm/string.hpp>
40
41
42 using std::min;
43 using std::shared_ptr;
44 using std::string;
45 using boost::bind;
46
47
48 JobView::JobView (shared_ptr<Job> job, wxWindow* parent, wxWindow* container, wxFlexGridSizer* table)
49         : _job (job)
50         , _table (table)
51         , _parent (parent)
52         , _container (container)
53         , _gauge (0)
54 {
55
56 }
57
58
59 void
60 JobView::setup ()
61 {
62         int n = insert_position ();
63
64         _gauge_message = new wxBoxSizer (wxVERTICAL);
65         _gauge = new wxGauge (_container, wxID_ANY, 100);
66         /* This seems to be required to allow the gauge to shrink under OS X */
67         _gauge->SetMinSize (wxSize (0, -1));
68         _gauge_message->Add (_gauge, 0, wxEXPAND | wxLEFT | wxRIGHT);
69         _message = new StaticText (_container, wxT(" \n "), wxDefaultPosition, wxDefaultSize, wxST_ELLIPSIZE_MIDDLE);
70         _gauge_message->Add (_message, 1, wxEXPAND | wxALL, 6);
71         _table->Insert (n, _gauge_message, 1, wxEXPAND | wxLEFT | wxRIGHT);
72         ++n;
73
74         _buttons = new wxBoxSizer (wxHORIZONTAL);
75
76         _cancel = new Button (_container, _("Cancel"));
77         _cancel->Bind (wxEVT_BUTTON, &JobView::cancel_clicked, this);
78         _buttons->Add (_cancel, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_BUTTON_STACK_GAP);
79
80         _details = new Button (_container, _("Details..."));
81         _details->Bind (wxEVT_BUTTON, &JobView::details_clicked, this);
82         _details->Enable (false);
83         _buttons->Add (_details, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_BUTTON_STACK_GAP);
84
85         finish_setup (_container, _buttons);
86
87         _controls = new wxBoxSizer (wxVERTICAL);
88         _controls->Add (_buttons);
89         _notify = new CheckBox (_container, _("Notify when complete"));
90         _notify->bind(&JobView::notify_clicked, this);
91         _notify->SetValue (Config::instance()->default_notify());
92         _controls->Add (_notify, 0, wxTOP, DCPOMATIC_BUTTON_STACK_GAP);
93
94         _table->Insert (n, _controls, 1, wxALIGN_CENTER_VERTICAL | wxALL, 3);
95
96         _progress_connection = _job->Progress.connect (boost::bind (&JobView::progress, this));
97         _finished_connection = _job->Finished.connect (boost::bind (&JobView::finished, this));
98
99         progress ();
100
101         _table->Layout ();
102 }
103
104
105 void
106 JobView::maybe_pulse ()
107 {
108         if (_gauge && _job->running()) {
109                 auto elapsed = _job->seconds_since_last_progress_update();
110                 if (!_job->progress() || !elapsed || *elapsed > 4) {
111                         _gauge->Pulse ();
112                 }
113         }
114 }
115
116
117 void
118 JobView::progress ()
119 {
120         string whole = "<b>" + _job->name () + "</b>\n";
121         if (!_job->sub_name().empty ()) {
122                 whole += _job->sub_name() + " ";
123         }
124         auto s = _job->status ();
125         /* Watch out for < > in the error string */
126         boost::algorithm::replace_all (s, "<", "&lt;");
127         boost::algorithm::replace_all (s, ">", "&gt;");
128 #ifdef DCPOMATIC_LINUX
129         boost::algorithm::replace_all(s, "_", "__");
130 #endif
131         whole += s;
132         if (whole != _last_message) {
133                 _message->SetLabelMarkup (std_to_wx (whole));
134                 /* This hack fixes the size of _message on OS X */
135                 _message->InvalidateBestSize ();
136                 _message->SetSize (_message->GetBestSize ());
137                 _gauge_message->Layout ();
138                 _last_message = whole;
139         }
140         if (_job->progress ()) {
141                 _gauge->SetValue (min (100.0f, _job->progress().get() * 100));
142         }
143 }
144
145
146 void
147 JobView::finished ()
148 {
149         progress ();
150
151         if (!_job->finished_cancelled ()) {
152                 _gauge->SetValue (100);
153         }
154
155         _cancel->Enable (false);
156         _notify->Enable (false);
157         if (!_job->error_details().empty ()) {
158                 _details->Enable (true);
159         }
160
161         if (_job->message()) {
162                 MessageDialog dialog(_parent, std_to_wx(_job->name()), std_to_wx(_job->message().get()));
163                 dialog.ShowModal();
164         }
165
166         if (_job->enable_notify() && _notify->GetValue()) {
167                 if (Config::instance()->notification(Config::MESSAGE_BOX)) {
168                         wxMessageBox (std_to_wx(_job->name() + ": " + _job->status()), _("DCP-o-matic"), wxICON_INFORMATION);
169                 }
170                 if (Config::instance()->notification(Config::EMAIL)) {
171                         string body = Config::instance()->notification_email();
172                         boost::algorithm::replace_all (body, "$JOB_NAME", _job->name());
173                         boost::algorithm::replace_all (body, "$JOB_STATUS", _job->status());
174                         JobManager::instance()->add_after (_job, shared_ptr<Job> (new SendNotificationEmailJob (body)));
175                 }
176         }
177 }
178
179
180 void
181 JobView::details_clicked (wxCommandEvent &)
182 {
183         auto s = _job->error_summary();
184         s[0] = toupper (s[0]);
185         error_dialog (_parent, std_to_wx(s), std_to_wx(_job->error_details()));
186 }
187
188
189 void
190 JobView::cancel_clicked (wxCommandEvent &)
191 {
192         if (confirm_dialog (_parent, _("Are you sure you want to cancel this job?"))) {
193                 _job->cancel ();
194         }
195 }
196
197
198 void
199 JobView::insert (int pos)
200 {
201         _table->Insert (pos, _gauge_message, 1, wxEXPAND | wxLEFT | wxRIGHT);
202         _table->Insert (pos + 1, _controls, 1, wxALIGN_CENTER_VERTICAL | wxALL, 3);
203         _table->Layout ();
204 }
205
206
207 void
208 JobView::detach ()
209 {
210         _table->Detach (_gauge_message);
211         _table->Detach (_controls);
212 }
213
214
215 void
216 JobView::notify_clicked ()
217 {
218         Config::instance()->set_default_notify (_notify->GetValue ());
219 }