DirectoryView.cpp 10 KB

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