GFileSystemModel.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #pragma once
  27. #include <AK/HashMap.h>
  28. #include <AK/NonnullOwnPtrVector.h>
  29. #include <LibCore/CNotifier.h>
  30. #include <LibGUI/GModel.h>
  31. #include <sys/stat.h>
  32. #include <time.h>
  33. class GFileSystemModel : public GModel
  34. , public Weakable<GFileSystemModel> {
  35. friend struct Node;
  36. public:
  37. enum Mode {
  38. Invalid,
  39. DirectoriesOnly,
  40. FilesAndDirectories
  41. };
  42. enum Column {
  43. Icon = 0,
  44. Name,
  45. Size,
  46. Owner,
  47. Group,
  48. Permissions,
  49. ModificationTime,
  50. Inode,
  51. __Count,
  52. };
  53. struct Node {
  54. ~Node() { close(m_watch_fd); }
  55. String name;
  56. size_t size { 0 };
  57. mode_t mode { 0 };
  58. uid_t uid { 0 };
  59. gid_t gid { 0 };
  60. ino_t inode { 0 };
  61. time_t mtime { 0 };
  62. size_t total_size { 0 };
  63. mutable RefPtr<GraphicsBitmap> thumbnail;
  64. bool is_directory() const { return S_ISDIR(mode); }
  65. bool is_executable() const { return mode & S_IXUSR; }
  66. String full_path(const GFileSystemModel&) const;
  67. private:
  68. friend class GFileSystemModel;
  69. Node* parent { nullptr };
  70. NonnullOwnPtrVector<Node> children;
  71. bool has_traversed { false };
  72. int m_watch_fd { -1 };
  73. RefPtr<CNotifier> m_notifier;
  74. GModelIndex index(const GFileSystemModel&, int column) const;
  75. void traverse_if_needed(const GFileSystemModel&);
  76. void reify_if_needed(const GFileSystemModel&);
  77. bool fetch_data_using_lstat(const String& full_path);
  78. };
  79. static NonnullRefPtr<GFileSystemModel> create(const StringView& root_path = "/", Mode mode = Mode::FilesAndDirectories)
  80. {
  81. return adopt(*new GFileSystemModel(root_path, mode));
  82. }
  83. virtual ~GFileSystemModel() override;
  84. String root_path() const { return m_root_path; }
  85. void set_root_path(const StringView&);
  86. String full_path(const GModelIndex&) const;
  87. GModelIndex index(const StringView& path, int column) const;
  88. const Node& node(const GModelIndex& index) const;
  89. GIcon icon_for_file(const mode_t mode, const String& name) const;
  90. Function<void(int done, int total)> on_thumbnail_progress;
  91. Function<void()> on_root_path_change;
  92. virtual int tree_column() const { return Column::Name; }
  93. virtual int row_count(const GModelIndex& = GModelIndex()) const override;
  94. virtual int column_count(const GModelIndex& = GModelIndex()) const override;
  95. virtual String column_name(int column) const override;
  96. virtual ColumnMetadata column_metadata(int column) const override;
  97. virtual GVariant data(const GModelIndex&, Role = Role::Display) const override;
  98. virtual void update() override;
  99. virtual GModelIndex parent_index(const GModelIndex&) const override;
  100. virtual GModelIndex index(int row, int column = 0, const GModelIndex& parent = GModelIndex()) const override;
  101. virtual StringView drag_data_type() const override { return "url-list"; }
  102. static String timestamp_string(time_t timestamp)
  103. {
  104. auto* tm = localtime(&timestamp);
  105. return String::format("%4u-%02u-%02u %02u:%02u:%02u",
  106. tm->tm_year + 1900,
  107. tm->tm_mon + 1,
  108. tm->tm_mday,
  109. tm->tm_hour,
  110. tm->tm_min,
  111. tm->tm_sec);
  112. }
  113. private:
  114. GFileSystemModel(const StringView& root_path, Mode);
  115. String name_for_uid(uid_t) const;
  116. String name_for_gid(gid_t) const;
  117. HashMap<uid_t, String> m_user_names;
  118. HashMap<gid_t, String> m_group_names;
  119. bool fetch_thumbnail_for(const Node& node);
  120. GIcon icon_for(const Node& node) const;
  121. String m_root_path;
  122. Mode m_mode { Invalid };
  123. OwnPtr<Node> m_root { nullptr };
  124. GIcon m_directory_icon;
  125. GIcon m_file_icon;
  126. GIcon m_symlink_icon;
  127. GIcon m_socket_icon;
  128. GIcon m_executable_icon;
  129. GIcon m_filetype_image_icon;
  130. GIcon m_filetype_sound_icon;
  131. GIcon m_filetype_html_icon;
  132. unsigned m_thumbnail_progress { 0 };
  133. unsigned m_thumbnail_progress_total { 0 };
  134. };