Various KDM and encryption fixes.
[dcpomatic.git] / src / wx / kdm_dialog.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <wx/treectrl.h>
21 #include <wx/datectrl.h>
22 #include <wx/timectrl.h>
23 #include "lib/cinema.h"
24 #include "lib/config.h"
25 #include "kdm_dialog.h"
26 #include "cinema_dialog.h"
27 #include "screen_dialog.h"
28 #include "wx_util.h"
29 #ifdef __WXMSW__
30 #include "dir_picker_ctrl.h"
31 #else
32 #include <wx/filepicker.h>
33 #endif
34
35 using std::string;
36 using std::map;
37 using std::list;
38 using std::pair;
39 using std::make_pair;
40 using boost::shared_ptr;
41
42 KDMDialog::KDMDialog (wxWindow* parent)
43         : wxDialog (parent, wxID_ANY, _("Make KDMs"))
44 {
45         wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
46         wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
47         
48         _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS);
49         targets->Add (_targets, 1, wxEXPAND | wxALL, 6);
50
51         _root = _targets->AddRoot ("Foo");
52
53         list<shared_ptr<Cinema> > c = Config::instance()->cinemas ();
54         for (list<shared_ptr<Cinema> >::iterator i = c.begin(); i != c.end(); ++i) {
55                 add_cinema (*i);
56         }
57
58         _targets->ExpandAll ();
59
60         wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
61
62         _add_cinema = new wxButton (this, wxID_ANY, _("Add Cinema..."));
63         target_buttons->Add (_add_cinema, 1, wxEXPAND, 6);
64         _edit_cinema = new wxButton (this, wxID_ANY, _("Edit Cinema..."));
65         target_buttons->Add (_edit_cinema, 1, wxEXPAND, 6);
66         _remove_cinema = new wxButton (this, wxID_ANY, _("Remove Cinema"));
67         target_buttons->Add (_remove_cinema, 1, wxEXPAND, 6);
68         
69         _add_screen = new wxButton (this, wxID_ANY, _("Add Screen..."));
70         target_buttons->Add (_add_screen, 1, wxEXPAND, 6);
71         _edit_screen = new wxButton (this, wxID_ANY, _("Edit Screen..."));
72         target_buttons->Add (_edit_screen, 1, wxEXPAND, 6);
73         _remove_screen = new wxButton (this, wxID_ANY, _("Remove Screen"));
74         target_buttons->Add (_remove_screen, 1, wxEXPAND, 6);
75
76         targets->Add (target_buttons, 0, 0, 6);
77
78         vertical->Add (targets, 1, wxEXPAND | wxALL, 6);
79
80         wxFlexGridSizer* table = new wxFlexGridSizer (3, 2, 6);
81         add_label_to_sizer (table, this, "From", true);
82         _from_date = new wxDatePickerCtrl (this, wxID_ANY);
83         table->Add (_from_date, 1, wxEXPAND);
84         _from_time = new wxTimePickerCtrl (this, wxID_ANY);
85         table->Add (_from_time, 1, wxEXPAND);
86         
87         add_label_to_sizer (table, this, "Until", true);
88         _until_date = new wxDatePickerCtrl (this, wxID_ANY);
89         table->Add (_until_date, 1, wxEXPAND);
90         _until_time = new wxTimePickerCtrl (this, wxID_ANY);
91         table->Add (_until_time, 1, wxEXPAND);
92
93         add_label_to_sizer (table, this, "Write to", true);
94
95 #ifdef __WXMSW__
96         _folder = new DirPickerCtrl (this);
97 #else   
98         _folder = new wxDirPickerCtrl (this, wxDD_DIR_MUST_EXIST);
99 #endif
100
101         table->Add (_folder, 1, wxEXPAND);
102         
103         vertical->Add (table, 0, wxEXPAND | wxALL, 6);
104
105         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
106         if (buttons) {
107                 vertical->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
108         }
109
110         _targets->Connect (wxID_ANY, wxEVT_COMMAND_TREE_SEL_CHANGED, wxCommandEventHandler (KDMDialog::targets_selection_changed), 0, this);
111
112         _add_cinema->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::add_cinema_clicked), 0, this);
113         _edit_cinema->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::edit_cinema_clicked), 0, this);
114         _remove_cinema->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::remove_cinema_clicked), 0, this);
115
116         _add_screen->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::add_screen_clicked), 0, this);
117         _edit_screen->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::edit_screen_clicked), 0, this);
118         _remove_screen->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::remove_screen_clicked), 0, this);
119
120         setup_sensitivity ();
121         
122         SetSizer (vertical);
123         vertical->Layout ();
124         vertical->SetSizeHints (this);
125 }
126
127 list<pair<wxTreeItemId, shared_ptr<Cinema> > >
128 KDMDialog::selected_cinemas () const
129 {
130         wxArrayTreeItemIds s;
131         _targets->GetSelections (s);
132
133         list<pair<wxTreeItemId, shared_ptr<Cinema> > > c;
134         for (size_t i = 0; i < s.GetCount(); ++i) {
135                 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator j = _cinemas.find (s[i]);
136                 if (j != _cinemas.end ()) {
137                         c.push_back (make_pair (j->first, j->second));
138                 }
139         }
140
141         return c;
142 }
143
144 list<pair<wxTreeItemId, shared_ptr<Screen> > >
145 KDMDialog::selected_screens () const
146 {
147         wxArrayTreeItemIds s;
148         _targets->GetSelections (s);
149
150         list<pair<wxTreeItemId, shared_ptr<Screen> > > c;
151         for (size_t i = 0; i < s.GetCount(); ++i) {
152                 map<wxTreeItemId, shared_ptr<Screen> >::const_iterator j = _screens.find (s[i]);
153                 if (j != _screens.end ()) {
154                         c.push_back (make_pair (j->first, j->second));
155                 }
156         }
157
158         return c;
159 }
160
161 void
162 KDMDialog::targets_selection_changed (wxCommandEvent &)
163 {
164         setup_sensitivity ();
165 }
166
167 void
168 KDMDialog::setup_sensitivity ()
169 {
170         bool const sc = selected_cinemas().size() == 1;
171         bool const ss = selected_screens().size() == 1;
172         
173         _edit_cinema->Enable (sc);
174         _remove_cinema->Enable (sc);
175         
176         _add_screen->Enable (sc);
177         _edit_screen->Enable (ss);
178         _remove_screen->Enable (ss);
179 }
180
181 void
182 KDMDialog::add_cinema (shared_ptr<Cinema> c)
183 {
184         _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
185
186         list<shared_ptr<Screen> > sc = c->screens ();
187         for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
188                 add_screen (c, *i);
189         }
190 }
191
192 void
193 KDMDialog::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
194 {
195         map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator i = _cinemas.begin();
196         while (i != _cinemas.end() && i->second != c) {
197                 ++i;
198         }
199
200         if (i == _cinemas.end()) {
201                 return;
202         }
203
204         _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
205 }
206
207 void
208 KDMDialog::add_cinema_clicked (wxCommandEvent &)
209 {
210         CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
211         d->ShowModal ();
212
213         shared_ptr<Cinema> c (new Cinema (d->name(), d->email()));
214         Config::instance()->add_cinema (c);
215         add_cinema (c);
216
217         Config::instance()->write ();
218         
219         d->Destroy ();
220 }
221
222 void
223 KDMDialog::edit_cinema_clicked (wxCommandEvent &)
224 {
225         if (selected_cinemas().size() != 1) {
226                 return;
227         }
228
229         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
230         
231         CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->email);
232         d->ShowModal ();
233
234         c.second->name = d->name ();
235         c.second->email = d->email ();
236         _targets->SetItemText (c.first, std_to_wx (d->name()));
237
238         Config::instance()->write ();
239
240         d->Destroy ();  
241 }
242
243 void
244 KDMDialog::remove_cinema_clicked (wxCommandEvent &)
245 {
246         if (selected_cinemas().size() != 1) {
247                 return;
248         }
249
250         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
251
252         Config::instance()->remove_cinema (c.second);
253         _targets->Delete (c.first);
254
255         Config::instance()->write ();   
256 }
257
258 void
259 KDMDialog::add_screen_clicked (wxCommandEvent &)
260 {
261         if (selected_cinemas().size() != 1) {
262                 return;
263         }
264
265         shared_ptr<Cinema> c = selected_cinemas().front().second;
266         
267         ScreenDialog* d = new ScreenDialog (this, "Add Screen");
268         d->ShowModal ();
269
270         shared_ptr<Screen> s (new Screen (d->name(), d->certificate()));
271         c->add_screen (s);
272         add_screen (c, s);
273
274         Config::instance()->write ();
275
276         d->Destroy ();
277 }
278
279 void
280 KDMDialog::edit_screen_clicked (wxCommandEvent &)
281 {
282         if (selected_screens().size() != 1) {
283                 return;
284         }
285
286         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
287         
288         ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->certificate);
289         d->ShowModal ();
290
291         s.second->name = d->name ();
292         s.second->certificate = d->certificate ();
293         _targets->SetItemText (s.first, std_to_wx (d->name()));
294
295         Config::instance()->write ();
296
297         d->Destroy ();
298 }
299
300 void
301 KDMDialog::remove_screen_clicked (wxCommandEvent &)
302 {
303         if (selected_screens().size() != 1) {
304                 return;
305         }
306
307         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
308
309         map<wxTreeItemId, shared_ptr<Cinema> >::iterator i = _cinemas.begin ();
310         list<shared_ptr<Screen> > sc = i->second->screens ();
311         while (i != _cinemas.end() && find (sc.begin(), sc.end(), s.second) == sc.end()) {
312                 ++i;
313         }
314
315         if (i == _cinemas.end()) {
316                 return;
317         }
318
319         i->second->remove_screen (s.second);
320         _targets->Delete (s.first);
321
322         Config::instance()->write ();
323 }
324
325 list<shared_ptr<Screen> >
326 KDMDialog::screens () const
327 {
328         list<shared_ptr<Screen> > s;
329
330         list<pair<wxTreeItemId, shared_ptr<Cinema> > > cinemas = selected_cinemas ();
331         for (list<pair<wxTreeItemId, shared_ptr<Cinema> > >::iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
332                 list<shared_ptr<Screen> > sc = i->second->screens ();
333                 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
334                         s.push_back (*j);
335                 }
336         }
337
338         list<pair<wxTreeItemId, shared_ptr<Screen> > > screens = selected_screens ();
339         for (list<pair<wxTreeItemId, shared_ptr<Screen> > >::iterator i = screens.begin(); i != screens.end(); ++i) {
340                 s.push_back (i->second);
341         }
342
343         s.sort ();
344         s.unique ();
345
346         return s;
347 }
348
349 boost::posix_time::ptime
350 KDMDialog::from () const
351 {
352         return posix_time (_from_date, _from_time);
353 }
354
355 boost::posix_time::ptime
356 KDMDialog::posix_time (wxDatePickerCtrl* date_picker, wxTimePickerCtrl* time_picker)
357 {
358         wxDateTime const date = date_picker->GetValue ();
359         wxDateTime const time = time_picker->GetValue ();
360         return boost::posix_time::ptime (
361                 boost::gregorian::date (date.GetYear(), date.GetMonth() + 1, date.GetDay()),
362                 boost::posix_time::time_duration (time.GetHour(), time.GetMinute(), time.GetSecond())
363                 );
364 }
365
366 boost::posix_time::ptime
367 KDMDialog::until () const
368 {
369         return posix_time (_until_date, _until_time);
370 }
371
372 string
373 KDMDialog::directory () const
374 {
375         return wx_to_std (_folder->GetPath ());
376 }