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