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