DirectoryView.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. FileSystemPath path(String::format("%s/%s", model().path().characters(), entry.name.characters()));
  13. if (entry.is_directory()) {
  14. open(path.string());
  15. return;
  16. }
  17. if (entry.is_executable()) {
  18. if (fork() == 0) {
  19. int rc = execl(path.string().characters(), path.string().characters(), nullptr);
  20. if (rc < 0)
  21. perror("exec");
  22. ASSERT_NOT_REACHED();
  23. }
  24. return;
  25. }
  26. if (path.string().to_lowercase().ends_with(".png")) {
  27. if (fork() == 0) {
  28. int rc = execl("/bin/qs", "/bin/qs", path.string().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.string().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.copy_ref()));
  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_item_view->on_model_notification = [this](const GModelNotification& notification) {
  54. if (notification.type() == GModelNotification::Type::ModelUpdated) {
  55. set_status_message(String::format("%d item%s (%u byte%s)",
  56. model().row_count(),
  57. model().row_count() != 1 ? "s" : "",
  58. model().bytes_in_files(),
  59. model().bytes_in_files() != 1 ? "s" : ""));
  60. if (on_path_change)
  61. on_path_change(model().path());
  62. }
  63. };
  64. m_model->on_thumbnail_progress = [this](int done, int total) {
  65. if (on_thumbnail_progress)
  66. on_thumbnail_progress(done, total);
  67. };
  68. m_item_view->on_activation = [&](const GModelIndex& index) {
  69. handle_activation(index);
  70. };
  71. m_table_view->on_activation = [&](auto& index) {
  72. auto& filter_model = (GSortingProxyModel&)*m_table_view->model();
  73. handle_activation(filter_model.map_to_target(index));
  74. };
  75. set_view_mode(ViewMode::Icon);
  76. }
  77. DirectoryView::~DirectoryView()
  78. {
  79. }
  80. void DirectoryView::set_view_mode(ViewMode mode)
  81. {
  82. if (m_view_mode == mode)
  83. return;
  84. m_view_mode = mode;
  85. update();
  86. if (mode == ViewMode::List) {
  87. set_active_widget(m_table_view);
  88. return;
  89. }
  90. if (mode == ViewMode::Icon) {
  91. set_active_widget(m_item_view);
  92. return;
  93. }
  94. ASSERT_NOT_REACHED();
  95. }
  96. void DirectoryView::add_path_to_history(const StringView& path)
  97. {
  98. if (m_path_history_position < m_path_history.size())
  99. m_path_history.resize(m_path_history_position + 1);
  100. m_path_history.append(path);
  101. m_path_history_position = m_path_history.size() - 1;
  102. }
  103. void DirectoryView::open(const StringView& path)
  104. {
  105. add_path_to_history(path);
  106. model().open(path);
  107. }
  108. void DirectoryView::set_status_message(const StringView& message)
  109. {
  110. if (on_status_message)
  111. on_status_message(message);
  112. }
  113. void DirectoryView::open_parent_directory()
  114. {
  115. auto path = String::format("%s/..", model().path().characters());
  116. add_path_to_history(path);
  117. model().open(path);
  118. }
  119. void DirectoryView::refresh()
  120. {
  121. model().update();
  122. }
  123. void DirectoryView::open_previous_directory()
  124. {
  125. if (m_path_history_position > 0) {
  126. m_path_history_position--;
  127. model().open(m_path_history[m_path_history_position]);
  128. }
  129. }
  130. void DirectoryView::open_next_directory()
  131. {
  132. if (m_path_history_position < m_path_history.size() - 1) {
  133. m_path_history_position++;
  134. model().open(m_path_history[m_path_history_position]);
  135. }
  136. }