DirectoryView.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 <LibGUI/GSortingProxyModel.h>
  29. #include <stdio.h>
  30. #include <unistd.h>
  31. // FIXME: Remove this hackery once printf() supports floats.
  32. static String number_string_with_one_decimal(float number, const char* suffix)
  33. {
  34. float decimals = number - (int)number;
  35. return String::format("%d.%d %s", (int)number, (int)(decimals * 10), suffix);
  36. }
  37. static String human_readable_size(size_t size)
  38. {
  39. if (size < 1 * KB)
  40. return String::format("%zu bytes", size);
  41. if (size < 1 * MB)
  42. return number_string_with_one_decimal((float)size / (float)KB, "KB");
  43. if (size < 1 * GB)
  44. return number_string_with_one_decimal((float)size / (float)MB, "MB");
  45. return number_string_with_one_decimal((float)size / (float)GB, "GB");
  46. }
  47. void DirectoryView::handle_activation(const GModelIndex& index)
  48. {
  49. if (!index.is_valid())
  50. return;
  51. dbgprintf("on activation: %d,%d, this=%p, m_model=%p\n", index.row(), index.column(), this, m_model.ptr());
  52. auto& node = model().node(index);
  53. auto path = node.full_path(model());
  54. if (node.is_directory()) {
  55. open(path);
  56. return;
  57. }
  58. if (node.is_executable()) {
  59. if (fork() == 0) {
  60. int rc = execl(path.characters(), path.characters(), nullptr);
  61. if (rc < 0)
  62. perror("exec");
  63. ASSERT_NOT_REACHED();
  64. }
  65. return;
  66. }
  67. if (path.to_lowercase().ends_with(".png")) {
  68. if (fork() == 0) {
  69. int rc = execl("/bin/qs", "/bin/qs", path.characters(), nullptr);
  70. if (rc < 0)
  71. perror("exec");
  72. ASSERT_NOT_REACHED();
  73. }
  74. return;
  75. }
  76. if (path.to_lowercase().ends_with(".html")) {
  77. if (fork() == 0) {
  78. int rc = execl("/bin/Browser", "/bin/Browser", path.characters(), nullptr);
  79. if (rc < 0)
  80. perror("exec");
  81. ASSERT_NOT_REACHED();
  82. }
  83. return;
  84. }
  85. if (path.to_lowercase().ends_with(".wav")) {
  86. if (fork() == 0) {
  87. int rc = execl("/bin/SoundPlayer", "/bin/SoundPlayer", path.characters(), nullptr);
  88. if (rc < 0)
  89. perror("exec");
  90. ASSERT_NOT_REACHED();
  91. }
  92. return;
  93. }
  94. if (fork() == 0) {
  95. int rc = execl("/bin/TextEditor", "/bin/TextEditor", path.characters(), nullptr);
  96. if (rc < 0)
  97. perror("exec");
  98. ASSERT_NOT_REACHED();
  99. }
  100. };
  101. DirectoryView::DirectoryView(GWidget* parent)
  102. : GStackWidget(parent)
  103. , m_model(GFileSystemModel::create())
  104. {
  105. set_active_widget(nullptr);
  106. m_item_view = GItemView::construct(this);
  107. m_item_view->set_model(model());
  108. m_columns_view = GColumnsView::construct(this);
  109. m_columns_view->set_model(model());
  110. m_table_view = GTableView::construct(this);
  111. m_table_view->set_model(GSortingProxyModel::create(m_model));
  112. m_table_view->model()->set_key_column_and_sort_order(GFileSystemModel::Column::Name, GSortOrder::Ascending);
  113. m_item_view->set_model_column(GFileSystemModel::Column::Name);
  114. m_columns_view->set_model_column(GFileSystemModel::Column::Name);
  115. m_model->on_root_path_change = [this] {
  116. m_table_view->selection().clear();
  117. m_item_view->selection().clear();
  118. if (on_path_change)
  119. on_path_change(model().root_path());
  120. };
  121. // NOTE: We're using the on_update hook on the GSortingProxyModel here instead of
  122. // the GFileSystemModel's hook. This is because GSortingProxyModel has already
  123. // installed an on_update hook on the GFileSystemModel internally.
  124. // FIXME: This is an unfortunate design. We should come up with something better.
  125. m_table_view->model()->on_update = [this] {
  126. for_each_view_implementation([](auto& view) {
  127. view.selection().clear();
  128. });
  129. update_statusbar();
  130. };
  131. m_model->on_thumbnail_progress = [this](int done, int total) {
  132. if (on_thumbnail_progress)
  133. on_thumbnail_progress(done, total);
  134. };
  135. m_item_view->on_activation = [&](const GModelIndex& index) {
  136. handle_activation(index);
  137. };
  138. m_columns_view->on_activation = [&](const GModelIndex& index) {
  139. handle_activation(index);
  140. };
  141. m_table_view->on_activation = [&](auto& index) {
  142. auto& filter_model = (GSortingProxyModel&)*m_table_view->model();
  143. handle_activation(filter_model.map_to_target(index));
  144. };
  145. m_table_view->on_selection_change = [this] {
  146. update_statusbar();
  147. if (on_selection_change)
  148. on_selection_change(*m_table_view);
  149. };
  150. m_item_view->on_selection_change = [this] {
  151. update_statusbar();
  152. if (on_selection_change)
  153. on_selection_change(*m_item_view);
  154. };
  155. m_columns_view->on_selection_change = [this] {
  156. update_statusbar();
  157. if (on_selection_change)
  158. on_selection_change(*m_columns_view);
  159. };
  160. m_table_view->on_context_menu_request = [this](auto& index, auto& event) {
  161. if (on_context_menu_request)
  162. on_context_menu_request(*m_table_view, index, event);
  163. };
  164. m_item_view->on_context_menu_request = [this](auto& index, auto& event) {
  165. if (on_context_menu_request)
  166. on_context_menu_request(*m_item_view, index, event);
  167. };
  168. m_columns_view->on_context_menu_request = [this](auto& index, auto& event) {
  169. if (on_context_menu_request)
  170. on_context_menu_request(*m_columns_view, index, event);
  171. };
  172. set_view_mode(ViewMode::Icon);
  173. }
  174. DirectoryView::~DirectoryView()
  175. {
  176. }
  177. void DirectoryView::set_view_mode(ViewMode mode)
  178. {
  179. if (m_view_mode == mode)
  180. return;
  181. m_view_mode = mode;
  182. update();
  183. if (mode == ViewMode::List) {
  184. set_active_widget(m_table_view);
  185. return;
  186. }
  187. if (mode == ViewMode::Columns) {
  188. set_active_widget(m_columns_view);
  189. return;
  190. }
  191. if (mode == ViewMode::Icon) {
  192. set_active_widget(m_item_view);
  193. return;
  194. }
  195. ASSERT_NOT_REACHED();
  196. }
  197. void DirectoryView::add_path_to_history(const StringView& path)
  198. {
  199. if (m_path_history_position < m_path_history.size())
  200. m_path_history.resize(m_path_history_position + 1);
  201. m_path_history.append(path);
  202. m_path_history_position = m_path_history.size() - 1;
  203. }
  204. void DirectoryView::open(const StringView& path)
  205. {
  206. add_path_to_history(path);
  207. model().set_root_path(path);
  208. }
  209. void DirectoryView::set_status_message(const StringView& message)
  210. {
  211. if (on_status_message)
  212. on_status_message(message);
  213. }
  214. void DirectoryView::open_parent_directory()
  215. {
  216. auto path = String::format("%s/..", model().root_path().characters());
  217. add_path_to_history(path);
  218. model().set_root_path(path);
  219. }
  220. void DirectoryView::refresh()
  221. {
  222. model().update();
  223. }
  224. void DirectoryView::open_previous_directory()
  225. {
  226. if (m_path_history_position > 0) {
  227. m_path_history_position--;
  228. model().set_root_path(m_path_history[m_path_history_position]);
  229. }
  230. }
  231. void DirectoryView::open_next_directory()
  232. {
  233. if (m_path_history_position < m_path_history.size() - 1) {
  234. m_path_history_position++;
  235. model().set_root_path(m_path_history[m_path_history_position]);
  236. }
  237. }
  238. void DirectoryView::update_statusbar()
  239. {
  240. size_t total_size = model().node({}).total_size;
  241. if (current_view().selection().is_empty()) {
  242. set_status_message(String::format("%d item%s (%s)",
  243. model().row_count(),
  244. model().row_count() != 1 ? "s" : "",
  245. human_readable_size(total_size).characters()));
  246. return;
  247. }
  248. int selected_item_count = current_view().selection().size();
  249. size_t selected_byte_count = 0;
  250. current_view().selection().for_each_index([&](auto& index) {
  251. auto& model = *current_view().model();
  252. auto size_index = model.sibling(index.row(), GFileSystemModel::Column::Size, model.parent_index(index));
  253. auto file_size = model.data(size_index).to_i32();
  254. selected_byte_count += file_size;
  255. });
  256. set_status_message(String::format("%d item%s selected (%s)",
  257. selected_item_count,
  258. selected_item_count != 1 ? "s" : "",
  259. human_readable_size(selected_byte_count).characters()));
  260. }