KDM path 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 <wx/stdpaths.h>
24 #include "lib/cinema.h"
25 #include "lib/config.h"
26 #include "kdm_dialog.h"
27 #include "cinema_dialog.h"
28 #include "screen_dialog.h"
29 #include "wx_util.h"
30 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
31 #include "dir_picker_ctrl.h"
32 #else
33 #include <wx/filepicker.h>
34 #endif
35
36 using std::string;
37 using std::map;
38 using std::list;
39 using std::pair;
40 using std::make_pair;
41 using boost::shared_ptr;
42
43 KDMDialog::KDMDialog (wxWindow* parent)
44         : wxDialog (parent, wxID_ANY, _("Make KDMs"))
45 {
46         wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
47         wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
48         
49         _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS);
50         targets->Add (_targets, 1, wxEXPAND | wxALL, 6);
51
52         _root = _targets->AddRoot ("Foo");
53
54         list<shared_ptr<Cinema> > c = Config::instance()->cinemas ();
55         for (list<shared_ptr<Cinema> >::iterator i = c.begin(); i != c.end(); ++i) {
56                 add_cinema (*i);
57         }
58
59         _targets->ExpandAll ();
60
61         wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
62
63         _add_cinema = new wxButton (this, wxID_ANY, _("Add Cinema..."));
64         target_buttons->Add (_add_cinema, 1, wxEXPAND, 6);
65         _edit_cinema = new wxButton (this, wxID_ANY, _("Edit Cinema..."));
66         target_buttons->Add (_edit_cinema, 1, wxEXPAND, 6);
67         _remove_cinema = new wxButton (this, wxID_ANY, _("Remove Cinema"));
68         target_buttons->Add (_remove_cinema, 1, wxEXPAND, 6);
69         
70         _add_screen = new wxButton (this, wxID_ANY, _("Add Screen..."));
71         target_buttons->Add (_add_screen, 1, wxEXPAND, 6);
72         _edit_screen = new wxButton (this, wxID_ANY, _("Edit Screen..."));
73         target_buttons->Add (_edit_screen, 1, wxEXPAND, 6);
74         _remove_screen = new wxButton (this, wxID_ANY, _("Remove Screen"));
75         target_buttons->Add (_remove_screen, 1, wxEXPAND, 6);
76
77         targets->Add (target_buttons, 0, 0, 6);
78
79         vertical->Add (targets, 1, wxEXPAND | wxALL, 6);
80
81         wxFlexGridSizer* table = new wxFlexGridSizer (3, 2, 6);
82         add_label_to_sizer (table, this, "From", true);
83         _from_date = new wxDatePickerCtrl (this, wxID_ANY);
84         table->Add (_from_date, 1, wxEXPAND);
85         _from_time = new wxTimePickerCtrl (this, wxID_ANY);
86         table->Add (_from_time, 1, wxEXPAND);
87         
88         add_label_to_sizer (table, this, "Until", true);
89         _until_date = new wxDatePickerCtrl (this, wxID_ANY);
90         table->Add (_until_date, 1, wxEXPAND);
91         _until_time = new wxTimePickerCtrl (this, wxID_ANY);
92         table->Add (_until_time, 1, wxEXPAND);
93
94         add_label_to_sizer (table, this, "Write to", true);
95
96 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
97         _folder = new DirPickerCtrl (this); 
98 #else   
99         _folder = new wxDirPickerCtrl (this, wxID_ANY);
100 #endif
101
102         _folder->SetPath (wxStandardPaths::Get().GetDocumentsDir());
103         
104         table->Add (_folder, 1, wxEXPAND);
105         
106         vertical->Add (table, 0, wxEXPAND | wxALL, 6);
107
108         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
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         wxButton* ok = dynamic_cast<wxButton *> (FindWindowById (wxID_OK));
184         ok->Enable (sc || sc);
185 }
186
187 void
188 KDMDialog::add_cinema (shared_ptr<Cinema> c)
189 {
190         _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
191
192         list<shared_ptr<Screen> > sc = c->screens ();
193         for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
194                 add_screen (c, *i);
195         }
196 }
197
198 void
199 KDMDialog::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
200 {
201         map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator i = _cinemas.begin();
202         while (i != _cinemas.end() && i->second != c) {
203                 ++i;
204         }
205
206         if (i == _cinemas.end()) {
207                 return;
208         }
209
210         _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
211 }
212
213 void
214 KDMDialog::add_cinema_clicked (wxCommandEvent &)
215 {
216         CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
217         d->ShowModal ();
218
219         shared_ptr<Cinema> c (new Cinema (d->name(), d->email()));
220         Config::instance()->add_cinema (c);
221         add_cinema (c);
222
223         Config::instance()->write ();
224         
225         d->Destroy ();
226 }
227
228 void
229 KDMDialog::edit_cinema_clicked (wxCommandEvent &)
230 {
231         if (selected_cinemas().size() != 1) {
232                 return;
233         }
234
235         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
236         
237         CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->email);
238         d->ShowModal ();
239
240         c.second->name = d->name ();
241         c.second->email = d->email ();
242         _targets->SetItemText (c.first, std_to_wx (d->name()));
243
244         Config::instance()->write ();
245
246         d->Destroy ();  
247 }
248
249 void
250 KDMDialog::remove_cinema_clicked (wxCommandEvent &)
251 {
252         if (selected_cinemas().size() != 1) {
253                 return;
254         }
255
256         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
257
258         Config::instance()->remove_cinema (c.second);
259         _targets->Delete (c.first);
260
261         Config::instance()->write ();   
262 }
263
264 void
265 KDMDialog::add_screen_clicked (wxCommandEvent &)
266 {
267         if (selected_cinemas().size() != 1) {
268                 return;
269         }
270
271         shared_ptr<Cinema> c = selected_cinemas().front().second;
272         
273         ScreenDialog* d = new ScreenDialog (this, "Add Screen");
274         d->ShowModal ();
275
276         shared_ptr<Screen> s (new Screen (d->name(), d->certificate()));
277         c->add_screen (s);
278         add_screen (c, s);
279
280         Config::instance()->write ();
281
282         d->Destroy ();
283 }
284
285 void
286 KDMDialog::edit_screen_clicked (wxCommandEvent &)
287 {
288         if (selected_screens().size() != 1) {
289                 return;
290         }
291
292         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
293         
294         ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->certificate);
295         d->ShowModal ();
296
297         s.second->name = d->name ();
298         s.second->certificate = d->certificate ();
299         _targets->SetItemText (s.first, std_to_wx (d->name()));
300
301         Config::instance()->write ();
302
303         d->Destroy ();
304 }
305
306 void
307 KDMDialog::remove_screen_clicked (wxCommandEvent &)
308 {
309         if (selected_screens().size() != 1) {
310                 return;
311         }
312
313         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
314
315         map<wxTreeItemId, shared_ptr<Cinema> >::iterator i = _cinemas.begin ();
316         list<shared_ptr<Screen> > sc = i->second->screens ();
317         while (i != _cinemas.end() && find (sc.begin(), sc.end(), s.second) == sc.end()) {
318                 ++i;
319         }
320
321         if (i == _cinemas.end()) {
322                 return;
323         }
324
325         i->second->remove_screen (s.second);
326         _targets->Delete (s.first);
327
328         Config::instance()->write ();
329 }
330
331 list<shared_ptr<Screen> >
332 KDMDialog::screens () const
333 {
334         list<shared_ptr<Screen> > s;
335
336         list<pair<wxTreeItemId, shared_ptr<Cinema> > > cinemas = selected_cinemas ();
337         for (list<pair<wxTreeItemId, shared_ptr<Cinema> > >::iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
338                 list<shared_ptr<Screen> > sc = i->second->screens ();
339                 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
340                         s.push_back (*j);
341                 }
342         }
343
344         list<pair<wxTreeItemId, shared_ptr<Screen> > > screens = selected_screens ();
345         for (list<pair<wxTreeItemId, shared_ptr<Screen> > >::iterator i = screens.begin(); i != screens.end(); ++i) {
346                 s.push_back (i->second);
347         }
348
349         s.sort ();
350         s.unique ();
351
352         return s;
353 }
354
355 boost::posix_time::ptime
356 KDMDialog::from () const
357 {
358         return posix_time (_from_date, _from_time);
359 }
360
361 boost::posix_time::ptime
362 KDMDialog::posix_time (wxDatePickerCtrl* date_picker, wxTimePickerCtrl* time_picker)
363 {
364         wxDateTime const date = date_picker->GetValue ();
365         wxDateTime const time = time_picker->GetValue ();
366         return boost::posix_time::ptime (
367                 boost::gregorian::date (date.GetYear(), date.GetMonth() + 1, date.GetDay()),
368                 boost::posix_time::time_duration (time.GetHour(), time.GetMinute(), time.GetSecond())
369                 );
370 }
371
372 boost::posix_time::ptime
373 KDMDialog::until () const
374 {
375         return posix_time (_until_date, _until_time);
376 }
377
378 string
379 KDMDialog::directory () const
380 {
381         return wx_to_std (_folder->GetPath ());
382 }