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