Part of work to add emailing of KDMs.
[dcpomatic.git] / src / wx / kdm_dialog.cc
1 /*
2     Copyright (C) 2012 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 "lib/cinema.h"
25 #include "lib/config.h"
26 #include "kdm_dialog.h"
27 #include "cinema_dialog.h"
28 #include "screen_dialog.h"
29 #include "wx_util.h"
30 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
31 #include "dir_picker_ctrl.h"
32 #else
33 #include <wx/filepicker.h>
34 #endif
35
36 using std::string;
37 using std::map;
38 using std::list;
39 using std::pair;
40 using std::make_pair;
41 using boost::shared_ptr;
42
43 KDMDialog::KDMDialog (wxWindow* parent)
44         : wxDialog (parent, wxID_ANY, _("Make KDMs"))
45 {
46         wxBoxSizer* vertical = new wxBoxSizer (wxVERTICAL);
47         wxBoxSizer* targets = new wxBoxSizer (wxHORIZONTAL);
48         
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);
51
52         _root = _targets->AddRoot ("Foo");
53
54         list<shared_ptr<Cinema> > c = Config::instance()->cinemas ();
55         for (list<shared_ptr<Cinema> >::iterator i = c.begin(); i != c.end(); ++i) {
56                 add_cinema (*i);
57         }
58
59         _targets->ExpandAll ();
60
61         wxBoxSizer* target_buttons = new wxBoxSizer (wxVERTICAL);
62
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);
69         
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);
76
77         targets->Add (target_buttons, 0, 0, 6);
78
79         vertical->Add (targets, 1, wxEXPAND | wxALL, 6);
80
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);
87         
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);
93
94         _write_to = new wxRadioButton (this, wxID_ANY, _("Write to"));
95         table->Add (_write_to, 1, wxEXPAND);
96
97 #ifdef DCPOMATIC_USE_OWN_DIR_PICKER
98         _folder = new DirPickerCtrl (this); 
99 #else   
100         _folder = new wxDirPickerCtrl (this, wxID_ANY);
101 #endif
102
103         _folder->SetPath (wxStandardPaths::Get().GetDocumentsDir());
104         
105         table->Add (_folder, 1, wxEXPAND);
106         table->AddSpacer (0);
107
108         _email = new wxRadioButton (this, wxID_ANY, _("Send by email"));
109         table->Add (_email, 1, wxEXPAND);
110         table->AddSpacer (0);
111         
112         vertical->Add (table, 0, wxEXPAND | wxALL, 6);
113
114         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
115         if (buttons) {
116                 vertical->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
117         }
118
119         _targets->Bind       (wxEVT_COMMAND_TREE_SEL_CHANGED, boost::bind (&KDMDialog::setup_sensitivity, this));
120
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));
124
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));
128
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));
131
132         setup_sensitivity ();
133         
134         SetSizer (vertical);
135         vertical->Layout ();
136         vertical->SetSizeHints (this);
137 }
138
139 list<pair<wxTreeItemId, shared_ptr<Cinema> > >
140 KDMDialog::selected_cinemas () const
141 {
142         wxArrayTreeItemIds s;
143         _targets->GetSelections (s);
144
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));
150                 }
151         }
152
153         return c;
154 }
155
156 list<pair<wxTreeItemId, shared_ptr<Screen> > >
157 KDMDialog::selected_screens () const
158 {
159         wxArrayTreeItemIds s;
160         _targets->GetSelections (s);
161
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));
167                 }
168         }
169
170         return c;
171 }
172
173 void
174 KDMDialog::setup_sensitivity ()
175 {
176         bool const sc = selected_cinemas().size() == 1;
177         bool const ss = selected_screens().size() == 1;
178         
179         _edit_cinema->Enable (sc);
180         _remove_cinema->Enable (sc);
181         
182         _add_screen->Enable (sc);
183         _edit_screen->Enable (ss);
184         _remove_screen->Enable (ss);
185
186         wxButton* ok = dynamic_cast<wxButton *> (FindWindowById (wxID_OK));
187         ok->Enable (sc || ss);
188
189         _folder->Enable (_write_to->GetValue ());
190 }
191
192 void
193 KDMDialog::add_cinema (shared_ptr<Cinema> c)
194 {
195         _cinemas[_targets->AppendItem (_root, std_to_wx (c->name))] = c;
196
197         list<shared_ptr<Screen> > sc = c->screens ();
198         for (list<shared_ptr<Screen> >::iterator i = sc.begin(); i != sc.end(); ++i) {
199                 add_screen (c, *i);
200         }
201 }
202
203 void
204 KDMDialog::add_screen (shared_ptr<Cinema> c, shared_ptr<Screen> s)
205 {
206         map<wxTreeItemId, shared_ptr<Cinema> >::const_iterator i = _cinemas.begin();
207         while (i != _cinemas.end() && i->second != c) {
208                 ++i;
209         }
210
211         if (i == _cinemas.end()) {
212                 return;
213         }
214
215         _screens[_targets->AppendItem (i->first, std_to_wx (s->name))] = s;
216 }
217
218 void
219 KDMDialog::add_cinema_clicked ()
220 {
221         CinemaDialog* d = new CinemaDialog (this, "Add Cinema");
222         d->ShowModal ();
223
224         shared_ptr<Cinema> c (new Cinema (d->name(), d->email()));
225         Config::instance()->add_cinema (c);
226         add_cinema (c);
227
228         Config::instance()->write ();
229         
230         d->Destroy ();
231 }
232
233 void
234 KDMDialog::edit_cinema_clicked ()
235 {
236         if (selected_cinemas().size() != 1) {
237                 return;
238         }
239
240         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
241         
242         CinemaDialog* d = new CinemaDialog (this, "Edit cinema", c.second->name, c.second->email);
243         d->ShowModal ();
244
245         c.second->name = d->name ();
246         c.second->email = d->email ();
247         _targets->SetItemText (c.first, std_to_wx (d->name()));
248
249         Config::instance()->write ();
250
251         d->Destroy ();  
252 }
253
254 void
255 KDMDialog::remove_cinema_clicked ()
256 {
257         if (selected_cinemas().size() != 1) {
258                 return;
259         }
260
261         pair<wxTreeItemId, shared_ptr<Cinema> > c = selected_cinemas().front();
262
263         Config::instance()->remove_cinema (c.second);
264         _targets->Delete (c.first);
265
266         Config::instance()->write ();   
267 }
268
269 void
270 KDMDialog::add_screen_clicked ()
271 {
272         if (selected_cinemas().size() != 1) {
273                 return;
274         }
275
276         shared_ptr<Cinema> c = selected_cinemas().front().second;
277         
278         ScreenDialog* d = new ScreenDialog (this, "Add Screen");
279         d->ShowModal ();
280
281         shared_ptr<Screen> s (new Screen (d->name(), d->certificate()));
282         c->add_screen (s);
283         add_screen (c, s);
284
285         Config::instance()->write ();
286
287         d->Destroy ();
288 }
289
290 void
291 KDMDialog::edit_screen_clicked ()
292 {
293         if (selected_screens().size() != 1) {
294                 return;
295         }
296
297         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
298         
299         ScreenDialog* d = new ScreenDialog (this, "Edit screen", s.second->name, s.second->certificate);
300         d->ShowModal ();
301
302         s.second->name = d->name ();
303         s.second->certificate = d->certificate ();
304         _targets->SetItemText (s.first, std_to_wx (d->name()));
305
306         Config::instance()->write ();
307
308         d->Destroy ();
309 }
310
311 void
312 KDMDialog::remove_screen_clicked ()
313 {
314         if (selected_screens().size() != 1) {
315                 return;
316         }
317
318         pair<wxTreeItemId, shared_ptr<Screen> > s = selected_screens().front();
319
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()) {
323                 ++i;
324         }
325
326         if (i == _cinemas.end()) {
327                 return;
328         }
329
330         i->second->remove_screen (s.second);
331         _targets->Delete (s.first);
332
333         Config::instance()->write ();
334 }
335
336 list<shared_ptr<Screen> >
337 KDMDialog::screens () const
338 {
339         list<shared_ptr<Screen> > s;
340
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) {
345                         s.push_back (*j);
346                 }
347         }
348
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);
352         }
353
354         s.sort ();
355         s.unique ();
356
357         return s;
358 }
359
360 boost::posix_time::ptime
361 KDMDialog::from () const
362 {
363         return posix_time (_from_date, _from_time);
364 }
365
366 boost::posix_time::ptime
367 KDMDialog::posix_time (wxDatePickerCtrl* date_picker, wxTimePickerCtrl* time_picker)
368 {
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())
374                 );
375 }
376
377 boost::posix_time::ptime
378 KDMDialog::until () const
379 {
380         return posix_time (_until_date, _until_time);
381 }
382
383 string
384 KDMDialog::directory () const
385 {
386         return wx_to_std (_folder->GetPath ());
387 }