Project.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Project.h"
  27. #include <AK/FileSystemPath.h>
  28. #include <AK/QuickSort.h>
  29. #include <AK/StringBuilder.h>
  30. #include <LibCore/File.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <sys/stat.h>
  34. struct Project::ProjectTreeNode : public RefCounted<ProjectTreeNode> {
  35. enum class Type {
  36. Invalid,
  37. Project,
  38. Directory,
  39. File,
  40. };
  41. ProjectTreeNode& find_or_create_subdirectory(const String& name)
  42. {
  43. for (auto& child : children) {
  44. if (child->type == Type::Directory && child->name == name)
  45. return *child;
  46. }
  47. auto new_child = adopt(*new ProjectTreeNode);
  48. new_child->type = Type::Directory;
  49. new_child->name = name;
  50. new_child->parent = this;
  51. auto* ptr = new_child.ptr();
  52. children.append(move(new_child));
  53. return *ptr;
  54. }
  55. void sort()
  56. {
  57. if (type == Type::File)
  58. return;
  59. quick_sort(children.begin(), children.end(), [](auto& a, auto& b) {
  60. return a->name < b->name;
  61. });
  62. for (auto& child : children)
  63. child->sort();
  64. }
  65. Type type { Type::Invalid };
  66. String name;
  67. String path;
  68. Vector<NonnullRefPtr<ProjectTreeNode>> children;
  69. ProjectTreeNode* parent { nullptr };
  70. };
  71. class ProjectModel final : public GUI::Model {
  72. public:
  73. explicit ProjectModel(Project& project)
  74. : m_project(project)
  75. {
  76. }
  77. virtual int row_count(const GUI::ModelIndex& index) const override
  78. {
  79. if (!index.is_valid())
  80. return 1;
  81. auto* node = static_cast<Project::ProjectTreeNode*>(index.internal_data());
  82. return node->children.size();
  83. }
  84. virtual int column_count(const GUI::ModelIndex&) const override
  85. {
  86. return 1;
  87. }
  88. virtual GUI::Variant data(const GUI::ModelIndex& index, Role role = Role::Display) const override
  89. {
  90. auto* node = static_cast<Project::ProjectTreeNode*>(index.internal_data());
  91. if (role == Role::Display) {
  92. return node->name;
  93. }
  94. if (role == Role::Custom) {
  95. return node->path;
  96. }
  97. if (role == Role::Icon) {
  98. if (node->type == Project::ProjectTreeNode::Type::Project)
  99. return m_project.m_project_icon;
  100. if (node->type == Project::ProjectTreeNode::Type::Directory)
  101. return m_project.m_directory_icon;
  102. if (node->name.ends_with(".cpp"))
  103. return m_project.m_cplusplus_icon;
  104. if (node->name.ends_with(".h"))
  105. return m_project.m_header_icon;
  106. return m_project.m_file_icon;
  107. }
  108. if (role == Role::Font) {
  109. extern String g_currently_open_file;
  110. if (node->name == g_currently_open_file)
  111. return Gfx::Font::default_bold_font();
  112. return {};
  113. }
  114. return {};
  115. }
  116. virtual GUI::ModelIndex index(int row, int column = 0, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override
  117. {
  118. if (!parent.is_valid()) {
  119. return create_index(row, column, &m_project.root_node());
  120. }
  121. auto& node = *static_cast<Project::ProjectTreeNode*>(parent.internal_data());
  122. return create_index(row, column, node.children.at(row).ptr());
  123. }
  124. GUI::ModelIndex parent_index(const GUI::ModelIndex& index) const override
  125. {
  126. if (!index.is_valid())
  127. return {};
  128. auto& node = *static_cast<Project::ProjectTreeNode*>(index.internal_data());
  129. if (!node.parent)
  130. return {};
  131. if (!node.parent->parent) {
  132. return create_index(0, 0, &m_project.root_node());
  133. ASSERT_NOT_REACHED();
  134. return {};
  135. }
  136. for (size_t row = 0; row < node.parent->parent->children.size(); ++row) {
  137. if (node.parent->parent->children[row].ptr() == node.parent)
  138. return create_index(row, 0, node.parent);
  139. }
  140. ASSERT_NOT_REACHED();
  141. return {};
  142. }
  143. virtual void update() override
  144. {
  145. did_update();
  146. }
  147. private:
  148. Project& m_project;
  149. };
  150. Project::Project(const String& path, Vector<String>&& filenames)
  151. : m_path(path)
  152. {
  153. m_name = FileSystemPath(m_path).basename();
  154. m_file_icon = GIcon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png"));
  155. m_cplusplus_icon = GIcon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-cplusplus.png"));
  156. m_header_icon = GIcon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-header.png"));
  157. m_directory_icon = GIcon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-folder.png"));
  158. m_project_icon = GIcon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-hack-studio.png"));
  159. for (auto& filename : filenames) {
  160. m_files.append(ProjectFile::construct_with_name(filename));
  161. }
  162. m_model = adopt(*new ProjectModel(*this));
  163. rebuild_tree();
  164. }
  165. Project::~Project()
  166. {
  167. }
  168. OwnPtr<Project> Project::load_from_file(const String& path)
  169. {
  170. auto file = Core::File::construct(path);
  171. if (!file->open(Core::File::ReadOnly))
  172. return nullptr;
  173. Vector<String> files;
  174. for (;;) {
  175. auto line = file->read_line(1024);
  176. if (line.is_null())
  177. break;
  178. files.append(String::copy(line, Chomp));
  179. }
  180. return OwnPtr(new Project(path, move(files)));
  181. }
  182. bool Project::add_file(const String& filename)
  183. {
  184. m_files.append(ProjectFile::construct_with_name(filename));
  185. rebuild_tree();
  186. m_model->update();
  187. return save();
  188. }
  189. bool Project::remove_file(const String& filename)
  190. {
  191. if (!get_file(filename))
  192. return false;
  193. m_files.remove_first_matching([filename](auto& file) { return file->name() == filename; });
  194. rebuild_tree();
  195. m_model->update();
  196. return save();
  197. }
  198. bool Project::save()
  199. {
  200. auto project_file = Core::File::construct(m_path);
  201. if (!project_file->open(Core::File::WriteOnly))
  202. return false;
  203. for (auto& file : m_files) {
  204. // FIXME: Check for error here. IODevice::printf() needs some work on error reporting.
  205. project_file->printf("%s\n", file.name().characters());
  206. }
  207. if (!project_file->close())
  208. return false;
  209. return true;
  210. }
  211. ProjectFile* Project::get_file(const String& filename)
  212. {
  213. for (auto& file : m_files) {
  214. if (FileSystemPath(file.name()).string() == FileSystemPath(filename).string())
  215. return &file;
  216. }
  217. return nullptr;
  218. }
  219. void Project::rebuild_tree()
  220. {
  221. auto root = adopt(*new ProjectTreeNode);
  222. root->name = m_name;
  223. root->type = ProjectTreeNode::Type::Project;
  224. for (auto& file : m_files) {
  225. FileSystemPath path(file.name());
  226. ProjectTreeNode* current = root.ptr();
  227. StringBuilder partial_path;
  228. for (size_t i = 0; i < path.parts().size(); ++i) {
  229. auto& part = path.parts().at(i);
  230. if (part == ".")
  231. continue;
  232. if (i != path.parts().size() - 1) {
  233. current = &current->find_or_create_subdirectory(part);
  234. continue;
  235. }
  236. struct stat st;
  237. if (lstat(path.string().characters(), &st) < 0)
  238. continue;
  239. if (S_ISDIR(st.st_mode)) {
  240. current = &current->find_or_create_subdirectory(part);
  241. continue;
  242. }
  243. auto file_node = adopt(*new ProjectTreeNode);
  244. file_node->name = part;
  245. file_node->path = path.string();
  246. file_node->type = Project::ProjectTreeNode::Type::File;
  247. file_node->parent = current;
  248. current->children.append(move(file_node));
  249. break;
  250. }
  251. }
  252. root->sort();
  253. #if 0
  254. Function<void(ProjectTreeNode&, int indent)> dump_tree = [&](ProjectTreeNode& node, int indent) {
  255. for (int i = 0; i < indent; ++i)
  256. printf(" ");
  257. if (node.name.is_null())
  258. printf("(null)\n");
  259. else
  260. printf("%s\n", node.name.characters());
  261. for (auto& child : node.children) {
  262. dump_tree(*child, indent + 2);
  263. }
  264. };
  265. dump_tree(*root, 0);
  266. #endif
  267. m_root_node = move(root);
  268. m_model->update();
  269. }