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