PropertiesWindow.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 <LibDesktop/Launcher.h>
  11. #include <LibGUI/BoxLayout.h>
  12. #include <LibGUI/CheckBox.h>
  13. #include <LibGUI/FileIconProvider.h>
  14. #include <LibGUI/FilePicker.h>
  15. #include <LibGUI/LinkLabel.h>
  16. #include <LibGUI/MessageBox.h>
  17. #include <LibGUI/SeparatorWidget.h>
  18. #include <LibGUI/TabWidget.h>
  19. #include <grp.h>
  20. #include <limits.h>
  21. #include <pwd.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. PropertiesWindow::PropertiesWindow(const String& path, bool disable_rename, Window* parent_window)
  26. : Window(parent_window)
  27. {
  28. auto lexical_path = LexicalPath(path);
  29. auto& main_widget = set_main_widget<GUI::Widget>();
  30. main_widget.set_layout<GUI::VerticalBoxLayout>();
  31. main_widget.layout()->set_margins({ 4, 4, 4, 4 });
  32. main_widget.set_fill_with_background_color(true);
  33. set_rect({ 0, 0, 360, 420 });
  34. set_resizable(false);
  35. auto& tab_widget = main_widget.add<GUI::TabWidget>();
  36. auto& general_tab = tab_widget.add_tab<GUI::Widget>("General");
  37. general_tab.set_layout<GUI::VerticalBoxLayout>();
  38. general_tab.layout()->set_margins({ 12, 8, 12, 8 });
  39. general_tab.layout()->set_spacing(10);
  40. auto& file_container = general_tab.add<GUI::Widget>();
  41. file_container.set_layout<GUI::HorizontalBoxLayout>();
  42. file_container.layout()->set_spacing(20);
  43. file_container.set_fixed_height(34);
  44. m_icon = file_container.add<GUI::ImageWidget>();
  45. m_icon->set_fixed_size(32, 32);
  46. m_name = lexical_path.basename();
  47. m_path = lexical_path.string();
  48. m_parent_path = lexical_path.dirname();
  49. m_name_box = file_container.add<GUI::TextBox>();
  50. m_name_box->set_text(m_name);
  51. m_name_box->set_mode(disable_rename ? GUI::TextBox::Mode::DisplayOnly : GUI::TextBox::Mode::Editable);
  52. m_name_box->on_change = [&]() {
  53. m_name_dirty = m_name != m_name_box->text();
  54. m_apply_button->set_enabled(m_name_dirty || m_permissions_dirty);
  55. };
  56. set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/properties.png"));
  57. general_tab.add<GUI::SeparatorWidget>(Gfx::Orientation::Horizontal);
  58. struct stat st;
  59. if (lstat(path.characters(), &st)) {
  60. perror("stat");
  61. return;
  62. }
  63. String owner_name;
  64. String group_name;
  65. if (auto* pw = getpwuid(st.st_uid)) {
  66. owner_name = pw->pw_name;
  67. } else {
  68. owner_name = "n/a";
  69. }
  70. if (auto* gr = getgrgid(st.st_gid)) {
  71. group_name = gr->gr_name;
  72. } else {
  73. group_name = "n/a";
  74. }
  75. m_mode = st.st_mode;
  76. m_old_mode = st.st_mode;
  77. auto properties = Vector<PropertyValuePair>();
  78. properties.append({ "Type:", get_description(m_mode) });
  79. auto parent_link = URL::create_with_file_protocol(m_parent_path, m_name);
  80. properties.append(PropertyValuePair { "Location:", path, Optional(parent_link) });
  81. if (S_ISLNK(m_mode)) {
  82. auto link_destination = Core::File::read_link(path);
  83. if (link_destination.is_null()) {
  84. perror("readlink");
  85. } else {
  86. auto link_directory = LexicalPath(link_destination);
  87. auto link_parent = URL::create_with_file_protocol(link_directory.dirname(), link_directory.basename());
  88. properties.append({ "Link target:", link_destination, Optional(link_parent) });
  89. }
  90. }
  91. properties.append({ "Size:", human_readable_size_long(st.st_size) });
  92. properties.append({ "Owner:", String::formatted("{} ({})", owner_name, st.st_uid) });
  93. properties.append({ "Group:", String::formatted("{} ({})", group_name, st.st_gid) });
  94. properties.append({ "Created at:", GUI::FileSystemModel::timestamp_string(st.st_ctime) });
  95. properties.append({ "Last modified:", GUI::FileSystemModel::timestamp_string(st.st_mtime) });
  96. make_property_value_pairs(properties, general_tab);
  97. general_tab.add<GUI::SeparatorWidget>(Gfx::Orientation::Horizontal);
  98. make_permission_checkboxes(general_tab, { S_IRUSR, S_IWUSR, S_IXUSR }, "Owner:", m_mode);
  99. make_permission_checkboxes(general_tab, { S_IRGRP, S_IWGRP, S_IXGRP }, "Group:", m_mode);
  100. make_permission_checkboxes(general_tab, { S_IROTH, S_IWOTH, S_IXOTH }, "Others:", m_mode);
  101. general_tab.layout()->add_spacer();
  102. auto& button_widget = main_widget.add<GUI::Widget>();
  103. button_widget.set_layout<GUI::HorizontalBoxLayout>();
  104. button_widget.set_fixed_height(24);
  105. button_widget.layout()->set_spacing(5);
  106. button_widget.layout()->add_spacer();
  107. make_button("OK", button_widget).on_click = [this](auto) {
  108. if (apply_changes())
  109. close();
  110. };
  111. make_button("Cancel", button_widget).on_click = [this](auto) {
  112. close();
  113. };
  114. m_apply_button = make_button("Apply", button_widget);
  115. m_apply_button->on_click = [this](auto) { apply_changes(); };
  116. m_apply_button->set_enabled(false);
  117. update();
  118. }
  119. PropertiesWindow::~PropertiesWindow()
  120. {
  121. }
  122. void PropertiesWindow::update()
  123. {
  124. m_icon->set_bitmap(GUI::FileIconProvider::icon_for_path(make_full_path(m_name), m_mode).bitmap_for_size(32));
  125. set_title(String::formatted("{} - Properties", m_name));
  126. }
  127. void PropertiesWindow::permission_changed(mode_t mask, bool set)
  128. {
  129. if (set) {
  130. m_mode |= mask;
  131. } else {
  132. m_mode &= ~mask;
  133. }
  134. m_permissions_dirty = m_mode != m_old_mode;
  135. m_apply_button->set_enabled(m_name_dirty || m_permissions_dirty);
  136. }
  137. String PropertiesWindow::make_full_path(const String& name)
  138. {
  139. return String::formatted("{}/{}", m_parent_path, name);
  140. }
  141. bool PropertiesWindow::apply_changes()
  142. {
  143. if (m_name_dirty) {
  144. String new_name = m_name_box->text();
  145. String new_file = make_full_path(new_name).characters();
  146. if (Core::File::exists(new_file)) {
  147. GUI::MessageBox::show(this, String::formatted("A file \"{}\" already exists!", new_name), "Error", GUI::MessageBox::Type::Error);
  148. return false;
  149. }
  150. if (rename(make_full_path(m_name).characters(), new_file.characters())) {
  151. GUI::MessageBox::show(this, String::formatted("Could not rename file: {}!", strerror(errno)), "Error", GUI::MessageBox::Type::Error);
  152. return false;
  153. }
  154. m_name = new_name;
  155. m_name_dirty = false;
  156. update();
  157. }
  158. if (m_permissions_dirty) {
  159. if (chmod(make_full_path(m_name).characters(), m_mode)) {
  160. GUI::MessageBox::show(this, String::formatted("Could not update permissions: {}!", strerror(errno)), "Error", GUI::MessageBox::Type::Error);
  161. return false;
  162. }
  163. m_old_mode = m_mode;
  164. m_permissions_dirty = false;
  165. }
  166. update();
  167. m_apply_button->set_enabled(false);
  168. return true;
  169. }
  170. void PropertiesWindow::make_permission_checkboxes(GUI::Widget& parent, PermissionMasks masks, String label_string, mode_t mode)
  171. {
  172. auto& widget = parent.add<GUI::Widget>();
  173. widget.set_layout<GUI::HorizontalBoxLayout>();
  174. widget.set_fixed_height(16);
  175. widget.layout()->set_spacing(10);
  176. auto& label = widget.add<GUI::Label>(label_string);
  177. label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  178. struct stat st;
  179. if (lstat(m_path.characters(), &st)) {
  180. perror("stat");
  181. return;
  182. }
  183. auto can_edit_checkboxes = st.st_uid == getuid();
  184. auto& box_read = widget.add<GUI::CheckBox>("Read");
  185. box_read.set_checked(mode & masks.read);
  186. box_read.on_checked = [&, masks](bool checked) { permission_changed(masks.read, checked); };
  187. box_read.set_enabled(can_edit_checkboxes);
  188. auto& box_write = widget.add<GUI::CheckBox>("Write");
  189. box_write.set_checked(mode & masks.write);
  190. box_write.on_checked = [&, masks](bool checked) { permission_changed(masks.write, checked); };
  191. box_write.set_enabled(can_edit_checkboxes);
  192. auto& box_execute = widget.add<GUI::CheckBox>("Execute");
  193. box_execute.set_checked(mode & masks.execute);
  194. box_execute.on_checked = [&, masks](bool checked) { permission_changed(masks.execute, checked); };
  195. box_execute.set_enabled(can_edit_checkboxes);
  196. }
  197. void PropertiesWindow::make_property_value_pairs(const Vector<PropertyValuePair>& pairs, GUI::Widget& parent)
  198. {
  199. int max_width = 0;
  200. Vector<NonnullRefPtr<GUI::Label>> property_labels;
  201. property_labels.ensure_capacity(pairs.size());
  202. for (auto pair : pairs) {
  203. auto& label_container = parent.add<GUI::Widget>();
  204. label_container.set_layout<GUI::HorizontalBoxLayout>();
  205. label_container.set_fixed_height(14);
  206. label_container.layout()->set_spacing(12);
  207. auto& label_property = label_container.add<GUI::Label>(pair.property);
  208. label_property.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  209. if (!pair.link.has_value()) {
  210. label_container.add<GUI::Label>(pair.value).set_text_alignment(Gfx::TextAlignment::CenterLeft);
  211. } else {
  212. auto& link = label_container.add<GUI::LinkLabel>(pair.value);
  213. link.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  214. link.on_click = [pair]() {
  215. Desktop::Launcher::open(pair.link.value());
  216. };
  217. }
  218. max_width = max(max_width, label_property.font().width(pair.property));
  219. property_labels.append(label_property);
  220. }
  221. for (auto label : property_labels)
  222. label->set_fixed_width(max_width);
  223. }
  224. GUI::Button& PropertiesWindow::make_button(String text, GUI::Widget& parent)
  225. {
  226. auto& button = parent.add<GUI::Button>(text);
  227. button.set_fixed_size(70, 22);
  228. return button;
  229. }