mod about;
mod action;
mod database;
mod widget;
mod window;
use about::About;
use action::Action;
use widget::Widget;
use window::Window;
use crate::Profile;
use gtk::{
gio::{Cancellable, File},
prelude::{GtkWindowExt, IsA},
Application, FileLauncher,
};
use sqlite::Transaction;
use std::rc::Rc;
pub struct Browser {
action: Rc<Action>,
widget: Rc<Widget>,
window: Rc<Window>,
}
impl Browser {
pub fn new(profile: Rc<Profile>) -> Browser {
let action = Rc::new(Action::new());
let window = Rc::new(Window::new(profile.clone(), action.clone()));
let widget = Rc::new(Widget::new(
window.widget().gobject(),
&[
(
action.id(),
action.gobject().clone(),
),
(
window.action().id(),
window.action().gobject().clone(),
),
],
));
action.about().connect_activate({
let window = window.clone();
move || {
About::new().present(Some(window.widget().gobject()));
}
});
action.close().connect_activate({
let widget = widget.clone();
move || widget.gobject().close()
});
action.debug().connect_activate({
let widget = widget.clone();
move || {
widget.gobject().emit_enable_debugging(true);
}
});
action.profile().connect_activate({
let profile = profile.clone();
move || {
FileLauncher::new(Some(&File::for_path(profile.config_path.as_path()))).launch(
None::<>k::Window>,
None::<&Cancellable>,
|result| {
if let Err(error) = result {
println!("{error}")
}
},
); }
});
action.update().connect_activate({
let window = window.clone();
move |tab_item_id| window.update(tab_item_id)
});
Self {
action,
widget,
window,
}
}
pub fn clean(&self, transaction: &Transaction, app_id: &i64) -> Result<(), String> {
match database::select(transaction, app_id) {
Ok(records) => {
for record in records {
match database::delete(transaction, &record.id) {
Ok(_) => {
self.window.clean(transaction, &record.id)?;
self.widget.clean(transaction, &record.id)?;
}
Err(e) => return Err(e.to_string()),
}
}
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
pub fn restore(&self, transaction: &Transaction, app_id: &i64) -> Result<(), String> {
match database::select(transaction, app_id) {
Ok(records) => {
for record in records {
self.widget.restore(transaction, &record.id)?;
self.window.restore(transaction, &record.id)?;
}
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
pub fn save(&self, transaction: &Transaction, app_id: &i64) -> Result<(), String> {
match database::insert(transaction, app_id) {
Ok(_) => {
let id = database::last_insert_id(transaction);
self.widget.save(transaction, &id)?;
self.window.save(transaction, &id)?;
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
pub fn init(&self, application: Option<&impl IsA<Application>>) -> &Self {
self.widget.gobject().set_application(application);
self.window.init();
self
}
pub fn present(&self) -> &Self {
self.widget.gobject().present();
self
}
pub fn update(&self) {
self.window.update(None);
}
pub fn action(&self) -> &Rc<Action> {
&self.action
}
pub fn window(&self) -> &Rc<Window> {
&self.window
}
}
pub fn migrate(tx: &Transaction) -> Result<(), String> {
if let Err(e) = database::init(tx) {
return Err(e.to_string());
}
window::migrate(tx)?;
widget::migrate(tx)?;
Ok(())
}