Skip to content

Commit

Permalink
Use session desktop file path to differentiate them instead of the co…
Browse files Browse the repository at this point in the history
…mmand.
  • Loading branch information
apognu committed Oct 21, 2023
1 parent 2b119d9 commit 1812f1e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 15 deletions.
26 changes: 18 additions & 8 deletions src/greeter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -85,6 +85,7 @@ pub struct Session {
pub name: String,
pub command: String,
pub session_type: SessionType,
pub path: Option<PathBuf>,
}

#[derive(SmartDefault)]
Expand All @@ -103,6 +104,7 @@ pub struct Greeter {
pub selected_user: usize,
pub command: Option<String>,
pub new_command: String,
pub session_path: Option<PathBuf>,
pub session_paths: Vec<(PathBuf, SessionType)>,
pub sessions: Vec<Session>,
pub selected_session: usize,
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
}
Expand All @@ -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;
Expand Down
31 changes: 30 additions & 1 deletion src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -104,18 +105,44 @@ pub fn write_last_username(username: &str, name: Option<&str>) {
}
}

pub fn get_last_session_path() -> Result<PathBuf, io::Error> {
Ok(PathBuf::from(fs::read_to_string(LAST_SESSION_PATH)?))
}

pub fn get_last_session() -> Result<String, io::Error> {
fs::read_to_string(LAST_SESSION)
}

pub fn write_last_session_path<P>(session: &P)
where
P: AsRef<Path>,
{
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<PathBuf, io::Error> {
Ok(PathBuf::from(fs::read_to_string(format!("{LAST_SESSION_PATH}-{username}"))?))
}

pub fn get_last_user_session(username: &str) -> Result<String, io::Error> {
fs::read_to_string(format!("{LAST_SESSION}-{username}"))
}

pub fn write_last_user_session_path<P>(username: &str, session: P)
where
P: AsRef<Path>,
{
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);
}
Expand Down Expand Up @@ -202,6 +229,7 @@ pub fn get_sessions(greeter: &Greeter) -> Result<Vec<Session>, Box<dyn Error>> {
name: command.clone(),
command: command.clone(),
session_type: SessionType::default(),
path: None,
}],
_ => vec![],
};
Expand All @@ -218,7 +246,7 @@ fn load_desktop_file<P>(path: P, session_type: SessionType) -> Result<Session, B
where
P: AsRef<Path>,
{
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")?;
Expand All @@ -228,6 +256,7 @@ where
name: name.to_string(),
command: exec.to_string(),
session_type,
path: Some(path.as_ref().into()),
})
}

Expand Down
7 changes: 5 additions & 2 deletions src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -161,7 +165,6 @@ impl Ipc {
greeter.reset(false).await;
}
}

}
}

Expand Down
22 changes: 18 additions & 4 deletions src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -177,6 +177,7 @@ pub async fn handle(greeter: Arc<RwLock<Greeter>>, events: &mut Events, ipc: Ipc

if greeter.remember_session {
write_last_session(&greeter.new_command);
delete_last_session_path();
}

greeter.mode = greeter.previous_mode;
Expand All @@ -194,11 +195,20 @@ pub async fn handle(greeter: Arc<RwLock<Greeter>>, 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());
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1812f1e

Please sign in to comment.