Various work on certificate handling for screens; need XML config here, now.
[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
47         add_label_to_sizer (vertical, this, "Make KDMs for");
48
49         wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
50         
51         _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS);
52         targets->Add (_targets, 1, wxEXPAND | wxALL, 6);
53
54         _root = _targets->AddRoot ("Foo");
55
56         list<shared_ptr<Cinema> > c = Config::instance()->cinemas ();
57         for (list<shared_ptr<Cinema> >::iterator i = c.begin(); i != c.end(); ++i) {
58                 add_cinema (*i);
59         }
60
61         _targets->ExpandAll ();
62
63         wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
64
65         _add_cinema = new wxButton (this, wxID_ANY, _("Add Cinema..."));
66         target_buttons->Add (_add_cinema, 1, 0, 6);
67         _edit_cinema = new wxButton (this, wxID_ANY, _("Edit Cinema..."));
68         target_buttons->Add (_edit_cinema, 1, 0, 6);
69         _remove_cinema = new wxButton (this, wxID_ANY, _("Remove Cinema"));
70         target_buttons->Add (_remove_cinema, 1, 0, 6);
71         
72         _add_screen = new wxButton (this, wxID_ANY, _("Add Screen..."));
73         target_buttons->Add (_add_screen, 1, 0, 6);
74         _edit_screen = new wxButton (this, wxID_ANY, _("Edit Screen..."));
75         target_buttons->Add (_edit_screen, 1, 0, 6);
76         _remove_screen = new wxButton (this, wxID_ANY, _("Remove Screen"));
77         target_buttons->Add (_remove_screen, 1, 0, 6);
78
79         targets->Add (target_buttons, 0, 0, 6);
80
81         vertical->Add (targets, 1, wxEXPAND | wxALL, 6);
82
83         wxFlexGridSizer* table = new wxFlexGridSizer (3, 2, 6);
84         add_label_to_sizer (table, this, "From");
85         _from_date = new wxDatePickerCtrl (this, wxID_ANY);
86         table->Add (_from_date, 1, wxEXPAND);
87         _from_time = new wxTimePickerCtrl (this, wxID_ANY);
88         table->Add (_from_time, 1, wxEXPAND);
89         
90         add_label_to_sizer (table, this, "Until");
91         _until_date = new wxDatePickerCtrl (this, wxID_ANY);
92         table->Add (_until_date, 1, wxEXPAND);
93         _until_time = new wxTimePickerCtrl (this, wxID_ANY);
94         table->Add (_until_time, 1, wxEXPAND);
95
96         add_label_to_sizer (table, this, "Write to");
97
98 #ifdef __WXMSW__
99         _folder = new DirPickerCtrl (this);
100 #else   
101         _folder = new wxDirPickerCtrl (this, wxDD_DIR_MUST_EXIST);
102 #endif
103
104         table->Add (_folder, 1, wxEXPAND);
105         
106         vertical->Add (table, 0, wxEXPAND | wxALL, 6);
107
108         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
109         if (buttons) {
110                 vertical->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
111         }
112
113         _targets->Connect (wxID_ANY, wxEVT_COMMAND_TREE_SEL_CHANGED, wxCommandEventHandler (KDMDialog::targets_selection_changed), 0, this);
114
115         _add_cinema->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::add_cinema_clicked), 0, this);
116         _edit_cinema->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::edit_cinema_clicked), 0, this);
117         _remove_cinema->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::remove_cinema_clicked), 0, this);
118
119         _add_screen->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::add_screen_clicked), 0, this);
120         _edit_screen->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::edit_screen_clicked), 0, this);
121         _remove_screen->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (KDMDialog::remove_screen_clicked), 0, this);
122
123         setup_sensitivity ();
124         
125         SetSizer (vertical);
126         vertical->Layout ();
127         vertical->SetSizeHints (this);
128 }
129
130 list<pair<wxTreeItemId, shared_ptr<Cinema> > >
131 KDMDialog::selected_cinemas () const
132 {
133         wxArrayTreeItemIds s;
134         _targets->GetSelections (s);
135
136         list<pair<wxTreeItemId, shared_ptr<Cinema> > > c;
137         for (size_t i = 0; i < s.GetCount(); ++i) {
138                 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator j = _cinemas.find (s[i]);
139                 if (j != _cinemas.end ()) {
140                         c.push_back (make_pair (j->first, j->second));
141                 }
142         }
143
144         return c;
145 }
146
147 list<pair<wxTreeItemId, shared_ptr<Screen> > >
148 KDMDialog::selected_screens () const
149 {
150         wxArrayTreeItemIds s;
151         _targets->GetSelections (s);
152
153         list<pair<wxTreeItemId, shared_ptr<Screen> > > c;
154         for (size_t i = 0; i < s.GetCount(); ++i) {
155                 map<wxTreeItemId, shared_ptr<Screen> >::const_iterator j = _screens.find (s[i]);
156                 if (j != _screens.end ()) {
157                         c.push_back (make_pair (j->first, j->second));
158                 }
159         }
160
161         return c;
162 }
163
164 void
165 KDMDialog::targets_selection_changed (wxCommandEvent &)
166 {
167         setup_sensitivity ();
168 }
169
170 void
171 KDMDialog::setup_sensitivity ()
172 {
173         bool const sc = selected_cinemas().size() == 1;
174         bool const ss = selected_screens().size() == 1;
175         
176         _edit_cinema->Enable (sc);
177         _remove_cinema->Enable (sc);
178         
179         _add_screen->Enable (sc);
180         _edit_screen->Enable (ss);
181         _remove_screen->Enable (ss);
182 }
183
184 void
185 KDMDialog::add_cinema (shared_ptr<Cinema> c)
186 {
187         _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
188
189         for (list<shared_ptr<Screen> >::iterator i = c->screens.begin(); i != c->screens.end(); ++i) {
190                 add_screen (c, *i);
191         }
192 }
193
194 void
195 KDMDialog::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
196 {
197         map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator i = _cinemas.begin();
198         while (i != _cinemas.end() && i->second != c) {
199                 ++i;
200         }
201
202         if (i == _cinemas.end()) {
203                 return;
204         }
205
206         _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
207 }
208
209 void
210 KDMDialog::add_cinema_clicked (wxCommandEvent &)
211 {
212         CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
213         d->ShowModal ();
214
215         shared_ptr<Cinema> c (new Cinema (d->name(), d->email()));
216         Config::instance()->add_cinema (c);
217         add_cinema (c);
218
219         Config::instance()->write ();
220         
221         d->Destroy ();
222 }
223
224 void
225 KDMDialog::edit_cinema_clicked (wxCommandEvent &)
226 {
227         if (selected_cinemas().size() != 1) {
228                 return;
229         }
230
231         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
232         
233         CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->email);
234         d->ShowModal ();
235
236         c.second->name = d->name ();
237         c.second->email = d->email ();
238         _targets->SetItemText (c.first, std_to_wx (d->name()));
239
240         Config::instance()->write ();
241
242         d->Destroy ();  
243 }
244
245 void
246 KDMDialog::remove_cinema_clicked (wxCommandEvent &)
247 {
248         if (selected_cinemas().size() != 1) {
249                 return;
250         }
251
252         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
253
254         Config::instance()->remove_cinema (c.second);
255         _targets->Delete (c.first);
256
257         Config::instance()->write ();   
258 }
259
260 void
261 KDMDialog::add_screen_clicked (wxCommandEvent &)
262 {
263         if (selected_cinemas().size() != 1) {
264                 return;
265         }
266
267         shared_ptr<Cinema> c = selected_cinemas().front().second;
268         
269         ScreenDialog* d = new ScreenDialog (this, "Add Screen");
270         d->ShowModal ();
271
272         shared_ptr<Screen> s (new Screen (d->name(), d->certificate()));
273         c->screens.push_back (s);
274         add_screen (c, s);
275
276         Config::instance()->write ();
277
278         d->Destroy ();
279 }
280
281 void
282 KDMDialog::edit_screen_clicked (wxCommandEvent &)
283 {
284         if (selected_screens().size() != 1) {
285                 return;
286         }
287
288         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
289         
290         ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->certificate);
291         d->ShowModal ();
292
293         s.second->name = d->name ();
294         s.second->certificate = d->certificate ();
295         _targets->SetItemText (s.first, std_to_wx (d->name()));
296
297         Config::instance()->write ();
298
299         d->Destroy ();
300 }
301
302 void
303 KDMDialog::remove_screen_clicked (wxCommandEvent &)
304 {
305         if (selected_screens().size() != 1) {
306                 return;
307         }
308
309         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
310
311         map<wxTreeItemId, shared_ptr<Cinema> >::iterator i = _cinemas.begin ();
312         while (i != _cinemas.end() && find (i->second->screens.begin(), i->second->screens.end(), s.second) == i->second->screens.end()) {
313                 ++i;
314         }
315
316         if (i == _cinemas.end()) {
317                 return;
318         }
319
320         i->second->screens.remove (s.second);
321         _targets->Delete (s.first);
322
323         Config::instance()->write ();
324 }
325
326 list<shared_ptr<Screen> >
327 KDMDialog::screens () const
328 {
329         list<shared_ptr<Screen> > s;
330
331         list<pair<wxTreeItemId, shared_ptr<Cinema> > > cinemas = selected_cinemas ();
332         for (list<pair<wxTreeItemId, shared_ptr<Cinema> > >::iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
333                 for (list<shared_ptr<Screen> >::iterator j = i->second->screens.begin(); j != i->second->screens.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 }