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