DirectoryView.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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/NumberFormat.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibGUI/MessageBox.h>
  30. #include <LibGUI/SortingProxyModel.h>
  31. #include <stdio.h>
  32. #include <unistd.h>
  33. NonnullRefPtr<GUI::Action> LauncherHandler::create_launch_action(Function<void(const LauncherHandler&)> launch_handler)
  34. {
  35. RefPtr<Gfx::Bitmap> icon;
  36. auto icon_file = details().icons.get("16x16");
  37. if (icon_file.has_value())
  38. icon = Gfx::Bitmap::load_from_file(icon_file.value());
  39. return GUI::Action::create(details().name, move(icon), [this, launch_handler = move(launch_handler)](auto&) {
  40. launch_handler(*this);
  41. });
  42. }
  43. RefPtr<LauncherHandler> DirectoryView::get_default_launch_handler(const NonnullRefPtrVector<LauncherHandler>& handlers)
  44. {
  45. // If this is an application, pick it first
  46. for (size_t i = 0; i < handlers.size(); i++) {
  47. if (handlers[i].details().launcher_type == Desktop::Launcher::LauncherType::Application)
  48. return handlers[i];
  49. }
  50. // If there's a handler preferred by the user, pick this first
  51. for (size_t i = 0; i < handlers.size(); i++) {
  52. if (handlers[i].details().launcher_type == Desktop::Launcher::LauncherType::UserPreferred)
  53. return handlers[i];
  54. }
  55. // Otherwise, use the user's default, if available
  56. for (size_t i = 0; i < handlers.size(); i++) {
  57. if (handlers[i].details().launcher_type == Desktop::Launcher::LauncherType::UserDefault)
  58. return handlers[i];
  59. }
  60. // If still no match, use the first one we find
  61. if (!handlers.is_empty()) {
  62. return handlers[0];
  63. }
  64. return {};
  65. }
  66. NonnullRefPtrVector<LauncherHandler> DirectoryView::get_launch_handlers(const URL& url)
  67. {
  68. NonnullRefPtrVector<LauncherHandler> handlers;
  69. for (auto& h : Desktop::Launcher::get_handlers_with_details_for_url(url)) {
  70. handlers.append(adopt(*new LauncherHandler(h)));
  71. }
  72. return handlers;
  73. }
  74. NonnullRefPtrVector<LauncherHandler> DirectoryView::get_launch_handlers(const String& path)
  75. {
  76. return get_launch_handlers(URL::create_with_file_protocol(path));
  77. }
  78. void DirectoryView::handle_activation(const GUI::ModelIndex& index)
  79. {
  80. if (!index.is_valid())
  81. return;
  82. dbgprintf("on activation: %d,%d, this=%p, m_model=%p\n", index.row(), index.column(), this, m_model.ptr());
  83. auto& node = model().node(index);
  84. auto path = node.full_path(model());
  85. struct stat st;
  86. if (stat(path.characters(), &st) < 0) {
  87. perror("stat");
  88. return;
  89. }
  90. if (S_ISDIR(st.st_mode)) {
  91. open(path);
  92. return;
  93. }
  94. auto url = URL::create_with_file_protocol(path);
  95. auto launcher_handlers = get_launch_handlers(url);
  96. auto default_launcher = get_default_launch_handler(launcher_handlers);
  97. if (default_launcher && on_launch) {
  98. on_launch(url, *default_launcher);
  99. } else {
  100. auto error_message = String::format("Could not open %s", path.characters());
  101. GUI::MessageBox::show(window(), error_message, "File Manager", GUI::MessageBox::Type::Error);
  102. }
  103. }
  104. DirectoryView::DirectoryView()
  105. : m_model(GUI::FileSystemModel::create())
  106. {
  107. set_active_widget(nullptr);
  108. set_content_margins({ 2, 2, 2, 2 });
  109. m_icon_view = add<GUI::IconView>();
  110. m_icon_view->set_model(model());
  111. m_columns_view = add<GUI::ColumnsView>();
  112. m_columns_view->set_model(model());
  113. m_table_view = add<GUI::TableView>();
  114. m_table_view->set_model(GUI::SortingProxyModel::create(m_model));
  115. m_table_view->model()->set_key_column_and_sort_order(GUI::FileSystemModel::Column::Name, GUI::SortOrder::Ascending);
  116. m_icon_view->set_model_column(GUI::FileSystemModel::Column::Name);
  117. m_columns_view->set_model_column(GUI::FileSystemModel::Column::Name);
  118. m_model->on_error = [this](int error, const char* error_string) {
  119. bool quit = false;
  120. if (m_path_history.size())
  121. open(m_path_history.at(m_path_history_position));
  122. else
  123. quit = true;
  124. if (on_error)
  125. on_error(error, error_string, quit);
  126. };
  127. m_model->on_complete = [this] {
  128. m_table_view->selection().clear();
  129. m_icon_view->selection().clear();
  130. add_path_to_history(model().root_path());
  131. if (on_path_change)
  132. on_path_change(model().root_path());
  133. };
  134. m_model->register_client(*this);
  135. m_model->on_thumbnail_progress = [this](int done, int total) {
  136. if (on_thumbnail_progress)
  137. on_thumbnail_progress(done, total);
  138. };
  139. m_icon_view->on_activation = [&](const GUI::ModelIndex& index) {
  140. handle_activation(index);
  141. };
  142. m_columns_view->on_activation = [&](const GUI::ModelIndex& index) {
  143. handle_activation(index);
  144. };
  145. m_table_view->on_activation = [&](auto& index) {
  146. handle_activation(map_table_view_index(index));
  147. };
  148. m_table_view->on_selection_change = [this] {
  149. update_statusbar();
  150. if (on_selection_change)
  151. on_selection_change(*m_table_view);
  152. };
  153. m_icon_view->on_selection_change = [this] {
  154. update_statusbar();
  155. if (on_selection_change)
  156. on_selection_change(*m_icon_view);
  157. };
  158. m_columns_view->on_selection_change = [this] {
  159. update_statusbar();
  160. if (on_selection_change)
  161. on_selection_change(*m_columns_view);
  162. };
  163. m_table_view->on_context_menu_request = [this](auto& index, auto& event) {
  164. if (on_context_menu_request)
  165. on_context_menu_request(*m_table_view, map_table_view_index(index), event);
  166. };
  167. m_icon_view->on_context_menu_request = [this](auto& index, auto& event) {
  168. if (on_context_menu_request)
  169. on_context_menu_request(*m_icon_view, index, event);
  170. };
  171. m_columns_view->on_context_menu_request = [this](auto& index, auto& event) {
  172. if (on_context_menu_request)
  173. on_context_menu_request(*m_columns_view, index, event);
  174. };
  175. m_table_view->on_drop = [this](auto& index, auto& event) {
  176. if (on_drop)
  177. on_drop(*m_table_view, map_table_view_index(index), event);
  178. };
  179. m_icon_view->on_drop = [this](auto& index, auto& event) {
  180. if (on_drop)
  181. on_drop(*m_icon_view, index, event);
  182. };
  183. m_columns_view->on_drop = [this](auto& index, auto& event) {
  184. if (on_drop)
  185. on_drop(*m_columns_view, index, event);
  186. };
  187. set_view_mode(ViewMode::Icon);
  188. }
  189. DirectoryView::~DirectoryView()
  190. {
  191. m_model->unregister_client(*this);
  192. }
  193. void DirectoryView::on_model_update(unsigned flags)
  194. {
  195. if (flags & GUI::Model::UpdateFlag::InvalidateAllIndexes) {
  196. for_each_view_implementation([](auto& view) {
  197. view.selection().clear();
  198. });
  199. }
  200. update_statusbar();
  201. }
  202. void DirectoryView::set_view_mode(ViewMode mode)
  203. {
  204. if (m_view_mode == mode)
  205. return;
  206. m_view_mode = mode;
  207. update();
  208. if (mode == ViewMode::Table) {
  209. set_active_widget(m_table_view);
  210. return;
  211. }
  212. if (mode == ViewMode::Columns) {
  213. set_active_widget(m_columns_view);
  214. return;
  215. }
  216. if (mode == ViewMode::Icon) {
  217. set_active_widget(m_icon_view);
  218. return;
  219. }
  220. ASSERT_NOT_REACHED();
  221. }
  222. void DirectoryView::add_path_to_history(const StringView& path)
  223. {
  224. if (m_path_history.size() && m_path_history.at(m_path_history_position) == path)
  225. return;
  226. if (m_path_history_position < m_path_history.size())
  227. m_path_history.resize(m_path_history_position + 1);
  228. m_path_history.append(path);
  229. m_path_history_position = m_path_history.size() - 1;
  230. }
  231. void DirectoryView::open(const StringView& path)
  232. {
  233. if (model().root_path() == path) {
  234. model().update();
  235. return;
  236. }
  237. model().set_root_path(path);
  238. }
  239. void DirectoryView::set_status_message(const StringView& message)
  240. {
  241. if (on_status_message)
  242. on_status_message(message);
  243. }
  244. void DirectoryView::open_parent_directory()
  245. {
  246. auto path = String::format("%s/..", model().root_path().characters());
  247. model().set_root_path(path);
  248. }
  249. void DirectoryView::refresh()
  250. {
  251. model().update();
  252. }
  253. void DirectoryView::open_previous_directory()
  254. {
  255. if (m_path_history_position > 0) {
  256. m_path_history_position--;
  257. model().set_root_path(m_path_history[m_path_history_position]);
  258. }
  259. }
  260. void DirectoryView::open_next_directory()
  261. {
  262. if (m_path_history_position < m_path_history.size() - 1) {
  263. m_path_history_position++;
  264. model().set_root_path(m_path_history[m_path_history_position]);
  265. }
  266. }
  267. GUI::ModelIndex DirectoryView::map_table_view_index(const GUI::ModelIndex& index) const
  268. {
  269. auto& filter_model = (const GUI::SortingProxyModel&)*m_table_view->model();
  270. return filter_model.map_to_target(index);
  271. }
  272. void DirectoryView::update_statusbar()
  273. {
  274. size_t total_size = model().node({}).total_size;
  275. if (current_view().selection().is_empty()) {
  276. set_status_message(String::format("%d item%s (%s)",
  277. model().row_count(),
  278. model().row_count() != 1 ? "s" : "",
  279. human_readable_size(total_size).characters()));
  280. return;
  281. }
  282. int selected_item_count = current_view().selection().size();
  283. size_t selected_byte_count = 0;
  284. current_view().selection().for_each_index([&](auto& index) {
  285. auto& model = *current_view().model();
  286. auto size_index = model.sibling(index.row(), GUI::FileSystemModel::Column::Size, model.parent_index(index));
  287. auto file_size = model.data(size_index).to_i32();
  288. selected_byte_count += file_size;
  289. });
  290. StringBuilder builder;
  291. builder.append(String::number(selected_item_count));
  292. builder.append(" item");
  293. if (selected_item_count != 1)
  294. builder.append('s');
  295. builder.append(" selected (");
  296. builder.append(human_readable_size(selected_byte_count).characters());
  297. builder.append(')');
  298. if (selected_item_count == 1) {
  299. auto index = current_view().selection().first();
  300. // FIXME: This is disgusting. This code should not even be aware that there is a GUI::SortingProxyModel in the table view.
  301. if (m_view_mode == ViewMode::Table)
  302. index = map_table_view_index(index);
  303. auto& node = model().node(index);
  304. if (!node.symlink_target.is_empty()) {
  305. builder.append(" -> ");
  306. builder.append(node.symlink_target);
  307. }
  308. }
  309. set_status_message(builder.to_string());
  310. }
  311. void DirectoryView::set_should_show_dotfiles(bool show_dotfiles)
  312. {
  313. m_model->set_should_show_dotfiles(show_dotfiles);
  314. }