directory_create_dialog.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #include "directory_create_dialog.h"
  31. #include "core/os/dir_access.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_scale.h"
  34. #include "scene/gui/box_container.h"
  35. #include "scene/gui/label.h"
  36. #include "scene/gui/line_edit.h"
  37. #include "scene/gui/panel_container.h"
  38. static String sanitize_input(const String &p_path) {
  39. String path = p_path.strip_edges();
  40. if (path.ends_with("/")) {
  41. path = path.left(path.length() - 1);
  42. }
  43. return path;
  44. }
  45. String DirectoryCreateDialog::_validate_path(const String &p_path) const {
  46. if (p_path.empty()) {
  47. return TTR("Folder name cannot be empty.");
  48. }
  49. if (p_path.find("\\") != -1 || p_path.find(":") != -1 || p_path.find("*") != -1 ||
  50. p_path.find("|") != -1 || p_path.find(">") != -1) {
  51. return TTR("Folder name contains invalid characters.");
  52. }
  53. const Vector<String> parts = p_path.split("/");
  54. for (int i = 0; i < parts.size(); i++) {
  55. const String part = parts[i];
  56. if (part.empty()) {
  57. return TTR("Folder name cannot be empty.");
  58. }
  59. if (part.ends_with(" ") || part[0] == ' ') {
  60. return TTR("Folder name cannot begin or end with a space.");
  61. }
  62. if (part[0] == '.') {
  63. return TTR("Folder name cannot begin with a dot.");
  64. }
  65. }
  66. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  67. da->change_dir(base_dir);
  68. if (da->file_exists(p_path)) {
  69. return TTR("File with that name already exists.");
  70. }
  71. if (da->dir_exists(p_path)) {
  72. return TTR("Folder with that name already exists.");
  73. }
  74. return String();
  75. }
  76. void DirectoryCreateDialog::_on_dir_path_changed(const String &p_text) {
  77. const String path = sanitize_input(p_text);
  78. const String error = _validate_path(path);
  79. if (error.empty()) {
  80. status_label->add_color_override("font_color", get_color("success_color", "Editor"));
  81. if (path.find("/") != -1) {
  82. status_label->set_text(TTR("Using slashes in folder names will create subfolders recursively."));
  83. } else {
  84. status_label->set_text(TTR("Folder name is valid."));
  85. }
  86. } else {
  87. status_label->add_color_override("font_color", get_color("error_color", "Editor"));
  88. status_label->set_text(error);
  89. }
  90. get_ok()->set_disabled(!error.empty());
  91. }
  92. void DirectoryCreateDialog::ok_pressed() {
  93. const String path = sanitize_input(dir_path->get_text());
  94. // The OK button should be disabled if the path is invalid, but just in case.
  95. const String error = _validate_path(path);
  96. ERR_FAIL_COND_MSG(!error.empty(), error);
  97. Error err;
  98. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  99. err = da->change_dir(base_dir);
  100. ERR_FAIL_COND_MSG(err != OK, "Cannot open directory '" + base_dir + "'.");
  101. print_verbose("Making folder " + path + " in " + base_dir);
  102. err = da->make_dir_recursive(path);
  103. if (err == OK) {
  104. emit_signal("dir_created");
  105. } else {
  106. EditorNode::get_singleton()->show_warning(TTR("Could not create folder."));
  107. }
  108. hide();
  109. }
  110. void DirectoryCreateDialog::_post_popup() {
  111. ConfirmationDialog::_post_popup();
  112. label->set_text(vformat(TTR("Create new folder in %s:"), base_dir));
  113. minimum_size_changed();
  114. dir_path->grab_focus();
  115. }
  116. void DirectoryCreateDialog::config(const String &p_base_dir) {
  117. base_dir = p_base_dir;
  118. label->set_text(""); // Set the correct text later in _post_popup to avoid GH-47005.
  119. dir_path->set_text("new folder");
  120. dir_path->select_all();
  121. _on_dir_path_changed(dir_path->get_text());
  122. }
  123. void DirectoryCreateDialog::_bind_methods() {
  124. ClassDB::bind_method(D_METHOD("_on_dir_path_changed"), &DirectoryCreateDialog::_on_dir_path_changed);
  125. ADD_SIGNAL(MethodInfo("dir_created"));
  126. }
  127. void DirectoryCreateDialog::_notification(int p_what) {
  128. switch (p_what) {
  129. case NOTIFICATION_THEME_CHANGED: {
  130. status_panel->add_style_override("panel", get_stylebox("bg", "Tree"));
  131. } break;
  132. }
  133. }
  134. DirectoryCreateDialog::DirectoryCreateDialog() {
  135. set_title(TTR("Create Folder"));
  136. set_custom_minimum_size(Size2i(480, 0) * EDSCALE);
  137. VBoxContainer *vb = memnew(VBoxContainer);
  138. add_child(vb);
  139. label = memnew(Label);
  140. label->set_autowrap(true);
  141. vb->add_child(label);
  142. dir_path = memnew(LineEdit);
  143. dir_path->connect("text_changed", this, "_on_dir_path_changed");
  144. vb->add_child(dir_path);
  145. register_text_enter(dir_path);
  146. Control *spacing = memnew(Control);
  147. spacing->set_custom_minimum_size(Size2(0, 10 * EDSCALE));
  148. vb->add_child(spacing);
  149. status_panel = memnew(PanelContainer);
  150. status_panel->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  151. vb->add_child(status_panel);
  152. status_label = memnew(Label);
  153. status_label->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  154. status_panel->add_child(status_label);
  155. }