DirectoryView.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 (path.to_lowercase().ends_with(".wav")) {
  36. if (fork() == 0) {
  37. int rc = execl("/bin/SoundPlayer", "/bin/SoundPlayer", path.characters(), nullptr);
  38. if (rc < 0)
  39. perror("exec");
  40. ASSERT_NOT_REACHED();
  41. }
  42. return;
  43. }
  44. if (fork() == 0) {
  45. int rc = execl("/bin/TextEditor", "/bin/TextEditor", path.characters(), nullptr);
  46. if (rc < 0)
  47. perror("exec");
  48. ASSERT_NOT_REACHED();
  49. }
  50. };
  51. DirectoryView::DirectoryView(GWidget* parent)
  52. : GStackWidget(parent)
  53. , m_model(GDirectoryModel::create())
  54. {
  55. set_active_widget(nullptr);
  56. m_item_view = new GItemView(this);
  57. m_item_view->set_model(model());
  58. m_table_view = new GTableView(this);
  59. m_table_view->set_model(GSortingProxyModel::create(m_model));
  60. m_table_view->model()->set_key_column_and_sort_order(GDirectoryModel::Column::Name, GSortOrder::Ascending);
  61. m_item_view->set_model_column(GDirectoryModel::Column::Name);
  62. m_table_view->model()->on_update = [this] {
  63. set_status_message(String::format("%d item%s (%u byte%s)",
  64. model().row_count(),
  65. model().row_count() != 1 ? "s" : "",
  66. model().bytes_in_files(),
  67. model().bytes_in_files() != 1 ? "s" : ""));
  68. if (on_path_change)
  69. on_path_change(model().path());
  70. };
  71. m_model->on_thumbnail_progress = [this](int done, int total) {
  72. if (on_thumbnail_progress)
  73. on_thumbnail_progress(done, total);
  74. };
  75. m_item_view->on_activation = [&](const GModelIndex& index) {
  76. handle_activation(index);
  77. };
  78. m_table_view->on_activation = [&](auto& index) {
  79. auto& filter_model = (GSortingProxyModel&)*m_table_view->model();
  80. handle_activation(filter_model.map_to_target(index));
  81. };
  82. set_view_mode(ViewMode::Icon);
  83. }
  84. DirectoryView::~DirectoryView()
  85. {
  86. }
  87. void DirectoryView::set_view_mode(ViewMode mode)
  88. {
  89. if (m_view_mode == mode)
  90. return;
  91. m_view_mode = mode;
  92. update();
  93. if (mode == ViewMode::List) {
  94. set_active_widget(m_table_view);
  95. return;
  96. }
  97. if (mode == ViewMode::Icon) {
  98. set_active_widget(m_item_view);
  99. return;
  100. }
  101. ASSERT_NOT_REACHED();
  102. }
  103. void DirectoryView::add_path_to_history(const StringView& path)
  104. {
  105. if (m_path_history_position < m_path_history.size())
  106. m_path_history.resize(m_path_history_position + 1);
  107. m_path_history.append(path);
  108. m_path_history_position = m_path_history.size() - 1;
  109. }
  110. void DirectoryView::open(const StringView& path)
  111. {
  112. add_path_to_history(path);
  113. model().open(path);
  114. }
  115. void DirectoryView::set_status_message(const StringView& message)
  116. {
  117. if (on_status_message)
  118. on_status_message(message);
  119. }
  120. void DirectoryView::open_parent_directory()
  121. {
  122. auto path = String::format("%s/..", model().path().characters());
  123. add_path_to_history(path);
  124. model().open(path);
  125. }
  126. void DirectoryView::refresh()
  127. {
  128. model().update();
  129. }
  130. void DirectoryView::open_previous_directory()
  131. {
  132. if (m_path_history_position > 0) {
  133. m_path_history_position--;
  134. model().open(m_path_history[m_path_history_position]);
  135. }
  136. }
  137. void DirectoryView::open_next_directory()
  138. {
  139. if (m_path_history_position < m_path_history.size() - 1) {
  140. m_path_history_position++;
  141. model().open(m_path_history[m_path_history_position]);
  142. }
  143. }