FileSystemModel.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/DateTime.h>
  30. #include <LibCore/Notifier.h>
  31. #include <LibGUI/Model.h>
  32. #include <string.h>
  33. #include <sys/stat.h>
  34. #include <time.h>
  35. namespace GUI {
  36. class FileSystemModel
  37. : public Model
  38. , public Weakable<FileSystemModel> {
  39. friend struct Node;
  40. public:
  41. enum Mode {
  42. Invalid,
  43. DirectoriesOnly,
  44. FilesAndDirectories
  45. };
  46. enum Column {
  47. Icon = 0,
  48. Name,
  49. Size,
  50. Owner,
  51. Group,
  52. Permissions,
  53. ModificationTime,
  54. Inode,
  55. SymlinkTarget,
  56. __Count,
  57. };
  58. struct Node {
  59. ~Node() { close(m_watch_fd); }
  60. String name;
  61. String symlink_target;
  62. size_t size { 0 };
  63. mode_t mode { 0 };
  64. uid_t uid { 0 };
  65. gid_t gid { 0 };
  66. ino_t inode { 0 };
  67. time_t mtime { 0 };
  68. size_t total_size { 0 };
  69. mutable RefPtr<Gfx::Bitmap> thumbnail;
  70. bool is_directory() const { return S_ISDIR(mode); }
  71. bool is_executable() const { return mode & (S_IXUSR | S_IXGRP | S_IXOTH); }
  72. bool is_selected() const { return m_selected; }
  73. void set_selected(bool selected);
  74. bool has_error() const { return m_error != 0; }
  75. int error() const { return m_error; }
  76. const char* error_string() const { return strerror(m_error); }
  77. String full_path() const;
  78. private:
  79. friend class FileSystemModel;
  80. explicit Node(FileSystemModel& model)
  81. : m_model(model)
  82. {
  83. }
  84. FileSystemModel& m_model;
  85. Node* parent { nullptr };
  86. NonnullOwnPtrVector<Node> children;
  87. bool has_traversed { false };
  88. bool m_selected { false };
  89. int m_watch_fd { -1 };
  90. RefPtr<Core::Notifier> m_notifier;
  91. int m_error { 0 };
  92. ModelIndex index(int column) const;
  93. void traverse_if_needed();
  94. void reify_if_needed();
  95. bool fetch_data(const String& full_path, bool is_root);
  96. };
  97. static NonnullRefPtr<FileSystemModel> create(const StringView& root_path = "/", Mode mode = Mode::FilesAndDirectories)
  98. {
  99. return adopt(*new FileSystemModel(root_path, mode));
  100. }
  101. virtual ~FileSystemModel() override;
  102. String root_path() const { return m_root_path; }
  103. void set_root_path(const StringView&);
  104. String full_path(const ModelIndex&) const;
  105. ModelIndex index(const StringView& path, int column) const;
  106. void update_node_on_selection(const ModelIndex&, const bool);
  107. ModelIndex m_previously_selected_index {};
  108. const Node& node(const ModelIndex& index) const;
  109. Function<void(int done, int total)> on_thumbnail_progress;
  110. Function<void()> on_complete;
  111. Function<void(int error, const char* error_string)> on_error;
  112. virtual int tree_column() const override { return Column::Name; }
  113. virtual int row_count(const ModelIndex& = ModelIndex()) const override;
  114. virtual int column_count(const ModelIndex& = ModelIndex()) const override;
  115. virtual String column_name(int column) const override;
  116. virtual Variant data(const ModelIndex&, ModelRole = ModelRole::Display) const override;
  117. virtual void update() override;
  118. virtual ModelIndex parent_index(const ModelIndex&) const override;
  119. virtual ModelIndex index(int row, int column = 0, const ModelIndex& parent = ModelIndex()) const override;
  120. virtual StringView drag_data_type() const override { return "text/uri-list"; }
  121. virtual bool accepts_drag(const ModelIndex&, const StringView& data_type) override;
  122. virtual bool is_column_sortable(int column_index) const override { return column_index != Column::Icon; }
  123. static String timestamp_string(time_t timestamp)
  124. {
  125. return Core::DateTime::from_timestamp(timestamp).to_string();
  126. }
  127. bool should_show_dotfiles() const { return m_should_show_dotfiles; }
  128. void set_should_show_dotfiles(bool);
  129. private:
  130. FileSystemModel(const StringView& root_path, Mode);
  131. String name_for_uid(uid_t) const;
  132. String name_for_gid(gid_t) const;
  133. HashMap<uid_t, String> m_user_names;
  134. HashMap<gid_t, String> m_group_names;
  135. bool fetch_thumbnail_for(const Node& node);
  136. GUI::Icon icon_for(const Node& node) const;
  137. String m_root_path;
  138. Mode m_mode { Invalid };
  139. OwnPtr<Node> m_root { nullptr };
  140. unsigned m_thumbnail_progress { 0 };
  141. unsigned m_thumbnail_progress_total { 0 };
  142. bool m_should_show_dotfiles { false };
  143. };
  144. }