Basica save-as (duplicate) (#746).
[dcpomatic.git] / src / wx / film_name_location_dialog.cc
1 /*
2     Copyright (C) 2012-2017 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 #include "wx_util.h"
22 #include "film_name_location_dialog.h"
23 #ifdef DCPOMATIC_USE_OWN_PICKER
24 #include "dir_picker_ctrl.h"
25 #endif
26 #include "lib/config.h"
27 #include "lib/compose.hpp"
28 #include <wx/stdpaths.h>
29 #include <boost/filesystem.hpp>
30 #include <boost/foreach.hpp>
31
32 using namespace std;
33 using namespace boost;
34
35 boost::optional<boost::filesystem::path> FilmNameLocationDialog::_directory;
36
37 FilmNameLocationDialog::FilmNameLocationDialog (wxWindow* parent, wxString title, bool offer_templates)
38         : TableDialog (parent, title, 2, 1, true)
39 {
40         add (_("Film name"), true);
41         _name = add (new wxTextCtrl (this, wxID_ANY));
42
43         add (_("Create in folder"), true);
44
45 #ifdef DCPOMATIC_USE_OWN_PICKER
46         _folder = new DirPickerCtrl (this);
47 #else
48         _folder = new wxDirPickerCtrl (this, wxID_ANY, wxEmptyString, wxDirSelectorPromptStr, wxDefaultPosition, wxSize (300, -1));
49 #endif
50
51         if (!_directory) {
52                 _directory = Config::instance()->default_directory_or (wx_to_std (wxStandardPaths::Get().GetDocumentsDir()));
53         }
54
55         _folder->SetPath (std_to_wx (_directory.get().string()));
56         add (_folder);
57
58         if (offer_templates) {
59                 _use_template = new wxCheckBox (this, wxID_ANY, _("From template"));
60                 add (_use_template);
61                 _template_name = new wxChoice (this, wxID_ANY);
62                 add (_template_name);
63         }
64
65         _name->SetFocus ();
66
67         if (offer_templates) {
68                 _template_name->Enable (false);
69
70                 BOOST_FOREACH (string i, Config::instance()->templates ()) {
71                         _template_name->Append (std_to_wx (i));
72                 }
73
74                 _use_template->Bind (wxEVT_CHECKBOX, bind (&FilmNameLocationDialog::use_template_clicked, this));
75         }
76
77         layout ();
78 }
79
80 void
81 FilmNameLocationDialog::use_template_clicked ()
82 {
83         _template_name->Enable (_use_template->GetValue ());
84 }
85
86 FilmNameLocationDialog::~FilmNameLocationDialog ()
87 {
88         _directory = wx_to_std (_folder->GetPath ());
89 }
90
91 boost::filesystem::path
92 FilmNameLocationDialog::path () const
93 {
94         filesystem::path p;
95         p /= wx_to_std (_folder->GetPath ());
96         p /= wx_to_std (_name->GetValue ());
97         return p;
98 }
99
100 optional<string>
101 FilmNameLocationDialog::template_name () const
102 {
103         if (!_use_template->GetValue() || _template_name->GetSelection() == -1) {
104                 return optional<string> ();
105         }
106
107         return wx_to_std (_template_name->GetString(_template_name->GetSelection()));
108 }
109
110 /** Check the path that is in our controls and offer confirmations or errors as required.
111  *  @return true if the path should be used.
112  */
113 bool
114 FilmNameLocationDialog::check_path ()
115 {
116         if (boost::filesystem::is_directory (path()) && !boost::filesystem::is_empty(path())) {
117                 if (!confirm_dialog (
118                             this,
119                             std_to_wx (
120                                     String::compose (wx_to_std (_("The directory %1 already exists and is not empty.  "
121                                                                   "Are you sure you want to use it?")),
122                                                      path().string().c_str())
123                                     )
124                             )) {
125                         return false;
126                 }
127         } else if (boost::filesystem::is_regular_file (path())) {
128                 error_dialog (
129                         this,
130                         String::compose (wx_to_std (_("%1 already exists as a file, so you cannot use it for a film.")), path().c_str())
131                         );
132                 return false;
133         }
134
135         return true;
136 }