DirectoryView.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "DirectoryView.h"
  2. #include <AK/FileSystemPath.h>
  3. #include <LibGUI/GSortingProxyModel.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. void DirectoryView::handle_activation(const GModelIndex& index)
  7. {
  8. if (!index.is_valid())
  9. return;
  10. dbgprintf("on activation: %d,%d, this=%p, m_model=%p\n", index.row(), index.column(), this, m_model.ptr());
  11. auto& entry = model().entry(index.row());
  12. auto path = canonicalized_path(String::format("%s/%s", model().path().characters(), entry.name.characters()));
  13. if (entry.is_directory()) {
  14. open(path);
  15. return;
  16. }
  17. if (entry.is_executable()) {
  18. if (fork() == 0) {
  19. int rc = execl(path.characters(), path.characters(), nullptr);
  20. if (rc < 0)
  21. perror("exec");
  22. ASSERT_NOT_REACHED();
  23. }
  24. return;
  25. }
  26. if (path.to_lowercase().ends_with(".png")) {
  27. if (fork() == 0) {
  28. int rc = execl("/bin/qs", "/bin/qs", path.characters(), nullptr);
  29. if (rc < 0)
  30. perror("exec");
  31. ASSERT_NOT_REACHED();
  32. }
  33. return;
  34. }
  35. if (fork() == 0) {
  36. int rc = execl("/bin/TextEditor", "/bin/TextEditor", path.characters(), nullptr);
  37. if (rc < 0)
  38. perror("exec");
  39. ASSERT_NOT_REACHED();
  40. }
  41. };
  42. DirectoryView::DirectoryView(GWidget* parent)
  43. : GStackWidget(parent)
  44. , m_model(GDirectoryModel::create())
  45. {
  46. set_active_widget(nullptr);
  47. m_item_view = new GItemView(this);
  48. m_item_view->set_model(model());
  49. m_table_view = new GTableView(this);
  50. m_table_view->set_model(GSortingProxyModel::create(m_model));
  51. m_table_view->model()->set_key_column_and_sort_order(GDirectoryModel::Column::Name, GSortOrder::Ascending);
  52. m_item_view->set_model_column(GDirectoryModel::Column::Name);
  53. m_table_view->model()->on_model_update = [this](auto&) {
  54. set_status_message(String::format("%d item%s (%u byte%s)",
  55. model().row_count(),
  56. model().row_count() != 1 ? "s" : "",
  57. model().bytes_in_files(),
  58. model().bytes_in_files() != 1 ? "s" : ""));
  59. if (on_path_change)
  60. on_path_change(model().path());
  61. };
  62. m_model->on_thumbnail_progress = [this](int done, int total) {
  63. if (on_thumbnail_progress)
  64. on_thumbnail_progress(done, total);
  65. };
  66. m_item_view->on_activation = [&](const GModelIndex& index) {
  67. handle_activation(index);
  68. };
  69. m_table_view->on_activation = [&](auto& index) {
  70. auto& filter_model = (GSortingProxyModel&)*m_table_view->model();
  71. handle_activation(filter_model.map_to_target(index));
  72. };
  73. set_view_mode(ViewMode::Icon);
  74. }
  75. DirectoryView::~DirectoryView()
  76. {
  77. }
  78. void DirectoryView::set_view_mode(ViewMode mode)
  79. {
  80. if (m_view_mode == mode)
  81. return;
  82. m_view_mode = mode;
  83. update();
  84. if (mode == ViewMode::List) {
  85. set_active_widget(m_table_view);
  86. return;
  87. }
  88. if (mode == ViewMode::Icon) {
  89. set_active_widget(m_item_view);
  90. return;
  91. }
  92. ASSERT_NOT_REACHED();
  93. }
  94. void DirectoryView::add_path_to_history(const StringView& path)
  95. {
  96. if (m_path_history_position < m_path_history.size())
  97. m_path_history.resize(m_path_history_position + 1);
  98. m_path_history.append(path);
  99. m_path_history_position = m_path_history.size() - 1;
  100. }
  101. void DirectoryView::open(const StringView& path)
  102. {
  103. add_path_to_history(path);
  104. model().open(path);
  105. }
  106. void DirectoryView::set_status_message(const StringView& message)
  107. {
  108. if (on_status_message)
  109. on_status_message(message);
  110. }
  111. void DirectoryView::open_parent_directory()
  112. {
  113. auto path = String::format("%s/..", model().path().characters());
  114. add_path_to_history(path);
  115. model().open(path);
  116. }
  117. void DirectoryView::refresh()
  118. {
  119. model().update();
  120. }
  121. void DirectoryView::open_previous_directory()
  122. {
  123. if (m_path_history_position > 0) {
  124. m_path_history_position--;
  125. model().open(m_path_history[m_path_history_position]);
  126. }
  127. }
  128. void DirectoryView::open_next_directory()
  129. {
  130. if (m_path_history_position < m_path_history.size() - 1) {
  131. m_path_history_position++;
  132. model().open(m_path_history[m_path_history_position]);
  133. }
  134. }