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