NetworkStatisticsWidget.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "NetworkStatisticsWidget.h"
  8. #include <LibGUI/BoxLayout.h>
  9. #include <LibGUI/GroupBox.h>
  10. #include <LibGUI/JsonArrayModel.h>
  11. #include <LibGUI/Menu.h>
  12. #include <LibGUI/Process.h>
  13. #include <LibGUI/SortingProxyModel.h>
  14. #include <LibGUI/TableView.h>
  15. #include <LibGfx/Painter.h>
  16. REGISTER_WIDGET(SystemMonitor, NetworkStatisticsWidget)
  17. namespace SystemMonitor {
  18. NetworkStatisticsWidget::NetworkStatisticsWidget()
  19. {
  20. on_first_show = [this](auto&) {
  21. set_layout<GUI::VerticalBoxLayout>(4);
  22. set_fill_with_background_color(true);
  23. m_network_connected_bitmap = Gfx::Bitmap::load_from_file("/res/icons/16x16/network-connected.png"sv).release_value_but_fixme_should_propagate_errors();
  24. m_network_disconnected_bitmap = Gfx::Bitmap::load_from_file("/res/icons/16x16/network-disconnected.png"sv).release_value_but_fixme_should_propagate_errors();
  25. m_network_link_down_bitmap = Gfx::Bitmap::create(m_network_connected_bitmap->format(), m_network_connected_bitmap->size()).release_value_but_fixme_should_propagate_errors();
  26. {
  27. Gfx::Painter painter(*m_network_link_down_bitmap);
  28. painter.blit_filtered(Gfx::IntPoint {}, *m_network_connected_bitmap, m_network_connected_bitmap->rect(), [](Color color) {
  29. return color.to_grayscale();
  30. });
  31. }
  32. auto& adapters_group_box = add<GUI::GroupBox>("Adapters"sv);
  33. adapters_group_box.set_layout<GUI::VerticalBoxLayout>(6);
  34. adapters_group_box.set_fixed_height(120);
  35. m_adapter_table_view = adapters_group_box.add<GUI::TableView>();
  36. Vector<GUI::JsonArrayModel::FieldSpec> net_adapters_fields;
  37. net_adapters_fields.empend("", Gfx::TextAlignment::CenterLeft,
  38. [this](JsonObject const& object) -> GUI::Variant {
  39. if (!object.get_bool("link_up"sv).value_or(false))
  40. return *m_network_link_down_bitmap;
  41. else
  42. return object.get_deprecated_string("ipv4_address"sv).value_or("").is_empty() ? *m_network_disconnected_bitmap : *m_network_connected_bitmap;
  43. });
  44. net_adapters_fields.empend("name", "Name", Gfx::TextAlignment::CenterLeft);
  45. net_adapters_fields.empend("class_name", "Class", Gfx::TextAlignment::CenterLeft);
  46. net_adapters_fields.empend("mac_address", "MAC", Gfx::TextAlignment::CenterLeft);
  47. net_adapters_fields.empend("Link status", Gfx::TextAlignment::CenterLeft,
  48. [](JsonObject const& object) -> DeprecatedString {
  49. if (!object.get_bool("link_up"sv).value_or(false))
  50. return "Down";
  51. return DeprecatedString::formatted("{} Mb/s {}-duplex", object.get_i32("link_speed"sv).value_or(0),
  52. object.get_bool("link_full_duplex"sv).value_or(false) ? "full"sv : "half"sv);
  53. });
  54. net_adapters_fields.empend("IPv4", Gfx::TextAlignment::CenterLeft,
  55. [](JsonObject const& object) -> DeprecatedString {
  56. return object.get_deprecated_string("ipv4_address"sv).value_or(""sv);
  57. });
  58. net_adapters_fields.empend("packets_in", "Pkt In", Gfx::TextAlignment::CenterRight);
  59. net_adapters_fields.empend("packets_out", "Pkt Out", Gfx::TextAlignment::CenterRight);
  60. net_adapters_fields.empend("bytes_in", "Bytes In", Gfx::TextAlignment::CenterRight);
  61. net_adapters_fields.empend("bytes_out", "Bytes Out", Gfx::TextAlignment::CenterRight);
  62. m_adapter_model = GUI::JsonArrayModel::create("/sys/kernel/net/adapters", move(net_adapters_fields));
  63. m_adapter_table_view->set_model(MUST(GUI::SortingProxyModel::create(*m_adapter_model)));
  64. m_adapter_context_menu = MUST(GUI::Menu::try_create());
  65. m_adapter_context_menu->add_action(GUI::Action::create(
  66. "Open in Network Settings...", MUST(Gfx::Bitmap::load_from_file("/res/icons/16x16/network.png"sv)), [this](GUI::Action&) {
  67. m_adapter_table_view->selection().for_each_index([this](GUI::ModelIndex const& index) {
  68. auto adapter_name = index.sibling_at_column(1).data().as_string();
  69. GUI::Process::spawn_or_show_error(window(), "/bin/Escalator"sv, Array { "/bin/NetworkSettings", adapter_name.characters() });
  70. });
  71. },
  72. this));
  73. m_adapter_table_view->on_context_menu_request = [this](GUI::ModelIndex const& index, GUI::ContextMenuEvent const& event) {
  74. if (!index.is_valid()) {
  75. return;
  76. }
  77. auto adapter_name = index.sibling_at_column(1).data().as_string();
  78. if (adapter_name == "loop") {
  79. return;
  80. }
  81. m_adapter_context_menu->popup(event.screen_position());
  82. };
  83. auto& tcp_sockets_group_box = add<GUI::GroupBox>("TCP Sockets"sv);
  84. tcp_sockets_group_box.set_layout<GUI::VerticalBoxLayout>(6);
  85. m_tcp_socket_table_view = tcp_sockets_group_box.add<GUI::TableView>();
  86. Vector<GUI::JsonArrayModel::FieldSpec> net_tcp_fields;
  87. net_tcp_fields.empend("peer_address", "Peer", Gfx::TextAlignment::CenterLeft);
  88. net_tcp_fields.empend("peer_port", "Port", Gfx::TextAlignment::CenterRight);
  89. net_tcp_fields.empend("local_address", "Local", Gfx::TextAlignment::CenterLeft);
  90. net_tcp_fields.empend("local_port", "Port", Gfx::TextAlignment::CenterRight);
  91. net_tcp_fields.empend("state", "State", Gfx::TextAlignment::CenterLeft);
  92. net_tcp_fields.empend("ack_number", "Ack#", Gfx::TextAlignment::CenterRight);
  93. net_tcp_fields.empend("sequence_number", "Seq#", Gfx::TextAlignment::CenterRight);
  94. net_tcp_fields.empend("packets_in", "Pkt In", Gfx::TextAlignment::CenterRight);
  95. net_tcp_fields.empend("packets_out", "Pkt Out", Gfx::TextAlignment::CenterRight);
  96. net_tcp_fields.empend("bytes_in", "Bytes In", Gfx::TextAlignment::CenterRight);
  97. net_tcp_fields.empend("bytes_out", "Bytes Out", Gfx::TextAlignment::CenterRight);
  98. m_tcp_socket_model = GUI::JsonArrayModel::create("/sys/kernel/net/tcp", move(net_tcp_fields));
  99. m_tcp_socket_table_view->set_model(MUST(GUI::SortingProxyModel::create(*m_tcp_socket_model)));
  100. auto& udp_sockets_group_box = add<GUI::GroupBox>("UDP Sockets"sv);
  101. udp_sockets_group_box.set_layout<GUI::VerticalBoxLayout>(6);
  102. m_udp_socket_table_view = udp_sockets_group_box.add<GUI::TableView>();
  103. Vector<GUI::JsonArrayModel::FieldSpec> net_udp_fields;
  104. net_udp_fields.empend("peer_address", "Peer", Gfx::TextAlignment::CenterLeft);
  105. net_udp_fields.empend("peer_port", "Port", Gfx::TextAlignment::CenterRight);
  106. net_udp_fields.empend("local_address", "Local", Gfx::TextAlignment::CenterLeft);
  107. net_udp_fields.empend("local_port", "Port", Gfx::TextAlignment::CenterRight);
  108. m_udp_socket_model = GUI::JsonArrayModel::create("/sys/kernel/net/udp", move(net_udp_fields));
  109. m_udp_socket_table_view->set_model(MUST(GUI::SortingProxyModel::create(*m_udp_socket_model)));
  110. m_update_timer = add<Core::Timer>(
  111. 1000, [this] {
  112. update_models();
  113. });
  114. m_update_timer->start();
  115. update_models();
  116. };
  117. }
  118. void NetworkStatisticsWidget::update_models()
  119. {
  120. m_adapter_model->update();
  121. m_tcp_socket_model->update();
  122. m_udp_socket_model->update();
  123. }
  124. }