summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/FileSystemModel.h
blob: 3120a5de3039e0b042283c5f05b42ede167235ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/HashMap.h>
#include <LibCore/DateTime.h>
#include <LibCore/FileWatcher.h>
#include <LibGUI/Model.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>

namespace GUI {

class FileSystemModel
    : public Model
    , public Weakable<FileSystemModel> {
    friend struct Node;

public:
    enum Mode {
        Invalid,
        DirectoriesOnly,
        FilesAndDirectories
    };

    enum Column {
        Icon = 0,
        Name,
        Size,
        User,
        Group,
        Permissions,
        ModificationTime,
        Inode,
        SymlinkTarget,
        __Count,
    };

    struct Node {
        ~Node() = default;

        DeprecatedString name;
        DeprecatedString symlink_target;
        size_t size { 0 };
        mode_t mode { 0 };
        uid_t uid { 0 };
        gid_t gid { 0 };
        ino_t inode { 0 };
        time_t mtime { 0 };
        bool is_accessible_directory { false };

        size_t total_size { 0 };

        mutable RefPtr<Gfx::Bitmap> thumbnail;
        bool is_directory() const { return S_ISDIR(mode); }
        bool is_symlink_to_directory() const;
        bool is_executable() const { return mode & (S_IXUSR | S_IXGRP | S_IXOTH); }

        bool is_selected() const { return m_selected; }
        void set_selected(bool selected);

        bool has_error() const { return m_error != 0; }
        int error() const { return m_error; }
        char const* error_string() const { return strerror(m_error); }

        DeprecatedString full_path() const;

    private:
        friend class FileSystemModel;

        explicit Node(FileSystemModel& model)
            : m_model(model)
        {
        }

        FileSystemModel& m_model;

        Node* m_parent { nullptr };
        Vector<NonnullOwnPtr<Node>> m_children;
        bool m_has_traversed { false };

        bool m_selected { false };

        int m_error { 0 };
        bool m_parent_of_root { false };

        ModelIndex index(int column) const;
        void traverse_if_needed();
        void reify_if_needed();
        bool fetch_data(DeprecatedString const& full_path, bool is_root);

        OwnPtr<Node> create_child(DeprecatedString const& child_name);
    };

    static NonnullRefPtr<FileSystemModel> create(DeprecatedString root_path = "/", Mode mode = Mode::FilesAndDirectories)
    {
        return adopt_ref(*new FileSystemModel(root_path, mode));
    }
    virtual ~FileSystemModel() override = default;

    DeprecatedString root_path() const { return m_root_path; }
    void set_root_path(DeprecatedString);
    DeprecatedString full_path(ModelIndex const&) const;
    ModelIndex index(DeprecatedString path, int column) const;

    void update_node_on_selection(ModelIndex const&, bool const);
    ModelIndex m_previously_selected_index {};

    Node const& node(ModelIndex const& index) const;

    Function<void(int done, int total)> on_thumbnail_progress;
    Function<void()> on_complete;
    Function<void(int error, char const* error_string)> on_directory_change_error;
    Function<void(int error, char const* error_string)> on_rename_error;
    Function<void(DeprecatedString const& old_name, DeprecatedString const& new_name)> on_rename_successful;
    Function<void()> on_root_path_removed;

    virtual int tree_column() const override { return Column::Name; }
    virtual int row_count(ModelIndex const& = ModelIndex()) const override;
    virtual int column_count(ModelIndex const& = ModelIndex()) const override;
    virtual DeprecatedString column_name(int column) const override;
    virtual Variant data(ModelIndex const&, ModelRole = ModelRole::Display) const override;
    virtual ModelIndex parent_index(ModelIndex const&) const override;
    virtual ModelIndex index(int row, int column = 0, ModelIndex const& parent = ModelIndex()) const override;
    virtual StringView drag_data_type() const override { return "text/uri-list"sv; }
    virtual bool accepts_drag(ModelIndex const&, Vector<DeprecatedString> const& mime_types) const override;
    virtual bool is_column_sortable(int column_index) const override { return column_index != Column::Icon; }
    virtual bool is_editable(ModelIndex const&) const override;
    virtual bool is_searchable() const override { return true; }
    virtual void set_data(ModelIndex const&, Variant const&) override;
    virtual Vector<ModelIndex> matches(StringView, unsigned = MatchesFlag::AllMatching, ModelIndex const& = ModelIndex()) override;
    virtual void invalidate() override;

    static DeprecatedString timestamp_string(time_t timestamp)
    {
        return Core::DateTime::from_timestamp(timestamp).to_deprecated_string();
    }

    bool should_show_dotfiles() const { return m_should_show_dotfiles; }
    void set_should_show_dotfiles(bool);

    Optional<Vector<DeprecatedString>> allowed_file_extensions() const { return m_allowed_file_extensions; }
    void set_allowed_file_extensions(Optional<Vector<DeprecatedString>> const& allowed_file_extensions);

private:
    FileSystemModel(DeprecatedString root_path, Mode);

    DeprecatedString name_for_uid(uid_t) const;
    DeprecatedString name_for_gid(gid_t) const;

    Optional<Node const&> node_for_path(DeprecatedString const&) const;

    HashMap<uid_t, DeprecatedString> m_user_names;
    HashMap<gid_t, DeprecatedString> m_group_names;

    bool fetch_thumbnail_for(Node const& node);
    GUI::Icon icon_for(Node const& node) const;

    void handle_file_event(Core::FileWatcherEvent const& event);

    DeprecatedString m_root_path;
    Mode m_mode { Invalid };
    OwnPtr<Node> m_root { nullptr };

    unsigned m_thumbnail_progress { 0 };
    unsigned m_thumbnail_progress_total { 0 };

    Optional<Vector<DeprecatedString>> m_allowed_file_extensions;

    bool m_should_show_dotfiles { false };

    RefPtr<Core::FileWatcher> m_file_watcher;
};

}