2 Copyright (C) 2020-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/dir_picker_ctrl.h"
23 #include "wx/editable_list.h"
24 #include "wx/wx_signal_manager.h"
25 #include "lib/combine_dcp_job.h"
26 #include "lib/config.h"
27 #include "lib/cross.h"
28 #include "lib/job_manager.h"
30 #include <dcp/combine.h>
31 DCPOMATIC_DISABLE_WARNINGS
32 #include <wx/filepicker.h>
33 DCPOMATIC_ENABLE_WARNINGS
35 #include <boost/bind/bind.hpp>
36 #include <boost/filesystem.hpp>
40 using std::dynamic_pointer_cast;
42 using std::make_shared;
43 using std::shared_ptr;
46 using boost::optional;
47 #if BOOST_VERSION >= 106100
48 using namespace boost::placeholders;
53 display_string (boost::filesystem::path p, int)
55 return p.filename().string();
59 class DirDialogWrapper : public wxDirDialog
62 DirDialogWrapper (wxWindow* parent)
63 : wxDirDialog (parent, _("Choose a DCP folder"), wxT(""), wxDD_DIR_MUST_EXIST)
68 boost::filesystem::path get () const
70 return boost::filesystem::path(wx_to_std(GetPath()));
73 void set (boost::filesystem::path)
80 class DOMFrame : public wxFrame
83 explicit DOMFrame (wxString const & title)
84 : wxFrame (nullptr, -1, title)
86 /* Use a panel as the only child of the Frame so that we avoid
87 the dark-grey background on Windows.
89 auto overall_panel = new wxPanel (this);
90 auto s = new wxBoxSizer (wxHORIZONTAL);
91 s->Add (overall_panel, 1, wxEXPAND);
94 vector<EditableListColumn> columns;
95 columns.push_back(EditableListColumn(_("Input DCP"), 600, true));
97 _input = new EditableList<boost::filesystem::path, DirDialogWrapper>(
100 boost::bind(&DOMFrame::inputs, this),
101 boost::bind(&DOMFrame::set_inputs, this, _1),
107 auto output = new wxBoxSizer (wxHORIZONTAL);
108 add_label_to_sizer (output, overall_panel, _("Output DCP folder"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
109 _output = new DirPickerCtrl (overall_panel);
110 output->Add (_output, 1, wxEXPAND);
112 _combine = new Button (overall_panel, _("Combine"));
114 auto sizer = new wxBoxSizer (wxVERTICAL);
115 sizer->Add (_input, 1, wxALL | wxEXPAND, DCPOMATIC_DIALOG_BORDER);
116 sizer->Add (output, 0, wxALL | wxEXPAND, DCPOMATIC_DIALOG_BORDER);
117 sizer->Add (_combine, 0, wxALL | wxALIGN_RIGHT, DCPOMATIC_DIALOG_BORDER);
118 overall_panel->SetSizer (sizer);
120 SetSize (768, GetSize().GetHeight() + 32);
122 _combine->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::combine, this));
123 _output->Bind (wxEVT_DIRPICKER_CHANGED, boost::bind(&DOMFrame::setup_sensitivity, this));
125 setup_sensitivity ();
129 void set_inputs (vector<boost::filesystem::path> inputs)
134 vector<boost::filesystem::path> inputs () const
141 using namespace boost::filesystem;
143 path const output = wx_to_std(_output->GetPath());
145 if (is_directory(output) && !is_empty(output)) {
146 if (!confirm_dialog (
149 String::compose(wx_to_std(_("The directory %1 already exists and is not empty. "
150 "Are you sure you want to use it?")),
156 } else if (is_regular_file(output)) {
159 String::compose (wx_to_std(_("%1 already exists as a file, so you cannot use it for a DCP.")), output.string())
164 auto jm = JobManager::instance ();
165 jm->add (make_shared<CombineDCPJob>(_inputs, output));
166 bool const ok = display_progress (_("DCP-o-matic Combine"), _("Combining DCPs"));
171 DCPOMATIC_ASSERT (!jm->get().empty());
172 auto last = dynamic_pointer_cast<CombineDCPJob> (jm->get().back());
173 DCPOMATIC_ASSERT (last);
175 if (last->finished_ok()) {
176 message_dialog (this, _("DCPs combined successfully."));
178 auto m = std_to_wx(last->error_summary());
179 if (!last->error_details().empty()) {
180 m += wxString::Format(" (%s)", std_to_wx(last->error_details()));
182 error_dialog (this, m);
186 void setup_sensitivity ()
188 _combine->Enable (!_output->GetPath().IsEmpty());
191 EditableList<boost::filesystem::path, DirDialogWrapper>* _input;
192 DirPickerCtrl* _output;
193 vector<boost::filesystem::path> _inputs;
198 class App : public wxApp
203 bool OnInit () override
206 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
207 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
209 SetAppName (_("DCP-o-matic Combiner"));
211 if (!wxApp::OnInit()) {
215 #ifdef DCPOMATIC_LINUX
216 unsetenv ("UBUNTU_MENUPROXY");
220 make_foreground_application ();
223 dcpomatic_setup_path_encoding ();
225 /* Enable i18n; this will create a Config object
226 to look for a force-configured language. This Config
227 object will be wrong, however, because dcpomatic_setup
228 hasn't yet been called and there aren't any filters etc.
231 dcpomatic_setup_i18n ();
233 /* Set things up, including filters etc.
234 which will now be internationalised correctly.
238 /* Force the configuration to be re-loaded correctly next
243 _frame = new DOMFrame (_("DCP-o-matic DCP Combiner"));
244 SetTopWindow (_frame);
248 signal_manager = new wxSignalManager (this);
249 Bind (wxEVT_IDLE, boost::bind(&App::idle, this, _1));
253 error_dialog (nullptr, wxString::Format ("DCP-o-matic DCP Combiner could not start."), std_to_wx(e.what()));
260 void config_failed_to_load ()
262 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
265 void config_warning (string m)
267 message_dialog (_frame, std_to_wx(m));
270 void idle (wxIdleEvent& ev)
272 signal_manager->ui_idle ();
276 void report_exception ()
280 } catch (FileError& e) {
284 _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
285 std_to_wx (e.what()),
286 std_to_wx (e.file().string().c_str ())
289 } catch (exception& e) {
293 _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
294 std_to_wx (e.what ())
298 error_dialog (nullptr, _("An unknown exception occurred.") + " " + REPORT_PROBLEM);
302 bool OnExceptionInMainLoop () override
305 /* This will terminate the program */
309 void OnUnhandledException () override
314 DOMFrame* _frame = nullptr;