Updated zh_CN translation from Akivili Collindort.
[dcpomatic.git] / src / tools / dcpomatic_batch.cc
1 /*
2     Copyright (C) 2013-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/about_dialog.h"
23 #include "wx/dcpomatic_button.h"
24 #include "wx/full_config_dialog.h"
25 #include "wx/job_manager_view.h"
26 #include "wx/servers_list_dialog.h"
27 #include "wx/wx_ptr.h"
28 #include "wx/wx_signal_manager.h"
29 #include "wx/wx_util.h"
30 #include "lib/compose.hpp"
31 #include "lib/config.h"
32 #include "lib/dcpomatic_socket.h"
33 #include "lib/film.h"
34 #include "lib/job.h"
35 #include "lib/job_manager.h"
36 #include "lib/make_dcp.h"
37 #include "lib/transcode_job.h"
38 #include "lib/util.h"
39 #include "lib/version.h"
40 #include <dcp/warnings.h>
41 LIBDCP_DISABLE_WARNINGS
42 #include <wx/aboutdlg.h>
43 #include <wx/cmdline.h>
44 #include <wx/dnd.h>
45 #include <wx/preferences.h>
46 #include <wx/splash.h>
47 #include <wx/stdpaths.h>
48 #include <wx/wx.h>
49 LIBDCP_ENABLE_WARNINGS
50 #include <iostream>
51 #include <set>
52
53
54 using std::cout;
55 using std::dynamic_pointer_cast;
56 using std::exception;
57 using std::list;
58 using std::make_shared;
59 using std::set;
60 using std::shared_ptr;
61 using std::string;
62 using boost::scoped_array;
63 using boost::thread;
64 #if BOOST_VERSION >= 106100
65 using namespace boost::placeholders;
66 #endif
67
68
69 static list<boost::filesystem::path> films_to_load;
70
71
72 enum {
73         ID_file_add_film = 1,
74         ID_tools_encoding_servers,
75         ID_help_about
76 };
77
78
79 void
80 setup_menu (wxMenuBar* m)
81 {
82         auto file = new wxMenu;
83         file->Append (ID_file_add_film, _("&Add Film...\tCtrl-A"));
84 #ifdef DCPOMATIC_OSX
85         file->Append (wxID_EXIT, _("&Exit"));
86 #else
87         file->Append (wxID_EXIT, _("&Quit"));
88 #endif
89
90 #ifdef DCPOMATIC_OSX
91         file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
92 #else
93         auto edit = new wxMenu;
94         edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
95 #endif
96
97         auto tools = new wxMenu;
98         tools->Append (ID_tools_encoding_servers, _("Encoding servers..."));
99
100         auto help = new wxMenu;
101         help->Append (ID_help_about, _("About"));
102
103         m->Append (file, _("&File"));
104 #ifndef DCPOMATIC_OSX
105         m->Append (edit, _("&Edit"));
106 #endif
107         m->Append (tools, _("&Tools"));
108         m->Append (help, _("&Help"));
109 }
110
111
112 class DOMFrame : public wxFrame
113 {
114 public:
115         enum class Tool {
116                 ADD,
117                 PAUSE
118         };
119
120         class DCPDropTarget : public wxFileDropTarget
121         {
122         public:
123                 DCPDropTarget(DOMFrame* owner)
124                         : _frame(owner)
125                 {}
126
127                 bool OnDropFiles(wxCoord, wxCoord, wxArrayString const& filenames) override
128                 {
129                         if (filenames.GetCount() == 1) {
130                                 /* Try to load a directory */
131                                 auto path = boost::filesystem::path(wx_to_std(filenames[0]));
132                                 if (boost::filesystem::is_directory(path)) {
133                                         _frame->start_job(wx_to_std(filenames[0]));
134                                         return true;
135                                 }
136                         }
137
138                         return false;
139                 }
140
141         private:
142                 DOMFrame* _frame;
143         };
144
145         explicit DOMFrame (wxString const & title)
146                 : wxFrame (nullptr, -1, title)
147                 , _sizer (new wxBoxSizer(wxVERTICAL))
148         {
149                 auto bar = new wxMenuBar;
150                 setup_menu (bar);
151                 SetMenuBar (bar);
152
153                 Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this, _1));
154
155                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_film, this),    ID_file_add_film);
156                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_quit, this),        wxID_EXIT);
157                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
158                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_encoding_servers, this), ID_tools_encoding_servers);
159                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this),       ID_help_about);
160
161                 auto panel = new wxPanel (this);
162                 auto s = new wxBoxSizer (wxHORIZONTAL);
163                 s->Add (panel, 1, wxEXPAND);
164                 SetSizer (s);
165
166                 wxBitmap add(icon_path("add"), wxBITMAP_TYPE_PNG);
167                 wxBitmap pause(icon_path("pause"), wxBITMAP_TYPE_PNG);
168
169                 auto toolbar = new wxToolBar(panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL);
170                 toolbar->SetMargins(4, 4);
171                 toolbar->SetToolBitmapSize(wxSize(32, 32));
172                 toolbar->AddTool(static_cast<int>(Tool::ADD), _("Add film"), add, _("Add film for conversion"));
173                 toolbar->AddCheckTool(static_cast<int>(Tool::PAUSE), _("Pause/resume"), pause, wxNullBitmap, _("Pause or resume conversion"));
174                 toolbar->Realize();
175                 _sizer->Add(toolbar, 0, wxALL, 6);
176
177                 toolbar->Bind(wxEVT_TOOL, bind(&DOMFrame::tool_clicked, this, _1));
178
179                 auto job_manager_view = new JobManagerView (panel, true);
180                 _sizer->Add (job_manager_view, 1, wxALL | wxEXPAND, 6);
181
182                 panel->SetSizer (_sizer);
183
184                 Bind (wxEVT_CLOSE_WINDOW, boost::bind(&DOMFrame::close, this, _1));
185                 Bind (wxEVT_SIZE, boost::bind(&DOMFrame::sized, this, _1));
186
187                 SetDropTarget(new DCPDropTarget(this));
188         }
189
190         void tool_clicked(wxCommandEvent& ev)
191         {
192                 switch (static_cast<Tool>(ev.GetId())) {
193                 case Tool::ADD:
194                         add_film();
195                         break;
196                 case Tool::PAUSE:
197                 {
198                         auto jm = JobManager::instance();
199                         if (jm->paused()) {
200                                 jm->resume();
201                         } else {
202                                 jm->pause();
203                         }
204                         break;
205                 }
206                 }
207         }
208
209         void start_job (boost::filesystem::path path)
210         {
211                 try {
212                         auto film = make_shared<Film>(path);
213                         film->read_metadata ();
214
215                         double total_required;
216                         double available;
217                         bool can_hard_link;
218
219                         film->should_be_enough_disk_space (total_required, available, can_hard_link);
220
221                         set<shared_ptr<const Film>> films;
222
223                         for (auto i: JobManager::instance()->get()) {
224                                 films.insert (i->film());
225                         }
226
227                         for (auto i: films) {
228                                 double progress = 0;
229                                 for (auto j: JobManager::instance()->get()) {
230                                         if (i == j->film() && dynamic_pointer_cast<TranscodeJob>(j)) {
231                                                 progress = j->progress().get_value_or(0);
232                                         }
233                                 }
234
235                                 double required;
236                                 i->should_be_enough_disk_space (required, available, can_hard_link);
237                                 total_required += (1 - progress) * required;
238                         }
239
240                         if ((total_required - available) > 1) {
241                                 if (!confirm_dialog (
242                                             this,
243                                             wxString::Format(
244                                                     _("The DCPs for this film and the films already in the queue will take up about %.1f GB.  The "
245                                                       "disks that you are using only have %.1f GB available.  Do you want to add this film to the queue anyway?"),
246                                                     total_required, available))) {
247                                         return;
248                                 }
249                         }
250
251                         make_dcp (film, TranscodeJob::ChangedBehaviour::STOP);
252                 } catch (std::exception& e) {
253                         auto p = std_to_wx (path.string ());
254                         auto b = p.ToUTF8 ();
255                         error_dialog (this, wxString::Format(_("Could not open film at %s"), p.data()), std_to_wx(e.what()));
256                 }
257         }
258
259 private:
260         void sized (wxSizeEvent& ev)
261         {
262                 _sizer->Layout ();
263                 ev.Skip ();
264         }
265
266         bool should_close ()
267         {
268                 if (!JobManager::instance()->work_to_do()) {
269                         return true;
270                 }
271
272                 auto d = make_wx<wxMessageDialog>(
273                         nullptr,
274                         _("There are unfinished jobs; are you sure you want to quit?"),
275                         _("Unfinished jobs"),
276                         wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
277                         );
278
279                 return d->ShowModal() == wxID_YES;
280         }
281
282         void close (wxCloseEvent& ev)
283         {
284                 if (!should_close()) {
285                         ev.Veto ();
286                         return;
287                 }
288
289                 ev.Skip ();
290         }
291
292         void file_add_film ()
293         {
294                 add_film ();
295         }
296
297         void file_quit ()
298         {
299                 if (should_close()) {
300                         Close (true);
301                 }
302         }
303
304         void edit_preferences ()
305         {
306                 if (!_config_dialog) {
307                         _config_dialog = create_full_config_dialog ();
308                 }
309                 _config_dialog->Show (this);
310         }
311
312         void tools_encoding_servers ()
313         {
314                 if (!_servers_list_dialog) {
315                         _servers_list_dialog = new ServersListDialog (this);
316                 }
317
318                 _servers_list_dialog->Show ();
319         }
320
321         void help_about ()
322         {
323                 auto d = make_wx<AboutDialog>(this);
324                 d->ShowModal ();
325         }
326
327         void add_film ()
328         {
329                 auto dialog = make_wx<wxDirDialog>(this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
330                 if (_last_parent) {
331                         dialog->SetPath(std_to_wx(_last_parent.get().string()));
332                 }
333
334                 int r;
335                 while (true) {
336                         r = dialog->ShowModal();
337                         if (r == wxID_OK && dialog->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
338                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
339                         } else {
340                                 break;
341                         }
342                 }
343
344                 if (r == wxID_OK) {
345                         start_job(wx_to_std(dialog->GetPath()));
346                 }
347
348                 _last_parent = boost::filesystem::path(wx_to_std(dialog->GetPath())).parent_path();
349         }
350
351         void config_changed (Config::Property what)
352         {
353                 /* Instantly save any config changes when using the DCP-o-matic GUI */
354                 if (what == Config::CINEMAS) {
355                         try {
356                                 Config::instance()->write_cinemas();
357                         } catch (exception& e) {
358                                 error_dialog (
359                                         this,
360                                         wxString::Format(
361                                                 _("Could not write to cinemas file at %s.  Your changes have not been saved."),
362                                                 std_to_wx (Config::instance()->cinemas_file().string()).data()
363                                                 )
364                                         );
365                         }
366                 } else {
367                         try {
368                                 Config::instance()->write_config();
369                         } catch (exception& e) {
370                                 error_dialog (
371                                         this,
372                                         wxString::Format(
373                                                 _("Could not write to config file at %s.  Your changes have not been saved."),
374                                                 std_to_wx (Config::instance()->cinemas_file().string()).data()
375                                                 )
376                                         );
377                         }
378                 }
379         }
380
381         boost::optional<boost::filesystem::path> _last_parent;
382         wxSizer* _sizer;
383         wxPreferencesEditor* _config_dialog = nullptr;
384         ServersListDialog* _servers_list_dialog = nullptr;
385 };
386
387
388 static const wxCmdLineEntryDesc command_line_description[] = {
389         { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
390         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
391 };
392
393
394 class JobServer : public Server, public Signaller
395 {
396 public:
397         JobServer()
398                 : Server (BATCH_JOB_PORT)
399         {}
400
401         void handle (shared_ptr<Socket> socket) override
402         {
403                 try {
404                         int const length = socket->read_uint32 ();
405                         scoped_array<char> buffer(new char[length]);
406                         socket->read (reinterpret_cast<uint8_t*>(buffer.get()), length);
407                         string s (buffer.get());
408                         emit(boost::bind(boost::ref(StartJob), s));
409                         socket->write (reinterpret_cast<uint8_t const *>("OK"), 3);
410                 } catch (...) {
411
412                 }
413         }
414
415         boost::signals2::signal<void(std::string)> StartJob;
416 };
417
418
419 class App : public wxApp
420 {
421         bool OnInit () override
422         {
423                 wxInitAllImageHandlers ();
424
425                 SetAppName (_("DCP-o-matic Batch Converter"));
426                 is_batch_converter = true;
427
428                 Config::FailedToLoad.connect(boost::bind(&App::config_failed_to_load, this, _1));
429                 Config::Warning.connect (boost::bind(&App::config_warning, this, _1));
430
431                 auto splash = maybe_show_splash ();
432
433                 if (!wxApp::OnInit()) {
434                         return false;
435                 }
436
437 #ifdef DCPOMATIC_LINUX
438                 unsetenv ("UBUNTU_MENUPROXY");
439 #endif
440
441                 dcpomatic_setup_path_encoding ();
442
443                 /* Enable i18n; this will create a Config object
444                    to look for a force-configured language.  This Config
445                    object will be wrong, however, because dcpomatic_setup
446                    hasn't yet been called and there aren't any filters etc.
447                    set up yet.
448                 */
449                 dcpomatic_setup_i18n ();
450
451                 /* Set things up, including filters etc.
452                    which will now be internationalised correctly.
453                 */
454                 dcpomatic_setup ();
455
456                 /* Force the configuration to be re-loaded correctly next
457                    time it is needed.
458                 */
459                 Config::drop ();
460
461                 _frame = new DOMFrame (_("DCP-o-matic Batch Converter"));
462                 SetTopWindow (_frame);
463                 _frame->Maximize ();
464                 if (splash) {
465                         splash->Destroy ();
466                 }
467                 _frame->Show ();
468
469                 try {
470                         auto server = new JobServer();
471                         server->StartJob.connect(bind(&DOMFrame::start_job, _frame, _1));
472                         new thread (boost::bind (&JobServer::run, server));
473                 } catch (boost::system::system_error& e) {
474                         error_dialog(_frame, _("Could not listen for new batch jobs.  Perhaps another instance of the DCP-o-matic Batch Converter is running."));
475                 }
476
477                 signal_manager = new wxSignalManager (this);
478                 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
479
480                 shared_ptr<Film> film;
481                 for (auto i: films_to_load) {
482                         if (boost::filesystem::is_directory(i)) {
483                                 try {
484                                         film = make_shared<Film>(i);
485                                         film->read_metadata ();
486                                         make_dcp (film, TranscodeJob::ChangedBehaviour::EXAMINE_THEN_STOP);
487                                 } catch (exception& e) {
488                                         error_dialog (
489                                                 0,
490                                                 std_to_wx(String::compose(wx_to_std(_("Could not load film %1")), i.string())),
491                                                 std_to_wx(e.what())
492                                                 );
493                                 }
494                         }
495                 }
496
497                 return true;
498         }
499
500         void idle ()
501         {
502                 signal_manager->ui_idle ();
503         }
504
505         void OnInitCmdLine (wxCmdLineParser& parser) override
506         {
507                 parser.SetDesc (command_line_description);
508                 parser.SetSwitchChars (wxT ("-"));
509         }
510
511         bool OnCmdLineParsed (wxCmdLineParser& parser) override
512         {
513                 for (size_t i = 0; i < parser.GetParamCount(); ++i) {
514                         films_to_load.push_back (wx_to_std(parser.GetParam(i)));
515                 }
516
517                 return true;
518         }
519
520         void config_failed_to_load(Config::LoadFailure what)
521         {
522                 report_config_load_failure(_frame, what);
523         }
524
525         void config_warning (string m)
526         {
527                 message_dialog (_frame, std_to_wx(m));
528         }
529
530         DOMFrame* _frame;
531 };
532
533
534 IMPLEMENT_APP (App)