Supporters update.
[dcpomatic.git] / src / wx / dkdm_output_panel.cc
1 /*
2     Copyright (C) 2015-2020 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
22 #include "check_box.h"
23 #include "confirm_kdm_email_dialog.h"
24 #include "dcpomatic_button.h"
25 #include "dkdm_output_panel.h"
26 #include "kdm_timing_panel.h"
27 #include "name_format_editor.h"
28 #include "wx_util.h"
29 #include "lib/config.h"
30 #include "lib/send_kdm_email_job.h"
31 #include <dcp/exceptions.h>
32 #include <dcp/types.h>
33 #include <dcp/warnings.h>
34
35 #ifdef DCPOMATIC_USE_OWN_PICKER
36 #include "dir_picker_ctrl.h"
37 #else
38 LIBDCP_DISABLE_WARNINGS
39 #include <wx/filepicker.h>
40 LIBDCP_ENABLE_WARNINGS
41 #endif
42
43 LIBDCP_DISABLE_WARNINGS
44 #include <wx/stdpaths.h>
45 LIBDCP_ENABLE_WARNINGS
46
47
48 using std::exception;
49 using std::function;
50 using std::list;
51 using std::make_pair;
52 using std::pair;
53 using std::shared_ptr;
54 using std::string;
55
56
57 DKDMOutputPanel::DKDMOutputPanel (wxWindow* parent)
58         : wxPanel (parent, wxID_ANY)
59 {
60         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, 0);
61         table->AddGrowableCol (1);
62
63         add_label_to_sizer (table, this, _("Filename format"), true, 0, wxALIGN_TOP | wxTOP | wxLEFT | wxRIGHT);
64         dcp::NameFormat::Map titles;
65         titles['f'] = wx_to_std (_("film name"));
66         titles['b'] = wx_to_std (_("from date/time"));
67         titles['e'] = wx_to_std (_("to date/time"));
68         dcp::NameFormat::Map ex;
69         ex['f'] = "Bambi";
70         ex['b'] = "2012/03/15 12:30";
71         ex['e'] = "2012/03/22 02:30";
72         _filename_format = new NameFormatEditor (this, Config::instance()->dkdm_filename_format(), titles, ex, ".xml");
73         table->Add (_filename_format->panel(), 1, wxEXPAND);
74
75         _write_to = new CheckBox (this, _("Write to"));
76         table->Add (_write_to, 1, wxEXPAND);
77
78 #ifdef DCPOMATIC_USE_OWN_PICKER
79         _folder = new DirPickerCtrl (this);
80 #else
81         _folder = new wxDirPickerCtrl (this, wxID_ANY, wxEmptyString, wxDirSelectorPromptStr, wxDefaultPosition, wxSize (300, -1));
82 #endif
83
84         boost::optional<boost::filesystem::path> path = Config::instance()->default_kdm_directory ();
85         if (path) {
86                 _folder->SetPath (std_to_wx (path->string ()));
87         } else {
88                 _folder->SetPath (wxStandardPaths::Get().GetDocumentsDir());
89         }
90
91         table->Add (_folder, 1, wxEXPAND);
92
93         _email = new CheckBox (this, _("Send by email"));
94         table->Add (_email, 1, wxEXPAND);
95         table->AddSpacer (0);
96
97         _write_to->bind(&DKDMOutputPanel::setup_sensitivity, this);
98         _email->bind(&DKDMOutputPanel::setup_sensitivity, this);
99
100         SetSizer (table);
101 }
102
103
104 void
105 DKDMOutputPanel::setup_sensitivity ()
106 {
107         _folder->Enable (_write_to->GetValue());
108 }
109
110
111 pair<shared_ptr<Job>, int>
112 DKDMOutputPanel::make (
113         list<KDMWithMetadataPtr> kdms, string name, function<bool (boost::filesystem::path)> confirm_overwrite
114         )
115 {
116         /* Decide whether to proceed */
117
118         bool proceed = true;
119
120         if (_email->GetValue()) {
121
122                 if (Config::instance()->mail_server().empty()) {
123                         proceed = false;
124                         error_dialog (this, _("You must set up a mail server in Preferences before you can send emails."));
125                 }
126
127                 bool kdms_with_no_email = false;
128                 for (auto i: kdms) {
129                         if (i->emails().empty()) {
130                                 kdms_with_no_email = true;
131                         }
132                 }
133
134                 if (proceed && kdms_with_no_email && !confirm_dialog (
135                             this,
136                             _("You have selected some cinemas that have no configured email address.  Do you want to continue?")
137                             )) {
138                         proceed = false;
139                 }
140
141                 if (proceed && Config::instance()->confirm_kdm_email()) {
142                         list<string> emails;
143                         for (auto const& i: kdms) {
144                                 for (auto j: i->emails()) {
145                                         emails.push_back (j);
146                                 }
147                         }
148
149                         if (!emails.empty ()) {
150                                 ConfirmKDMEmailDialog* d = new ConfirmKDMEmailDialog (this, emails);
151                                 if (d->ShowModal() == wxID_CANCEL) {
152                                         proceed = false;
153                                 }
154                         }
155                 }
156         }
157
158         if (!proceed) {
159                 return make_pair (shared_ptr<Job>(), 0);
160         }
161
162         Config::instance()->set_dkdm_filename_format (_filename_format->get());
163
164         int written = 0;
165         shared_ptr<Job> job;
166
167         try {
168                 written = write_files (
169                         kdms,
170                         directory(),
171                         _filename_format->get(),
172                         confirm_overwrite
173                         );
174
175                 if (_email->GetValue ()) {
176                         job.reset (
177                                 new SendKDMEmailJob (
178                                         kdms,
179                                         _filename_format->get(),
180                                         _filename_format->get(),
181                                         name,
182                                         {}
183                                         )
184                                 );
185                 }
186
187         } catch (dcp::NotEncryptedError& e) {
188                 error_dialog (this, _("CPL's content is not encrypted."));
189         } catch (exception& e) {
190                 error_dialog (this, std_to_wx(e.what()));
191         } catch (...) {
192                 error_dialog (this, _("An unknown exception occurred."));
193         }
194
195         return make_pair (job, written);
196 }
197
198
199 boost::filesystem::path
200 DKDMOutputPanel::directory () const
201 {
202         return wx_to_std (_folder->GetPath ());
203 }