FileSystemModel.cpp 22 KB

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