Project.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include "Project.h"
  2. #include <AK/FileSystemPath.h>
  3. #include <AK/QuickSort.h>
  4. #include <AK/StringBuilder.h>
  5. #include <LibCore/CFile.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <sys/stat.h>
  9. struct Project::ProjectTreeNode : public RefCounted<ProjectTreeNode> {
  10. enum class Type {
  11. Invalid,
  12. Project,
  13. Directory,
  14. File,
  15. };
  16. ProjectTreeNode& find_or_create_subdirectory(const String& name)
  17. {
  18. for (auto& child : children) {
  19. if (child->type == Type::Directory && child->name == name)
  20. return *child;
  21. }
  22. auto new_child = adopt(*new ProjectTreeNode);
  23. new_child->type = Type::Directory;
  24. new_child->name = name;
  25. new_child->parent = this;
  26. auto* ptr = new_child.ptr();
  27. children.append(move(new_child));
  28. return *ptr;
  29. }
  30. void sort()
  31. {
  32. if (type == Type::File)
  33. return;
  34. quick_sort(children.begin(), children.end(), [](auto& a, auto& b) {
  35. return a->name < b->name;
  36. });
  37. for (auto& child : children)
  38. child->sort();
  39. }
  40. Type type { Type::Invalid };
  41. String name;
  42. String path;
  43. Vector<NonnullRefPtr<ProjectTreeNode>> children;
  44. ProjectTreeNode* parent { nullptr };
  45. };
  46. class ProjectModel final : public GModel {
  47. public:
  48. explicit ProjectModel(Project& project)
  49. : m_project(project)
  50. {
  51. }
  52. virtual int row_count(const GModelIndex& index) const override
  53. {
  54. if (!index.is_valid())
  55. return 1;
  56. auto* node = static_cast<Project::ProjectTreeNode*>(index.internal_data());
  57. return node->children.size();
  58. }
  59. virtual int column_count(const GModelIndex&) const override
  60. {
  61. return 1;
  62. }
  63. virtual GVariant data(const GModelIndex& index, Role role = Role::Display) const override
  64. {
  65. auto* node = static_cast<Project::ProjectTreeNode*>(index.internal_data());
  66. if (role == Role::Display) {
  67. return node->name;
  68. }
  69. if (role == Role::Custom) {
  70. return node->path;
  71. }
  72. if (role == Role::Icon) {
  73. if (node->type == Project::ProjectTreeNode::Type::Project)
  74. return m_project.m_project_icon;
  75. if (node->type == Project::ProjectTreeNode::Type::Directory)
  76. return m_project.m_directory_icon;
  77. if (node->name.ends_with(".cpp"))
  78. return m_project.m_cplusplus_icon;
  79. if (node->name.ends_with(".h"))
  80. return m_project.m_header_icon;
  81. return m_project.m_file_icon;
  82. }
  83. if (role == Role::Font) {
  84. extern String g_currently_open_file;
  85. if (node->name == g_currently_open_file)
  86. return Font::default_bold_font();
  87. return {};
  88. }
  89. return {};
  90. }
  91. virtual GModelIndex index(int row, int column = 0, const GModelIndex& parent = GModelIndex()) const override
  92. {
  93. if (!parent.is_valid()) {
  94. return create_index(row, column, &m_project.root_node());
  95. }
  96. auto& node = *static_cast<Project::ProjectTreeNode*>(parent.internal_data());
  97. return create_index(row, column, node.children.at(row).ptr());
  98. }
  99. GModelIndex parent_index(const GModelIndex& index) const override
  100. {
  101. if (!index.is_valid())
  102. return {};
  103. auto& node = *static_cast<Project::ProjectTreeNode*>(index.internal_data());
  104. if (!node.parent)
  105. return {};
  106. if (!node.parent->parent) {
  107. return create_index(0, 0, &m_project.root_node());
  108. ASSERT_NOT_REACHED();
  109. return {};
  110. }
  111. for (int row = 0; row < node.parent->parent->children.size(); ++row) {
  112. if (node.parent->parent->children[row].ptr() == node.parent)
  113. return create_index(row, 0, node.parent);
  114. }
  115. ASSERT_NOT_REACHED();
  116. return {};
  117. }
  118. virtual void update() override
  119. {
  120. did_update();
  121. }
  122. private:
  123. Project& m_project;
  124. };
  125. Project::Project(const String& path, Vector<String>&& filenames)
  126. : m_path(path)
  127. {
  128. m_file_icon = GIcon(GraphicsBitmap::load_from_file("/res/icons/16x16/filetype-unknown.png"));
  129. m_cplusplus_icon = GIcon(GraphicsBitmap::load_from_file("/res/icons/16x16/filetype-cplusplus.png"));
  130. m_header_icon = GIcon(GraphicsBitmap::load_from_file("/res/icons/16x16/filetype-header.png"));
  131. m_directory_icon = GIcon(GraphicsBitmap::load_from_file("/res/icons/16x16/filetype-folder.png"));
  132. m_project_icon = GIcon(GraphicsBitmap::load_from_file("/res/icons/16x16/app-hack-studio.png"));
  133. for (auto& filename : filenames) {
  134. m_files.append(ProjectFile::construct_with_name(filename));
  135. }
  136. m_model = adopt(*new ProjectModel(*this));
  137. rebuild_tree();
  138. }
  139. Project::~Project()
  140. {
  141. }
  142. OwnPtr<Project> Project::load_from_file(const String& path)
  143. {
  144. auto file = CFile::construct(path);
  145. if (!file->open(CFile::ReadOnly))
  146. return nullptr;
  147. Vector<String> files;
  148. for (;;) {
  149. auto line = file->read_line(1024);
  150. if (line.is_null())
  151. break;
  152. files.append(String::copy(line, Chomp));
  153. }
  154. return OwnPtr(new Project(path, move(files)));
  155. }
  156. bool Project::add_file(const String& filename)
  157. {
  158. auto project_file = CFile::construct(m_path);
  159. if (!project_file->open(CFile::WriteOnly))
  160. return false;
  161. for (auto& file : m_files) {
  162. // FIXME: Check for error here. CIODevice::printf() needs some work on error reporting.
  163. project_file->printf("%s\n", file.name().characters());
  164. }
  165. project_file->printf("%s\n", filename.characters());
  166. if (!project_file->close())
  167. return false;
  168. m_files.append(ProjectFile::construct_with_name(filename));
  169. m_model->update();
  170. return true;
  171. }
  172. ProjectFile* Project::get_file(const String& filename)
  173. {
  174. for (auto& file : m_files) {
  175. if (file.name() == filename)
  176. return &file;
  177. }
  178. return nullptr;
  179. }
  180. void Project::rebuild_tree()
  181. {
  182. auto root = adopt(*new ProjectTreeNode);
  183. root->type = ProjectTreeNode::Type::Project;
  184. for (auto& file : m_files) {
  185. FileSystemPath path(file.name());
  186. ProjectTreeNode* current = root.ptr();
  187. StringBuilder partial_path;
  188. for (int i = 0; i < path.parts().size(); ++i) {
  189. auto& part = path.parts().at(i);
  190. if (part == ".")
  191. continue;
  192. if (i != path.parts().size() - 1) {
  193. current = &current->find_or_create_subdirectory(part);
  194. continue;
  195. }
  196. struct stat st;
  197. if (lstat(path.string().characters(), &st) < 0)
  198. continue;
  199. if (S_ISDIR(st.st_mode)) {
  200. current = &current->find_or_create_subdirectory(part);
  201. continue;
  202. }
  203. auto file_node = adopt(*new ProjectTreeNode);
  204. file_node->name = part;
  205. file_node->path = path.string();
  206. file_node->type = Project::ProjectTreeNode::Type::File;
  207. file_node->parent = current;
  208. current->children.append(move(file_node));
  209. break;
  210. }
  211. }
  212. root->sort();
  213. #if 0
  214. Function<void(ProjectTreeNode&, int indent)> dump_tree = [&](ProjectTreeNode& node, int indent) {
  215. for (int i = 0; i < indent; ++i)
  216. printf(" ");
  217. if (node.name.is_null())
  218. printf("(null)\n");
  219. else
  220. printf("%s\n", node.name.characters());
  221. for (auto& child : node.children) {
  222. dump_tree(*child, indent + 2);
  223. }
  224. };
  225. dump_tree(*root, 0);
  226. #endif
  227. m_root_node = move(root);
  228. m_model->update();
  229. }