PropertiesDialog.cpp 12 KB

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