Basics of job view when sending KDM emails from dcpomatic_kdm.
[dcpomatic.git] / src / wx / job_view.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "job_view.h"
21 #include "wx_util.h"
22 #include "lib/job.h"
23 #include "lib/compose.hpp"
24 #include <wx/wx.h>
25
26 using std::string;
27 using std::min;
28 using boost::shared_ptr;
29
30 JobView::JobView (shared_ptr<Job> job, wxWindow* parent, wxWindow* container, wxFlexGridSizer* table)
31         : _job (job)
32         , _parent (parent)
33 {
34         int n = 0;
35
36         _gauge_message = new wxBoxSizer (wxVERTICAL);
37         _gauge = new wxGauge (container, wxID_ANY, 100);
38         /* This seems to be required to allow the gauge to shrink under OS X */
39         _gauge->SetMinSize (wxSize (0, -1));
40         _gauge_message->Add (_gauge, 0, wxEXPAND | wxLEFT | wxRIGHT);
41         _message = new wxStaticText (container, wxID_ANY, wxT (" \n "));
42         _gauge_message->Add (_message, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
43         table->Insert (n, _gauge_message, 1, wxEXPAND | wxLEFT | wxRIGHT);
44         ++n;
45
46         _cancel = new wxButton (container, wxID_ANY, _("Cancel"));
47         _cancel->Bind (wxEVT_COMMAND_BUTTON_CLICKED, &JobView::cancel_clicked, this);
48         table->Insert (n, _cancel, 1, wxALIGN_CENTER_VERTICAL | wxALL, 3);
49         ++n;
50
51         _pause = new wxButton (container, wxID_ANY, _("Pause"));
52         _pause->Bind (wxEVT_COMMAND_BUTTON_CLICKED, &JobView::pause_clicked, this);
53         table->Insert (n, _pause, 1, wxALIGN_CENTER_VERTICAL | wxALL, 3);
54         ++n;
55
56         _details = new wxButton (container, wxID_ANY, _("Details..."));
57         _details->Bind (wxEVT_COMMAND_BUTTON_CLICKED, &JobView::details_clicked, this);
58         _details->Enable (false);
59         table->Insert (n, _details, 1, wxALIGN_CENTER_VERTICAL | wxALL, 3);
60         ++n;
61
62         _progress_connection = job->Progress.connect (boost::bind (&JobView::progress, this));
63         _finished_connection = job->Finished.connect (boost::bind (&JobView::finished, this));
64
65         progress ();
66
67         table->Layout ();
68 }
69
70 void
71 JobView::maybe_pulse ()
72 {
73         if (_job->running() && !_job->progress ()) {
74                 _gauge->Pulse ();
75         }
76 }
77
78 void
79 JobView::progress ()
80 {
81         string whole = "<b>" + _job->name () + "</b>\n";
82         if (!_job->sub_name().empty ()) {
83                 whole += _job->sub_name() + " ";
84         }
85         whole += _job->status ();
86         if (whole != _last_message) {
87                 _message->SetLabelMarkup (std_to_wx (whole));
88                 _gauge_message->Layout ();
89                 _last_message = whole;
90         }
91         if (_job->progress ()) {
92                 _gauge->SetValue (min (100.0f, _job->progress().get() * 100));
93         }
94 }
95
96 void
97 JobView::finished ()
98 {
99         progress ();
100
101         if (!_job->finished_cancelled ()) {
102                 _gauge->SetValue (100);
103         }
104
105         _cancel->Enable (false);
106         _pause->Enable (false);
107         if (!_job->error_details().empty ()) {
108                 _details->Enable (true);
109         }
110 }
111
112 void
113 JobView::details_clicked (wxCommandEvent &)
114 {
115         string s = _job->error_summary();
116         s[0] = toupper (s[0]);
117         error_dialog (_parent, std_to_wx (String::compose ("%1.\n\n%2", s, _job->error_details())));
118 }
119
120 void
121 JobView::cancel_clicked (wxCommandEvent &)
122 {
123         _job->cancel ();
124 }
125
126 void
127 JobView::pause_clicked (wxCommandEvent &)
128 {
129         if (_job->paused()) {
130                 _job->resume ();
131                 _pause->SetLabel (_("Pause"));
132         } else {
133                 _job->pause ();
134                 _pause->SetLabel (_("Resume"));
135         }
136 }