Merge 1.0 in.
[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         for (list<shared_ptr<Screen> >::iterator i = c->screens.begin(); i != c->screens.end(); ++i) {
187                 add_screen (c, *i);
188         }
189 }
190
191 void
192 KDMDialog::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
193 {
194         map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator i = _cinemas.begin();
195         while (i != _cinemas.end() && i->second != c) {
196                 ++i;
197         }
198
199         if (i == _cinemas.end()) {
200                 return;
201         }
202
203         _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
204 }
205
206 void
207 KDMDialog::add_cinema_clicked (wxCommandEvent &)
208 {
209         CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
210         d->ShowModal ();
211
212         shared_ptr<Cinema> c (new Cinema (d->name(), d->email()));
213         Config::instance()->add_cinema (c);
214         add_cinema (c);
215
216         Config::instance()->write ();
217         
218         d->Destroy ();
219 }
220
221 void
222 KDMDialog::edit_cinema_clicked (wxCommandEvent &)
223 {
224         if (selected_cinemas().size() != 1) {
225                 return;
226         }
227
228         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
229         
230         CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->email);
231         d->ShowModal ();
232
233         c.second->name = d->name ();
234         c.second->email = d->email ();
235         _targets->SetItemText (c.first, std_to_wx (d->name()));
236
237         Config::instance()->write ();
238
239         d->Destroy ();  
240 }
241
242 void
243 KDMDialog::remove_cinema_clicked (wxCommandEvent &)
244 {
245         if (selected_cinemas().size() != 1) {
246                 return;
247         }
248
249         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
250
251         Config::instance()->remove_cinema (c.second);
252         _targets->Delete (c.first);
253
254         Config::instance()->write ();   
255 }
256
257 void
258 KDMDialog::add_screen_clicked (wxCommandEvent &)
259 {
260         if (selected_cinemas().size() != 1) {
261                 return;
262         }
263
264         shared_ptr<Cinema> c = selected_cinemas().front().second;
265         
266         ScreenDialog* d = new ScreenDialog (this, "Add Screen");
267         d->ShowModal ();
268
269         shared_ptr<Screen> s (new Screen (d->name(), d->certificate()));
270         c->screens.push_back (s);
271         add_screen (c, s);
272
273         Config::instance()->write ();
274
275         d->Destroy ();
276 }
277
278 void
279 KDMDialog::edit_screen_clicked (wxCommandEvent &)
280 {
281         if (selected_screens().size() != 1) {
282                 return;
283         }
284
285         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
286         
287         ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->certificate);
288         d->ShowModal ();
289
290         s.second->name = d->name ();
291         s.second->certificate = d->certificate ();
292         _targets->SetItemText (s.first, std_to_wx (d->name()));
293
294         Config::instance()->write ();
295
296         d->Destroy ();
297 }
298
299 void
300 KDMDialog::remove_screen_clicked (wxCommandEvent &)
301 {
302         if (selected_screens().size() != 1) {
303                 return;
304         }
305
306         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
307
308         map<wxTreeItemId, shared_ptr<Cinema> >::iterator i = _cinemas.begin ();
309         while (i != _cinemas.end() && find (i->second->screens.begin(), i->second->screens.end(), s.second) == i->second->screens.end()) {
310                 ++i;
311         }
312
313         if (i == _cinemas.end()) {
314                 return;
315         }
316
317         i->second->screens.remove (s.second);
318         _targets->Delete (s.first);
319
320         Config::instance()->write ();
321 }
322
323 list<shared_ptr<Screen> >
324 KDMDialog::screens () const
325 {
326         list<shared_ptr<Screen> > s;
327
328         list<pair<wxTreeItemId, shared_ptr<Cinema> > > cinemas = selected_cinemas ();
329         for (list<pair<wxTreeItemId, shared_ptr<Cinema> > >::iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
330                 for (list<shared_ptr<Screen> >::iterator j = i->second->screens.begin(); j != i->second->screens.end(); ++j) {
331                         s.push_back (*j);
332                 }
333         }
334
335         list<pair<wxTreeItemId, shared_ptr<Screen> > > screens = selected_screens ();
336         for (list<pair<wxTreeItemId, shared_ptr<Screen> > >::iterator i = screens.begin(); i != screens.end(); ++i) {
337                 s.push_back (i->second);
338         }
339
340         s.sort ();
341         s.unique ();
342
343         return s;
344 }
345
346 boost::posix_time::ptime
347 KDMDialog::from () const
348 {
349         return posix_time (_from_date, _from_time);
350 }
351
352 boost::posix_time::ptime
353 KDMDialog::posix_time (wxDatePickerCtrl* date_picker, wxTimePickerCtrl* time_picker)
354 {
355         wxDateTime const date = date_picker->GetValue ();
356         wxDateTime const time = time_picker->GetValue ();
357         return boost::posix_time::ptime (
358                 boost::gregorian::date (date.GetYear(), date.GetMonth() + 1, date.GetDay()),
359                 boost::posix_time::time_duration (time.GetHour(), time.GetMinute(), time.GetSecond())
360                 );
361 }
362
363 boost::posix_time::ptime
364 KDMDialog::until () const
365 {
366         return posix_time (_until_date, _until_time);
367 }
368
369 string
370 KDMDialog::directory () const
371 {
372         return wx_to_std (_folder->GetPath ());
373 }