PropertiesDialog.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/StringBuilder.h>
  28. #include <LibGUI/GBoxLayout.h>
  29. #include <LibGUI/GCheckBox.h>
  30. #include <LibGUI/GFilePicker.h>
  31. #include <LibGUI/GMessageBox.h>
  32. #include <LibGUI/GTabWidget.h>
  33. #include <limits.h>
  34. #include <pwd.h>
  35. #include <stdio.h>
  36. #include <unistd.h>
  37. PropertiesDialog::PropertiesDialog(GUI::FileSystemModel& model, String path, bool disable_rename, Core::Object* parent)
  38. : Dialog(parent)
  39. , m_model(model)
  40. {
  41. auto file_path = FileSystemPath(path);
  42. ASSERT(file_path.is_valid());
  43. auto main_widget = GUI::Widget::construct();
  44. main_widget->set_layout(make<GUI::VBoxLayout>());
  45. main_widget->layout()->set_margins({ 4, 4, 4, 4 });
  46. main_widget->set_fill_with_background_color(true);
  47. set_main_widget(main_widget);
  48. set_rect({ 0, 0, 360, 420 });
  49. set_resizable(false);
  50. auto tab_widget = GUI::TabWidget::construct(main_widget);
  51. auto general_tab = GUI::Widget::construct(tab_widget.ptr());
  52. general_tab->set_layout(make<GUI::VBoxLayout>());
  53. general_tab->layout()->set_margins({ 12, 8, 12, 8 });
  54. general_tab->layout()->set_spacing(10);
  55. tab_widget->add_widget("General", general_tab);
  56. general_tab->layout()->add_spacer();
  57. auto file_container = GUI::Widget::construct(general_tab.ptr());
  58. file_container->set_layout(make<GUI::HBoxLayout>());
  59. file_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  60. file_container->layout()->set_spacing(20);
  61. file_container->set_preferred_size(0, 34);
  62. m_icon = GUI::Label::construct(file_container);
  63. m_icon->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  64. m_icon->set_preferred_size(32, 32);
  65. m_name = file_path.basename();
  66. m_name_box = GUI::TextBox::construct(file_container);
  67. m_name_box->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  68. m_name_box->set_preferred_size({ 0, 22 });
  69. m_name_box->set_text(m_name);
  70. m_name_box->on_change = [&, disable_rename]() {
  71. if (disable_rename) {
  72. m_name_box->set_text(m_name); //FIXME: GTextBox does not support set_enabled yet...
  73. } else {
  74. m_name_dirty = m_name != m_name_box->text();
  75. m_apply_button->set_enabled(true);
  76. }
  77. };
  78. set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/properties.png"));
  79. make_divider(general_tab);
  80. struct stat st;
  81. if (lstat(path.characters(), &st)) {
  82. perror("stat");
  83. return;
  84. }
  85. struct passwd* user_pw = getpwuid(st.st_uid);
  86. struct passwd* group_pw = getpwuid(st.st_gid);
  87. ASSERT(user_pw && group_pw);
  88. m_mode = st.st_mode;
  89. auto properties = Vector<PropertyValuePair>();
  90. properties.append({ "Type:", get_description(m_mode) });
  91. properties.append({ "Location:", path });
  92. if (S_ISLNK(m_mode)) {
  93. char link_destination[PATH_MAX];
  94. if (readlink(path.characters(), link_destination, sizeof(link_destination))) {
  95. perror("readlink");
  96. return;
  97. }
  98. properties.append({ "Link target:", link_destination });
  99. }
  100. properties.append({ "Size:", String::format("%zu bytes", st.st_size) });
  101. properties.append({ "Owner:", String::format("%s (%lu)", user_pw->pw_name, static_cast<u32>(user_pw->pw_uid)) });
  102. properties.append({ "Group:", String::format("%s (%lu)", group_pw->pw_name, static_cast<u32>(group_pw->pw_uid)) });
  103. properties.append({ "Created at:", GUI::FileSystemModel::timestamp_string(st.st_ctime) });
  104. properties.append({ "Last modified:", GUI::FileSystemModel::timestamp_string(st.st_mtime) });
  105. make_property_value_pairs(properties, general_tab);
  106. make_divider(general_tab);
  107. make_permission_checkboxes(general_tab, { S_IRUSR, S_IWUSR, S_IXUSR }, "Owner:", m_mode);
  108. make_permission_checkboxes(general_tab, { S_IRGRP, S_IWGRP, S_IXGRP }, "Group:", m_mode);
  109. make_permission_checkboxes(general_tab, { S_IROTH, S_IWOTH, S_IXOTH }, "Others:", m_mode);
  110. general_tab->layout()->add_spacer();
  111. auto button_widget = GUI::Widget::construct(main_widget.ptr());
  112. button_widget->set_layout(make<GUI::HBoxLayout>());
  113. button_widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  114. button_widget->set_preferred_size(0, 24);
  115. button_widget->layout()->set_spacing(5);
  116. button_widget->layout()->add_spacer();
  117. make_button("OK", button_widget)->on_click = [&](auto&) {if(apply_changes()) close(); };
  118. make_button("Cancel", button_widget)->on_click = [&](auto&) { close(); };
  119. m_apply_button = make_button("Apply", button_widget);
  120. m_apply_button->on_click = [&](auto&) { apply_changes(); };
  121. m_apply_button->set_enabled(false);
  122. update();
  123. }
  124. PropertiesDialog::~PropertiesDialog() {}
  125. void PropertiesDialog::update()
  126. {
  127. m_icon->set_icon(const_cast<Gfx::Bitmap*>(m_model.icon_for_file(m_mode, m_name).bitmap_for_size(32)));
  128. set_title(String::format("Properties of \"%s\"", m_name.characters()));
  129. }
  130. void PropertiesDialog::permission_changed(mode_t mask, bool set)
  131. {
  132. if (set) {
  133. m_mode |= mask;
  134. } else {
  135. m_mode &= ~mask;
  136. }
  137. m_permissions_dirty = true;
  138. m_apply_button->set_enabled(true);
  139. }
  140. String PropertiesDialog::make_full_path(String name)
  141. {
  142. return String::format("%s/%s", m_model.root_path().characters(), name.characters());
  143. }
  144. bool PropertiesDialog::apply_changes()
  145. {
  146. if (m_name_dirty) {
  147. String new_name = m_name_box->text();
  148. String new_file = make_full_path(new_name).characters();
  149. if (GUI::FilePicker::file_exists(new_file)) {
  150. GUI::MessageBox::show(String::format("A file \"%s\" already exists!", new_name.characters()), "Error", GUI::MessageBox::Type::Error);
  151. return false;
  152. }
  153. if (rename(make_full_path(m_name).characters(), new_file.characters())) {
  154. GUI::MessageBox::show(String::format("Could not rename file: %s!", strerror(errno)), "Error", GUI::MessageBox::Type::Error);
  155. return false;
  156. }
  157. m_name = new_name;
  158. m_name_dirty = false;
  159. update();
  160. }
  161. if (m_permissions_dirty) {
  162. if (chmod(make_full_path(m_name).characters(), m_mode)) {
  163. GUI::MessageBox::show(String::format("Could not update permissions: %s!", strerror(errno)), "Error", GUI::MessageBox::Type::Error);
  164. return false;
  165. }
  166. m_permissions_dirty = false;
  167. }
  168. update();
  169. m_apply_button->set_enabled(false);
  170. return true;
  171. }
  172. void PropertiesDialog::make_permission_checkboxes(NonnullRefPtr<GUI::Widget>& parent, PermissionMasks masks, String label_string, mode_t mode)
  173. {
  174. auto widget = GUI::Widget::construct(parent.ptr());
  175. widget->set_layout(make<GUI::HBoxLayout>());
  176. widget->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  177. widget->set_preferred_size(0, 16);
  178. widget->layout()->set_spacing(10);
  179. auto label = GUI::Label::construct(label_string, widget);
  180. label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  181. auto box_read = GUI::CheckBox::construct("Read", widget);
  182. box_read->set_checked(mode & masks.read);
  183. box_read->on_checked = [&, masks](bool checked) { permission_changed(masks.read, checked); };
  184. auto box_write = GUI::CheckBox::construct("Write", widget);
  185. box_write->set_checked(mode & masks.write);
  186. box_write->on_checked = [&, masks](bool checked) { permission_changed(masks.write, checked); };
  187. auto box_execute = GUI::CheckBox::construct("Execute", widget);
  188. box_execute->set_checked(mode & masks.execute);
  189. box_execute->on_checked = [&, masks](bool checked) { permission_changed(masks.execute, checked); };
  190. }
  191. void PropertiesDialog::make_property_value_pairs(const Vector<PropertyValuePair>& pairs, NonnullRefPtr<GUI::Widget>& parent)
  192. {
  193. int max_width = 0;
  194. Vector<NonnullRefPtr<GUI::Label>> property_labels;
  195. property_labels.ensure_capacity(pairs.size());
  196. for (auto pair : pairs) {
  197. auto label_container = GUI::Widget::construct(parent.ptr());
  198. label_container->set_layout(make<GUI::HBoxLayout>());
  199. label_container->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  200. label_container->set_preferred_size(0, 14);
  201. label_container->layout()->set_spacing(12);
  202. auto label_property = GUI::Label::construct(pair.property, label_container);
  203. label_property->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  204. label_property->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  205. GUI::Label::construct(pair.value, label_container)->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  206. max_width = max(max_width, label_property->font().width(pair.property));
  207. property_labels.append(label_property);
  208. }
  209. for (auto label : property_labels)
  210. label->set_preferred_size({ max_width, 0 });
  211. }
  212. NonnullRefPtr<GUI::Button> PropertiesDialog::make_button(String text, NonnullRefPtr<GUI::Widget>& parent)
  213. {
  214. auto button = GUI::Button::construct(text, parent.ptr());
  215. button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  216. button->set_preferred_size(70, 22);
  217. return button;
  218. }
  219. void PropertiesDialog::make_divider(NonnullRefPtr<GUI::Widget>& parent)
  220. {
  221. parent->layout()->add_spacer();
  222. auto divider = GUI::Frame::construct(parent.ptr());
  223. divider->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  224. divider->set_preferred_size({ 0, 2 });
  225. divider->set_frame_shape(Gfx::FrameShape::HorizontalLine);
  226. divider->set_frame_shadow(Gfx::FrameShadow::Sunken);
  227. divider->set_frame_thickness(2);
  228. parent->layout()->add_spacer();
  229. }