Call it DCP-o-matic Disk Writer for now.
[dcpomatic.git] / src / tools / dcpomatic_dist.cc
1 /*
2     Copyright (C) 2019-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 "wx/wx_signal_manager.h"
22 #include "wx/wx_util.h"
23 #include "wx/job_manager_view.h"
24 #include "wx/drive_wipe_warning_dialog.h"
25 #include "lib/file_log.h"
26 #include "lib/dcpomatic_log.h"
27 #include "lib/util.h"
28 #include "lib/config.h"
29 #include "lib/signal_manager.h"
30 #include "lib/cross.h"
31 #include "lib/copy_to_drive_job.h"
32 #include "lib/job_manager.h"
33 #include <wx/wx.h>
34 #include <boost/process.hpp>
35 #ifdef __WXOSX__
36 #include <ApplicationServices/ApplicationServices.h>
37 #endif
38
39 using std::string;
40 using std::exception;
41 using std::cout;
42 using std::cerr;
43 using boost::shared_ptr;
44
45 class DOMFrame : public wxFrame
46 {
47 public:
48         explicit DOMFrame (wxString const & title)
49                 : wxFrame (0, -1, title)
50                 , _sizer (new wxBoxSizer(wxVERTICAL))
51         {
52                 /* Use a panel as the only child of the Frame so that we avoid
53                    the dark-grey background on Windows.
54                 */
55                 wxPanel* overall_panel = new wxPanel (this);
56                 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
57                 s->Add (overall_panel, 1, wxEXPAND);
58                 SetSizer (s);
59
60                 wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
61
62                 int r = 0;
63                 add_label_to_sizer (grid, overall_panel, _("DCP"), true, wxGBPosition(r, 0));
64                 wxBoxSizer* dcp_name_sizer = new wxBoxSizer (wxHORIZONTAL);
65                 _dcp_name = new wxStaticText (overall_panel, wxID_ANY, wxEmptyString);
66                 dcp_name_sizer->Add (_dcp_name, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
67                 _dcp_open = new wxButton (overall_panel, wxID_ANY, _("Open..."));
68                 dcp_name_sizer->Add (_dcp_open, 0);
69                 grid->Add (dcp_name_sizer, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
70                 ++r;
71
72                 add_label_to_sizer (grid, overall_panel, _("Drive"), true, wxGBPosition(r, 0));
73                 wxBoxSizer* drive_sizer = new wxBoxSizer (wxHORIZONTAL);
74                 _drive = new wxChoice (overall_panel, wxID_ANY);
75                 drive_sizer->Add (_drive, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
76                 _drive_refresh = new wxButton (overall_panel, wxID_ANY, _("Refresh"));
77                 drive_sizer->Add (_drive_refresh, 0);
78                 grid->Add (drive_sizer, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
79                 ++r;
80
81                 _jobs = new JobManagerView (overall_panel, false);
82                 grid->Add (_jobs, wxGBPosition(r, 0), wxGBSpan(6, 2), wxEXPAND);
83                 r += 6;
84
85                 _copy = new wxButton (overall_panel, wxID_ANY, _("Copy DCP"));
86                 grid->Add (_copy, wxGBPosition(r, 0), wxGBSpan(1, 2), wxEXPAND);
87                 ++r;
88
89                 grid->AddGrowableCol (1);
90
91                 _dcp_open->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::open, this));
92                 _copy->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::copy, this));
93                 _drive->Bind (wxEVT_CHOICE, boost::bind(&DOMFrame::setup_sensitivity, this));
94                 _drive_refresh->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::drive_refresh, this));
95
96                 _sizer->Add (grid, 1, wxALL | wxEXPAND, DCPOMATIC_DIALOG_BORDER);
97                 overall_panel->SetSizer (_sizer);
98                 Fit ();
99                 SetSize (512, GetSize().GetHeight() + 32);
100
101                 /* XXX: this is a hack, but I expect we'll need logs and I'm not sure if there's
102                  * a better place to put them.
103                  */
104                 dcpomatic_log.reset(new FileLog(config_path() / "dist.log"));
105                 dcpomatic_log->set_types (dcpomatic_log->types() | LogEntry::TYPE_DIST);
106                 LOG_DIST_NC("dcpomatic_dist started");
107
108                 drive_refresh ();
109
110                 Bind (wxEVT_SIZE, boost::bind (&DOMFrame::sized, this, _1));
111
112                 _writer = new boost::process::child ("dcpomatic2_dist_writer", boost::process::std_in < _to_writer, boost::process::std_out > _from_writer);
113         }
114
115 private:
116         void sized (wxSizeEvent& ev)
117         {
118                 _sizer->Layout ();
119                 ev.Skip ();
120         }
121
122         void open ()
123         {
124                 wxDirDialog* d = new wxDirDialog (this, _("Choose a DCP folder"), wxT(""), wxDD_DIR_MUST_EXIST);
125                 int r = d->ShowModal ();
126                 boost::filesystem::path const path (wx_to_std(d->GetPath()));
127                 d->Destroy ();
128
129                 if (r != wxID_OK) {
130                         return;
131                 }
132
133                 _dcp_path = path;
134                 _dcp_name->SetLabel (std_to_wx(_dcp_path->filename().string()));
135                 setup_sensitivity ();
136         }
137
138         void copy ()
139         {
140                 DCPOMATIC_ASSERT (_drive->GetSelection() != wxNOT_FOUND);
141                 DCPOMATIC_ASSERT (static_cast<bool>(_dcp_path));
142                 DriveWipeWarningDialog* d = new DriveWipeWarningDialog (this, _drive->GetString(_drive->GetSelection()));
143                 int const r = d->ShowModal ();
144                 bool ok = r == wxID_OK && d->confirmed();
145                 d->Destroy ();
146
147                 if (!ok) {
148                         return;
149                 }
150
151                 JobManager::instance()->add(shared_ptr<Job>(new CopyToDriveJob(*_dcp_path, _drives[_drive->GetSelection()], _to_writer, _from_writer)));
152         }
153
154         void drive_refresh ()
155         {
156                 int const sel = _drive->GetSelection ();
157                 wxString current;
158                 if (sel != wxNOT_FOUND) {
159                         current = _drive->GetString (sel);
160                 }
161                 _drive->Clear ();
162                 int re_select = wxNOT_FOUND;
163                 int j = 0;
164                 _drives.clear ();
165                 BOOST_FOREACH (Drive i, get_drives()) {
166                         if (!i.mounted()) {
167                                 _drives.push_back (i);
168                         }
169                 }
170                 BOOST_FOREACH (Drive i, _drives) {
171                         wxString const s = std_to_wx(i.description());
172                         if (s == current) {
173                                 re_select = j;
174                         }
175                         _drive->Append(s);
176                         ++j;
177                 }
178                 _drive->SetSelection (re_select);
179                 setup_sensitivity ();
180         }
181
182         void setup_sensitivity ()
183         {
184                 _copy->Enable (static_cast<bool>(_dcp_path) && _drive->GetSelection() != wxNOT_FOUND);
185         }
186
187         wxStaticText* _dcp_name;
188         wxButton* _dcp_open;
189         wxChoice* _drive;
190         wxButton* _drive_refresh;
191         wxButton* _copy;
192         JobManagerView* _jobs;
193         boost::optional<boost::filesystem::path> _dcp_path;
194         std::vector<Drive> _drives;
195         boost::process::child* _writer;
196         boost::process::opstream _to_writer;
197         boost::process::ipstream _from_writer;
198         wxSizer* _sizer;
199 };
200
201 class App : public wxApp
202 {
203 public:
204         App ()
205                 : _frame (0)
206         {}
207
208         bool OnInit ()
209         {
210                 try {
211                         Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
212                         Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
213
214                         SetAppName (_("DCP-o-matic Disk Writer"));
215
216                         if (!wxApp::OnInit()) {
217                                 return false;
218                         }
219
220 #ifdef DCPOMATIC_LINUX
221                         unsetenv ("UBUNTU_MENUPROXY");
222 #endif
223
224 #ifdef __WXOSX__
225                         ProcessSerialNumber serial;
226                         GetCurrentProcess (&serial);
227                         TransformProcessType (&serial, kProcessTransformToForegroundApplication);
228 #endif
229
230                         dcpomatic_setup_path_encoding ();
231
232                         /* Enable i18n; this will create a Config object
233                            to look for a force-configured language.  This Config
234                            object will be wrong, however, because dcpomatic_setup
235                            hasn't yet been called and there aren't any filters etc.
236                            set up yet.
237                         */
238                         dcpomatic_setup_i18n ();
239
240                         /* Set things up, including filters etc.
241                            which will now be internationalised correctly.
242                         */
243                         dcpomatic_setup ();
244
245                         /* Force the configuration to be re-loaded correctly next
246                            time it is needed.
247                         */
248                         Config::drop ();
249
250                         _frame = new DOMFrame (_("DCP-o-matic Disk Writer"));
251                         SetTopWindow (_frame);
252
253                         _frame->Show ();
254
255                         signal_manager = new wxSignalManager (this);
256                         Bind (wxEVT_IDLE, boost::bind (&App::idle, this, _1));
257                 }
258                 catch (exception& e)
259                 {
260                         error_dialog (0, wxString::Format ("DCP-o-matic could not start."), std_to_wx(e.what()));
261                 }
262
263                 return true;
264         }
265
266         void config_failed_to_load ()
267         {
268                 message_dialog (_frame, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
269         }
270
271         void config_warning (string m)
272         {
273                 message_dialog (_frame, std_to_wx(m));
274         }
275
276         void idle (wxIdleEvent& ev)
277         {
278                 signal_manager->ui_idle ();
279                 ev.Skip ();
280         }
281
282         DOMFrame* _frame;
283 };
284
285 IMPLEMENT_APP (App)