2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
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"
30 #include "dir_picker_ctrl.h"
32 #include <wx/filepicker.h>
40 using boost::shared_ptr;
42 KDMDialog::KDMDialog (wxWindow* parent)
43 : wxDialog (parent, wxID_ANY, _("Make KDMs"))
45 wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
46 wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
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);
51 _root = _targets->AddRoot ("Foo");
53 list<shared_ptr<Cinema> > c = Config::instance()->cinemas ();
54 for (list<shared_ptr<Cinema> >::iterator i = c.begin(); i != c.end(); ++i) {
58 _targets->ExpandAll ();
60 wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
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);
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);
76 targets->Add (target_buttons, 0, 0, 6);
78 vertical->Add (targets, 1, wxEXPAND | wxALL, 6);
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);
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);
93 add_label_to_sizer (table, this, "Write to", true);
96 _folder = new DirPickerCtrl (this);
98 _folder = new wxDirPickerCtrl (this, wxDD_DIR_MUST_EXIST);
101 table->Add (_folder, 1, wxEXPAND);
103 vertical->Add (table, 0, wxEXPAND | wxALL, 6);
105 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
107 vertical->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
110 _targets->Connect (wxID_ANY, wxEVT_COMMAND_TREE_SEL_CHANGED, wxCommandEventHandler (KDMDialog::targets_selection_changed), 0, this);
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);
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);
120 setup_sensitivity ();
124 vertical->SetSizeHints (this);
127 list<pair<wxTreeItemId, shared_ptr<Cinema> > >
128 KDMDialog::selected_cinemas () const
130 wxArrayTreeItemIds s;
131 _targets->GetSelections (s);
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));
144 list<pair<wxTreeItemId, shared_ptr<Screen> > >
145 KDMDialog::selected_screens () const
147 wxArrayTreeItemIds s;
148 _targets->GetSelections (s);
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));
162 KDMDialog::targets_selection_changed (wxCommandEvent &)
164 setup_sensitivity ();
168 KDMDialog::setup_sensitivity ()
170 bool const sc = selected_cinemas().size() == 1;
171 bool const ss = selected_screens().size() == 1;
173 _edit_cinema->Enable (sc);
174 _remove_cinema->Enable (sc);
176 _add_screen->Enable (sc);
177 _edit_screen->Enable (ss);
178 _remove_screen->Enable (ss);
182 KDMDialog::add_cinema (shared_ptr<Cinema> c)
184 _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
186 for (list<shared_ptr<Screen> >::iterator i = c->screens.begin(); i != c->screens.end(); ++i) {
192 KDMDialog::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
194 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator i = _cinemas.begin();
195 while (i != _cinemas.end() && i->second != c) {
199 if (i == _cinemas.end()) {
203 _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
207 KDMDialog::add_cinema_clicked (wxCommandEvent &)
209 CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
212 shared_ptr<Cinema> c (new Cinema (d->name(), d->email()));
213 Config::instance()->add_cinema (c);
216 Config::instance()->write ();
222 KDMDialog::edit_cinema_clicked (wxCommandEvent &)
224 if (selected_cinemas().size() != 1) {
228 pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
230 CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->email);
233 c.second->name = d->name ();
234 c.second->email = d->email ();
235 _targets->SetItemText (c.first, std_to_wx (d->name()));
237 Config::instance()->write ();
243 KDMDialog::remove_cinema_clicked (wxCommandEvent &)
245 if (selected_cinemas().size() != 1) {
249 pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
251 Config::instance()->remove_cinema (c.second);
252 _targets->Delete (c.first);
254 Config::instance()->write ();
258 KDMDialog::add_screen_clicked (wxCommandEvent &)
260 if (selected_cinemas().size() != 1) {
264 shared_ptr<Cinema> c = selected_cinemas().front().second;
266 ScreenDialog* d = new ScreenDialog (this, "Add Screen");
269 shared_ptr<Screen> s (new Screen (d->name(), d->certificate()));
270 c->screens.push_back (s);
273 Config::instance()->write ();
279 KDMDialog::edit_screen_clicked (wxCommandEvent &)
281 if (selected_screens().size() != 1) {
285 pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
287 ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->certificate);
290 s.second->name = d->name ();
291 s.second->certificate = d->certificate ();
292 _targets->SetItemText (s.first, std_to_wx (d->name()));
294 Config::instance()->write ();
300 KDMDialog::remove_screen_clicked (wxCommandEvent &)
302 if (selected_screens().size() != 1) {
306 pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
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()) {
313 if (i == _cinemas.end()) {
317 i->second->screens.remove (s.second);
318 _targets->Delete (s.first);
320 Config::instance()->write ();
323 list<shared_ptr<Screen> >
324 KDMDialog::screens () const
326 list<shared_ptr<Screen> > s;
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) {
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);
346 boost::posix_time::ptime
347 KDMDialog::from () const
349 return posix_time (_from_date, _from_time);
352 boost::posix_time::ptime
353 KDMDialog::posix_time (wxDatePickerCtrl* date_picker, wxTimePickerCtrl* time_picker)
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())
363 boost::posix_time::ptime
364 KDMDialog::until () const
366 return posix_time (_until_date, _until_time);
370 KDMDialog::directory () const
372 return wx_to_std (_folder->GetPath ());