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