mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 20:07:36 +00:00
Applets/Network: Port get_adapter_info() to ErrorOr<String>
This commit is contained in:
parent
a3ddba4191
commit
dd92d09268
1 changed files with 14 additions and 28 deletions
|
@ -53,16 +53,16 @@ private:
|
||||||
|
|
||||||
void update_widget()
|
void update_widget()
|
||||||
{
|
{
|
||||||
auto adapter_info = get_adapter_info();
|
auto adapter_info_or_error = get_adapter_info();
|
||||||
|
if (adapter_info_or_error.is_error()) {
|
||||||
if (adapter_info == "") {
|
dbgln("Couldn't update adapter info: {}", adapter_info_or_error.error());
|
||||||
set_connected(false);
|
set_connected(false);
|
||||||
m_adapter_info = "No network adapters";
|
m_adapter_info = "No network adapters"_string;
|
||||||
} else {
|
} else {
|
||||||
m_adapter_info = adapter_info;
|
m_adapter_info = adapter_info_or_error.release_value();
|
||||||
}
|
}
|
||||||
|
|
||||||
set_tooltip_deprecated(m_adapter_info);
|
set_tooltip(m_adapter_info);
|
||||||
|
|
||||||
if (m_connected)
|
if (m_connected)
|
||||||
NetworkWidget::set_bitmap(m_connected_icon);
|
NetworkWidget::set_bitmap(m_connected_icon);
|
||||||
|
@ -103,29 +103,15 @@ private:
|
||||||
m_connected = connected;
|
m_connected = connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
DeprecatedString get_adapter_info()
|
ErrorOr<String> get_adapter_info()
|
||||||
{
|
{
|
||||||
|
auto file = TRY(Core::File::open("/sys/kernel/net/adapters"sv, Core::File::OpenMode::Read));
|
||||||
|
auto file_contents = TRY(file->read_until_eof());
|
||||||
|
auto json = TRY(JsonValue::from_string(file_contents));
|
||||||
|
|
||||||
StringBuilder adapter_info;
|
StringBuilder adapter_info;
|
||||||
|
|
||||||
auto file_or_error = Core::File::open("/sys/kernel/net/adapters"sv, Core::File::OpenMode::Read);
|
|
||||||
if (file_or_error.is_error()) {
|
|
||||||
dbgln("Error: Could not open /sys/kernel/net/adapters: {}", file_or_error.error());
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
auto file_contents_or_error = file_or_error.value()->read_until_eof();
|
|
||||||
if (file_contents_or_error.is_error()) {
|
|
||||||
dbgln("Error: Could not read /sys/kernel/net/adapters: {}", file_contents_or_error.error());
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
auto json = JsonValue::from_string(file_contents_or_error.value());
|
|
||||||
|
|
||||||
if (json.is_error())
|
|
||||||
return adapter_info.to_deprecated_string();
|
|
||||||
|
|
||||||
int connected_adapters = 0;
|
int connected_adapters = 0;
|
||||||
json.value().as_array().for_each([&adapter_info, &connected_adapters](auto& value) {
|
json.as_array().for_each([&adapter_info, &connected_adapters](auto& value) {
|
||||||
auto& if_object = value.as_object();
|
auto& if_object = value.as_object();
|
||||||
auto ip_address = if_object.get_deprecated_string("ipv4_address"sv).value_or("no IP");
|
auto ip_address = if_object.get_deprecated_string("ipv4_address"sv).value_or("no IP");
|
||||||
auto ifname = if_object.get_deprecated_string("name"sv).value();
|
auto ifname = if_object.get_deprecated_string("name"sv).value();
|
||||||
|
@ -151,10 +137,10 @@ private:
|
||||||
// show connected icon so long as at least one adapter is connected
|
// show connected icon so long as at least one adapter is connected
|
||||||
connected_adapters ? set_connected(true) : set_connected(false);
|
connected_adapters ? set_connected(true) : set_connected(false);
|
||||||
|
|
||||||
return adapter_info.to_deprecated_string();
|
return adapter_info.to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
DeprecatedString m_adapter_info;
|
String m_adapter_info;
|
||||||
bool m_connected = false;
|
bool m_connected = false;
|
||||||
bool m_notifications = true;
|
bool m_notifications = true;
|
||||||
NonnullRefPtr<Gfx::Bitmap> m_connected_icon;
|
NonnullRefPtr<Gfx::Bitmap> m_connected_icon;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue