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/aboutdlg.h>
21 #include <wx/stdpaths.h>
22 #include <wx/cmdline.h>
24 #include "lib/version.h"
25 #include "lib/compose.hpp"
26 #include "lib/config.h"
29 #include "lib/job_manager.h"
30 #include "wx/wx_util.h"
31 #include "wx/about_dialog.h"
32 #include "wx/wx_signal_manager.h"
33 #include "wx/job_manager_view.h"
36 using boost::shared_ptr;
38 static std::string film_to_load;
47 setup_menu (wxMenuBar* m)
49 wxMenu* file = new wxMenu;
50 file->Append (ID_file_add_film, _("&Add Film..."));
51 file->Append (ID_file_quit, _("&Quit"));
53 wxMenu* help = new wxMenu;
54 help->Append (ID_help_about, _("About"));
56 m->Append (file, _("&File"));
57 m->Append (help, _("&Help"));
60 class DOMFrame : public wxFrame
63 DOMFrame (wxString const & title)
64 : wxFrame (NULL, -1, title)
65 , _sizer (new wxBoxSizer (wxVERTICAL))
67 wxMenuBar* bar = new wxMenuBar;
71 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::file_add_film, this), ID_file_add_film);
72 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::file_quit, this), ID_file_quit);
73 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&DOMFrame::help_about, this), ID_help_about);
75 wxPanel* panel = new wxPanel (this);
76 wxSizer* s = new wxBoxSizer (wxHORIZONTAL);
77 s->Add (panel, 1, wxEXPAND);
80 JobManagerView* job_manager_view = new JobManagerView (panel);
81 _sizer->Add (job_manager_view, 1, wxALL | wxEXPAND, 6);
83 wxSizer* buttons = new wxBoxSizer (wxHORIZONTAL);
84 wxButton* add = new wxButton (panel, wxID_ANY, _("Add Film..."));
85 add->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&DOMFrame::add_film, this));
86 buttons->Add (add, 1, wxALL, 6);
88 _sizer->Add (buttons, 0, wxALL, 6);
90 panel->SetSizer (_sizer);
92 Bind (wxEVT_CLOSE_WINDOW, boost::bind (&DOMFrame::close, this, _1));
93 Bind (wxEVT_SIZE, boost::bind (&DOMFrame::sized, this, _1));
97 void sized (wxSizeEvent& ev)
105 if (!JobManager::instance()->work_to_do ()) {
109 wxMessageDialog* d = new wxMessageDialog (
111 _("There are unfinished jobs; are you sure you want to quit?"),
112 _("Unfinished jobs"),
113 wxYES_NO | wxYES_DEFAULT | wxICON_QUESTION
116 bool const r = d->ShowModal() == wxID_YES;
121 void close (wxCloseEvent& ev)
123 if (!should_close ()) {
131 void file_add_film ()
138 if (should_close ()) {
145 AboutDialog* d = new AboutDialog (this);
152 wxDirDialog* c = new wxDirDialog (this, _("Select film to open"), wxStandardPaths::Get().GetDocumentsDir(), wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST);
154 c->SetPath (std_to_wx (_last_parent.get().string ()));
160 if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
161 error_dialog (this, _("You did not select a folder. Make sure that you select a folder before clicking Open."));
169 shared_ptr<Film> film (new Film (wx_to_std (c->GetPath ())));
170 film->read_metadata ();
172 } catch (std::exception& e) {
173 wxString p = c->GetPath ();
174 wxCharBuffer b = p.ToUTF8 ();
175 error_dialog (this, wxString::Format (_("Could not open film at %s (%s)"), p.data(), std_to_wx (e.what()).data()));
179 _last_parent = boost::filesystem::path (wx_to_std (c->GetPath ())).parent_path ();
184 boost::optional<boost::filesystem::path> _last_parent;
188 static const wxCmdLineEntryDesc command_line_description[] = {
189 { wxCMD_LINE_PARAM, 0, 0, "film to load", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
190 { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
193 class App : public wxApp
197 SetAppName (_("DCP-o-matic Batch Converter"));
199 if (!wxApp::OnInit()) {
203 #ifdef DCPOMATIC_LINUX
204 unsetenv ("UBUNTU_MENUPROXY");
207 dcpomatic_setup_path_encoding ();
209 /* Enable i18n; this will create a Config object
210 to look for a force-configured language. This Config
211 object will be wrong, however, because dcpomatic_setup
212 hasn't yet been called and there aren't any filters etc.
215 dcpomatic_setup_i18n ();
217 /* Set things up, including filters etc.
218 which will now be internationalised correctly.
222 /* Force the configuration to be re-loaded correctly next
227 DOMFrame* f = new DOMFrame (_("DCP-o-matic Batch Converter"));
232 signal_manager = new wxSignalManager (this);
233 this->Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
235 shared_ptr<Film> film;
236 if (!film_to_load.empty() && boost::filesystem::is_directory (film_to_load)) {
238 film.reset (new Film (film_to_load));
239 film->read_metadata ();
241 } catch (exception& e) {
242 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load film %1 (%2)")), film_to_load, e.what())));
251 signal_manager->ui_idle ();
254 void OnInitCmdLine (wxCmdLineParser& parser)
256 parser.SetDesc (command_line_description);
257 parser.SetSwitchChars (wxT ("-"));
260 bool OnCmdLineParsed (wxCmdLineParser& parser)
262 if (parser.GetParamCount() > 0) {
263 film_to_load = wx_to_std (parser.GetParam(0));