ProcessManager: Use a GJsonArrayModel for the process file desciptors

This commit is contained in:
Andreas Kling 2019-08-10 11:07:55 +02:00
parent 078ce97c41
commit 2eead3dfc0
Notes: sideshowbarker 2024-07-19 12:46:21 +09:00
4 changed files with 13 additions and 128 deletions

View File

@ -9,7 +9,6 @@ OBJS = \
ProcessMemoryMapWidget.o \
ProcessMemoryMapModel.o \
ProcessFileDescriptorMapWidget.o \
ProcessFileDescriptorMapModel.o \
NetworkStatisticsWidget.o \
main.o

View File

@ -1,92 +0,0 @@
#include "ProcessFileDescriptorMapModel.h"
#include <AK/JsonObject.h>
#include <AK/StringBuilder.h>
#include <LibCore/CFile.h>
void ProcessFileDescriptorMapModel::update()
{
CFile file(String::format("/proc/%d/fds", m_pid));
if (!file.open(CIODevice::ReadOnly)) {
dbg() << "Unable to open " << file.filename();
return;
}
auto json = JsonValue::from_string(file.read_all());
ASSERT(json.is_array());
m_process_fds = json.as_array();
did_update();
}
int ProcessFileDescriptorMapModel::row_count(const GModelIndex&) const
{
return m_process_fds.size();
}
String ProcessFileDescriptorMapModel::column_name(int column) const
{
switch (column) {
case Column::FileDescriptor:
return "FD";
case Column::ClassName:
return "Class";
case Column::Offset:
return "Offset";
case Column::Access:
return "Access";
case Column::Path:
return "Path";
default:
ASSERT_NOT_REACHED();
}
}
GModel::ColumnMetadata ProcessFileDescriptorMapModel::column_metadata(int column) const
{
switch (column) {
case Column::FileDescriptor:
return { 32, TextAlignment::CenterRight };
case Column::ClassName:
return { 80, TextAlignment::CenterLeft };
case Column::Offset:
return { 40, TextAlignment::CenterRight };
case Column::Access:
return { 60, TextAlignment::CenterLeft };
case Column::Path:
return { 300, TextAlignment::CenterLeft };
default:
ASSERT_NOT_REACHED();
}
return {};
}
GVariant ProcessFileDescriptorMapModel::data(const GModelIndex& index, Role role) const
{
auto& fd_object = m_process_fds.at(index.row()).as_object();
if (role == GModel::Role::Display) {
switch (index.column()) {
case Column::FileDescriptor:
return fd_object.get("fd").to_int();
case Column::ClassName:
return fd_object.get("class").to_string();
case Column::Offset:
return fd_object.get("offset").to_int();
case Column::Access:
return fd_object.get("seekable").to_bool() ? "Seekable" : "Sequential";
case Column::Path:
return fd_object.get("absolute_path").to_string();
default:
ASSERT_NOT_REACHED();
}
}
return {};
}
void ProcessFileDescriptorMapModel::set_pid(pid_t pid)
{
if (m_pid == pid)
return;
m_pid = pid;
update();
}

View File

@ -1,32 +0,0 @@
#pragma once
#include <AK/JsonArray.h>
#include <LibGUI/GModel.h>
class ProcessFileDescriptorMapModel final : public GModel {
public:
enum Column {
FileDescriptor,
Path,
Offset,
Access,
ClassName,
__Count
};
ProcessFileDescriptorMapModel() {}
virtual ~ProcessFileDescriptorMapModel() override {}
virtual int row_count(const GModelIndex& = GModelIndex()) const override;
virtual int column_count(const GModelIndex& = GModelIndex()) const override { return Column::__Count; }
virtual String column_name(int) const override;
virtual ColumnMetadata column_metadata(int) const override;
virtual GVariant data(const GModelIndex&, Role = Role::Display) const override;
virtual void update() override;
void set_pid(pid_t);
private:
JsonArray m_process_fds;
int m_pid { -1 };
};

View File

@ -1,6 +1,6 @@
#include "ProcessFileDescriptorMapWidget.h"
#include "ProcessFileDescriptorMapModel.h"
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GJsonArrayModel.h>
#include <LibGUI/GTableView.h>
ProcessFileDescriptorMapWidget::ProcessFileDescriptorMapWidget(GWidget* parent)
@ -10,7 +10,17 @@ ProcessFileDescriptorMapWidget::ProcessFileDescriptorMapWidget(GWidget* parent)
layout()->set_margins({ 4, 4, 4, 4 });
m_table_view = new GTableView(this);
m_table_view->set_size_columns_to_fit_content(true);
m_table_view->set_model(adopt(*new ProcessFileDescriptorMapModel));
Vector<GJsonArrayModel::FieldSpec> pid_fds_fields;
pid_fds_fields.empend("fd", "FD", TextAlignment::CenterRight);
pid_fds_fields.empend("class", "Class", TextAlignment::CenterLeft);
pid_fds_fields.empend("offset", "Offset", TextAlignment::CenterRight);
pid_fds_fields.empend("absolute_path", "Path", TextAlignment::CenterLeft);
pid_fds_fields.empend("seekable", "Access", TextAlignment::CenterLeft, [](auto& data) {
return data.to_bool() ? "Seekable" : "Sequential";
});
m_table_view->set_model(GJsonArrayModel::create({}, move(pid_fds_fields)));
}
ProcessFileDescriptorMapWidget::~ProcessFileDescriptorMapWidget()
@ -22,5 +32,5 @@ void ProcessFileDescriptorMapWidget::set_pid(pid_t pid)
if (m_pid == pid)
return;
m_pid = pid;
static_cast<ProcessFileDescriptorMapModel*>(m_table_view->model())->set_pid(pid);
static_cast<GJsonArrayModel*>(m_table_view->model())->set_json_path(String::format("/proc/%d/fds", m_pid));
}