| 1 |
// Fragments - add_connection_dialog.rs |
| •2 |
// Copyright (C) 2022-2025 Felix Häcker <[email protected]> |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify |
| 5 |
// it under the terms of the GNU General Public License as published by |
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
| 7 |
// (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU General Public License |
| 15 |
// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
use std::cell::{Cell, RefCell}; |
| 18 |
|
| 19 |
use adw::prelude::*; |
| 20 |
use adw::subclass::prelude::*; |
| 21 |
use glib::{clone, subclass}; |
| 22 |
use gtk::{glib, CompositeTemplate}; |
| 23 |
use transmission_gobject::{ClientError, TrClient}; |
| 24 |
|
| 25 |
use crate::app::FrgApplication; |
| 26 |
use crate::backend::{FrgConnection, FrgConnectionManager}; |
| 27 |
use crate::i18n::i18n_f; |
| 28 |
|
| 29 |
mod imp { |
| 30 |
use super::*; |
| 31 |
|
| 32 |
#[derive(Debug, Default, CompositeTemplate)] |
| 33 |
#[template(resource = "/de/haeckerfelix/Fragments/gtk/add_connection_dialog.ui")] |
| 34 |
pub struct FrgAddConnectionDialog { |
| 35 |
#[template_child] |
| 36 |
stack: TemplateChild<gtk::Stack>, |
| 37 |
#[template_child] |
| 38 |
title_row: TemplateChild<adw::EntryRow>, |
| 39 |
#[template_child] |
| 40 |
host_row: TemplateChild<adw::EntryRow>, |
| 41 |
#[template_child] |
| 42 |
port_spinbutton: TemplateChild<adw::SpinRow>, |
| 43 |
#[template_child] |
| 44 |
path_row: TemplateChild<adw::EntryRow>, |
| 45 |
#[template_child] |
| 46 |
ssl_switch: TemplateChild<adw::SwitchRow>, |
| 47 |
#[template_child] |
| 48 |
connect_button: TemplateChild<gtk::Button>, |
| 49 |
#[template_child] |
| 50 |
error_label: TemplateChild<gtk::Label>, |
| 51 |
|
| 52 |
title_ok: Cell<bool>, |
| 53 |
address_ok: Cell<bool>, |
| 54 |
address: RefCell<String>, |
| 55 |
} |
| 56 |
|
| 57 |
#[glib::object_subclass] |
| 58 |
impl ObjectSubclass for FrgAddConnectionDialog { |
| 59 |
const NAME: &'static str = "FrgAddConnectionDialog"; |
| 60 |
type ParentType = adw::Dialog; |
| 61 |
type Type = super::FrgAddConnectionDialog; |
| 62 |
|
| 63 |
fn class_init(klass: &mut Self::Class) { |
| 64 |
klass.bind_template(); |
| 65 |
klass.bind_template_callbacks(); |
| 66 |
} |
| 67 |
|
| 68 |
fn instance_init(obj: &subclass::InitializingObject<Self>) { |
| 69 |
obj.init_template(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
impl ObjectImpl for FrgAddConnectionDialog {} |
| 74 |
|
| 75 |
impl WidgetImpl for FrgAddConnectionDialog {} |
| 76 |
|
| 77 |
impl AdwDialogImpl for FrgAddConnectionDialog {} |
| 78 |
|
| 79 |
#[gtk::template_callbacks] |
| 80 |
impl FrgAddConnectionDialog { |
| 81 |
fn add_connection(&self, connection: FrgConnection) { |
| 82 |
let cm = FrgConnectionManager::default(); |
| 83 |
let app = FrgApplication::default(); |
| 84 |
|
| 85 |
cm.connections().append(&connection); |
| 86 |
app.activate_action("set-connection", Some(&connection.uuid().to_variant())); |
| 87 |
self.obj().close(); |
| 88 |
} |
| 89 |
|
| 90 |
#[template_callback] |
| 91 |
fn connect_button_clicked(&self) { |
| •92 |
let fut = clone!( |
| 93 |
#[weak(rename_to = this)] |
| 94 |
self, |
| 95 |
async move { |
| 96 |
let title = this.title_row.text(); |
| 97 |
let address = this.address.borrow().clone(); |
| 98 |
|
| 99 |
let connection = FrgConnection::new(&title, &address); |
| 100 |
|
| 101 |
this.stack.set_visible_child_name("loading"); |
| 102 |
this.connect_button.set_sensitive(false); |
| 103 |
|
| 104 |
let res = TrClient::test_connectivity(connection.address(), None).await; |
| 105 |
match res { |
| 106 |
Ok(_) => this.add_connection(connection), |
| 107 |
Err(ref err) => { |
| 108 |
// Skip "Unauthorized" errors since we can handle those |
| 109 |
if matches!(err, ClientError::TransmissionUnauthorized) { |
| 110 |
this.add_connection(connection); |
| 111 |
} else { |
| 112 |
let msg = i18n_f( |
| 113 |
"Could not connect with “{}”:\n{}", |
| 114 |
&[&this.address.borrow(), &err.to_string()], |
| 115 |
); |
| 116 |
|
| 117 |
this.error_label.set_visible(true); |
| 118 |
this.error_label.set_text(&msg); |
| 119 |
|
| 120 |
this.stack.set_visible_child_name("input"); |
| 121 |
this.connect_button.set_sensitive(true); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| •126 |
); |
| 127 |
glib::spawn_future_local(fut); |
| 128 |
} |
| 129 |
|
| 130 |
#[template_callback] |
| 131 |
fn validate_title(&self) { |
| 132 |
if self.title_row.text().is_empty() { |
| 133 |
self.title_row.add_css_class("error"); |
| 134 |
self.title_ok.set(false); |
| 135 |
} else { |
| 136 |
self.title_row.remove_css_class("error"); |
| 137 |
self.title_ok.set(true); |
| 138 |
} |
| 139 |
|
| 140 |
self.update_connect_button(); |
| 141 |
} |
| 142 |
|
| 143 |
#[template_callback] |
| 144 |
fn validate_address(&self) { |
| 145 |
let host = self.host_row.text(); |
| 146 |
let port = self.port_spinbutton.value(); |
| 147 |
let path = self.path_row.text(); |
| 148 |
let is_ssl = self.ssl_switch.is_active(); |
| 149 |
|
| 150 |
let address = if is_ssl { |
| 151 |
format!("https://{host}:{port}{path}") |
| 152 |
} else { |
| 153 |
format!("http://{host}:{port}{path}") |
| 154 |
}; |
| 155 |
*self.address.borrow_mut() = address.to_string(); |
| 156 |
|
| 157 |
let valid_address = url::Url::parse(&address).is_ok(); |
| 158 |
|
| 159 |
if !self.host_row.text().is_empty() && valid_address { |
| 160 |
self.host_row.remove_css_class("error"); |
| 161 |
self.address_ok.set(true); |
| 162 |
} else { |
| 163 |
// Don't recolor entry if it is empty |
| 164 |
if !self.host_row.text().is_empty() { |
| 165 |
self.host_row.add_css_class("error"); |
| 166 |
} else { |
| 167 |
self.host_row.remove_css_class("error"); |
| 168 |
} |
| 169 |
|
| 170 |
self.address_ok.set(false); |
| 171 |
} |
| 172 |
|
| 173 |
self.update_connect_button(); |
| 174 |
} |
| 175 |
|
| 176 |
fn update_connect_button(&self) { |
| 177 |
let sensitive = self.title_ok.get() && self.address_ok.get(); |
| 178 |
self.connect_button.set_sensitive(sensitive); |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
glib::wrapper! { |
| 184 |
pub struct FrgAddConnectionDialog(ObjectSubclass<imp::FrgAddConnectionDialog>) |
| •185 |
@extends gtk::Widget, adw::Dialog, |
| 186 |
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget; |
| 187 |
} |
| 188 |
|
| 189 |
impl FrgAddConnectionDialog { |
| 190 |
pub fn new() -> Self { |
| 191 |
Self::default() |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
impl Default for FrgAddConnectionDialog { |
| 196 |
fn default() -> Self { |
| 197 |
glib::Object::new() |
| 198 |
} |
| 199 |
} |