mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-03-31 16:45:27 +00:00
implement proxy settings dialog (rules tab)
This commit is contained in:
parent
b548fb16d3
commit
e548efc93f
12 changed files with 660 additions and 12 deletions
|
|
@ -1,16 +1,18 @@
|
|||
mod about;
|
||||
mod action;
|
||||
mod database;
|
||||
mod proxy;
|
||||
mod widget;
|
||||
pub mod window;
|
||||
|
||||
use about::About;
|
||||
use action::Action;
|
||||
use proxy::Proxy;
|
||||
use widget::Widget;
|
||||
use window::Window;
|
||||
|
||||
use crate::Profile;
|
||||
use adw::{AboutDialog, Application, prelude::AdwDialogExt};
|
||||
use adw::{AboutDialog, Application, PreferencesDialog, prelude::AdwDialogExt};
|
||||
use anyhow::Result;
|
||||
use gtk::{
|
||||
FileLauncher,
|
||||
|
|
@ -91,6 +93,12 @@ impl Browser {
|
|||
}
|
||||
});
|
||||
|
||||
action.proxy.connect_activate({
|
||||
let profile = profile.clone();
|
||||
let window = window.clone();
|
||||
move || PreferencesDialog::proxy(&profile).present(Some(&window.g_box))
|
||||
});
|
||||
|
||||
// Return new activated `Self`
|
||||
Self {
|
||||
action,
|
||||
|
|
|
|||
|
|
@ -2,11 +2,13 @@ mod about;
|
|||
mod close;
|
||||
mod debug;
|
||||
mod profile;
|
||||
mod proxy;
|
||||
|
||||
use about::About;
|
||||
use close::Close;
|
||||
use debug::Debug;
|
||||
use profile::Profile;
|
||||
use proxy::Proxy;
|
||||
|
||||
use gtk::{
|
||||
gio::SimpleActionGroup,
|
||||
|
|
@ -22,6 +24,7 @@ pub struct Action {
|
|||
pub close: Rc<Close>,
|
||||
pub debug: Rc<Debug>,
|
||||
pub profile: Rc<Profile>,
|
||||
pub proxy: Rc<Proxy>,
|
||||
// Group
|
||||
pub id: GString,
|
||||
pub simple_action_group: SimpleActionGroup,
|
||||
|
|
@ -43,6 +46,7 @@ impl Action {
|
|||
let close = Rc::new(Close::new());
|
||||
let debug = Rc::new(Debug::new());
|
||||
let profile = Rc::new(Profile::new());
|
||||
let proxy = Rc::new(Proxy::new());
|
||||
|
||||
// Generate unique group ID
|
||||
let id = uuid_string_random();
|
||||
|
|
@ -55,6 +59,7 @@ impl Action {
|
|||
simple_action_group.add_action(&close.simple_action);
|
||||
simple_action_group.add_action(&debug.simple_action);
|
||||
simple_action_group.add_action(&profile.simple_action);
|
||||
simple_action_group.add_action(&proxy.simple_action);
|
||||
|
||||
// Done
|
||||
Self {
|
||||
|
|
@ -62,6 +67,7 @@ impl Action {
|
|||
close,
|
||||
debug,
|
||||
profile,
|
||||
proxy,
|
||||
id,
|
||||
simple_action_group,
|
||||
}
|
||||
|
|
|
|||
31
src/app/browser/action/proxy.rs
Normal file
31
src/app/browser/action/proxy.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
use gtk::{gio::SimpleAction, glib::uuid_string_random};
|
||||
|
||||
/// [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) wrapper for `Profile` action of `Browser` group
|
||||
pub struct Proxy {
|
||||
pub simple_action: SimpleAction,
|
||||
}
|
||||
|
||||
impl Default for Proxy {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Proxy {
|
||||
// Constructors
|
||||
|
||||
/// Create new `Self`
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
simple_action: SimpleAction::new(&uuid_string_random(), None),
|
||||
}
|
||||
}
|
||||
|
||||
// Events
|
||||
|
||||
/// Define callback function for
|
||||
/// [SimpleAction::activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal
|
||||
pub fn connect_activate(&self, callback: impl Fn() + 'static) {
|
||||
self.simple_action.connect_activate(move |_, _| callback());
|
||||
}
|
||||
}
|
||||
80
src/app/browser/proxy.rs
Normal file
80
src/app/browser/proxy.rs
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
//! Proxy settings dialog
|
||||
|
||||
mod rules;
|
||||
|
||||
use super::Profile;
|
||||
use adw::{
|
||||
PreferencesGroup, PreferencesPage,
|
||||
prelude::{AdwDialogExt, PreferencesDialogExt, PreferencesGroupExt, PreferencesPageExt},
|
||||
};
|
||||
use rules::Rules;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub trait Proxy {
|
||||
fn proxy(profile: &Rc<Profile>) -> Self;
|
||||
}
|
||||
|
||||
impl Proxy for adw::PreferencesDialog {
|
||||
fn proxy(profile: &Rc<Profile>) -> Self {
|
||||
// Init components
|
||||
let rules = Rules::build(profile);
|
||||
|
||||
// Init widget
|
||||
let d = adw::PreferencesDialog::builder()
|
||||
.search_enabled(true)
|
||||
.title("Proxy")
|
||||
.build();
|
||||
|
||||
d.add(&{
|
||||
let p = PreferencesPage::builder()
|
||||
.title("Rules")
|
||||
.icon_name("system-run-symbolic")
|
||||
.build();
|
||||
p.add(&{
|
||||
let g = PreferencesGroup::new();
|
||||
g.add(&rules.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()
|
||||
.title("Exceptions")
|
||||
.icon_name("action-unavailable-symbolic")
|
||||
.build(),
|
||||
);
|
||||
|
||||
d.add(
|
||||
&PreferencesPage::builder()
|
||||
.title("Interface")
|
||||
.icon_name("preferences-desktop-display-symbolic")
|
||||
.build(),
|
||||
);
|
||||
|
||||
d.connect_closed({
|
||||
let profile = profile.clone();
|
||||
move |_| {
|
||||
profile.proxy.clear();
|
||||
for rule in rules.take() {
|
||||
if rule.validate() {
|
||||
profile.proxy.add_rule(
|
||||
rule.id,
|
||||
rule.is_enabled(),
|
||||
rule.priority(),
|
||||
rule.request().to_string(),
|
||||
rule.url().to_string(),
|
||||
rule.time,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
d
|
||||
}
|
||||
}
|
||||
93
src/app/browser/proxy/rules.rs
Normal file
93
src/app/browser/proxy/rules.rs
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
mod rule;
|
||||
|
||||
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
||||
|
||||
use super::Profile;
|
||||
use gtk::{
|
||||
Box,
|
||||
glib::{GString, uuid_string_random},
|
||||
prelude::BoxExt,
|
||||
};
|
||||
use rule::Rule;
|
||||
|
||||
pub struct Rules {
|
||||
pub widget: Box,
|
||||
rules: Rc<RefCell<HashMap<GString, Rule>>>,
|
||||
}
|
||||
|
||||
impl Rules {
|
||||
pub fn build(profile: &Rc<Profile>) -> Self {
|
||||
let config = profile.proxy.rules();
|
||||
|
||||
let rules: Rc<RefCell<HashMap<GString, Rule>>> =
|
||||
Rc::new(RefCell::new(HashMap::with_capacity(config.len())));
|
||||
|
||||
let form = Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.spacing(8)
|
||||
.build();
|
||||
|
||||
{
|
||||
let mut r = rules.borrow_mut();
|
||||
|
||||
for proxy in config {
|
||||
let key = uuid_string_random();
|
||||
let rule = Rule::build(
|
||||
proxy.id,
|
||||
Some(&proxy.time),
|
||||
Some(&proxy.request),
|
||||
Some(&proxy.url),
|
||||
Some(proxy.priority),
|
||||
proxy.is_enabled,
|
||||
{
|
||||
let rules = rules.clone();
|
||||
let key = key.clone();
|
||||
let form = form.clone();
|
||||
move || form.remove(&rules.borrow_mut().remove(&key).unwrap().widget)
|
||||
},
|
||||
);
|
||||
rule.validate();
|
||||
form.append(&rule.widget);
|
||||
assert!(r.insert(key, rule).is_none())
|
||||
}
|
||||
}
|
||||
|
||||
let add = {
|
||||
let b = Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.spacing(8)
|
||||
.build();
|
||||
|
||||
b.append(&rule::new({
|
||||
let rules = rules.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 key = key.clone();
|
||||
let form = form.clone();
|
||||
move || form.remove(&rules.borrow_mut().remove(&key).unwrap().widget)
|
||||
});
|
||||
form.append(&rule.widget);
|
||||
assert!(rules.borrow_mut().insert(key, rule).is_none())
|
||||
}
|
||||
}));
|
||||
b
|
||||
};
|
||||
|
||||
let widget = Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.spacing(8)
|
||||
.build();
|
||||
|
||||
widget.append(&form);
|
||||
widget.append(&add);
|
||||
|
||||
Self { rules, widget }
|
||||
}
|
||||
|
||||
pub fn take(&self) -> Vec<Rule> {
|
||||
self.rules.take().into_values().collect()
|
||||
}
|
||||
}
|
||||
250
src/app/browser/proxy/rules/rule.rs
Normal file
250
src/app/browser/proxy/rules/rule.rs
Normal file
|
|
@ -0,0 +1,250 @@
|
|||
use gtk::{
|
||||
Align, Box, Button, Entry, Switch,
|
||||
glib::{DateTime, GString},
|
||||
prelude::{BoxExt, ButtonExt, EditableExt, WidgetExt},
|
||||
};
|
||||
|
||||
pub struct Rule {
|
||||
pub id: Option<i64>,
|
||||
priority: Entry,
|
||||
request: Entry,
|
||||
status: Switch,
|
||||
url: Entry,
|
||||
pub time: DateTime,
|
||||
pub widget: Box,
|
||||
}
|
||||
|
||||
impl Rule {
|
||||
// Constructors
|
||||
|
||||
pub fn build(
|
||||
id: Option<i64>,
|
||||
time: Option<&DateTime>,
|
||||
request: Option<&str>,
|
||||
url: Option<&str>,
|
||||
priority: Option<i32>,
|
||||
is_enabled: bool,
|
||||
on_delete: impl Fn() + 'static,
|
||||
) -> Self {
|
||||
// Init components
|
||||
|
||||
let status = Switch::builder()
|
||||
.active(is_enabled)
|
||||
.valign(Align::Center)
|
||||
.build();
|
||||
|
||||
let request = Entry::builder()
|
||||
.max_width_chars(12)
|
||||
.placeholder_text("Request")
|
||||
.tooltip_text("Supports regex expressions")
|
||||
.text(request.unwrap_or(".*"))
|
||||
.build();
|
||||
|
||||
let url = Entry::builder()
|
||||
.hexpand(true)
|
||||
.placeholder_text("Proxy URL")
|
||||
.text(url.unwrap_or_default())
|
||||
.tooltip_text("e.g. socks5://127.0.0.1:1080")
|
||||
.build();
|
||||
|
||||
let priority = Entry::builder()
|
||||
.max_width_chars(1)
|
||||
.placeholder_text("Priority")
|
||||
.text(priority.unwrap_or(0).to_string())
|
||||
.tooltip_text("Apply in priority")
|
||||
.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(&request);
|
||||
widget.append(&url);
|
||||
widget.append(&priority);
|
||||
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 rule?")
|
||||
.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))
|
||||
}
|
||||
});
|
||||
|
||||
priority.connect_changed({
|
||||
let url = url.clone();
|
||||
move |this| {
|
||||
validate(this, &url);
|
||||
}
|
||||
});
|
||||
|
||||
url.connect_changed({
|
||||
let priority = priority.clone();
|
||||
move |this| {
|
||||
validate(&priority, this);
|
||||
}
|
||||
});
|
||||
|
||||
status.connect_state_set({
|
||||
let priority = priority.clone();
|
||||
let request = request.clone();
|
||||
let url = url.clone();
|
||||
move |_, state| {
|
||||
validate(&priority, &url);
|
||||
|
||||
priority.set_sensitive(state);
|
||||
request.set_sensitive(state);
|
||||
url.set_sensitive(state);
|
||||
|
||||
gtk::glib::Propagation::Proceed
|
||||
}
|
||||
});
|
||||
|
||||
Self {
|
||||
id,
|
||||
priority,
|
||||
request,
|
||||
status,
|
||||
time: time.cloned().unwrap_or(DateTime::now_local().unwrap()),
|
||||
url,
|
||||
widget,
|
||||
}
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
pub fn validate(&self) -> bool {
|
||||
validate(&self.priority, &self.url)
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
pub fn priority(&self) -> i32 {
|
||||
self.priority.text().parse::<i32>().unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn request(&self) -> GString {
|
||||
self.request.text()
|
||||
}
|
||||
|
||||
pub fn url(&self) -> GString {
|
||||
self.url.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(priority: &Entry, url: &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_priority(value: &str) -> Result<(), String> {
|
||||
if value.parse::<i32>().is_err() {
|
||||
Err("Priority value is not valid integer".to_string())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_url(value: &str) -> Result<(), String> {
|
||||
match gtk::glib::Uri::parse(value, gtk::glib::UriFlags::NONE) {
|
||||
Ok(uri) => {
|
||||
if uri.scheme().is_empty() {
|
||||
Err("Scheme is empty".to_string())
|
||||
} else if uri.host().is_none() {
|
||||
Err("Host is required".to_string())
|
||||
} else if uri.port() == -1 {
|
||||
Err("Port is required".to_string())
|
||||
} else if !uri.path().is_empty() {
|
||||
Err("URL should not contain the path part".to_string())
|
||||
} else if uri.query().is_some() {
|
||||
Err("URL should not contain the query part".to_string())
|
||||
} else if uri.fragment().is_some() {
|
||||
Err("URL should not contain the fragment (anchor) part".to_string())
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
let v = validate_priority(&priority.text());
|
||||
let is_valid_priority = v.is_ok();
|
||||
highlight(priority, v);
|
||||
|
||||
let v = validate_url(&url.text());
|
||||
let is_valid_url = v.is_ok();
|
||||
highlight(url, v);
|
||||
|
||||
is_valid_priority && is_valid_url
|
||||
}
|
||||
|
|
@ -160,6 +160,13 @@ impl Menu for MenuButton {
|
|||
|
||||
main.append_submenu(Some("History"), &main_history);
|
||||
|
||||
// Main > Proxy
|
||||
main.append(Some("Proxy settings"), Some(&format!(
|
||||
"{}.{}",
|
||||
browser_action.id,
|
||||
browser_action.proxy.simple_action.name()
|
||||
))); // @TODO make the Settings submenu
|
||||
|
||||
// Main > Tool
|
||||
let main_tool = gio::Menu::new();
|
||||
|
||||
|
|
@ -170,7 +177,7 @@ impl Menu for MenuButton {
|
|||
browser_action.debug.simple_action.name()
|
||||
)));
|
||||
|
||||
main_tool.append(Some("Profile"), Some(&format!(
|
||||
main_tool.append(Some("Open profile directory"), Some(&format!(
|
||||
"{}.{}",
|
||||
browser_action.id,
|
||||
browser_action.profile.simple_action.name()
|
||||
|
|
@ -200,7 +207,7 @@ impl Menu for MenuButton {
|
|||
.build();
|
||||
|
||||
// Generate dynamical menu items
|
||||
menu_button.set_create_popup_func({
|
||||
menu_button.set_create_popup_func({
|
||||
let profile = profile.clone();
|
||||
let main_bookmarks = main_bookmarks.clone();
|
||||
let window_action = window_action.clone();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue