DirectoryView.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "DirectoryView.h"
  2. #include <AK/FileSystemPath.h>
  3. #include <LibGUI/GSortingProxyModel.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. // FIXME: Remove this hackery once printf() supports floats.
  7. static String number_string_with_one_decimal(float number, const char* suffix)
  8. {
  9. float decimals = number - (int)number;
  10. return String::format("%d.%d %s", (int)number, (int)(decimals * 10), suffix);
  11. }
  12. static String human_readable_size(size_t size)
  13. {
  14. if (size < 1 * KB)
  15. return String::format("%zu bytes", size);
  16. if (size < 1 * MB)
  17. return number_string_with_one_decimal((float)size / (float)KB, "KB");
  18. if (size < 1 * GB)
  19. return number_string_with_one_decimal((float)size / (float)MB, "MB");
  20. return number_string_with_one_decimal((float)size / (float)GB, "GB");
  21. }
  22. void DirectoryView::handle_activation(const GModelIndex& index)
  23. {
  24. if (!index.is_valid())
  25. return;
  26. dbgprintf("on activation: %d,%d, this=%p, m_model=%p\n", index.row(), index.column(), this, m_model.ptr());
  27. auto& entry = model().entry(index.row());
  28. auto path = canonicalized_path(String::format("%s/%s", model().path().characters(), entry.name.characters()));
  29. if (entry.is_directory()) {
  30. open(path);
  31. return;
  32. }
  33. if (entry.is_executable()) {
  34. if (fork() == 0) {
  35. int rc = execl(path.characters(), path.characters(), nullptr);
  36. if (rc < 0)
  37. perror("exec");
  38. ASSERT_NOT_REACHED();
  39. }
  40. return;
  41. }
  42. if (path.to_lowercase().ends_with(".png")) {
  43. if (fork() == 0) {
  44. int rc = execl("/bin/qs", "/bin/qs", path.characters(), nullptr);
  45. if (rc < 0)
  46. perror("exec");
  47. ASSERT_NOT_REACHED();
  48. }
  49. return;
  50. }
  51. if (path.to_lowercase().ends_with(".wav")) {
  52. if (fork() == 0) {
  53. int rc = execl("/bin/SoundPlayer", "/bin/SoundPlayer", path.characters(), nullptr);
  54. if (rc < 0)
  55. perror("exec");
  56. ASSERT_NOT_REACHED();
  57. }
  58. return;
  59. }
  60. if (fork() == 0) {
  61. int rc = execl("/bin/TextEditor", "/bin/TextEditor", path.characters(), nullptr);
  62. if (rc < 0)
  63. perror("exec");
  64. ASSERT_NOT_REACHED();
  65. }
  66. };
  67. DirectoryView::DirectoryView(GWidget* parent)
  68. : GStackWidget(parent)
  69. , m_model(GDirectoryModel::create())
  70. {
  71. set_active_widget(nullptr);
  72. m_item_view = GItemView::construct(this);
  73. m_item_view->set_model(model());
  74. m_table_view = GTableView::construct(this);
  75. m_table_view->set_model(GSortingProxyModel::create(m_model));
  76. m_table_view->model()->set_key_column_and_sort_order(GDirectoryModel::Column::Name, GSortOrder::Ascending);
  77. m_item_view->set_model_column(GDirectoryModel::Column::Name);
  78. m_model->on_path_change = [this] {
  79. m_table_view->selection().clear();
  80. m_item_view->selection().clear();
  81. if (on_path_change)
  82. on_path_change(model().path());
  83. };
  84. // NOTE: We're using the on_update hook on the GSortingProxyModel here instead of
  85. // the GDirectoryModel's hook. This is because GSortingProxyModel has already
  86. // installed an on_update hook on the GDirectoryModel internally.
  87. // FIXME: This is an unfortunate design. We should come up with something better.
  88. m_table_view->model()->on_update = [this] {
  89. update_statusbar();
  90. };
  91. m_model->on_thumbnail_progress = [this](int done, int total) {
  92. if (on_thumbnail_progress)
  93. on_thumbnail_progress(done, total);
  94. };
  95. m_item_view->on_activation = [&](const GModelIndex& index) {
  96. handle_activation(index);
  97. };
  98. m_table_view->on_activation = [&](auto& index) {
  99. auto& filter_model = (GSortingProxyModel&)*m_table_view->model();
  100. handle_activation(filter_model.map_to_target(index));
  101. };
  102. m_table_view->on_selection_change = [this] {
  103. update_statusbar();
  104. if (on_selection_change)
  105. on_selection_change(*m_table_view);
  106. };
  107. m_item_view->on_selection_change = [this] {
  108. update_statusbar();
  109. if (on_selection_change)
  110. on_selection_change(*m_item_view);
  111. };
  112. m_table_view->on_context_menu_request = [this](auto& index, auto& event) {
  113. if (on_context_menu_request)
  114. on_context_menu_request(*m_table_view, index, event);
  115. };
  116. m_item_view->on_context_menu_request = [this](auto& index, auto& event) {
  117. if (on_context_menu_request)
  118. on_context_menu_request(*m_item_view, index, event);
  119. };
  120. set_view_mode(ViewMode::Icon);
  121. }
  122. DirectoryView::~DirectoryView()
  123. {
  124. }
  125. void DirectoryView::set_view_mode(ViewMode mode)
  126. {
  127. if (m_view_mode == mode)
  128. return;
  129. m_view_mode = mode;
  130. update();
  131. if (mode == ViewMode::List) {
  132. set_active_widget(m_table_view);
  133. return;
  134. }
  135. if (mode == ViewMode::Icon) {
  136. set_active_widget(m_item_view);
  137. return;
  138. }
  139. ASSERT_NOT_REACHED();
  140. }
  141. void DirectoryView::add_path_to_history(const StringView& path)
  142. {
  143. if (m_path_history_position < m_path_history.size())
  144. m_path_history.resize(m_path_history_position + 1);
  145. m_path_history.append(path);
  146. m_path_history_position = m_path_history.size() - 1;
  147. }
  148. void DirectoryView::open(const StringView& path)
  149. {
  150. add_path_to_history(path);
  151. model().open(path);
  152. }
  153. void DirectoryView::set_status_message(const StringView& message)
  154. {
  155. if (on_status_message)
  156. on_status_message(message);
  157. }
  158. void DirectoryView::open_parent_directory()
  159. {
  160. auto path = String::format("%s/..", model().path().characters());
  161. add_path_to_history(path);
  162. model().open(path);
  163. }
  164. void DirectoryView::refresh()
  165. {
  166. model().update();
  167. }
  168. void DirectoryView::open_previous_directory()
  169. {
  170. if (m_path_history_position > 0) {
  171. m_path_history_position--;
  172. model().open(m_path_history[m_path_history_position]);
  173. }
  174. }
  175. void DirectoryView::open_next_directory()
  176. {
  177. if (m_path_history_position < m_path_history.size() - 1) {
  178. m_path_history_position++;
  179. model().open(m_path_history[m_path_history_position]);
  180. }
  181. }
  182. void DirectoryView::update_statusbar()
  183. {
  184. if (current_view().selection().is_empty()) {
  185. set_status_message(String::format("%d item%s (%s)",
  186. model().row_count(),
  187. model().row_count() != 1 ? "s" : "",
  188. human_readable_size(model().bytes_in_files()).characters()));
  189. return;
  190. }
  191. int selected_item_count = current_view().selection().size();
  192. size_t selected_byte_count = 0;
  193. current_view().selection().for_each_index([&](auto& index) {
  194. auto size_index = current_view().model()->index(index.row(), GDirectoryModel::Column::Size);
  195. auto file_size = current_view().model()->data(size_index).to_int();
  196. selected_byte_count += file_size;
  197. });
  198. set_status_message(String::format("%d item%s selected (%s)",
  199. selected_item_count,
  200. selected_item_count != 1 ? "s" : "",
  201. human_readable_size(selected_byte_count).characters()));
  202. }