NewProjectDialog.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2021, Nick Vella <nick@nxk.io>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "NewProjectDialog.h"
  8. #include "ProjectTemplatesModel.h"
  9. #include <DevTools/HackStudio/Dialogs/NewProjectDialogGML.h>
  10. #include <DevTools/HackStudio/ProjectTemplate.h>
  11. #include <AK/LexicalPath.h>
  12. #include <AK/String.h>
  13. #include <LibCore/Directory.h>
  14. #include <LibCore/File.h>
  15. #include <LibGUI/BoxLayout.h>
  16. #include <LibGUI/Button.h>
  17. #include <LibGUI/FilePicker.h>
  18. #include <LibGUI/IconView.h>
  19. #include <LibGUI/Label.h>
  20. #include <LibGUI/MessageBox.h>
  21. #include <LibGUI/TextBox.h>
  22. #include <LibGUI/Widget.h>
  23. #include <LibRegex/Regex.h>
  24. namespace HackStudio {
  25. static Regex<PosixExtended> const s_project_name_validity_regex("^([A-Za-z0-9_-])*$");
  26. GUI::Dialog::ExecResult NewProjectDialog::show(GUI::Window* parent_window)
  27. {
  28. auto dialog = NewProjectDialog::construct(parent_window);
  29. if (parent_window)
  30. dialog->set_icon(parent_window->icon());
  31. auto result = dialog->exec();
  32. return result;
  33. }
  34. NewProjectDialog::NewProjectDialog(GUI::Window* parent)
  35. : Dialog(parent)
  36. , m_model(ProjectTemplatesModel::create())
  37. {
  38. resize(500, 385);
  39. center_on_screen();
  40. set_resizable(false);
  41. set_modal(true);
  42. set_title("New project");
  43. auto& main_widget = set_main_widget<GUI::Widget>();
  44. main_widget.load_from_gml(new_project_dialog_gml);
  45. m_icon_view_container = *main_widget.find_descendant_of_type_named<GUI::Widget>("icon_view_container");
  46. m_icon_view = m_icon_view_container->add<GUI::IconView>();
  47. m_icon_view->set_always_wrap_item_labels(true);
  48. m_icon_view->set_model(m_model);
  49. m_icon_view->set_model_column(ProjectTemplatesModel::Column::Name);
  50. m_icon_view->on_selection_change = [&]() {
  51. update_dialog();
  52. };
  53. m_icon_view->on_activation = [&](auto&) {
  54. if (m_input_valid)
  55. do_create_project();
  56. };
  57. m_description_label = *main_widget.find_descendant_of_type_named<GUI::Label>("description_label");
  58. m_name_input = *main_widget.find_descendant_of_type_named<GUI::TextBox>("name_input");
  59. m_name_input->on_change = [&]() {
  60. update_dialog();
  61. };
  62. m_name_input->on_return_pressed = [&]() {
  63. if (m_input_valid)
  64. do_create_project();
  65. };
  66. m_create_in_input = *main_widget.find_descendant_of_type_named<GUI::TextBox>("create_in_input");
  67. m_create_in_input->on_change = [&]() {
  68. update_dialog();
  69. };
  70. m_create_in_input->on_return_pressed = [&]() {
  71. if (m_input_valid)
  72. do_create_project();
  73. };
  74. m_full_path_label = *main_widget.find_descendant_of_type_named<GUI::Label>("full_path_label");
  75. m_ok_button = *main_widget.find_descendant_of_type_named<GUI::Button>("ok_button");
  76. m_ok_button->on_click = [this](auto) {
  77. do_create_project();
  78. };
  79. m_cancel_button = *main_widget.find_descendant_of_type_named<GUI::Button>("cancel_button");
  80. m_cancel_button->on_click = [this](auto) {
  81. done(ExecResult::Cancel);
  82. };
  83. m_browse_button = *find_descendant_of_type_named<GUI::Button>("browse_button");
  84. m_browse_button->on_click = [this](auto) {
  85. Optional<String> path = GUI::FilePicker::get_open_filepath(this, {}, Core::StandardPaths::home_directory(), true);
  86. if (path.has_value())
  87. m_create_in_input->set_text(path.value().view());
  88. };
  89. }
  90. RefPtr<ProjectTemplate> NewProjectDialog::selected_template()
  91. {
  92. if (m_icon_view->selection().is_empty()) {
  93. return {};
  94. }
  95. auto project_template = m_model->template_for_index(m_icon_view->selection().first());
  96. VERIFY(!project_template.is_null());
  97. return project_template;
  98. }
  99. void NewProjectDialog::update_dialog()
  100. {
  101. auto project_template = selected_template();
  102. m_input_valid = true;
  103. if (project_template) {
  104. m_description_label->set_text(project_template->description());
  105. } else {
  106. m_description_label->set_text("Select a project template to continue.");
  107. m_input_valid = false;
  108. }
  109. auto maybe_project_path = get_project_full_path();
  110. if (maybe_project_path.has_value()) {
  111. m_full_path_label->set_text(maybe_project_path.value());
  112. } else {
  113. m_full_path_label->set_text("Invalid name or creation directory.");
  114. m_input_valid = false;
  115. }
  116. m_ok_button->set_enabled(m_input_valid);
  117. }
  118. Optional<String> NewProjectDialog::get_available_project_name()
  119. {
  120. auto create_in = m_create_in_input->text();
  121. auto chosen_name = m_name_input->text();
  122. // Ensure project name isn't empty or entirely whitespace
  123. if (chosen_name.is_empty() || chosen_name.is_whitespace())
  124. return {};
  125. // Validate project name with validity regex
  126. if (!s_project_name_validity_regex.has_match(chosen_name))
  127. return {};
  128. // Check for up-to 999 variations of the project name, in case it's already taken
  129. for (int i = 0; i < 1000; i++) {
  130. auto candidate = (i == 0)
  131. ? chosen_name
  132. : String::formatted("{}-{}", chosen_name, i);
  133. if (!Core::File::exists(String::formatted("{}/{}", create_in, candidate)))
  134. return candidate;
  135. }
  136. return {};
  137. }
  138. Optional<String> NewProjectDialog::get_project_full_path()
  139. {
  140. // Do not permit forward-slashes in project names
  141. if (m_name_input->text().contains("/"))
  142. return {};
  143. auto create_in = m_create_in_input->text();
  144. auto maybe_project_name = get_available_project_name();
  145. if (!maybe_project_name.has_value())
  146. return {};
  147. return LexicalPath::join(create_in, *maybe_project_name).string();
  148. }
  149. void NewProjectDialog::do_create_project()
  150. {
  151. auto project_template = selected_template();
  152. if (!project_template) {
  153. GUI::MessageBox::show_error(this, "Could not create project: no template selected.");
  154. return;
  155. }
  156. auto maybe_project_name = get_available_project_name();
  157. auto maybe_project_full_path = get_project_full_path();
  158. if (!maybe_project_name.has_value() || !maybe_project_full_path.has_value()) {
  159. GUI::MessageBox::show_error(this, "Could not create project: invalid project name or path.");
  160. return;
  161. }
  162. auto create_in = m_create_in_input->text();
  163. if (!Core::File::exists(create_in) || !Core::File::is_directory(create_in)) {
  164. auto result = GUI::MessageBox::show(this, String::formatted("The directory {} does not exist yet, would you like to create it?", create_in), "New project", GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo);
  165. if (result != GUI::MessageBox::ExecResult::Yes)
  166. return;
  167. auto created = Core::Directory::create(maybe_project_full_path.value(), Core::Directory::CreateDirectories::Yes);
  168. if (created.is_error()) {
  169. GUI::MessageBox::show_error(this, String::formatted("Could not create directory {}", create_in));
  170. return;
  171. }
  172. }
  173. auto creation_result = project_template->create_project(maybe_project_name.value(), maybe_project_full_path.value());
  174. if (!creation_result.is_error()) {
  175. // Successfully created, attempt to open the new project
  176. m_created_project_path = maybe_project_full_path.value();
  177. done(ExecResult::OK);
  178. } else {
  179. GUI::MessageBox::show_error(this, String::formatted("Could not create project: {}", creation_result.error()));
  180. }
  181. }
  182. }