Serenity Operating System
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
27#include "Project.h"
28#include <AK/FileSystemPath.h>
29#include <AK/QuickSort.h>
30#include <AK/StringBuilder.h>
31#include <LibCore/DirIterator.h>
32#include <LibCore/File.h>
33#include <stdio.h>
34#include <string.h>
35#include <sys/stat.h>
36#include <unistd.h>
37
38struct Project::ProjectTreeNode : public RefCounted<ProjectTreeNode> {
39 enum class Type {
40 Invalid,
41 Project,
42 Directory,
43 File,
44 };
45
46 ProjectTreeNode& find_or_create_subdirectory(const String& name)
47 {
48 for (auto& child : children) {
49 if (child->type == Type::Directory && child->name == name)
50 return *child;
51 }
52 auto new_child = adopt(*new ProjectTreeNode);
53 new_child->type = Type::Directory;
54 new_child->name = name;
55 new_child->parent = this;
56 auto* ptr = new_child.ptr();
57 children.append(move(new_child));
58 return *ptr;
59 }
60
61 void sort()
62 {
63 if (type == Type::File)
64 return;
65 quick_sort(children, [](auto& a, auto& b) {
66 return a->name < b->name;
67 });
68 for (auto& child : children)
69 child->sort();
70 }
71
72 Type type { Type::Invalid };
73 String name;
74 String path;
75 Vector<NonnullRefPtr<ProjectTreeNode>> children;
76 ProjectTreeNode* parent { nullptr };
77};
78
79class ProjectModel final : public GUI::Model {
80public:
81 explicit ProjectModel(Project& project)
82 : m_project(project)
83 {
84 }
85
86 virtual int row_count(const GUI::ModelIndex& index) const override
87 {
88 if (!index.is_valid())
89 return 1;
90 auto* node = static_cast<Project::ProjectTreeNode*>(index.internal_data());
91 return node->children.size();
92 }
93
94 virtual int column_count(const GUI::ModelIndex&) const override
95 {
96 return 1;
97 }
98
99 virtual GUI::Variant data(const GUI::ModelIndex& index, Role role = Role::Display) const override
100 {
101 auto* node = static_cast<Project::ProjectTreeNode*>(index.internal_data());
102 if (role == Role::Display) {
103 return node->name;
104 }
105 if (role == Role::Custom) {
106 return node->path;
107 }
108 if (role == Role::Icon) {
109 if (node->type == Project::ProjectTreeNode::Type::Project)
110 return m_project.m_project_icon;
111 if (node->type == Project::ProjectTreeNode::Type::Directory)
112 return m_project.m_directory_icon;
113 if (node->name.ends_with(".cpp"))
114 return m_project.m_cplusplus_icon;
115 if (node->name.ends_with(".h"))
116 return m_project.m_header_icon;
117 return m_project.m_file_icon;
118 }
119 if (role == Role::Font) {
120 extern String g_currently_open_file;
121 if (node->name == g_currently_open_file)
122 return Gfx::Font::default_bold_font();
123 return {};
124 }
125 return {};
126 }
127
128 virtual GUI::ModelIndex index(int row, int column = 0, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override
129 {
130 if (!parent.is_valid()) {
131 return create_index(row, column, &m_project.root_node());
132 }
133 auto& node = *static_cast<Project::ProjectTreeNode*>(parent.internal_data());
134 return create_index(row, column, node.children.at(row).ptr());
135 }
136
137 GUI::ModelIndex parent_index(const GUI::ModelIndex& index) const override
138 {
139 if (!index.is_valid())
140 return {};
141 auto& node = *static_cast<Project::ProjectTreeNode*>(index.internal_data());
142 if (!node.parent)
143 return {};
144
145 if (!node.parent->parent) {
146 return create_index(0, 0, &m_project.root_node());
147 ASSERT_NOT_REACHED();
148 return {};
149 }
150
151 for (size_t row = 0; row < node.parent->parent->children.size(); ++row) {
152 if (node.parent->parent->children[row].ptr() == node.parent)
153 return create_index(row, 0, node.parent);
154 }
155
156 ASSERT_NOT_REACHED();
157 return {};
158 }
159
160 virtual void update() override
161 {
162 did_update();
163 }
164
165private:
166 Project& m_project;
167};
168
169Project::Project(const String& path, Vector<String>&& filenames)
170 : m_path(path)
171{
172 m_name = FileSystemPath(m_path).basename();
173
174 m_file_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png"));
175 m_cplusplus_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-cplusplus.png"));
176 m_header_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-header.png"));
177 m_directory_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-folder.png"));
178 m_project_icon = GUI::Icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-hack-studio.png"));
179
180 for (auto& filename : filenames) {
181 m_files.append(ProjectFile::construct_with_name(filename));
182 }
183
184 m_model = adopt(*new ProjectModel(*this));
185
186 rebuild_tree();
187}
188
189Project::~Project()
190{
191}
192
193OwnPtr<Project> Project::load_from_file(const String& path)
194{
195 auto file = Core::File::construct(path);
196 if (!file->open(Core::File::ReadOnly))
197 return nullptr;
198
199 auto type = ProjectType::Cpp;
200 Vector<String> files;
201
202 auto add_glob = [&](String path) {
203 auto split = path.split('*', true);
204 for (auto& item : split) {
205 dbg() << item;
206 }
207 ASSERT(split.size() == 2);
208 auto cwd = getcwd(nullptr, 0);
209 Core::DirIterator it(cwd, Core::DirIterator::Flags::SkipParentAndBaseDir);
210 while (it.has_next()) {
211 auto path = it.next_path();
212 if (!split[0].is_empty() && !path.starts_with(split[0]))
213 continue;
214
215 if (!split[1].is_empty() && !path.ends_with(split[1]))
216 continue;
217
218 files.append(path);
219 }
220 };
221
222 for (;;) {
223 auto line = file->read_line(1024);
224 if (line.is_null())
225 break;
226
227 auto path = String::copy(line, Chomp);
228 if (path.contains("*"))
229 add_glob(path);
230 else
231 files.append(path);
232 }
233
234 for (auto& file : files) {
235 if (file.ends_with(".js")) {
236 type = ProjectType::Javascript;
237 break;
238 }
239 }
240
241 quick_sort(files);
242
243 auto project = OwnPtr(new Project(path, move(files)));
244 project->m_type = type;
245 return project;
246}
247
248bool Project::add_file(const String& filename)
249{
250 m_files.append(ProjectFile::construct_with_name(filename));
251 rebuild_tree();
252 m_model->update();
253 return save();
254}
255
256bool Project::remove_file(const String& filename)
257{
258 if (!get_file(filename))
259 return false;
260 m_files.remove_first_matching([filename](auto& file) { return file->name() == filename; });
261 rebuild_tree();
262 m_model->update();
263 return save();
264}
265
266bool Project::save()
267{
268 auto project_file = Core::File::construct(m_path);
269 if (!project_file->open(Core::File::WriteOnly))
270 return false;
271
272 for (auto& file : m_files) {
273 // FIXME: Check for error here. IODevice::printf() needs some work on error reporting.
274 project_file->printf("%s\n", file.name().characters());
275 }
276
277 if (!project_file->close())
278 return false;
279
280 return true;
281}
282
283ProjectFile* Project::get_file(const String& filename)
284{
285 for (auto& file : m_files) {
286 if (FileSystemPath(file.name()).string() == FileSystemPath(filename).string())
287 return &file;
288 }
289 return nullptr;
290}
291
292String Project::default_file() const
293{
294 if (m_type == ProjectType::Cpp)
295 return "main.cpp";
296
297 if (m_files.size() > 0)
298 return m_files.first().name();
299
300 ASSERT_NOT_REACHED();
301}
302
303void Project::rebuild_tree()
304{
305 auto root = adopt(*new ProjectTreeNode);
306 root->name = m_name;
307 root->type = ProjectTreeNode::Type::Project;
308
309 for (auto& file : m_files) {
310 FileSystemPath path(file.name());
311 ProjectTreeNode* current = root.ptr();
312 StringBuilder partial_path;
313
314 for (size_t i = 0; i < path.parts().size(); ++i) {
315 auto& part = path.parts().at(i);
316 if (part == ".")
317 continue;
318 if (i != path.parts().size() - 1) {
319 current = ¤t->find_or_create_subdirectory(part);
320 continue;
321 }
322 struct stat st;
323 if (lstat(path.string().characters(), &st) < 0)
324 continue;
325
326 if (S_ISDIR(st.st_mode)) {
327 current = ¤t->find_or_create_subdirectory(part);
328 continue;
329 }
330 auto file_node = adopt(*new ProjectTreeNode);
331 file_node->name = part;
332 file_node->path = path.string();
333 file_node->type = Project::ProjectTreeNode::Type::File;
334 file_node->parent = current;
335 current->children.append(move(file_node));
336 break;
337 }
338 }
339
340 root->sort();
341
342#if 0
343 Function<void(ProjectTreeNode&, int indent)> dump_tree = [&](ProjectTreeNode& node, int indent) {
344 for (int i = 0; i < indent; ++i)
345 printf(" ");
346 if (node.name.is_null())
347 printf("(null)\n");
348 else
349 printf("%s\n", node.name.characters());
350 for (auto& child : node.children) {
351 dump_tree(*child, indent + 2);
352 }
353 };
354
355 dump_tree(*root, 0);
356#endif
357
358 m_root_node = move(root);
359 m_model->update();
360}