Splash behaviour does not need to be branded after all.
[dcpomatic.git] / src / wx / kdm_cpl_panel.cc
1 /*
2     Copyright (C) 2015-2021 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
22 #include "dcpomatic_button.h"
23 #include "kdm_cpl_panel.h"
24 #include "static_text.h"
25 #include "wx_util.h"
26 #include <dcp/filesystem.h>
27 #include <dcp/warnings.h>
28 LIBDCP_DISABLE_WARNINGS
29 #include <libxml++/libxml++.h>
30 LIBDCP_ENABLE_WARNINGS
31 #include <libcxml/cxml.h>
32
33
34 using std::vector;
35
36
37 KDMCPLPanel::KDMCPLPanel (wxWindow* parent, vector<CPLSummary> cpls)
38         : wxPanel (parent, wxID_ANY)
39         , _cpls (cpls)
40 {
41         auto vertical = new wxBoxSizer (wxVERTICAL);
42
43         /* CPL choice */
44         auto s = new wxBoxSizer (wxHORIZONTAL);
45         add_label_to_sizer (s, this, _("CPL"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
46         _cpl = new wxChoice (this, wxID_ANY);
47         s->Add (_cpl, 1, wxTOP | wxEXPAND, DCPOMATIC_CHOICE_TOP_PAD);
48         _cpl_browse = new Button (this, _("Browse..."));
49         s->Add (_cpl_browse, 0, wxLEFT, DCPOMATIC_SIZER_X_GAP);
50         vertical->Add (s, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP + 2);
51
52         /* CPL details */
53         auto table = new wxFlexGridSizer(2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
54         add_label_to_sizer (table, this, _("DCP directory"), true);
55         _dcp_directory = new StaticText (this, "");
56         table->Add (_dcp_directory);
57         add_label_to_sizer (table, this, _("CPL ID"), true);
58         _cpl_id = new StaticText (this, "");
59         table->Add (_cpl_id);
60         add_label_to_sizer (table, this, _("CPL annotation text"), true);
61         _cpl_annotation_text = new StaticText (this, "");
62         table->Add (_cpl_annotation_text);
63         vertical->Add (table, 0, wxEXPAND | wxTOP, DCPOMATIC_SIZER_GAP + 2);
64
65         update_cpl_choice ();
66
67         _cpl->Bind(wxEVT_CHOICE, boost::bind(&KDMCPLPanel::update_cpl_summary, this));
68         _cpl_browse->Bind(wxEVT_BUTTON, boost::bind(&KDMCPLPanel::cpl_browse_clicked, this));
69
70         SetSizerAndFit (vertical);
71 }
72
73 void
74 KDMCPLPanel::update_cpl_choice ()
75 {
76         _cpl->Clear ();
77
78         for (auto const& i: _cpls) {
79                 _cpl->Append (std_to_wx(i.cpl_id));
80
81                 if (_cpls.size() > 0) {
82                         _cpl->SetSelection (0);
83                 }
84         }
85
86         update_cpl_summary ();
87 }
88
89 void
90 KDMCPLPanel::update_cpl_summary ()
91 {
92         int const n = _cpl->GetSelection();
93         if (n == wxNOT_FOUND) {
94                 return;
95         }
96
97         _dcp_directory->SetLabel (std_to_wx (_cpls[n].dcp_directory));
98         _cpl_id->SetLabel (std_to_wx (_cpls[n].cpl_id));
99         _cpl_annotation_text->SetLabel (std_to_wx(_cpls[n].cpl_annotation_text.get_value_or("")));
100
101         Changed();
102 }
103
104 void
105 KDMCPLPanel::cpl_browse_clicked ()
106 {
107         auto d = make_wx<wxFileDialog>(this, _("Select CPL XML file"), wxEmptyString, wxEmptyString, "*.xml");
108         if (d->ShowModal() == wxID_CANCEL) {
109                 return;
110         }
111
112         boost::filesystem::path cpl_file (wx_to_std (d->GetPath ()));
113         boost::filesystem::path dcp_dir = cpl_file.parent_path ();
114
115         try {
116                 /* XXX: hack alert */
117                 cxml::Document cpl_document ("CompositionPlaylist");
118                 cpl_document.read_file(dcp::filesystem::fix_long_path(cpl_file));
119
120                 bool encrypted = false;
121                 for (auto i: cpl_document.node_children("ReelList")) {
122                         for (auto j: i->node_children("Reel")) {
123                                 for (auto k: j->node_children("AssetList")) {
124                                         for (auto l: k->node_children()) {
125                                                 if (!l->node_children("KeyId").empty()) {
126                                                         encrypted = true;
127                                                 }
128                                         }
129                                 }
130                         }
131                 }
132
133                 if (!encrypted) {
134                         error_dialog (this, _("This CPL contains no encrypted assets."));
135                         return;
136                 }
137
138                 /* We're ignoring the CPLSummary timestamp stuff here and just putting the new one in at the end
139                    of the list, then selecting it.
140                 */
141
142                 _cpls.push_back (
143                         CPLSummary (
144                                 dcp_dir.filename().string(),
145                                 cpl_document.string_child("Id").substr (9),
146                                 cpl_document.string_child("ContentTitleText"),
147                                 cpl_file,
148                                 encrypted,
149                                 0
150                                 )
151                         );
152         } catch (xmlpp::exception& e) {
153                 error_dialog (this, _("This is not a valid CPL file"), std_to_wx(e.what()));
154                 return;
155         } catch (cxml::Error& e) {
156                 error_dialog (this, _("This is not a valid CPL file"), std_to_wx(e.what()));
157                 return;
158         }
159
160         update_cpl_choice ();
161         _cpl->SetSelection (_cpls.size() - 1);
162         update_cpl_summary ();
163 }
164
165 boost::filesystem::path
166 KDMCPLPanel::cpl () const
167 {
168         int const item = _cpl->GetSelection ();
169         DCPOMATIC_ASSERT (item >= 0);
170         return _cpls[item].cpl_file;
171 }
172
173 bool
174 KDMCPLPanel::has_selected () const
175 {
176         return _cpl->GetSelection() != -1;
177 }