Merge branch 'master' of ssh://git.carlh.net/home/carl/git/dcpomatic
[dcpomatic.git] / src / wx / kdm_dialog.cc
1 /*
2     Copyright (C) 2012-2016 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 "kdm_dialog.h"
22 #include "wx_util.h"
23 #include "screens_panel.h"
24 #include "kdm_timing_panel.h"
25 #include "kdm_output_panel.h"
26 #include "kdm_cpl_panel.h"
27 #include "confirm_kdm_email_dialog.h"
28 #include "lib/film.h"
29 #include "lib/screen.h"
30 #include "lib/screen_kdm.h"
31 #include "lib/send_kdm_email_job.h"
32 #include "lib/job_manager.h"
33 #include "lib/cinema_kdms.h"
34 #include "lib/config.h"
35 #include "lib/cinema.h"
36 #include <libcxml/cxml.h>
37 #include <dcp/exceptions.h>
38 #include <wx/treectrl.h>
39 #include <wx/listctrl.h>
40 #include <iostream>
41
42 using std::string;
43 using std::exception;
44 using std::map;
45 using std::list;
46 using std::pair;
47 using std::cout;
48 using std::vector;
49 using std::make_pair;
50 using boost::shared_ptr;
51
52 KDMDialog::KDMDialog (wxWindow* parent, shared_ptr<const Film> film)
53         : wxDialog (parent, wxID_ANY, _("Make KDMs"))
54         , _film (film)
55 {
56         /* Main sizers */
57         wxBoxSizer* horizontal = new wxBoxSizer (wxHORIZONTAL);
58         wxBoxSizer* left = new wxBoxSizer (wxVERTICAL);
59         wxBoxSizer* right = new wxBoxSizer (wxVERTICAL);
60
61         horizontal->Add (left, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP * 4);
62         horizontal->Add (right, 1, wxEXPAND);
63
64         /* Font for sub-headings */
65         wxFont subheading_font (*wxNORMAL_FONT);
66         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
67
68         /* Sub-heading: Screens */
69         wxStaticText* h = new wxStaticText (this, wxID_ANY, _("Screens"));
70         h->SetFont (subheading_font);
71         left->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
72         _screens = new ScreensPanel (this);
73         left->Add (_screens, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
74
75         /* Sub-heading: Timing */
76         /// TRANSLATORS: translate the word "Timing" here; do not include the "KDM|" prefix
77         h = new wxStaticText (this, wxID_ANY, S_("KDM|Timing"));
78         h->SetFont (subheading_font);
79         right->Add (h, 0, wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_Y_GAP * 2);
80         _timing = new KDMTimingPanel (this);
81         right->Add (_timing);
82
83         /* Sub-heading: CPL */
84         h = new wxStaticText (this, wxID_ANY, _("CPL"));
85         h->SetFont (subheading_font);
86         right->Add (h, 0, wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_Y_GAP * 2);
87         _cpl = new KDMCPLPanel (this, film->cpls ());
88         right->Add (_cpl, 0, wxEXPAND);
89
90         /* Sub-heading: Output */
91         h = new wxStaticText (this, wxID_ANY, _("Output"));
92         h->SetFont (subheading_font);
93         right->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
94         _output = new KDMOutputPanel (this, film->interop ());
95         right->Add (_output, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP);
96
97         _make = new wxButton (this, wxID_ANY, _("Make KDMs"));
98         right->Add (_make, 0, wxTOP | wxBOTTOM, DCPOMATIC_SIZER_GAP);
99
100         /* Make an overall sizer to get a nice border */
101
102         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
103         overall_sizer->Add (horizontal, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, DCPOMATIC_DIALOG_BORDER);
104
105         /* Bind */
106
107         _screens->ScreensChanged.connect (boost::bind (&KDMDialog::setup_sensitivity, this));
108         _timing->TimingChanged.connect (boost::bind (&KDMDialog::setup_sensitivity, this));
109         _make->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::make_clicked, this));
110
111         setup_sensitivity ();
112
113         SetSizer (overall_sizer);
114         overall_sizer->Layout ();
115         overall_sizer->SetSizeHints (this);
116 }
117
118 void
119 KDMDialog::setup_sensitivity ()
120 {
121         _screens->setup_sensitivity ();
122         _output->setup_sensitivity ();
123         _make->Enable (!_screens->screens().empty() && _timing->valid() && _cpl->has_selected());
124 }
125
126 void
127 KDMDialog::make_clicked ()
128 {
129         shared_ptr<const Film> film = _film.lock ();
130         DCPOMATIC_ASSERT (film);
131
132         _output->save_kdm_name_format ();
133
134         try {
135                 list<ScreenKDM> screen_kdms = film->make_kdms (
136                         _screens->screens(), _cpl->cpl(), _timing->from(), _timing->until(), _output->formulation()
137                         );
138
139                 dcp::NameFormat::Map name_values;
140                 name_values['f'] = film->name();
141                 name_values['b'] = dcp::LocalTime(_timing->from()).date() + " " + dcp::LocalTime(_timing->from()).time_of_day();
142                 name_values['e'] = dcp::LocalTime(_timing->until()).date() + " " + dcp::LocalTime(_timing->until()).time_of_day();
143
144                 if (_output->write_to ()) {
145                         ScreenKDM::write_files (
146                                 screen_kdms,
147                                 _output->directory(),
148                                 _output->name_format(),
149                                 name_values
150                                 );
151                 }
152
153                 if (_output->email ()) {
154
155                         list<CinemaKDMs> const cinema_kdms = CinemaKDMs::collect (screen_kdms);
156
157                         bool ok = true;
158
159                         if (Config::instance()->confirm_kdm_email ()) {
160                                 list<string> emails;
161                                 BOOST_FOREACH (CinemaKDMs i, cinema_kdms) {
162                                         BOOST_FOREACH (string j, i.cinema->emails) {
163                                                 emails.push_back (j);
164                                         }
165                                 }
166
167                                 ConfirmKDMEmailDialog* d = new ConfirmKDMEmailDialog (this, emails);
168                                 if (d->ShowModal() == wxID_CANCEL) {
169                                         ok = false;
170                                 }
171                         }
172
173                         if (ok) {
174                                 JobManager::instance()->add (
175                                         shared_ptr<Job> (new SendKDMEmailJob (
176                                                                  cinema_kdms,
177                                                                  _output->name_format(),
178                                                                  name_values,
179                                                                  film->dcp_name(),
180                                                                  film->log()
181                                                                  ))
182                                         );
183                         }
184                 }
185         } catch (dcp::NotEncryptedError& e) {
186                 error_dialog (this, _("CPL's content is not encrypted."));
187         } catch (exception& e) {
188                 error_dialog (this, e.what ());
189         } catch (...) {
190                 error_dialog (this, _("An unknown exception occurred."));
191         }
192 }