FileSystemModel.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #define ENUMERATE_FILETYPES \
  36. __ENUMERATE_FILETYPE(cplusplus, ".cpp") \
  37. __ENUMERATE_FILETYPE(header, ".h") \
  38. __ENUMERATE_FILETYPE(html, ".html") \
  39. __ENUMERATE_FILETYPE(image, ".png") \
  40. __ENUMERATE_FILETYPE(java, ".java") \
  41. __ENUMERATE_FILETYPE(javascript, ".js") \
  42. __ENUMERATE_FILETYPE(library, ".so", ".a") \
  43. __ENUMERATE_FILETYPE(object, ".o", ".obj") \
  44. __ENUMERATE_FILETYPE(pdf, ".pdf") \
  45. __ENUMERATE_FILETYPE(python, ".py") \
  46. __ENUMERATE_FILETYPE(sound, ".wav") \
  47. __ENUMERATE_FILETYPE(ini, ".ini") \
  48. __ENUMERATE_FILETYPE(text, ".txt", ".md")
  49. namespace GUI {
  50. class FileSystemModel
  51. : public Model
  52. , public Weakable<FileSystemModel> {
  53. friend struct Node;
  54. public:
  55. enum Mode {
  56. Invalid,
  57. DirectoriesOnly,
  58. FilesAndDirectories
  59. };
  60. enum Column {
  61. Icon = 0,
  62. Name,
  63. Size,
  64. Owner,
  65. Group,
  66. Permissions,
  67. ModificationTime,
  68. Inode,
  69. SymlinkTarget,
  70. __Count,
  71. };
  72. struct Node {
  73. ~Node() { close(m_watch_fd); }
  74. String name;
  75. String symlink_target;
  76. size_t size { 0 };
  77. mode_t mode { 0 };
  78. uid_t uid { 0 };
  79. gid_t gid { 0 };
  80. ino_t inode { 0 };
  81. time_t mtime { 0 };
  82. size_t total_size { 0 };
  83. mutable RefPtr<Gfx::Bitmap> thumbnail;
  84. bool is_directory() const { return S_ISDIR(mode); }
  85. bool is_executable() const { return mode & (S_IXUSR | S_IXGRP | S_IXOTH); }
  86. bool has_error() const { return m_error != 0; }
  87. int error() const { return m_error; }
  88. const char* error_string() const { return strerror(m_error); }
  89. String full_path(const FileSystemModel&) const;
  90. private:
  91. friend class FileSystemModel;
  92. Node* parent { nullptr };
  93. NonnullOwnPtrVector<Node> children;
  94. bool has_traversed { false };
  95. int m_watch_fd { -1 };
  96. RefPtr<Core::Notifier> m_notifier;
  97. int m_error { 0 };
  98. ModelIndex index(const FileSystemModel&, int column) const;
  99. void traverse_if_needed(const FileSystemModel&);
  100. void reify_if_needed(const FileSystemModel&);
  101. bool fetch_data(const String& full_path, bool is_root);
  102. };
  103. static NonnullRefPtr<FileSystemModel> create(const StringView& root_path = "/", Mode mode = Mode::FilesAndDirectories)
  104. {
  105. return adopt(*new FileSystemModel(root_path, mode));
  106. }
  107. virtual ~FileSystemModel() override;
  108. String root_path() const { return m_root_path; }
  109. void set_root_path(const StringView&);
  110. String full_path(const ModelIndex&) const;
  111. ModelIndex index(const StringView& path, int column) const;
  112. const Node& node(const ModelIndex& index) const;
  113. GUI::Icon icon_for_file(const mode_t mode, const String& name) const;
  114. Function<void(int done, int total)> on_thumbnail_progress;
  115. Function<void()> on_complete;
  116. Function<void(int error, const char* error_string)> on_error;
  117. virtual int tree_column() const override { return Column::Name; }
  118. virtual int row_count(const ModelIndex& = ModelIndex()) const override;
  119. virtual int column_count(const ModelIndex& = ModelIndex()) const override;
  120. virtual String column_name(int column) const override;
  121. virtual ColumnMetadata column_metadata(int column) const override;
  122. virtual Variant data(const ModelIndex&, Role = Role::Display) const override;
  123. virtual void update() override;
  124. virtual ModelIndex parent_index(const ModelIndex&) const override;
  125. virtual ModelIndex index(int row, int column = 0, const ModelIndex& parent = ModelIndex()) const override;
  126. virtual StringView drag_data_type() const override { return "text/uri-list"; }
  127. virtual bool accepts_drag(const ModelIndex&, const StringView& data_type) override;
  128. static String timestamp_string(time_t timestamp)
  129. {
  130. return Core::DateTime::from_timestamp(timestamp).to_string();
  131. }
  132. private:
  133. FileSystemModel(const StringView& root_path, Mode);
  134. String name_for_uid(uid_t) const;
  135. String name_for_gid(gid_t) const;
  136. HashMap<uid_t, String> m_user_names;
  137. HashMap<gid_t, String> m_group_names;
  138. bool fetch_thumbnail_for(const Node& node);
  139. GUI::Icon icon_for(const Node& node) const;
  140. String m_root_path;
  141. Mode m_mode { Invalid };
  142. OwnPtr<Node> m_root { nullptr };
  143. GUI::Icon m_directory_icon;
  144. GUI::Icon m_file_icon;
  145. GUI::Icon m_symlink_icon;
  146. GUI::Icon m_socket_icon;
  147. GUI::Icon m_executable_icon;
  148. #define __ENUMERATE_FILETYPE(filetype_name, ...) \
  149. GUI::Icon m_filetype_##filetype_name##_icon;
  150. ENUMERATE_FILETYPES
  151. #undef __ENUMERATE_FILETYPE
  152. unsigned m_thumbnail_progress { 0 };
  153. unsigned m_thumbnail_progress_total { 0 };
  154. };
  155. }