FileSystemModel.cpp 22 KB

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