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