FileSystemModel.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/LexicalPath.h>
  9. #include <AK/NumberFormat.h>
  10. #include <AK/QuickSort.h>
  11. #include <AK/StringBuilder.h>
  12. #include <LibCore/DirIterator.h>
  13. #include <LibCore/File.h>
  14. #include <LibCore/StandardPaths.h>
  15. #include <LibGUI/AbstractView.h>
  16. #include <LibGUI/FileIconProvider.h>
  17. #include <LibGUI/FileSystemModel.h>
  18. #include <LibGUI/Painter.h>
  19. #include <LibGfx/Bitmap.h>
  20. #include <LibThreading/BackgroundAction.h>
  21. #include <grp.h>
  22. #include <pwd.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27. namespace GUI {
  28. ModelIndex FileSystemModel::Node::index(int column) const
  29. {
  30. if (!m_parent)
  31. return {};
  32. for (size_t row = 0; row < m_parent->m_children.size(); ++row) {
  33. if (&m_parent->m_children[row] == this)
  34. return m_model.create_index(row, column, const_cast<Node*>(this));
  35. }
  36. VERIFY_NOT_REACHED();
  37. }
  38. bool FileSystemModel::Node::fetch_data(String const& full_path, bool is_root)
  39. {
  40. struct stat st;
  41. int rc;
  42. if (is_root)
  43. rc = stat(full_path.characters(), &st);
  44. else
  45. rc = lstat(full_path.characters(), &st);
  46. if (rc < 0) {
  47. m_error = errno;
  48. perror("stat/lstat");
  49. return false;
  50. }
  51. size = st.st_size;
  52. mode = st.st_mode;
  53. uid = st.st_uid;
  54. gid = st.st_gid;
  55. inode = st.st_ino;
  56. mtime = st.st_mtime;
  57. if (S_ISLNK(mode)) {
  58. symlink_target = Core::File::read_link(full_path);
  59. if (symlink_target.is_null())
  60. perror("readlink");
  61. }
  62. if (S_ISDIR(mode)) {
  63. is_accessible_directory = access(full_path.characters(), R_OK | X_OK) == 0;
  64. }
  65. return true;
  66. }
  67. void FileSystemModel::Node::traverse_if_needed()
  68. {
  69. if (!is_directory() || m_has_traversed)
  70. return;
  71. m_has_traversed = true;
  72. if (m_parent_of_root) {
  73. auto root = adopt_own(*new Node(m_model));
  74. root->fetch_data("/", true);
  75. root->name = "/";
  76. root->m_parent = this;
  77. m_children.append(move(root));
  78. return;
  79. }
  80. total_size = 0;
  81. auto full_path = this->full_path();
  82. Core::DirIterator di(full_path, m_model.should_show_dotfiles() ? Core::DirIterator::SkipParentAndBaseDir : Core::DirIterator::SkipDots);
  83. if (di.has_error()) {
  84. m_error = di.error();
  85. warnln("DirIterator: {}", di.error_string());
  86. return;
  87. }
  88. Vector<String> child_names;
  89. while (di.has_next()) {
  90. child_names.append(di.next_path());
  91. }
  92. quick_sort(child_names);
  93. NonnullOwnPtrVector<Node> directory_children;
  94. NonnullOwnPtrVector<Node> file_children;
  95. for (auto& child_name : child_names) {
  96. auto maybe_child = create_child(child_name);
  97. if (!maybe_child)
  98. continue;
  99. auto child = maybe_child.release_nonnull();
  100. total_size += child->size;
  101. if (S_ISDIR(child->mode))
  102. directory_children.append(move(child));
  103. else
  104. file_children.append(move(child));
  105. }
  106. m_children.extend(move(directory_children));
  107. m_children.extend(move(file_children));
  108. if (!m_model.m_file_watcher->is_watching(full_path)) {
  109. // We are not already watching this file, watch it
  110. auto result = m_model.m_file_watcher->add_watch(full_path,
  111. Core::FileWatcherEvent::Type::MetadataModified
  112. | Core::FileWatcherEvent::Type::ChildCreated
  113. | Core::FileWatcherEvent::Type::ChildDeleted
  114. | Core::FileWatcherEvent::Type::Deleted);
  115. if (result.is_error()) {
  116. dbgln("Couldn't watch '{}': {}", full_path, result.error());
  117. } else if (result.value() == false) {
  118. dbgln("Couldn't watch '{}', probably already watching", full_path);
  119. }
  120. }
  121. }
  122. OwnPtr<FileSystemModel::Node> FileSystemModel::Node::create_child(String const& child_name)
  123. {
  124. String child_path = LexicalPath::join(full_path(), child_name).string();
  125. auto child = adopt_own(*new Node(m_model));
  126. bool ok = child->fetch_data(child_path, false);
  127. if (!ok)
  128. return {};
  129. if (m_model.m_mode == DirectoriesOnly && !S_ISDIR(child->mode))
  130. return {};
  131. child->name = child_name;
  132. child->m_parent = this;
  133. return child;
  134. }
  135. void FileSystemModel::Node::reify_if_needed()
  136. {
  137. traverse_if_needed();
  138. if (mode != 0)
  139. return;
  140. fetch_data(full_path(), m_parent == nullptr || m_parent->m_parent_of_root);
  141. }
  142. bool FileSystemModel::Node::is_symlink_to_directory() const
  143. {
  144. if (!S_ISLNK(mode))
  145. return false;
  146. struct stat st;
  147. if (lstat(symlink_target.characters(), &st) < 0)
  148. return false;
  149. return S_ISDIR(st.st_mode);
  150. }
  151. String FileSystemModel::Node::full_path() const
  152. {
  153. Vector<String, 32> lineage;
  154. for (auto* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
  155. lineage.append(ancestor->name);
  156. }
  157. StringBuilder builder;
  158. builder.append(m_model.root_path());
  159. for (int i = lineage.size() - 1; i >= 0; --i) {
  160. builder.append('/');
  161. builder.append(lineage[i]);
  162. }
  163. builder.append('/');
  164. builder.append(name);
  165. return LexicalPath::canonicalized_path(builder.to_string());
  166. }
  167. ModelIndex FileSystemModel::index(String path, int column) const
  168. {
  169. Node const* node = node_for_path(move(path));
  170. if (node != nullptr) {
  171. return node->index(column);
  172. }
  173. return {};
  174. }
  175. FileSystemModel::Node const* FileSystemModel::node_for_path(String const& path) const
  176. {
  177. String resolved_path;
  178. if (path == m_root_path)
  179. resolved_path = "/";
  180. else if (!m_root_path.is_empty() && path.starts_with(m_root_path))
  181. resolved_path = LexicalPath::relative_path(path, m_root_path);
  182. else
  183. resolved_path = path;
  184. LexicalPath lexical_path(resolved_path);
  185. Node const* node = m_root->m_parent_of_root ? &m_root->m_children.first() : m_root;
  186. if (lexical_path.string() == "/")
  187. return node;
  188. auto& parts = lexical_path.parts_view();
  189. for (size_t i = 0; i < parts.size(); ++i) {
  190. auto& part = parts[i];
  191. bool found = false;
  192. for (auto& child : node->m_children) {
  193. if (child.name == part) {
  194. const_cast<Node&>(child).reify_if_needed();
  195. node = &child;
  196. found = true;
  197. if (i == parts.size() - 1)
  198. return node;
  199. break;
  200. }
  201. }
  202. if (!found)
  203. return nullptr;
  204. }
  205. return nullptr;
  206. }
  207. String FileSystemModel::full_path(ModelIndex const& index) const
  208. {
  209. auto& node = this->node(index);
  210. const_cast<Node&>(node).reify_if_needed();
  211. return node.full_path();
  212. }
  213. FileSystemModel::FileSystemModel(String root_path, Mode mode)
  214. : m_root_path(LexicalPath::canonicalized_path(move(root_path)))
  215. , m_mode(mode)
  216. {
  217. setpwent();
  218. while (auto* passwd = getpwent())
  219. m_user_names.set(passwd->pw_uid, passwd->pw_name);
  220. endpwent();
  221. setgrent();
  222. while (auto* group = getgrent())
  223. m_group_names.set(group->gr_gid, group->gr_name);
  224. endgrent();
  225. auto result = Core::FileWatcher::create();
  226. if (result.is_error()) {
  227. dbgln("{}", result.error());
  228. VERIFY_NOT_REACHED();
  229. }
  230. m_file_watcher = result.release_value();
  231. m_file_watcher->on_change = [this](Core::FileWatcherEvent const& event) {
  232. handle_file_event(event);
  233. };
  234. invalidate();
  235. }
  236. String FileSystemModel::name_for_uid(uid_t uid) const
  237. {
  238. auto it = m_user_names.find(uid);
  239. if (it == m_user_names.end())
  240. return String::number(uid);
  241. return (*it).value;
  242. }
  243. String FileSystemModel::name_for_gid(gid_t gid) const
  244. {
  245. auto it = m_group_names.find(gid);
  246. if (it == m_group_names.end())
  247. return String::number(gid);
  248. return (*it).value;
  249. }
  250. static String permission_string(mode_t mode)
  251. {
  252. StringBuilder builder;
  253. if (S_ISDIR(mode))
  254. builder.append("d");
  255. else if (S_ISLNK(mode))
  256. builder.append("l");
  257. else if (S_ISBLK(mode))
  258. builder.append("b");
  259. else if (S_ISCHR(mode))
  260. builder.append("c");
  261. else if (S_ISFIFO(mode))
  262. builder.append("f");
  263. else if (S_ISSOCK(mode))
  264. builder.append("s");
  265. else if (S_ISREG(mode))
  266. builder.append("-");
  267. else
  268. builder.append("?");
  269. builder.append(mode & S_IRUSR ? 'r' : '-');
  270. builder.append(mode & S_IWUSR ? 'w' : '-');
  271. builder.append(mode & S_ISUID ? 's' : (mode & S_IXUSR ? 'x' : '-'));
  272. builder.append(mode & S_IRGRP ? 'r' : '-');
  273. builder.append(mode & S_IWGRP ? 'w' : '-');
  274. builder.append(mode & S_ISGID ? 's' : (mode & S_IXGRP ? 'x' : '-'));
  275. builder.append(mode & S_IROTH ? 'r' : '-');
  276. builder.append(mode & S_IWOTH ? 'w' : '-');
  277. if (mode & S_ISVTX)
  278. builder.append('t');
  279. else
  280. builder.append(mode & S_IXOTH ? 'x' : '-');
  281. return builder.to_string();
  282. }
  283. void FileSystemModel::Node::set_selected(bool selected)
  284. {
  285. if (m_selected == selected)
  286. return;
  287. m_selected = selected;
  288. }
  289. void FileSystemModel::update_node_on_selection(ModelIndex const& index, bool const selected)
  290. {
  291. Node& node = const_cast<Node&>(this->node(index));
  292. node.set_selected(selected);
  293. }
  294. void FileSystemModel::set_root_path(String root_path)
  295. {
  296. if (root_path.is_null())
  297. m_root_path = {};
  298. else
  299. m_root_path = LexicalPath::canonicalized_path(move(root_path));
  300. invalidate();
  301. if (m_root->has_error()) {
  302. if (on_directory_change_error)
  303. on_directory_change_error(m_root->error(), m_root->error_string());
  304. } else if (on_complete) {
  305. on_complete();
  306. }
  307. }
  308. void FileSystemModel::invalidate()
  309. {
  310. m_root = adopt_own(*new Node(*this));
  311. if (m_root_path.is_null())
  312. m_root->m_parent_of_root = true;
  313. m_root->reify_if_needed();
  314. Model::invalidate();
  315. }
  316. void FileSystemModel::handle_file_event(Core::FileWatcherEvent const& event)
  317. {
  318. if (event.type == Core::FileWatcherEvent::Type::ChildCreated) {
  319. if (node_for_path(event.event_path) != nullptr)
  320. return;
  321. } else {
  322. if (node_for_path(event.event_path) == nullptr)
  323. return;
  324. }
  325. switch (event.type) {
  326. case Core::FileWatcherEvent::Type::ChildCreated: {
  327. LexicalPath path { event.event_path };
  328. auto& parts = path.parts_view();
  329. StringView child_name = parts.last();
  330. auto parent_name = path.parent().string();
  331. Node* parent = const_cast<Node*>(node_for_path(parent_name));
  332. if (parent == nullptr) {
  333. dbgln("Got a ChildCreated on '{}' but that path does not exist?!", parent_name);
  334. break;
  335. }
  336. int child_count = parent->m_children.size();
  337. auto maybe_child = parent->create_child(child_name);
  338. if (!maybe_child)
  339. break;
  340. begin_insert_rows(parent->index(0), child_count, child_count);
  341. auto child = maybe_child.release_nonnull();
  342. parent->total_size += child->size;
  343. parent->m_children.append(move(child));
  344. end_insert_rows();
  345. break;
  346. }
  347. case Core::FileWatcherEvent::Type::Deleted:
  348. case Core::FileWatcherEvent::Type::ChildDeleted: {
  349. Node* child = const_cast<Node*>(node_for_path(event.event_path));
  350. if (child == nullptr) {
  351. dbgln("Got a ChildDeleted/Deleted on '{}' but the child does not exist?! (already gone?)", event.event_path);
  352. break;
  353. }
  354. auto index = child->index(0);
  355. begin_delete_rows(index.parent(), index.row(), index.row());
  356. Node* parent = child->m_parent;
  357. parent->m_children.remove(index.row());
  358. end_delete_rows();
  359. for_each_view([&](AbstractView& view) {
  360. view.selection().remove_all_matching([&](auto& selection_index) {
  361. return selection_index.internal_data() == index.internal_data();
  362. });
  363. if (view.cursor_index().internal_data() == index.internal_data()) {
  364. view.set_cursor({}, GUI::AbstractView::SelectionUpdate::None);
  365. }
  366. });
  367. break;
  368. }
  369. case Core::FileWatcherEvent::Type::MetadataModified: {
  370. // FIXME: Do we do anything in case the metadata is modified?
  371. // Perhaps re-stat'ing the modified node would make sense
  372. // here, but let's leave that to when we actually need it.
  373. break;
  374. }
  375. default:
  376. VERIFY_NOT_REACHED();
  377. }
  378. did_update(UpdateFlag::DontInvalidateIndices);
  379. }
  380. int FileSystemModel::row_count(ModelIndex const& index) const
  381. {
  382. Node& node = const_cast<Node&>(this->node(index));
  383. node.reify_if_needed();
  384. if (node.is_directory())
  385. return node.m_children.size();
  386. return 0;
  387. }
  388. FileSystemModel::Node const& FileSystemModel::node(ModelIndex const& index) const
  389. {
  390. if (!index.is_valid())
  391. return *m_root;
  392. VERIFY(index.internal_data());
  393. return *(Node*)index.internal_data();
  394. }
  395. ModelIndex FileSystemModel::index(int row, int column, ModelIndex const& parent) const
  396. {
  397. if (row < 0 || column < 0)
  398. return {};
  399. auto& node = this->node(parent);
  400. const_cast<Node&>(node).reify_if_needed();
  401. if (static_cast<size_t>(row) >= node.m_children.size())
  402. return {};
  403. return create_index(row, column, &node.m_children[row]);
  404. }
  405. ModelIndex FileSystemModel::parent_index(ModelIndex const& index) const
  406. {
  407. if (!index.is_valid())
  408. return {};
  409. auto& node = this->node(index);
  410. if (!node.m_parent) {
  411. VERIFY(&node == m_root);
  412. return {};
  413. }
  414. return node.m_parent->index(index.column());
  415. }
  416. Variant FileSystemModel::data(ModelIndex const& index, ModelRole role) const
  417. {
  418. VERIFY(index.is_valid());
  419. if (role == ModelRole::TextAlignment) {
  420. switch (index.column()) {
  421. case Column::Icon:
  422. return Gfx::TextAlignment::Center;
  423. case Column::Size:
  424. case Column::Inode:
  425. return Gfx::TextAlignment::CenterRight;
  426. case Column::Name:
  427. case Column::User:
  428. case Column::Group:
  429. case Column::ModificationTime:
  430. case Column::Permissions:
  431. case Column::SymlinkTarget:
  432. return Gfx::TextAlignment::CenterLeft;
  433. default:
  434. VERIFY_NOT_REACHED();
  435. }
  436. }
  437. auto& node = this->node(index);
  438. if (role == ModelRole::Custom) {
  439. // For GUI::FileSystemModel, custom role means the full path.
  440. VERIFY(index.column() == Column::Name);
  441. return node.full_path();
  442. }
  443. if (role == ModelRole::MimeData) {
  444. if (index.column() == Column::Name)
  445. return URL::create_with_file_scheme(node.full_path()).serialize();
  446. return {};
  447. }
  448. if (role == ModelRole::Sort) {
  449. switch (index.column()) {
  450. case Column::Icon:
  451. return node.is_directory() ? 0 : 1;
  452. case Column::Name:
  453. // NOTE: The children of a Node are grouped by directory-or-file and then sorted alphabetically.
  454. // Hence, the sort value for the name column is simply the index row. :^)
  455. return index.row();
  456. case Column::Size:
  457. return (int)node.size;
  458. case Column::User:
  459. return name_for_uid(node.uid);
  460. case Column::Group:
  461. return name_for_gid(node.gid);
  462. case Column::Permissions:
  463. return permission_string(node.mode);
  464. case Column::ModificationTime:
  465. return node.mtime;
  466. case Column::Inode:
  467. return (int)node.inode;
  468. case Column::SymlinkTarget:
  469. return node.symlink_target;
  470. }
  471. VERIFY_NOT_REACHED();
  472. }
  473. if (role == ModelRole::Display) {
  474. switch (index.column()) {
  475. case Column::Icon:
  476. return icon_for(node);
  477. case Column::Name:
  478. return node.name;
  479. case Column::Size:
  480. return human_readable_size(node.size);
  481. case Column::User:
  482. return name_for_uid(node.uid);
  483. case Column::Group:
  484. return name_for_gid(node.gid);
  485. case Column::Permissions:
  486. return permission_string(node.mode);
  487. case Column::ModificationTime:
  488. return timestamp_string(node.mtime);
  489. case Column::Inode:
  490. return (int)node.inode;
  491. case Column::SymlinkTarget:
  492. return node.symlink_target;
  493. }
  494. }
  495. if (role == ModelRole::Icon) {
  496. return icon_for(node);
  497. }
  498. if (role == ModelRole::IconOpacity) {
  499. if (node.name.starts_with('.'))
  500. return 0.5f;
  501. return {};
  502. }
  503. return {};
  504. }
  505. Icon FileSystemModel::icon_for(Node const& node) const
  506. {
  507. if (node.full_path() == "/")
  508. return FileIconProvider::icon_for_path("/");
  509. if (Gfx::Bitmap::is_path_a_supported_image_format(node.name)) {
  510. if (!node.thumbnail) {
  511. if (!const_cast<FileSystemModel*>(this)->fetch_thumbnail_for(node))
  512. return FileIconProvider::filetype_image_icon();
  513. }
  514. return GUI::Icon(FileIconProvider::filetype_image_icon().bitmap_for_size(16), *node.thumbnail);
  515. }
  516. if (node.is_directory()) {
  517. if (node.full_path() == Core::StandardPaths::home_directory()) {
  518. if (node.is_selected())
  519. return FileIconProvider::home_directory_open_icon();
  520. return FileIconProvider::home_directory_icon();
  521. }
  522. if (node.full_path() == Core::StandardPaths::desktop_directory())
  523. return FileIconProvider::desktop_directory_icon();
  524. if (node.is_selected() && node.is_accessible_directory)
  525. return FileIconProvider::directory_open_icon();
  526. }
  527. return FileIconProvider::icon_for_path(node.full_path(), node.mode);
  528. }
  529. static HashMap<String, RefPtr<Gfx::Bitmap>> s_thumbnail_cache;
  530. static ErrorOr<NonnullRefPtr<Gfx::Bitmap>> render_thumbnail(StringView path)
  531. {
  532. auto bitmap = TRY(Gfx::Bitmap::try_load_from_file(path));
  533. auto thumbnail = TRY(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { 32, 32 }));
  534. double scale = min(32 / (double)bitmap->width(), 32 / (double)bitmap->height());
  535. auto destination = Gfx::IntRect(0, 0, (int)(bitmap->width() * scale), (int)(bitmap->height() * scale)).centered_within(thumbnail->rect());
  536. Painter painter(thumbnail);
  537. painter.draw_scaled_bitmap(destination, *bitmap, bitmap->rect());
  538. return thumbnail;
  539. }
  540. bool FileSystemModel::fetch_thumbnail_for(Node const& node)
  541. {
  542. // See if we already have the thumbnail
  543. // we're looking for in the cache.
  544. auto path = node.full_path();
  545. auto it = s_thumbnail_cache.find(path);
  546. if (it != s_thumbnail_cache.end()) {
  547. if (!(*it).value)
  548. return false;
  549. node.thumbnail = (*it).value;
  550. return true;
  551. }
  552. // Otherwise, arrange to render the thumbnail
  553. // in background and make it available later.
  554. s_thumbnail_cache.set(path, nullptr);
  555. m_thumbnail_progress_total++;
  556. auto weak_this = make_weak_ptr();
  557. (void)Threading::BackgroundAction<ErrorOr<NonnullRefPtr<Gfx::Bitmap>>>::construct(
  558. [path](auto&) {
  559. return render_thumbnail(path);
  560. },
  561. [this, path, weak_this](auto thumbnail_or_error) {
  562. if (thumbnail_or_error.is_error()) {
  563. s_thumbnail_cache.set(path, nullptr);
  564. dbgln("Failed to load thumbnail for {}: {}", path, thumbnail_or_error.error());
  565. } else {
  566. s_thumbnail_cache.set(path, thumbnail_or_error.release_value());
  567. }
  568. // The model was destroyed, no need to update
  569. // progress or call any event handlers.
  570. if (weak_this.is_null())
  571. return;
  572. m_thumbnail_progress++;
  573. if (on_thumbnail_progress)
  574. on_thumbnail_progress(m_thumbnail_progress, m_thumbnail_progress_total);
  575. if (m_thumbnail_progress == m_thumbnail_progress_total) {
  576. m_thumbnail_progress = 0;
  577. m_thumbnail_progress_total = 0;
  578. }
  579. did_update(UpdateFlag::DontInvalidateIndices);
  580. });
  581. return false;
  582. }
  583. int FileSystemModel::column_count(ModelIndex const&) const
  584. {
  585. return Column::__Count;
  586. }
  587. String FileSystemModel::column_name(int column) const
  588. {
  589. switch (column) {
  590. case Column::Icon:
  591. return "";
  592. case Column::Name:
  593. return "Name";
  594. case Column::Size:
  595. return "Size";
  596. case Column::User:
  597. return "User";
  598. case Column::Group:
  599. return "Group";
  600. case Column::Permissions:
  601. return "Mode";
  602. case Column::ModificationTime:
  603. return "Modified";
  604. case Column::Inode:
  605. return "Inode";
  606. case Column::SymlinkTarget:
  607. return "Symlink target";
  608. }
  609. VERIFY_NOT_REACHED();
  610. }
  611. bool FileSystemModel::accepts_drag(ModelIndex const& index, Vector<String> const& mime_types) const
  612. {
  613. if (!mime_types.contains_slow("text/uri-list"))
  614. return false;
  615. if (!index.is_valid())
  616. return true;
  617. auto& node = this->node(index);
  618. return node.is_directory();
  619. }
  620. void FileSystemModel::set_should_show_dotfiles(bool show)
  621. {
  622. if (m_should_show_dotfiles == show)
  623. return;
  624. m_should_show_dotfiles = show;
  625. // FIXME: add a way to granularly update in this case.
  626. invalidate();
  627. }
  628. bool FileSystemModel::is_editable(ModelIndex const& index) const
  629. {
  630. if (!index.is_valid())
  631. return false;
  632. return index.column() == Column::Name;
  633. }
  634. void FileSystemModel::set_data(ModelIndex const& index, Variant const& data)
  635. {
  636. VERIFY(is_editable(index));
  637. Node& node = const_cast<Node&>(this->node(index));
  638. auto dirname = LexicalPath::dirname(node.full_path());
  639. auto new_full_path = String::formatted("{}/{}", dirname, data.to_string());
  640. int rc = rename(node.full_path().characters(), new_full_path.characters());
  641. if (rc < 0) {
  642. if (on_rename_error)
  643. on_rename_error(errno, strerror(errno));
  644. return;
  645. }
  646. if (on_rename_successful)
  647. on_rename_successful(node.full_path(), new_full_path);
  648. }
  649. Vector<ModelIndex> FileSystemModel::matches(StringView searching, unsigned flags, ModelIndex const& index)
  650. {
  651. Node& node = const_cast<Node&>(this->node(index));
  652. node.reify_if_needed();
  653. Vector<ModelIndex> found_indices;
  654. for (auto& child : node.m_children) {
  655. if (string_matches(child.name, searching, flags)) {
  656. const_cast<Node&>(child).reify_if_needed();
  657. found_indices.append(child.index(Column::Name));
  658. if (flags & FirstMatchOnly)
  659. break;
  660. }
  661. }
  662. return found_indices;
  663. }
  664. }