Skip to content

Commit

Permalink
Read the list of keymaps once
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Nov 29, 2023
1 parent 879ae13 commit be2cabb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
14 changes: 9 additions & 5 deletions rust/agama-dbus-server/src/locale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod timezone;
use crate::error::Error;
use agama_locale_data::KeymapId;
use anyhow::Context;
use keyboard::{get_keymaps, Keymap};
use keyboard::KeymapsDatabase;
use locale::LocalesDatabase;
use std::process::Command;
use timezone::TimezonesDatabase;
Expand All @@ -18,7 +18,7 @@ pub struct Locale {
locales: Vec<String>,
locales_db: LocalesDatabase,
keymap: KeymapId,
keymaps: Vec<Keymap>,
keymaps_db: KeymapsDatabase,
ui_locale: String,
}

Expand Down Expand Up @@ -88,7 +88,8 @@ impl Locale {
/// * The name of the keyboard in language set by the UILocale property.
fn list_keymaps(&self) -> Result<Vec<(String, String)>, Error> {
let keymaps = self
.keymaps
.keymaps_db
.entries()
.iter()
.map(|k| (k.id.to_string(), k.localized_description()))
.collect();
Expand All @@ -106,7 +107,7 @@ impl Locale {
.parse()
.map_err(|_e| zbus::fdo::Error::InvalidArgs("Invalid keymap".to_string()))?;

if !self.keymaps.iter().any(|k| k.id == keymap_id) {
if !self.keymaps_db.exists(&keymap_id) {
return Err(zbus::fdo::Error::Failed(
"Invalid keyboard value".to_string(),
));
Expand Down Expand Up @@ -184,13 +185,16 @@ impl Locale {
timezones_db.read(&locale)?;
let default_timezone = timezones_db.entries().get(0).unwrap();

let mut keymaps_db = KeymapsDatabase::new();
keymaps_db.read()?;

let locale = Self {
keymap: "us".parse().unwrap(),
timezone: default_timezone.code.to_string(),
locales: vec![default_locale.code.to_string()],
locales_db,
timezones_db,
keymaps: get_keymaps()?,
keymaps_db,
ui_locale: locale.to_string(),
};

Expand Down
33 changes: 32 additions & 1 deletion rust/agama-dbus-server/src/locale/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,42 @@ impl Keymap {
}
}

/// Represents the keymaps database.
///
/// The list of supported keymaps is read from `systemd-localed` and the
/// descriptions from the X Keyboard Configuraiton Database (see
/// `agama_locale_data::XkbConfigRegistry`).
#[derive(Default)]
pub struct KeymapsDatabase {
keymaps: Vec<Keymap>,
}

impl KeymapsDatabase {
pub fn new() -> Self {
Self::default()
}

/// Reads the list of keymaps.
pub fn read(&mut self) -> anyhow::Result<()> {
self.keymaps = get_keymaps()?;
Ok(())
}

pub fn exists(&self, id: &KeymapId) -> bool {
self.keymaps.iter().any(|k| &k.id == id)
}

/// Returns the list of keymaps.
pub fn entries(&self) -> &Vec<Keymap> {
&self.keymaps
}
}

/// Returns the list of keymaps to offer.
///
/// It only includes the keyboards supported by `localectl` but getting
/// the description from the X Keyboard Configuration Database.
pub fn get_keymaps() -> anyhow::Result<Vec<Keymap>> {
fn get_keymaps() -> anyhow::Result<Vec<Keymap>> {
let mut keymaps: Vec<Keymap> = vec![];
let xkb_descriptions = get_keymap_descriptions();
let keymap_ids = get_localectl_keymaps()?;
Expand Down
1 change: 1 addition & 0 deletions rust/agama-dbus-server/src/locale/locale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ impl LocalesDatabase {
false
}

/// Returns the list of locales.
pub fn entries(&self) -> &Vec<LocaleEntry> {
&self.locales
}
Expand Down
1 change: 1 addition & 0 deletions rust/agama-dbus-server/src/locale/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl TimezonesDatabase {
self.timezones.iter().any(|t| &t.code == timezone)
}

/// Returns the list of timezones.
pub fn entries(&self) -> &Vec<TimezoneEntry> {
&self.timezones
}
Expand Down

0 comments on commit be2cabb

Please sign in to comment.