| 1 |
// Fragments - preferences_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 adw::prelude::*; |
| 18 |
use adw::subclass::prelude::*; |
| 19 |
use ashpd::desktop::open_uri::OpenFileRequest; |
| 20 |
use ashpd::WindowIdentifier; |
| 21 |
use glib::{clone, subclass, Properties}; |
| 22 |
use gtk::{glib, CompositeTemplate}; |
| 23 |
|
| 24 |
use crate::app::FrgApplication; |
| 25 |
use crate::backend::{FrgConnectionManager, FrgDirectoryType}; |
| 26 |
use crate::i18n::{i18n, i18n_f}; |
| 27 |
use crate::settings::{settings_manager, Key}; |
| 28 |
use crate::utils; |
| 29 |
|
| 30 |
mod imp { |
| 31 |
use super::*; |
| 32 |
|
| 33 |
#[derive(Default, Debug, CompositeTemplate, Properties)] |
| 34 |
#[template(resource = "/de/haeckerfelix/Fragments/gtk/preferences_dialog.ui")] |
| 35 |
#[properties(wrapper_type = super::FrgPreferencesDialog)] |
| 36 |
pub struct FrgPreferencesDialog { |
| 37 |
#[template_child] |
| 38 |
remote_notice_group: TemplateChild<adw::PreferencesGroup>, |
| 39 |
#[template_child] |
| 40 |
remote_notice_row: TemplateChild<adw::ActionRow>, |
| 41 |
|
| 42 |
// General page |
| 43 |
#[template_child] |
| 44 |
inhibit_suspend_active_torrents_switch: TemplateChild<gtk::Switch>, |
| 45 |
#[template_child] |
| 46 |
trash_original_torrent_files_switch: TemplateChild<gtk::Switch>, |
| 47 |
#[template_child] |
| 48 |
notifications_downloaded_switch: TemplateChild<gtk::Switch>, |
| 49 |
#[template_child] |
| 50 |
notifications_new_torrent_switch: TemplateChild<gtk::Switch>, |
| 51 |
#[template_child] |
| 52 |
remote_access_switch: TemplateChild<gtk::Switch>, |
| 53 |
#[template_child] |
| 54 |
open_webinterface_row: TemplateChild<adw::ActionRow>, |
| 55 |
|
| 56 |
// Downloading page |
| 57 |
#[template_child] |
| 58 |
download_dir_button: TemplateChild<gtk::Button>, |
| 59 |
#[template_child] |
| 60 |
download_dir_content: TemplateChild<adw::ButtonContent>, |
| 61 |
#[template_child] |
| 62 |
incomplete_dir_expander: TemplateChild<adw::ExpanderRow>, |
| 63 |
#[template_child] |
| 64 |
incomplete_dir_button: TemplateChild<gtk::Button>, |
| 65 |
#[template_child] |
| 66 |
incomplete_dir_content: TemplateChild<adw::ButtonContent>, |
| 67 |
#[template_child] |
| 68 |
download_queue_expander: TemplateChild<adw::ExpanderRow>, |
| 69 |
#[template_child] |
| 70 |
download_queue_size_spinbutton: TemplateChild<adw::SpinRow>, |
| 71 |
#[template_child] |
| 72 |
start_added_torrents_switch: TemplateChild<gtk::Switch>, |
| 73 |
|
| 74 |
// Network page |
| 75 |
#[template_child] |
| 76 |
port_spinbutton: TemplateChild<adw::SpinRow>, |
| 77 |
#[template_child] |
| 78 |
random_port_switch: TemplateChild<gtk::Switch>, |
| 79 |
#[template_child] |
| 80 |
port_forwarding_switch: TemplateChild<gtk::Switch>, |
| 81 |
#[template_child] |
| 82 |
port_test_label: TemplateChild<gtk::Label>, |
| 83 |
#[template_child] |
| 84 |
port_test_button: TemplateChild<gtk::Button>, |
| 85 |
#[template_child] |
| 86 |
max_peers_torrent_spinbutton: TemplateChild<adw::SpinRow>, |
| 87 |
#[template_child] |
| 88 |
max_peers_overall_spinbutton: TemplateChild<adw::SpinRow>, |
| 89 |
#[template_child] |
| 90 |
encryption_comborow: TemplateChild<adw::ComboRow>, |
| 91 |
|
| 92 |
#[property(get)] |
| 93 |
connection_manager: FrgConnectionManager, |
| 94 |
} |
| 95 |
|
| 96 |
#[glib::object_subclass] |
| 97 |
impl ObjectSubclass for FrgPreferencesDialog { |
| 98 |
const NAME: &'static str = "FrgPreferencesDialog"; |
| 99 |
type ParentType = adw::PreferencesDialog; |
| 100 |
type Type = super::FrgPreferencesDialog; |
| 101 |
|
| 102 |
fn class_init(klass: &mut Self::Class) { |
| 103 |
klass.bind_template(); |
| 104 |
klass.bind_template_callbacks(); |
| 105 |
} |
| 106 |
|
| 107 |
fn instance_init(obj: &subclass::InitializingObject<Self>) { |
| 108 |
obj.init_template(); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
#[glib::derived_properties] |
| 113 |
impl ObjectImpl for FrgPreferencesDialog { |
| 114 |
fn constructed(&self) { |
| 115 |
self.parent_constructed(); |
| 116 |
|
| 117 |
let client = self.connection_manager.client(); |
| 118 |
let session = client.session(); |
| 119 |
|
| 120 |
// We show/hide few widgets depending on the connection type (remote/local) |
| •121 |
self.connection_manager |
| 122 |
.connect_current_connection_notify(clone!( |
| 123 |
#[weak(rename_to = this)] |
| 124 |
self, |
| 125 |
move |_| { |
| 126 |
this.update_remote_buttons(); |
| 127 |
} |
| 128 |
)); |
| 129 |
|
| 130 |
// General page |
| 131 |
// |
| 132 |
settings_manager::bind_property( |
| 133 |
Key::InhibitSuspendActiveTorrents, |
| 134 |
&*self.inhibit_suspend_active_torrents_switch, |
| 135 |
"active", |
| 136 |
); |
| 137 |
self.inhibit_suspend_active_torrents_switch |
| 138 |
.connect_active_notify(|_| { |
| 139 |
FrgApplication::default().update_inhibit_state(); |
| 140 |
}); |
| 141 |
settings_manager::bind_property( |
| 142 |
Key::TrashOriginalTorrentFiles, |
| 143 |
&*self.trash_original_torrent_files_switch, |
| 144 |
"active", |
| 145 |
); |
| 146 |
settings_manager::bind_property( |
| 147 |
Key::EnableNotificationsNewTorrent, |
| 148 |
&*self.notifications_new_torrent_switch, |
| 149 |
"active", |
| 150 |
); |
| 151 |
settings_manager::bind_property( |
| 152 |
Key::EnableNotificationsDownloaded, |
| 153 |
&*self.notifications_downloaded_switch, |
| 154 |
"active", |
| 155 |
); |
| 156 |
settings_manager::bind_property( |
| 157 |
Key::RemoteAccess, |
| 158 |
&*self.remote_access_switch, |
| 159 |
"active", |
| 160 |
); |
| •161 |
self.remote_access_switch.connect_state_set(clone!( |
| 162 |
#[weak(rename_to = this)] |
| 163 |
self, |
| 164 |
#[upgrade_or_panic] |
| 165 |
move |switch, _| { |
| 166 |
settings_manager::set_boolean(Key::RemoteAccess, switch.is_active()); |
| 167 |
switch.set_sensitive(false); |
| 168 |
|
| 169 |
let fut = clone!( |
| 170 |
#[weak] |
| 171 |
switch, |
| 172 |
#[weak] |
| 173 |
this, |
| 174 |
async move { |
| 175 |
if let Err(err) = FrgConnectionManager::default().reconnect().await { |
| 176 |
utils::inapp_notification( |
| 177 |
&i18n("Unable to restart Transmission daemon"), |
| 178 |
Some(this.obj().upcast_ref::<gtk::Widget>()), |
| 179 |
); |
| 180 |
warn!("Unable to restart Transmission daemon: {}", err.to_string()); |
| 181 |
} |
| 182 |
|
| 183 |
switch.set_sensitive(true); |
| 184 |
} |
| 185 |
); |
| 186 |
glib::spawn_future_local(fut); |
| 187 |
glib::Propagation::Proceed |
| 188 |
} |
| 189 |
)); |
| 190 |
self.open_webinterface_row.connect_activated(clone!( |
| 191 |
#[weak(rename_to = this)] |
| 192 |
self, |
| 193 |
move |_| { |
| 194 |
let fut = async move { |
| •195 |
let uri = ashpd::Uri::parse("http://127.0.0.1:9091/").unwrap(); |
| 196 |
let native = this.obj().native().unwrap(); |
| 197 |
let identifier = WindowIdentifier::from_native(&native).await; |
| 198 |
if let Err(err) = OpenFileRequest::default() |
| 199 |
.identifier(identifier) |
| 200 |
.send_uri(&uri) |
| 201 |
.await |
| 202 |
{ |
| 203 |
debug!("Unable to open webinterface: {:?}", err); |
| •204 |
utils::inapp_notification( |
| 205 |
"Unable to open webinterface", |
| 206 |
Some(this.obj().upcast_ref::<gtk::Widget>()), |
| 207 |
); |
| 208 |
} |
| 209 |
}; |
| 210 |
glib::spawn_future_local(fut); |
| •211 |
} |
| 212 |
)); |
| 213 |
|
| 214 |
// Downloading page |
| 215 |
// |
| •216 |
client.connect_is_connected_notify(clone!( |
| 217 |
#[weak(rename_to = this)] |
| 218 |
self, |
| 219 |
move |_| { |
| 220 |
this.update_paths(); |
| 221 |
} |
| 222 |
)); |
| 223 |
|
| •224 |
session.connect_download_dir_notify(glib::clone!( |
| 225 |
#[weak(rename_to = this)] |
| 226 |
self, |
| 227 |
move |_| { |
| 228 |
this.update_paths(); |
| 229 |
} |
| 230 |
)); |
| 231 |
|
| 232 |
self.download_dir_button.connect_clicked(move |btn| { |
| 233 |
let parent = btn.root().unwrap().downcast::<gtk::Window>().unwrap(); |
| 234 |
utils::session_dir_filechooser(&parent, FrgDirectoryType::Download); |
| 235 |
}); |
| 236 |
|
| 237 |
session |
| 238 |
.bind_property( |
| 239 |
"incomplete-dir-enabled", |
| 240 |
&*self.incomplete_dir_expander, |
| 241 |
"enable-expansion", |
| 242 |
) |
| 243 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 244 |
.build(); |
| 245 |
|
| •246 |
session.connect_incomplete_dir_enabled_notify(glib::clone!(move |_| { |
| 247 |
FrgConnectionManager::default().check_directories() |
| 248 |
},)); |
| 249 |
|
| •250 |
session.connect_incomplete_dir_notify(glib::clone!( |
| 251 |
#[weak(rename_to = this)] |
| 252 |
self, |
| 253 |
move |_| { |
| 254 |
this.update_paths(); |
| 255 |
} |
| 256 |
)); |
| 257 |
|
| 258 |
self.incomplete_dir_button.connect_clicked(move |btn| { |
| 259 |
let parent = btn.root().unwrap().downcast::<gtk::Window>().unwrap(); |
| 260 |
utils::session_dir_filechooser(&parent.upcast(), FrgDirectoryType::Incomplete); |
| 261 |
}); |
| 262 |
|
| 263 |
session |
| 264 |
.bind_property( |
| 265 |
"download-queue-enabled", |
| 266 |
&*self.download_queue_expander, |
| 267 |
"enable-expansion", |
| 268 |
) |
| 269 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 270 |
.build(); |
| 271 |
|
| 272 |
session |
| 273 |
.bind_property( |
| 274 |
"download-queue-size", |
| 275 |
&*self.download_queue_size_spinbutton, |
| 276 |
"value", |
| 277 |
) |
| 278 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 279 |
.build(); |
| 280 |
|
| 281 |
session |
| 282 |
.bind_property( |
| 283 |
"start-added-torrents", |
| 284 |
&*self.start_added_torrents_switch, |
| 285 |
"active", |
| 286 |
) |
| 287 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 288 |
.build(); |
| 289 |
|
| 290 |
// Network page |
| 291 |
// |
| 292 |
session |
| 293 |
.bind_property("peer-port", &*self.port_spinbutton, "value") |
| 294 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 295 |
.build(); |
| 296 |
|
| 297 |
session |
| 298 |
.bind_property( |
| 299 |
"peer-port-random-on-start", |
| 300 |
&*self.random_port_switch, |
| 301 |
"active", |
| 302 |
) |
| 303 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 304 |
.build(); |
| 305 |
|
| 306 |
session |
| 307 |
.bind_property( |
| 308 |
"port-forwarding-enabled", |
| 309 |
&*self.port_forwarding_switch, |
| 310 |
"active", |
| 311 |
) |
| 312 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 313 |
.build(); |
| 314 |
|
| 315 |
session |
| 316 |
.bind_property( |
| 317 |
"peer-limit-per-torrent", |
| 318 |
&*self.max_peers_torrent_spinbutton, |
| 319 |
"value", |
| 320 |
) |
| 321 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 322 |
.build(); |
| 323 |
|
| 324 |
session |
| 325 |
.bind_property( |
| 326 |
"peer-limit-global", |
| 327 |
&*self.max_peers_overall_spinbutton, |
| 328 |
"value", |
| 329 |
) |
| 330 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 331 |
.build(); |
| 332 |
|
| 333 |
session |
| 334 |
.bind_property("encryption", &*self.encryption_comborow, "selected") |
| 335 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 336 |
.build(); |
| 337 |
|
| 338 |
self.update_paths(); |
| 339 |
self.update_remote_buttons(); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
impl WidgetImpl for FrgPreferencesDialog {} |
| 344 |
|
| 345 |
impl AdwDialogImpl for FrgPreferencesDialog {} |
| 346 |
|
| 347 |
impl PreferencesDialogImpl for FrgPreferencesDialog {} |
| 348 |
|
| 349 |
#[gtk::template_callbacks] |
| 350 |
impl FrgPreferencesDialog { |
| 351 |
#[template_callback] |
| 352 |
async fn port_test_clicked(&self) { |
| 353 |
let client = self.connection_manager.client(); |
| 354 |
|
| 355 |
self.port_test_button.set_sensitive(false); |
| 356 |
let msg = i18n("Testing…"); |
| 357 |
self.port_test_label.set_label(&msg); |
| 358 |
|
| 359 |
self.port_test_label.remove_css_class("heading"); |
| 360 |
self.port_test_label.remove_css_class("error"); |
| 361 |
self.port_test_label.remove_css_class("success"); |
| 362 |
|
| 363 |
let msg = match client.test_port().await { |
| 364 |
Ok(succeeded) => { |
| 365 |
if succeeded { |
| 366 |
self.port_test_label.add_css_class("success"); |
| 367 |
i18n("Port is open. You can communicate with other peers.") |
| 368 |
} else { |
| 369 |
self.port_test_label.add_css_class("error"); |
| 370 |
i18n("Port is closed. Communication with other peers is limited.\nCheck your router or firewall if port forwarding is enabled for your computer. ") |
| 371 |
} |
| 372 |
} |
| 373 |
Err(_) => { |
| 374 |
self.port_test_label.add_css_class("error"); |
| 375 |
i18n("Test failed. Make sure you are connected to the internet.") |
| 376 |
} |
| 377 |
}; |
| 378 |
|
| 379 |
self.port_test_label.add_css_class("heading"); |
| 380 |
self.port_test_label.set_label(&msg); |
| 381 |
self.port_test_button.set_sensitive(true); |
| 382 |
} |
| 383 |
|
| 384 |
fn update_paths(&self) { |
| 385 |
let session = self.connection_manager.client().session(); |
| 386 |
|
| 387 |
if let Some(file) = session.download_dir() { |
| 388 |
let download_dir = file.basename().unwrap().to_str().unwrap().to_string(); |
| 389 |
self.download_dir_content.set_label(&download_dir); |
| 390 |
} |
| 391 |
if let Some(file) = session.incomplete_dir() { |
| 392 |
let incomplete_dir = file.basename().unwrap().to_str().unwrap().to_string(); |
| 393 |
self.incomplete_dir_content.set_label(&incomplete_dir); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
fn update_remote_buttons(&self) { |
| 398 |
let is_fragments = self.connection_manager.current_connection().is_fragments(); |
| 399 |
let title = self.connection_manager.current_connection().title(); |
| 400 |
|
| 401 |
self.download_dir_button.set_sensitive(is_fragments); |
| 402 |
self.incomplete_dir_button.set_sensitive(is_fragments); |
| 403 |
|
| 404 |
self.remote_notice_group.set_visible(!is_fragments); |
| 405 |
self.remote_notice_row |
| 406 |
.set_title(&i18n_f("You are connected with “{}”", &[&title])); |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
glib::wrapper! { |
| 412 |
pub struct FrgPreferencesDialog( |
| 413 |
ObjectSubclass<imp::FrgPreferencesDialog>) |
| •414 |
@extends gtk::Widget, adw::Dialog, adw::PreferencesDialog, |
| 415 |
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget; |
| 416 |
} |
| 417 |
|
| 418 |
impl Default for FrgPreferencesDialog { |
| 419 |
fn default() -> Self { |
| 420 |
glib::Object::new() |
| 421 |
} |
| 422 |
} |