2 Copyright (C) 2013-2015 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/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/preferences.h>
44 using boost::shared_ptr;
46 using boost::scoped_array;
48 static boost::optional<boost::filesystem::path> film_to_load;
52 ID_tools_encoding_servers,
57 setup_menu (wxMenuBar* m)
59 wxMenu* file = new wxMenu;
60 file->Append (ID_file_add_film, _("&Add Film..."));
62 file->Append (wxID_EXIT, _("&Exit"));
64 file->Append (wxID_EXIT, _("&Quit"));
68 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
70 wxMenu* edit = new wxMenu;
71 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
74 wxMenu* tools = new wxMenu;
75 tools->Append (ID_tools_encoding_servers, _("Encoding servers..."));
77 wxMenu* help = new wxMenu;
78 help->Append (ID_help_about, _("About"));
80 m->Append (file, _("&File"));
82 m->Append (edit, _("&Edit"));
84 m->Append (tools, _("&Tools"));
85 m->Append (help, _("&Help"));
88 class DOMFrame : public wxFrame
91 DOMFrame (wxString const & title)
92 : wxFrame (NULL, -1, title)
93 , _sizer (new wxBoxSizer (wxVERTICAL))
95 , _servers_list_dialog (0)
97 wxMenuBar* bar = new wxMenuBar;
101 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::file_add_film, this), ID_file_add_film);
102 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::file_quit, this), wxID_EXIT);
103 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
104 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::tools_encoding_servers, this), ID_tools_encoding_servers);
105 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::help_about, this), ID_help_about);
107 wxPanel* panel = new wxPanel (this);
108 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
109 s->Add (panel, 1, wxEXPAND);
112 JobManagerView* job_manager_view = new JobManagerView (panel);
113 _sizer->Add (job_manager_view, 1, wxALL | wxEXPAND, 6);
115 wxSizer* buttons = new wxBoxSizer (wxHORIZONTAL);
116 wxButton* add = new wxButton (panel, wxID_ANY, _("Add Film..."));
117 add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&DOMFrame::add_film, this));
118 buttons->Add (add, 1, wxALL, 6);
120 _sizer->Add (buttons, 0, wxALL, 6);
122 panel->SetSizer (_sizer);
124 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&DOMFrame::close, this, _1));
125 Bind (wxEVT_SIZE, boost::bind (&DOMFrame::sized, this, _1));
128 void start_job (boost::filesystem::path path)
131 shared_ptr<Film> film (new Film (path));
132 film->read_metadata ();
134 } catch (std::exception& e) {
135 wxString p = std_to_wx (path.string ());
136 wxCharBuffer b = p.ToUTF8 ();
137 error_dialog (this, wxString::Format (_("Could not open film at %s (%s)"), p.data(), std_to_wx (e.what()).data()));
142 void sized (wxSizeEvent& ev)
150 if (!JobManager::instance()->work_to_do ()) {
154 wxMessageDialog* d = new wxMessageDialog (
156 _("There are unfinished jobs; are you sure you want to quit?"),
157 _("Unfinished jobs"),
158 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
161 bool const r = d->ShowModal() == wxID_YES;
166 void close (wxCloseEvent& ev)
168 if (!should_close ()) {
176 void file_add_film ()
183 if (should_close ()) {
188 void edit_preferences ()
190 if (!_config_dialog) {
191 _config_dialog = create_config_dialog ();
193 _config_dialog->Show (this);
196 void tools_encoding_servers ()
198 if (!_servers_list_dialog) {
199 _servers_list_dialog = new ServersListDialog (this);
202 _servers_list_dialog->Show ();
207 AboutDialog* d = new AboutDialog (this);
214 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
216 c->SetPath (std_to_wx (_last_parent.get().string ()));
222 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
223 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
230 start_job (wx_to_std (c->GetPath ()));
233 _last_parent = boost::filesystem::path (wx_to_std (c->GetPath ())).parent_path ();
238 boost::optional<boost::filesystem::path> _last_parent;
240 wxPreferencesEditor* _config_dialog;
241 ServersListDialog* _servers_list_dialog;
244 static const wxCmdLineEntryDesc command_line_description[] = {
245 { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
246 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
249 class JobServer : public Server
252 JobServer (DOMFrame* frame)
253 : Server (Config::instance()->server_port_base() + 2)
257 void handle (shared_ptr<Socket> socket)
260 int const length = socket->read_uint32 ();
261 scoped_array<char> buffer (new char[length]);
262 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
263 string s (buffer.get());
264 _frame->start_job (s);
265 socket->write (reinterpret_cast<uint8_t const *> ("OK"), 3);
275 class App : public wxApp
279 SetAppName (_("DCP-o-matic Batch Converter"));
281 if (!wxApp::OnInit()) {
285 #ifdef DCPOMATIC_LINUX
286 unsetenv ("UBUNTU_MENUPROXY");
289 dcpomatic_setup_path_encoding ();
291 /* Enable i18n; this will create a Config object
292 to look for a force-configured language. This Config
293 object will be wrong, however, because dcpomatic_setup
294 hasn't yet been called and there aren't any filters etc.
297 dcpomatic_setup_i18n ();
299 /* Set things up, including filters etc.
300 which will now be internationalised correctly.
304 /* Force the configuration to be re-loaded correctly next
309 DOMFrame* f = new DOMFrame (_("DCP-o-matic Batch Converter"));
314 JobServer* server = new JobServer (f);
315 new thread (boost::bind (&JobServer::run, server));
317 signal_manager = new wxSignalManager (this);
318 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
320 shared_ptr<Film> film;
321 if (film_to_load && boost::filesystem::is_directory (film_to_load.get())) {
323 film.reset (new Film (film_to_load.get()));
324 film->read_metadata ();
326 } catch (exception& e) {
328 0, std_to_wx (String::compose (wx_to_std (_("Could not load film %1 (%2)")), film_to_load->string(), e.what()))
338 signal_manager->ui_idle ();
341 void OnInitCmdLine (wxCmdLineParser& parser)
343 parser.SetDesc (command_line_description);
344 parser.SetSwitchChars (wxT ("-"));
347 bool OnCmdLineParsed (wxCmdLineParser& parser)
349 if (parser.GetParamCount() > 0) {
350 film_to_load = wx_to_std (parser.GetParam(0));