Skip to content

Commit

Permalink
move handling of terminal settings into separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
13hannes11 committed Sep 10, 2024
1 parent e9beaf3 commit 957d720
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::gtk::Align;
use crate::util::prerequisit::get_installed_terminals;
use crate::util::prerequisit::is_toolbox_installed;
use crate::util::terminal::get_installed_terminals;

use crate::modals::settings::SettingsMsg;
use crate::util::toolbox::ToolbxContainer;
Expand Down
16 changes: 11 additions & 5 deletions src/modals/settings.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use crate::util::prerequisit::get_installed_terminals;
use crate::util::terminal::get_installed_terminals;
use crate::util::terminal::TerminalType;
use relm4::adw::prelude::{
ComboRowExt, PreferencesGroupExt, PreferencesPageExt, PreferencesRowExt, PreferencesWindowExt,
};

use crate::util::settings::get_terminal;
use crate::util::settings::set_terminal;
use relm4::gtk::prelude::Cast;
use relm4::gtk::prelude::GtkWindowExt;
use relm4::gtk::prelude::ListModelExt;
Expand Down Expand Up @@ -58,8 +62,7 @@ impl SimpleComponent for SettingsDialog {
);
});

let settings = gio::Settings::new(APP_ID);
let terminal = settings.string("terminal");
let terminal: String = get_terminal().into();
println!("{}", terminal);

let terminal_selection = terminal_selection_model
Expand Down Expand Up @@ -109,8 +112,11 @@ impl SimpleComponent for SettingsDialog {
match msg {
SettingsMsg::OpenSettings => {}
SettingsMsg::TerminalSelectionChanged(terminal) => {
let settings = gio::Settings::new(APP_ID);
settings.set_string("terminal", &terminal);
let terminal_type: Result<TerminalType, ()> = terminal.as_str().try_into();
match terminal_type {
Ok(t) => set_terminal(t),
_ => println!("Found unknown terminal string: {}", terminal),
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod prerequisit;
pub mod settings;
pub mod terminal;
pub mod toolbox;
29 changes: 0 additions & 29 deletions src/util/prerequisit.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,6 @@
use crate::util::toolbox::ToolbxError;
use std::process::Command;

#[derive(Debug)]
pub enum TerminalType {
GnomeTerminal,
Konsole,
}

pub fn get_installed_terminals() -> Result<Vec<TerminalType>, ToolbxError> {
let output = Command::new("flatpak-spawn")
.arg("--host")
.arg("gnome-terminal")
.arg("--version")
.output();

if output.is_err() {
return Err(ToolbxError::CommandExecutionError(
output.unwrap_err().to_string(),
));
}
let output = output.unwrap();

if output.status.code() == Some(0) {
Ok(vec![TerminalType::GnomeTerminal, TerminalType::Konsole])
} else {
Err(ToolbxError::CommandUnsuccessfulError(
String::from_utf8_lossy(&output.stderr).into_owned(),
))
}
}

pub fn is_toolbox_installed() -> Result<bool, ToolbxError> {
let output = Command::new("flatpak-spawn")
.arg("--host")
Expand Down
16 changes: 16 additions & 0 deletions src/util/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::gio;
use crate::util::terminal::TerminalType;
use crate::APP_ID;
use relm4::gtk::prelude::SettingsExt;

pub fn get_terminal() -> TerminalType {
let settings = gio::Settings::new(APP_ID);
let terminal = settings.string("terminal");
terminal.as_str().try_into().unwrap_or_default()
}

pub fn set_terminal(terminal: TerminalType) {
let settings = gio::Settings::new(APP_ID);
let terminal: String = terminal.into();
settings.set_string("terminal", &terminal);
}
49 changes: 49 additions & 0 deletions src/util/terminal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use crate::util::toolbox::ToolbxError;
use std::process::Command;

#[derive(Debug, Default)]
pub enum TerminalType {
#[default]
GnomeTerminal,
Konsole,
}

impl From<TerminalType> for String {
fn from(value: TerminalType) -> Self {
match value {
TerminalType::GnomeTerminal => "GnomeTerminal".to_string(),
TerminalType::Konsole => "Konsole".to_string(),
}
}
}

impl TryFrom<&str> for TerminalType {
type Error = ();

fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(TerminalType::GnomeTerminal)
}
}

pub fn get_installed_terminals() -> Result<Vec<TerminalType>, ToolbxError> {
let output = Command::new("flatpak-spawn")
.arg("--host")
.arg("gnome-terminal")
.arg("--version")
.output();

if output.is_err() {
return Err(ToolbxError::CommandExecutionError(
output.unwrap_err().to_string(),
));
}
let output = output.unwrap();

if output.status.code() == Some(0) {
Ok(vec![TerminalType::GnomeTerminal, TerminalType::Konsole])
} else {
Err(ToolbxError::CommandUnsuccessfulError(
String::from_utf8_lossy(&output.stderr).into_owned(),
))
}
}

0 comments on commit 957d720

Please sign in to comment.