Tidying.
[dcpomatic.git] / src / wx / film_name_location_dialog.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21
22 #include "check_box.h"
23 #include "film_name_location_dialog.h"
24 #include "wx_util.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"
28 #endif
29 #include "lib/compose.hpp"
30 #include "lib/config.h"
31 #include <wx/stdpaths.h>
32 #include <boost/filesystem.hpp>
33
34
35 using namespace std;
36 using namespace boost;
37
38
39 optional<filesystem::path> FilmNameLocationDialog::_directory;
40
41
42 FilmNameLocationDialog::FilmNameLocationDialog (wxWindow* parent, wxString title, bool offer_templates)
43         : TableDialog (parent, title, 2, 1, true)
44 {
45         add (_("Film name"), true);
46         _name = add (new wxTextCtrl (this, wxID_ANY));
47
48         add (_("Create in folder"), true);
49
50 #ifdef DCPOMATIC_USE_OWN_PICKER
51         _folder = new DirPickerCtrl (this);
52         _folder->Changed.connect (bind(&FilmNameLocationDialog::folder_changed, this));
53 #else
54         _folder = new wxDirPickerCtrl (this, wxID_ANY, wxEmptyString, wxDirSelectorPromptStr, wxDefaultPosition, wxSize (300, -1));
55         _folder->Bind (wxEVT_DIRPICKER_CHANGED, bind(&FilmNameLocationDialog::folder_changed, this));
56 #endif
57
58         auto dir = _directory.get_value_or(Config::instance()->default_directory_or(wx_to_std(wxStandardPaths::Get().GetDocumentsDir())));
59         _folder->SetPath (std_to_wx(dir.string()));
60         add (_folder);
61
62         if (offer_templates) {
63                 _use_template = new CheckBox (this, _("From template"));
64                 add (_use_template);
65                 _template_name = new wxChoice (this, wxID_ANY);
66                 add (_template_name);
67         }
68
69         _name->SetFocus ();
70
71         if (offer_templates) {
72                 _template_name->Enable (false);
73
74                 for (auto i: Config::instance()->templates ()) {
75                         _template_name->Append (std_to_wx(i));
76                 }
77
78                 _use_template->Bind (wxEVT_CHECKBOX, bind(&FilmNameLocationDialog::use_template_clicked, this));
79         }
80
81         layout ();
82
83         _name->Bind (wxEVT_TEXT, bind(&FilmNameLocationDialog::setup_sensitivity, this));
84         setup_sensitivity ();
85 }
86
87
88 void
89 FilmNameLocationDialog::setup_sensitivity ()
90 {
91         auto ok = dynamic_cast<wxButton *>(FindWindowById(wxID_OK, this));
92         if (ok) {
93                 ok->Enable (!_name->GetValue().IsEmpty());
94         }
95 }
96
97
98 void
99 FilmNameLocationDialog::use_template_clicked ()
100 {
101         _template_name->Enable (_use_template->GetValue());
102 }
103
104
105 void
106 FilmNameLocationDialog::folder_changed ()
107 {
108         _directory = wx_to_std (_folder->GetPath());
109 }
110
111
112 filesystem::path
113 FilmNameLocationDialog::path () const
114 {
115         filesystem::path p;
116         p /= wx_to_std (_folder->GetPath());
117         p /= wx_to_std (_name->GetValue());
118         return p;
119 }
120
121
122 optional<string>
123 FilmNameLocationDialog::template_name () const
124 {
125         if (!_use_template->GetValue() || _template_name->GetSelection() == -1) {
126                 return {};
127         }
128
129         return wx_to_std (_template_name->GetString(_template_name->GetSelection()));
130 }
131
132
133 /** Check the path that is in our controls and offer confirmations or errors as required.
134  *  @return true if the path should be used.
135  */
136 bool
137 FilmNameLocationDialog::check_path ()
138 {
139         if (filesystem::is_directory(path()) && !filesystem::is_empty(path())) {
140                 if (!confirm_dialog (
141                             this,
142                             std_to_wx (
143                                     String::compose(wx_to_std(_("The directory %1 already exists and is not empty.  "
144                                                                   "Are you sure you want to use it?")),
145                                                     path().string().c_str())
146                                     )
147                             )) {
148                         return false;
149                 }
150         } else if (boost::filesystem::is_regular_file(path())) {
151                 error_dialog (
152                         this,
153                         String::compose (wx_to_std(_("%1 already exists as a file, so you cannot use it for a film.")), path().c_str())
154                         );
155                 return false;
156         }
157
158         return true;
159 }
160