PropertiesWindow.cpp 11 KB

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