NewProjectDialog.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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_title("New project");
  42. auto& main_widget = set_main_widget<GUI::Widget>();
  43. main_widget.load_from_gml(new_project_dialog_gml);
  44. m_icon_view_container = *main_widget.find_descendant_of_type_named<GUI::Widget>("icon_view_container");
  45. m_icon_view = m_icon_view_container->add<GUI::IconView>();
  46. m_icon_view->set_always_wrap_item_labels(true);
  47. m_icon_view->set_model(m_model);
  48. m_icon_view->set_model_column(ProjectTemplatesModel::Column::Name);
  49. m_icon_view->on_selection_change = [&]() {
  50. update_dialog();
  51. };
  52. m_icon_view->on_activation = [&](auto&) {
  53. if (m_input_valid)
  54. do_create_project();
  55. };
  56. m_description_label = *main_widget.find_descendant_of_type_named<GUI::Label>("description_label");
  57. m_name_input = *main_widget.find_descendant_of_type_named<GUI::TextBox>("name_input");
  58. m_name_input->on_change = [&]() {
  59. update_dialog();
  60. };
  61. m_create_in_input = *main_widget.find_descendant_of_type_named<GUI::TextBox>("create_in_input");
  62. m_create_in_input->on_change = [&]() {
  63. update_dialog();
  64. };
  65. m_full_path_label = *main_widget.find_descendant_of_type_named<GUI::Label>("full_path_label");
  66. m_ok_button = *main_widget.find_descendant_of_type_named<GUI::Button>("ok_button");
  67. m_ok_button->set_default(true);
  68. m_ok_button->on_click = [this](auto) {
  69. do_create_project();
  70. };
  71. m_cancel_button = *main_widget.find_descendant_of_type_named<GUI::Button>("cancel_button");
  72. m_cancel_button->on_click = [this](auto) {
  73. done(ExecResult::Cancel);
  74. };
  75. m_browse_button = *find_descendant_of_type_named<GUI::Button>("browse_button");
  76. m_browse_button->on_click = [this](auto) {
  77. Optional<String> path = GUI::FilePicker::get_open_filepath(this, {}, Core::StandardPaths::home_directory(), true);
  78. if (path.has_value())
  79. m_create_in_input->set_text(path.value().view());
  80. };
  81. }
  82. RefPtr<ProjectTemplate> NewProjectDialog::selected_template()
  83. {
  84. if (m_icon_view->selection().is_empty()) {
  85. return {};
  86. }
  87. auto project_template = m_model->template_for_index(m_icon_view->selection().first());
  88. VERIFY(!project_template.is_null());
  89. return project_template;
  90. }
  91. void NewProjectDialog::update_dialog()
  92. {
  93. auto project_template = selected_template();
  94. m_input_valid = true;
  95. if (project_template) {
  96. m_description_label->set_text(project_template->description());
  97. } else {
  98. m_description_label->set_text("Select a project template to continue.");
  99. m_input_valid = false;
  100. }
  101. auto maybe_project_path = get_project_full_path();
  102. if (maybe_project_path.has_value()) {
  103. m_full_path_label->set_text(maybe_project_path.value());
  104. } else {
  105. m_full_path_label->set_text("Invalid name or creation directory.");
  106. m_input_valid = false;
  107. }
  108. m_ok_button->set_enabled(m_input_valid);
  109. }
  110. Optional<String> NewProjectDialog::get_available_project_name()
  111. {
  112. auto create_in = m_create_in_input->text();
  113. auto chosen_name = m_name_input->text();
  114. // Ensure project name isn't empty or entirely whitespace
  115. if (chosen_name.is_empty() || chosen_name.is_whitespace())
  116. return {};
  117. // Validate project name with validity regex
  118. if (!s_project_name_validity_regex.has_match(chosen_name))
  119. return {};
  120. // Check for up-to 999 variations of the project name, in case it's already taken
  121. for (int i = 0; i < 1000; i++) {
  122. auto candidate = (i == 0)
  123. ? chosen_name
  124. : String::formatted("{}-{}", chosen_name, i);
  125. if (!Core::File::exists(String::formatted("{}/{}", create_in, candidate)))
  126. return candidate;
  127. }
  128. return {};
  129. }
  130. Optional<String> NewProjectDialog::get_project_full_path()
  131. {
  132. // Do not permit forward-slashes in project names
  133. if (m_name_input->text().contains('/'))
  134. return {};
  135. auto create_in = m_create_in_input->text();
  136. auto maybe_project_name = get_available_project_name();
  137. if (!maybe_project_name.has_value())
  138. return {};
  139. return LexicalPath::join(create_in, *maybe_project_name).string();
  140. }
  141. void NewProjectDialog::do_create_project()
  142. {
  143. auto project_template = selected_template();
  144. if (!project_template) {
  145. GUI::MessageBox::show_error(this, "Could not create project: no template selected."sv);
  146. return;
  147. }
  148. auto maybe_project_name = get_available_project_name();
  149. auto maybe_project_full_path = get_project_full_path();
  150. if (!maybe_project_name.has_value() || !maybe_project_full_path.has_value()) {
  151. GUI::MessageBox::show_error(this, "Could not create project: invalid project name or path."sv);
  152. return;
  153. }
  154. auto create_in = m_create_in_input->text();
  155. if (!Core::File::exists(create_in) || !Core::File::is_directory(create_in)) {
  156. auto result = GUI::MessageBox::show(this, String::formatted("The directory {} does not exist yet, would you like to create it?", create_in), "New project"sv, GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo);
  157. if (result != GUI::MessageBox::ExecResult::Yes)
  158. return;
  159. auto created = Core::Directory::create(maybe_project_full_path.value(), Core::Directory::CreateDirectories::Yes);
  160. if (created.is_error()) {
  161. GUI::MessageBox::show_error(this, String::formatted("Could not create directory {}", create_in));
  162. return;
  163. }
  164. }
  165. auto creation_result = project_template->create_project(maybe_project_name.value(), maybe_project_full_path.value());
  166. if (!creation_result.is_error()) {
  167. // Successfully created, attempt to open the new project
  168. m_created_project_path = maybe_project_full_path.value();
  169. done(ExecResult::OK);
  170. } else {
  171. GUI::MessageBox::show_error(this, String::formatted("Could not create project: {}", creation_result.error()));
  172. }
  173. }
  174. }