Serenity Operating System
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
27#pragma once
28
29#include <AK/HashMap.h>
30#include <AK/NonnullOwnPtrVector.h>
31#include <LibCore/DateTime.h>
32#include <LibCore/Notifier.h>
33#include <LibGUI/Model.h>
34#include <sys/stat.h>
35#include <time.h>
36
37namespace GUI {
38
39class FileSystemModel
40 : public Model
41 , public Weakable<FileSystemModel> {
42 friend struct Node;
43
44public:
45 enum Mode {
46 Invalid,
47 DirectoriesOnly,
48 FilesAndDirectories
49 };
50
51 enum Column {
52 Icon = 0,
53 Name,
54 Size,
55 Owner,
56 Group,
57 Permissions,
58 ModificationTime,
59 Inode,
60 SymlinkTarget,
61 __Count,
62 };
63
64 struct Node {
65 ~Node() { close(m_watch_fd); }
66
67 String name;
68 String symlink_target;
69 size_t size { 0 };
70 mode_t mode { 0 };
71 uid_t uid { 0 };
72 gid_t gid { 0 };
73 ino_t inode { 0 };
74 time_t mtime { 0 };
75
76 size_t total_size { 0 };
77
78 mutable RefPtr<Gfx::Bitmap> thumbnail;
79 bool is_directory() const { return S_ISDIR(mode); }
80 bool is_executable() const { return mode & (S_IXUSR | S_IXGRP | S_IXOTH); }
81
82 String full_path(const FileSystemModel&) const;
83
84 private:
85 friend class FileSystemModel;
86
87 Node* parent { nullptr };
88 NonnullOwnPtrVector<Node> children;
89 bool has_traversed { false };
90
91 int m_watch_fd { -1 };
92 RefPtr<Core::Notifier> m_notifier;
93
94 ModelIndex index(const FileSystemModel&, int column) const;
95 void traverse_if_needed(const FileSystemModel&);
96 void reify_if_needed(const FileSystemModel&);
97 bool fetch_data(const String& full_path, bool is_root);
98 };
99
100 static NonnullRefPtr<FileSystemModel> create(const StringView& root_path = "/", Mode mode = Mode::FilesAndDirectories)
101 {
102 return adopt(*new FileSystemModel(root_path, mode));
103 }
104 virtual ~FileSystemModel() override;
105
106 String root_path() const { return m_root_path; }
107 void set_root_path(const StringView&);
108 String full_path(const ModelIndex&) const;
109 ModelIndex index(const StringView& path, int column) const;
110
111 const Node& node(const ModelIndex& index) const;
112 GIcon icon_for_file(const mode_t mode, const String& name) const;
113
114 Function<void(int done, int total)> on_thumbnail_progress;
115 Function<void()> on_root_path_change;
116
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
129 static String timestamp_string(time_t timestamp)
130 {
131 return Core::DateTime::from_timestamp(timestamp).to_string();
132 }
133
134private:
135 FileSystemModel(const StringView& root_path, Mode);
136
137 String name_for_uid(uid_t) const;
138 String name_for_gid(gid_t) const;
139
140 HashMap<uid_t, String> m_user_names;
141 HashMap<gid_t, String> m_group_names;
142
143 bool fetch_thumbnail_for(const Node& node);
144 GIcon icon_for(const Node& node) const;
145
146 String m_root_path;
147 Mode m_mode { Invalid };
148 OwnPtr<Node> m_root { nullptr };
149
150 GIcon m_directory_icon;
151 GIcon m_file_icon;
152 GIcon m_symlink_icon;
153 GIcon m_socket_icon;
154 GIcon m_executable_icon;
155 GIcon m_filetype_image_icon;
156 GIcon m_filetype_sound_icon;
157 GIcon m_filetype_html_icon;
158
159 unsigned m_thumbnail_progress { 0 };
160 unsigned m_thumbnail_progress_total { 0 };
161};
162
163}