implement proxy exceptions tab

This commit is contained in:
yggverse 2025-07-26 05:21:29 +03:00
parent b15e4a5fc1
commit a7c6e79aac
13 changed files with 462 additions and 72 deletions

View file

@ -3,6 +3,7 @@ mod memory;
use anyhow::Result;
use database::Database;
use gtk::glib::DateTime;
use memory::Memory;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
@ -25,10 +26,12 @@ impl Ignore {
{
// build in-memory index...
let mut m = memory.borrow_mut();
for i in rows {
for row in rows {
m.push(Memory {
is_enabled: i.is_enabled,
host: i.host,
id: Some(row.id),
host: row.host,
is_enabled: row.is_enabled,
time: DateTime::from_unix_local(row.time)?,
});
}
}
@ -36,7 +39,35 @@ impl Ignore {
Ok(Self { database, memory })
}
// Actions
// Setters
pub fn add(&self, id: Option<i64>, is_enabled: bool, host: String, time: DateTime) {
self.memory.borrow_mut().push(Memory {
id,
host,
is_enabled,
time,
}) // @TODO validate?
}
pub fn clear(&self) {
self.memory.borrow_mut().clear();
}
pub fn save(&self) -> Result<()> {
let rules = self.memory.take();
let mut keep_id = Vec::with_capacity(rules.len());
for rule in rules {
keep_id.push(self.database.persist(
rule.id,
rule.time.to_unix(),
rule.is_enabled,
rule.host,
)?);
}
self.database.clean(keep_id)?;
Ok(())
}
// Getters

View file

@ -26,6 +26,36 @@ impl Database {
pub fn rows(&self) -> Result<Vec<Row>> {
rows(&self.pool.get()?.unchecked_transaction()?, self.profile_id)
}
// Setters
pub fn clean(&self, keep_id: Vec<i64>) -> Result<()> {
let mut c = self.pool.get()?;
let tx = c.transaction()?;
clean(&tx, keep_id)?;
tx.commit()?;
Ok(())
}
pub fn persist(
&self,
id: Option<i64>,
time: i64,
is_enabled: bool,
host: String,
) -> Result<i64> {
let mut c = self.pool.get()?;
let tx = c.transaction()?;
let id = match id {
Some(id) => {
update(&tx, id, time, is_enabled, host)?;
id
}
None => insert(&tx, self.profile_id, time, is_enabled, host)?,
};
tx.commit()?;
Ok(id)
}
}
// Low-level DB API
@ -46,6 +76,54 @@ pub fn init(tx: &Transaction) -> Result<usize> {
)?)
}
fn clean(tx: &Transaction, keep_id: Vec<i64>) -> Result<usize> {
if keep_id.is_empty() {
return Ok(0);
}
Ok(tx.execute(
&format!(
"DELETE FROM `profile_proxy_ignore` WHERE `id` NOT IN ({})",
keep_id
.into_iter()
.map(|id| id.to_string())
.collect::<Vec<String>>()
.join(",")
),
[],
)?)
}
fn insert(
tx: &Transaction,
profile_id: i64,
time: i64,
is_enabled: bool,
host: String,
) -> Result<i64> {
tx.execute(
"INSERT INTO `profile_proxy_ignore` (
`profile_id`,
`time`,
`is_enabled`,
`host`
) VALUES (?, ?, ?, ?)",
(profile_id, time, is_enabled, host),
)?;
Ok(tx.last_insert_rowid())
}
fn update(tx: &Transaction, id: i64, time: i64, is_enabled: bool, host: String) -> Result<usize> {
Ok(tx.execute(
"UPDATE `profile_proxy_ignore`
SET `time` = ?,
`is_enabled` = ?,
`host` = ?
WHERE `id` = ?",
(time, is_enabled, host, id),
)?)
}
fn rows(tx: &Transaction, profile_id: i64) -> Result<Vec<Row>> {
let mut stmt = tx.prepare(
"SELECT `id`,
@ -60,9 +138,9 @@ fn rows(tx: &Transaction, profile_id: i64) -> Result<Vec<Row>> {
let result = stmt.query_map([profile_id], |row| {
Ok(Row {
//id: row.get(0)?,
id: row.get(0)?,
//profile_id: row.get(1)?,
//time: DateTime::from_unix_local(row.get(2)?).unwrap(),
time: row.get(2)?,
host: row.get(3)?,
is_enabled: row.get(4)?,
})

View file

@ -1,4 +1,6 @@
pub struct Row {
pub host: String,
pub id: i64,
pub is_enabled: bool,
pub time: i64,
}

View file

@ -1,5 +1,7 @@
#[derive(Clone)]
pub struct Memory {
pub host: String,
pub id: Option<i64>,
pub is_enabled: bool,
pub time: gtk::glib::DateTime,
}

View file

@ -29,10 +29,10 @@ impl Rule {
for row in rows {
m.push(Memory {
id: Some(row.id),
time: row.time,
is_enabled: row.is_enabled,
priority: row.priority,
request: row.request,
time: DateTime::from_unix_local(row.time)?,
url: row.url,
});
}
@ -52,8 +52,7 @@ impl Rule {
url: String,
time: DateTime,
) {
let mut rules = self.memory.borrow_mut();
rules.push(Memory {
self.memory.borrow_mut().push(Memory {
id,
time,
is_enabled,
@ -73,7 +72,7 @@ impl Rule {
for rule in rules {
keep_id.push(self.database.persist(
rule.id,
rule.time,
rule.time.to_unix(),
rule.is_enabled,
rule.priority,
rule.request,

View file

@ -1,7 +1,6 @@
mod row;
use anyhow::Result;
use gtk::glib::DateTime;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
use row::Row;
@ -41,7 +40,7 @@ impl Database {
pub fn persist(
&self,
id: Option<i64>,
time: DateTime,
time: i64,
is_enabled: bool,
priority: i32,
request: String,
@ -109,7 +108,7 @@ fn clean(tx: &Transaction, keep_id: Vec<i64>) -> Result<usize> {
fn insert(
tx: &Transaction,
profile_id: i64,
time: DateTime,
time: i64,
is_enabled: bool,
priority: i32,
request: String,
@ -124,14 +123,7 @@ fn insert(
`request`,
`url`
) VALUES (?, ?, ?, ?, ?, ?)",
(
profile_id,
time.to_unix(),
is_enabled,
priority,
request,
url,
),
(profile_id, time, is_enabled, priority, request, url),
)?;
Ok(tx.last_insert_rowid())
}
@ -139,7 +131,7 @@ fn insert(
fn update(
tx: &Transaction,
id: i64,
time: DateTime,
time: i64,
is_enabled: bool,
priority: i32,
request: String,
@ -154,7 +146,7 @@ fn update(
`url` = ?
WHERE `id` = ?",
(time.to_unix(), is_enabled, priority, request, url, id),
(time, is_enabled, priority, request, url, id),
)?)
}
@ -177,7 +169,7 @@ fn rows(tx: &Transaction, profile_id: i64) -> Result<Vec<Row>> {
Ok(Row {
id: row.get(0)?,
//profile_id: row.get(1)?,
time: DateTime::from_unix_local(row.get(2)?).unwrap(),
time: row.get(2)?,
is_enabled: row.get(3)?,
priority: row.get(4)?,
request: row.get(5)?,

View file

@ -3,6 +3,6 @@ pub struct Row {
pub is_enabled: bool,
pub priority: i32,
pub request: String,
pub time: gtk::glib::DateTime,
pub time: i64,
pub url: String,
}