diff options
author | Sergey Bugaev <bugaevc@gmail.com> | 2020-01-10 18:58:00 +0300 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2020-01-10 17:45:59 +0100 |
commit | fdeb91e000f57407334c7cfdd11ccd7c97dd51c6 (patch) | |
tree | 1e68a3045aebeb9e3bd04ea2eac5298651540b6d /Libraries/LibGUI/GFileSystemModel.h | |
parent | 0f18a16e2c855a99e5c261793a8d36565b410a72 (diff) | |
download | serenity-fdeb91e000f57407334c7cfdd11ccd7c97dd51c6.zip |
LibGUI+FileManager: Merge GDirectoryModel into GFileSystemModel
We used to have two different models for displaying file system contents:
the FileManager-grade table-like directory model, which exposed rich data
(such as file icons with integrated image previews) about contents of a
single directory, and the tree-like GFileSystemModel, which only exposed
a tree of file names with very basic info about them.
This commit unifies the two. The new GFileSystemModel can be used both as a
tree-like and as a table-like model, or in fact in both ways simultaneously.
It exposes rich data about a file system subtree rooted at the given root.
The users of the two previous models are all ported to use this new model.
Diffstat (limited to 'Libraries/LibGUI/GFileSystemModel.h')
-rw-r--r-- | Libraries/LibGUI/GFileSystemModel.h | 109 |
1 files changed, 99 insertions, 10 deletions
diff --git a/Libraries/LibGUI/GFileSystemModel.h b/Libraries/LibGUI/GFileSystemModel.h index 8e4e481ec8..3919193017 100644 --- a/Libraries/LibGUI/GFileSystemModel.h +++ b/Libraries/LibGUI/GFileSystemModel.h @@ -1,9 +1,15 @@ #pragma once +#include <AK/HashMap.h> +#include <AK/NonnullOwnPtrVector.h> +#include <LibCore/CNotifier.h> #include <LibGUI/GModel.h> +#include <sys/stat.h> +#include <time.h> -class GFileSystemModel : public GModel { - friend class Node; +class GFileSystemModel : public GModel + , public Weakable<GFileSystemModel> { + friend struct Node; public: enum Mode { @@ -12,6 +18,53 @@ public: FilesAndDirectories }; + enum Column { + Icon = 0, + Name, + Size, + Owner, + Group, + Permissions, + ModificationTime, + Inode, + __Count, + }; + + struct Node { + ~Node() { close(m_watch_fd); } + + String name; + size_t size { 0 }; + mode_t mode { 0 }; + uid_t uid { 0 }; + gid_t gid { 0 }; + ino_t inode { 0 }; + time_t mtime { 0 }; + + size_t total_size { 0 }; + + mutable RefPtr<GraphicsBitmap> thumbnail; + bool is_directory() const { return S_ISDIR(mode); } + bool is_executable() const { return mode & S_IXUSR; } + + String full_path(const GFileSystemModel&) const; + + private: + friend class GFileSystemModel; + + Node* parent { nullptr }; + NonnullOwnPtrVector<Node> children; + bool has_traversed { false }; + + int m_watch_fd { -1 }; + RefPtr<CNotifier> m_notifier; + + GModelIndex index(const GFileSystemModel&, int column) const; + void traverse_if_needed(const GFileSystemModel&); + void reify_if_needed(const GFileSystemModel&); + bool fetch_data_using_lstat(const String& full_path); + }; + static NonnullRefPtr<GFileSystemModel> create(const StringView& root_path = "/", Mode mode = Mode::FilesAndDirectories) { return adopt(*new GFileSystemModel(root_path, mode)); @@ -19,27 +72,63 @@ public: virtual ~GFileSystemModel() override; String root_path() const { return m_root_path; } - String path(const GModelIndex&) const; - GModelIndex index(const StringView& path) const; + void set_root_path(const StringView&); + String full_path(const GModelIndex&) const; + GModelIndex index(const StringView& path, int column) const; + + const Node& node(const GModelIndex& index) const; + GIcon icon_for_file(const mode_t mode, const String& name) const; + + Function<void(int done, int total)> on_thumbnail_progress; + Function<void()> on_root_path_change; + virtual int tree_column() const { return Column::Name; } virtual int row_count(const GModelIndex& = GModelIndex()) const override; virtual int column_count(const GModelIndex& = GModelIndex()) const override; + virtual String column_name(int column) const override; + virtual ColumnMetadata column_metadata(int column) const override; virtual GVariant data(const GModelIndex&, Role = Role::Display) const override; virtual void update() override; virtual GModelIndex parent_index(const GModelIndex&) const override; virtual GModelIndex index(int row, int column = 0, const GModelIndex& parent = GModelIndex()) const override; + static String timestamp_string(time_t timestamp) + { + auto* tm = localtime(×tamp); + return String::format("%4u-%02u-%02u %02u:%02u:%02u", + tm->tm_year + 1900, + tm->tm_mon + 1, + tm->tm_mday, + tm->tm_hour, + tm->tm_min, + tm->tm_sec); + } + private: GFileSystemModel(const StringView& root_path, Mode); + String name_for_uid(uid_t) const; + String name_for_gid(gid_t) const; + + HashMap<uid_t, String> m_user_names; + HashMap<gid_t, String> m_group_names; + + bool fetch_thumbnail_for(const Node& node); + GIcon icon_for(const Node& node) const; + String m_root_path; Mode m_mode { Invalid }; + OwnPtr<Node> m_root { nullptr }; - struct Node; - Node* m_root { nullptr }; - void cleanup(); - - GIcon m_open_folder_icon; - GIcon m_closed_folder_icon; + GIcon m_directory_icon; GIcon m_file_icon; + GIcon m_symlink_icon; + GIcon m_socket_icon; + GIcon m_executable_icon; + GIcon m_filetype_image_icon; + GIcon m_filetype_sound_icon; + GIcon m_filetype_html_icon; + + unsigned m_thumbnail_progress { 0 }; + unsigned m_thumbnail_progress_total { 0 }; }; |