2 Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 #include "wx/wx_util.h"
22 #include "wx/about_dialog.h"
23 #include "wx/wx_signal_manager.h"
24 #include "wx/job_manager_view.h"
25 #include "wx/full_config_dialog.h"
26 #include "wx/servers_list_dialog.h"
27 #include "lib/version.h"
28 #include "lib/compose.hpp"
29 #include "lib/config.h"
32 #include "lib/job_manager.h"
33 #include "lib/dcpomatic_socket.h"
34 #include <wx/aboutdlg.h>
35 #include <wx/stdpaths.h>
36 #include <wx/cmdline.h>
37 #include <wx/splash.h>
38 #include <wx/preferences.h>
40 #include <boost/foreach.hpp>
47 using boost::shared_ptr;
49 using boost::scoped_array;
51 static list<boost::filesystem::path> films_to_load;
55 ID_tools_encoding_servers,
60 setup_menu (wxMenuBar* m)
62 wxMenu* file = new wxMenu;
63 file->Append (ID_file_add_film, _("&Add Film...\tCtrl-A"));
65 file->Append (wxID_EXIT, _("&Exit"));
67 file->Append (wxID_EXIT, _("&Quit"));
71 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
73 wxMenu* edit = new wxMenu;
74 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
77 wxMenu* tools = new wxMenu;
78 tools->Append (ID_tools_encoding_servers, _("Encoding servers..."));
80 wxMenu* help = new wxMenu;
81 help->Append (ID_help_about, _("About"));
83 m->Append (file, _("&File"));
85 m->Append (edit, _("&Edit"));
87 m->Append (tools, _("&Tools"));
88 m->Append (help, _("&Help"));
91 class DOMFrame : public wxFrame
94 DOMFrame (wxString const & title)
95 : wxFrame (NULL, -1, title)
96 , _sizer (new wxBoxSizer (wxVERTICAL))
98 , _servers_list_dialog (0)
100 wxMenuBar* bar = new wxMenuBar;
104 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_film, this), ID_file_add_film);
105 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_quit, this), wxID_EXIT);
106 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
107 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_encoding_servers, this), ID_tools_encoding_servers);
108 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), ID_help_about);
110 wxPanel* panel = new wxPanel (this);
111 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
112 s->Add (panel, 1, wxEXPAND);
115 JobManagerView* job_manager_view = new JobManagerView (panel, true);
116 _sizer->Add (job_manager_view, 1, wxALL | wxEXPAND, 6);
118 wxSizer* buttons = new wxBoxSizer (wxHORIZONTAL);
119 wxButton* add = new wxButton (panel, wxID_ANY, _("Add Film..."));
120 add->Bind (wxEVT_BUTTON, boost::bind (&DOMFrame::add_film, this));
121 buttons->Add (add, 1, wxALL, 6);
123 _sizer->Add (buttons, 0, wxALL, 6);
125 panel->SetSizer (_sizer);
127 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&DOMFrame::close, this, _1));
128 Bind (wxEVT_SIZE, boost::bind (&DOMFrame::sized, this, _1));
131 void start_job (boost::filesystem::path path)
134 shared_ptr<Film> film (new Film (path));
135 film->read_metadata ();
137 } catch (std::exception& e) {
138 wxString p = std_to_wx (path.string ());
139 wxCharBuffer b = p.ToUTF8 ();
140 error_dialog (this, wxString::Format (_("Could not open film at %s"), p.data()), std_to_wx(e.what()));
145 void sized (wxSizeEvent& ev)
153 if (!JobManager::instance()->work_to_do ()) {
157 wxMessageDialog* d = new wxMessageDialog (
159 _("There are unfinished jobs; are you sure you want to quit?"),
160 _("Unfinished jobs"),
161 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
164 bool const r = d->ShowModal() == wxID_YES;
169 void close (wxCloseEvent& ev)
171 if (!should_close ()) {
179 void file_add_film ()
186 if (should_close ()) {
191 void edit_preferences ()
193 if (!_config_dialog) {
194 _config_dialog = create_full_config_dialog ();
196 _config_dialog->Show (this);
199 void tools_encoding_servers ()
201 if (!_servers_list_dialog) {
202 _servers_list_dialog = new ServersListDialog (this);
205 _servers_list_dialog->Show ();
210 AboutDialog* d = new AboutDialog (this);
217 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
219 c->SetPath (std_to_wx (_last_parent.get().string ()));
225 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
226 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
233 start_job (wx_to_std (c->GetPath ()));
236 _last_parent = boost::filesystem::path (wx_to_std (c->GetPath ())).parent_path ();
241 boost::optional<boost::filesystem::path> _last_parent;
243 wxPreferencesEditor* _config_dialog;
244 ServersListDialog* _servers_list_dialog;
247 static const wxCmdLineEntryDesc command_line_description[] = {
248 { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
249 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
252 class JobServer : public Server
255 JobServer (DOMFrame* frame)
256 : Server (BATCH_JOB_PORT)
260 void handle (shared_ptr<Socket> socket)
263 int const length = socket->read_uint32 ();
264 scoped_array<char> buffer (new char[length]);
265 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
266 string s (buffer.get());
267 _frame->start_job (s);
268 socket->write (reinterpret_cast<uint8_t const *> ("OK"), 3);
278 class App : public wxApp
282 SetAppName (_("DCP-o-matic Batch Converter"));
283 is_batch_converter = true;
285 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
286 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
288 wxSplashScreen* splash = maybe_show_splash ();
290 if (!wxApp::OnInit()) {
294 #ifdef DCPOMATIC_LINUX
295 unsetenv ("UBUNTU_MENUPROXY");
298 dcpomatic_setup_path_encoding ();
300 /* Enable i18n; this will create a Config object
301 to look for a force-configured language. This Config
302 object will be wrong, however, because dcpomatic_setup
303 hasn't yet been called and there aren't any filters etc.
306 dcpomatic_setup_i18n ();
308 /* Set things up, including filters etc.
309 which will now be internationalised correctly.
313 /* Force the configuration to be re-loaded correctly next
318 _frame = new DOMFrame (_("DCP-o-matic Batch Converter"));
319 SetTopWindow (_frame);
326 JobServer* server = new JobServer (_frame);
327 new thread (boost::bind (&JobServer::run, server));
329 signal_manager = new wxSignalManager (this);
330 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
332 shared_ptr<Film> film;
333 BOOST_FOREACH (boost::filesystem::path i, films_to_load) {
334 if (boost::filesystem::is_directory (i)) {
336 film.reset (new Film (i));
337 film->read_metadata ();
339 } catch (exception& e) {
342 std_to_wx (String::compose (wx_to_std (_("Could not load film %1")), i.string())),
354 signal_manager->ui_idle ();
357 void OnInitCmdLine (wxCmdLineParser& parser)
359 parser.SetDesc (command_line_description);
360 parser.SetSwitchChars (wxT ("-"));
363 bool OnCmdLineParsed (wxCmdLineParser& parser)
365 for (size_t i = 0; i < parser.GetParamCount(); ++i) {
366 films_to_load.push_back (wx_to_std (parser.GetParam(i)));
372 void config_failed_to_load ()
374 message_dialog (_frame, _("The existing configuration failed to load. Default values will be used instead. These may take a short time to create."));
377 void config_warning (string m)
379 message_dialog (_frame, std_to_wx (m));