2024-10-05 03:27:09 +03:00
|
|
|
mod database;
|
|
|
|
|
|
|
|
use database::Database;
|
|
|
|
|
|
|
|
use gtk::{prelude::GtkWindowExt, ApplicationWindow, Box, HeaderBar};
|
2024-10-06 01:02:23 +03:00
|
|
|
use sqlite::{Connection, Transaction};
|
2024-10-06 00:43:35 +03:00
|
|
|
use std::sync::{Arc, RwLock};
|
2024-10-05 03:27:09 +03:00
|
|
|
|
|
|
|
// Default options
|
|
|
|
const DEFAULT_HEIGHT: i32 = 480;
|
|
|
|
const DEFAULT_WIDTH: i32 = 640;
|
|
|
|
const MAXIMIZED: bool = false;
|
|
|
|
|
|
|
|
pub struct Widget {
|
|
|
|
database: Arc<Database>,
|
|
|
|
application_window: ApplicationWindow,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Widget {
|
|
|
|
// Construct
|
|
|
|
pub fn new(
|
2024-10-06 01:02:23 +03:00
|
|
|
profile_database_connection: Arc<RwLock<Connection>>,
|
2024-10-05 03:27:09 +03:00
|
|
|
titlebar: &HeaderBar,
|
|
|
|
child: &Box,
|
|
|
|
) -> Self {
|
|
|
|
// Init database
|
2024-10-06 00:43:35 +03:00
|
|
|
let database = {
|
|
|
|
// Init writable database connection
|
|
|
|
let mut connection = match profile_database_connection.write() {
|
|
|
|
Ok(connection) => connection,
|
2024-10-06 00:49:43 +03:00
|
|
|
Err(e) => todo!("{e}"),
|
2024-10-06 00:43:35 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Init new transaction
|
|
|
|
let transaction = match connection.transaction() {
|
|
|
|
Ok(transaction) => transaction,
|
2024-10-06 00:49:43 +03:00
|
|
|
Err(e) => todo!("{e}"),
|
2024-10-06 00:43:35 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Init database structure
|
|
|
|
match Database::init(&transaction) {
|
|
|
|
Ok(database) => match transaction.commit() {
|
|
|
|
Ok(_) => Arc::new(database),
|
2024-10-06 00:49:43 +03:00
|
|
|
Err(e) => todo!("{e}"),
|
2024-10-06 00:43:35 +03:00
|
|
|
},
|
2024-10-06 00:49:43 +03:00
|
|
|
Err(e) => todo!("{e}"),
|
2024-10-06 00:43:35 +03:00
|
|
|
}
|
2024-10-05 03:27:09 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Init GTK
|
|
|
|
let application_window = ApplicationWindow::builder()
|
|
|
|
.titlebar(titlebar)
|
|
|
|
.child(child)
|
|
|
|
.default_height(DEFAULT_HEIGHT)
|
|
|
|
.default_width(DEFAULT_WIDTH)
|
|
|
|
.maximized(MAXIMIZED)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
// Return new struct
|
|
|
|
Self {
|
|
|
|
database,
|
|
|
|
application_window,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actions
|
2024-10-06 00:43:35 +03:00
|
|
|
pub fn clean(&self, tx: &Transaction, app_browser_id: &i64) {
|
|
|
|
match self.database.records(tx, app_browser_id) {
|
2024-10-05 03:27:09 +03:00
|
|
|
Ok(records) => {
|
|
|
|
for record in records {
|
2024-10-06 00:43:35 +03:00
|
|
|
match self.database.delete(tx, &record.id) {
|
2024-10-05 03:27:09 +03:00
|
|
|
Ok(_) => {
|
|
|
|
// Delegate clean action to childs
|
|
|
|
// nothing yet..
|
|
|
|
}
|
2024-10-06 00:49:43 +03:00
|
|
|
Err(e) => todo!("{e}"),
|
2024-10-05 03:27:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-06 00:49:43 +03:00
|
|
|
Err(e) => todo!("{e}"),
|
2024-10-05 03:27:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 00:43:35 +03:00
|
|
|
pub fn restore(&self, tx: &Transaction, app_browser_id: &i64) {
|
|
|
|
match self.database.records(tx, app_browser_id) {
|
2024-10-05 15:32:03 +03:00
|
|
|
Ok(records) => {
|
|
|
|
for record in records {
|
|
|
|
// Restore widget
|
|
|
|
self.application_window.set_maximized(record.is_maximized);
|
|
|
|
self.application_window
|
|
|
|
.set_default_size(record.default_width, record.default_height);
|
|
|
|
|
|
|
|
// Delegate restore action to childs
|
|
|
|
// nothing yet..
|
|
|
|
}
|
|
|
|
}
|
2024-10-06 00:49:43 +03:00
|
|
|
Err(e) => todo!("{e}"),
|
2024-10-05 15:32:03 +03:00
|
|
|
}
|
2024-10-05 03:27:09 +03:00
|
|
|
}
|
|
|
|
|
2024-10-06 00:43:35 +03:00
|
|
|
pub fn save(&self, tx: &Transaction, app_browser_id: &i64) {
|
2024-10-05 03:27:09 +03:00
|
|
|
match self.database.add(
|
2024-10-06 00:43:35 +03:00
|
|
|
tx,
|
2024-10-05 03:27:09 +03:00
|
|
|
app_browser_id,
|
2024-10-05 04:54:08 +03:00
|
|
|
&self.application_window.default_width(),
|
|
|
|
&self.application_window.default_height(),
|
|
|
|
&self.application_window.is_maximized(),
|
2024-10-05 03:27:09 +03:00
|
|
|
) {
|
|
|
|
Ok(_) => {
|
|
|
|
// Delegate save action to childs
|
|
|
|
// let id = self.database.last_insert_id();
|
|
|
|
// nothing yet..
|
|
|
|
}
|
2024-10-06 00:49:43 +03:00
|
|
|
Err(e) => todo!("{e}"),
|
2024-10-05 03:27:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getters
|
|
|
|
pub fn application_window(&self) -> &ApplicationWindow {
|
|
|
|
&self.application_window
|
|
|
|
}
|
|
|
|
}
|