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