Confirm overwrite of KDMs (#1008).
[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 using boost::bind;
52
53 KDMDialog::KDMDialog (wxWindow* parent, shared_ptr<const Film> film)
54         : wxDialog (parent, wxID_ANY, _("Make KDMs"))
55         , _film (film)
56 {
57         /* Main sizers */
58         wxBoxSizer* horizontal = new wxBoxSizer (wxHORIZONTAL);
59         wxBoxSizer* left = new wxBoxSizer (wxVERTICAL);
60         wxBoxSizer* right = new wxBoxSizer (wxVERTICAL);
61
62         horizontal->Add (left, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP * 4);
63         horizontal->Add (right, 1, wxEXPAND);
64
65         /* Font for sub-headings */
66         wxFont subheading_font (*wxNORMAL_FONT);
67         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
68
69         /* Sub-heading: Screens */
70         wxStaticText* h = new wxStaticText (this, wxID_ANY, _("Screens"));
71         h->SetFont (subheading_font);
72         left->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
73         _screens = new ScreensPanel (this);
74         left->Add (_screens, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
75
76         /* Sub-heading: Timing */
77         /// TRANSLATORS: translate the word "Timing" here; do not include the "KDM|" prefix
78         h = new wxStaticText (this, wxID_ANY, S_("KDM|Timing"));
79         h->SetFont (subheading_font);
80         right->Add (h, 0, wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_Y_GAP * 2);
81         _timing = new KDMTimingPanel (this);
82         right->Add (_timing);
83
84         /* Sub-heading: CPL */
85         h = new wxStaticText (this, wxID_ANY, _("CPL"));
86         h->SetFont (subheading_font);
87         right->Add (h, 0, wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_Y_GAP * 2);
88         _cpl = new KDMCPLPanel (this, film->cpls ());
89         right->Add (_cpl, 0, wxEXPAND);
90
91         /* Sub-heading: Output */
92         h = new wxStaticText (this, wxID_ANY, _("Output"));
93         h->SetFont (subheading_font);
94         right->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
95         _output = new KDMOutputPanel (this, film->interop ());
96         right->Add (_output, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP);
97
98         _make = new wxButton (this, wxID_ANY, _("Make KDMs"));
99         right->Add (_make, 0, wxTOP | wxBOTTOM, DCPOMATIC_SIZER_GAP);
100
101         /* Make an overall sizer to get a nice border */
102
103         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
104         overall_sizer->Add (horizontal, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, DCPOMATIC_DIALOG_BORDER);
105
106         /* Bind */
107
108         _screens->ScreensChanged.connect (boost::bind (&KDMDialog::setup_sensitivity, this));
109         _timing->TimingChanged.connect (boost::bind (&KDMDialog::setup_sensitivity, this));
110         _make->Bind (wxEVT_BUTTON, boost::bind (&KDMDialog::make_clicked, this));
111
112         setup_sensitivity ();
113
114         SetSizer (overall_sizer);
115         overall_sizer->Layout ();
116         overall_sizer->SetSizeHints (this);
117 }
118
119 void
120 KDMDialog::setup_sensitivity ()
121 {
122         _screens->setup_sensitivity ();
123         _output->setup_sensitivity ();
124         _make->Enable (!_screens->screens().empty() && _timing->valid() && _cpl->has_selected());
125 }
126
127 bool
128 KDMDialog::confirm_overwrite (boost::filesystem::path path)
129 {
130         return confirm_dialog (
131                 this,
132                 wxString::Format (_("File %s already exists.  Do you want to overwrite it?"), std_to_wx(path.string()).data())
133                 );
134 }
135
136 void
137 KDMDialog::make_clicked ()
138 {
139         shared_ptr<const Film> film = _film.lock ();
140         DCPOMATIC_ASSERT (film);
141
142         _output->save_kdm_name_format ();
143
144         try {
145                 list<ScreenKDM> screen_kdms = film->make_kdms (
146                         _screens->screens(), _cpl->cpl(), _timing->from(), _timing->until(), _output->formulation()
147                         );
148
149                 dcp::NameFormat::Map name_values;
150                 name_values['f'] = film->name();
151                 name_values['b'] = dcp::LocalTime(_timing->from()).date() + " " + dcp::LocalTime(_timing->from()).time_of_day();
152                 name_values['e'] = dcp::LocalTime(_timing->until()).date() + " " + dcp::LocalTime(_timing->until()).time_of_day();
153
154                 if (_output->write_to ()) {
155                         ScreenKDM::write_files (
156                                 screen_kdms,
157                                 _output->directory(),
158                                 _output->name_format(),
159                                 name_values,
160                                 bind (&KDMDialog::confirm_overwrite, this, _1)
161                                 );
162                 }
163
164                 if (_output->email ()) {
165
166                         list<CinemaKDMs> const cinema_kdms = CinemaKDMs::collect (screen_kdms);
167
168                         bool ok = true;
169
170                         if (Config::instance()->confirm_kdm_email ()) {
171                                 list<string> emails;
172                                 BOOST_FOREACH (CinemaKDMs i, cinema_kdms) {
173                                         BOOST_FOREACH (string j, i.cinema->emails) {
174                                                 emails.push_back (j);
175                                         }
176                                 }
177
178                                 ConfirmKDMEmailDialog* d = new ConfirmKDMEmailDialog (this, emails);
179                                 if (d->ShowModal() == wxID_CANCEL) {
180                                         ok = false;
181                                 }
182                         }
183
184                         if (ok) {
185                                 JobManager::instance()->add (
186                                         shared_ptr<Job> (new SendKDMEmailJob (
187                                                                  cinema_kdms,
188                                                                  _output->name_format(),
189                                                                  name_values,
190                                                                  film->dcp_name(),
191                                                                  film->log()
192                                                                  ))
193                                         );
194                         }
195                 }
196         } catch (dcp::NotEncryptedError& e) {
197                 error_dialog (this, _("CPL's content is not encrypted."));
198         } catch (exception& e) {
199                 error_dialog (this, e.what ());
200         } catch (...) {
201                 error_dialog (this, _("An unknown exception occurred."));
202         }
203 }