DirectoryView.h 6.3 KB

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