DirectoryView.cpp 9.7 KB

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