Debian Codegraph @

editor/gui/directory_create_dialog.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* directory_create_dialog.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "directory_create_dialog.h"
32
33 #include "core/io/dir_access.h"
34 #include "editor/editor_node.h"
35 #include "editor/gui/editor_validation_panel.h"
36 #include "editor/themes/editor_scale.h"
37 #include "scene/gui/box_container.h"
38 #include "scene/gui/label.h"
39 #include "scene/gui/line_edit.h"
40
41 String DirectoryCreateDialog::_sanitize_input(const String &p_path) const {
42 String path = p_path.strip_edges();
43 if (mode == MODE_DIRECTORY) {
44 path = path.trim_suffix("/");
45 }
46 return path;
47 }
48
49 String DirectoryCreateDialog::_validate_path(const String &p_path) const {
50 if (p_path.is_empty()) {
51 return TTR("Name cannot be empty.");
52 }
53 if (mode == MODE_FILE && p_path.ends_with("/")) {
54 return TTR("File name can't end with /.");
55 }
56
57 const PackedStringArray splits = p_path.split("/");
58 for (int i = 0; i < splits.size(); i++) {
59 const String &part = splits[i];
60 bool is_file = mode == MODE_FILE && i == splits.size() - 1;
61
62 if (part.is_empty()) {
63 if (is_file) {
64 return TTR("File name cannot be empty.");
65 } else {
66 return TTR("Folder name cannot be empty.");
67 }
68 }
69 if (part.contains_char('\\') || part.contains_char(':') || part.contains_char('*') ||
70 part.contains_char('|') || part.contains_char('>') || part.ends_with(".") || part.ends_with(" ")) {
71 if (is_file) {
72 return TTR("File name contains invalid characters.");
73 } else {
74 return TTR("Folder name contains invalid characters.");
75 }
76 }
77 if (part[0] == '.') {
78 if (is_file) {
79 return TTR("File name begins with a dot.");
80 } else {
81 return TTR("Folder name begins with a dot.");
82 }
83 }
84 }
85
86 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
87 da->change_dir(base_dir);
88 if (da->file_exists(p_path)) {
89 return TTR("File with that name already exists.");
90 }
91 if (da->dir_exists(p_path)) {
92 return TTR("Folder with that name already exists.");
93 }
94
95 return String();
96 }
97
98 void DirectoryCreateDialog::_on_dir_path_changed() {
99 const String path = _sanitize_input(dir_path->get_text());
100 const String error = _validate_path(path);
101
102 if (error.is_empty()) {
103 if (path.contains_char('/')) {
104 if (mode == MODE_DIRECTORY) {
105 validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Using slashes in folder names will create subfolders recursively."), EditorValidationPanel::MSG_OK);
106 } else {
107 validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Using slashes in path will create the file in subfolder, creating new subfolders if necessary."), EditorValidationPanel::MSG_OK);
108 }
109 } else if (mode == MODE_FILE) {
110 validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, TTR("File name is valid."), EditorValidationPanel::MSG_OK);
111 }
112 } else {
113 validation_panel->set_message(EditorValidationPanel::MSG_ID_DEFAULT, error, EditorValidationPanel::MSG_ERROR);
114 }
115 }
116
117 void DirectoryCreateDialog::ok_pressed() {
118 const String path = _sanitize_input(dir_path->get_text());
119
120 // The OK button should be disabled if the path is invalid, but just in case.
121 const String error = _validate_path(path);
122 ERR_FAIL_COND_MSG(!error.is_empty(), error);
123
124 accept_callback.call(base_dir.path_join(path));
125 hide();
126 }
127
128 void DirectoryCreateDialog::_post_popup() {
129 ConfirmationDialog::_post_popup();
130 dir_path->grab_focus();
131 }
132
133 void DirectoryCreateDialog::config(const String &p_base_dir, const Callable &p_accept_callback, int p_mode, const String &p_title, const String &p_default_name) {
134 set_title(p_title);
135 base_dir = p_base_dir;
136 base_path_label->set_text(vformat(TTR("Base path: %s"), base_dir));
137 accept_callback = p_accept_callback;
138 mode = p_mode;
139
140 dir_path->set_text(p_default_name);
141 validation_panel->update();
142
143 if (p_mode == MODE_FILE) {
144 int extension_pos = p_default_name.rfind_char('.');
145 if (extension_pos > -1) {
146 dir_path->select(0, extension_pos);
147 return;
148 }
149 }
150 dir_path->select_all();
151 }
152
153 DirectoryCreateDialog::DirectoryCreateDialog() {
154 set_min_size(Size2i(480, 0) * EDSCALE);
155
156 VBoxContainer *vb = memnew(VBoxContainer);
157 add_child(vb);
158
159 base_path_label = memnew(Label);
160 base_path_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
161 base_path_label->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_WORD_ELLIPSIS);
162 vb->add_child(base_path_label);
163
164 Label *name_label = memnew(Label);
165 name_label->set_text(TTR("Name:"));
166 name_label->set_theme_type_variation("HeaderSmall");
167 vb->add_child(name_label);
168
169 dir_path = memnew(LineEdit);
170 dir_path->set_accessibility_name(TTRC("Name:"));
171 vb->add_child(dir_path);
172 register_text_enter(dir_path);
173
174 Control *spacing = memnew(Control);
175 spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
176 vb->add_child(spacing);
177
178 validation_panel = memnew(EditorValidationPanel);
179 vb->add_child(validation_panel);
180 validation_panel->add_line(EditorValidationPanel::MSG_ID_DEFAULT, TTR("Folder name is valid."));
181 validation_panel->set_update_callback(callable_mp(this, &DirectoryCreateDialog::_on_dir_path_changed));
182 validation_panel->set_accept_button(get_ok_button());
183
184 dir_path->connect(SceneStringName(text_changed), callable_mp(validation_panel, &EditorValidationPanel::update).unbind(1));
185 }