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>
39 #include <boost/foreach.hpp>
46 using boost::shared_ptr;
48 using boost::scoped_array;
50 static list<boost::filesystem::path> films_to_load;
54 ID_tools_encoding_servers,
59 setup_menu (wxMenuBar* m)
61 wxMenu* file = new wxMenu;
62 file->Append (ID_file_add_film, _("&Add Film..."));
64 file->Append (wxID_EXIT, _("&Exit"));
66 file->Append (wxID_EXIT, _("&Quit"));
70 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
72 wxMenu* edit = new wxMenu;
73 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
76 wxMenu* tools = new wxMenu;
77 tools->Append (ID_tools_encoding_servers, _("Encoding servers..."));
79 wxMenu* help = new wxMenu;
80 help->Append (ID_help_about, _("About"));
82 m->Append (file, _("&File"));
84 m->Append (edit, _("&Edit"));
86 m->Append (tools, _("&Tools"));
87 m->Append (help, _("&Help"));
90 class DOMFrame : public wxFrame
93 DOMFrame (wxString const & title)
94 : wxFrame (NULL, -1, title)
95 , _sizer (new wxBoxSizer (wxVERTICAL))
97 , _servers_list_dialog (0)
99 wxMenuBar* bar = new wxMenuBar;
103 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_film, this), ID_file_add_film);
104 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_quit, this), wxID_EXIT);
105 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
106 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_encoding_servers, this), ID_tools_encoding_servers);
107 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), ID_help_about);
109 wxPanel* panel = new wxPanel (this);
110 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
111 s->Add (panel, 1, wxEXPAND);
114 JobManagerView* job_manager_view = new JobManagerView (panel, true);
115 _sizer->Add (job_manager_view, 1, wxALL | wxEXPAND, 6);
117 wxSizer* buttons = new wxBoxSizer (wxHORIZONTAL);
118 wxButton* add = new wxButton (panel, wxID_ANY, _("Add Film..."));
119 add->Bind (wxEVT_BUTTON, boost::bind (&DOMFrame::add_film, this));
120 buttons->Add (add, 1, wxALL, 6);
122 _sizer->Add (buttons, 0, wxALL, 6);
124 panel->SetSizer (_sizer);
126 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&DOMFrame::close, this, _1));
127 Bind (wxEVT_SIZE, boost::bind (&DOMFrame::sized, this, _1));
130 void start_job (boost::filesystem::path path)
133 shared_ptr<Film> film (new Film (path));
134 film->read_metadata ();
136 } catch (std::exception& e) {
137 wxString p = std_to_wx (path.string ());
138 wxCharBuffer b = p.ToUTF8 ();
139 error_dialog (this, wxString::Format (_("Could not open film at %s (%s)"), p.data(), std_to_wx (e.what()).data()));
144 void sized (wxSizeEvent& ev)
152 if (!JobManager::instance()->work_to_do ()) {
156 wxMessageDialog* d = new wxMessageDialog (
158 _("There are unfinished jobs; are you sure you want to quit?"),
159 _("Unfinished jobs"),
160 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
163 bool const r = d->ShowModal() == wxID_YES;
168 void close (wxCloseEvent& ev)
170 if (!should_close ()) {
178 void file_add_film ()
185 if (should_close ()) {
190 void edit_preferences ()
192 if (!_config_dialog) {
193 _config_dialog = create_config_dialog ();
195 _config_dialog->Show (this);
198 void tools_encoding_servers ()
200 if (!_servers_list_dialog) {
201 _servers_list_dialog = new ServersListDialog (this);
204 _servers_list_dialog->Show ();
209 AboutDialog* d = new AboutDialog (this);
216 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
218 c->SetPath (std_to_wx (_last_parent.get().string ()));
224 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
225 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
232 start_job (wx_to_std (c->GetPath ()));
235 _last_parent = boost::filesystem::path (wx_to_std (c->GetPath ())).parent_path ();
240 boost::optional<boost::filesystem::path> _last_parent;
242 wxPreferencesEditor* _config_dialog;
243 ServersListDialog* _servers_list_dialog;
246 static const wxCmdLineEntryDesc command_line_description[] = {
247 { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
248 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
251 class JobServer : public Server
254 JobServer (DOMFrame* frame)
255 : Server (Config::instance()->server_port_base() + 2)
259 void handle (shared_ptr<Socket> socket)
262 int const length = socket->read_uint32 ();
263 scoped_array<char> buffer (new char[length]);
264 socket->read (reinterpret_cast<uint8_t*> (buffer.get()), length);
265 string s (buffer.get());
266 _frame->start_job (s);
267 socket->write (reinterpret_cast<uint8_t const *> ("OK"), 3);
277 class App : public wxApp
281 SetAppName (_("DCP-o-matic Batch Converter"));
283 if (!wxApp::OnInit()) {
287 #ifdef DCPOMATIC_LINUX
288 unsetenv ("UBUNTU_MENUPROXY");
291 dcpomatic_setup_path_encoding ();
293 /* Enable i18n; this will create a Config object
294 to look for a force-configured language. This Config
295 object will be wrong, however, because dcpomatic_setup
296 hasn't yet been called and there aren't any filters etc.
299 dcpomatic_setup_i18n ();
301 /* Set things up, including filters etc.
302 which will now be internationalised correctly.
306 /* Force the configuration to be re-loaded correctly next
311 DOMFrame* f = new DOMFrame (_("DCP-o-matic Batch Converter"));
316 JobServer* server = new JobServer (f);
317 new thread (boost::bind (&JobServer::run, server));
319 signal_manager = new wxSignalManager (this);
320 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
322 shared_ptr<Film> film;
323 BOOST_FOREACH (boost::filesystem::path i, films_to_load) {
324 if (boost::filesystem::is_directory (i)) {
326 film.reset (new Film (i));
327 film->read_metadata ();
329 } catch (exception& e) {
333 wx_to_std (_("Could not load film %1 (%2)")), i.string(),
347 signal_manager->ui_idle ();
350 void OnInitCmdLine (wxCmdLineParser& parser)
352 parser.SetDesc (command_line_description);
353 parser.SetSwitchChars (wxT ("-"));
356 bool OnCmdLineParsed (wxCmdLineParser& parser)
358 for (size_t i = 0; i < parser.GetParamCount(); ++i) {
359 films_to_load.push_back (wx_to_std (parser.GetParam(i)));