2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "wx/wx_util.h"
21 #include "wx/about_dialog.h"
22 #include "wx/wx_signal_manager.h"
23 #include "wx/job_manager_view.h"
24 #include "wx/config_dialog.h"
25 #include "wx/servers_list_dialog.h"
26 #include "lib/version.h"
27 #include "lib/compose.hpp"
28 #include "lib/config.h"
31 #include "lib/job_manager.h"
32 #include "lib/dcpomatic_socket.h"
33 #include <wx/aboutdlg.h>
34 #include <wx/stdpaths.h>
35 #include <wx/cmdline.h>
36 #include <wx/preferences.h>
43 using boost::shared_ptr;
45 using boost::scoped_array;
47 static std::string film_to_load;
51 ID_tools_encoding_servers,
56 setup_menu (wxMenuBar* m)
58 wxMenu* file = new wxMenu;
59 file->Append (ID_file_add_film, _("&Add Film..."));
61 file->Append (wxID_EXIT, _("&Exit"));
63 file->Append (wxID_EXIT, _("&Quit"));
67 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
69 wxMenu* edit = new wxMenu;
70 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
73 wxMenu* tools = new wxMenu;
74 tools->Append (ID_tools_encoding_servers, _("Encoding servers..."));
76 wxMenu* help = new wxMenu;
77 help->Append (ID_help_about, _("About"));
79 m->Append (file, _("&File"));
81 m->Append (edit, _("&Edit"));
83 m->Append (tools, _("&Tools"));
84 m->Append (help, _("&Help"));
87 class DOMFrame : public wxFrame
90 DOMFrame (wxString const & title)
91 : wxFrame (NULL, -1, title)
92 , _sizer (new wxBoxSizer (wxVERTICAL))
94 , _servers_list_dialog (0)
96 wxMenuBar* bar = new wxMenuBar;
100 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::file_add_film, this), ID_file_add_film);
101 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::file_quit, this), wxID_EXIT);
102 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
103 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::tools_encoding_servers, this), ID_tools_encoding_servers);
104 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::help_about, this), ID_help_about);
106 wxPanel* panel = new wxPanel (this);
107 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
108 s->Add (panel, 1, wxEXPAND);
111 JobManagerView* job_manager_view = new JobManagerView (panel);
112 _sizer->Add (job_manager_view, 1, wxALL | wxEXPAND, 6);
114 wxSizer* buttons = new wxBoxSizer (wxHORIZONTAL);
115 wxButton* add = new wxButton (panel, wxID_ANY, _("Add Film..."));
116 add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&DOMFrame::add_film, this));
117 buttons->Add (add, 1, wxALL, 6);
119 _sizer->Add (buttons, 0, wxALL, 6);
121 panel->SetSizer (_sizer);
123 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&DOMFrame::close, this, _1));
124 Bind (wxEVT_SIZE, boost::bind (&DOMFrame::sized, this, _1));
127 void start_job (boost::filesystem::path path)
130 shared_ptr<Film> film (new Film (path));
131 film->read_metadata ();
133 } catch (std::exception& e) {
134 wxString p = std_to_wx (path.string ());
135 wxCharBuffer b = p.ToUTF8 ();
136 error_dialog (this, wxString::Format (_("Could not open film at %s (%s)"), p.data(), std_to_wx (e.what()).data()));
141 void sized (wxSizeEvent& ev)
149 if (!JobManager::instance()->work_to_do ()) {
153 wxMessageDialog* d = new wxMessageDialog (
155 _("There are unfinished jobs; are you sure you want to quit?"),
156 _("Unfinished jobs"),
157 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
160 bool const r = d->ShowModal() == wxID_YES;
165 void close (wxCloseEvent& ev)
167 if (!should_close ()) {
175 void file_add_film ()
182 if (should_close ()) {
187 void edit_preferences ()
189 if (!_config_dialog) {
190 _config_dialog = create_config_dialog ();
192 _config_dialog->Show (this);
195 void tools_encoding_servers ()
197 if (!_servers_list_dialog) {
198 _servers_list_dialog = new ServersListDialog (this);
201 _servers_list_dialog->Show ();
206 AboutDialog* d = new AboutDialog (this);
213 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
215 c->SetPath (std_to_wx (_last_parent.get().string ()));
221 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
222 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
229 start_job (wx_to_std (c->GetPath ()));
232 _last_parent = boost::filesystem::path (wx_to_std (c->GetPath ())).parent_path ();
237 boost::optional<boost::filesystem::path> _last_parent;
239 wxPreferencesEditor* _config_dialog;
240 ServersListDialog* _servers_list_dialog;
243 static const wxCmdLineEntryDesc command_line_description[] = {
244 { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
245 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
248 class JobServer : public Server
251 JobServer (DOMFrame* frame)
252 : Server (Config::instance()->server_port_base() + 2)
256 void handle (shared_ptr<Socket> socket)
259 int const length = socket->read_uint32 ();
260 cout << "len=" << length << "\n";
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);
274 class App : public wxApp
278 SetAppName (_("DCP-o-matic Batch Converter"));
280 if (!wxApp::OnInit()) {
284 #ifdef DCPOMATIC_LINUX
285 unsetenv ("UBUNTU_MENUPROXY");
288 dcpomatic_setup_path_encoding ();
290 /* Enable i18n; this will create a Config object
291 to look for a force-configured language. This Config
292 object will be wrong, however, because dcpomatic_setup
293 hasn't yet been called and there aren't any filters etc.
296 dcpomatic_setup_i18n ();
298 /* Set things up, including filters etc.
299 which will now be internationalised correctly.
303 /* Force the configuration to be re-loaded correctly next
308 DOMFrame* f = new DOMFrame (_("DCP-o-matic Batch Converter"));
313 JobServer* server = new JobServer (f);
314 new thread (boost::bind (&JobServer::run, server));
316 signal_manager = new wxSignalManager (this);
317 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
319 shared_ptr<Film> film;
320 if (!film_to_load.empty() && boost::filesystem::is_directory (film_to_load)) {
322 film.reset (new Film (film_to_load));
323 film->read_metadata ();
325 } catch (exception& e) {
326 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load film %1 (%2)")), film_to_load, e.what())));
335 signal_manager->ui_idle ();
338 void OnInitCmdLine (wxCmdLineParser& parser)
340 parser.SetDesc (command_line_description);
341 parser.SetSwitchChars (wxT ("-"));
344 bool OnCmdLineParsed (wxCmdLineParser& parser)
346 if (parser.GetParamCount() > 0) {
347 film_to_load = wx_to_std (parser.GetParam(0));