Extract common code out into kdm_for_screen()
[dcpomatic.git] / src / tools / swaroop_dcpomatic_playlist.cc
1 /*
2     Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "../wx/wx_util.h"
22 #include "../wx/wx_signal_manager.h"
23 #include "../wx/content_view.h"
24 #include "../wx/dcpomatic_button.h"
25 #include "../lib/util.h"
26 #include "../lib/config.h"
27 #include "../lib/cross.h"
28 #include "../lib/film.h"
29 #include "../lib/dcp_content.h"
30 #include "../lib/swaroop_spl_entry.h"
31 #include "../lib/swaroop_spl.h"
32 #include <wx/wx.h>
33 #include <wx/listctrl.h>
34 #include <wx/imaglist.h>
35 #include <wx/spinctrl.h>
36 #ifdef __WXOSX__
37 #include <ApplicationServices/ApplicationServices.h>
38 #endif
39
40 using std::exception;
41 using std::cout;
42 using std::string;
43 using boost::optional;
44 using boost::shared_ptr;
45 using boost::weak_ptr;
46 using boost::bind;
47 using boost::dynamic_pointer_cast;
48
49 class ContentDialog : public wxDialog, public ContentStore
50 {
51 public:
52         ContentDialog (wxWindow* parent)
53                 : wxDialog (parent, wxID_ANY, _("Add content"), wxDefaultPosition, wxSize(800, 640))
54                 , _content_view (new ContentView(this))
55         {
56                 _content_view->update ();
57
58                 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
59                 SetSizer (overall_sizer);
60
61                 overall_sizer->Add (_content_view, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
62
63                 wxSizer* buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
64                 if (buttons) {
65                         overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
66                 }
67
68                 overall_sizer->Layout ();
69         }
70
71         shared_ptr<Content> selected () const
72         {
73                 return _content_view->selected ();
74         }
75
76         shared_ptr<Content> get (string digest) const
77         {
78                 return _content_view->get (digest);
79         }
80
81 private:
82         ContentView* _content_view;
83 };
84
85 class DOMFrame : public wxFrame
86 {
87 public:
88         explicit DOMFrame (wxString const & title)
89                 : wxFrame (0, -1, title)
90                 , _content_dialog (new ContentDialog(this))
91         {
92                 /* Use a panel as the only child of the Frame so that we avoid
93                    the dark-grey background on Windows.
94                 */
95                 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
96                 wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
97
98                 _list = new wxListCtrl (
99                         overall_panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL
100                         );
101
102                 _list->AppendColumn (_("Name"), wxLIST_FORMAT_LEFT, 400);
103                 _list->AppendColumn (_("CPL"), wxLIST_FORMAT_LEFT, 350);
104                 _list->AppendColumn (_("Type"), wxLIST_FORMAT_CENTRE, 100);
105                 _list->AppendColumn (_("Format"), wxLIST_FORMAT_CENTRE, 75);
106                 _list->AppendColumn (_("Encrypted"), wxLIST_FORMAT_CENTRE, 90);
107                 _list->AppendColumn (_("Skippable"), wxLIST_FORMAT_CENTRE, 90);
108                 _list->AppendColumn (_("Disable timeline"), wxLIST_FORMAT_CENTRE, 125);
109                 _list->AppendColumn (_("Stop after play"), wxLIST_FORMAT_CENTRE, 125);
110
111                 wxImageList* images = new wxImageList (16, 16);
112                 wxIcon tick_icon;
113                 wxIcon no_tick_icon;
114 #ifdef DCPOMATIX_OSX
115                 tick_icon.LoadFile ("tick.png", wxBITMAP_TYPE_PNG_RESOURCE);
116                 no_tick_icon.LoadFile ("no_tick.png", wxBITMAP_TYPE_PNG_RESOURCE);
117 #else
118                 boost::filesystem::path tick_path = shared_path() / "tick.png";
119                 tick_icon.LoadFile (std_to_wx(tick_path.string()));
120                 boost::filesystem::path no_tick_path = shared_path() / "no_tick.png";
121                 no_tick_icon.LoadFile (std_to_wx(no_tick_path.string()));
122 #endif
123                 images->Add (tick_icon);
124                 images->Add (no_tick_icon);
125
126                 _list->SetImageList (images, wxIMAGE_LIST_SMALL);
127
128                 h_sizer->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
129
130                 wxBoxSizer* button_sizer = new wxBoxSizer (wxVERTICAL);
131                 _up = new Button (overall_panel, _("Up"));
132                 _down = new Button (overall_panel, _("Down"));
133                 _add = new Button (overall_panel, _("Add"));
134                 _remove = new Button (overall_panel, _("Remove"));
135                 _save = new Button (overall_panel, _("Save playlist"));
136                 _load = new Button (overall_panel, _("Load playlist"));
137                 button_sizer->Add (_up, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
138                 button_sizer->Add (_down, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
139                 button_sizer->Add (_add, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
140                 button_sizer->Add (_remove, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
141                 button_sizer->Add (_save, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
142                 button_sizer->Add (_load, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
143
144                 h_sizer->Add (button_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
145
146                 wxBoxSizer* v_sizer = new wxBoxSizer (wxVERTICAL);
147
148                 wxBoxSizer* allowed_shows_sizer = new wxBoxSizer (wxHORIZONTAL);
149                 _allowed_shows_enable = new wxCheckBox (overall_panel, wxID_ANY, _("Limit number of shows with this playlist to"));
150                 allowed_shows_sizer->Add (_allowed_shows_enable, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
151                 _allowed_shows = new wxSpinCtrl (overall_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 1, 65536, 100);
152                 allowed_shows_sizer->Add (_allowed_shows);
153
154                 v_sizer->Add (allowed_shows_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
155                 v_sizer->Add (h_sizer);
156
157                 overall_panel->SetSizer (v_sizer);
158
159                 _list->Bind (wxEVT_LEFT_DOWN, bind(&DOMFrame::list_left_click, this, _1));
160                 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, boost::bind (&DOMFrame::selection_changed, this));
161                 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, boost::bind (&DOMFrame::selection_changed, this));
162                 _up->Bind (wxEVT_BUTTON, bind(&DOMFrame::up_clicked, this));
163                 _down->Bind (wxEVT_BUTTON, bind(&DOMFrame::down_clicked, this));
164                 _add->Bind (wxEVT_BUTTON, bind(&DOMFrame::add_clicked, this));
165                 _remove->Bind (wxEVT_BUTTON, bind(&DOMFrame::remove_clicked, this));
166                 _save->Bind (wxEVT_BUTTON, bind(&DOMFrame::save_clicked, this));
167                 _load->Bind (wxEVT_BUTTON, bind(&DOMFrame::load_clicked, this));
168                 _allowed_shows_enable->Bind (wxEVT_CHECKBOX, bind(&DOMFrame::allowed_shows_changed, this));
169                 _allowed_shows->Bind (wxEVT_SPINCTRL, bind(&DOMFrame::allowed_shows_changed, this));
170
171                 setup_sensitivity ();
172         }
173
174 private:
175
176         void allowed_shows_changed ()
177         {
178                 if (_allowed_shows_enable->GetValue()) {
179                         _playlist.set_allowed_shows (_allowed_shows->GetValue());
180                 } else {
181                         _playlist.unset_allowed_shows ();
182                 }
183                 setup_sensitivity ();
184         }
185
186         void add (SPLEntry e)
187         {
188                 wxListItem item;
189                 item.SetId (_list->GetItemCount());
190                 long const N = _list->InsertItem (item);
191                 set_item (N, e);
192         }
193
194         void selection_changed ()
195         {
196                 setup_sensitivity ();
197         }
198
199         void set_item (long N, SPLEntry e)
200         {
201                 _list->SetItem (N, 0, std_to_wx(e.name));
202                 _list->SetItem (N, 1, std_to_wx(e.id));
203                 _list->SetItem (N, 2, std_to_wx(dcp::content_kind_to_string(e.kind)));
204                 _list->SetItem (N, 3, e.type == SPLEntry::DCP ? _("DCP") : _("E-cinema"));
205                 _list->SetItem (N, 4, e.encrypted ? S_("Question|Y") : S_("Question|N"));
206                 _list->SetItem (N, COLUMN_SKIPPABLE, wxEmptyString, e.skippable ? 0 : 1);
207                 _list->SetItem (N, COLUMN_DISABLE_TIMELINE, wxEmptyString, e.disable_timeline ? 0 : 1);
208                 _list->SetItem (N, COLUMN_STOP_AFTER_PLAY, wxEmptyString, e.stop_after_play ? 0 : 1);
209         }
210
211         void setup_sensitivity ()
212         {
213                 int const num_selected = _list->GetSelectedItemCount ();
214                 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
215                 _up->Enable (selected > 0);
216                 _down->Enable (selected != -1 && selected < (_list->GetItemCount() - 1));
217                 _remove->Enable (num_selected > 0);
218                 _allowed_shows->Enable (_allowed_shows_enable->GetValue());
219         }
220
221         void list_left_click (wxMouseEvent& ev)
222         {
223                 int flags;
224                 long item = _list->HitTest (ev.GetPosition(), flags, 0);
225                 int x = ev.GetPosition().x;
226                 optional<int> column;
227                 for (int i = 0; i < _list->GetColumnCount(); ++i) {
228                         x -= _list->GetColumnWidth (i);
229                         if (x < 0) {
230                                 column = i;
231                                 break;
232                         }
233                 }
234
235                 if (item != -1 && column) {
236                         switch (*column) {
237                         case COLUMN_SKIPPABLE:
238                                 _playlist[item].skippable = !_playlist[item].skippable;
239                                 break;
240                         case COLUMN_DISABLE_TIMELINE:
241                                 _playlist[item].disable_timeline = !_playlist[item].disable_timeline;
242                                 break;
243                         case COLUMN_STOP_AFTER_PLAY:
244                                 _playlist[item].stop_after_play = !_playlist[item].stop_after_play;
245                                 break;
246                         default:
247                                 ev.Skip ();
248                         }
249                         set_item (item, _playlist[item]);
250                 } else {
251                         ev.Skip ();
252                 }
253         }
254
255         void add_clicked ()
256         {
257                 int const r = _content_dialog->ShowModal ();
258                 if (r == wxID_OK) {
259                         shared_ptr<Content> content = _content_dialog->selected ();
260                         if (content) {
261                                 SPLEntry e (content);
262                                 add (e);
263                                 _playlist.add (e);
264                         }
265                 }
266         }
267
268         void up_clicked ()
269         {
270                 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
271                 if (s < 1) {
272                         return;
273                 }
274
275                 SPLEntry tmp = _playlist[s];
276                 _playlist[s] = _playlist[s-1];
277                 _playlist[s-1] = tmp;
278
279                 set_item (s - 1, _playlist[s-1]);
280                 set_item (s, _playlist[s]);
281         }
282
283         void down_clicked ()
284         {
285                 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
286                 if (s > (_list->GetItemCount() - 1)) {
287                         return;
288                 }
289
290                 SPLEntry tmp = _playlist[s];
291                 _playlist[s] = _playlist[s+1];
292                 _playlist[s+1] = tmp;
293
294                 set_item (s + 1, _playlist[s+1]);
295                 set_item (s, _playlist[s]);
296         }
297
298         void remove_clicked ()
299         {
300                 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
301                 if (s == -1) {
302                         return;
303                 }
304
305                 _playlist.remove (s);
306                 _list->DeleteItem (s);
307         }
308
309         void save_clicked ()
310         {
311                 Config* c = Config::instance ();
312                 wxString default_dir = c->player_playlist_directory() ? std_to_wx(c->player_playlist_directory()->string()) : wxString(wxEmptyString);
313                 wxFileDialog* d = new wxFileDialog (this, _("Select playlist file"), default_dir, wxEmptyString, wxT("XML files (*.xml)|*.xml"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
314                 if (d->ShowModal() == wxID_OK) {
315                         boost::filesystem::path file = wx_to_std (d->GetPath());
316                         file.replace_extension (".xml");
317                         _playlist.write (file);
318                 }
319         }
320
321         void load_clicked ()
322         {
323                 Config* c = Config::instance ();
324                 wxString default_dir = c->player_playlist_directory() ? std_to_wx(c->player_playlist_directory()->string()) : wxString(wxEmptyString);
325                 wxFileDialog* d = new wxFileDialog (this, _("Select playlist file"), default_dir, wxEmptyString, wxT("XML files (*.xml)|*.xml"));
326                 if (d->ShowModal() == wxID_OK) {
327                         _list->DeleteAllItems ();
328                         _playlist.read (wx_to_std(d->GetPath()), _content_dialog);
329                         if (!_playlist.missing()) {
330                                 _list->DeleteAllItems ();
331                                 BOOST_FOREACH (SPLEntry i, _playlist.get()) {
332                                         add (i);
333                                 }
334                         } else {
335                                 error_dialog (this, _("Some content in this playlist was not found."));
336                         }
337                         optional<int> allowed_shows = _playlist.allowed_shows ();
338                         _allowed_shows_enable->SetValue (static_cast<bool>(allowed_shows));
339                         if (allowed_shows) {
340                                 _allowed_shows->SetValue (*allowed_shows);
341                         } else {
342                                 _allowed_shows->SetValue (65536);
343                         }
344                         setup_sensitivity ();
345                 }
346         }
347
348         wxListCtrl* _list;
349         wxButton* _up;
350         wxButton* _down;
351         wxButton* _add;
352         wxButton* _remove;
353         wxButton* _save;
354         wxButton* _load;
355         wxCheckBox* _allowed_shows_enable;
356         wxSpinCtrl* _allowed_shows;
357         SPL _playlist;
358         ContentDialog* _content_dialog;
359
360         enum {
361                 COLUMN_SKIPPABLE = 5,
362                 COLUMN_DISABLE_TIMELINE = 6,
363                 COLUMN_STOP_AFTER_PLAY = 7
364         };
365 };
366
367 /** @class App
368  *  @brief The magic App class for wxWidgets.
369  */
370 class App : public wxApp
371 {
372 public:
373         App ()
374                 : wxApp ()
375                 , _frame (0)
376         {}
377
378 private:
379
380         bool OnInit ()
381         try
382         {
383                 SetAppName (_("DCP-o-matic KDM Creator"));
384
385                 if (!wxApp::OnInit()) {
386                         return false;
387                 }
388
389 #ifdef DCPOMATIC_LINUX
390                 unsetenv ("UBUNTU_MENUPROXY");
391 #endif
392
393 #ifdef __WXOSX__
394                 ProcessSerialNumber serial;
395                 GetCurrentProcess (&serial);
396                 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
397 #endif
398
399                 dcpomatic_setup_path_encoding ();
400
401                 /* Enable i18n; this will create a Config object
402                    to look for a force-configured language.  This Config
403                    object will be wrong, however, because dcpomatic_setup
404                    hasn't yet been called and there aren't any filters etc.
405                    set up yet.
406                 */
407                 dcpomatic_setup_i18n ();
408
409                 /* Set things up, including filters etc.
410                    which will now be internationalised correctly.
411                 */
412                 dcpomatic_setup ();
413
414                 /* Force the configuration to be re-loaded correctly next
415                    time it is needed.
416                 */
417                 Config::drop ();
418
419                 _frame = new DOMFrame (_("DCP-o-matic Playlist Editor"));
420                 SetTopWindow (_frame);
421                 _frame->Maximize ();
422                 _frame->Show ();
423
424                 signal_manager = new wxSignalManager (this);
425                 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
426
427                 return true;
428         }
429         catch (exception& e)
430         {
431                 error_dialog (0, _("DCP-o-matic could not start"), std_to_wx(e.what()));
432                 return true;
433         }
434
435         /* An unhandled exception has occurred inside the main event loop */
436         bool OnExceptionInMainLoop ()
437         {
438                 try {
439                         throw;
440                 } catch (FileError& e) {
441                         error_dialog (
442                                 0,
443                                 wxString::Format (
444                                         _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
445                                         std_to_wx (e.what()),
446                                         std_to_wx (e.file().string().c_str ())
447                                         )
448                                 );
449                 } catch (exception& e) {
450                         error_dialog (
451                                 0,
452                                 wxString::Format (
453                                         _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
454                                         std_to_wx (e.what ())
455                                         )
456                                 );
457                 } catch (...) {
458                         error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
459                 }
460
461                 /* This will terminate the program */
462                 return false;
463         }
464
465         void OnUnhandledException ()
466         {
467                 error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
468         }
469
470         void idle ()
471         {
472                 signal_manager->ui_idle ();
473         }
474
475         DOMFrame* _frame;
476 };
477
478 IMPLEMENT_APP (App)