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