2 Copyright (C) 2012-2021 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/>.
22 #include "check_box.h"
23 #include "film_name_location_dialog.h"
25 /* This must come after wx_util.h as it defines DCPOMATIC_USE_OWN_PICKER */
26 #ifdef DCPOMATIC_USE_OWN_PICKER
27 #include "dir_picker_ctrl.h"
29 #include "lib/compose.hpp"
30 #include "lib/config.h"
31 #include <dcp/warnings.h>
32 LIBDCP_DISABLE_WARNINGS
33 #include <wx/stdpaths.h>
34 LIBDCP_ENABLE_WARNINGS
35 #include <boost/filesystem.hpp>
40 using boost::optional;
43 optional<boost::filesystem::path> FilmNameLocationDialog::_directory;
46 FilmNameLocationDialog::FilmNameLocationDialog (wxWindow* parent, wxString title, bool offer_templates)
47 : TableDialog (parent, title, 2, 1, true)
49 add (_("Film name"), true);
50 _name = add (new wxTextCtrl (this, wxID_ANY));
52 add (_("Create in folder"), true);
54 #ifdef DCPOMATIC_USE_OWN_PICKER
55 _folder = new DirPickerCtrl (this);
56 _folder->Changed.connect (bind(&FilmNameLocationDialog::folder_changed, this));
58 _folder = new wxDirPickerCtrl (this, wxID_ANY, wxEmptyString, wxDirSelectorPromptStr, wxDefaultPosition, wxSize (300, -1));
59 _folder->Bind (wxEVT_DIRPICKER_CHANGED, bind(&FilmNameLocationDialog::folder_changed, this));
62 auto dir = _directory.get_value_or(Config::instance()->default_directory_or(wx_to_std(wxStandardPaths::Get().GetDocumentsDir())));
63 _folder->SetPath (std_to_wx(dir.string()));
66 if (offer_templates) {
67 _use_template = new CheckBox (this, _("From template"));
69 _template_name = new wxChoice (this, wxID_ANY);
75 if (offer_templates) {
76 _template_name->Enable (false);
78 for (auto i: Config::instance()->templates ()) {
79 _template_name->Append (std_to_wx(i));
82 _use_template->bind(&FilmNameLocationDialog::use_template_clicked, this);
87 _name->Bind (wxEVT_TEXT, bind(&FilmNameLocationDialog::setup_sensitivity, this));
93 FilmNameLocationDialog::setup_sensitivity ()
95 auto ok = dynamic_cast<wxButton *>(FindWindowById(wxID_OK, this));
97 ok->Enable (!_name->GetValue().IsEmpty());
103 FilmNameLocationDialog::use_template_clicked ()
105 _template_name->Enable (_use_template->GetValue());
110 FilmNameLocationDialog::folder_changed ()
112 _directory = wx_to_std (_folder->GetPath());
116 boost::filesystem::path
117 FilmNameLocationDialog::path () const
119 boost::filesystem::path p;
120 p /= wx_to_std (_folder->GetPath());
121 p /= wx_to_std (_name->GetValue());
127 FilmNameLocationDialog::template_name () const
129 if (!_use_template->GetValue() || _template_name->GetSelection() == -1) {
133 return wx_to_std (_template_name->GetString(_template_name->GetSelection()));
137 /** Check the path that is in our controls and offer confirmations or errors as required.
138 * @return true if the path should be used.
141 FilmNameLocationDialog::check_path ()
143 if (boost::filesystem::is_directory(path()) && !boost::filesystem::is_empty(path())) {
144 if (!confirm_dialog (
147 String::compose(wx_to_std(_("The directory %1 already exists and is not empty. "
148 "Are you sure you want to use it?")),
149 path().string().c_str())
154 } else if (boost::filesystem::is_regular_file(path())) {
157 String::compose (wx_to_std(_("%1 already exists as a file, so you cannot use it for a film.")), path().c_str())