Try to be more careful when loading FrameInfos from disk.
[dcpomatic.git] / src / wx / job_manager_view.cc
1 /*
2     Copyright (C) 2012 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 /** @file src/job_manager_view.cc
21  *  @brief Class generating a GTK widget to show the progress of jobs.
22  */
23
24 #include "lib/job_manager.h"
25 #include "lib/job.h"
26 #include "lib/util.h"
27 #include "lib/exceptions.h"
28 #include "job_manager_view.h"
29 #include "wx_util.h"
30
31 using std::string;
32 using std::list;
33 using std::map;
34 using std::cout;
35 using boost::shared_ptr;
36 using boost::weak_ptr;
37
38 class JobRecord
39 {
40 public:
41         JobRecord (shared_ptr<Job> job, wxScrolledWindow* window, wxPanel* panel, wxFlexGridSizer* table, bool pause)
42                 : _job (job)
43                 , _window (window)
44                 , _panel (panel)
45                 , _table (table)
46         {
47                 int n = 0;
48                 
49                 wxStaticText* m = new wxStaticText (panel, wxID_ANY, std_to_wx (_job->name ()));
50                 table->Insert (n, m, 0, wxALIGN_CENTER_VERTICAL | wxALL, 6);
51                 ++n;
52         
53                 _gauge = new wxGauge (panel, wxID_ANY, 100);
54                 table->Insert (n, _gauge, 1, wxEXPAND | wxLEFT | wxRIGHT);
55                 ++n;
56                 
57                 _message = new wxStaticText (panel, wxID_ANY, std_to_wx (""));
58                 table->Insert (n, _message, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
59                 ++n;
60         
61                 _cancel = new wxButton (panel, wxID_ANY, _("Cancel"));
62                 _cancel->Bind (wxEVT_COMMAND_BUTTON_CLICKED, &JobRecord::cancel_clicked, this);
63                 table->Insert (n, _cancel, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
64                 ++n;
65         
66                 if (pause) {
67                         _pause = new wxButton (_panel, wxID_ANY, _("Pause"));
68                         _pause->Bind (wxEVT_COMMAND_BUTTON_CLICKED, &JobRecord::pause_clicked, this);
69                         table->Insert (n, _pause, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
70                         ++n;
71                 }
72         
73                 _details = new wxButton (_panel, wxID_ANY, _("Details..."));
74                 _details->Bind (wxEVT_COMMAND_BUTTON_CLICKED, &JobRecord::details_clicked, this);
75                 _details->Enable (false);
76                 table->Insert (n, _details, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
77                 ++n;
78         
79                 job->Progress.connect (boost::bind (&JobRecord::progress, this));
80                 job->Finished.connect (boost::bind (&JobRecord::finished, this));
81         
82                 table->Layout ();
83                 panel->FitInside ();
84         }
85
86         void maybe_pulse ()
87         {
88                 if (_job->running() && _job->progress_unknown ()) {
89                         _gauge->Pulse ();
90                 }
91         }
92
93 private:
94
95         void progress ()
96         {
97                 float const p = _job->overall_progress ();
98                 if (p >= 0) {
99                         checked_set (_message, _job->status ());
100                         _gauge->SetValue (p * 100);
101                 }
102
103                 _table->Layout ();
104                 _window->FitInside ();
105         }
106
107         void finished ()
108         {
109                 checked_set (_message, _job->status ());
110                 if (!_job->finished_cancelled ()) {
111                         _gauge->SetValue (100);
112                 }
113                 
114                 _cancel->Enable (false);
115                 if (!_job->error_details().empty ()) {
116                         _details->Enable (true);
117                 }
118                 
119                 _table->Layout ();
120                 _window->FitInside ();
121         }
122
123         void details_clicked (wxCommandEvent &)
124         {
125                 string s = _job->error_summary();
126                 s[0] = toupper (s[0]);
127                 error_dialog (_window, std_to_wx (String::compose ("%1.\n\n%2", s, _job->error_details())));
128         }
129         
130         void cancel_clicked (wxCommandEvent &)
131         {
132                 _job->cancel ();
133         }
134
135         void pause_clicked (wxCommandEvent &)
136         {
137                 if (_job->paused()) {
138                         _job->resume ();
139                         _pause->SetLabel (_("Pause"));
140                 } else {
141                         _job->pause ();
142                         _pause->SetLabel (_("Resume"));
143                 }
144         }
145         
146         boost::shared_ptr<Job> _job;
147         wxScrolledWindow* _window;
148         wxPanel* _panel;
149         wxFlexGridSizer* _table;
150         wxGauge* _gauge;
151         wxStaticText* _message;
152         wxButton* _cancel;
153         wxButton* _pause;
154         wxButton* _details;
155 };
156
157 /** Must be called in the GUI thread */
158 JobManagerView::JobManagerView (wxWindow* parent, Buttons buttons)
159         : wxScrolledWindow (parent)
160         , _buttons (buttons)
161 {
162         _panel = new wxPanel (this);
163         wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
164         sizer->Add (_panel, 1, wxEXPAND);
165         SetSizer (sizer);
166
167         int N = 5;
168         if (buttons & PAUSE) {
169                 ++N;
170         }
171         
172         _table = new wxFlexGridSizer (N, 6, 6);
173         _table->AddGrowableCol (1, 1);
174         _panel->SetSizer (_table);
175
176         SetScrollRate (0, 32);
177
178         Bind (wxEVT_TIMER, boost::bind (&JobManagerView::periodic, this));
179         _timer.reset (new wxTimer (this));
180         _timer->Start (1000);
181         
182         JobManager::instance()->JobAdded.connect (bind (&JobManagerView::job_added, this, _1));
183 }
184
185 void
186 JobManagerView::job_added (weak_ptr<Job> j)
187 {
188         shared_ptr<Job> job = j.lock ();
189         if (job) {
190                 _job_records.push_back (shared_ptr<JobRecord> (new JobRecord (job, this, _panel, _table, _buttons & PAUSE)));
191         }
192 }
193
194 void
195 JobManagerView::periodic ()
196 {
197         for (list<shared_ptr<JobRecord> >::iterator i = _job_records.begin(); i != _job_records.end(); ++i) {
198                 (*i)->maybe_pulse ();
199         }
200 }