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