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