summaryrefslogtreecommitdiff
path: root/src/wx/dkdm_output_panel.cc
blob: d8645ff9713d5e8a24d51aded011e87fb1ac4f23 (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
/*
    Copyright (C) 2015-2020 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 "lib/config.h"
#include "lib/send_kdm_email_job.h"
#include "lib/warnings.h"
#include "dkdm_output_panel.h"
#include "kdm_timing_panel.h"
#include "confirm_kdm_email_dialog.h"
#include "wx_util.h"
#include "name_format_editor.h"
#include "check_box.h"
#include "dcpomatic_button.h"
#include <dcp/exceptions.h>
#include <dcp/types.h>
#ifdef DCPOMATIC_USE_OWN_PICKER
#include "dir_picker_ctrl.h"
#else
DCPOMATIC_DISABLE_WARNINGS
#include <wx/filepicker.h>
DCPOMATIC_ENABLE_WARNINGS
#endif
#include <wx/stdpaths.h>

using std::pair;
using std::string;
using std::list;
using std::exception;
using std::make_pair;
using boost::shared_ptr;
using boost::function;


DKDMOutputPanel::DKDMOutputPanel (wxWindow* parent)
	: wxPanel (parent, wxID_ANY)
{
	wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, 0);
	table->AddGrowableCol (1);

	add_label_to_sizer (table, this, _("Filename format"), true, 0, wxALIGN_TOP | wxTOP | wxLEFT | wxRIGHT);
	dcp::NameFormat::Map titles;
	titles['f'] = wx_to_std (_("film name"));
	titles['b'] = wx_to_std (_("from date/time"));
	titles['e'] = wx_to_std (_("to date/time"));
	dcp::NameFormat::Map ex;
	ex['f'] = "Bambi";
	ex['b'] = "2012/03/15 12:30";
	ex['e'] = "2012/03/22 02:30";
	_filename_format = new NameFormatEditor (this, Config::instance()->dkdm_filename_format(), titles, ex, ".xml");
	table->Add (_filename_format->panel(), 1, wxEXPAND);

	_write_to = new CheckBox (this, _("Write to"));
	table->Add (_write_to, 1, wxEXPAND);

#ifdef DCPOMATIC_USE_OWN_PICKER
	_folder = new DirPickerCtrl (this);
#else
	_folder = new wxDirPickerCtrl (this, wxID_ANY, wxEmptyString, wxDirSelectorPromptStr, wxDefaultPosition, wxSize (300, -1));
#endif

	boost::optional<boost::filesystem::path> path = Config::instance()->default_kdm_directory ();
	if (path) {
		_folder->SetPath (std_to_wx (path->string ()));
	} else {
		_folder->SetPath (wxStandardPaths::Get().GetDocumentsDir());
	}

	table->Add (_folder, 1, wxEXPAND);

	_email = new CheckBox (this, _("Send by email"));
	table->Add (_email, 1, wxEXPAND);
	table->AddSpacer (0);

	_write_to->Bind (wxEVT_CHECKBOX, boost::bind(&DKDMOutputPanel::setup_sensitivity, this));
	_email->Bind (wxEVT_CHECKBOX, boost::bind(&DKDMOutputPanel::setup_sensitivity, this));

	SetSizer (table);
}


void
DKDMOutputPanel::setup_sensitivity ()
{
	_folder->Enable (_write_to->GetValue());
}


pair<shared_ptr<Job>, int>
DKDMOutputPanel::make (
	list<KDMWithMetadataPtr> kdms, string name, function<bool (boost::filesystem::path)> confirm_overwrite
	)
{
	/* Decide whether to proceed */

	bool proceed = true;

	if (_email->GetValue()) {

		if (Config::instance()->mail_server().empty()) {
			proceed = false;
			error_dialog (this, _("You must set up a mail server in Preferences before you can send emails."));
		}

		bool kdms_with_no_email = false;
		BOOST_FOREACH (KDMWithMetadataPtr i, kdms) {
			if (i->emails().empty()) {
				kdms_with_no_email = true;
			}
		}

		if (proceed && kdms_with_no_email && !confirm_dialog (
			    this,
			    _("You have selected some cinemas that have no configured email address.  Do you want to continue?")
			    )) {
			proceed = false;
		}

		if (proceed && Config::instance()->confirm_kdm_email()) {
			list<string> emails;
			BOOST_FOREACH (KDMWithMetadataPtr const& i, kdms) {
				BOOST_FOREACH (string j, i->emails()) {
					emails.push_back (j);
				}
			}

			if (!emails.empty ()) {
				ConfirmKDMEmailDialog* d = new ConfirmKDMEmailDialog (this, emails);
				if (d->ShowModal() == wxID_CANCEL) {
					proceed = false;
				}
			}
		}
	}

	if (!proceed) {
		return make_pair (shared_ptr<Job>(), 0);
	}

	Config::instance()->set_dkdm_filename_format (_filename_format->get());

	int written = 0;
	shared_ptr<Job> job;

	try {
		written = write_files (
			kdms,
			directory(),
			_filename_format->get(),
			confirm_overwrite
			);

		if (_email->GetValue ()) {
			job.reset (
				new SendKDMEmailJob (
					kdms,
					_filename_format->get(),
					_filename_format->get(),
					name
					)
				);
		}

	} catch (dcp::NotEncryptedError& e) {
		error_dialog (this, _("CPL's content is not encrypted."));
	} catch (exception& e) {
		error_dialog (this, std_to_wx(e.what()));
	} catch (...) {
		error_dialog (this, _("An unknown exception occurred."));
	}

	return make_pair (job, written);
}


boost::filesystem::path
DKDMOutputPanel::directory () const
{
	return wx_to_std (_folder->GetPath ());
}