Browse Source

draft browser database features

master
yggverse 2 months ago
parent
commit
f34202bb46
  1. 14
      src/app.rs
  2. 60
      src/app/browser.rs
  3. 98
      src/app/browser/database.rs

14
src/app.rs

@ -31,8 +31,8 @@ pub struct App { @@ -31,8 +31,8 @@ pub struct App {
impl App {
// Construct
pub fn new(profile_database_connection: Arc<Connection>, profile_path: PathBuf) -> Self {
// Init database model
let database = match Database::init(profile_database_connection) {
// Init database
let database = match Database::init(profile_database_connection.clone()) {
Ok(database) => Arc::new(database),
Err(error) => panic!("{error}"), // @TODO
};
@ -82,7 +82,7 @@ impl App { @@ -82,7 +82,7 @@ impl App {
// Init components
let browser = Arc::new(Browser::new(
/*db.clone(),*/
profile_database_connection,
profile_path,
action_tool_debug.simple(),
action_tool_profile_directory.simple(),
@ -140,7 +140,7 @@ impl App { @@ -140,7 +140,7 @@ impl App {
for record in records {
match database.delete(record.id) {
Ok(_) => {
// Delegate clean action to children components
// Delegate clean action to childs
browser.clean(record.id);
}
Err(error) => panic!("{error}"), // @TODO
@ -150,10 +150,8 @@ impl App { @@ -150,10 +150,8 @@ impl App {
// Save current session to DB
match database.add() {
Ok(_) => {
let app_id = database.last_insert_id();
// Delegate save action to children components
browser.save(app_id);
// Delegate save action to childs
browser.save(database.last_insert_id());
}
Err(error) => panic!("{error}"), // @TODO
}

60
src/app/browser.rs

@ -1,12 +1,14 @@ @@ -1,12 +1,14 @@
mod database;
mod header;
mod main;
use database::Database;
use header::Header;
use main::Main;
use gtk::{
gio::{AppInfo, AppLaunchContext, SimpleAction},
prelude::{ActionMapExt, GtkWindowExt},
prelude::{ActionMapExt, GtkWindowExt, WidgetExt},
ApplicationWindow,
};
use std::{path::PathBuf, sync::Arc};
@ -16,18 +18,19 @@ const DEFAULT_WIDTH: i32 = 640; @@ -16,18 +18,19 @@ const DEFAULT_WIDTH: i32 = 640;
pub struct Browser {
// Extras
// db: db::Browser,
widget: ApplicationWindow,
database: Arc<Database>,
// Components
// header: Arc<Header>,
// main: Arc<Main>,
// GTK
widget: ApplicationWindow,
}
impl Browser {
// Construct
pub fn new(
// Extras
// connection: Arc<sqlite::Connection>,
profile_database_connection: Arc<sqlite::Connection>,
profile_path: PathBuf,
// Actions
action_tool_debug: Arc<SimpleAction>,
@ -44,7 +47,10 @@ impl Browser { @@ -44,7 +47,10 @@ impl Browser {
action_tab_pin: Arc<SimpleAction>,
) -> Browser {
// Init database
// let db = db::Browser::new(connection); @TODO
let database = match Database::init(profile_database_connection) {
Ok(database) => Arc::new(database),
Err(error) => panic!("{error}"), // @TODO
};
// Init components
let header = Arc::new(Header::new(
@ -183,18 +189,52 @@ impl Browser { @@ -183,18 +189,52 @@ impl Browser {
// Return new activated Browser struct
Self {
// db,
database,
widget,
// Components
// header,
// main,
}
}
// Actions
pub fn clean(&self, app_id: i64) {}
pub fn restore(&self, app_id: i64) {}
pub fn save(&self, app_id: i64) {}
pub fn clean(&self, app_id: i64) {
match self.database.records(app_id) {
Ok(records) => {
for record in records {
match self.database.delete(record.id) {
Ok(_) => {
// Delegate clean action to childs
// self.header.clean(record.id);
// self.main.clean(record.id);
}
Err(error) => panic!("{error}"), // @TODO
}
}
}
Err(error) => panic!("{error}"), // @TODO
}
}
pub fn restore(&self, app_id: i64) {
// @TODO
}
pub fn save(&self, app_id: i64) {
match self.database.add(
app_id,
self.widget.width(),
self.widget.height(),
self.widget.is_fullscreen(),
) {
Ok(_) => {
// Delegate save action to childs
// let id = self.database.last_insert_id();
// self.header.save(id);
// self.main.save(id);
}
Err(error) => panic!("{error}"), // @TODO
}
}
// Getters
pub fn widget(&self) -> &ApplicationWindow {

98
src/app/browser/database.rs

@ -0,0 +1,98 @@ @@ -0,0 +1,98 @@
use sqlite::{Connection, Error};
use std::sync::Arc;
pub struct Table {
pub id: i64,
pub app_id: i64,
// pub time: i64,
pub width: i32,
pub height: i32,
pub is_fullscreen: bool,
}
pub struct Database {
connection: Arc<Connection>,
}
impl Database {
pub fn init(connection: Arc<Connection>) -> Result<Database, Error> {
connection.execute(
"CREATE TABLE IF NOT EXISTS `app_browser`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`time` INTEGER NOT NULL DEFAULT (UNIXEPOCH('NOW')),
`app_id` INTEGER NOT NULL,
`width` INTEGER NOT NULL,
`height` INTEGER NOT NULL,
`is_fullscreen` INTEGER NOT NULL
)",
[],
)?;
Ok(Self { connection })
}
pub fn add(
&self,
app_id: i64,
width: i32,
height: i32,
is_fullscreen: bool,
) -> Result<usize, Error> {
self.connection.execute(
"INSERT INTO `app_browser` (
`app_id`,
`width`,
`height`,
`is_fullscreen`
) VALUES (?, ?, ?, ?)",
[
app_id,
width as i64,
height as i64,
match is_fullscreen {
true => 1,
false => 0,
},
],
)
}
pub fn records(&self, app_id: i64) -> Result<Vec<Table>, Error> {
let mut statement = self.connection.prepare(
"SELECT `id`,
`app_id`,
`width`,
`height`,
`is_fullscreen` FROM `app_browser` WHERE `app_id` = ?",
)?;
let result = statement.query_map([app_id], |row| {
Ok(Table {
id: row.get(0)?,
app_id: row.get(1)?,
width: row.get(2)?,
height: row.get(3)?,
is_fullscreen: row.get(4)?,
})
})?;
let mut records = Vec::new();
for record in result {
let table = record?;
records.push(table);
}
Ok(records)
}
pub fn delete(&self, id: i64) -> Result<usize, Error> {
self.connection
.execute("DELETE FROM `app_browser` WHERE `id` = ?", [id])
}
pub fn last_insert_id(&self) -> i64 {
self.connection.last_insert_rowid()
}
}
Loading…
Cancel
Save