Skip to content

Commit

Permalink
changed IP and transport filters logic and appearance on initial page
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Dec 14, 2023
1 parent 3c03dc2 commit b196444
Show file tree
Hide file tree
Showing 14 changed files with 211 additions and 228 deletions.
62 changes: 2 additions & 60 deletions src/gui/components/radio.rs
Original file line number Diff line number Diff line change
@@ -1,71 +1,13 @@
use iced::widget::{Column, Radio, Row, Text};
use iced::{Alignment, Font, Renderer};

use crate::gui::styles::style_constants::FONT_SIZE_SUBTITLE;
use crate::gui::styles::text::TextType;
use crate::gui::types::message::Message;
use crate::notifications::types::notifications::{
BytesNotification, FavoriteNotification, Notification, PacketsNotification,
};
use crate::notifications::types::sound::Sound;
use crate::translations::translations::{
ip_version_translation, sound_translation, transport_protocol_translation,
};
use crate::{ChartType, IpVersion, Language, StyleType, TransProtocol};

pub fn ip_version_radios(
active: IpVersion,
font: Font,
language: Language,
) -> Column<'static, Message, Renderer<StyleType>> {
let mut ret_val = Column::new().spacing(10).push(
ip_version_translation(language)
.font(font)
.style(TextType::Subtitle)
.size(FONT_SIZE_SUBTITLE),
);
for option in IpVersion::ALL {
ret_val = ret_val.push(
Radio::new(
option.get_radio_label(language),
option,
Some(active),
Message::IpVersionSelection,
)
.spacing(7)
.font(font)
.size(15),
);
}
ret_val
}

pub fn transport_protocol_radios(
active: TransProtocol,
font: Font,
language: Language,
) -> Column<'static, Message, Renderer<StyleType>> {
let mut ret_val = Column::new().spacing(10).push(
Text::new(transport_protocol_translation(language))
.font(font)
.style(TextType::Subtitle)
.size(FONT_SIZE_SUBTITLE),
);
for option in TransProtocol::ALL {
ret_val = ret_val.push(
Radio::new(
option.get_radio_label(language),
option,
Some(active),
Message::TransportProtocolSelection,
)
.spacing(7)
.font(font)
.size(15),
);
}
ret_val
}
use crate::translations::translations::sound_translation;
use crate::{ChartType, Language, StyleType};

pub fn sound_packets_threshold_radios(
packets_notification: PacketsNotification,
Expand Down
165 changes: 100 additions & 65 deletions src/gui/pages/initial_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
//!
//! It contains elements to select network adapter and traffic filters.
use iced::alignment::{Horizontal, Vertical};
use iced::widget::scrollable::Direction;
use iced::widget::tooltip::Position;
use iced::widget::{
button, horizontal_space, vertical_space, Button, Column, Container, PickList, Row, Scrollable,
Text, Tooltip,
button, horizontal_space, vertical_space, Button, Column, Container, Row, Scrollable, Text,
Tooltip,
};
use iced::Length::FillPortion;
use iced::{alignment, Alignment, Font, Length, Renderer};
use iced::{alignment, Font, Length, Renderer};
use pcap::Device;
use std::collections::HashSet;

use crate::gui::components::radio::{ip_version_radios, transport_protocol_radios};
use crate::gui::styles::button::ButtonType;
use crate::gui::styles::container::ContainerType;
use crate::gui::styles::scrollbar::ScrollbarType;
Expand All @@ -22,11 +23,12 @@ use crate::gui::styles::types::gradient_type::GradientType;
use crate::gui::types::message::Message;
use crate::gui::types::sniffer::Sniffer;
use crate::translations::translations::{
address_translation, addresses_translation, all_translation, application_protocol_translation,
choose_adapters_translation, select_filters_translation, start_translation,
address_translation, addresses_translation, choose_adapters_translation,
ip_version_translation, select_filters_translation, start_translation,
transport_protocol_translation,
};
use crate::utils::types::icon::Icon;
use crate::{AppProtocol, Language, StyleType};
use crate::{IpVersion, Language, StyleType, TransProtocol};

/// Computes the body of gui initial page
pub fn initial_page(sniffer: &Sniffer) -> Container<Message, Renderer<StyleType>> {
Expand All @@ -37,83 +39,116 @@ pub fn initial_page(sniffer: &Sniffer) -> Container<Message, Renderer<StyleType>

let col_adapter = get_col_adapter(sniffer, font);

let ip_active = sniffer.filters.ip;
let col_ip_radio = ip_version_radios(ip_active, font, language);
let col_ip = Column::new()
.spacing(10)
.width(FillPortion(5))
.push(col_ip_radio);

let transport_active = sniffer.filters.transport;
let col_transport_radio = transport_protocol_radios(transport_active, font, language);
let col_transport = Column::new()
.align_items(Alignment::Center)
.spacing(10)
.width(FillPortion(9))
.push(col_transport_radio)
.push(vertical_space(FillPortion(2)))
.push(button_start(font, language, color_gradient))
.push(vertical_space(FillPortion(1)));

let app_active = if sniffer.filters.application.ne(&AppProtocol::Other) {
Some(sniffer.filters.application)
} else {
None
};
let picklist_app = PickList::new(
if app_active.is_some() {
&AppProtocol::ALL[..]
} else {
&AppProtocol::ALL[1..]
},
app_active,
Message::AppProtocolSelection,
)
.padding([3, 7])
.placeholder(all_translation(language))
.font(font);
let col_app = Column::new()
.width(FillPortion(8))
.spacing(10)
.push(
Text::new(application_protocol_translation(language))
.font(font)
.style(TextType::Subtitle)
.size(FONT_SIZE_SUBTITLE),
)
.push(picklist_app);
let ip_active = &sniffer.filters.ip;
let col_ip_buttons = col_ip_buttons(ip_active, font, language);

let transport_active = &sniffer.filters.transport;
let col_transport_buttons = col_transport_buttons(transport_active, font, language);

let filters = Column::new()
let filters_pane = Column::new()
.width(FillPortion(6))
.padding(10)
.spacing(15)
.push(
Row::new().push(
select_filters_translation(language)
.font(font)
.style(TextType::Title)
.size(FONT_SIZE_TITLE),
),
select_filters_translation(language)
.font(font)
.style(TextType::Title)
.size(FONT_SIZE_TITLE),
)
.push(
Row::new()
.spacing(10)
.height(FillPortion(3))
.push(col_ip)
.push(col_transport)
.push(col_app),
);
.push(col_ip_buttons)
.push(col_transport_buttons),
)
.push(button_start(font, language, color_gradient));

let body = Column::new().push(vertical_space(Length::Fixed(5.0))).push(
Row::new()
.push(col_adapter)
.push(horizontal_space(Length::Fixed(30.0)))
.push(filters),
.push(filters_pane),
);

Container::new(body).height(Length::Fill)
}

fn col_ip_buttons(
active_ip_filters: &HashSet<IpVersion>,
font: Font,
language: Language,
) -> Column<'static, Message, Renderer<StyleType>> {
let mut buttons_row = Row::new().spacing(5).padding([0, 0, 0, 5]);
for option in IpVersion::ALL {
let is_active = active_ip_filters.contains(&option);
buttons_row = buttons_row.push(
Button::new(
Text::new(option.to_string())
.horizontal_alignment(Horizontal::Center)
.vertical_alignment(Vertical::Center)
.font(font),
)
.width(Length::Fixed(80.0))
.height(Length::Fixed(35.0))
.style(if is_active {
ButtonType::BorderedRoundSelected
} else {
ButtonType::BorderedRound
})
.on_press(Message::IpVersionSelection(option, !is_active)),
);
}

Column::new()
.width(Length::Fill)
.spacing(7)
.push(
ip_version_translation(language)
.font(font)
.style(TextType::Subtitle)
.size(FONT_SIZE_SUBTITLE),
)
.push(buttons_row)
}

fn col_transport_buttons(
active_transport_filters: &HashSet<TransProtocol>,
font: Font,
language: Language,
) -> Column<'static, Message, Renderer<StyleType>> {
let mut buttons_row = Row::new().spacing(5).padding([0, 0, 0, 5]);
for option in TransProtocol::ALL {
let is_active = active_transport_filters.contains(&option);
buttons_row = buttons_row.push(
Button::new(
Text::new(option.to_string())
.horizontal_alignment(Horizontal::Center)
.vertical_alignment(Vertical::Center)
.font(font),
)
.width(Length::Fixed(80.0))
.height(Length::Fixed(35.0))
.style(if is_active {
ButtonType::BorderedRoundSelected
} else {
ButtonType::BorderedRound
})
.on_press(Message::TransportProtocolSelection(option, !is_active)),
);
}

Column::new()
.width(Length::Fill)
.spacing(7)
.push(
Text::new(transport_protocol_translation(language))
.font(font)
.style(TextType::Subtitle)
.size(FONT_SIZE_SUBTITLE),
)
.push(buttons_row)
}

fn button_start(
font: Font,
language: Language,
Expand Down
8 changes: 4 additions & 4 deletions src/gui/pages/overview_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn overview_page(sniffer: &Sniffer) -> Container<Message, Renderer<StyleType
(observed, 0) => {
//no packets have been filtered but some have been observed
body =
body_no_observed(sniffer.filters, observed, font, language, &sniffer.waiting);
body_no_observed(&sniffer.filters, observed, font, language, &sniffer.waiting);
}
(_observed, filtered) => {
//observed > filtered > 0 || observed = filtered > 0
Expand Down Expand Up @@ -169,7 +169,7 @@ fn body_no_packets(
}

fn body_no_observed(
filters: Filters,
filters: &Filters,
observed: u128,
font: Font,
language: Language,
Expand Down Expand Up @@ -417,7 +417,7 @@ fn lazy_col_info(
sniffer.runtime_data.tot_sent_bytes + sniffer.runtime_data.tot_received_bytes;
let all_bytes = sniffer.runtime_data.all_bytes;

let col_device_filters = col_device_filters(language, font, sniffer.filters, &sniffer.device);
let col_device_filters = col_device_filters(language, font, &sniffer.filters, &sniffer.device);

let col_data_representation =
col_data_representation(language, font, sniffer.traffic_chart.chart_type);
Expand Down Expand Up @@ -504,7 +504,7 @@ fn container_chart(sniffer: &Sniffer, font: Font) -> Container<Message, Renderer
fn col_device_filters(
language: Language,
font: Font,
filters: Filters,
filters: &Filters,
device: &MyDevice,
) -> Column<'static, Message, Renderer<StyleType>> {
#[cfg(not(target_os = "windows"))]
Expand Down
10 changes: 3 additions & 7 deletions src/gui/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use crate::networking::types::host::Host;
use crate::networking::types::search_parameters::SearchParameters;
use crate::notifications::types::notifications::Notification;
use crate::utils::types::web_page::WebPage;
use crate::{
AppProtocol, ChartType, IpVersion, Language, ReportSortType, StyleType, TransProtocol,
};
use crate::{ChartType, IpVersion, Language, ReportSortType, StyleType, TransProtocol};

#[derive(Debug, Clone)]
/// Messages types that permit to react to application interactions/subscriptions
Expand All @@ -22,11 +20,9 @@ pub enum Message {
/// Select adapter
AdapterSelection(String),
/// Select IP filter
IpVersionSelection(IpVersion),
IpVersionSelection(IpVersion, bool),
/// Select transport filter
TransportProtocolSelection(TransProtocol),
/// Select application filter
AppProtocolSelection(AppProtocol),
TransportProtocolSelection(TransProtocol, bool),
/// Select chart type to be displayed
ChartSelection(ChartType),
/// Select report type to be displayed
Expand Down
21 changes: 16 additions & 5 deletions src/gui/types/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,20 @@ impl Sniffer {
match message {
Message::TickRun => return self.refresh_data(),
Message::AdapterSelection(name) => self.set_adapter(&name),
Message::IpVersionSelection(version) => self.filters.ip = version,
Message::TransportProtocolSelection(protocol) => self.filters.transport = protocol,
Message::AppProtocolSelection(protocol) => self.filters.application = protocol,
Message::IpVersionSelection(version, insert) => {
if insert {
self.filters.ip.insert(version);
} else {
self.filters.ip.remove(&version);
}
}
Message::TransportProtocolSelection(protocol, insert) => {
if insert {
self.filters.transport.insert(protocol);
} else {
self.filters.transport.remove(&protocol);
}
}
Message::ChartSelection(unit) => self.traffic_chart.change_kind(unit),
Message::ReportSortSelection(sort) => self.report_sort_type = sort,
Message::OpenWebPage(web_page) => Self::open_web(&web_page),
Expand Down Expand Up @@ -343,7 +354,7 @@ impl Sniffer {
if pcap_error.is_none() {
// no pcap error
let current_capture_id = self.current_capture_id.clone();
let filters = self.filters;
let filters = self.filters.clone();
let country_mmdb_reader = self.country_mmdb_reader.clone();
let asn_mmdb_reader = self.asn_mmdb_reader.clone();
thread::Builder::new()
Expand All @@ -353,7 +364,7 @@ impl Sniffer {
&current_capture_id,
&device,
cap.unwrap(),
filters,
&filters,
&info_traffic_mutex,
&country_mmdb_reader,
&asn_mmdb_reader,
Expand Down
Loading

0 comments on commit b196444

Please sign in to comment.