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