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