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 <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"
30 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
31 #include "dir_picker_ctrl.h"
33 #include <wx/filepicker.h>
41 using boost::shared_ptr;
43 KDMDialog::KDMDialog (wxWindow* parent)
44 : wxDialog (parent, wxID_ANY, _("Make KDMs"))
46 wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
47 wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
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);
52 _root = _targets->AddRoot ("Foo");
54 list<shared_ptr<Cinema> > c = Config::instance()->cinemas ();
55 for (list<shared_ptr<Cinema> >::iterator i = c.begin(); i != c.end(); ++i) {
59 _targets->ExpandAll ();
61 wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
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);
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);
77 targets->Add (target_buttons, 0, 0, 6);
79 vertical->Add (targets, 1, wxEXPAND | wxALL, 6);
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);
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);
94 _write_to = new wxRadioButton (this, wxID_ANY, _("Write to"));
95 table->Add (_write_to, 1, wxEXPAND);
97 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
98 _folder = new DirPickerCtrl (this);
100 _folder = new wxDirPickerCtrl (this, wxID_ANY);
103 _folder->SetPath (wxStandardPaths::Get().GetDocumentsDir());
105 table->Add (_folder, 1, wxEXPAND);
106 table->AddSpacer (0);
108 _email = new wxRadioButton (this, wxID_ANY, _("Send by email"));
109 table->Add (_email, 1, wxEXPAND);
110 table->AddSpacer (0);
112 vertical->Add (table, 0, wxEXPAND | wxALL, 6);
114 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
116 vertical->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
119 _targets->Bind (wxEVT_COMMAND_TREE_SEL_CHANGED, boost::bind (&KDMDialog::setup_sensitivity, this));
121 _add_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::add_cinema_clicked, this));
122 _edit_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::edit_cinema_clicked, this));
123 _remove_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::remove_cinema_clicked, this));
125 _add_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::add_screen_clicked, this));
126 _edit_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::edit_screen_clicked, this));
127 _remove_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::remove_screen_clicked, this));
129 _write_to->Bind (wxEVT_COMMAND_RADIOBUTTON_SELECTED, boost::bind (&KDMDialog::setup_sensitivity, this));
130 _email->Bind (wxEVT_COMMAND_RADIOBUTTON_SELECTED, boost::bind (&KDMDialog::setup_sensitivity, this));
132 setup_sensitivity ();
136 vertical->SetSizeHints (this);
139 list<pair<wxTreeItemId, shared_ptr<Cinema> > >
140 KDMDialog::selected_cinemas () const
142 wxArrayTreeItemIds s;
143 _targets->GetSelections (s);
145 list<pair<wxTreeItemId, shared_ptr<Cinema> > > c;
146 for (size_t i = 0; i < s.GetCount(); ++i) {
147 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator j = _cinemas.find (s[i]);
148 if (j != _cinemas.end ()) {
149 c.push_back (make_pair (j->first, j->second));
156 list<pair<wxTreeItemId, shared_ptr<Screen> > >
157 KDMDialog::selected_screens () const
159 wxArrayTreeItemIds s;
160 _targets->GetSelections (s);
162 list<pair<wxTreeItemId, shared_ptr<Screen> > > c;
163 for (size_t i = 0; i < s.GetCount(); ++i) {
164 map<wxTreeItemId, shared_ptr<Screen> >::const_iterator j = _screens.find (s[i]);
165 if (j != _screens.end ()) {
166 c.push_back (make_pair (j->first, j->second));
174 KDMDialog::setup_sensitivity ()
176 bool const sc = selected_cinemas().size() == 1;
177 bool const ss = selected_screens().size() == 1;
179 _edit_cinema->Enable (sc);
180 _remove_cinema->Enable (sc);
182 _add_screen->Enable (sc);
183 _edit_screen->Enable (ss);
184 _remove_screen->Enable (ss);
186 wxButton* ok = dynamic_cast<wxButton *> (FindWindowById (wxID_OK));
187 ok->Enable (sc || ss);
189 _folder->Enable (_write_to->GetValue ());
193 KDMDialog::add_cinema (shared_ptr<Cinema> c)
195 _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
197 list<shared_ptr<Screen> > sc = c->screens ();
198 for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
204 KDMDialog::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
206 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator i = _cinemas.begin();
207 while (i != _cinemas.end() && i->second != c) {
211 if (i == _cinemas.end()) {
215 _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
219 KDMDialog::add_cinema_clicked ()
221 CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
224 shared_ptr<Cinema> c (new Cinema (d->name(), d->email()));
225 Config::instance()->add_cinema (c);
228 Config::instance()->write ();
234 KDMDialog::edit_cinema_clicked ()
236 if (selected_cinemas().size() != 1) {
240 pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
242 CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->email);
245 c.second->name = d->name ();
246 c.second->email = d->email ();
247 _targets->SetItemText (c.first, std_to_wx (d->name()));
249 Config::instance()->write ();
255 KDMDialog::remove_cinema_clicked ()
257 if (selected_cinemas().size() != 1) {
261 pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
263 Config::instance()->remove_cinema (c.second);
264 _targets->Delete (c.first);
266 Config::instance()->write ();
270 KDMDialog::add_screen_clicked ()
272 if (selected_cinemas().size() != 1) {
276 shared_ptr<Cinema> c = selected_cinemas().front().second;
278 ScreenDialog* d = new ScreenDialog (this, "Add Screen");
281 shared_ptr<Screen> s (new Screen (d->name(), d->certificate()));
285 Config::instance()->write ();
291 KDMDialog::edit_screen_clicked ()
293 if (selected_screens().size() != 1) {
297 pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
299 ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->certificate);
302 s.second->name = d->name ();
303 s.second->certificate = d->certificate ();
304 _targets->SetItemText (s.first, std_to_wx (d->name()));
306 Config::instance()->write ();
312 KDMDialog::remove_screen_clicked ()
314 if (selected_screens().size() != 1) {
318 pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
320 map<wxTreeItemId, shared_ptr<Cinema> >::iterator i = _cinemas.begin ();
321 list<shared_ptr<Screen> > sc = i->second->screens ();
322 while (i != _cinemas.end() && find (sc.begin(), sc.end(), s.second) == sc.end()) {
326 if (i == _cinemas.end()) {
330 i->second->remove_screen (s.second);
331 _targets->Delete (s.first);
333 Config::instance()->write ();
336 list<shared_ptr<Screen> >
337 KDMDialog::screens () const
339 list<shared_ptr<Screen> > s;
341 list<pair<wxTreeItemId, shared_ptr<Cinema> > > cinemas = selected_cinemas ();
342 for (list<pair<wxTreeItemId, shared_ptr<Cinema> > >::iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
343 list<shared_ptr<Screen> > sc = i->second->screens ();
344 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
349 list<pair<wxTreeItemId, shared_ptr<Screen> > > screens = selected_screens ();
350 for (list<pair<wxTreeItemId, shared_ptr<Screen> > >::iterator i = screens.begin(); i != screens.end(); ++i) {
351 s.push_back (i->second);
360 boost::posix_time::ptime
361 KDMDialog::from () const
363 return posix_time (_from_date, _from_time);
366 boost::posix_time::ptime
367 KDMDialog::posix_time (wxDatePickerCtrl* date_picker, wxTimePickerCtrl* time_picker)
369 wxDateTime const date = date_picker->GetValue ();
370 wxDateTime const time = time_picker->GetValue ();
371 return boost::posix_time::ptime (
372 boost::gregorian::date (date.GetYear(), date.GetMonth() + 1, date.GetDay()),
373 boost::posix_time::time_duration (time.GetHour(), time.GetMinute(), time.GetSecond())
377 boost::posix_time::ptime
378 KDMDialog::until () const
380 return posix_time (_until_date, _until_time);
384 KDMDialog::directory () const
386 return wx_to_std (_folder->GetPath ());
390 KDMDialog::write_to () const
392 return _write_to->GetValue ();