DirectoryView.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. , m_sorting_model(GUI::SortingProxyModel::create(m_model))
  107. {
  108. set_active_widget(nullptr);
  109. set_content_margins({ 2, 2, 2, 2 });
  110. m_icon_view = add<GUI::IconView>();
  111. m_icon_view->set_model(m_sorting_model);
  112. m_columns_view = add<GUI::ColumnsView>();
  113. m_columns_view->set_model(m_sorting_model);
  114. m_table_view = add<GUI::TableView>();
  115. m_table_view->set_model(m_sorting_model);
  116. m_table_view->set_key_column_and_sort_order(GUI::FileSystemModel::Column::Name, GUI::SortOrder::Ascending);
  117. m_icon_view->set_model_column(GUI::FileSystemModel::Column::Name);
  118. m_columns_view->set_model_column(GUI::FileSystemModel::Column::Name);
  119. m_model->on_error = [this](int error, const char* error_string) {
  120. bool quit = false;
  121. if (m_path_history.size())
  122. open(m_path_history.at(m_path_history_position));
  123. else
  124. quit = true;
  125. if (on_error)
  126. on_error(error, error_string, quit);
  127. };
  128. m_model->on_complete = [this] {
  129. m_table_view->selection().clear();
  130. m_icon_view->selection().clear();
  131. add_path_to_history(model().root_path());
  132. if (on_path_change)
  133. on_path_change(model().root_path());
  134. };
  135. m_model->register_client(*this);
  136. m_model->on_thumbnail_progress = [this](int done, int total) {
  137. if (on_thumbnail_progress)
  138. on_thumbnail_progress(done, total);
  139. };
  140. m_icon_view->on_activation = [&](auto& index) {
  141. handle_activation(map_index(index));
  142. };
  143. m_columns_view->on_activation = [&](auto& index) {
  144. handle_activation(map_index(index));
  145. };
  146. m_table_view->on_activation = [&](auto& index) {
  147. handle_activation(map_index(index));
  148. };
  149. m_table_view->on_selection_change = [this] {
  150. update_statusbar();
  151. if (on_selection_change)
  152. on_selection_change(*m_table_view);
  153. };
  154. m_icon_view->on_selection_change = [this] {
  155. update_statusbar();
  156. if (on_selection_change)
  157. on_selection_change(*m_icon_view);
  158. };
  159. m_columns_view->on_selection_change = [this] {
  160. update_statusbar();
  161. if (on_selection_change)
  162. on_selection_change(*m_columns_view);
  163. };
  164. m_table_view->on_context_menu_request = [this](auto& index, auto& event) {
  165. if (on_context_menu_request)
  166. on_context_menu_request(*m_table_view, map_index(index), event);
  167. };
  168. m_icon_view->on_context_menu_request = [this](auto& index, auto& event) {
  169. if (on_context_menu_request)
  170. on_context_menu_request(*m_icon_view, map_index(index), event);
  171. };
  172. m_columns_view->on_context_menu_request = [this](auto& index, auto& event) {
  173. if (on_context_menu_request)
  174. on_context_menu_request(*m_columns_view, map_index(index), event);
  175. };
  176. m_table_view->on_drop = [this](auto& index, auto& event) {
  177. if (on_drop)
  178. on_drop(*m_table_view, map_index(index), event);
  179. };
  180. m_icon_view->on_drop = [this](auto& index, auto& event) {
  181. if (on_drop)
  182. on_drop(*m_icon_view, map_index(index), event);
  183. };
  184. m_columns_view->on_drop = [this](auto& index, auto& event) {
  185. if (on_drop)
  186. on_drop(*m_columns_view, map_index(index), event);
  187. };
  188. set_view_mode(ViewMode::Icon);
  189. }
  190. DirectoryView::~DirectoryView()
  191. {
  192. m_model->unregister_client(*this);
  193. }
  194. void DirectoryView::model_did_update(unsigned flags)
  195. {
  196. if (flags & GUI::Model::UpdateFlag::InvalidateAllIndexes) {
  197. for_each_view_implementation([](auto& view) {
  198. view.selection().clear();
  199. });
  200. }
  201. update_statusbar();
  202. }
  203. void DirectoryView::set_view_mode(ViewMode mode)
  204. {
  205. if (m_view_mode == mode)
  206. return;
  207. m_view_mode = mode;
  208. update();
  209. if (mode == ViewMode::Table) {
  210. set_active_widget(m_table_view);
  211. return;
  212. }
  213. if (mode == ViewMode::Columns) {
  214. set_active_widget(m_columns_view);
  215. return;
  216. }
  217. if (mode == ViewMode::Icon) {
  218. set_active_widget(m_icon_view);
  219. return;
  220. }
  221. ASSERT_NOT_REACHED();
  222. }
  223. void DirectoryView::add_path_to_history(const StringView& path)
  224. {
  225. if (m_path_history.size() && m_path_history.at(m_path_history_position) == path)
  226. return;
  227. if (m_path_history_position < m_path_history.size())
  228. m_path_history.resize(m_path_history_position + 1);
  229. m_path_history.append(path);
  230. m_path_history_position = m_path_history.size() - 1;
  231. }
  232. void DirectoryView::open(const StringView& path)
  233. {
  234. if (model().root_path() == path) {
  235. model().update();
  236. return;
  237. }
  238. model().set_root_path(path);
  239. }
  240. void DirectoryView::set_status_message(const StringView& message)
  241. {
  242. if (on_status_message)
  243. on_status_message(message);
  244. }
  245. void DirectoryView::open_parent_directory()
  246. {
  247. auto path = String::format("%s/..", model().root_path().characters());
  248. model().set_root_path(path);
  249. }
  250. void DirectoryView::refresh()
  251. {
  252. model().update();
  253. }
  254. void DirectoryView::open_previous_directory()
  255. {
  256. if (m_path_history_position > 0) {
  257. m_path_history_position--;
  258. model().set_root_path(m_path_history[m_path_history_position]);
  259. }
  260. }
  261. void DirectoryView::open_next_directory()
  262. {
  263. if (m_path_history_position < m_path_history.size() - 1) {
  264. m_path_history_position++;
  265. model().set_root_path(m_path_history[m_path_history_position]);
  266. }
  267. }
  268. GUI::ModelIndex DirectoryView::map_index(const GUI::ModelIndex& index) const
  269. {
  270. return m_sorting_model->map_to_source(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.index(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& node = model().node(map_index(current_view().selection().first()));
  300. if (!node.symlink_target.is_empty()) {
  301. builder.append(" -> ");
  302. builder.append(node.symlink_target);
  303. }
  304. }
  305. set_status_message(builder.to_string());
  306. }
  307. void DirectoryView::set_should_show_dotfiles(bool show_dotfiles)
  308. {
  309. m_model->set_should_show_dotfiles(show_dotfiles);
  310. }