implement proxy settings dialog (rules tab)

This commit is contained in:
yggverse 2025-07-25 13:41:21 +03:00
parent b548fb16d3
commit e548efc93f
12 changed files with 660 additions and 12 deletions

View file

@ -2,6 +2,7 @@ mod ignore;
mod rule;
use anyhow::Result;
use gtk::glib::DateTime;
use ignore::Ignore;
use r2d2::Pool;
use r2d2_sqlite::SqliteConnectionManager;
@ -34,6 +35,44 @@ impl Database {
}
// Setters
pub fn clean_rules(&self, keep_id: Vec<i64>) -> Result<()> {
let mut c = self.pool.get()?;
let tx = c.transaction()?;
clean_rules(&tx, keep_id)?;
tx.commit()?;
Ok(())
}
pub fn persist_rule(
&self,
id: Option<i64>,
time: DateTime,
is_enabled: bool,
priority: i32,
request: String,
url: String,
) -> Result<i64> {
let mut c = self.pool.get()?;
let tx = c.transaction()?;
let id = match id {
Some(id) => {
update_rule(&tx, id, time, is_enabled, priority, request, url)?;
id
}
None => insert_rule(
&tx,
self.profile_id,
time,
is_enabled,
priority,
request,
url,
)?,
};
tx.commit()?;
Ok(id)
}
}
// Low-level DB API
@ -50,8 +89,7 @@ pub fn init(tx: &Transaction) -> Result<usize> {
`is_enabled` INTEGER NOT NULL,
`host` VARCHAR(255) NOT NULL,
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`),
UNIQUE (`host`)
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`)
)",
[],
)?;
@ -67,8 +105,7 @@ pub fn init(tx: &Transaction) -> Result<usize> {
`request` VARCHAR(1024) NOT NULL,
`url` VARCHAR(255) NOT NULL,
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`),
UNIQUE (`request`)
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`)
)",
[],
)?;
@ -76,6 +113,75 @@ pub fn init(tx: &Transaction) -> Result<usize> {
Ok(s)
}
pub fn clean_rules(tx: &Transaction, keep_id: Vec<i64>) -> Result<usize> {
if keep_id.is_empty() {
return Ok(0);
}
Ok(tx.execute(
&format!(
"DELETE FROM `profile_proxy_rule` WHERE `id` NOT IN ({})",
keep_id
.into_iter()
.map(|id| id.to_string())
.collect::<Vec<String>>()
.join(",")
),
[],
)?)
}
pub fn insert_rule(
tx: &Transaction,
profile_id: i64,
time: DateTime,
is_enabled: bool,
priority: i32,
request: String,
url: String,
) -> Result<i64> {
tx.execute(
"INSERT INTO `profile_proxy_rule` (
`profile_id`,
`time`,
`is_enabled`,
`priority`,
`request`,
`url`
) VALUES (?, ?, ?, ?, ?, ?)",
(
profile_id,
time.to_unix(),
is_enabled,
priority,
request,
url,
),
)?;
Ok(tx.last_insert_rowid())
}
pub fn update_rule(
tx: &Transaction,
id: i64,
time: DateTime,
is_enabled: bool,
priority: i32,
request: String,
url: String,
) -> Result<usize> {
Ok(tx.execute(
"UPDATE `profile_proxy_rule`
SET `time` = ?,
`is_enabled` = ?,
`priority` = ?,
`request` = ?,
`url` = ?
WHERE `id` = ?",
(time.to_unix(), is_enabled, priority, request, url, id),
)?)
}
pub fn ignores(tx: &Transaction, profile_id: i64) -> Result<Vec<Ignore>> {
let mut stmt = tx.prepare(
"SELECT `id`,
@ -125,11 +231,11 @@ pub fn rules(tx: &Transaction, profile_id: i64) -> Result<Vec<Rule>> {
let result = stmt.query_map([profile_id], |row| {
Ok(Rule {
//id: row.get(0)?,
id: row.get(0)?,
//profile_id: row.get(1)?,
//time: DateTime::from_unix_local(row.get(2)?).unwrap(),
time: DateTime::from_unix_local(row.get(2)?).unwrap(),
is_enabled: row.get(3)?,
//priority: row.get(4)?,
priority: row.get(4)?,
request: row.get(5)?,
url: row.get(6)?,
})

View file

@ -1,5 +1,8 @@
pub struct Rule {
pub id: i64,
pub is_enabled: bool,
pub priority: i32,
pub request: String,
pub time: gtk::glib::DateTime,
pub url: String,
}

View file

@ -1,5 +1,9 @@
#[derive(Clone)]
pub struct Rule {
pub id: Option<i64>,
pub is_enabled: bool,
pub priority: i32,
pub request: String,
pub time: gtk::glib::DateTime,
pub url: String,
}