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