Tidy and fix logging.
[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 std::runtime_error;
51 using boost::shared_ptr;
52 using boost::bind;
53
54 KDMDialog::KDMDialog (wxWindow* parent, shared_ptr<const Film> film)
55         : wxDialog (parent, wxID_ANY, _("Make KDMs"))
56         , _film (film)
57 {
58         /* Main sizers */
59         wxBoxSizer* horizontal = new wxBoxSizer (wxHORIZONTAL);
60         wxBoxSizer* left = new wxBoxSizer (wxVERTICAL);
61         wxBoxSizer* right = new wxBoxSizer (wxVERTICAL);
62
63         horizontal->Add (left, 1, wxEXPAND | wxRIGHT, DCPOMATIC_SIZER_X_GAP * 4);
64         horizontal->Add (right, 1, wxEXPAND);
65
66         /* Font for sub-headings */
67         wxFont subheading_font (*wxNORMAL_FONT);
68         subheading_font.SetWeight (wxFONTWEIGHT_BOLD);
69
70         /* Sub-heading: Screens */
71         wxStaticText* h = new wxStaticText (this, wxID_ANY, _("Screens"));
72         h->SetFont (subheading_font);
73         left->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
74         _screens = new ScreensPanel (this);
75         left->Add (_screens, 1, wxEXPAND | wxBOTTOM, DCPOMATIC_SIZER_Y_GAP);
76
77         /* Sub-heading: Timing */
78         /// TRANSLATORS: translate the word "Timing" here; do not include the "KDM|" prefix
79         h = new wxStaticText (this, wxID_ANY, S_("KDM|Timing"));
80         h->SetFont (subheading_font);
81         right->Add (h, 0, wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_Y_GAP * 2);
82         _timing = new KDMTimingPanel (this);
83         right->Add (_timing);
84
85         /* Sub-heading: CPL */
86         h = new wxStaticText (this, wxID_ANY, _("CPL"));
87         h->SetFont (subheading_font);
88         right->Add (h, 0, wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_Y_GAP * 2);
89         _cpl = new KDMCPLPanel (this, film->cpls ());
90         right->Add (_cpl, 0, wxEXPAND);
91
92         /* Sub-heading: Output */
93         h = new wxStaticText (this, wxID_ANY, _("Output"));
94         h->SetFont (subheading_font);
95         right->Add (h, 0, wxALIGN_CENTER_VERTICAL | wxTOP, DCPOMATIC_SIZER_Y_GAP * 2);
96         _output = new KDMOutputPanel (this, film->interop ());
97         right->Add (_output, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP);
98
99         _make = new wxButton (this, wxID_ANY, _("Make KDMs"));
100         right->Add (_make, 0, wxTOP | wxBOTTOM, DCPOMATIC_SIZER_GAP);
101
102         /* Make an overall sizer to get a nice border */
103
104         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
105         overall_sizer->Add (horizontal, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, DCPOMATIC_DIALOG_BORDER);
106
107         /* Bind */
108
109         _screens->ScreensChanged.connect (boost::bind (&KDMDialog::setup_sensitivity, this));
110         _timing->TimingChanged.connect (boost::bind (&KDMDialog::setup_sensitivity, this));
111         _make->Bind (wxEVT_BUTTON, boost::bind (&KDMDialog::make_clicked, this));
112
113         setup_sensitivity ();
114
115         SetSizer (overall_sizer);
116         overall_sizer->Layout ();
117         overall_sizer->SetSizeHints (this);
118 }
119
120 void
121 KDMDialog::setup_sensitivity ()
122 {
123         _screens->setup_sensitivity ();
124         _output->setup_sensitivity ();
125         _make->Enable (!_screens->screens().empty() && _timing->valid() && _cpl->has_selected());
126 }
127
128 bool
129 KDMDialog::confirm_overwrite (boost::filesystem::path path)
130 {
131         return confirm_dialog (
132                 this,
133                 wxString::Format (_("File %s already exists.  Do you want to overwrite it?"), std_to_wx(path.string()).data())
134                 );
135 }
136
137 void
138 KDMDialog::make_clicked ()
139 {
140         shared_ptr<const Film> film = _film.lock ();
141         DCPOMATIC_ASSERT (film);
142
143         list<ScreenKDM> screen_kdms;
144         try {
145
146                 screen_kdms = film->make_kdms (
147                         _screens->screens(), _cpl->cpl(), _timing->from(), _timing->until(), _output->formulation(),
148                         !_output->forensic_mark_video(), _output->forensic_mark_audio() ? boost::optional<int>() : 0
149                         );
150
151         } catch (runtime_error& e) {
152                 error_dialog (this, std_to_wx(e.what()));
153                 return;
154         }
155
156         pair<shared_ptr<Job>, int> result = _output->make (screen_kdms, film->name(), _timing, bind (&KDMDialog::confirm_overwrite, this, _1));
157         if (result.first) {
158                 JobManager::instance()->add (result.first);
159         }
160
161         if (result.second > 0) {
162                 /* XXX: proper plural form support in wxWidgets? */
163                 wxString s = result.second == 1 ? _("%d KDM written to %s") : _("%d KDMs written to %s");
164                 message_dialog (
165                         this,
166                         wxString::Format (s, result.second, std_to_wx(_output->directory().string()).data())
167                         );
168         }
169 }