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/Vector.h>
30#include <LibGUI/ColumnsView.h>
31#include <LibGUI/FileSystemModel.h>
32#include <LibGUI/ItemView.h>
33#include <LibGUI/StackWidget.h>
34#include <LibGUI/TableView.h>
35#include <sys/stat.h>
36
37class DirectoryView final : public GUI::StackWidget {
38 C_OBJECT(DirectoryView)
39public:
40 virtual ~DirectoryView() override;
41
42 void open(const StringView& path);
43 String path() const { return model().root_path(); }
44 void open_parent_directory();
45 void open_previous_directory();
46 void open_next_directory();
47 int path_history_size() const { return m_path_history.size(); }
48 int path_history_position() const { return m_path_history_position; }
49
50 void refresh();
51
52 Function<void(const StringView&)> on_path_change;
53 Function<void(GUI::AbstractView&)> on_selection_change;
54 Function<void(const GUI::AbstractView&, const GUI::ModelIndex&, const GUI::ContextMenuEvent&)> on_context_menu_request;
55 Function<void(const GUI::AbstractView&, const GUI::ModelIndex&, const GUI::DropEvent&)> on_drop;
56 Function<void(const StringView&)> on_status_message;
57 Function<void(int done, int total)> on_thumbnail_progress;
58
59 enum ViewMode {
60 Invalid,
61 List,
62 Columns,
63 Icon
64 };
65 void set_view_mode(ViewMode);
66 ViewMode view_mode() const { return m_view_mode; }
67
68 GUI::AbstractView& current_view()
69 {
70 switch (m_view_mode) {
71 case ViewMode::List:
72 return *m_table_view;
73 case ViewMode::Columns:
74 return *m_columns_view;
75 case ViewMode::Icon:
76 return *m_item_view;
77 default:
78 ASSERT_NOT_REACHED();
79 }
80 }
81
82 template<typename Callback>
83 void for_each_view_implementation(Callback callback)
84 {
85 callback(*m_table_view);
86 callback(*m_item_view);
87 callback(*m_columns_view);
88 }
89
90 GUI::FileSystemModel& model() { return *m_model; }
91
92private:
93 DirectoryView();
94 const GUI::FileSystemModel& model() const { return *m_model; }
95
96 void handle_activation(const GUI::ModelIndex&);
97
98 void set_status_message(const StringView&);
99 void update_statusbar();
100
101 ViewMode m_view_mode { Invalid };
102
103 NonnullRefPtr<GUI::FileSystemModel> m_model;
104 size_t m_path_history_position { 0 };
105 Vector<String> m_path_history;
106 void add_path_to_history(const StringView& path);
107
108 RefPtr<GUI::TableView> m_table_view;
109 RefPtr<GUI::ItemView> m_item_view;
110 RefPtr<GUI::ColumnsView> m_columns_view;
111};