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