2 Copyright (C) 2018-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 #include "wx/about_dialog.h"
23 #include "wx/content_view.h"
24 #include "wx/dcpomatic_button.h"
25 #include "wx/playlist_editor_config_dialog.h"
26 #include "wx/wx_signal_manager.h"
27 #include "wx/wx_util.h"
28 #include "lib/config.h"
29 #include "lib/constants.h"
30 #include "lib/cross.h"
31 #include "lib/dcp_content.h"
34 #include "lib/spl_entry.h"
35 #include <dcp/filesystem.h>
36 #include <dcp/warnings.h>
37 LIBDCP_DISABLE_WARNINGS
38 #include <wx/imaglist.h>
39 #include <wx/listctrl.h>
40 #include <wx/preferences.h>
41 #include <wx/spinctrl.h>
43 LIBDCP_ENABLE_WARNINGS
49 using std::make_shared;
51 using std::shared_ptr;
56 using boost::optional;
57 using std::dynamic_pointer_cast;
58 #if BOOST_VERSION >= 106100
59 using namespace boost::placeholders;
65 save_playlist(shared_ptr<const SPL> playlist)
67 if (auto dir = Config::instance()->player_playlist_directory()) {
68 playlist->write(*dir / (playlist->id() + ".xml"));
73 class ContentDialog : public wxDialog, public ContentStore
76 ContentDialog (wxWindow* parent)
77 : wxDialog (parent, wxID_ANY, _("Add content"), wxDefaultPosition, wxSize(800, 640))
78 , _content_view (new ContentView(this))
80 _content_view->update ();
82 auto overall_sizer = new wxBoxSizer (wxVERTICAL);
83 SetSizer (overall_sizer);
85 overall_sizer->Add (_content_view, 1, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);
87 auto buttons = CreateSeparatedButtonSizer (wxOK | wxCANCEL);
89 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
92 overall_sizer->Layout ();
94 _content_view->Bind(wxEVT_LIST_ITEM_ACTIVATED, boost::bind(&ContentDialog::EndModal, this, wxID_OK));
95 _config_changed_connection = Config::instance()->Changed.connect(boost::bind(&ContentView::update, _content_view));
98 shared_ptr<Content> selected () const
100 return _content_view->selected ();
103 shared_ptr<Content> get (string digest) const override
105 return _content_view->get (digest);
109 ContentView* _content_view;
110 boost::signals2::scoped_connection _config_changed_connection;
118 PlaylistList (wxPanel* parent, ContentStore* content_store)
119 : _sizer (new wxBoxSizer(wxVERTICAL))
120 , _content_store (content_store)
123 auto label = new wxStaticText (parent, wxID_ANY, wxEmptyString);
124 label->SetLabelMarkup (_("<b>Playlists</b>"));
125 _sizer->Add (label, 0, wxTOP | wxLEFT, DCPOMATIC_SIZER_GAP * 2);
127 _list = new wxListCtrl (
128 parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL
131 _list->AppendColumn (_("Name"), wxLIST_FORMAT_LEFT, 840);
132 _list->AppendColumn (_("Length"), wxLIST_FORMAT_LEFT, 100);
134 auto button_sizer = new wxBoxSizer (wxVERTICAL);
135 _new = new Button (parent, _("New"));
136 button_sizer->Add (_new, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
137 _delete = new Button (parent, _("Delete"));
138 button_sizer->Add (_delete, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
140 auto list = new wxBoxSizer (wxHORIZONTAL);
141 list->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
142 list->Add (button_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
148 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, bind(&PlaylistList::selection_changed, this));
149 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, bind(&PlaylistList::selection_changed, this));
150 _new->Bind (wxEVT_BUTTON, bind(&PlaylistList::new_playlist, this));
151 _delete->Bind (wxEVT_BUTTON, bind(&PlaylistList::delete_playlist, this));
161 shared_ptr<SignalSPL> first_playlist () const
163 if (_playlists.empty()) {
167 return _playlists.front ();
170 boost::signals2::signal<void (shared_ptr<SignalSPL>)> Edit;
173 void setup_sensitivity()
175 _delete->Enable(static_cast<bool>(selected()));
178 void add_playlist_to_view (shared_ptr<const SignalSPL> playlist)
181 item.SetId (_list->GetItemCount());
182 long const N = _list->InsertItem (item);
183 _list->SetItem (N, 0, std_to_wx(playlist->name()));
186 void add_playlist_to_model (shared_ptr<SignalSPL> playlist)
188 _playlists.push_back (playlist);
189 playlist->Changed.connect(bind(&PlaylistList::changed, this, weak_ptr<SignalSPL>(playlist), _1));
192 void changed(weak_ptr<SignalSPL> wp, SignalSPL::Change change)
194 auto playlist = wp.lock ();
200 case SignalSPL::Change::NAME:
203 for (auto i: _playlists) {
205 _list->SetItem (N, 0, std_to_wx(i->name()));
211 case SignalSPL::Change::CONTENT:
212 save_playlist(playlist);
217 void load_playlists ()
219 auto path = Config::instance()->player_playlist_directory();
224 _list->DeleteAllItems ();
226 for (auto i: dcp::filesystem::directory_iterator(*path)) {
227 auto spl = make_shared<SignalSPL>();
229 spl->read (i, _content_store);
230 add_playlist_to_model (spl);
234 for (auto i: _playlists) {
235 add_playlist_to_view (i);
241 auto dir = Config::instance()->player_playlist_directory();
243 error_dialog(_parent, _("No playlist folder is specified in preferences. Please set one and then try again."));
247 shared_ptr<SignalSPL> spl (new SignalSPL(wx_to_std(_("New Playlist"))));
248 add_playlist_to_model (spl);
249 add_playlist_to_view (spl);
250 _list->SetItemState (_list->GetItemCount() - 1, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
253 boost::optional<int> selected() const
255 long int selected = _list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
256 if (selected < 0 || selected >= int(_playlists.size())) {
263 void delete_playlist ()
265 auto index = selected();
270 auto dir = Config::instance()->player_playlist_directory();
275 dcp::filesystem::remove(*dir / (_playlists[*index]->id() + ".xml"));
276 _list->DeleteItem(*index);
277 _playlists.erase(_playlists.begin() + *index);
279 Edit(shared_ptr<SignalSPL>());
282 void selection_changed ()
284 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
285 if (selected < 0 || selected >= int(_playlists.size())) {
286 Edit (shared_ptr<SignalSPL>());
288 Edit (_playlists[selected]);
298 vector<shared_ptr<SignalSPL>> _playlists;
299 ContentStore* _content_store;
304 class PlaylistContent
307 PlaylistContent (wxPanel* parent, ContentDialog* content_dialog)
308 : _content_dialog (content_dialog)
309 , _sizer (new wxBoxSizer(wxVERTICAL))
311 auto title = new wxBoxSizer (wxHORIZONTAL);
312 auto label = new wxStaticText (parent, wxID_ANY, wxEmptyString);
313 label->SetLabelMarkup (_("<b>Playlist:</b>"));
314 title->Add (label, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
315 _name = new wxTextCtrl (parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(400, -1));
316 title->Add (_name, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
317 _save_name = new Button(parent, _("Save"));
318 title->Add(_save_name);
319 _sizer->Add (title, 0, wxTOP | wxLEFT, DCPOMATIC_SIZER_GAP * 2);
321 auto list = new wxBoxSizer (wxHORIZONTAL);
323 _list = new wxListCtrl (
324 parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT | wxLC_SINGLE_SEL
327 _list->AppendColumn (_("Name"), wxLIST_FORMAT_LEFT, 400);
328 _list->AppendColumn (_("CPL"), wxLIST_FORMAT_LEFT, 350);
329 _list->AppendColumn (_("Type"), wxLIST_FORMAT_LEFT, 100);
330 _list->AppendColumn (_("Encrypted"), wxLIST_FORMAT_CENTRE, 90);
332 auto images = new wxImageList (16, 16);
335 tick_icon.LoadFile (bitmap_path("tick.png"), wxBITMAP_TYPE_PNG);
336 no_tick_icon.LoadFile (bitmap_path("no_tick.png"), wxBITMAP_TYPE_PNG);
337 images->Add (tick_icon);
338 images->Add (no_tick_icon);
340 _list->SetImageList (images, wxIMAGE_LIST_SMALL);
342 list->Add (_list, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
344 auto button_sizer = new wxBoxSizer (wxVERTICAL);
345 _up = new Button (parent, _("Up"));
346 _down = new Button (parent, _("Down"));
347 _add = new Button (parent, _("Add"));
348 _remove = new Button (parent, _("Remove"));
349 button_sizer->Add (_up, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
350 button_sizer->Add (_down, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
351 button_sizer->Add (_add, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
352 button_sizer->Add (_remove, 0, wxEXPAND | wxBOTTOM, DCPOMATIC_BUTTON_STACK_GAP);
354 list->Add (button_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
358 _list->Bind (wxEVT_COMMAND_LIST_ITEM_SELECTED, bind(&PlaylistContent::setup_sensitivity, this));
359 _list->Bind (wxEVT_COMMAND_LIST_ITEM_DESELECTED, bind(&PlaylistContent::setup_sensitivity, this));
360 _name->Bind (wxEVT_TEXT, bind(&PlaylistContent::name_changed, this));
361 _save_name->bind(&PlaylistContent::save_name_clicked, this);
362 _up->Bind (wxEVT_BUTTON, bind(&PlaylistContent::up_clicked, this));
363 _down->Bind (wxEVT_BUTTON, bind(&PlaylistContent::down_clicked, this));
364 _add->Bind (wxEVT_BUTTON, bind(&PlaylistContent::add_clicked, this));
365 _remove->Bind (wxEVT_BUTTON, bind(&PlaylistContent::remove_clicked, this));
375 void set (shared_ptr<SignalSPL> playlist)
377 _playlist = playlist;
378 _list->DeleteAllItems ();
380 for (auto i: _playlist->get()) {
383 _name->SetValue (std_to_wx(_playlist->name()));
385 _name->SetValue (wxT(""));
387 setup_sensitivity ();
390 shared_ptr<SignalSPL> playlist () const
397 void save_name_clicked()
400 _playlist->set_name(wx_to_std(_name->GetValue()));
401 save_playlist(_playlist);
411 void add (SPLEntry e)
414 item.SetId (_list->GetItemCount());
415 long const N = _list->InsertItem (item);
419 void set_item (long N, SPLEntry e)
421 _list->SetItem (N, 0, std_to_wx(e.name));
422 _list->SetItem (N, 1, std_to_wx(e.id));
423 _list->SetItem (N, 2, std_to_wx(e.kind->name()));
424 _list->SetItem (N, 3, e.encrypted ? S_("Question|Y") : S_("Question|N"));
427 void setup_sensitivity ()
429 bool const have_list = static_cast<bool>(_playlist);
430 int const num_selected = _list->GetSelectedItemCount ();
431 long int selected = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
432 _name->Enable (have_list);
433 _save_name->Enable(_playlist && _playlist->name() != wx_to_std(_name->GetValue()));
434 _list->Enable (have_list);
435 _up->Enable (have_list && selected > 0);
436 _down->Enable (have_list && selected != -1 && selected < (_list->GetItemCount() - 1));
437 _add->Enable (have_list);
438 _remove->Enable (have_list && num_selected > 0);
443 int const r = _content_dialog->ShowModal ();
445 auto content = _content_dialog->selected ();
447 SPLEntry e (content);
449 DCPOMATIC_ASSERT (_playlist);
457 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
462 DCPOMATIC_ASSERT (_playlist);
464 _playlist->swap(s, s - 1);
466 set_item (s - 1, (*_playlist)[s-1]);
467 set_item (s, (*_playlist)[s]);
472 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
473 if (s > (_list->GetItemCount() - 1)) {
477 DCPOMATIC_ASSERT (_playlist);
479 _playlist->swap(s, s + 1);
481 set_item (s + 1, (*_playlist)[s+1]);
482 set_item (s, (*_playlist)[s]);
485 void remove_clicked ()
487 long int s = _list->GetNextItem (-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
492 DCPOMATIC_ASSERT (_playlist);
493 _playlist->remove (s);
494 _list->DeleteItem (s);
497 ContentDialog* _content_dialog;
506 shared_ptr<SignalSPL> _playlist;
510 class DOMFrame : public wxFrame
513 explicit DOMFrame (wxString const & title)
514 : wxFrame (nullptr, wxID_ANY, title)
515 , _content_dialog (new ContentDialog(this))
516 , _config_dialog (nullptr)
518 auto bar = new wxMenuBar;
522 /* Use a panel as the only child of the Frame so that we avoid
523 the dark-grey background on Windows.
525 auto overall_panel = new wxPanel (this, wxID_ANY);
526 auto sizer = new wxBoxSizer (wxVERTICAL);
528 _playlist_list = new PlaylistList (overall_panel, _content_dialog);
529 _playlist_content = new PlaylistContent (overall_panel, _content_dialog);
531 sizer->Add (_playlist_list->sizer());
532 sizer->Add (_playlist_content->sizer());
534 overall_panel->SetSizer (sizer);
536 _playlist_list->Edit.connect (bind(&DOMFrame::change_playlist, this, _1));
538 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
539 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
540 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
542 _config_changed_connection = Config::instance()->Changed.connect(boost::bind(&DOMFrame::config_changed, this));
549 /* false here allows the close handler to veto the close request */
555 auto d = make_wx<AboutDialog>(this);
559 void edit_preferences ()
561 if (!_config_dialog) {
562 _config_dialog = create_playlist_editor_config_dialog ();
564 _config_dialog->Show (this);
567 void change_playlist (shared_ptr<SignalSPL> playlist)
569 auto old = _playlist_content->playlist ();
573 _playlist_content->set (playlist);
576 void setup_menu (wxMenuBar* m)
578 auto file = new wxMenu;
580 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
581 file->Append (wxID_EXIT, _("&Exit"));
583 file->Append (wxID_EXIT, _("&Quit"));
587 auto edit = new wxMenu;
588 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
591 auto help = new wxMenu;
593 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
595 help->Append (wxID_ABOUT, _("About"));
598 m->Append (file, _("&File"));
600 m->Append (edit, _("&Edit"));
602 m->Append (help, _("&Help"));
606 void config_changed ()
609 Config::instance()->write_config();
610 } catch (exception& e) {
614 _("Could not write to config file at %s. Your changes have not been saved."),
615 std_to_wx (Config::instance()->cinemas_file().string()).data()
621 ContentDialog* _content_dialog;
622 PlaylistList* _playlist_list;
623 PlaylistContent* _playlist_content;
624 wxPreferencesEditor* _config_dialog;
625 boost::signals2::scoped_connection _config_changed_connection;
630 * @brief The magic App class for wxWidgets.
632 class App : public wxApp
642 bool OnInit () override
645 wxInitAllImageHandlers ();
646 SetAppName (_("DCP-o-matic Playlist Editor"));
648 if (!wxApp::OnInit()) {
652 #ifdef DCPOMATIC_LINUX
653 unsetenv ("UBUNTU_MENUPROXY");
657 make_foreground_application ();
660 dcpomatic_setup_path_encoding ();
662 /* Enable i18n; this will create a Config object
663 to look for a force-configured language. This Config
664 object will be wrong, however, because dcpomatic_setup
665 hasn't yet been called and there aren't any filters etc.
668 dcpomatic_setup_i18n ();
670 /* Set things up, including filters etc.
671 which will now be internationalised correctly.
675 /* Force the configuration to be re-loaded correctly next
680 _frame = new DOMFrame (_("DCP-o-matic Playlist Editor"));
681 SetTopWindow (_frame);
685 signal_manager = new wxSignalManager (this);
686 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
692 error_dialog (0, _("DCP-o-matic could not start"), std_to_wx(e.what()));
696 /* An unhandled exception has occurred inside the main event loop */
697 bool OnExceptionInMainLoop () override
701 } catch (FileError& e) {
705 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
706 std_to_wx (e.what()),
707 std_to_wx (e.file().string().c_str ())
710 } catch (exception& e) {
714 _("An exception occurred: %s.\n\n") + " " + REPORT_PROBLEM,
715 std_to_wx (e.what ())
719 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
722 /* This will terminate the program */
726 void OnUnhandledException () override
728 error_dialog (0, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
733 signal_manager->ui_idle ();