Working on TimeZone stage.

This commit is contained in:
2026-04-28 23:53:50 +02:00
parent 632c6f7f98
commit 3361562841
12 changed files with 282 additions and 9 deletions
+240
View File
@@ -0,0 +1,240 @@
// <Kira Installer - universal Linux installer.>
// Copyright (C) <2026> <Kira Foundation>
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/*
This is Welcome stage, used to greet used and allow user to choose program language
*/
use crate::stage::{ConfigValue, StageAction, StageResult};
use iced::{Alignment, widget};
use rust_i18n::t;
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TimeZoneData {
region: String,
zone: String,
}
impl TimeZoneData {
fn to_string(&self) -> String {
format!("{}/{}", self.region, self.zone)
}
fn from_str(s: &str) -> Option<Self> {
let idx = s.find('/')?;
let (region, zone) = s.split_at(idx);
Some(Self {
region: region.into(),
zone: zone.into(),
})
}
}
impl std::fmt::Display for TimeZoneData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}/{}", self.region, self.zone)
}
}
#[derive(Debug, Clone)]
pub struct TimeZoneStage {
time_zone: Option<TimeZoneData>,
time_zone_region: Option<String>,
time_zones: Result<HashMap<String, Vec<TimeZoneData>>, String>,
regions: Vec<String>,
zones: Vec<TimeZoneData>,
}
#[derive(Debug, Clone)]
pub enum Message {
SelectRegion(String),
SelectZone(TimeZoneData),
Next,
Back,
}
/// Getting list of awailable timezones from system.
fn get_timezones_blocking() -> Result<Vec<TimeZoneData>, String> {
use std::process::Command;
//timedatectl list-timezones --no-pager
match Command::new("timedatectl")
.arg("list-timezones")
.arg("--no-pager")
.output()
{
Ok(cmd_output) => {
if cmd_output.status.success() {
//Etc/UTC
match String::from_utf8(cmd_output.stdout) {
Ok(str_tz_list) => Ok(str_tz_list
.split("\n")
.filter(|sb| sb.contains('/'))
.filter_map(|str_t_zone| TimeZoneData::from_str(str_t_zone.trim()))
.collect::<Vec<TimeZoneData>>()),
Err(ex) => Err(format!("Exception while converting to UTF8 {}", ex)),
}
} else {
Err(format!(
"Error getting timezones list: {}",
String::from_utf8(cmd_output.stderr).unwrap_or("UNKNOWN".into())
))
}
}
Err(ex) => Err(format!("Exception while trying to list time zones {}", ex)),
}
}
impl TimeZoneStage {
pub fn new() -> Self {
match get_timezones_blocking() {
Ok(tzs) => {
let mut time_zones: HashMap<String, Vec<TimeZoneData>> = HashMap::new();
for tz in &tzs {
if let Some(z) = time_zones.get_mut(tz.region.as_str()) {
z.push(tz.clone());
} else {
time_zones.insert(tz.region.clone(), vec![tz.clone()]);
}
}
//let time_zones: HashMap<String, TimeZoneData> = HashMap::from_iter(tzs.iter().map(|tz| (tz.region.clone(), tz.clone())));
Self {
regions: time_zones.keys().cloned().collect(),
zones: tzs.clone(),
time_zone: None,
time_zone_region: None,
time_zones: Ok(time_zones),
}
}
Err(ex) => {
println!(
"Exception while trying to get time zones list from system {}",
ex
);
Self {
time_zone: None,
time_zone_region: None,
time_zones: Err(ex),
regions: Vec::new(),
zones: Vec::new(),
}
}
}
}
fn gen_result(&self) -> StageResult {
if let Some(tz) = &self.time_zone {
StageResult {
name: "time_zone".to_string(),
config: Some(HashMap::from([
("name".to_string(), ConfigValue::String(tz.to_string())),
("region".to_string(), ConfigValue::String(tz.region.clone())),
("zone".to_string(), ConfigValue::String(tz.zone.clone())),
])),
resuts: None,
error: None,
}
} else {
StageResult {
name: "time_zone".to_string(),
config: None,
resuts: None,
error: None,
}
}
}
pub fn update(&mut self, message: Message) -> StageAction {
match message {
Message::SelectRegion(region) => {
self.time_zone_region = Some(region);
StageAction::None
}
Message::SelectZone(zone) => {
self.time_zone = Some(zone);
StageAction::None
}
Message::Next => StageAction::Next(self.gen_result()),
Message::Back => StageAction::Back,
}
}
pub fn view(&self) -> iced::Element<'_, Message> {
let (next_button, main_content) = match &self.time_zones {
Ok(time_zones) => (
widget::button(widget::text(t!("button.next"))).on_press(Message::Next),
widget::container(
widget::column![
widget::text(t!("timezone.select_timezone")),
widget::row![
widget::text(t!("timezone.select_region")),
widget::pick_list(
self.regions.clone(),
self.time_zone_region.clone(),
Message::SelectRegion,
)
.placeholder("Select Region")
],
widget::row![
widget::text(t!("timezone.select_timezone")),
widget::pick_list(
time_zones
.get(
self.time_zone_region
.clone()
.unwrap_or("Etc".into())
.as_str()
)
.unwrap()
.clone(),
self.time_zone.clone(),
Message::SelectZone,
)
.placeholder("Select Timezone")
]
]
.padding(10)
.spacing(10)
.align_x(Alignment::Center),
),
),
Err(_) => (
widget::button(widget::text(t!("button.next"))),
widget::container(widget::text(t!("timezone.init_error"))),
),
};
let back_button = widget::button(widget::text(t!("button.back"))).on_press(Message::Back);
widget::column![
main_content
.height(iced::Length::Fill)
.width(iced::Length::Fill)
.align_x(Alignment::Center)
.align_y(Alignment::Center),
widget::row![
back_button,
widget::space::horizontal(), // Pushes the right button to the far right
next_button
]
.width(iced::Length::Fill)
.align_y(Alignment::End)
.padding(10),
]
.align_x(Alignment::Center)
.spacing(10)
.into()
}
}