summaryrefslogtreecommitdiff
path: root/Userland/Applications/SystemMonitor/ProcessFileDescriptorMapWidget.cpp
blob: 6bd0e8d0f3151dc96932f465f6178465f5b1e343 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "ProcessFileDescriptorMapWidget.h"
#include <LibGUI/BoxLayout.h>
#include <LibGUI/JsonArrayModel.h>
#include <LibGUI/SortingProxyModel.h>
#include <LibGUI/TableView.h>

REGISTER_WIDGET(SystemMonitor, ProcessFileDescriptorMapWidget)

namespace SystemMonitor {

ErrorOr<NonnullRefPtr<ProcessFileDescriptorMapWidget>> ProcessFileDescriptorMapWidget::try_create()
{
    auto widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ProcessFileDescriptorMapWidget()));
    TRY(widget->try_set_layout<GUI::VerticalBoxLayout>(4));
    widget->m_table_view = TRY(widget->try_add<GUI::TableView>());

    Vector<GUI::JsonArrayModel::FieldSpec> pid_fds_fields;
    TRY(pid_fds_fields.try_empend("fd", "FD"_short_string, Gfx::TextAlignment::CenterRight));
    TRY(pid_fds_fields.try_empend("class", "Class"_short_string, Gfx::TextAlignment::CenterLeft));
    TRY(pid_fds_fields.try_empend("offset", "Offset"_short_string, Gfx::TextAlignment::CenterRight));
    TRY(pid_fds_fields.try_empend("absolute_path", "Path"_short_string, Gfx::TextAlignment::CenterLeft));
    TRY(pid_fds_fields.try_empend("Access"_short_string, Gfx::TextAlignment::CenterLeft, [](auto& object) {
        return object.get_bool("seekable"sv).value_or(false) ? "Seekable" : "Sequential";
    }));
    TRY(pid_fds_fields.try_empend(TRY("Blocking"_string), Gfx::TextAlignment::CenterLeft, [](auto& object) {
        return object.get_bool("blocking"sv).value_or(false) ? "Blocking" : "Nonblocking";
    }));
    TRY(pid_fds_fields.try_empend("On exec"_short_string, Gfx::TextAlignment::CenterLeft, [](auto& object) {
        return object.get_bool("cloexec"sv).value_or(false) ? "Close" : "Keep";
    }));
    TRY(pid_fds_fields.try_empend(TRY("Can read"_string), Gfx::TextAlignment::CenterLeft, [](auto& object) {
        return object.get_bool("can_read"sv).value_or(false) ? "Yes" : "No";
    }));
    TRY(pid_fds_fields.try_empend(TRY("Can write"_string), Gfx::TextAlignment::CenterLeft, [](auto& object) {
        return object.get_bool("can_write"sv).value_or(false) ? "Yes" : "No";
    }));

    widget->m_model = GUI::JsonArrayModel::create({}, move(pid_fds_fields));
    widget->m_table_view->set_model(TRY(GUI::SortingProxyModel::create(*widget->m_model)));

    return widget;
}

void ProcessFileDescriptorMapWidget::set_pid(pid_t pid)
{
    if (m_pid == pid)
        return;
    m_pid = pid;
    m_model->set_json_path(DeprecatedString::formatted("/proc/{}/fds", m_pid));
}

}