PropertiesWindow.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "PropertiesWindow.h"
  7. #include <AK/LexicalPath.h>
  8. #include <AK/NumberFormat.h>
  9. #include <AK/StringBuilder.h>
  10. #include <Applications/FileManager/PropertiesWindowGeneralTabGML.h>
  11. #include <LibDesktop/Launcher.h>
  12. #include <LibGUI/BoxLayout.h>
  13. #include <LibGUI/CheckBox.h>
  14. #include <LibGUI/FileIconProvider.h>
  15. #include <LibGUI/FilePicker.h>
  16. #include <LibGUI/IconView.h>
  17. #include <LibGUI/LinkLabel.h>
  18. #include <LibGUI/MessageBox.h>
  19. #include <LibGUI/SeparatorWidget.h>
  20. #include <LibGUI/TabWidget.h>
  21. #include <grp.h>
  22. #include <limits.h>
  23. #include <pwd.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. PropertiesWindow::PropertiesWindow(const String& path, bool disable_rename, Window* parent_window)
  28. : Window(parent_window)
  29. {
  30. auto lexical_path = LexicalPath(path);
  31. auto& main_widget = set_main_widget<GUI::Widget>();
  32. main_widget.set_layout<GUI::VerticalBoxLayout>();
  33. main_widget.layout()->set_margins({ 4, 4, 4, 4 });
  34. main_widget.set_fill_with_background_color(true);
  35. set_rect({ 0, 0, 360, 420 });
  36. set_resizable(false);
  37. set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/properties.png"));
  38. auto& tab_widget = main_widget.add<GUI::TabWidget>();
  39. auto& general_tab = tab_widget.add_tab<GUI::Widget>("General");
  40. general_tab.load_from_gml(properties_window_general_tab_gml);
  41. m_name = lexical_path.basename();
  42. m_path = lexical_path.string();
  43. m_parent_path = lexical_path.dirname();
  44. m_icon = general_tab.find_descendant_of_type_named<GUI::ImageWidget>("icon");
  45. m_name_box = general_tab.find_descendant_of_type_named<GUI::TextBox>("name");
  46. m_name_box->set_text(m_name);
  47. m_name_box->set_mode(disable_rename ? GUI::TextBox::Mode::DisplayOnly : GUI::TextBox::Mode::Editable);
  48. m_name_box->on_change = [&]() {
  49. m_name_dirty = m_name != m_name_box->text();
  50. m_apply_button->set_enabled(m_name_dirty || m_permissions_dirty);
  51. };
  52. struct stat st;
  53. if (lstat(path.characters(), &st)) {
  54. perror("stat");
  55. return;
  56. }
  57. String owner_name;
  58. String group_name;
  59. if (auto* pw = getpwuid(st.st_uid)) {
  60. owner_name = pw->pw_name;
  61. } else {
  62. owner_name = "n/a";
  63. }
  64. if (auto* gr = getgrgid(st.st_gid)) {
  65. group_name = gr->gr_name;
  66. } else {
  67. group_name = "n/a";
  68. }
  69. m_mode = st.st_mode;
  70. m_old_mode = st.st_mode;
  71. auto type = general_tab.find_descendant_of_type_named<GUI::Label>("type");
  72. type->set_text(get_description(m_mode));
  73. auto location = general_tab.find_descendant_of_type_named<GUI::LinkLabel>("location");
  74. location->set_text(path);
  75. location->on_click = [this] {
  76. Desktop::Launcher::open(URL::create_with_file_protocol(m_parent_path, m_name));
  77. };
  78. if (S_ISLNK(m_mode)) {
  79. auto link_destination = Core::File::read_link(path);
  80. if (link_destination.is_null()) {
  81. perror("readlink");
  82. } else {
  83. auto link_location = general_tab.find_descendant_of_type_named<GUI::LinkLabel>("link_location");
  84. link_location->set_text(link_destination);
  85. link_location->on_click = [link_destination] {
  86. auto link_directory = LexicalPath(link_destination);
  87. Desktop::Launcher::open(URL::create_with_file_protocol(link_directory.dirname(), link_directory.basename()));
  88. };
  89. }
  90. } else {
  91. auto link_location_widget = general_tab.find_descendant_of_type_named<GUI::Widget>("link_location_widget");
  92. general_tab.remove_child(*link_location_widget);
  93. }
  94. auto size = general_tab.find_descendant_of_type_named<GUI::Label>("size");
  95. size->set_text(human_readable_size_long(st.st_size));
  96. auto owner = general_tab.find_descendant_of_type_named<GUI::Label>("owner");
  97. owner->set_text(String::formatted("{} ({})", owner_name, st.st_uid));
  98. auto group = general_tab.find_descendant_of_type_named<GUI::Label>("group");
  99. group->set_text(String::formatted("{} ({})", group_name, st.st_gid));
  100. auto created_at = general_tab.find_descendant_of_type_named<GUI::Label>("created_at");
  101. created_at->set_text(GUI::FileSystemModel::timestamp_string(st.st_ctime));
  102. auto last_modified = general_tab.find_descendant_of_type_named<GUI::Label>("last_modified");
  103. last_modified->set_text(GUI::FileSystemModel::timestamp_string(st.st_mtime));
  104. auto owner_read = general_tab.find_descendant_of_type_named<GUI::CheckBox>("owner_read");
  105. auto owner_write = general_tab.find_descendant_of_type_named<GUI::CheckBox>("owner_write");
  106. auto owner_execute = general_tab.find_descendant_of_type_named<GUI::CheckBox>("owner_execute");
  107. setup_permission_checkboxes(*owner_read, *owner_write, *owner_execute, { S_IRUSR, S_IWUSR, S_IXUSR }, m_mode);
  108. auto group_read = general_tab.find_descendant_of_type_named<GUI::CheckBox>("group_read");
  109. auto group_write = general_tab.find_descendant_of_type_named<GUI::CheckBox>("group_write");
  110. auto group_execute = general_tab.find_descendant_of_type_named<GUI::CheckBox>("group_execute");
  111. setup_permission_checkboxes(*group_read, *group_write, *group_execute, { S_IRGRP, S_IWGRP, S_IXGRP }, m_mode);
  112. auto others_read = general_tab.find_descendant_of_type_named<GUI::CheckBox>("others_read");
  113. auto others_write = general_tab.find_descendant_of_type_named<GUI::CheckBox>("others_write");
  114. auto others_execute = general_tab.find_descendant_of_type_named<GUI::CheckBox>("others_execute");
  115. setup_permission_checkboxes(*others_read, *others_write, *others_execute, { S_IROTH, S_IWOTH, S_IXOTH }, m_mode);
  116. auto& button_widget = main_widget.add<GUI::Widget>();
  117. button_widget.set_layout<GUI::HorizontalBoxLayout>();
  118. button_widget.set_fixed_height(24);
  119. button_widget.layout()->set_spacing(5);
  120. button_widget.layout()->add_spacer();
  121. make_button("OK", button_widget).on_click = [this](auto) {
  122. if (apply_changes())
  123. close();
  124. };
  125. make_button("Cancel", button_widget).on_click = [this](auto) {
  126. close();
  127. };
  128. m_apply_button = make_button("Apply", button_widget);
  129. m_apply_button->on_click = [this](auto) { apply_changes(); };
  130. m_apply_button->set_enabled(false);
  131. update();
  132. }
  133. PropertiesWindow::~PropertiesWindow()
  134. {
  135. }
  136. void PropertiesWindow::update()
  137. {
  138. m_icon->set_bitmap(GUI::FileIconProvider::icon_for_path(make_full_path(m_name), m_mode).bitmap_for_size(32));
  139. set_title(String::formatted("{} - Properties", m_name));
  140. }
  141. void PropertiesWindow::permission_changed(mode_t mask, bool set)
  142. {
  143. if (set) {
  144. m_mode |= mask;
  145. } else {
  146. m_mode &= ~mask;
  147. }
  148. m_permissions_dirty = m_mode != m_old_mode;
  149. m_apply_button->set_enabled(m_name_dirty || m_permissions_dirty);
  150. }
  151. String PropertiesWindow::make_full_path(const String& name)
  152. {
  153. return String::formatted("{}/{}", m_parent_path, name);
  154. }
  155. bool PropertiesWindow::apply_changes()
  156. {
  157. if (m_name_dirty) {
  158. String new_name = m_name_box->text();
  159. String new_file = make_full_path(new_name).characters();
  160. if (Core::File::exists(new_file)) {
  161. GUI::MessageBox::show(this, String::formatted("A file \"{}\" already exists!", new_name), "Error", GUI::MessageBox::Type::Error);
  162. return false;
  163. }
  164. if (rename(make_full_path(m_name).characters(), new_file.characters())) {
  165. GUI::MessageBox::show(this, String::formatted("Could not rename file: {}!", strerror(errno)), "Error", GUI::MessageBox::Type::Error);
  166. return false;
  167. }
  168. m_name = new_name;
  169. m_name_dirty = false;
  170. update();
  171. }
  172. if (m_permissions_dirty) {
  173. if (chmod(make_full_path(m_name).characters(), m_mode)) {
  174. GUI::MessageBox::show(this, String::formatted("Could not update permissions: {}!", strerror(errno)), "Error", GUI::MessageBox::Type::Error);
  175. return false;
  176. }
  177. m_old_mode = m_mode;
  178. m_permissions_dirty = false;
  179. }
  180. update();
  181. m_apply_button->set_enabled(false);
  182. return true;
  183. }
  184. void PropertiesWindow::setup_permission_checkboxes(GUI::CheckBox& box_read, GUI::CheckBox& box_write, GUI::CheckBox& box_execute, PermissionMasks masks, mode_t mode)
  185. {
  186. struct stat st;
  187. if (lstat(m_path.characters(), &st)) {
  188. perror("stat");
  189. return;
  190. }
  191. auto can_edit_checkboxes = st.st_uid == getuid();
  192. box_read.set_checked(mode & masks.read);
  193. box_read.on_checked = [&, masks](bool checked) { permission_changed(masks.read, checked); };
  194. box_read.set_enabled(can_edit_checkboxes);
  195. box_write.set_checked(mode & masks.write);
  196. box_write.on_checked = [&, masks](bool checked) { permission_changed(masks.write, checked); };
  197. box_write.set_enabled(can_edit_checkboxes);
  198. box_execute.set_checked(mode & masks.execute);
  199. box_execute.on_checked = [&, masks](bool checked) { permission_changed(masks.execute, checked); };
  200. box_execute.set_enabled(can_edit_checkboxes);
  201. }
  202. GUI::Button& PropertiesWindow::make_button(String text, GUI::Widget& parent)
  203. {
  204. auto& button = parent.add<GUI::Button>(text);
  205. button.set_fixed_size(70, 22);
  206. return button;
  207. }