Various hacking.
[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 namespace std;
32 using namespace boost;
33
34 /** Must be called in the GUI thread */
35 JobManagerView::JobManagerView (wxWindow* parent)
36         : wxPanel (parent)
37 {
38         _sizer = new wxFlexGridSizer (2, 6, 6);
39         SetSizer (_sizer);
40
41 #if 0   
42         add_label_to_sizer (_sizer, this, "Hello world");
43         wxGauge* g = new wxGauge (this, wxID_ANY, 100);
44         _sizer->Add (g, 1, wxEXPAND | wxALL);
45
46         add_label_to_sizer (_sizer, this, "Shit");
47         g = new wxGauge (this, wxID_ANY, 100);
48         _sizer->Add (g, 1, wxEXPAND | wxALL);
49 #endif  
50         
51         Connect (wxID_ANY, wxEVT_TIMER, wxTimerEventHandler (JobManagerView::periodic), 0, this);
52         _timer.reset (new wxTimer (this));
53         _timer->Start (1000);
54
55         update ();
56 }
57
58 void
59 JobManagerView::periodic (wxTimerEvent &)
60 {
61         update ();
62 }
63
64 /** Update the view by examining the state of each job.
65  *  Must be called in the GUI thread.
66  */
67 void
68 JobManagerView::update ()
69 {
70         list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
71
72         for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
73                 
74                 if (_job_records.find (*i) == _job_records.end ()) {
75                         add_label_to_sizer (_sizer, this, (*i)->name ());
76
77                         JobRecord r;
78                         r.gauge = new wxGauge (this, wxID_ANY, 100);
79                         r.informed_of_finish = false;
80                         
81                         _sizer->Add (r.gauge, 1, wxEXPAND);
82                         _job_records[*i] = r;
83                         _sizer->Layout ();
84                 }
85
86                 bool inform_of_finish = false;
87                 string const st = (*i)->status ();
88
89                 if (!(*i)->finished ()) {
90                         float const p = (*i)->overall_progress ();
91                         if (p >= 0) {
92 //                              r[_columns.text] = st;
93                                 _job_records[*i].gauge->SetValue (p * 100);
94                         } else {
95 //                              r[_columns.text] = "Running";
96                                 _job_records[*i].gauge->Pulse ();
97                         }
98                 }
99                 
100                 /* Hack to work around our lack of cross-thread
101                    signalling; we tell the job to emit_finished()
102                    from here (the GUI thread).
103                 */
104                 
105                 if ((*i)->finished_ok ()) {
106                         if (!_job_records[*i].informed_of_finish) {
107                                 _job_records[*i].gauge->SetValue (100);
108 //                              r[_columns.text] = st;
109                                 inform_of_finish = true;
110                         }
111                 } else if ((*i)->finished_in_error ()) {
112                         if (!_job_records[*i].informed_of_finish) {
113                                 _job_records[*i].gauge->SetValue (100);
114 //                              r[_columns.text] = st;
115                                 inform_of_finish = true;
116                         }
117                 }
118
119                 if (inform_of_finish) {
120                         try {
121                                 (*i)->emit_finished ();
122                         } catch (OpenFileError& e) {
123                                 stringstream s;
124                                 s << "Error: " << e.what();
125                                 error_dialog (this, s.str ());
126                         }
127                         _job_records[*i].informed_of_finish = true;
128                 }
129         }
130 }