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