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