summaryrefslogtreecommitdiff
path: root/Userland/Applications/SystemMonitor/NetworkStatisticsWidget.cpp
blob: 8518a06f81d34fabc751ea3a5a0941f4937f42ea (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "NetworkStatisticsWidget.h"
#include <LibGUI/BoxLayout.h>
#include <LibGUI/GroupBox.h>
#include <LibGUI/JsonArrayModel.h>
#include <LibGUI/SortingProxyModel.h>
#include <LibGUI/TableView.h>
#include <LibGfx/Painter.h>

REGISTER_WIDGET(SystemMonitor, NetworkStatisticsWidget)

namespace SystemMonitor {

NetworkStatisticsWidget::NetworkStatisticsWidget()
{
    on_first_show = [this](auto&) {
        set_layout<GUI::VerticalBoxLayout>();
        layout()->set_margins(4);
        set_fill_with_background_color(true);

        m_network_connected_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/network-connected.png").release_value_but_fixme_should_propagate_errors();
        m_network_disconnected_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/network-disconnected.png").release_value_but_fixme_should_propagate_errors();

        m_network_link_down_bitmap = Gfx::Bitmap::try_create(m_network_connected_bitmap->format(), m_network_connected_bitmap->size()).release_value_but_fixme_should_propagate_errors();
        {
            Gfx::Painter painter(*m_network_link_down_bitmap);
            painter.blit_filtered({}, *m_network_connected_bitmap, m_network_connected_bitmap->rect(), [](Color color) {
                return color.to_grayscale();
            });
        }

        auto& adapters_group_box = add<GUI::GroupBox>("Adapters");
        adapters_group_box.set_layout<GUI::VerticalBoxLayout>();
        adapters_group_box.layout()->set_margins(6);
        adapters_group_box.set_fixed_height(120);

        m_adapter_table_view = adapters_group_box.add<GUI::TableView>();

        Vector<GUI::JsonArrayModel::FieldSpec> net_adapters_fields;
        net_adapters_fields.empend("", Gfx::TextAlignment::CenterLeft,
            [this](JsonObject const& object) -> GUI::Variant {
                if (!object.get("link_up").as_bool())
                    return *m_network_link_down_bitmap;
                else
                    return object.get("ipv4_address").as_string_or("").is_empty() ? *m_network_disconnected_bitmap : *m_network_connected_bitmap;
            });
        net_adapters_fields.empend("name", "Name", Gfx::TextAlignment::CenterLeft);
        net_adapters_fields.empend("class_name", "Class", Gfx::TextAlignment::CenterLeft);
        net_adapters_fields.empend("mac_address", "MAC", Gfx::TextAlignment::CenterLeft);
        net_adapters_fields.empend("Link status", Gfx::TextAlignment::CenterLeft,
            [](JsonObject const& object) -> String {
                if (!object.get("link_up").as_bool())
                    return "Down";

                return String::formatted("{} Mb/s {}-duplex", object.get("link_speed").to_i32(),
                    object.get("link_full_duplex").as_bool() ? "full" : "half");
            });
        net_adapters_fields.empend("IPv4", Gfx::TextAlignment::CenterLeft,
            [](JsonObject const& object) -> String {
                return object.get("ipv4_address").as_string_or("");
            });
        net_adapters_fields.empend("packets_in", "Pkt In", Gfx::TextAlignment::CenterRight);
        net_adapters_fields.empend("packets_out", "Pkt Out", Gfx::TextAlignment::CenterRight);
        net_adapters_fields.empend("bytes_in", "Bytes In", Gfx::TextAlignment::CenterRight);
        net_adapters_fields.empend("bytes_out", "Bytes Out", Gfx::TextAlignment::CenterRight);
        m_adapter_model = GUI::JsonArrayModel::create("/proc/net/adapters", move(net_adapters_fields));
        m_adapter_table_view->set_model(MUST(GUI::SortingProxyModel::create(*m_adapter_model)));

        auto& tcp_sockets_group_box = add<GUI::GroupBox>("TCP Sockets");
        tcp_sockets_group_box.set_layout<GUI::VerticalBoxLayout>();
        tcp_sockets_group_box.layout()->set_margins(6);

        m_tcp_socket_table_view = tcp_sockets_group_box.add<GUI::TableView>();

        Vector<GUI::JsonArrayModel::FieldSpec> net_tcp_fields;
        net_tcp_fields.empend("peer_address", "Peer", Gfx::TextAlignment::CenterLeft);
        net_tcp_fields.empend("peer_port", "Port", Gfx::TextAlignment::CenterRight);
        net_tcp_fields.empend("local_address", "Local", Gfx::TextAlignment::CenterLeft);
        net_tcp_fields.empend("local_port", "Port", Gfx::TextAlignment::CenterRight);
        net_tcp_fields.empend("state", "State", Gfx::TextAlignment::CenterLeft);
        net_tcp_fields.empend("ack_number", "Ack#", Gfx::TextAlignment::CenterRight);
        net_tcp_fields.empend("sequence_number", "Seq#", Gfx::TextAlignment::CenterRight);
        net_tcp_fields.empend("packets_in", "Pkt In", Gfx::TextAlignment::CenterRight);
        net_tcp_fields.empend("packets_out", "Pkt Out", Gfx::TextAlignment::CenterRight);
        net_tcp_fields.empend("bytes_in", "Bytes In", Gfx::TextAlignment::CenterRight);
        net_tcp_fields.empend("bytes_out", "Bytes Out", Gfx::TextAlignment::CenterRight);
        m_tcp_socket_model = GUI::JsonArrayModel::create("/proc/net/tcp", move(net_tcp_fields));
        m_tcp_socket_table_view->set_model(MUST(GUI::SortingProxyModel::create(*m_tcp_socket_model)));

        auto& udp_sockets_group_box = add<GUI::GroupBox>("UDP Sockets");
        udp_sockets_group_box.set_layout<GUI::VerticalBoxLayout>();
        udp_sockets_group_box.layout()->set_margins(6);

        m_udp_socket_table_view = udp_sockets_group_box.add<GUI::TableView>();

        Vector<GUI::JsonArrayModel::FieldSpec> net_udp_fields;
        net_udp_fields.empend("peer_address", "Peer", Gfx::TextAlignment::CenterLeft);
        net_udp_fields.empend("peer_port", "Port", Gfx::TextAlignment::CenterRight);
        net_udp_fields.empend("local_address", "Local", Gfx::TextAlignment::CenterLeft);
        net_udp_fields.empend("local_port", "Port", Gfx::TextAlignment::CenterRight);
        m_udp_socket_model = GUI::JsonArrayModel::create("/proc/net/udp", move(net_udp_fields));
        m_udp_socket_table_view->set_model(MUST(GUI::SortingProxyModel::create(*m_udp_socket_model)));

        m_update_timer = add<Core::Timer>(
            1000, [this] {
                update_models();
            });

        update_models();
    };
}

void NetworkStatisticsWidget::update_models()
{
    m_adapter_model->update();
    m_tcp_socket_model->update();
    m_udp_socket_model->update();
}

}