Merge master.
[dcpomatic.git] / src / wx / kdm_dialog.cc
1 /*
2     Copyright (C) 2012-2014 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 <wx/stdpaths.h>
24 #include <wx/listctrl.h>
25 #include "lib/cinema.h"
26 #include "lib/config.h"
27 #include "lib/film.h"
28 #include "kdm_dialog.h"
29 #include "cinema_dialog.h"
30 #include "screen_dialog.h"
31 #include "wx_util.h"
32 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
33 #include "dir_picker_ctrl.h"
34 #else
35 #include <wx/filepicker.h>
36 #endif
37
38 using std::string;
39 using std::map;
40 using std::list;
41 using std::pair;
42 using std::cout;
43 using std::make_pair;
44 using boost::shared_ptr;
45
46 KDMDialog::KDMDialog (wxWindow* parent, boost::shared_ptr<const Film> film)
47         : wxDialog (parent, wxID_ANY, _("Make KDMs"))
48 {
49         wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
50         wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
51         
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);
54
55         _root = _targets->AddRoot ("Foo");
56
57         list<shared_ptr<Cinema> > c = Config::instance()->cinemas ();
58         for (list<shared_ptr<Cinema> >::iterator i = c.begin(); i != c.end(); ++i) {
59                 add_cinema (*i);
60         }
61
62         _targets->ExpandAll ();
63
64         wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
65
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);
72         
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);
79
80         targets->Add (target_buttons, 0, 0, 6);
81
82         vertical->Add (targets, 1, wxEXPAND | wxALL, 6);
83
84         wxFlexGridSizer* table = new wxFlexGridSizer (3, 2, 6);
85         add_label_to_sizer (table, this, _("From"), true);
86         wxDateTime from;
87         from.SetToCurrent ();
88         _from_date = new wxDatePickerCtrl (this, wxID_ANY, from);
89         table->Add (_from_date, 1, wxEXPAND);
90         _from_time = new wxTimePickerCtrl (this, wxID_ANY, from);
91         table->Add (_from_time, 1, wxEXPAND);
92
93         add_label_to_sizer (table, this, _("Until"), true);
94         wxDateTime to = from;
95         /* 1 week from now */
96         to.Add (wxDateSpan (0, 0, 1, 0));
97         _until_date = new wxDatePickerCtrl (this, wxID_ANY, to);
98         table->Add (_until_date, 1, wxEXPAND);
99         _until_time = new wxTimePickerCtrl (this, wxID_ANY, to);
100         table->Add (_until_time, 1, wxEXPAND);
101
102         vertical->Add (table, 0, wxEXPAND | wxALL, 6);
103
104         _dcps = new wxListCtrl (this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL);
105         wxListItem ip;
106         ip.SetId (0);
107         ip.SetText (_("DCP"));
108         ip.SetWidth (400);
109         _dcps->InsertColumn (0, ip);
110         vertical->Add (_dcps, 0, wxEXPAND | wxALL, 6);
111         
112         list<boost::filesystem::path> dcps = film->dcps ();
113         for (list<boost::filesystem::path>::const_iterator i = dcps.begin(); i != dcps.end(); ++i) {
114                 wxListItem item;
115                 int const n = _dcps->GetItemCount ();
116                 item.SetId (n);
117                 _dcps->InsertItem (item);
118                 _dcps->SetItem (n, 0, std_to_wx (i->string ()));
119
120                 if (dcps.size() == 1 || i->string() == film->dcp_name ()) {
121                         _dcps->SetItemState (n, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
122                 }
123         }
124         
125         table = new wxFlexGridSizer (2, 2, 6);
126
127         _write_to = new wxRadioButton (this, wxID_ANY, _("Write to"));
128         table->Add (_write_to, 1, wxEXPAND);
129
130 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
131         _folder = new DirPickerCtrl (this); 
132 #else   
133         _folder = new wxDirPickerCtrl (this, wxID_ANY, wxEmptyString, wxDirSelectorPromptStr, wxDefaultPosition, wxSize (300, -1));
134 #endif
135
136         _folder->SetPath (wxStandardPaths::Get().GetDocumentsDir());
137         
138         table->Add (_folder, 1, wxEXPAND);
139
140         _email = new wxRadioButton (this, wxID_ANY, _("Send by email"));
141         table->Add (_email, 1, wxEXPAND);
142         table->AddSpacer (0);
143         
144         vertical->Add (table, 0, wxEXPAND | wxALL, 6);
145
146         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
147         if (buttons) {
148                 vertical->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
149         }
150
151         _write_to->SetValue (true);
152
153         _targets->Bind       (wxEVT_COMMAND_TREE_SEL_CHANGED, boost::bind (&KDMDialog::setup_sensitivity, this));
154
155         _add_cinema->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::add_cinema_clicked, this));
156         _edit_cinema->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::edit_cinema_clicked, this));
157         _remove_cinema->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::remove_cinema_clicked, this));
158
159         _add_screen->Bind    (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::add_screen_clicked, this));
160         _edit_screen->Bind   (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::edit_screen_clicked, this));
161         _remove_screen->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&KDMDialog::remove_screen_clicked, this));
162
163         _dcps->Bind          (wxEVT_COMMAND_LIST_ITEM_SELECTED,   boost::bind (&KDMDialog::setup_sensitivity, this));
164         _dcps->Bind          (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&KDMDialog::setup_sensitivity, this));
165
166         _write_to->Bind      (wxEVT_COMMAND_RADIOBUTTON_SELECTED, boost::bind (&KDMDialog::setup_sensitivity, this));
167         _email->Bind         (wxEVT_COMMAND_RADIOBUTTON_SELECTED, boost::bind (&KDMDialog::setup_sensitivity, this));
168
169         setup_sensitivity ();
170         
171         SetSizer (vertical);
172         vertical->Layout ();
173         vertical->SetSizeHints (this);
174 }
175
176 list<pair<wxTreeItemId, shared_ptr<Cinema> > >
177 KDMDialog::selected_cinemas () const
178 {
179         wxArrayTreeItemIds s;
180         _targets->GetSelections (s);
181
182         list<pair<wxTreeItemId, shared_ptr<Cinema> > > c;
183         for (size_t i = 0; i < s.GetCount(); ++i) {
184                 map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator j = _cinemas.find (s[i]);
185                 if (j != _cinemas.end ()) {
186                         c.push_back (make_pair (j->first, j->second));
187                 }
188         }
189
190         return c;
191 }
192
193 list<pair<wxTreeItemId, shared_ptr<Screen> > >
194 KDMDialog::selected_screens () const
195 {
196         wxArrayTreeItemIds s;
197         _targets->GetSelections (s);
198
199         list<pair<wxTreeItemId, shared_ptr<Screen> > > c;
200         for (size_t i = 0; i < s.GetCount(); ++i) {
201                 map<wxTreeItemId, shared_ptr<Screen> >::const_iterator j = _screens.find (s[i]);
202                 if (j != _screens.end ()) {
203                         c.push_back (make_pair (j->first, j->second));
204                 }
205         }
206
207         return c;
208 }
209
210 void
211 KDMDialog::setup_sensitivity ()
212 {
213         bool const sc = selected_cinemas().size() == 1;
214         bool const ss = selected_screens().size() == 1;
215         bool const sd = _dcps->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED) != -1;
216         
217         _edit_cinema->Enable (sc);
218         _remove_cinema->Enable (sc);
219         
220         _add_screen->Enable (sc);
221         _edit_screen->Enable (ss);
222         _remove_screen->Enable (ss);
223
224         wxButton* ok = dynamic_cast<wxButton *> (FindWindowById (wxID_OK));
225         ok->Enable ((selected_cinemas().size() > 0 || selected_screens().size() > 0) && sd);
226
227         _folder->Enable (_write_to->GetValue ());
228 }
229
230 void
231 KDMDialog::add_cinema (shared_ptr<Cinema> c)
232 {
233         _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
234
235         list<shared_ptr<Screen> > sc = c->screens ();
236         for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
237                 add_screen (c, *i);
238         }
239 }
240
241 void
242 KDMDialog::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
243 {
244         map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator i = _cinemas.begin();
245         while (i != _cinemas.end() && i->second != c) {
246                 ++i;
247         }
248
249         if (i == _cinemas.end()) {
250                 return;
251         }
252
253         _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
254 }
255
256 void
257 KDMDialog::add_cinema_clicked ()
258 {
259         CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
260         d->ShowModal ();
261
262         shared_ptr<Cinema> c (new Cinema (d->name(), d->email()));
263         Config::instance()->add_cinema (c);
264         add_cinema (c);
265
266         d->Destroy ();
267 }
268
269 void
270 KDMDialog::edit_cinema_clicked ()
271 {
272         if (selected_cinemas().size() != 1) {
273                 return;
274         }
275
276         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
277         
278         CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->email);
279         d->ShowModal ();
280
281         c.second->name = d->name ();
282         c.second->email = d->email ();
283         _targets->SetItemText (c.first, std_to_wx (d->name()));
284
285         Config::instance()->changed ();
286
287         d->Destroy ();  
288 }
289
290 void
291 KDMDialog::remove_cinema_clicked ()
292 {
293         if (selected_cinemas().size() != 1) {
294                 return;
295         }
296
297         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
298
299         Config::instance()->remove_cinema (c.second);
300         _targets->Delete (c.first);
301 }
302
303 void
304 KDMDialog::add_screen_clicked ()
305 {
306         if (selected_cinemas().size() != 1) {
307                 return;
308         }
309
310         shared_ptr<Cinema> c = selected_cinemas().front().second;
311         
312         ScreenDialog* d = new ScreenDialog (this, "Add Screen");
313         if (d->ShowModal () != wxID_OK) {
314                 return;
315         }
316
317         shared_ptr<Screen> s (new Screen (d->name(), d->certificate()));
318         c->add_screen (s);
319         add_screen (c, s);
320
321         Config::instance()->changed ();
322
323         d->Destroy ();
324 }
325
326 void
327 KDMDialog::edit_screen_clicked ()
328 {
329         if (selected_screens().size() != 1) {
330                 return;
331         }
332
333         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
334         
335         ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->certificate);
336         d->ShowModal ();
337
338         s.second->name = d->name ();
339         s.second->certificate = d->certificate ();
340         _targets->SetItemText (s.first, std_to_wx (d->name()));
341
342         Config::instance()->changed ();
343
344         d->Destroy ();
345 }
346
347 void
348 KDMDialog::remove_screen_clicked ()
349 {
350         if (selected_screens().size() != 1) {
351                 return;
352         }
353
354         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
355
356         map<wxTreeItemId, shared_ptr<Cinema> >::iterator i = _cinemas.begin ();
357         while (i != _cinemas.end ()) {
358                 list<shared_ptr<Screen> > sc = i->second->screens ();
359                 if (find (sc.begin(), sc.end(), s.second) != sc.end ()) {
360                         break;
361                 }
362         }
363
364         if (i == _cinemas.end()) {
365                 return;
366         }
367
368         i->second->remove_screen (s.second);
369         _targets->Delete (s.first);
370
371         Config::instance()->changed ();
372 }
373
374 list<shared_ptr<Screen> >
375 KDMDialog::screens () const
376 {
377         list<shared_ptr<Screen> > s;
378
379         list<pair<wxTreeItemId, shared_ptr<Cinema> > > cinemas = selected_cinemas ();
380         for (list<pair<wxTreeItemId, shared_ptr<Cinema> > >::iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
381                 list<shared_ptr<Screen> > sc = i->second->screens ();
382                 for (list<shared_ptr<Screen> >::const_iterator j = sc.begin(); j != sc.end(); ++j) {
383                         s.push_back (*j);
384                 }
385         }
386
387         list<pair<wxTreeItemId, shared_ptr<Screen> > > screens = selected_screens ();
388         for (list<pair<wxTreeItemId, shared_ptr<Screen> > >::iterator i = screens.begin(); i != screens.end(); ++i) {
389                 s.push_back (i->second);
390         }
391
392         s.sort ();
393         s.unique ();
394
395         return s;
396 }
397
398 boost::posix_time::ptime
399 KDMDialog::from () const
400 {
401         return posix_time (_from_date, _from_time);
402 }
403
404 boost::posix_time::ptime
405 KDMDialog::posix_time (wxDatePickerCtrl* date_picker, wxTimePickerCtrl* time_picker)
406 {
407         wxDateTime const date = date_picker->GetValue ();
408         wxDateTime const time = time_picker->GetValue ();
409         return boost::posix_time::ptime (
410                 boost::gregorian::date (date.GetYear(), date.GetMonth() + 1, date.GetDay()),
411                 boost::posix_time::time_duration (time.GetHour(), time.GetMinute(), time.GetSecond())
412                 );
413 }
414
415 boost::posix_time::ptime
416 KDMDialog::until () const
417 {
418         return posix_time (_until_date, _until_time);
419 }
420
421 boost::filesystem::path
422 KDMDialog::dcp () const
423 {
424         int const item = _dcps->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
425         assert (item >= 0);
426         return wx_to_std (_dcps->GetItemText (item));
427 }
428
429 boost::filesystem::path
430 KDMDialog::directory () const
431 {
432         return wx_to_std (_folder->GetPath ());
433 }
434
435 bool
436 KDMDialog::write_to () const
437 {
438         return _write_to->GetValue ();
439 }