| 1 |
// Fragments - preferences_dialog.rs |
| 2 |
// Copyright (C) 2022-2024 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.connect_current_connection_notify( |
| 122 |
clone!(@weak self as this => move |_|{ |
| 123 |
this.update_remote_buttons(); |
| 124 |
}), |
| 125 |
); |
| 126 |
|
| 127 |
// General page |
| 128 |
// |
| 129 |
settings_manager::bind_property( |
| 130 |
Key::InhibitSuspendActiveTorrents, |
| 131 |
&*self.inhibit_suspend_active_torrents_switch, |
| 132 |
"active", |
| 133 |
); |
| 134 |
self.inhibit_suspend_active_torrents_switch |
| 135 |
.connect_active_notify(|_| { |
| 136 |
FrgApplication::default().update_inhibit_state(); |
| 137 |
}); |
| 138 |
settings_manager::bind_property( |
| 139 |
Key::TrashOriginalTorrentFiles, |
| 140 |
&*self.trash_original_torrent_files_switch, |
| 141 |
"active", |
| 142 |
); |
| 143 |
settings_manager::bind_property( |
| 144 |
Key::EnableNotificationsNewTorrent, |
| 145 |
&*self.notifications_new_torrent_switch, |
| 146 |
"active", |
| 147 |
); |
| 148 |
settings_manager::bind_property( |
| 149 |
Key::EnableNotificationsDownloaded, |
| 150 |
&*self.notifications_downloaded_switch, |
| 151 |
"active", |
| 152 |
); |
| 153 |
settings_manager::bind_property( |
| 154 |
Key::RemoteAccess, |
| 155 |
&*self.remote_access_switch, |
| 156 |
"active", |
| 157 |
); |
| 158 |
self.remote_access_switch.connect_state_set(clone!(@weak self as this => @default-panic, move |switch, _| { |
| 159 |
settings_manager::set_boolean(Key::RemoteAccess, switch.is_active()); |
| 160 |
switch.set_sensitive(false); |
| 161 |
|
| 162 |
let fut = clone!(@weak switch, @weak this => async move { |
| 163 |
if let Err(err) = FrgConnectionManager::default().reconnect().await{ |
| 164 |
utils::inapp_notification(&i18n("Unable to restart Transmission daemon"), Some(this.obj().upcast_ref::<gtk::Widget>())); |
| 165 |
warn!("Unable to restart Transmission daemon: {}", err.to_string()); |
| 166 |
} |
| 167 |
|
| 168 |
switch.set_sensitive(true); |
| 169 |
}); |
| 170 |
glib::spawn_future_local(fut); |
| 171 |
glib::Propagation::Proceed |
| 172 |
})); |
| 173 |
self.open_webinterface_row |
| 174 |
.connect_activated(clone!(@weak self as this => move |_| { |
| 175 |
let fut = async move { |
| 176 |
let uri = url::Url::parse("http://127.0.0.1:9091/").unwrap(); |
| 177 |
let native = this.obj().native().unwrap(); |
| 178 |
let identifier = WindowIdentifier::from_native(&native).await; |
| 179 |
if let Err(err) = OpenFileRequest::default() |
| 180 |
.identifier(identifier) |
| 181 |
.send_uri(&uri) |
| 182 |
.await |
| 183 |
{ |
| 184 |
debug!("Unable to open webinterface: {:?}", err); |
| 185 |
utils::inapp_notification("Unable to open webinterface", Some(this.obj().upcast_ref::<gtk::Widget>())); |
| 186 |
} |
| 187 |
}; |
| 188 |
glib::spawn_future_local(fut); |
| 189 |
})); |
| 190 |
|
| 191 |
// Downloading page |
| 192 |
// |
| 193 |
client.connect_is_connected_notify(clone!(@weak self as this => move |_|{ |
| 194 |
this.update_paths(); |
| 195 |
})); |
| 196 |
|
| 197 |
session.connect_download_dir_notify(glib::clone!(@weak self as this => move |_| { |
| 198 |
this.update_paths(); |
| 199 |
})); |
| 200 |
|
| 201 |
self.download_dir_button.connect_clicked(move |btn| { |
| 202 |
let parent = btn.root().unwrap().downcast::<gtk::Window>().unwrap(); |
| 203 |
utils::session_dir_filechooser(&parent, FrgDirectoryType::Download); |
| 204 |
}); |
| 205 |
|
| 206 |
session |
| 207 |
.bind_property( |
| 208 |
"incomplete-dir-enabled", |
| 209 |
&*self.incomplete_dir_expander, |
| 210 |
"enable-expansion", |
| 211 |
) |
| 212 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 213 |
.build(); |
| 214 |
|
| 215 |
session.connect_incomplete_dir_enabled_notify( |
| 216 |
glib::clone!(@weak self as this => move |_| { |
| 217 |
FrgConnectionManager::default().check_directories(); |
| 218 |
}), |
| 219 |
); |
| 220 |
|
| 221 |
session.connect_incomplete_dir_notify(glib::clone!(@weak self as this => move |_| { |
| 222 |
this.update_paths(); |
| 223 |
})); |
| 224 |
|
| 225 |
self.incomplete_dir_button.connect_clicked(move |btn| { |
| 226 |
let parent = btn.root().unwrap().downcast::<gtk::Window>().unwrap(); |
| 227 |
utils::session_dir_filechooser(&parent.upcast(), FrgDirectoryType::Incomplete); |
| 228 |
}); |
| 229 |
|
| 230 |
session |
| 231 |
.bind_property( |
| 232 |
"download-queue-enabled", |
| 233 |
&*self.download_queue_expander, |
| 234 |
"enable-expansion", |
| 235 |
) |
| 236 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 237 |
.build(); |
| 238 |
|
| 239 |
session |
| 240 |
.bind_property( |
| 241 |
"download-queue-size", |
| 242 |
&*self.download_queue_size_spinbutton, |
| 243 |
"value", |
| 244 |
) |
| 245 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 246 |
.build(); |
| 247 |
|
| 248 |
session |
| 249 |
.bind_property( |
| 250 |
"start-added-torrents", |
| 251 |
&*self.start_added_torrents_switch, |
| 252 |
"active", |
| 253 |
) |
| 254 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 255 |
.build(); |
| 256 |
|
| 257 |
// Network page |
| 258 |
// |
| 259 |
session |
| 260 |
.bind_property("peer-port", &*self.port_spinbutton, "value") |
| 261 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 262 |
.build(); |
| 263 |
|
| 264 |
session |
| 265 |
.bind_property( |
| 266 |
"peer-port-random-on-start", |
| 267 |
&*self.random_port_switch, |
| 268 |
"active", |
| 269 |
) |
| 270 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 271 |
.build(); |
| 272 |
|
| 273 |
session |
| 274 |
.bind_property( |
| 275 |
"port-forwarding-enabled", |
| 276 |
&*self.port_forwarding_switch, |
| 277 |
"active", |
| 278 |
) |
| 279 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 280 |
.build(); |
| 281 |
|
| 282 |
session |
| 283 |
.bind_property( |
| 284 |
"peer-limit-per-torrent", |
| 285 |
&*self.max_peers_torrent_spinbutton, |
| 286 |
"value", |
| 287 |
) |
| 288 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 289 |
.build(); |
| 290 |
|
| 291 |
session |
| 292 |
.bind_property( |
| 293 |
"peer-limit-global", |
| 294 |
&*self.max_peers_overall_spinbutton, |
| 295 |
"value", |
| 296 |
) |
| 297 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 298 |
.build(); |
| 299 |
|
| 300 |
session |
| 301 |
.bind_property("encryption", &*self.encryption_comborow, "selected") |
| 302 |
.flags(glib::BindingFlags::SYNC_CREATE | glib::BindingFlags::BIDIRECTIONAL) |
| 303 |
.build(); |
| 304 |
|
| 305 |
self.update_paths(); |
| 306 |
self.update_remote_buttons(); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
impl WidgetImpl for FrgPreferencesDialog {} |
| 311 |
|
| 312 |
impl AdwDialogImpl for FrgPreferencesDialog {} |
| 313 |
|
| 314 |
impl PreferencesDialogImpl for FrgPreferencesDialog {} |
| 315 |
|
| 316 |
#[gtk::template_callbacks] |
| 317 |
impl FrgPreferencesDialog { |
| 318 |
#[template_callback] |
| 319 |
async fn port_test_clicked(&self) { |
| 320 |
let client = self.connection_manager.client(); |
| 321 |
|
| 322 |
self.port_test_button.set_sensitive(false); |
| 323 |
let msg = i18n("Testing…"); |
| 324 |
self.port_test_label.set_label(&msg); |
| 325 |
|
| 326 |
self.port_test_label.remove_css_class("heading"); |
| 327 |
self.port_test_label.remove_css_class("error"); |
| 328 |
self.port_test_label.remove_css_class("success"); |
| 329 |
|
| 330 |
let msg = match client.test_port().await { |
| 331 |
Ok(succeeded) => { |
| 332 |
if succeeded { |
| 333 |
self.port_test_label.add_css_class("success"); |
| 334 |
i18n("Port is open. You can communicate with other peers.") |
| 335 |
} else { |
| 336 |
self.port_test_label.add_css_class("error"); |
| 337 |
i18n("Port is closed. Communication with other peers is limited.\nCheck your router or firewall if port forwarding is enabled for your computer. ") |
| 338 |
} |
| 339 |
} |
| 340 |
Err(_) => { |
| 341 |
self.port_test_label.add_css_class("error"); |
| 342 |
i18n("Test failed. Make sure you are connected to the internet.") |
| 343 |
} |
| 344 |
}; |
| 345 |
|
| 346 |
self.port_test_label.add_css_class("heading"); |
| 347 |
self.port_test_label.set_label(&msg); |
| 348 |
self.port_test_button.set_sensitive(true); |
| 349 |
} |
| 350 |
|
| 351 |
fn update_paths(&self) { |
| 352 |
let session = self.connection_manager.client().session(); |
| 353 |
|
| 354 |
if let Some(file) = session.download_dir() { |
| 355 |
let download_dir = file.basename().unwrap().to_str().unwrap().to_string(); |
| 356 |
self.download_dir_content.set_label(&download_dir); |
| 357 |
} |
| 358 |
if let Some(file) = session.incomplete_dir() { |
| 359 |
let incomplete_dir = file.basename().unwrap().to_str().unwrap().to_string(); |
| 360 |
self.incomplete_dir_content.set_label(&incomplete_dir); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
fn update_remote_buttons(&self) { |
| 365 |
let is_fragments = self.connection_manager.current_connection().is_fragments(); |
| 366 |
let title = self.connection_manager.current_connection().title(); |
| 367 |
|
| 368 |
self.download_dir_button.set_sensitive(is_fragments); |
| 369 |
self.incomplete_dir_button.set_sensitive(is_fragments); |
| 370 |
|
| 371 |
self.remote_notice_group.set_visible(!is_fragments); |
| 372 |
self.remote_notice_row |
| 373 |
.set_title(&i18n_f("You are connected with “{}”", &[&title])); |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
glib::wrapper! { |
| 379 |
pub struct FrgPreferencesDialog( |
| 380 |
ObjectSubclass<imp::FrgPreferencesDialog>) |
| 381 |
@extends gtk::Widget, adw::Dialog, adw::PreferencesDialog; |
| 382 |
} |
| 383 |
|
| 384 |
impl Default for FrgPreferencesDialog { |
| 385 |
fn default() -> Self { |
| 386 |
glib::Object::new() |
| 387 |
} |
| 388 |
} |