2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-01-02 02:31:30 +03:00
|
|
|
#include "PropertiesWindow.h"
|
2020-05-26 14:52:44 +03:00
|
|
|
#include <AK/LexicalPath.h>
|
2021-03-24 23:14:16 +03:00
|
|
|
#include <AK/NumberFormat.h>
|
2019-11-20 23:52:15 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-12-20 21:36:47 +03:00
|
|
|
#include <LibDesktop/Launcher.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
#include <LibGUI/CheckBox.h>
|
2020-08-12 20:29:02 +03:00
|
|
|
#include <LibGUI/FileIconProvider.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/FilePicker.h>
|
2020-12-26 15:00:24 +03:00
|
|
|
#include <LibGUI/LinkLabel.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/MessageBox.h>
|
2020-12-30 05:53:37 +03:00
|
|
|
#include <LibGUI/SeparatorWidget.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/TabWidget.h>
|
2020-03-05 17:54:52 +03:00
|
|
|
#include <grp.h>
|
2020-01-31 04:26:21 +03:00
|
|
|
#include <limits.h>
|
2019-11-20 23:52:15 +03:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
2020-03-08 14:05:14 +03:00
|
|
|
#include <string.h>
|
2019-11-20 23:52:15 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-01-02 02:31:30 +03:00
|
|
|
PropertiesWindow::PropertiesWindow(const String& path, bool disable_rename, Window* parent_window)
|
|
|
|
: Window(parent_window)
|
2019-11-20 23:52:15 +03:00
|
|
|
{
|
2020-05-26 14:52:44 +03:00
|
|
|
auto lexical_path = LexicalPath(path);
|
2019-11-20 23:52:15 +03:00
|
|
|
|
2020-03-03 23:42:48 +03:00
|
|
|
auto& main_widget = set_main_widget<GUI::Widget>();
|
2020-03-04 11:43:54 +03:00
|
|
|
main_widget.set_layout<GUI::VerticalBoxLayout>();
|
2020-03-03 23:42:48 +03:00
|
|
|
main_widget.layout()->set_margins({ 4, 4, 4, 4 });
|
|
|
|
main_widget.set_fill_with_background_color(true);
|
2019-11-20 23:52:15 +03:00
|
|
|
|
|
|
|
set_rect({ 0, 0, 360, 420 });
|
|
|
|
set_resizable(false);
|
|
|
|
|
2020-03-04 21:07:55 +03:00
|
|
|
auto& tab_widget = main_widget.add<GUI::TabWidget>();
|
2019-11-20 23:52:15 +03:00
|
|
|
|
2020-04-04 12:10:07 +03:00
|
|
|
auto& general_tab = tab_widget.add_tab<GUI::Widget>("General");
|
|
|
|
general_tab.set_layout<GUI::VerticalBoxLayout>();
|
|
|
|
general_tab.layout()->set_margins({ 12, 8, 12, 8 });
|
|
|
|
general_tab.layout()->set_spacing(10);
|
2019-11-20 23:52:15 +03:00
|
|
|
|
2020-04-04 12:10:07 +03:00
|
|
|
auto& file_container = general_tab.add<GUI::Widget>();
|
2020-03-04 21:07:55 +03:00
|
|
|
file_container.set_layout<GUI::HorizontalBoxLayout>();
|
|
|
|
file_container.layout()->set_spacing(20);
|
2020-12-30 03:23:32 +03:00
|
|
|
file_container.set_fixed_height(34);
|
2019-11-20 23:52:15 +03:00
|
|
|
|
2020-07-22 16:29:51 +03:00
|
|
|
m_icon = file_container.add<GUI::ImageWidget>();
|
2020-12-30 03:23:32 +03:00
|
|
|
m_icon->set_fixed_size(32, 32);
|
2019-11-20 23:52:15 +03:00
|
|
|
|
2020-05-26 14:52:44 +03:00
|
|
|
m_name = lexical_path.basename();
|
|
|
|
m_path = lexical_path.string();
|
2020-07-31 23:12:57 +03:00
|
|
|
m_parent_path = lexical_path.dirname();
|
2019-11-20 23:52:15 +03:00
|
|
|
|
2020-03-04 21:07:55 +03:00
|
|
|
m_name_box = file_container.add<GUI::TextBox>();
|
2019-11-20 23:52:15 +03:00
|
|
|
m_name_box->set_text(m_name);
|
2020-07-18 19:10:48 +03:00
|
|
|
m_name_box->set_mode(disable_rename ? GUI::TextBox::Mode::DisplayOnly : GUI::TextBox::Mode::Editable);
|
2020-04-28 14:18:36 +03:00
|
|
|
m_name_box->on_change = [&]() {
|
|
|
|
m_name_dirty = m_name != m_name_box->text();
|
2020-06-18 20:11:46 +03:00
|
|
|
m_apply_button->set_enabled(m_name_dirty || m_permissions_dirty);
|
2019-11-20 23:52:15 +03:00
|
|
|
};
|
|
|
|
|
2020-02-06 13:56:38 +03:00
|
|
|
set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/properties.png"));
|
2020-12-30 05:53:37 +03:00
|
|
|
general_tab.add<GUI::SeparatorWidget>(Gfx::Orientation::Horizontal);
|
2019-11-20 23:52:15 +03:00
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if (lstat(path.characters(), &st)) {
|
|
|
|
perror("stat");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-29 20:35:20 +03:00
|
|
|
String owner_name;
|
|
|
|
String group_name;
|
|
|
|
|
|
|
|
if (auto* pw = getpwuid(st.st_uid)) {
|
|
|
|
owner_name = pw->pw_name;
|
|
|
|
} else {
|
|
|
|
owner_name = "n/a";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto* gr = getgrgid(st.st_gid)) {
|
|
|
|
group_name = gr->gr_name;
|
|
|
|
} else {
|
|
|
|
group_name = "n/a";
|
|
|
|
}
|
2019-11-20 23:52:15 +03:00
|
|
|
|
|
|
|
m_mode = st.st_mode;
|
2020-06-18 20:11:46 +03:00
|
|
|
m_old_mode = st.st_mode;
|
2019-11-20 23:52:15 +03:00
|
|
|
|
|
|
|
auto properties = Vector<PropertyValuePair>();
|
|
|
|
properties.append({ "Type:", get_description(m_mode) });
|
2021-03-02 00:27:37 +03:00
|
|
|
auto parent_link = URL::create_with_file_protocol(m_parent_path, m_name);
|
2021-07-03 17:28:23 +03:00
|
|
|
properties.append(PropertyValuePair { "Location:", path, parent_link });
|
2019-11-20 23:52:15 +03:00
|
|
|
|
|
|
|
if (S_ISLNK(m_mode)) {
|
2020-06-16 22:02:35 +03:00
|
|
|
auto link_destination = Core::File::read_link(path);
|
|
|
|
if (link_destination.is_null()) {
|
2019-11-20 23:52:15 +03:00
|
|
|
perror("readlink");
|
2020-03-05 20:23:10 +03:00
|
|
|
} else {
|
2020-12-20 21:36:47 +03:00
|
|
|
auto link_directory = LexicalPath(link_destination);
|
2021-03-02 00:27:37 +03:00
|
|
|
auto link_parent = URL::create_with_file_protocol(link_directory.dirname(), link_directory.basename());
|
2021-07-03 17:28:23 +03:00
|
|
|
properties.append({ "Link target:", link_destination, link_parent });
|
2019-11-20 23:52:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 23:14:16 +03:00
|
|
|
properties.append({ "Size:", human_readable_size_long(st.st_size) });
|
2020-10-05 15:59:50 +03:00
|
|
|
properties.append({ "Owner:", String::formatted("{} ({})", owner_name, st.st_uid) });
|
|
|
|
properties.append({ "Group:", String::formatted("{} ({})", group_name, st.st_gid) });
|
2020-02-02 17:07:41 +03:00
|
|
|
properties.append({ "Created at:", GUI::FileSystemModel::timestamp_string(st.st_ctime) });
|
|
|
|
properties.append({ "Last modified:", GUI::FileSystemModel::timestamp_string(st.st_mtime) });
|
2019-11-20 23:52:15 +03:00
|
|
|
|
|
|
|
make_property_value_pairs(properties, general_tab);
|
|
|
|
|
2020-12-30 05:53:37 +03:00
|
|
|
general_tab.add<GUI::SeparatorWidget>(Gfx::Orientation::Horizontal);
|
2019-11-20 23:52:15 +03:00
|
|
|
|
|
|
|
make_permission_checkboxes(general_tab, { S_IRUSR, S_IWUSR, S_IXUSR }, "Owner:", m_mode);
|
|
|
|
make_permission_checkboxes(general_tab, { S_IRGRP, S_IWGRP, S_IXGRP }, "Group:", m_mode);
|
|
|
|
make_permission_checkboxes(general_tab, { S_IROTH, S_IWOTH, S_IXOTH }, "Others:", m_mode);
|
|
|
|
|
2020-04-04 12:10:07 +03:00
|
|
|
general_tab.layout()->add_spacer();
|
2019-11-20 23:52:15 +03:00
|
|
|
|
2020-03-04 21:07:55 +03:00
|
|
|
auto& button_widget = main_widget.add<GUI::Widget>();
|
|
|
|
button_widget.set_layout<GUI::HorizontalBoxLayout>();
|
2020-12-30 03:23:32 +03:00
|
|
|
button_widget.set_fixed_height(24);
|
2020-03-04 21:07:55 +03:00
|
|
|
button_widget.layout()->set_spacing(5);
|
2019-11-20 23:52:15 +03:00
|
|
|
|
2020-03-04 21:07:55 +03:00
|
|
|
button_widget.layout()->add_spacer();
|
2019-11-20 23:52:15 +03:00
|
|
|
|
2020-05-12 21:30:33 +03:00
|
|
|
make_button("OK", button_widget).on_click = [this](auto) {
|
2020-03-03 19:02:38 +03:00
|
|
|
if (apply_changes())
|
|
|
|
close();
|
|
|
|
};
|
2020-05-12 21:30:33 +03:00
|
|
|
make_button("Cancel", button_widget).on_click = [this](auto) {
|
2020-03-03 19:02:38 +03:00
|
|
|
close();
|
|
|
|
};
|
2019-11-20 23:52:15 +03:00
|
|
|
|
|
|
|
m_apply_button = make_button("Apply", button_widget);
|
2020-05-12 21:30:33 +03:00
|
|
|
m_apply_button->on_click = [this](auto) { apply_changes(); };
|
2019-11-20 23:52:15 +03:00
|
|
|
m_apply_button->set_enabled(false);
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2021-01-02 02:31:30 +03:00
|
|
|
PropertiesWindow::~PropertiesWindow()
|
|
|
|
{
|
|
|
|
}
|
2019-11-20 23:52:15 +03:00
|
|
|
|
2021-01-02 02:31:30 +03:00
|
|
|
void PropertiesWindow::update()
|
2019-11-20 23:52:15 +03:00
|
|
|
{
|
2020-12-16 18:09:27 +03:00
|
|
|
m_icon->set_bitmap(GUI::FileIconProvider::icon_for_path(make_full_path(m_name), m_mode).bitmap_for_size(32));
|
2020-10-05 15:59:50 +03:00
|
|
|
set_title(String::formatted("{} - Properties", m_name));
|
2019-11-20 23:52:15 +03:00
|
|
|
}
|
|
|
|
|
2021-01-02 02:31:30 +03:00
|
|
|
void PropertiesWindow::permission_changed(mode_t mask, bool set)
|
2019-11-20 23:52:15 +03:00
|
|
|
{
|
|
|
|
if (set) {
|
|
|
|
m_mode |= mask;
|
|
|
|
} else {
|
|
|
|
m_mode &= ~mask;
|
|
|
|
}
|
|
|
|
|
2020-06-18 20:11:46 +03:00
|
|
|
m_permissions_dirty = m_mode != m_old_mode;
|
|
|
|
m_apply_button->set_enabled(m_name_dirty || m_permissions_dirty);
|
2019-11-20 23:52:15 +03:00
|
|
|
}
|
|
|
|
|
2021-01-02 02:31:30 +03:00
|
|
|
String PropertiesWindow::make_full_path(const String& name)
|
2019-11-20 23:52:15 +03:00
|
|
|
{
|
2020-10-05 15:59:50 +03:00
|
|
|
return String::formatted("{}/{}", m_parent_path, name);
|
2019-11-20 23:52:15 +03:00
|
|
|
}
|
|
|
|
|
2021-01-02 02:31:30 +03:00
|
|
|
bool PropertiesWindow::apply_changes()
|
2019-11-20 23:52:15 +03:00
|
|
|
{
|
|
|
|
if (m_name_dirty) {
|
|
|
|
String new_name = m_name_box->text();
|
|
|
|
String new_file = make_full_path(new_name).characters();
|
|
|
|
|
2021-02-20 01:46:54 +03:00
|
|
|
if (Core::File::exists(new_file)) {
|
2020-10-05 15:59:50 +03:00
|
|
|
GUI::MessageBox::show(this, String::formatted("A file \"{}\" already exists!", new_name), "Error", GUI::MessageBox::Type::Error);
|
2019-11-20 23:52:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rename(make_full_path(m_name).characters(), new_file.characters())) {
|
2020-10-05 15:59:50 +03:00
|
|
|
GUI::MessageBox::show(this, String::formatted("Could not rename file: {}!", strerror(errno)), "Error", GUI::MessageBox::Type::Error);
|
2019-11-20 23:52:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_name = new_name;
|
|
|
|
m_name_dirty = false;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_permissions_dirty) {
|
|
|
|
if (chmod(make_full_path(m_name).characters(), m_mode)) {
|
2020-10-05 15:59:50 +03:00
|
|
|
GUI::MessageBox::show(this, String::formatted("Could not update permissions: {}!", strerror(errno)), "Error", GUI::MessageBox::Type::Error);
|
2019-11-20 23:52:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-18 20:11:46 +03:00
|
|
|
m_old_mode = m_mode;
|
2019-11-20 23:52:15 +03:00
|
|
|
m_permissions_dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
m_apply_button->set_enabled(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-02 02:31:30 +03:00
|
|
|
void PropertiesWindow::make_permission_checkboxes(GUI::Widget& parent, PermissionMasks masks, String label_string, mode_t mode)
|
2019-11-20 23:52:15 +03:00
|
|
|
{
|
2020-04-04 12:10:07 +03:00
|
|
|
auto& widget = parent.add<GUI::Widget>();
|
2020-03-04 21:07:55 +03:00
|
|
|
widget.set_layout<GUI::HorizontalBoxLayout>();
|
2020-12-30 03:23:32 +03:00
|
|
|
widget.set_fixed_height(16);
|
2020-03-04 21:07:55 +03:00
|
|
|
widget.layout()->set_spacing(10);
|
|
|
|
|
|
|
|
auto& label = widget.add<GUI::Label>(label_string);
|
|
|
|
label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
|
|
|
|
2020-04-27 20:41:15 +03:00
|
|
|
struct stat st;
|
|
|
|
if (lstat(m_path.characters(), &st)) {
|
|
|
|
perror("stat");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto can_edit_checkboxes = st.st_uid == getuid();
|
|
|
|
|
2020-03-04 21:07:55 +03:00
|
|
|
auto& box_read = widget.add<GUI::CheckBox>("Read");
|
|
|
|
box_read.set_checked(mode & masks.read);
|
|
|
|
box_read.on_checked = [&, masks](bool checked) { permission_changed(masks.read, checked); };
|
2020-04-27 20:41:15 +03:00
|
|
|
box_read.set_enabled(can_edit_checkboxes);
|
2020-03-04 21:07:55 +03:00
|
|
|
|
|
|
|
auto& box_write = widget.add<GUI::CheckBox>("Write");
|
|
|
|
box_write.set_checked(mode & masks.write);
|
|
|
|
box_write.on_checked = [&, masks](bool checked) { permission_changed(masks.write, checked); };
|
2020-04-27 20:41:15 +03:00
|
|
|
box_write.set_enabled(can_edit_checkboxes);
|
2020-03-04 21:07:55 +03:00
|
|
|
|
|
|
|
auto& box_execute = widget.add<GUI::CheckBox>("Execute");
|
|
|
|
box_execute.set_checked(mode & masks.execute);
|
|
|
|
box_execute.on_checked = [&, masks](bool checked) { permission_changed(masks.execute, checked); };
|
2020-04-27 20:41:15 +03:00
|
|
|
box_execute.set_enabled(can_edit_checkboxes);
|
2019-11-20 23:52:15 +03:00
|
|
|
}
|
|
|
|
|
2021-01-02 02:31:30 +03:00
|
|
|
void PropertiesWindow::make_property_value_pairs(const Vector<PropertyValuePair>& pairs, GUI::Widget& parent)
|
2019-11-20 23:52:15 +03:00
|
|
|
{
|
|
|
|
int max_width = 0;
|
2020-02-02 17:07:41 +03:00
|
|
|
Vector<NonnullRefPtr<GUI::Label>> property_labels;
|
2019-11-20 23:52:15 +03:00
|
|
|
|
|
|
|
property_labels.ensure_capacity(pairs.size());
|
|
|
|
for (auto pair : pairs) {
|
2020-03-04 21:07:55 +03:00
|
|
|
auto& label_container = parent.add<GUI::Widget>();
|
|
|
|
label_container.set_layout<GUI::HorizontalBoxLayout>();
|
2020-12-30 03:23:32 +03:00
|
|
|
label_container.set_fixed_height(14);
|
2020-03-04 21:07:55 +03:00
|
|
|
label_container.layout()->set_spacing(12);
|
2019-11-20 23:52:15 +03:00
|
|
|
|
2020-03-04 21:07:55 +03:00
|
|
|
auto& label_property = label_container.add<GUI::Label>(pair.property);
|
|
|
|
label_property.set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
2019-11-20 23:52:15 +03:00
|
|
|
|
2020-12-20 21:36:47 +03:00
|
|
|
if (!pair.link.has_value()) {
|
|
|
|
label_container.add<GUI::Label>(pair.value).set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
|
|
|
} else {
|
2020-12-26 15:00:24 +03:00
|
|
|
auto& link = label_container.add<GUI::LinkLabel>(pair.value);
|
2020-12-20 21:36:47 +03:00
|
|
|
link.set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
|
|
|
link.on_click = [pair]() {
|
|
|
|
Desktop::Launcher::open(pair.link.value());
|
|
|
|
};
|
|
|
|
}
|
2019-11-20 23:52:15 +03:00
|
|
|
|
2020-03-04 21:07:55 +03:00
|
|
|
max_width = max(max_width, label_property.font().width(pair.property));
|
2019-11-20 23:52:15 +03:00
|
|
|
property_labels.append(label_property);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto label : property_labels)
|
2020-12-30 03:23:32 +03:00
|
|
|
label->set_fixed_width(max_width);
|
2019-11-20 23:52:15 +03:00
|
|
|
}
|
|
|
|
|
2021-01-02 02:31:30 +03:00
|
|
|
GUI::Button& PropertiesWindow::make_button(String text, GUI::Widget& parent)
|
2019-11-20 23:52:15 +03:00
|
|
|
{
|
2020-03-04 21:07:55 +03:00
|
|
|
auto& button = parent.add<GUI::Button>(text);
|
2020-12-30 03:23:32 +03:00
|
|
|
button.set_fixed_size(70, 22);
|
2019-11-20 23:52:15 +03:00
|
|
|
return button;
|
|
|
|
}
|