remove all code and start with relm4 0.6

This commit is contained in:
2024-02-26 19:48:54 +01:00
parent 67a80edc9d
commit 3826726b84
44 changed files with 1016 additions and 1381 deletions

166
src/app.rs Normal file
View File

@@ -0,0 +1,166 @@
use relm4::{
actions::{RelmAction, RelmActionGroup},
adw, gtk, main_application, Component, ComponentController, ComponentParts, ComponentSender,
Controller, SimpleComponent,
};
use gtk::prelude::{
ApplicationExt, ApplicationWindowExt, GtkWindowExt, OrientableExt, SettingsExt, WidgetExt,
};
use gtk::{gio, glib};
use crate::config::{APP_ID, PROFILE};
use crate::modals::about::AboutDialog;
pub(super) struct App {
about_dialog: Controller<AboutDialog>,
}
#[derive(Debug)]
pub(super) enum AppMsg {
Quit,
}
relm4::new_action_group!(pub(super) WindowActionGroup, "win");
//relm4::new_stateless_action!(PreferencesAction, WindowActionGroup, "preferences");
relm4::new_stateless_action!(pub(super) ShortcutsAction, WindowActionGroup, "show-help-overlay");
relm4::new_stateless_action!(AboutAction, WindowActionGroup, "about");
#[relm4::component(pub)]
impl SimpleComponent for App {
type Init = ();
type Input = AppMsg;
type Output = ();
type Widgets = AppWidgets;
menu! {
primary_menu: {
section! {
//"_Preferences" => PreferencesAction,
"_Keyboard" => ShortcutsAction,
"_About Toolbox Tuner" => AboutAction,
}
}
}
view! {
main_window = adw::ApplicationWindow::new(&main_application()) {
connect_close_request[sender] => move |_| {
sender.input(AppMsg::Quit);
gtk::Inhibit(true)
},
#[wrap(Some)]
set_help_overlay: shortcuts = &gtk::Builder::from_resource(
"/org/kuchelmeister/ToolboxTuner/gtk/help-overlay.ui"
)
.object::<gtk::ShortcutsWindow>("help_overlay")
.unwrap() -> gtk::ShortcutsWindow {
set_transient_for: Some(&main_window),
set_application: Some(&main_application()),
},
add_css_class?: if PROFILE == "Devel" {
Some("devel")
} else {
None
},
gtk::Box {
set_orientation: gtk::Orientation::Vertical,
adw::HeaderBar {
pack_end = &gtk::MenuButton {
set_icon_name: "open-menu-symbolic",
set_menu_model: Some(&primary_menu),
}
},
gtk::Label {
set_label: "Hello world!",
add_css_class: "title-header",
set_vexpand: true,
}
}
}
}
fn init(
_init: Self::Init,
root: &Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let about_dialog = AboutDialog::builder()
.transient_for(root)
.launch(())
.detach();
let model = Self { about_dialog };
let widgets = view_output!();
let mut actions = RelmActionGroup::<WindowActionGroup>::new();
let shortcuts_action = {
let shortcuts = widgets.shortcuts.clone();
RelmAction::<ShortcutsAction>::new_stateless(move |_| {
shortcuts.present();
})
};
let about_action = {
let sender = model.about_dialog.sender().clone();
RelmAction::<AboutAction>::new_stateless(move |_| {
sender.send(()).unwrap();
})
};
actions.add_action(shortcuts_action);
actions.add_action(about_action);
actions.register_for_widget(&widgets.main_window);
widgets.load_window_size();
ComponentParts { model, widgets }
}
fn update(&mut self, message: Self::Input, _sender: ComponentSender<Self>) {
match message {
AppMsg::Quit => main_application().quit(),
}
}
fn shutdown(&mut self, widgets: &mut Self::Widgets, _output: relm4::Sender<Self::Output>) {
widgets.save_window_size().unwrap();
}
}
impl AppWidgets {
fn save_window_size(&self) -> Result<(), glib::BoolError> {
let settings = gio::Settings::new(APP_ID);
let (width, height) = self.main_window.default_size();
settings.set_int("window-width", width)?;
settings.set_int("window-height", height)?;
settings.set_boolean("is-maximized", self.main_window.is_maximized())?;
Ok(())
}
fn load_window_size(&self) {
let settings = gio::Settings::new(APP_ID);
let width = settings.int("window-width");
let height = settings.int("window-height");
let is_maximized = settings.boolean("is-maximized");
self.main_window.set_default_size(width, height);
if is_maximized {
self.main_window.maximize();
}
}
}

View File

@@ -1,6 +1,7 @@
pub const APP_ID: &str = @APP_ID@;
pub const GETTEXT_PACKAGE: &str = @GETTEXT_PACKAGE@;
pub const LOCALEDIR: &str = @LOCALEDIR@;
#[allow(unused)]
pub const PKGDATADIR: &str = @PKGDATADIR@;
pub const PROFILE: &str = @PROFILE@;
pub const RESOURCES_FILE: &str = concat!(@PKGDATADIR@, "/resources.gresource");

View File

@@ -1,17 +1,47 @@
use std::collections::VecDeque;
#[rustfmt::skip]
mod config;
mod app;
mod modals;
mod setup;
use relm4::gtk::{Application, ApplicationWindow};
use relm4::{factory::FactoryVecDeque, RelmApp};
use ui::app::model::AppModel;
use util::toolbx::ToolbxContainer;
use gtk::prelude::ApplicationExt;
use relm4::{
actions::{AccelsPlus, RelmAction, RelmActionGroup},
gtk, main_application, RelmApp,
};
mod ui;
mod util;
use app::App;
use setup::setup;
relm4::new_action_group!(AppActionGroup, "app");
relm4::new_stateless_action!(QuitAction, AppActionGroup, "quit");
fn main() {
let mut model = AppModel {
toolboxes: FactoryVecDeque::new(),
// Enable logging
tracing_subscriber::fmt()
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::FULL)
.with_max_level(tracing::Level::INFO)
.init();
setup();
let app = main_application();
app.set_resource_base_path(Some("/org/kuchelmeister/ToolboxTuner/"));
let mut actions = RelmActionGroup::<AppActionGroup>::new();
let quit_action = {
let app = app.clone();
RelmAction::<QuitAction>::new_stateless(move |_| {
app.quit();
})
};
let app = RelmApp::new(model);
app.run();
actions.add_action(quit_action);
actions.register_for_main_application();
app.set_accelerators_for_action::<QuitAction>(&["<Control>q"]);
let app = RelmApp::from_app(app);
app.run::<App>(());
}

View File

@@ -25,20 +25,13 @@ if get_option('profile') == 'default'
cargo_options += [ '--release' ]
rust_target = 'release'
message('Building in release mode')
cargo_env = [ 'CARGO_HOME=' + meson.project_build_root() / 'cargo-home' ]
endif
if get_option('profile') == 'flathub'
cargo_options += [ '--release', '--offline' ]
rust_target = 'release'
message('Building in flathub release mode')
cargo_env = [ 'CARGO_HOME=/run/build/toolbx-tuner/cargo' ]
endif
if get_option('profile') == 'development'
else
rust_target = 'debug'
message('Building in debug mode')
cargo_env = [ 'CARGO_HOME=' + meson.project_build_root() / 'cargo-home' ]
endif
cargo_env = [ 'CARGO_HOME=' + meson.project_build_root() / 'cargo-home' ]
cargo_build = custom_target(
'cargo-build',
build_by_default: true,

50
src/modals/about.rs Normal file
View File

@@ -0,0 +1,50 @@
use gtk::prelude::GtkWindowExt;
use relm4::{adw, gtk, ComponentParts, ComponentSender, SimpleComponent};
use crate::config::{APP_ID, VERSION};
pub struct AboutDialog {}
impl SimpleComponent for AboutDialog {
type Init = ();
type Widgets = adw::AboutWindow;
type Input = ();
type Output = ();
type Root = adw::AboutWindow;
fn init_root() -> Self::Root {
adw::AboutWindow::builder()
.application_icon(APP_ID)
// Insert your license of choice here
.license_type(gtk::License::Lgpl30)
// Insert your website here
.website("https://github.com/13hannes11/toolbox-tuner")
// Insert your Issues page
.issue_url("https://github.com/13hannes11/toolbox-tuner/issues")
// Insert your application name here
.application_name("Toolbox Tuner")
.version(VERSION)
.translator_credits("translator-credits")
.copyright("© 2022-2024 Hannes Kuchelmeister")
.developers(vec!["Hannes Kuchelmeister"])
.designers(vec!["Hannes Kuchelmeister"])
.build()
}
fn init(
_: Self::Init,
root: &Self::Root,
_sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let model = Self {};
let widgets = root.clone();
widgets.set_hide_on_close(true);
ComponentParts { model, widgets }
}
fn update_view(&self, dialog: &mut Self::Widgets, _sender: ComponentSender<Self>) {
dialog.present();
}
}

1
src/modals/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod about;

39
src/setup.rs Normal file
View File

@@ -0,0 +1,39 @@
use relm4::gtk;
use gettextrs::{gettext, LocaleCategory};
use gtk::{gio, glib};
use crate::config::{APP_ID, GETTEXT_PACKAGE, LOCALEDIR, RESOURCES_FILE};
pub fn setup() {
// Initialize GTK
gtk::init().unwrap();
setup_gettext();
glib::set_application_name(&gettext("Toolbox Tuner"));
let res = gio::Resource::load(RESOURCES_FILE).expect("Could not load gresource file");
gio::resources_register(&res);
setup_css(&res);
gtk::Window::set_default_icon_name(APP_ID);
}
fn setup_gettext() {
// Prepare i18n
gettextrs::setlocale(LocaleCategory::LcAll, "");
gettextrs::bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR).expect("Unable to bind the text domain");
gettextrs::textdomain(GETTEXT_PACKAGE).expect("Unable to switch to the text domain");
}
fn setup_css(res: &gio::Resource) {
let data = res
.lookup_data(
"/org/kuchelmeister/ToolboxTuner/style.css",
gio::ResourceLookupFlags::NONE,
)
.unwrap();
relm4::set_global_css(&glib::GString::from_utf8_checked(data.to_vec()).unwrap());
}

View File

@@ -1,3 +0,0 @@
pub mod app;
pub mod components;
pub mod ui_strings;

View File

@@ -1,6 +0,0 @@
pub mod messages;
pub mod model;
pub mod toolbox_list;
pub mod update;
pub mod widgets;
pub mod workers;

View File

@@ -1,12 +0,0 @@
use relm4::factory::DynamicIndex;
use crate::util::toolbx::ToolbxContainer;
use super::model::ToolbxEntry;
pub enum AppMsg {
ToolbxListUpdate(Vec<ToolbxContainer>),
ToolbxContainerToggleStartStop(DynamicIndex),
OpenToolbxTerminal(DynamicIndex),
ToolbxContainerChanged(DynamicIndex, ToolbxEntry),
}

View File

@@ -1,65 +0,0 @@
use relm4::{factory::FactoryVecDeque, Model};
use crate::{ui::components::AppComponents, util::toolbx::ToolbxContainer};
use super::{messages::AppMsg, widgets::AppWidgets};
#[derive(Debug, Clone)]
pub struct ToolbxEntry {
pub toolbx_container: ToolbxContainer,
pub changing_status: bool,
// TODO: settings
}
impl ToolbxEntry {
pub fn update_container(&mut self, container: ToolbxContainer) {
std::mem::replace::<ToolbxContainer>(&mut self.toolbx_container, container);
}
pub fn update_entry(&mut self, container: ToolbxEntry) {
std::mem::replace::<ToolbxContainer>(
&mut self.toolbx_container,
container.toolbx_container,
);
self.changing_status = container.changing_status;
}
}
pub struct AppModel {
pub toolboxes: FactoryVecDeque<ToolbxEntry>,
}
impl Model for AppModel {
type Msg = AppMsg;
type Widgets = AppWidgets;
type Components = AppComponents;
}
impl AppModel {
pub fn update_toolbxes<I>(&mut self, toolbox_iter: I)
where
I: Iterator<Item = ToolbxContainer>,
{
// Update each toolbx entry if there were changes to it
// TODO: deal with the removal of toolboxes
for tbx_update in toolbox_iter {
println!("name: {}", tbx_update.name);
let mut exists = false;
for (index, tbx_entry) in self.toolboxes.iter().enumerate() {
if tbx_update.name == tbx_entry.toolbx_container.name {
self.toolboxes
.get_mut(index)
.map(|x| x.update_container(tbx_update.clone()));
exists = true;
break;
}
}
if !exists {
println!("{}", tbx_update.name);
self.toolboxes.push_back(ToolbxEntry {
toolbx_container: tbx_update,
changing_status: false,
})
}
}
}
}

View File

@@ -1,165 +0,0 @@
use relm4::{
adw::{
self,
prelude::{BoxExt, ButtonExt, WidgetExt},
traits::{ActionRowExt, PreferencesRowExt},
},
factory::{DynamicIndex, FactoryPrototype, FactoryVecDeque},
gtk, send, view, Sender,
};
use crate::{
ui::ui_strings::{
APP_ICON, APP_TOOLTIP, SETTINGS_ICON, SETTINGS_TOOLTIP, SHUTDOWN_ICON, SHUTDOWN_TOOLTIP,
START_ICON, START_TOOLTIP, TERMINAL_ICON, TERMINAL_TOOLTIP, UPDATE_ICON, UPDATE_TOOLTIP,
},
util::toolbx::ToolbxStatus,
};
use super::{messages::AppMsg, model::ToolbxEntry};
#[derive(Debug)]
pub struct FactoryWidgets {
pub action_row: adw::ActionRow,
status_button: gtk::Button,
status_spinner: gtk::Spinner,
}
impl FactoryPrototype for ToolbxEntry {
type Factory = FactoryVecDeque<Self>;
type Widgets = FactoryWidgets;
type Root = adw::ActionRow;
type View = gtk::ListBox;
type Msg = AppMsg;
fn init_view(&self, key: &DynamicIndex, sender: Sender<Self::Msg>) -> Self::Widgets {
let index_terminal = key.clone();
let index_settings = key.clone();
view! {
suffix_box = &gtk::Box{
append = &gtk::AspectFrame{
set_ratio: 1.0,
set_child = Some(&gtk::Button::from_icon_name(TERMINAL_ICON)) {
set_margin_start: 10,
set_margin_top: 10,
set_margin_bottom: 10,
set_tooltip_text: Some(TERMINAL_TOOLTIP),
set_css_classes: &["flat"],
connect_clicked(sender) => move |btn| {
send!(sender, AppMsg::OpenToolbxTerminal(index_terminal.clone()));
},
}
},
}
};
let mut status_button_tooltip = START_TOOLTIP;
let mut status_button_icon = START_ICON;
match self.toolbx_container.status {
ToolbxStatus::Running => {
status_button_tooltip = SHUTDOWN_TOOLTIP;
status_button_icon = SHUTDOWN_ICON;
}
_ => {
status_button_tooltip = START_TOOLTIP;
status_button_icon = START_ICON;
}
}
let subtitle = format!(
"created {}\n{}",
self.toolbx_container.created, self.toolbx_container.image
);
let index = key.clone();
view! {
status_spinner = &gtk::Spinner {
set_margin_top: 10,
set_margin_bottom: 10,
set_tooltip_text: Some(status_button_tooltip),
set_css_classes: &["circular"],
}
};
//status_spinner.start();
view! {
status_button = &gtk::Button::from_icon_name(status_button_icon) {
set_margin_top: 10,
set_margin_bottom: 10,
set_tooltip_text: Some(status_button_tooltip),
set_css_classes: &["circular"],
connect_clicked(sender) => move |btn| {
// Disable button
btn.set_sensitive(false);
send!(sender, AppMsg::ToolbxContainerToggleStartStop(index.clone()));
},
}
};
view! {
action_row = &adw::ActionRow {
set_title: &self.toolbx_container.name,
set_subtitle: subtitle.as_str(),
add_prefix = &gtk::Box {
append = &gtk::AspectFrame{
set_ratio: 1.0,
set_child: Some(&status_button),
}
},
add_suffix: &suffix_box,
}
};
FactoryWidgets {
action_row,
status_button,
status_spinner,
}
}
fn view(
&self,
key: &<Self::Factory as relm4::factory::Factory<Self, Self::View>>::Key,
widgets: &Self::Widgets,
) {
println!("updated {}", key.current_index());
// fixme: IDEALY this is would be done with message handling and only if the request actually is done
if self.changing_status {
widgets.status_button.set_sensitive(false);
widgets
.status_button
.set_child(Some(&widgets.status_spinner));
widgets.status_spinner.start();
} else {
match self.toolbx_container.status {
ToolbxStatus::Running => {
widgets.status_button.set_icon_name(SHUTDOWN_ICON);
widgets
.status_button
.set_tooltip_text(Some(SHUTDOWN_TOOLTIP));
}
_ => {
widgets.status_button.set_icon_name(START_ICON);
widgets.status_button.set_tooltip_text(Some(START_TOOLTIP));
}
}
widgets.status_button.set_sensitive(true);
widgets.status_spinner.stop();
}
}
fn root_widget(widgets: &Self::Widgets) -> &Self::Root {
&widgets.action_row
}
fn position(
&self,
key: &<Self::Factory as relm4::factory::Factory<Self, Self::View>>::Key,
) -> <Self::View as relm4::factory::FactoryView<Self::Root>>::Position {
}
}

View File

@@ -1,72 +0,0 @@
use std::process::Command;
use relm4::{AppUpdate, Sender};
use crate::{ui::components::AppComponents, util::toolbx::ToolbxStatus};
use super::{messages::AppMsg, model::AppModel, workers::AsyncHandlerMsg};
impl AppUpdate for AppModel {
fn update(&mut self, msg: AppMsg, components: &AppComponents, _sender: Sender<AppMsg>) -> bool {
match msg {
AppMsg::ToolbxContainerToggleStartStop(index) => {
if let Some(toolbx_container) = self.toolboxes.get_mut(index.current_index()) {
match toolbx_container.toolbx_container.status {
ToolbxStatus::Exited | ToolbxStatus::Configured | ToolbxStatus::Created => {
toolbx_container.changing_status = true;
components
.async_handler
.sender()
.blocking_send(AsyncHandlerMsg::StartToolbx(
index,
toolbx_container.clone(),
))
.expect("Receiver dropped");
}
ToolbxStatus::Running => {
toolbx_container.changing_status = true;
components
.async_handler
.sender()
.blocking_send(AsyncHandlerMsg::StopToolbx(
index,
toolbx_container.clone(),
))
.expect("Receiver dropped");
}
}
// TODO: tell button to reactivate somehow
}
}
AppMsg::ToolbxContainerChanged(index, container) => {
if let Some(toolbx_container) = self.toolboxes.get_mut(index.current_index()) {
toolbx_container.update_entry(container);
}
}
AppMsg::ToolbxListUpdate(tbx_vec) => {
println!("Updating Toolbox List");
self.update_toolbxes(tbx_vec.into_iter());
}
AppMsg::OpenToolbxTerminal(index) => {
if let Some(toolbx_container) = self.toolboxes.get_mut(index.current_index()) {
// TODO: support many terminals and check which are installed
let output = Command::new("flatpak-spawn")
.arg("--host")
.arg("gnome-terminal") //Command::new("gnome-terminal")
.arg("--")
.arg("toolbox")
.arg("enter")
.arg(toolbx_container.toolbx_container.name.clone())
.output();
println!("{:?}", output);
// TODO: update status on worker and add refresh spinner in the meantime
toolbx_container.toolbx_container.update_status();
}
}
}
true
}
}

View File

@@ -1,42 +0,0 @@
use relm4::{
adw::{
self,
prelude::{BoxExt, GtkWindowExt, OrientableExt, WidgetExt},
traits::AdwApplicationWindowExt,
},
gtk::{self, Align, PolicyType, SelectionMode},
WidgetPlus, Widgets,
};
use super::model::AppModel;
#[relm4::widget(pub)]
impl Widgets<AppModel, ()> for AppWidgets {
view! {
main_window = adw::ApplicationWindow {
set_default_width: 800,
set_default_height: 600,
set_content : main_box = Some(&gtk::Box) {
set_orientation: gtk::Orientation::Vertical,
append = &adw::HeaderBar {
set_title_widget = Some(&gtk::Label) {
set_label: "Toolbox Tuner",
}
},
append = &gtk::ScrolledWindow {
set_hexpand: true,
set_vexpand: true,
set_hscrollbar_policy: PolicyType::Never,
set_child = Some(&gtk::ListBox) {
set_valign: Align::Start,
set_selection_mode: SelectionMode::None,
set_margin_all: 30,
set_css_classes: &["boxed-list"],
factory!(model.toolboxes)
}
}
}
}
}
}

View File

@@ -1,83 +0,0 @@
use std::time::Duration;
use relm4::factory::DynamicIndex;
use relm4::{send, MessageHandler, Sender};
use tokio::runtime::{Builder, Runtime};
use tokio::sync::mpsc::{channel, Sender as TokioSender};
use crate::util::toolbx::ToolbxContainer;
use super::{
messages::AppMsg,
model::{AppModel, ToolbxEntry},
};
// Code adapted from https://relm4.org/book/stable/message_handler.html
pub struct AsyncHandler {
_rt: Runtime,
sender: TokioSender<AsyncHandlerMsg>,
}
#[derive(Debug)]
pub enum AsyncHandlerMsg {
StopToolbx(DynamicIndex, ToolbxEntry),
StartToolbx(DynamicIndex, ToolbxEntry),
UpdateToolbxes,
}
impl MessageHandler<AppModel> for AsyncHandler {
type Msg = AsyncHandlerMsg;
type Sender = TokioSender<AsyncHandlerMsg>;
fn init(_parent_model: &AppModel, parent_sender: Sender<AppMsg>) -> Self {
let (sender, mut rx) = channel::<AsyncHandlerMsg>(10);
let rt = Builder::new_multi_thread()
.worker_threads(8)
.enable_time()
.build()
.unwrap();
rt.spawn(async move {
while let Some(msg) = rx.recv().await {
let parent_sender = parent_sender.clone();
tokio::spawn(async move {
match msg {
AsyncHandlerMsg::UpdateToolbxes => {
let toolboxes = ToolbxContainer::get_toolboxes();
send! {parent_sender, AppMsg::ToolbxListUpdate(toolboxes)};
}
AsyncHandlerMsg::StopToolbx(index, mut tbx) => {
tbx.toolbx_container.stop();
tbx.changing_status = false;
send! {parent_sender, AppMsg::ToolbxContainerChanged(index, tbx)};
}
AsyncHandlerMsg::StartToolbx(index, mut tbx) => {
tbx.toolbx_container.start();
tbx.changing_status = false;
send! {parent_sender, AppMsg::ToolbxContainerChanged(index, tbx)};
}
}
});
}
});
let _sender = sender.clone();
rt.spawn(async move {
loop {
_sender.send(AsyncHandlerMsg::UpdateToolbxes).await;
tokio::time::sleep(Duration::from_secs(10)).await;
}
});
AsyncHandler { _rt: rt, sender }
}
fn send(&self, msg: Self::Msg) {
self.sender.blocking_send(msg).unwrap();
}
fn sender(&self) -> Self::Sender {
self.sender.clone()
}
}

View File

@@ -1,11 +0,0 @@
use relm4::RelmComponent;
use relm4::RelmMsgHandler;
use relm4::Sender;
use super::app::model::AppModel;
use super::app::workers::AsyncHandler;
#[derive(relm4::Components)]
pub struct AppComponents {
pub async_handler: RelmMsgHandler<AsyncHandler, AppModel>,
}

View File

@@ -1,20 +0,0 @@
pub const START_ICON: &str = r#"media-playback-start-symbolic"#;
pub const START_TOOLTIP: &str = r#"Start toolbox"#;
pub const SHUTDOWN_ICON: &str = r#"system-shutdown-symbolic"#;
pub const SHUTDOWN_TOOLTIP: &str = r#"Stop toolbox"#;
pub const UPDATE_ICON: &str = r#"software-update-available-symbolic"#;
pub const UPDATE_TOOLTIP: &str = r#"Update all applications inside of the toolbox"#;
pub const APP_ICON: &str = r#"view-grid-symbolic"#;
pub const APP_TOOLTIP: &str = r#"Select applications to showup in the application menu"#;
pub const TERMINAL_ICON: &str = r#"utilities-terminal-symbolic"#;
pub const TERMINAL_TOOLTIP: &str = r#"Open terminal inside of toolbox"#;
pub const SETTINGS_ICON: &str = r#"applications-system-symbolic"#;
pub const SETTINGS_TOOLTIP: &str = r#"Open toolbox settings"#;
pub const FOLDER_PICKER_ICON: &str = r#"folder-open-symbolic"#;
pub const FOLDER_PICKER_TOOLTIP: &str = r#"Select folder dialogue"#;

View File

@@ -1 +0,0 @@
pub mod toolbx;

View File

@@ -1,427 +0,0 @@
use serde::{Deserialize, Serialize};
use std::{fmt::Display, iter::zip, process::Command, str::FromStr, string::ParseError, sync::Arc};
#[derive(Debug, PartialEq)]
pub enum ToolbxError {
ParseStatusError(String),
JSONSerializationError(String),
CommandExecutionError(String),
CommandUnsuccessfulError(String),
}
impl std::error::Error for ToolbxError {}
impl Display for ToolbxError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ToolbxError::ParseStatusError(parse_error) => write!(f, "{}", parse_error),
ToolbxError::CommandExecutionError(command_exec_error) => {
write!(f, "{}", command_exec_error)
}
ToolbxError::JSONSerializationError(msg) => {
write!(f, "{}", msg)
}
ToolbxError::CommandUnsuccessfulError(command_unsuc_error) => {
write!(f, "{}", command_unsuc_error)
}
}
}
}
#[derive(Debug, PartialEq, Clone)]
pub enum ToolbxStatus {
Running,
Configured,
Created,
Exited,
}
impl Default for ToolbxStatus {
fn default() -> Self {
ToolbxStatus::Configured
}
}
impl FromStr for ToolbxStatus {
type Err = ToolbxError;
fn from_str(s: &str) -> Result<ToolbxStatus, ToolbxError> {
match s {
"running" => Ok(ToolbxStatus::Running),
"configured" => Ok(ToolbxStatus::Configured),
"created" => Ok(ToolbxStatus::Created),
"exited" => Ok(ToolbxStatus::Exited),
s => Err(ToolbxError::ParseStatusError(format!(
"'{}' is not a valid toolbx status.",
s
))),
}
}
}
#[derive(Debug, PartialEq, Default, Clone)]
pub struct ToolbxContainer {
pub id: String,
pub name: String,
pub created: String,
pub status: ToolbxStatus,
pub image: String,
}
pub type PodmanInspectArray = Vec<PodmanInspectInfo>;
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PodmanInspectInfo {
#[serde(rename = "Id")]
pub id: String,
#[serde(rename = "Created")]
pub created: String,
#[serde(rename = "State")]
pub state: PodManInspectState,
#[serde(rename = "Image")]
pub image: String,
#[serde(rename = "ImageName")]
pub image_name: String,
#[serde(rename = "Name")]
pub name: String,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PodManInspectState {
#[serde(rename = "Status")]
pub status: String,
}
pub enum ToolboxCreateParameter {
None,
Distro(String),
Image(String),
Release(String),
}
impl ToolbxContainer {
pub fn new(name: String) -> ToolbxContainer {
ToolbxContainer {
name: name,
..Default::default()
}
}
pub fn create(name: String, parameter: ToolboxCreateParameter) {
todo!("Implement actual functionality to create toolbox via commandline")
}
pub fn get_toolboxes() -> Vec<ToolbxContainer> {
let output = run_cmd_toolbx_list_containers();
println!("{}", output);
parse_cmd_list_containers(output.as_str())
}
fn parse_status(output: &str) -> Result<PodmanInspectInfo, ToolbxError> {
let result: Result<PodmanInspectArray, _> = serde_json::from_str(output);
match result {
Ok(inspect_vec) => match inspect_vec.first() {
Some(info) => Ok(info.clone()),
None => Err(ToolbxError::JSONSerializationError(
"Inspect command returned empty vector.".to_string(),
)),
},
Err(e) => Err(ToolbxError::JSONSerializationError(e.to_string())),
}
}
pub fn update_status(&mut self) -> Result<(), ToolbxError> {
let output = Command::new("flatpak-spawn")
.arg("--host")
.arg("podman")
.arg("container")
.arg("inspect")
.arg(self.name.clone())
.output()
.expect("Failed to execute command");
let output = String::from_utf8_lossy(&output.stdout).to_string();
let inspect_result = ToolbxContainer::parse_status(output.as_str())?;
self.status = ToolbxStatus::from_str(inspect_result.state.status.as_str())?;
Ok(())
}
pub fn stop(&mut self) -> Result<(), ToolbxError> {
let output = Command::new("flatpak-spawn")
.arg("--host") //Command::new("podman")
.arg("podman")
.arg("stop")
.arg(self.name.clone())
.output();
if output.is_err() {
return Err(ToolbxError::CommandExecutionError(
output.unwrap_err().to_string(),
));
}
let output = output.unwrap();
// Success: Output { status: ExitStatus(unix_wait_status(0)), stdout: "tbx_name\n", stderr: "" }
//Fail:
// Output {
// status: ExitStatus(unix_wait_status(32000)),
// stdout: "",
// stderr: "Error: no container with name or ID \"tbx_name\" found: no such container\n"
// }
if output.status.code() == Some(0) {
self.status = ToolbxStatus::Exited;
Ok(())
} else {
Err(ToolbxError::CommandUnsuccessfulError(
String::from_utf8_lossy(&output.stderr).into_owned(),
))
}
}
pub fn start(&mut self) -> Result<(), ToolbxError> {
let output = Command::new("flatpak-spawn")
.arg("--host") //Command::new("podman")
.arg("podman")
.arg("start")
.arg(self.name.clone())
.output();
if output.is_err() {
return Err(ToolbxError::CommandExecutionError(
output.unwrap_err().to_string(),
));
}
let output = output.unwrap();
// Success: status: Output { ExitStatus(unix_wait_status(0)), stdout: "tbx_name\n", stderr: "" }
// Fail: status:
// Output {
// status: ExitStatus(unix_wait_status(32000)),
// stdout: "",
// stderr: "Error: no container with name or ID \"tbx_name\" found: no such container\n"
// }
if output.status.code() == Some(0) {
self.status = ToolbxStatus::Running;
Ok(())
} else {
Err(ToolbxError::CommandUnsuccessfulError(
String::from_utf8_lossy(&output.stderr).into_owned(),
))
}
}
}
#[test]
fn test_start_1non_existing_container() {
// TODO: create container that exists based on simple image
// run command
// delete container
//let tbx = ToolbxContainer{created: "".to_string(), id: "".to_string(), name: "latex".to_string(), image: "".to_string(), status: ToolbxStatus::Exited};
//tbx.stop();
}
#[test]
fn test_inspect_parsing() {
let podman_inspect = concat!(
"[{",
"\"Id\": \"ae05203091ab4cdf047a9aeba6af8a7bed8105f7f59d09a35d2b64c837ecac0d\",",
"\"Created\": \"2021-12-10T20:51:43.140418098+01:00\",",
"\"State\": {",
"\"Status\": \"running\"",
"},",
"\"Image\": \"ab8bc106d4a710a7a27c538762864610467b3559f80b413d30e0a1bfcfe272a5\",",
"\"ImageName\": \"registry.fedoraproject.org/fedora-toolbox:35\",",
"\"Name\": \"rust\"",
"}]"
);
let inspect_info = ToolbxContainer::parse_status(podman_inspect).unwrap();
assert_eq!("running", inspect_info.state.status);
}
#[test]
fn test_start_non_existing_container() {
let name = "zy2lM6BdZoTnKHaVPkUJ".to_string();
let mut tbx = ToolbxContainer {
created: "".to_string(),
id: "".to_string(),
name: name.clone(),
image: "".to_string(),
status: ToolbxStatus::Exited,
};
assert_eq!(
Err(ToolbxError::CommandUnsuccessfulError(format!(
"Error: no container with name or ID \"{}\" found: no such container\n",
name
))),
tbx.start()
);
}
pub fn run_cmd_toolbx_list_containers() -> String {
let output = Command::new("flatpak-spawn")
.arg("--host")
.arg("toolbox")
.arg("list")
.arg("--containers")
.output()
.expect("Failed to execute command");
println!("{:?}", String::from_utf8_lossy(&output.stdout).to_string());
String::from_utf8_lossy(&output.stdout).to_string()
}
#[test]
#[ignore]
fn test_cmd_list_containers() {
// This requires toolbx to be installed
let toolbox_cmd_container_header =
"CONTAINER ID CONTAINER NAME CREATED STATUS IMAGE NAME";
assert!(run_cmd_toolbx_list_containers().starts_with(toolbox_cmd_container_header));
}
fn tokenize_line_list_containers(line: &str) -> Vec<String> {
let mut tokens = Vec::<String>::new();
let mut current_token = Vec::<char>::new();
let mut whitespace_section = false;
let mut iter = line.chars().peekable();
while let Some(&c) = iter.peek() {
match (whitespace_section, c) {
(false, ' ') => {
iter.next();
if Some(' ') == iter.peek().map(|x| x.clone()) {
whitespace_section = true;
} else {
current_token.push(c);
}
}
(true, ' ') => {
iter.next();
}
(true, c) => {
whitespace_section = false;
tokens.push(current_token.into_iter().collect());
current_token = Vec::new();
current_token.push(c);
iter.next();
}
(false, c) => {
current_token.push(c);
iter.next();
}
}
}
tokens.push(current_token.into_iter().collect());
tokens
}
#[test]
fn test_tokenize_line_list_containers() {
let toolbox_cmd_container_header = "ae05203091ab rust 4 months ago \
running registry.fedoraproject.org/fedora-toolbox:35";
let target = vec![
"ae05203091ab",
"rust",
"4 months ago",
"running",
"registry.fedoraproject.org/fedora-toolbox:35",
];
let result = tokenize_line_list_containers(toolbox_cmd_container_header);
assert_eq!(target, result);
}
fn parse_line_list_containers(line: &str) -> Result<ToolbxContainer, ToolbxError> {
let tokens = tokenize_line_list_containers(line);
if tokens.len() != 5 {
panic! {"Expected 5 tokens found {} in {:?}", tokens.len(), tokens};
}
Ok(ToolbxContainer {
id: tokens[0].clone(),
name: tokens[1].clone(),
created: tokens[2].clone(),
status: ToolbxStatus::from_str(&tokens[3])?,
image: tokens[4].clone(),
})
}
#[test]
fn test_parse_line_list_containers() {
let toolbox_cmd_container_header = "ae05203091ab rust 4 months ago \
running registry.fedoraproject.org/fedora-toolbox:35";
let target = ToolbxContainer {
id: "ae05203091ab".to_string(),
name: "rust".to_string(),
created: "4 months ago".to_string(),
status: ToolbxStatus::Running,
image: "registry.fedoraproject.org/fedora-toolbox:35".to_string(),
};
let result = parse_line_list_containers(toolbox_cmd_container_header);
assert_eq!(target, result.unwrap());
}
fn parse_cmd_list_containers(output: &str) -> Vec<ToolbxContainer> {
let lines = output.trim().split("\n").skip(1);
println!("{:?}", lines);
lines.map(parse_line_list_containers).flatten().collect()
}
#[test]
fn test_parse_cmd_list_containers() {
// This requires toolbx to be installed
let toolbox_cmd_container_header = concat!(
"CONTAINER ID CONTAINER NAME CREATED STATUS IMAGE NAME\n",
"cee1002b5f0b fedora-toolbox-35 2 months ago exited registry.fedoraproject.org/fedora-toolbox:35\n",
"9b611313bf65 latex 4 months ago configured registry.fedoraproject.org/fedora-toolbox:35\n",
"1235203091ab website 4 months ago created registry.fedoraproject.org/fedora-toolbox:35\n",
"ae05203091ab rust 4 months ago running registry.fedoraproject.org/fedora-toolbox:35\n"
);
let desired_result = vec![
ToolbxContainer {
id: "cee1002b5f0b".to_string(),
name: "fedora-toolbox-35".to_string(),
created: "2 months ago".to_string(),
status: ToolbxStatus::Exited,
image: "registry.fedoraproject.org/fedora-toolbox:35".to_string(),
},
ToolbxContainer {
id: "9b611313bf65".to_string(),
name: "latex".to_string(),
created: "4 months ago".to_string(),
status: ToolbxStatus::Configured,
image: "registry.fedoraproject.org/fedora-toolbox:35".to_string(),
},
ToolbxContainer {
id: "1235203091ab".to_string(),
name: "website".to_string(),
created: "4 months ago".to_string(),
status: ToolbxStatus::Created,
image: "registry.fedoraproject.org/fedora-toolbox:35".to_string(),
},
ToolbxContainer {
id: "ae05203091ab".to_string(),
name: "rust".to_string(),
created: "4 months ago".to_string(),
status: ToolbxStatus::Running,
image: "registry.fedoraproject.org/fedora-toolbox:35".to_string(),
},
];
for (result, desired) in zip(
parse_cmd_list_containers(toolbox_cmd_container_header).iter(),
desired_result.iter(),
) {
assert_eq!(result, desired)
}
}