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