PropertiesDialog.cpp 11 KB

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