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