DirectoryView.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/URL.h>
  28. #include <AK/Vector.h>
  29. #include <LibDesktop/Launcher.h>
  30. #include <LibGUI/Action.h>
  31. #include <LibGUI/ColumnsView.h>
  32. #include <LibGUI/FileSystemModel.h>
  33. #include <LibGUI/IconView.h>
  34. #include <LibGUI/StackWidget.h>
  35. #include <LibGUI/TableView.h>
  36. #include <fcntl.h>
  37. #include <sys/stat.h>
  38. namespace FileManager {
  39. class LauncherHandler : public RefCounted<LauncherHandler> {
  40. public:
  41. LauncherHandler(const NonnullRefPtr<Desktop::Launcher::Details>& details)
  42. : m_details(details)
  43. {
  44. }
  45. NonnullRefPtr<GUI::Action> create_launch_action(Function<void(const LauncherHandler&)>);
  46. const Desktop::Launcher::Details& details() const { return *m_details; }
  47. private:
  48. NonnullRefPtr<Desktop::Launcher::Details> m_details;
  49. };
  50. class DirectoryView final
  51. : public GUI::StackWidget
  52. , private GUI::ModelClient {
  53. C_OBJECT(DirectoryView);
  54. public:
  55. enum class Mode {
  56. Desktop,
  57. Normal,
  58. };
  59. virtual ~DirectoryView() override;
  60. void open(const StringView& path);
  61. String path() const { return model().root_path(); }
  62. void open_parent_directory();
  63. void open_previous_directory();
  64. void open_next_directory();
  65. int path_history_size() const { return m_path_history.size(); }
  66. int path_history_position() const { return m_path_history_position; }
  67. static RefPtr<LauncherHandler> get_default_launch_handler(const NonnullRefPtrVector<LauncherHandler>& handlers);
  68. static NonnullRefPtrVector<LauncherHandler> get_launch_handlers(const URL& url);
  69. static NonnullRefPtrVector<LauncherHandler> get_launch_handlers(const String& path);
  70. void refresh();
  71. void launch(const URL&, const LauncherHandler&) const;
  72. Function<void(const StringView& path, bool can_read_in_path, bool can_write_in_path)> on_path_change;
  73. Function<void(GUI::AbstractView&)> on_selection_change;
  74. Function<void(const GUI::ModelIndex&, const GUI::ContextMenuEvent&)> on_context_menu_request;
  75. Function<void(const StringView&)> on_status_message;
  76. Function<void(int done, int total)> on_thumbnail_progress;
  77. Function<void()> on_accepted_drop;
  78. enum ViewMode {
  79. Invalid,
  80. Table,
  81. Columns,
  82. Icon
  83. };
  84. void set_view_mode(ViewMode);
  85. ViewMode view_mode() const { return m_view_mode; }
  86. GUI::AbstractView& current_view()
  87. {
  88. switch (m_view_mode) {
  89. case ViewMode::Table:
  90. return *m_table_view;
  91. case ViewMode::Columns:
  92. return *m_columns_view;
  93. case ViewMode::Icon:
  94. return *m_icon_view;
  95. default:
  96. VERIFY_NOT_REACHED();
  97. }
  98. }
  99. const GUI::AbstractView& current_view() const
  100. {
  101. return const_cast<DirectoryView*>(this)->current_view();
  102. }
  103. template<typename Callback>
  104. void for_each_view_implementation(Callback callback)
  105. {
  106. if (m_icon_view)
  107. callback(*m_icon_view);
  108. if (m_table_view)
  109. callback(*m_table_view);
  110. if (m_columns_view)
  111. callback(*m_columns_view);
  112. }
  113. void set_should_show_dotfiles(bool);
  114. const GUI::FileSystemModel::Node& node(const GUI::ModelIndex&) const;
  115. bool is_desktop() const { return m_mode == Mode::Desktop; }
  116. Vector<String> selected_file_paths() const;
  117. GUI::Action& mkdir_action() { return *m_mkdir_action; }
  118. GUI::Action& touch_action() { return *m_touch_action; }
  119. GUI::Action& open_terminal_action() { return *m_open_terminal_action; }
  120. GUI::Action& delete_action() { return *m_delete_action; }
  121. GUI::Action& force_delete_action() { return *m_force_delete_action; }
  122. private:
  123. explicit DirectoryView(Mode);
  124. const GUI::FileSystemModel& model() const { return *m_model; }
  125. GUI::FileSystemModel& model() { return *m_model; }
  126. void handle_selection_change();
  127. void handle_drop(const GUI::ModelIndex&, const GUI::DropEvent&);
  128. void do_delete(bool should_confirm);
  129. // ^GUI::ModelClient
  130. virtual void model_did_update(unsigned) override;
  131. void setup_actions();
  132. void setup_model();
  133. void setup_icon_view();
  134. void setup_columns_view();
  135. void setup_table_view();
  136. void handle_activation(const GUI::ModelIndex&);
  137. void set_status_message(const StringView&);
  138. void update_statusbar();
  139. Mode m_mode { Mode::Normal };
  140. ViewMode m_view_mode { Invalid };
  141. NonnullRefPtr<GUI::FileSystemModel> m_model;
  142. NonnullRefPtr<GUI::SortingProxyModel> m_sorting_model;
  143. size_t m_path_history_position { 0 };
  144. Vector<String> m_path_history;
  145. void add_path_to_history(const StringView& path);
  146. RefPtr<GUI::Label> m_error_label;
  147. RefPtr<GUI::TableView> m_table_view;
  148. RefPtr<GUI::IconView> m_icon_view;
  149. RefPtr<GUI::ColumnsView> m_columns_view;
  150. RefPtr<GUI::Action> m_mkdir_action;
  151. RefPtr<GUI::Action> m_touch_action;
  152. RefPtr<GUI::Action> m_open_terminal_action;
  153. RefPtr<GUI::Action> m_delete_action;
  154. RefPtr<GUI::Action> m_force_delete_action;
  155. };
  156. }