diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-03-02 02:05:49 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-03-02 02:05:49 +0100 |
commit | 0c8242dd714dcb57c91798f237a1f3d5948e784c (patch) | |
tree | 7c574c3b98c96ae1dafbfc29b47c7ab8b889ac45 /Applications/FileManager/DirectoryTableModel.cpp | |
parent | 2c5a378ccc916cc8ffb9eb58bbd6558bdfe7f7a7 (diff) | |
download | serenity-0c8242dd714dcb57c91798f237a1f3d5948e784c.zip |
FileManager: Show user/group names instead of UID/GID if available.
Diffstat (limited to 'Applications/FileManager/DirectoryTableModel.cpp')
-rw-r--r-- | Applications/FileManager/DirectoryTableModel.cpp | 43 |
1 files changed, 35 insertions, 8 deletions
diff --git a/Applications/FileManager/DirectoryTableModel.cpp b/Applications/FileManager/DirectoryTableModel.cpp index f610bcbecc..c871d29342 100644 --- a/Applications/FileManager/DirectoryTableModel.cpp +++ b/Applications/FileManager/DirectoryTableModel.cpp @@ -2,6 +2,8 @@ #include <dirent.h> #include <stdio.h> #include <unistd.h> +#include <grp.h> +#include <pwd.h> #include <AK/FileSystemPath.h> #include <AK/StringBuilder.h> @@ -12,6 +14,16 @@ DirectoryTableModel::DirectoryTableModel() m_symlink_icon = GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/link16.rgb", { 16, 16 }); m_socket_icon = GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/socket16.rgb", { 16, 16 }); m_executable_icon = GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/executable16.rgb", { 16, 16 }); + + setpwent(); + while (auto* passwd = getpwent()) + m_user_names.set(passwd->pw_uid, passwd->pw_name); + endpwent(); + + setgrent(); + while (auto* group = getgrent()) + m_group_names.set(group->gr_gid, group->gr_name); + endgrent(); } DirectoryTableModel::~DirectoryTableModel() @@ -34,8 +46,8 @@ String DirectoryTableModel::column_name(int column) const case Column::Icon: return ""; case Column::Name: return "Name"; case Column::Size: return "Size"; - case Column::UID: return "UID"; - case Column::GID: return "GID"; + case Column::Owner: return "Owner"; + case Column::Group: return "Group"; case Column::Permissions: return "Mode"; case Column::Inode: return "Inode"; } @@ -48,9 +60,9 @@ GTableModel::ColumnMetadata DirectoryTableModel::column_metadata(int column) con case Column::Icon: return { 16, TextAlignment::Center }; case Column::Name: return { 120, TextAlignment::CenterLeft }; case Column::Size: return { 80, TextAlignment::CenterRight }; - case Column::UID: return { 80, TextAlignment::CenterRight }; - case Column::GID: return { 80, TextAlignment::CenterRight }; - case Column::Permissions: return { 100, TextAlignment::CenterLeft }; + case Column::Owner: return { 50, TextAlignment::CenterLeft }; + case Column::Group: return { 50, TextAlignment::CenterLeft }; + case Column::Permissions: return { 80, TextAlignment::CenterLeft }; case Column::Inode: return { 80, TextAlignment::CenterRight }; } ASSERT_NOT_REACHED(); @@ -69,7 +81,6 @@ const GraphicsBitmap& DirectoryTableModel::icon_for(const Entry& entry) const return *m_file_icon; } - static String permission_string(mode_t mode) { StringBuilder builder; @@ -108,6 +119,22 @@ static String permission_string(mode_t mode) return builder.to_string(); } +String DirectoryTableModel::name_for_uid(uid_t uid) const +{ + auto it = m_user_names.find(uid); + if (it == m_user_names.end()) + return String::format("%u", uid); + return (*it).value; +} + +String DirectoryTableModel::name_for_gid(uid_t gid) const +{ + auto it = m_user_names.find(gid); + if (it == m_user_names.end()) + return String::format("%u", gid); + return (*it).value; +} + GVariant DirectoryTableModel::data(int row, int column) const { auto& entry = this->entry(row); @@ -115,8 +142,8 @@ GVariant DirectoryTableModel::data(int row, int column) const case Column::Icon: return icon_for(entry); case Column::Name: return entry.name; case Column::Size: return (int)entry.size; - case Column::UID: return (int)entry.uid; - case Column::GID: return (int)entry.gid; + case Column::Owner: return name_for_uid(entry.uid); + case Column::Group: return name_for_gid(entry.gid); case Column::Permissions: return permission_string(entry.mode); case Column::Inode: return (int)entry.inode; } |