mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 17:15:28 +00:00
implement proxy exceptions tab
This commit is contained in:
parent
b15e4a5fc1
commit
a7c6e79aac
13 changed files with 462 additions and 72 deletions
|
|
@ -1,13 +1,15 @@
|
|||
//! Proxy settings dialog
|
||||
|
||||
mod rules;
|
||||
mod ignore;
|
||||
mod rule;
|
||||
|
||||
use super::Profile;
|
||||
use adw::{
|
||||
PreferencesGroup, PreferencesPage,
|
||||
prelude::{AdwDialogExt, PreferencesDialogExt, PreferencesGroupExt, PreferencesPageExt},
|
||||
};
|
||||
use rules::Rules;
|
||||
use ignore::Ignore;
|
||||
use rule::Rule;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub trait Proxy {
|
||||
|
|
@ -17,7 +19,8 @@ pub trait Proxy {
|
|||
impl Proxy for adw::PreferencesDialog {
|
||||
fn proxy(profile: &Rc<Profile>) -> Self {
|
||||
// Init components
|
||||
let rules = Rules::build(profile);
|
||||
let ignore = Ignore::build(profile);
|
||||
let rule = Rule::build(profile);
|
||||
|
||||
// Init widget
|
||||
let d = adw::PreferencesDialog::builder()
|
||||
|
|
@ -32,23 +35,24 @@ impl Proxy for adw::PreferencesDialog {
|
|||
.build();
|
||||
p.add(&{
|
||||
let g = PreferencesGroup::new();
|
||||
g.add(&rules.widget);
|
||||
g.add(&rule.widget);
|
||||
g
|
||||
});
|
||||
/* @TODO URL entry p.add(&{
|
||||
let g = PreferencesGroup::builder().title("Test").build();
|
||||
//g.add(&Box::rules(profile));
|
||||
g
|
||||
});*/
|
||||
p
|
||||
});
|
||||
|
||||
d.add(
|
||||
&PreferencesPage::builder()
|
||||
d.add(&{
|
||||
let p = PreferencesPage::builder()
|
||||
.title("Exceptions")
|
||||
.icon_name("action-unavailable-symbolic")
|
||||
.build(),
|
||||
);
|
||||
.build();
|
||||
p.add(&{
|
||||
let g = PreferencesGroup::new();
|
||||
g.add(&ignore.widget);
|
||||
g
|
||||
});
|
||||
p
|
||||
});
|
||||
|
||||
d.add(
|
||||
&PreferencesPage::builder()
|
||||
|
|
@ -61,18 +65,27 @@ impl Proxy for adw::PreferencesDialog {
|
|||
let profile = profile.clone();
|
||||
move |_| {
|
||||
profile.proxy.rule.clear();
|
||||
for rule in rules.take() {
|
||||
if rule.validate() {
|
||||
for r in rule.take() {
|
||||
if r.validate() {
|
||||
profile.proxy.rule.add(
|
||||
rule.id,
|
||||
rule.is_enabled(),
|
||||
rule.priority(),
|
||||
rule.request().to_string(),
|
||||
rule.url().to_string(),
|
||||
rule.time,
|
||||
r.id,
|
||||
r.is_enabled(),
|
||||
r.priority(),
|
||||
r.request().to_string(),
|
||||
r.url().to_string(),
|
||||
r.time,
|
||||
)
|
||||
}
|
||||
}
|
||||
profile.proxy.ignore.clear();
|
||||
for i in ignore.take() {
|
||||
if i.validate() {
|
||||
profile
|
||||
.proxy
|
||||
.ignore
|
||||
.add(i.id, i.is_enabled(), i.host().to_string(), i.time)
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
d
|
||||
|
|
|
|||
91
src/app/browser/proxy/ignore.rs
Normal file
91
src/app/browser/proxy/ignore.rs
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
mod row;
|
||||
|
||||
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
||||
|
||||
use super::Profile;
|
||||
use gtk::{
|
||||
Box,
|
||||
glib::{GString, uuid_string_random},
|
||||
prelude::BoxExt,
|
||||
};
|
||||
use row::Row;
|
||||
|
||||
pub struct Ignore {
|
||||
pub widget: Box,
|
||||
rows: Rc<RefCell<HashMap<GString, Row>>>,
|
||||
}
|
||||
|
||||
impl Ignore {
|
||||
pub fn build(profile: &Rc<Profile>) -> Self {
|
||||
let config = profile.proxy.ignore.all();
|
||||
|
||||
let rows: Rc<RefCell<HashMap<GString, Row>>> =
|
||||
Rc::new(RefCell::new(HashMap::with_capacity(config.len())));
|
||||
|
||||
let form = Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.spacing(8)
|
||||
.build();
|
||||
|
||||
{
|
||||
let mut r = rows.borrow_mut();
|
||||
|
||||
for proxy in config {
|
||||
let key = uuid_string_random();
|
||||
let row = Row::build(
|
||||
proxy.id,
|
||||
Some(&proxy.time),
|
||||
Some(&proxy.host),
|
||||
proxy.is_enabled,
|
||||
{
|
||||
let form = form.clone();
|
||||
let key = key.clone();
|
||||
let rows = rows.clone();
|
||||
move || form.remove(&rows.borrow_mut().remove(&key).unwrap().widget)
|
||||
},
|
||||
);
|
||||
row.validate();
|
||||
form.append(&row.widget);
|
||||
assert!(r.insert(key, row).is_none())
|
||||
}
|
||||
}
|
||||
|
||||
let add = {
|
||||
let b = Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.spacing(8)
|
||||
.build();
|
||||
|
||||
b.append(&row::new({
|
||||
let form = form.clone();
|
||||
let rows = rows.clone();
|
||||
move || {
|
||||
let key = uuid_string_random();
|
||||
let row = Row::build(None, None, None, false, {
|
||||
let rows = rows.clone();
|
||||
let key = key.clone();
|
||||
let form = form.clone();
|
||||
move || form.remove(&rows.borrow_mut().remove(&key).unwrap().widget)
|
||||
});
|
||||
form.append(&row.widget);
|
||||
assert!(rows.borrow_mut().insert(key, row).is_none())
|
||||
}
|
||||
}));
|
||||
b
|
||||
};
|
||||
|
||||
let widget = Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.spacing(8)
|
||||
.build();
|
||||
|
||||
widget.append(&form);
|
||||
widget.append(&add);
|
||||
|
||||
Self { rows, widget }
|
||||
}
|
||||
|
||||
pub fn take(&self) -> Vec<Row> {
|
||||
self.rows.take().into_values().collect()
|
||||
}
|
||||
}
|
||||
183
src/app/browser/proxy/ignore/row.rs
Normal file
183
src/app/browser/proxy/ignore/row.rs
Normal file
|
|
@ -0,0 +1,183 @@
|
|||
use gtk::{
|
||||
Align, Box, Button, Entry, Switch,
|
||||
glib::{DateTime, GString},
|
||||
prelude::{BoxExt, ButtonExt, EditableExt, WidgetExt},
|
||||
};
|
||||
|
||||
pub struct Row {
|
||||
pub id: Option<i64>,
|
||||
host: Entry,
|
||||
status: Switch,
|
||||
pub time: DateTime,
|
||||
pub widget: Box,
|
||||
}
|
||||
|
||||
impl Row {
|
||||
// Constructors
|
||||
|
||||
pub fn build(
|
||||
id: Option<i64>,
|
||||
time: Option<&DateTime>,
|
||||
host: Option<&str>,
|
||||
is_enabled: bool,
|
||||
on_delete: impl Fn() + 'static,
|
||||
) -> Self {
|
||||
// Init components
|
||||
|
||||
let status = Switch::builder()
|
||||
.active(is_enabled)
|
||||
.valign(Align::Center)
|
||||
.build();
|
||||
|
||||
let host = Entry::builder()
|
||||
.hexpand(true)
|
||||
.placeholder_text("Host")
|
||||
.text(host.unwrap_or_default())
|
||||
.build();
|
||||
|
||||
let delete = Button::builder()
|
||||
.css_classes(["error"])
|
||||
.icon_name("user-trash-symbolic")
|
||||
.tooltip_text("Delete")
|
||||
.build();
|
||||
|
||||
// Init widget
|
||||
|
||||
let widget = Box::builder()
|
||||
.orientation(gtk::Orientation::Horizontal)
|
||||
.spacing(8)
|
||||
.build();
|
||||
|
||||
widget.append(&status);
|
||||
widget.append(&host);
|
||||
widget.append(&delete);
|
||||
|
||||
// Activate
|
||||
|
||||
delete.connect_clicked({
|
||||
let c = std::rc::Rc::new(on_delete);
|
||||
move |this| {
|
||||
use adw::{
|
||||
AlertDialog, ResponseAppearance,
|
||||
prelude::{AdwDialogExt, AlertDialogExt, AlertDialogExtManual},
|
||||
};
|
||||
|
||||
const RESPONSE_CONFIRM: (&str, &str) = ("confirm", "Confirm");
|
||||
const RESPONSE_CANCEL: (&str, &str) = ("cancel", "Cancel");
|
||||
|
||||
let dialog = AlertDialog::builder()
|
||||
.heading("Delete this host?")
|
||||
.close_response(RESPONSE_CANCEL.0)
|
||||
.default_response(RESPONSE_CONFIRM.0)
|
||||
.build();
|
||||
|
||||
dialog.add_responses(&[RESPONSE_CANCEL, RESPONSE_CONFIRM]);
|
||||
dialog.set_response_appearance(RESPONSE_CONFIRM.0, ResponseAppearance::Destructive);
|
||||
dialog.connect_response(None, {
|
||||
let c = c.clone();
|
||||
move |dialog, response| {
|
||||
dialog.set_response_enabled(response, false); // prevent double-click
|
||||
if response == RESPONSE_CONFIRM.0 {
|
||||
c()
|
||||
}
|
||||
}
|
||||
});
|
||||
dialog.present(Some(this))
|
||||
}
|
||||
});
|
||||
|
||||
host.connect_changed(move |this| {
|
||||
validate(this);
|
||||
});
|
||||
|
||||
status.connect_state_set({
|
||||
let host = host.clone();
|
||||
move |_, state| {
|
||||
validate(&host);
|
||||
|
||||
host.set_sensitive(state);
|
||||
|
||||
gtk::glib::Propagation::Proceed
|
||||
}
|
||||
});
|
||||
|
||||
Self {
|
||||
id,
|
||||
status,
|
||||
time: time.cloned().unwrap_or(DateTime::now_local().unwrap()),
|
||||
host,
|
||||
widget,
|
||||
}
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
pub fn validate(&self) -> bool {
|
||||
validate(&self.host)
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
pub fn host(&self) -> GString {
|
||||
self.host.text()
|
||||
}
|
||||
|
||||
pub fn is_enabled(&self) -> bool {
|
||||
self.status.is_active()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(on_add: impl Fn() + 'static) -> Box {
|
||||
let b = Box::builder()
|
||||
.orientation(gtk::Orientation::Horizontal)
|
||||
.spacing(8)
|
||||
.build();
|
||||
|
||||
b.append(&{
|
||||
let add = Button::builder()
|
||||
.css_classes(["success"])
|
||||
.hexpand(true)
|
||||
.icon_name("list-add-symbolic")
|
||||
.tooltip_text("Add proxy")
|
||||
.build();
|
||||
|
||||
add.connect_clicked(move |_| on_add());
|
||||
add
|
||||
});
|
||||
b
|
||||
}
|
||||
|
||||
fn validate(host: &Entry) -> bool {
|
||||
fn highlight(entry: &Entry, error: Result<(), String>) {
|
||||
const E: &str = "error";
|
||||
match error {
|
||||
Err(e) => {
|
||||
entry.set_css_classes(&[E]);
|
||||
entry.set_tooltip_text(Some(&e))
|
||||
}
|
||||
Ok(()) => {
|
||||
entry.remove_css_class(E);
|
||||
entry.set_tooltip_text(Some("Value is valid"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_host(value: &str) -> Result<(), String> {
|
||||
match gtk::gio::InetAddress::from_string(value) {
|
||||
Some(address) => {
|
||||
if address.to_string() != value {
|
||||
Err("Host could not be parsed properly".to_string())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
None => Err("Valid host is required".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
let v = validate_host(&host.text());
|
||||
let is_valid_host = v.is_ok();
|
||||
highlight(host, v);
|
||||
|
||||
is_valid_host
|
||||
}
|
||||
|
|
@ -1,6 +1,4 @@
|
|||
mod rule;
|
||||
|
||||
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
||||
mod row;
|
||||
|
||||
use super::Profile;
|
||||
use gtk::{
|
||||
|
|
@ -8,18 +6,19 @@ use gtk::{
|
|||
glib::{GString, uuid_string_random},
|
||||
prelude::BoxExt,
|
||||
};
|
||||
use rule::Rule;
|
||||
use row::Row;
|
||||
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
||||
|
||||
pub struct Rules {
|
||||
pub struct Rule {
|
||||
pub widget: Box,
|
||||
rules: Rc<RefCell<HashMap<GString, Rule>>>,
|
||||
rows: Rc<RefCell<HashMap<GString, Row>>>,
|
||||
}
|
||||
|
||||
impl Rules {
|
||||
impl Rule {
|
||||
pub fn build(profile: &Rc<Profile>) -> Self {
|
||||
let config = profile.proxy.rule.all();
|
||||
|
||||
let rules: Rc<RefCell<HashMap<GString, Rule>>> =
|
||||
let rows: Rc<RefCell<HashMap<GString, Row>>> =
|
||||
Rc::new(RefCell::new(HashMap::with_capacity(config.len())));
|
||||
|
||||
let form = Box::builder()
|
||||
|
|
@ -28,11 +27,11 @@ impl Rules {
|
|||
.build();
|
||||
|
||||
{
|
||||
let mut r = rules.borrow_mut();
|
||||
let mut r = rows.borrow_mut();
|
||||
|
||||
for proxy in config {
|
||||
let key = uuid_string_random();
|
||||
let rule = Rule::build(
|
||||
let rule = Row::build(
|
||||
proxy.id,
|
||||
Some(&proxy.time),
|
||||
Some(&proxy.request),
|
||||
|
|
@ -40,10 +39,10 @@ impl Rules {
|
|||
Some(proxy.priority),
|
||||
proxy.is_enabled,
|
||||
{
|
||||
let rules = rules.clone();
|
||||
let rows = rows.clone();
|
||||
let key = key.clone();
|
||||
let form = form.clone();
|
||||
move || form.remove(&rules.borrow_mut().remove(&key).unwrap().widget)
|
||||
move || form.remove(&rows.borrow_mut().remove(&key).unwrap().widget)
|
||||
},
|
||||
);
|
||||
rule.validate();
|
||||
|
|
@ -58,19 +57,19 @@ impl Rules {
|
|||
.spacing(8)
|
||||
.build();
|
||||
|
||||
b.append(&rule::new({
|
||||
let rules = rules.clone();
|
||||
b.append(&row::new({
|
||||
let rows = rows.clone();
|
||||
let form = form.clone();
|
||||
move || {
|
||||
let key = uuid_string_random();
|
||||
let rule = Rule::build(None, None, None, None, None, false, {
|
||||
let rules = rules.clone();
|
||||
let row = Row::build(None, None, None, None, None, false, {
|
||||
let rows = rows.clone();
|
||||
let key = key.clone();
|
||||
let form = form.clone();
|
||||
move || form.remove(&rules.borrow_mut().remove(&key).unwrap().widget)
|
||||
move || form.remove(&rows.borrow_mut().remove(&key).unwrap().widget)
|
||||
});
|
||||
form.append(&rule.widget);
|
||||
assert!(rules.borrow_mut().insert(key, rule).is_none())
|
||||
form.append(&row.widget);
|
||||
assert!(rows.borrow_mut().insert(key, row).is_none())
|
||||
}
|
||||
}));
|
||||
b
|
||||
|
|
@ -84,10 +83,10 @@ impl Rules {
|
|||
widget.append(&form);
|
||||
widget.append(&add);
|
||||
|
||||
Self { rules, widget }
|
||||
Self { rows, widget }
|
||||
}
|
||||
|
||||
pub fn take(&self) -> Vec<Rule> {
|
||||
self.rules.take().into_values().collect()
|
||||
pub fn take(&self) -> Vec<Row> {
|
||||
self.rows.take().into_values().collect()
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@ use gtk::{
|
|||
prelude::{BoxExt, ButtonExt, EditableExt, WidgetExt},
|
||||
};
|
||||
|
||||
pub struct Rule {
|
||||
pub struct Row {
|
||||
pub id: Option<i64>,
|
||||
priority: Entry,
|
||||
request: Entry,
|
||||
|
|
@ -14,7 +14,7 @@ pub struct Rule {
|
|||
pub widget: Box,
|
||||
}
|
||||
|
||||
impl Rule {
|
||||
impl Row {
|
||||
// Constructors
|
||||
|
||||
pub fn build(
|
||||
|
|
@ -220,7 +220,7 @@ fn validate(priority: &Entry, url: &Entry) -> bool {
|
|||
Ok(uri) => {
|
||||
if uri.scheme().is_empty() {
|
||||
Err("Scheme is empty".to_string())
|
||||
} else if uri.host().is_none() {
|
||||
} else if uri.host().is_none_or(|h| h.is_empty()) {
|
||||
Err("Host is required".to_string())
|
||||
} else if uri.port() == -1 {
|
||||
Err("Port is required".to_string())
|
||||
Loading…
Add table
Add a link
Reference in a new issue