Debian Codegraph @

src/ui/add_connection_dialog.rs

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Fragments - add_connection_dialog.rs
2 // Copyright (C) 2022-2023 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 #[template_child]
52 spinner: TemplateChild<gtk::Spinner>,
53
54 title_ok: Cell<bool>,
55 address_ok: Cell<bool>,
56 address: RefCell<String>,
57 }
58
59 #[glib::object_subclass]
60 impl ObjectSubclass for FrgAddConnectionDialog {
61 const NAME: &'static str = "FrgAddConnectionDialog";
62 type ParentType = adw::Dialog;
63 type Type = super::FrgAddConnectionDialog;
64
65 fn class_init(klass: &mut Self::Class) {
66 klass.bind_template();
67 klass.bind_template_callbacks();
68 }
69
70 fn instance_init(obj: &subclass::InitializingObject<Self>) {
71 obj.init_template();
72 }
73 }
74
75 impl ObjectImpl for FrgAddConnectionDialog {}
76
77 impl WidgetImpl for FrgAddConnectionDialog {}
78
79 impl AdwDialogImpl for FrgAddConnectionDialog {}
80
81 #[gtk::template_callbacks]
82 impl FrgAddConnectionDialog {
83 fn add_connection(&self, connection: FrgConnection) {
84 let cm = FrgConnectionManager::default();
85 let app = FrgApplication::default();
86
87 cm.connections().append(&connection);
88 app.activate_action("set-connection", Some(&connection.uuid().to_variant()));
89 self.obj().close();
90 }
91
92 #[template_callback]
93 fn connect_button_clicked(&self) {
94 let fut = clone!(@weak self as this => async move {
95 let title = this.title_row.text();
96 let address = this.address.borrow().clone();
97
98 let connection = FrgConnection::new(&title, &address);
99
100 this.stack.set_visible_child_name("loading");
101 this.spinner.set_spinning(true);
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("Could not connect with “{}”:\n{}", &[&this.address.borrow(), &err.to_string()]);
113
114 this.error_label.set_visible(true);
115 this.error_label.set_text(&msg);
116
117 this.stack.set_visible_child_name("input");
118 this.spinner.set_spinning(false);
119 this.connect_button.set_sensitive(true);
120 }
121 }
122 }
123 });
124 glib::spawn_future_local(fut);
125 }
126
127 #[template_callback]
128 fn validate_title(&self) {
129 if self.title_row.text().is_empty() {
130 self.title_row.add_css_class("error");
131 self.title_ok.set(false);
132 } else {
133 self.title_row.remove_css_class("error");
134 self.title_ok.set(true);
135 }
136
137 self.update_connect_button();
138 }
139
140 #[template_callback]
141 fn validate_address(&self) {
142 let host = self.host_row.text();
143 let port = self.port_spinbutton.value();
144 let path = self.path_row.text();
145 let is_ssl = self.ssl_switch.is_active();
146
147 let address = if is_ssl {
148 format!("https://{host}:{port}{path}")
149 } else {
150 format!("http://{host}:{port}{path}")
151 };
152 *self.address.borrow_mut() = address.to_string();
153
154 let valid_address = url::Url::parse(&address).is_ok();
155
156 if !self.host_row.text().is_empty() && valid_address {
157 self.host_row.remove_css_class("error");
158 self.address_ok.set(true);
159 } else {
160 // Don't recolor entry if it is empty
161 if !self.host_row.text().is_empty() {
162 self.host_row.add_css_class("error");
163 } else {
164 self.host_row.remove_css_class("error");
165 }
166
167 self.address_ok.set(false);
168 }
169
170 self.update_connect_button();
171 }
172
173 fn update_connect_button(&self) {
174 let sensitive = self.title_ok.get() && self.address_ok.get();
175 self.connect_button.set_sensitive(sensitive);
176 }
177 }
178 }
179
180 glib::wrapper! {
181 pub struct FrgAddConnectionDialog(ObjectSubclass<imp::FrgAddConnectionDialog>)
182 @extends gtk::Widget, adw::Dialog;
183 }
184
185 impl FrgAddConnectionDialog {
186 pub fn new() -> Self {
187 Self::default()
188 }
189 }
190
191 impl Default for FrgAddConnectionDialog {
192 fn default() -> Self {
193 glib::Object::new()
194 }
195 }