NewProjectDialog.cpp 6.6 KB

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