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 <wx/listctrl.h>
25 #include "lib/cinema.h"
26 #include "lib/config.h"
28 #include "kdm_dialog.h"
29 #include "cinema_dialog.h"
30 #include "screen_dialog.h"
32 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
33 #include "dir_picker_ctrl.h"
35 #include <wx/filepicker.h>
44 using boost::shared_ptr;
46 KDMDialog::KDMDialog (wxWindow* parent, boost::shared_ptr<const Film> film)
47 : wxDialog (parent, wxID_ANY, _("Make KDMs"))
49 wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
50 wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
52 _targets = new wxTreeCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTR_HIDE_ROOT | wxTR_MULTIPLE | wxTR_HAS_BUTTONS);
53 targets->Add (_targets, 1, wxEXPAND | wxALL, 6);
55 _root = _targets->AddRoot ("Foo");
57 list<shared_ptr<Cinema> > c = Config::instance()->cinemas ();
58 for (list<shared_ptr<Cinema> >::iterator i = c.begin(); i != c.end(); ++i) {
62 _targets->ExpandAll ();
64 wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
66 _add_cinema = new wxButton (this, wxID_ANY, _("Add Cinema..."));
67 target_buttons->Add (_add_cinema, 1, wxEXPAND, 6);
68 _edit_cinema = new wxButton (this, wxID_ANY, _("Edit Cinema..."));
69 target_buttons->Add (_edit_cinema, 1, wxEXPAND, 6);
70 _remove_cinema = new wxButton (this, wxID_ANY, _("Remove Cinema"));
71 target_buttons->Add (_remove_cinema, 1, wxEXPAND, 6);
73 _add_screen = new wxButton (this, wxID_ANY, _("Add Screen..."));
74 target_buttons->Add (_add_screen, 1, wxEXPAND, 6);
75 _edit_screen = new wxButton (this, wxID_ANY, _("Edit Screen..."));
76 target_buttons->Add (_edit_screen, 1, wxEXPAND, 6);
77 _remove_screen = new wxButton (this, wxID_ANY, _("Remove Screen"));
78 target_buttons->Add (_remove_screen, 1, wxEXPAND, 6);
80 targets->Add (target_buttons, 0, 0, 6);
82 vertical->Add (targets, 1, wxEXPAND | wxALL, 6);
84 wxFlexGridSizer* table = new wxFlexGridSizer (3, 2, 6);
85 add_label_to_sizer (table, this, _("From"), true);
86 _from_date = new wxDatePickerCtrl (this, wxID_ANY);
87 table->Add (_from_date, 1, wxEXPAND);
88 _from_time = new wxTimePickerCtrl (this, wxID_ANY);
89 table->Add (_from_time, 1, wxEXPAND);
91 add_label_to_sizer (table, this, _("Until"), true);
92 _until_date = new wxDatePickerCtrl (this, wxID_ANY);
93 table->Add (_until_date, 1, wxEXPAND);
94 _until_time = new wxTimePickerCtrl (this, wxID_ANY);
95 table->Add (_until_time, 1, wxEXPAND);
97 vertical->Add (table, 0, wxEXPAND | wxALL, 6);
99 _dcps = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL);
102 ip.SetText (_("DCP"));
104 _dcps->InsertColumn (0, ip);
105 vertical->Add (_dcps, 0, wxEXPAND | wxALL, 6);
107 list<boost::filesystem::path> dcps = film->dcps ();
108 for (list<boost::filesystem::path>::const_iterator i = dcps.begin(); i != dcps.end(); ++i) {
110 int const n = _dcps->GetItemCount ();
112 _dcps->InsertItem (item);
113 _dcps->SetItem (n, 0, std_to_wx (i->string ()));
115 if (dcps.size() == 1 || i->string() == film->dcp_name ()) {
116 _dcps->SetItemState (n, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
120 table = new wxFlexGridSizer (3, 2, 6);
122 _write_to = new wxRadioButton (this, wxID_ANY, _("Write to"));
123 table->Add (_write_to, 1, wxEXPAND);
125 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
126 _folder = new DirPickerCtrl (this);
128 _folder = new wxDirPickerCtrl (this, wxID_ANY);
131 _folder->SetPath (wxStandardPaths::Get().GetDocumentsDir());
133 table->Add (_folder, 1, wxEXPAND);
134 table->AddSpacer (0);
136 _email = new wxRadioButton (this, wxID_ANY, _("Send by email"));
137 table->Add (_email, 1, wxEXPAND);
138 table->AddSpacer (0);
140 vertical->Add (table, 0, wxEXPAND | wxALL, 6);
142 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
144 vertical->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
147 _targets->Bind (wxEVT_COMMAND_TREE_SEL_CHANGED, boost::bind (&KDMDialog::setup_sensitivity, this));
149 _add_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::add_cinema_clicked, this));
150 _edit_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::edit_cinema_clicked, this));
151 _remove_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::remove_cinema_clicked, this));
153 _add_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::add_screen_clicked, this));
154 _edit_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::edit_screen_clicked, this));
155 _remove_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::remove_screen_clicked, this));
157 _dcps->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&KDMDialog::setup_sensitivity, this));
158 _dcps->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&KDMDialog::setup_sensitivity, this));
160 _write_to->Bind (wxEVT_COMMAND_RADIOBUTTON_SELECTED, boost::bind (&KDMDialog::setup_sensitivity, this));
161 _email->Bind (wxEVT_COMMAND_RADIOBUTTON_SELECTED, boost::bind (&KDMDialog::setup_sensitivity, this));
163 setup_sensitivity ();
167 vertical->SetSizeHints (this);
170 list<pair<wxTreeItemId, shared_ptr<Cinema> > >
171 KDMDialog::selected_cinemas () const
173 wxArrayTreeItemIds s;
174 _targets->GetSelections (s);
176 list<pair<wxTreeItemId, shared_ptr<Cinema> > > c;
177 for (size_t i = 0; i < s.GetCount(); ++i) {
178 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator j = _cinemas.find (s[i]);
179 if (j != _cinemas.end ()) {
180 c.push_back (make_pair (j->first, j->second));
187 list<pair<wxTreeItemId, shared_ptr<Screen> > >
188 KDMDialog::selected_screens () const
190 wxArrayTreeItemIds s;
191 _targets->GetSelections (s);
193 list<pair<wxTreeItemId, shared_ptr<Screen> > > c;
194 for (size_t i = 0; i < s.GetCount(); ++i) {
195 map<wxTreeItemId, shared_ptr<Screen> >::const_iterator j = _screens.find (s[i]);
196 if (j != _screens.end ()) {
197 c.push_back (make_pair (j->first, j->second));
205 KDMDialog::setup_sensitivity ()
207 bool const sc = selected_cinemas().size() == 1;
208 bool const ss = selected_screens().size() == 1;
209 bool const sd = _dcps->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED) != -1;
211 _edit_cinema->Enable (sc);
212 _remove_cinema->Enable (sc);
214 _add_screen->Enable (sc);
215 _edit_screen->Enable (ss);
216 _remove_screen->Enable (ss);
218 wxButton* ok = dynamic_cast<wxButton *> (FindWindowById (wxID_OK));
219 ok->Enable ((sc || ss) && sd);
221 _folder->Enable (_write_to->GetValue ());
225 KDMDialog::add_cinema (shared_ptr<Cinema> c)
227 _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
229 list<shared_ptr<Screen> > sc = c->screens ();
230 for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
236 KDMDialog::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
238 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator i = _cinemas.begin();
239 while (i != _cinemas.end() && i->second != c) {
243 if (i == _cinemas.end()) {
247 _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
251 KDMDialog::add_cinema_clicked ()
253 CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
256 shared_ptr<Cinema> c (new Cinema (d->name(), d->email()));
257 Config::instance()->add_cinema (c);
260 Config::instance()->write ();
266 KDMDialog::edit_cinema_clicked ()
268 if (selected_cinemas().size() != 1) {
272 pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
274 CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->email);
277 c.second->name = d->name ();
278 c.second->email = d->email ();
279 _targets->SetItemText (c.first, std_to_wx (d->name()));
281 Config::instance()->write ();
287 KDMDialog::remove_cinema_clicked ()
289 if (selected_cinemas().size() != 1) {
293 pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
295 Config::instance()->remove_cinema (c.second);
296 _targets->Delete (c.first);
298 Config::instance()->write ();
302 KDMDialog::add_screen_clicked ()
304 if (selected_cinemas().size() != 1) {
308 shared_ptr<Cinema> c = selected_cinemas().front().second;
310 ScreenDialog* d = new ScreenDialog (this, "Add Screen");
311 if (d->ShowModal () != wxID_OK) {
315 shared_ptr<Screen> s (new Screen (d->name(), d->certificate()));
319 Config::instance()->write ();
325 KDMDialog::edit_screen_clicked ()
327 if (selected_screens().size() != 1) {
331 pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
333 ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->certificate);
336 s.second->name = d->name ();
337 s.second->certificate = d->certificate ();
338 _targets->SetItemText (s.first, std_to_wx (d->name()));
340 Config::instance()->write ();
346 KDMDialog::remove_screen_clicked ()
348 if (selected_screens().size() != 1) {
352 pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
354 map<wxTreeItemId, shared_ptr<Cinema> >::iterator i = _cinemas.begin ();
355 list<shared_ptr<Screen> > sc = i->second->screens ();
356 while (i != _cinemas.end() && find (sc.begin(), sc.end(), s.second) == sc.end()) {
360 if (i == _cinemas.end()) {
364 i->second->remove_screen (s.second);
365 _targets->Delete (s.first);
367 Config::instance()->write ();
370 list<shared_ptr<Screen> >
371 KDMDialog::screens () const
373 list<shared_ptr<Screen> > s;
375 list<pair<wxTreeItemId, shared_ptr<Cinema> > > cinemas = selected_cinemas ();
376 for (list<pair<wxTreeItemId, shared_ptr<Cinema> > >::iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
377 list<shared_ptr<Screen> > sc = i->second->screens ();
378 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
383 list<pair<wxTreeItemId, shared_ptr<Screen> > > screens = selected_screens ();
384 for (list<pair<wxTreeItemId, shared_ptr<Screen> > >::iterator i = screens.begin(); i != screens.end(); ++i) {
385 s.push_back (i->second);
394 boost::posix_time::ptime
395 KDMDialog::from () const
397 return posix_time (_from_date, _from_time);
400 boost::posix_time::ptime
401 KDMDialog::posix_time (wxDatePickerCtrl* date_picker, wxTimePickerCtrl* time_picker)
403 wxDateTime const date = date_picker->GetValue ();
404 wxDateTime const time = time_picker->GetValue ();
405 return boost::posix_time::ptime (
406 boost::gregorian::date (date.GetYear(), date.GetMonth() + 1, date.GetDay()),
407 boost::posix_time::time_duration (time.GetHour(), time.GetMinute(), time.GetSecond())
411 boost::posix_time::ptime
412 KDMDialog::until () const
414 return posix_time (_until_date, _until_time);
417 boost::filesystem::path
418 KDMDialog::dcp () const
420 int const item = _dcps->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
422 return wx_to_std (_dcps->GetItemText (item));
425 boost::filesystem::path
426 KDMDialog::directory () const
428 return wx_to_std (_folder->GetPath ());
432 KDMDialog::write_to () const
434 return _write_to->GetValue ();