DirectoryView.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. #include "DirectoryView.h"
  27. #include <AK/FileSystemPath.h>
  28. #include <AK/NumberFormat.h>
  29. #include <AK/StringBuilder.h>
  30. #include <AK/URL.h>
  31. #include <LibCore/DesktopServices.h>
  32. #include <LibGUI/SortingProxyModel.h>
  33. #include <stdio.h>
  34. #include <unistd.h>
  35. void DirectoryView::handle_activation(const GUI::ModelIndex& index)
  36. {
  37. if (!index.is_valid())
  38. return;
  39. dbgprintf("on activation: %d,%d, this=%p, m_model=%p\n", index.row(), index.column(), this, m_model.ptr());
  40. auto& node = model().node(index);
  41. auto path = node.full_path(model());
  42. struct stat st;
  43. if (stat(path.characters(), &st) < 0) {
  44. perror("stat");
  45. return;
  46. }
  47. if (S_ISDIR(st.st_mode)) {
  48. open(path);
  49. return;
  50. }
  51. Core::DesktopServices::open(URL::create_with_file_protocol(path));
  52. }
  53. DirectoryView::DirectoryView()
  54. : m_model(GUI::FileSystemModel::create())
  55. {
  56. set_active_widget(nullptr);
  57. set_content_margins({ 2, 2, 2, 2 });
  58. m_icon_view = add<GUI::IconView>();
  59. m_icon_view->set_model(model());
  60. m_columns_view = add<GUI::ColumnsView>();
  61. m_columns_view->set_model(model());
  62. m_table_view = add<GUI::TableView>();
  63. m_table_view->set_model(GUI::SortingProxyModel::create(m_model));
  64. m_table_view->model()->set_key_column_and_sort_order(GUI::FileSystemModel::Column::Name, GUI::SortOrder::Ascending);
  65. m_icon_view->set_model_column(GUI::FileSystemModel::Column::Name);
  66. m_columns_view->set_model_column(GUI::FileSystemModel::Column::Name);
  67. m_model->on_error = [this](int error, const char* error_string) {
  68. bool quit = false;
  69. if (m_path_history.size())
  70. open(m_path_history.at(m_path_history_position));
  71. else
  72. quit = true;
  73. if (on_error)
  74. on_error(error, error_string, quit);
  75. };
  76. m_model->on_complete = [this] {
  77. m_table_view->selection().clear();
  78. m_icon_view->selection().clear();
  79. add_path_to_history(model().root_path());
  80. if (on_path_change)
  81. on_path_change(model().root_path());
  82. };
  83. // NOTE: We're using the on_update hook on the GUI::SortingProxyModel here instead of
  84. // the GUI::FileSystemModel's hook. This is because GUI::SortingProxyModel has already
  85. // installed an on_update hook on the GUI::FileSystemModel internally.
  86. // FIXME: This is an unfortunate design. We should come up with something better.
  87. m_table_view->model()->on_update = [this] {
  88. for_each_view_implementation([](auto& view) {
  89. view.selection().clear();
  90. });
  91. update_statusbar();
  92. };
  93. m_model->on_thumbnail_progress = [this](int done, int total) {
  94. if (on_thumbnail_progress)
  95. on_thumbnail_progress(done, total);
  96. };
  97. m_icon_view->on_activation = [&](const GUI::ModelIndex& index) {
  98. handle_activation(index);
  99. };
  100. m_columns_view->on_activation = [&](const GUI::ModelIndex& index) {
  101. handle_activation(index);
  102. };
  103. m_table_view->on_activation = [&](auto& index) {
  104. auto& filter_model = (GUI::SortingProxyModel&)*m_table_view->model();
  105. handle_activation(filter_model.map_to_target(index));
  106. };
  107. m_table_view->on_selection_change = [this] {
  108. update_statusbar();
  109. if (on_selection_change)
  110. on_selection_change(*m_table_view);
  111. };
  112. m_icon_view->on_selection_change = [this] {
  113. update_statusbar();
  114. if (on_selection_change)
  115. on_selection_change(*m_icon_view);
  116. };
  117. m_columns_view->on_selection_change = [this] {
  118. update_statusbar();
  119. if (on_selection_change)
  120. on_selection_change(*m_columns_view);
  121. };
  122. m_table_view->on_context_menu_request = [this](auto& index, auto& event) {
  123. if (on_context_menu_request)
  124. on_context_menu_request(*m_table_view, index, event);
  125. };
  126. m_icon_view->on_context_menu_request = [this](auto& index, auto& event) {
  127. if (on_context_menu_request)
  128. on_context_menu_request(*m_icon_view, index, event);
  129. };
  130. m_columns_view->on_context_menu_request = [this](auto& index, auto& event) {
  131. if (on_context_menu_request)
  132. on_context_menu_request(*m_columns_view, index, event);
  133. };
  134. m_table_view->on_drop = [this](auto& index, auto& event) {
  135. if (on_drop)
  136. on_drop(*m_table_view, index, event);
  137. };
  138. m_icon_view->on_drop = [this](auto& index, auto& event) {
  139. if (on_drop)
  140. on_drop(*m_icon_view, index, event);
  141. };
  142. m_columns_view->on_drop = [this](auto& index, auto& event) {
  143. if (on_drop)
  144. on_drop(*m_columns_view, index, event);
  145. };
  146. set_view_mode(ViewMode::Icon);
  147. }
  148. DirectoryView::~DirectoryView()
  149. {
  150. }
  151. void DirectoryView::set_view_mode(ViewMode mode)
  152. {
  153. if (m_view_mode == mode)
  154. return;
  155. m_view_mode = mode;
  156. update();
  157. if (mode == ViewMode::Table) {
  158. set_active_widget(m_table_view);
  159. return;
  160. }
  161. if (mode == ViewMode::Columns) {
  162. set_active_widget(m_columns_view);
  163. return;
  164. }
  165. if (mode == ViewMode::Icon) {
  166. set_active_widget(m_icon_view);
  167. return;
  168. }
  169. ASSERT_NOT_REACHED();
  170. }
  171. void DirectoryView::add_path_to_history(const StringView& path)
  172. {
  173. if (m_path_history.size() && m_path_history.at(m_path_history_position) == path)
  174. return;
  175. if (m_path_history_position < m_path_history.size())
  176. m_path_history.resize(m_path_history_position + 1);
  177. m_path_history.append(path);
  178. m_path_history_position = m_path_history.size() - 1;
  179. }
  180. void DirectoryView::open(const StringView& path)
  181. {
  182. if (model().root_path() == path) {
  183. model().update();
  184. return;
  185. }
  186. model().set_root_path(path);
  187. }
  188. void DirectoryView::set_status_message(const StringView& message)
  189. {
  190. if (on_status_message)
  191. on_status_message(message);
  192. }
  193. void DirectoryView::open_parent_directory()
  194. {
  195. auto path = String::format("%s/..", model().root_path().characters());
  196. model().set_root_path(path);
  197. }
  198. void DirectoryView::refresh()
  199. {
  200. model().update();
  201. }
  202. void DirectoryView::open_previous_directory()
  203. {
  204. if (m_path_history_position > 0) {
  205. m_path_history_position--;
  206. model().set_root_path(m_path_history[m_path_history_position]);
  207. }
  208. }
  209. void DirectoryView::open_next_directory()
  210. {
  211. if (m_path_history_position < m_path_history.size() - 1) {
  212. m_path_history_position++;
  213. model().set_root_path(m_path_history[m_path_history_position]);
  214. }
  215. }
  216. void DirectoryView::update_statusbar()
  217. {
  218. size_t total_size = model().node({}).total_size;
  219. if (current_view().selection().is_empty()) {
  220. set_status_message(String::format("%d item%s (%s)",
  221. model().row_count(),
  222. model().row_count() != 1 ? "s" : "",
  223. human_readable_size(total_size).characters()));
  224. return;
  225. }
  226. int selected_item_count = current_view().selection().size();
  227. size_t selected_byte_count = 0;
  228. current_view().selection().for_each_index([&](auto& index) {
  229. auto& model = *current_view().model();
  230. auto size_index = model.sibling(index.row(), GUI::FileSystemModel::Column::Size, model.parent_index(index));
  231. auto file_size = model.data(size_index).to_i32();
  232. selected_byte_count += file_size;
  233. });
  234. StringBuilder builder;
  235. builder.append(String::number(selected_item_count));
  236. builder.append(" item");
  237. if (selected_item_count != 1)
  238. builder.append('s');
  239. builder.append(" selected (");
  240. builder.append(human_readable_size(selected_byte_count).characters());
  241. builder.append(')');
  242. if (selected_item_count == 1) {
  243. auto index = current_view().selection().first();
  244. // FIXME: This is disgusting. This code should not even be aware that there is a GUI::SortingProxyModel in the table view.
  245. if (m_view_mode == ViewMode::Table) {
  246. auto& filter_model = (GUI::SortingProxyModel&)*m_table_view->model();
  247. index = filter_model.map_to_target(index);
  248. }
  249. auto& node = model().node(index);
  250. if (!node.symlink_target.is_empty()) {
  251. builder.append(" -> ");
  252. builder.append(node.symlink_target);
  253. }
  254. }
  255. set_status_message(builder.to_string());
  256. }