summaryrefslogtreecommitdiff
path: root/src/tools/dcpomatic_combiner.cc
blob: 4a783d6346f65679d289a29035199b7401c24bc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
    Copyright (C) 2020-2021 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "wx/dir_picker_ctrl.h"
#include "wx/editable_list.h"
#include "wx/wx_signal_manager.h"
#include "lib/combine_dcp_job.h"
#include "lib/config.h"
#include "lib/cross.h"
#include "lib/job_manager.h"
#include "lib/util.h"
#include <dcp/combine.h>
DCPOMATIC_DISABLE_WARNINGS
#include <wx/filepicker.h>
DCPOMATIC_ENABLE_WARNINGS
#include <wx/wx.h>
#include <boost/bind/bind.hpp>
#include <boost/filesystem.hpp>
#include <exception>


using std::dynamic_pointer_cast;
using std::exception;
using std::make_shared;
using std::shared_ptr;
using std::string;
using std::vector;
using boost::optional;
#if BOOST_VERSION >= 106100
using namespace boost::placeholders;
#endif


static string
display_string (boost::filesystem::path p, int)
{
	return p.filename().string();
}


class DirDialogWrapper : public wxDirDialog
{
public:
	DirDialogWrapper (wxWindow* parent)
		: wxDirDialog (parent, _("Choose a DCP folder"), wxT(""), wxDD_DIR_MUST_EXIST)
	{

	}

	boost::filesystem::path get () const
	{
		return boost::filesystem::path(wx_to_std(GetPath()));
	}

	void set (boost::filesystem::path)
	{
		/* Not used */
	}
};


class DOMFrame : public wxFrame
{
public:
	explicit DOMFrame (wxString const & title)
		: wxFrame (nullptr, -1, title)
	{
		/* Use a panel as the only child of the Frame so that we avoid
		   the dark-grey background on Windows.
		*/
		auto overall_panel = new wxPanel (this);
		auto s = new wxBoxSizer (wxHORIZONTAL);
		s->Add (overall_panel, 1, wxEXPAND);
		SetSizer (s);

		vector<EditableListColumn> columns;
		columns.push_back(EditableListColumn(_("Input DCP"), 600, true));

		_input = new EditableList<boost::filesystem::path, DirDialogWrapper>(
			overall_panel,
			columns,
			boost::bind(&DOMFrame::inputs, this),
			boost::bind(&DOMFrame::set_inputs, this, _1),
			&display_string,
			false,
			true
			);

		auto output = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
		output->AddGrowableCol (1, 1);

		add_label_to_sizer (output, overall_panel, _("Annotation text"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
		_annotation_text = new wxTextCtrl (overall_panel, wxID_ANY, wxT(""));
		output->Add (_annotation_text, 1, wxEXPAND);

		add_label_to_sizer (output, overall_panel, _("Output DCP folder"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
		_output = new DirPickerCtrl (overall_panel);
		output->Add (_output, 1, wxEXPAND);

		_combine = new Button (overall_panel, _("Combine"));

		auto sizer = new wxBoxSizer (wxVERTICAL);
		sizer->Add (_input, 1, wxALL | wxEXPAND, DCPOMATIC_DIALOG_BORDER);
		sizer->Add (output, 0, wxALL | wxEXPAND, DCPOMATIC_DIALOG_BORDER);
		sizer->Add (_combine, 0, wxALL | wxALIGN_RIGHT, DCPOMATIC_DIALOG_BORDER);
		overall_panel->SetSizer (sizer);
		Fit ();
		SetSize (768, GetSize().GetHeight() + 32);

		_combine->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::combine, this));
		_output->Bind (wxEVT_DIRPICKER_CHANGED, boost::bind(&DOMFrame::setup_sensitivity, this));

		setup_sensitivity ();
	}

private:
	void set_inputs (vector<boost::filesystem::path> inputs)
	{
		_inputs = inputs;
	}

	vector<boost::filesystem::path> inputs () const
	{
		return _inputs;
	}

	void combine ()
	{
		using namespace boost::filesystem;

		path const output = wx_to_std(_output->GetPath());

		if (is_directory(output) && !is_empty(output)) {
			if (!confirm_dialog (
				    this,
				    std_to_wx (
					    String::compose(wx_to_std(_("The directory %1 already exists and is not empty.  "
									"Are you sure you want to use it?")),
							    output.string())
					    )
				    )) {
				return;
			}
		} else if (is_regular_file(output)) {
			error_dialog (
				this,
				String::compose (wx_to_std(_("%1 already exists as a file, so you cannot use it for a DCP.")), output.string())
				);
			return;
		}

		auto jm = JobManager::instance ();
		jm->add (make_shared<CombineDCPJob>(_inputs, output, wx_to_std(_annotation_text->GetValue())));
		bool const ok = display_progress (_("DCP-o-matic Combine"), _("Combining DCPs"));
		if (!ok) {
			return;
		}

		DCPOMATIC_ASSERT (!jm->get().empty());
		auto last = dynamic_pointer_cast<CombineDCPJob> (jm->get().back());
		DCPOMATIC_ASSERT (last);

		if (last->finished_ok()) {
			message_dialog (this, _("DCPs combined successfully."));
		} else {
			auto m = std_to_wx(last->error_summary());
			if (!last->error_details().empty()) {
				m += wxString::Format(" (%s)", std_to_wx(last->error_details()));
			}
			error_dialog (this, m);
		}
	}

	void setup_sensitivity ()
	{
		_combine->Enable (!_output->GetPath().IsEmpty());
	}

	EditableList<boost::filesystem::path, DirDialogWrapper>* _input;
	wxTextCtrl* _annotation_text = nullptr;
	DirPickerCtrl* _output;
	vector<boost::filesystem::path> _inputs;
	Button* _combine;
};


class App : public wxApp
{
public:
	App () {}

	bool OnInit () override
	{
		try {
			Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
			Config::Warning.connect (boost::bind (&App::config_warning, this, _1));

			SetAppName (_("DCP-o-matic Combiner"));

			if (!wxApp::OnInit()) {
				return false;
			}

#ifdef DCPOMATIC_LINUX
			unsetenv ("UBUNTU_MENUPROXY");
#endif

#ifdef DCPOMATIC_OSX
			make_foreground_application ();
#endif

			dcpomatic_setup_path_encoding ();

			/* Enable i18n; this will create a Config object
			   to look for a force-configured language.  This Config
			   object will be wrong, however, because dcpomatic_setup
			   hasn't yet been called and there aren't any filters etc.
			   set up yet.
			*/
			dcpomatic_setup_i18n ();

			/* Set things up, including filters etc.
			   which will now be internationalised correctly.
			*/
			dcpomatic_setup ();

			/* Force the configuration to be re-loaded correctly next
			   time it is needed.
			*/
			Config::drop ();

			_frame = new DOMFrame (_("DCP-o-matic DCP Combiner"));
			SetTopWindow (_frame);

			_frame->Show ();

			signal_manager = new wxSignalManager (this);
			Bind (wxEVT_IDLE, boost::bind(&App::idle, this, _1));
		}
		catch (exception& e)
		{
			error_dialog (nullptr, wxString::Format ("DCP-o-matic DCP Combiner could not start."), std_to_wx(e.what()));
			return false;
		}

		return true;
	}

	void config_failed_to_load ()
	{
		message_dialog (_frame, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
	}

	void config_warning (string m)
	{
		message_dialog (_frame, std_to_wx(m));
	}

	void idle (wxIdleEvent& ev)
	{
		signal_manager->ui_idle ();
		ev.Skip ();
	}

	void report_exception ()
	{
		try {
			throw;
		} catch (FileError& e) {
			error_dialog (
				0,
				wxString::Format(
					_("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
					std_to_wx (e.what()),
					std_to_wx (e.file().string().c_str ())
					)
				);
		} catch (exception& e) {
			error_dialog (
				0,
				wxString::Format(
					_("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
					std_to_wx (e.what ())
					)
				);
		} catch (...) {
			error_dialog (nullptr, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
		}
	}

	bool OnExceptionInMainLoop () override
	{
		report_exception ();
		/* This will terminate the program */
		return false;
	}

	void OnUnhandledException () override
	{
		report_exception ();
	}

	DOMFrame* _frame = nullptr;
};

IMPLEMENT_APP (App)