Fix disk build.
[dcpomatic.git] / src / tools / dcpomatic_disk.cc
1 /*
2     Copyright (C) 2019-2021 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 "wx/wx_signal_manager.h"
23 #include "wx/wx_util.h"
24 #include "wx/job_manager_view.h"
25 #include "wx/drive_wipe_warning_dialog.h"
26 #include "wx/try_unmount_dialog.h"
27 #include "wx/message_dialog.h"
28 #include "wx/disk_warning_dialog.h"
29 #include "lib/file_log.h"
30 #include "lib/dcpomatic_log.h"
31 #include "lib/util.h"
32 #include "lib/config.h"
33 #include "lib/signal_manager.h"
34 #include "lib/cross.h"
35 #include "lib/copy_to_drive_job.h"
36 #include "lib/job_manager.h"
37 #include "lib/disk_writer_messages.h"
38 #include "lib/version.h"
39 #include "lib/warnings.h"
40 #include <wx/wx.h>
41 DCPOMATIC_DISABLE_WARNINGS
42 #include <boost/process.hpp>
43 DCPOMATIC_ENABLE_WARNINGS
44 #ifdef DCPOMATIC_WINDOWS
45 #include <boost/process/windows.hpp>
46 #endif
47 #ifdef DCPOMATIC_OSX
48 #include <notify.h>
49 #endif
50
51
52 using std::string;
53 using std::exception;
54 using std::cout;
55 using std::cerr;
56 using std::shared_ptr;
57 using boost::optional;
58 #if BOOST_VERSION >= 106100
59 using namespace boost::placeholders;
60 #endif
61
62
63 #ifdef DCPOMATIC_OSX
64 enum {
65         ID_tools_uninstall = 1,
66 };
67 #endif
68
69
70 class DOMFrame : public wxFrame
71 {
72 public:
73         explicit DOMFrame (wxString const & title)
74                 : wxFrame (nullptr, wxID_ANY, title)
75                 , _nanomsg (true)
76                 , _sizer (new wxBoxSizer(wxVERTICAL))
77         {
78 #ifdef DCPOMATIC_OSX
79                 auto bar = new wxMenuBar;
80                 auto tools = new wxMenu;
81                 tools->Append(ID_tools_uninstall, _("Uninstall..."));
82                 bar->Append(tools, _("Tools"));
83                 SetMenuBar (bar);
84                 Bind (wxEVT_MENU, boost::bind(&DOMFrame::uninstall, this), ID_tools_uninstall);
85 #endif
86
87                 /* Use a panel as the only child of the Frame so that we avoid
88                    the dark-grey background on Windows.
89                 */
90                 auto overall_panel = new wxPanel (this);
91                 auto s = new wxBoxSizer (wxHORIZONTAL);
92                 s->Add (overall_panel, 1, wxEXPAND);
93                 SetSizer (s);
94
95                 auto grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
96
97                 int r = 0;
98                 add_label_to_sizer (grid, overall_panel, _("DCP"), true, wxGBPosition(r, 0));
99                 auto dcp_name_sizer = new wxBoxSizer (wxHORIZONTAL);
100                 _dcp_name = new wxStaticText (overall_panel, wxID_ANY, wxEmptyString);
101                 dcp_name_sizer->Add (_dcp_name, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
102                 _dcp_open = new wxButton (overall_panel, wxID_ANY, _("Open..."));
103                 dcp_name_sizer->Add (_dcp_open, 0);
104                 grid->Add (dcp_name_sizer, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
105                 ++r;
106
107                 add_label_to_sizer (grid, overall_panel, _("Drive"), true, wxGBPosition(r, 0));
108                 auto drive_sizer = new wxBoxSizer (wxHORIZONTAL);
109                 _drive = new wxChoice (overall_panel, wxID_ANY);
110                 drive_sizer->Add (_drive, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
111                 _drive_refresh = new wxButton (overall_panel, wxID_ANY, _("Refresh"));
112                 drive_sizer->Add (_drive_refresh, 0);
113                 grid->Add (drive_sizer, wxGBPosition(r, 1), wxDefaultSpan, wxEXPAND);
114                 ++r;
115
116                 _jobs = new JobManagerView (overall_panel, false);
117                 grid->Add (_jobs, wxGBPosition(r, 0), wxGBSpan(6, 2), wxEXPAND);
118                 r += 6;
119
120                 _copy = new wxButton (overall_panel, wxID_ANY, _("Copy DCP"));
121                 grid->Add (_copy, wxGBPosition(r, 0), wxGBSpan(1, 2), wxEXPAND);
122                 ++r;
123
124                 grid->AddGrowableCol (1);
125
126                 _dcp_open->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::open, this));
127                 _copy->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::copy, this));
128                 _drive->Bind (wxEVT_CHOICE, boost::bind(&DOMFrame::setup_sensitivity, this));
129                 _drive_refresh->Bind (wxEVT_BUTTON, boost::bind(&DOMFrame::drive_refresh, this));
130
131                 _sizer->Add (grid, 1, wxALL | wxEXPAND, DCPOMATIC_DIALOG_BORDER);
132                 overall_panel->SetSizer (_sizer);
133                 Fit ();
134                 SetSize (1024, GetSize().GetHeight() + 32);
135
136                 /* XXX: this is a hack, but I expect we'll need logs and I'm not sure if there's
137                  * a better place to put them.
138                  */
139                 dcpomatic_log.reset(new FileLog(config_path() / "disk.log"));
140                 dcpomatic_log->set_types (dcpomatic_log->types() | LogEntry::TYPE_DISK);
141                 LOG_DISK("dcpomatic_disk %1 started", dcpomatic_git_commit);
142
143                 drive_refresh ();
144
145                 Bind (wxEVT_SIZE, boost::bind(&DOMFrame::sized, this, _1));
146                 Bind (wxEVT_CLOSE_WINDOW, boost::bind(&DOMFrame::close, this, _1));
147
148                 JobManager::instance()->ActiveJobsChanged.connect(boost::bind(&DOMFrame::setup_sensitivity, this));
149
150 #ifdef DCPOMATIC_WINDOWS
151                 /* We must use ::shell here, it seems, to avoid error code 740 (related to privilege escalation) */
152                 LOG_DISK("Starting writer process %1", disk_writer_path().string());
153                 _writer = new boost::process::child (disk_writer_path(), boost::process::shell, boost::process::windows::hide);
154 #endif
155
156 #ifdef DCPOMATIC_LINUX
157                 if (getenv("DCPOMATIC_NO_START_WRITER")) {
158                         LOG_DISK_NC("Not starting writer process as DCPOMATIC_NO_START_WRITER is set");
159                 } else {
160                         LOG_DISK("Starting writer process %1", disk_writer_path().string());
161                         _writer = new boost::process::child (disk_writer_path());
162                 }
163 #endif
164
165 #ifdef DCPOMATIC_OSX
166                 LOG_DISK_NC("Sending notification to writer daemon");
167                 notify_post ("com.dcpomatic.disk.writer.start");
168 #endif
169         }
170
171         ~DOMFrame ()
172         {
173                 _nanomsg.send(DISK_WRITER_QUIT "\n", 2000);
174         }
175
176 private:
177         void sized (wxSizeEvent& ev)
178         {
179                 _sizer->Layout ();
180                 ev.Skip ();
181         }
182
183
184 #ifdef DCPOMATIC_OSX
185         void uninstall()
186         {
187                 system(String::compose("osascript \"%1/uninstall_disk.applescript\"", resources_path().string()).c_str());
188         }
189 #endif
190
191
192         bool should_close ()
193         {
194                 if (!JobManager::instance()->work_to_do()) {
195                         return true;
196                 }
197
198                 auto d = new wxMessageDialog (
199                         0,
200                         _("There are unfinished jobs; are you sure you want to quit?"),
201                         _("Unfinished jobs"),
202                         wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
203                         );
204
205                 bool const r = d->ShowModal() == wxID_YES;
206                 d->Destroy ();
207                 return r;
208         }
209
210
211         void close (wxCloseEvent& ev)
212         {
213                 if (!should_close()) {
214                         ev.Veto ();
215                         return;
216                 }
217
218                 ev.Skip ();
219         }
220
221
222         void open ()
223         {
224                 auto d = new wxDirDialog (this, _("Choose a DCP folder"), wxT(""), wxDD_DIR_MUST_EXIST);
225                 int r = d->ShowModal ();
226                 boost::filesystem::path const path (wx_to_std(d->GetPath()));
227                 d->Destroy ();
228
229                 if (r != wxID_OK) {
230                         return;
231                 }
232
233                 _dcp_path = path;
234                 _dcp_name->SetLabel (std_to_wx(_dcp_path->filename().string()));
235                 setup_sensitivity ();
236         }
237
238         void copy ()
239         {
240                 DCPOMATIC_ASSERT (_drive->GetSelection() != wxNOT_FOUND);
241                 DCPOMATIC_ASSERT (static_cast<bool>(_dcp_path));
242
243                 bool have_writer = true;
244                 if (!_nanomsg.send(DISK_WRITER_PING "\n", 2000)) {
245                         have_writer = false;
246                 } else {
247                         auto reply = _nanomsg.receive (2000);
248                         if (!reply || *reply != DISK_WRITER_PONG) {
249                                 have_writer = false;
250                         }
251                 }
252
253                 if (!have_writer) {
254 #ifdef DCPOMATIC_WINDOWS
255                         auto m = new MessageDialog (
256                                 this,
257                                 _("DCP-o-matic Disk Writer"),
258                                 _("Do you see a 'User Account Control' dialogue asking about dcpomatic2_disk_writer.exe?  If so, click 'Yes', then try again.")
259                                 );
260                         m->ShowModal ();
261                         m->Destroy ();
262                         return;
263 #else
264                         throw CommunicationFailedError ();
265 #endif
266                 }
267
268                 auto const& drive = _drives[_drive->GetSelection()];
269                 if (drive.mounted()) {
270                         auto d = new TryUnmountDialog(this, drive.description());
271                         int const r = d->ShowModal ();
272                         d->Destroy ();
273                         if (r != wxID_OK) {
274                                 return;
275                         }
276
277                         LOG_DISK("Sending unmount request to disk writer for %1", drive.as_xml());
278                         if (!_nanomsg.send(DISK_WRITER_UNMOUNT "\n", 2000)) {
279                                 throw CommunicationFailedError ();
280                         }
281                         if (!_nanomsg.send(drive.as_xml(), 2000)) {
282                                 throw CommunicationFailedError ();
283                         }
284                         auto <string> reply = _nanomsg.receive (2000);
285                         if (!reply || *reply != DISK_WRITER_OK) {
286                                 auto * m = new MessageDialog (
287                                                 this,
288                                                 _("DCP-o-matic Disk Writer"),
289                                                 wxString::Format(_("The drive %s could not be unmounted.\nClose any application that is using it, then try again."), std_to_wx(drive.description()))
290                                                 );
291                                 m->ShowModal ();
292                                 m->Destroy ();
293                                 return;
294                         }
295                 }
296
297
298                 auto * d = new DriveWipeWarningDialog (this, _drive->GetString(_drive->GetSelection()));
299                 int const r = d->ShowModal ();
300                 bool ok = r == wxID_OK && d->confirmed();
301                 d->Destroy ();
302
303                 if (!ok) {
304                         return;
305                 }
306
307                 JobManager::instance()->add(make_shared<CopyToDriveJob>(*_dcp_path, _drives[_drive->GetSelection()], _nanomsg));
308                 setup_sensitivity ();
309         }
310
311         void drive_refresh ()
312         {
313                 int const sel = _drive->GetSelection ();
314                 wxString current;
315                 if (sel != wxNOT_FOUND) {
316                         current = _drive->GetString (sel);
317                 }
318                 _drive->Clear ();
319                 int re_select = wxNOT_FOUND;
320                 int j = 0;
321                 _drives = Drive::get ();
322                 for (auto i: _drives) {
323                         auto const s = std_to_wx(i.description());
324                         if (s == current) {
325                                 re_select = j;
326                         }
327                         _drive->Append(s);
328                         ++j;
329                 }
330                 _drive->SetSelection (re_select);
331                 setup_sensitivity ();
332         }
333
334         void setup_sensitivity ()
335         {
336                 _copy->Enable (static_cast<bool>(_dcp_path) && _drive->GetSelection() != wxNOT_FOUND && !JobManager::instance()->work_to_do());
337         }
338
339         wxStaticText* _dcp_name;
340         wxButton* _dcp_open;
341         wxChoice* _drive;
342         wxButton* _drive_refresh;
343         wxButton* _copy;
344         JobManagerView* _jobs;
345         boost::optional<boost::filesystem::path> _dcp_path;
346         std::vector<Drive> _drives;
347 #ifndef DCPOMATIC_OSX
348         boost::process::child* _writer;
349 #endif
350         Nanomsg _nanomsg;
351         wxSizer* _sizer;
352 };
353
354
355 class App : public wxApp
356 {
357 public:
358         App ()
359                 : _frame (0)
360         {}
361
362         bool OnInit ()
363         {
364                 try {
365                         Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
366                         Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
367
368                         SetAppName (_("DCP-o-matic Disk Writer"));
369
370                         if (!wxApp::OnInit()) {
371                                 return false;
372                         }
373
374 #ifdef DCPOMATIC_LINUX
375                         unsetenv ("UBUNTU_MENUPROXY");
376 #endif
377
378 #ifdef DCPOMATIC_OSX
379                         dcpomatic_sleep_seconds (1);
380                         make_foreground_application ();
381 #endif
382
383                         dcpomatic_setup_path_encoding ();
384
385                         /* Enable i18n; this will create a Config object
386                            to look for a force-configured language.  This Config
387                            object will be wrong, however, because dcpomatic_setup
388                            hasn't yet been called and there aren't any filters etc.
389                            set up yet.
390                         */
391                         dcpomatic_setup_i18n ();
392
393                         /* Set things up, including filters etc.
394                            which will now be internationalised correctly.
395                         */
396                         dcpomatic_setup ();
397
398                         /* Force the configuration to be re-loaded correctly next
399                            time it is needed.
400                         */
401                         Config::drop ();
402
403                         auto warning = new DiskWarningDialog ();
404                         warning->ShowModal ();
405                         if (!warning->confirmed()) {
406                                 return false;
407                         }
408                         warning->Destroy ();
409
410                         _frame = new DOMFrame (_("DCP-o-matic Disk Writer"));
411                         SetTopWindow (_frame);
412
413                         _frame->Show ();
414
415                         signal_manager = new wxSignalManager (this);
416                         Bind (wxEVT_IDLE, boost::bind (&App::idle, this, _1));
417                 }
418                 catch (exception& e)
419                 {
420                         error_dialog (0, wxString::Format ("DCP-o-matic could not start."), std_to_wx(e.what()));
421                         return false;
422                 }
423
424                 return true;
425         }
426
427         void config_failed_to_load ()
428         {
429                 message_dialog (_frame, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
430         }
431
432         void config_warning (string m)
433         {
434                 message_dialog (_frame, std_to_wx(m));
435         }
436
437         void idle (wxIdleEvent& ev)
438         {
439                 signal_manager->ui_idle ();
440                 ev.Skip ();
441         }
442
443         void report_exception ()
444         {
445                 try {
446                         throw;
447                 } catch (FileError& e) {
448                         error_dialog (
449                                 0,
450                                 wxString::Format (
451                                         _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
452                                         std_to_wx (e.what()),
453                                         std_to_wx (e.file().string().c_str ())
454                                         )
455                                 );
456                 } catch (exception& e) {
457                         error_dialog (
458                                 0,
459                                 wxString::Format (
460                                         _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
461                                         std_to_wx (e.what ())
462                                         )
463                                 );
464                 } catch (...) {
465                         error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
466                 }
467         }
468
469         bool OnExceptionInMainLoop ()
470         {
471                 report_exception ();
472                 /* This will terminate the program */
473                 return false;
474         }
475
476         void OnUnhandledException ()
477         {
478                 report_exception ();
479         }
480
481         DOMFrame* _frame;
482 };
483
484 IMPLEMENT_APP (App)