From 75df21d8faacb5faa00b1334020560eec5cb0d1e Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Tue, 17 Oct 2023 13:20:52 +0200 Subject: [PATCH] Use session desktop file path to differentiate them instead of the command. --- src/greeter.rs | 26 ++++++++++++++++++-------- src/info.rs | 31 ++++++++++++++++++++++++++++++- src/ipc.rs | 7 +++++-- src/keyboard.rs | 22 ++++++++++++++++++---- 4 files changed, 71 insertions(+), 15 deletions(-) diff --git a/src/greeter.rs b/src/greeter.rs index 595f59d..db0b17f 100644 --- a/src/greeter.rs +++ b/src/greeter.rs @@ -23,7 +23,7 @@ use tokio::{ use zeroize::Zeroize; use crate::{ - info::{get_issue, get_last_session, get_last_user_name, get_last_user_session, get_last_user_username, get_min_max_uids, get_users}, + info::{get_issue, get_last_session, get_last_session_path, get_last_user_name, get_last_user_session, get_last_user_session_path, get_last_user_username, get_min_max_uids, get_users}, power::PowerOption, }; @@ -85,6 +85,7 @@ pub struct Session { pub name: String, pub command: String, pub session_type: SessionType, + pub path: Option, } #[derive(SmartDefault)] @@ -103,6 +104,7 @@ pub struct Greeter { pub selected_user: usize, pub command: Option, pub new_command: String, + pub session_path: Option, pub session_paths: Vec<(PathBuf, SessionType)>, pub sessions: Vec, pub selected_session: usize, @@ -163,6 +165,10 @@ impl Greeter { greeter.username_mask = get_last_user_name(); if greeter.remember_user_session { + if let Ok(session_path) = get_last_user_session_path(&username) { + greeter.session_path = Some(session_path); + } + if let Ok(command) = get_last_user_session(&username) { greeter.command = Some(command); } @@ -171,12 +177,16 @@ impl Greeter { } if greeter.remember_session { - if let Ok(session) = get_last_session() { - greeter.command = Some(session.trim().to_string()); + if let Ok(session_path) = get_last_session_path() { + greeter.session_path = Some(session_path); + } + + if let Ok(command) = get_last_session() { + greeter.command = Some(command.trim().to_string()); } } - greeter.selected_session = greeter.sessions.iter().position(|Session { command, .. }| Some(command) == greeter.command.as_ref()).unwrap_or(0); + greeter.selected_session = greeter.sessions.iter().position(|Session { path, .. }| path.as_deref() == greeter.session_path.as_deref()).unwrap_or(0); greeter } @@ -197,11 +207,11 @@ impl Greeter { pub async fn reset(&mut self, soft: bool) { if soft { - self.mode = Mode::Password; - self.previous_mode = Mode::Password; + self.mode = Mode::Password; + self.previous_mode = Mode::Password; } else { - self.mode = Mode::Username; - self.previous_mode = Mode::Username; + self.mode = Mode::Username; + self.previous_mode = Mode::Username; } self.working = false; diff --git a/src/info.rs b/src/info.rs index b5460de..ce4d2b8 100644 --- a/src/info.rs +++ b/src/info.rs @@ -16,6 +16,7 @@ use crate::{Greeter, Session, SessionType}; const LAST_USER_USERNAME: &str = "/var/cache/tuigreet/lastuser"; const LAST_USER_NAME: &str = "/var/cache/tuigreet/lastuser-name"; const LAST_SESSION: &str = "/var/cache/tuigreet/lastsession"; +const LAST_SESSION_PATH: &str = "/var/cache/tuigreet/lastsession-path"; const DEFAULT_MIN_UID: u16 = 1000; const DEFAULT_MAX_UID: u16 = 60000; @@ -104,18 +105,44 @@ pub fn write_last_username(username: &str, name: Option<&str>) { } } +pub fn get_last_session_path() -> Result { + Ok(PathBuf::from(fs::read_to_string(LAST_SESSION_PATH)?)) +} + pub fn get_last_session() -> Result { fs::read_to_string(LAST_SESSION) } +pub fn write_last_session_path

(session: &P) +where + P: AsRef, +{ + let _ = fs::write(LAST_SESSION_PATH, session.as_ref().to_string_lossy().as_bytes()); +} + pub fn write_last_session(session: &str) { let _ = fs::write(LAST_SESSION, session); } +pub fn get_last_user_session_path(username: &str) -> Result { + Ok(PathBuf::from(fs::read_to_string(format!("{LAST_SESSION_PATH}-{username}"))?)) +} + pub fn get_last_user_session(username: &str) -> Result { fs::read_to_string(format!("{LAST_SESSION}-{username}")) } +pub fn write_last_user_session_path

(username: &str, session: P) +where + P: AsRef, +{ + let _ = fs::write(format!("{LAST_SESSION_PATH}-{username}"), session.as_ref().to_string_lossy().as_bytes()); +} + +pub fn delete_last_session_path() { + let _ = fs::remove_file(LAST_SESSION_PATH); +} + pub fn write_last_user_session(username: &str, session: &str) { let _ = fs::write(format!("{LAST_SESSION}-{username}"), session); } @@ -202,6 +229,7 @@ pub fn get_sessions(greeter: &Greeter) -> Result, Box> { name: command.clone(), command: command.clone(), session_type: SessionType::default(), + path: None, }], _ => vec![], }; @@ -218,7 +246,7 @@ fn load_desktop_file

(path: P, session_type: SessionType) -> Result, { - let desktop = Ini::load_from_file(path)?; + let desktop = Ini::load_from_file(path.as_ref())?; let section = desktop.section(Some("Desktop Entry")).ok_or("no Desktop Entry section in desktop file")?; let name = section.get("Name").ok_or("no Name property in desktop file")?; @@ -228,6 +256,7 @@ where name: name.to_string(), command: exec.to_string(), session_type, + path: Some(path.as_ref().into()), }) } diff --git a/src/ipc.rs b/src/ipc.rs index d753d24..7897df3 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -7,7 +7,7 @@ use tokio::sync::{ }; use crate::{ - info::{write_last_user_session, write_last_username}, + info::{write_last_user_session, write_last_user_session_path, write_last_username}, AuthStatus, Greeter, Mode, Session, SessionType, }; @@ -109,6 +109,10 @@ impl Ipc { if let Some(command) = &greeter.command { write_last_user_session(&greeter.username, command); } + + if let Some(session_path) = &greeter.session_path { + write_last_user_session_path(&greeter.username, session_path); + } } } @@ -161,7 +165,6 @@ impl Ipc { greeter.reset(false).await; } } - } } diff --git a/src/keyboard.rs b/src/keyboard.rs index 4927159..e5b8cf6 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -6,7 +6,7 @@ use tokio::sync::RwLock; use crate::{ event::{Event, Events}, - info::{get_last_user_session, write_last_session}, + info::{delete_last_session_path, get_last_user_session, get_last_user_session_path, write_last_session, write_last_session_path}, ipc::Ipc, power::power, ui::POWER_OPTIONS, @@ -177,6 +177,7 @@ pub async fn handle(greeter: Arc>, events: &mut Events, ipc: Ipc if greeter.remember_session { write_last_session(&greeter.new_command); + delete_last_session_path(); } greeter.mode = greeter.previous_mode; @@ -194,11 +195,20 @@ pub async fn handle(greeter: Arc>, events: &mut Events, ipc: Ipc } Mode::Sessions => { - let session = greeter.sessions.get(greeter.selected_session); + let session = match greeter.sessions.get(greeter.selected_session) { + Some(Session { name, path, command, .. }) => Some((name.clone(), path.clone(), command.clone())), + _ => None, + }; + + if let Some((_, path, command)) = session { + greeter.session_path = path.clone(); - if let Some(Session { command, .. }) = session { if greeter.remember_session { - write_last_session(command); + if let Some(path) = path { + write_last_session_path(&path); + } + + write_last_session(&command); } greeter.command = Some(command.clone()); @@ -293,6 +303,10 @@ async fn validate_username(greeter: &mut Greeter, ipc: &Ipc) { greeter.answer = String::new(); if greeter.remember_user_session { + if let Ok(last_session) = get_last_user_session_path(&greeter.username) { + greeter.selected_session = greeter.sessions.iter().position(|Session { path, .. }| path.as_deref() == Some(last_session.as_ref())).unwrap_or(0); + } + if let Ok(command) = get_last_user_session(&greeter.username) { greeter.selected_session = greeter.sessions.iter().position(|session| session.command == command).unwrap_or(0); greeter.command = Some(command);