feat: add graphical interface with relm4 and libadw

This commit is contained in:
uku 2025-05-09 22:35:00 +02:00
parent 837ce3ab3e
commit 601ee85b5f
Signed by: uku
SSH key fingerprint: SHA256:4P0aN6M8ajKukNi6aPOaX0LacanGYtlfjmN+m/sHY/o
6 changed files with 850 additions and 29 deletions

View file

@ -1,11 +1,19 @@
mod gobject;
mod zipline;
use std::{path::Path, time::Duration};
use std::{borrow::Cow, path::PathBuf, time::Duration};
use color_eyre::eyre::{OptionExt, Result, bail};
use gobject::GtkZiplineFolder;
use relm4::{
ComponentParts, RelmApp, SimpleComponent,
adw::{self, prelude::*},
gtk::{self, gio, glib::clone},
};
use tracing::Level;
use tracing_subscriber::EnvFilter;
use urlencoding::encode;
use urlencoding::Encoded;
use zipline::ZiplineFolder;
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
struct Config {
@ -27,6 +35,172 @@ impl Config {
}
}
#[derive(Debug)]
enum Message {
SetPath(PathBuf),
SetFolder(ZiplineFolder),
StartTheProcess,
}
struct Widgets {
file_picker_row: adw::ActionRow,
send_button: gtk::Button,
}
struct Tyrolienne {
config: Config,
video_path: Option<PathBuf>,
folder: ZiplineFolder,
}
impl Tyrolienne {
fn display_video_path(&self) -> Cow<'_, str> {
self.video_path
.as_ref()
.map(|p| p.to_string_lossy())
.unwrap_or(Cow::Borrowed("None"))
}
}
impl SimpleComponent for Tyrolienne {
type Input = Message;
type Output = ();
type Init = (Config, Vec<ZiplineFolder>);
type Root = adw::ApplicationWindow;
type Widgets = Widgets;
fn init_root() -> Self::Root {
adw::ApplicationWindow::builder()
.title("Tyrolienne")
.default_width(500)
.build()
}
fn init(
(config, folders): Self::Init,
root: Self::Root,
sender: relm4::ComponentSender<Self>,
) -> relm4::ComponentParts<Self> {
let model = Tyrolienne {
config,
video_path: None,
folder: folders[0].clone(),
};
let file_picker_row = adw::ActionRow::builder()
.activatable(true)
.title("Video file")
.subtitle(model.display_video_path())
.css_classes(["property"])
.build();
file_picker_row.connect_activated(clone!(
#[strong]
sender,
#[strong]
root,
move |_| {
let file_dialog = gtk::FileDialog::new();
file_dialog.open(
Some(&root),
gio::Cancellable::NONE,
clone!(
#[strong]
sender,
move |file| {
if let Some(path) = file.ok().and_then(|f| f.path()) {
sender.input(Message::SetPath(path));
}
}
),
);
}
));
let gtk_folders = folders
.iter()
.map(GtkZiplineFolder::from_folder)
.collect::<Vec<_>>();
let store = gio::ListStore::new::<GtkZiplineFolder>();
store.extend_from_slice(&gtk_folders);
let folder_row = adw::ComboRow::builder()
.title("Folder")
.model(&store)
.expression(gtk::PropertyExpression::new(
GtkZiplineFolder::r#type(),
None::<&gtk::Expression>,
"name",
))
.build();
folder_row.connect_activated(clone!(
#[strong]
sender,
move |r| {
if let Some(item) = r
.selected_item()
.and_then(|i| i.downcast::<GtkZiplineFolder>().ok())
{
sender.input(Message::SetFolder(item.as_folder()));
}
}
));
let send_button = gtk::Button::builder()
.label("Send")
.sensitive(false)
.margin_start(32)
.margin_end(32)
.margin_bottom(32)
.build();
send_button.connect_clicked(clone!(
#[strong]
sender,
move |_| sender.input(Message::StartTheProcess)
));
let list = gtk::ListBox::builder()
.margin_top(32)
.margin_end(32)
.margin_bottom(32)
.margin_start(32)
.selection_mode(gtk::SelectionMode::None)
.css_classes(["boxed-list"])
.build();
list.append(&file_picker_row);
list.append(&folder_row);
let root_box = gtk::Box::new(gtk::Orientation::Vertical, 0);
root_box.append(&adw::HeaderBar::new());
root_box.append(&list);
root_box.append(&send_button);
root.set_content(Some(&root_box));
let widgets = Widgets {
file_picker_row,
send_button,
};
ComponentParts { model, widgets }
}
fn update(&mut self, message: Self::Input, _sender: relm4::ComponentSender<Self>) {
match message {
Message::SetPath(path) => self.video_path = Some(path),
Message::SetFolder(folder) => self.folder = folder,
Message::StartTheProcess => the_process(self).unwrap(),
}
}
fn update_view(&self, widgets: &mut Self::Widgets, _sender: relm4::ComponentSender<Self>) {
widgets
.file_picker_row
.set_subtitle(&self.display_video_path());
widgets.send_button.set_sensitive(self.video_path.is_some());
}
}
fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env().add_directive(Level::INFO.into()))
@ -34,6 +208,17 @@ fn main() -> Result<()> {
color_eyre::install()?;
// TODO: show dialog in case these error
let config = get_config()?;
let folders = zipline::get_folders(&config)?;
let app = RelmApp::new("net.uku3lig.Tyrolienne");
app.run::<Tyrolienne>((config, folders));
Ok(())
}
fn get_config() -> Result<Config> {
let Some(config_dir) = dirs::config_dir() else {
bail!("could not get config dir!");
};
@ -59,37 +244,33 @@ fn main() -> Result<()> {
bail!("zipline url is empty! please set it at {config_file_path:?}");
}
let args: Vec<String> = std::env::args().collect();
let file = args.get(1).ok_or_eyre(
"please specify the path to the file you want to upload as the first command line argument",
)?;
Ok(config)
}
let folders = zipline::get_folders(&config)?;
let folder = &folders[0];
fn the_process(app: &Tyrolienne) -> Result<()> {
tracing::info!("uploading to folder '{}'...", app.folder.name);
tracing::info!("uploading to folder '{}'...", folder.name);
let res = zipline::upload_file(&config, folder, Path::new(file))?;
let res = zipline::upload_file(&app.config, &app.folder, app.video_path.as_ref().unwrap())?;
let zp_file = &res.files[0];
tracing::info!("recalculating thumbnails...");
zipline::recalc_thumbnails(&config)?;
zipline::recalc_thumbnails(&app.config)?;
std::thread::sleep(Duration::from_secs(5));
tracing::info!("fetching thumbnail url...");
let res = zipline::get_file_details(&config, &zp_file.id)?;
let res = zipline::get_file_details(&app.config, &zp_file.id)?;
let thumbnail_url = res
.thumbnail_url(&config)
.thumbnail_url(&app.config)
.ok_or_eyre("could not get thumbnail url")?;
// TODO get w&h from video
let autocomp_url = format!(
"https://autocompressor.net/av1?v={}&i={}&w=1920&h=1080",
encode(&zp_file.url),
encode(&thumbnail_url)
Encoded(&zp_file.url),
Encoded(&thumbnail_url)
);
tracing::info!("url: {autocomp_url}");