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);
47 add_label_to_sizer (vertical, this, "Make KDMs for");
49 wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
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);
54 _root = _targets->AddRoot ("Foo");
56 list<shared_ptr<Cinema> > c = Config::instance()->cinemas ();
57 for (list<shared_ptr<Cinema> >::iterator i = c.begin(); i != c.end(); ++i) {
61 _targets->ExpandAll ();
63 wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
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);
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);
79 targets->Add (target_buttons, 0, 0, 6);
81 vertical->Add (targets, 1, wxEXPAND | wxALL, 6);
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);
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);
96 add_label_to_sizer (table, this, "Write to");
99 _folder = new DirPickerCtrl (this);
101 _folder = new wxDirPickerCtrl (this, wxDD_DIR_MUST_EXIST);
104 table->Add (_folder, 1, wxEXPAND);
106 vertical->Add (table, 0, wxEXPAND | wxALL, 6);
108 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
110 vertical->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
113 _targets->Connect (wxID_ANY, wxEVT_COMMAND_TREE_SEL_CHANGED, wxCommandEventHandler (KDMDialog::targets_selection_changed), 0, this);
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);
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);
123 setup_sensitivity ();
127 vertical->SetSizeHints (this);
130 list<pair<wxTreeItemId, shared_ptr<Cinema> > >
131 KDMDialog::selected_cinemas () const
133 wxArrayTreeItemIds s;
134 _targets->GetSelections (s);
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));
147 list<pair<wxTreeItemId, shared_ptr<Screen> > >
148 KDMDialog::selected_screens () const
150 wxArrayTreeItemIds s;
151 _targets->GetSelections (s);
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));
165 KDMDialog::targets_selection_changed (wxCommandEvent &)
167 setup_sensitivity ();
171 KDMDialog::setup_sensitivity ()
173 bool const sc = selected_cinemas().size() == 1;
174 bool const ss = selected_screens().size() == 1;
176 _edit_cinema->Enable (sc);
177 _remove_cinema->Enable (sc);
179 _add_screen->Enable (sc);
180 _edit_screen->Enable (ss);
181 _remove_screen->Enable (ss);
185 KDMDialog::add_cinema (shared_ptr<Cinema> c)
187 _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
189 for (list<shared_ptr<Screen> >::iterator i = c->screens.begin(); i != c->screens.end(); ++i) {
195 KDMDialog::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
197 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator i = _cinemas.begin();
198 while (i != _cinemas.end() && i->second != c) {
202 if (i == _cinemas.end()) {
206 _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
210 KDMDialog::add_cinema_clicked (wxCommandEvent &)
212 CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
215 shared_ptr<Cinema> c (new Cinema (d->name(), d->email()));
216 Config::instance()->add_cinema (c);
219 Config::instance()->write ();
225 KDMDialog::edit_cinema_clicked (wxCommandEvent &)
227 if (selected_cinemas().size() != 1) {
231 pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
233 CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->email);
236 c.second->name = d->name ();
237 c.second->email = d->email ();
238 _targets->SetItemText (c.first, std_to_wx (d->name()));
240 Config::instance()->write ();
246 KDMDialog::remove_cinema_clicked (wxCommandEvent &)
248 if (selected_cinemas().size() != 1) {
252 pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
254 Config::instance()->remove_cinema (c.second);
255 _targets->Delete (c.first);
257 Config::instance()->write ();
261 KDMDialog::add_screen_clicked (wxCommandEvent &)
263 if (selected_cinemas().size() != 1) {
267 shared_ptr<Cinema> c = selected_cinemas().front().second;
269 ScreenDialog* d = new ScreenDialog (this, "Add Screen");
272 shared_ptr<Screen> s (new Screen (d->name()));
273 c->screens.push_back (s);
276 Config::instance()->write ();
282 KDMDialog::edit_screen_clicked (wxCommandEvent &)
284 if (selected_screens().size() != 1) {
288 pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
290 ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name);
293 s.second->name = d->name ();
294 _targets->SetItemText (s.first, std_to_wx (d->name()));
296 Config::instance()->write ();
302 KDMDialog::remove_screen_clicked (wxCommandEvent &)
304 if (selected_screens().size() != 1) {
308 pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
310 map<wxTreeItemId, shared_ptr<Cinema> >::iterator i = _cinemas.begin ();
311 while (i != _cinemas.end() && find (i->second->screens.begin(), i->second->screens.end(), s.second) == i->second->screens.end()) {
315 if (i == _cinemas.end()) {
319 i->second->screens.remove (s.second);
320 _targets->Delete (s.first);
322 Config::instance()->write ();
325 list<shared_ptr<Screen> >
326 KDMDialog::screens () const
328 list<shared_ptr<Screen> > s;
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 for (list<shared_ptr<Screen> >::iterator j = i->second->screens.begin(); j != i->second->screens.end(); ++j) {
337 list<pair<wxTreeItemId, shared_ptr<Screen> > > screens = selected_screens ();
338 for (list<pair<wxTreeItemId, shared_ptr<Screen> > >::iterator i = screens.begin(); i != screens.end(); ++i) {
339 s.push_back (i->second);
348 boost::posix_time::ptime
349 KDMDialog::from () const
351 return posix_time (_from_date, _from_time);
354 boost::posix_time::ptime
355 KDMDialog::posix_time (wxDatePickerCtrl* date_picker, wxTimePickerCtrl* time_picker)
357 wxDateTime const date = date_picker->GetValue ();
358 wxDateTime const time = time_picker->GetValue ();
359 return boost::posix_time::ptime (
360 boost::gregorian::date (date.GetYear(), date.GetMonth() + 1, date.GetDay()),
361 boost::posix_time::time_duration (time.GetHour(), time.GetMinute(), time.GetSecond())
365 boost::posix_time::ptime
366 KDMDialog::until () const
368 return posix_time (_until_date, _until_time);
372 KDMDialog::directory () const
374 return wx_to_std (_folder->GetPath ());