| 1 |
// Fragments - window.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 std::str::FromStr; |
| 18 |
|
| 19 |
use adw::subclass::prelude::AdwApplicationWindowImpl; |
| 20 |
use glib::{clone, subclass}; |
| 21 |
use gtk::prelude::*; |
| 22 |
use gtk::subclass::prelude::*; |
| 23 |
use gtk::{gdk, gio, glib, CompositeTemplate}; |
| 24 |
use magnet_uri::MagnetURI; |
| 25 |
use transmission_gobject::TrTorrent; |
| 26 |
use url::Url; |
| 27 |
|
| 28 |
use crate::app::FrgApplication; |
| 29 |
use crate::backend::{FrgConnectionManager, FrgPollingMode}; |
| 30 |
use crate::i18n::{i18n, i18n_f}; |
| 31 |
use crate::settings::{settings_manager, Key}; |
| 32 |
use crate::ui::{FrgConnectionBox, FrgConnectionPopover}; |
| 33 |
use crate::{config, utils}; |
| 34 |
|
| 35 |
mod imp { |
| 36 |
use super::*; |
| 37 |
|
| 38 |
#[derive(Default, Debug, CompositeTemplate)] |
| 39 |
#[template(resource = "/de/haeckerfelix/Fragments/gtk/window.ui")] |
| 40 |
pub struct FrgApplicationWindow { |
| 41 |
#[template_child] |
| 42 |
headerbar: TemplateChild<adw::HeaderBar>, |
| 43 |
#[template_child] |
| 44 |
window_title: TemplateChild<adw::WindowTitle>, |
| 45 |
#[template_child] |
| 46 |
app_menu_button: TemplateChild<gtk::MenuButton>, |
| 47 |
#[template_child] |
| 48 |
connection_menu: TemplateChild<gtk::MenuButton>, |
| 49 |
#[template_child] |
| 50 |
pub search_button: TemplateChild<gtk::Button>, |
| 51 |
#[template_child] |
| 52 |
pub search_bar: TemplateChild<gtk::SearchBar>, |
| 53 |
#[template_child] |
| 54 |
pub search_entry: TemplateChild<gtk::SearchEntry>, |
| 55 |
#[template_child] |
| 56 |
pub connection_box: TemplateChild<FrgConnectionBox>, |
| 57 |
#[template_child] |
| 58 |
connection_popover: TemplateChild<FrgConnectionPopover>, |
| 59 |
#[template_child] |
| 60 |
pub toast_overlay: TemplateChild<adw::ToastOverlay>, |
| 61 |
} |
| 62 |
|
| 63 |
#[glib::object_subclass] |
| 64 |
impl ObjectSubclass for FrgApplicationWindow { |
| 65 |
const NAME: &'static str = "FrgApplicationWindow"; |
| 66 |
type ParentType = adw::ApplicationWindow; |
| 67 |
type Type = super::FrgApplicationWindow; |
| 68 |
|
| 69 |
fn class_init(klass: &mut Self::Class) { |
| 70 |
klass.bind_template(); |
| 71 |
} |
| 72 |
|
| 73 |
fn instance_init(obj: &subclass::InitializingObject<Self>) { |
| 74 |
obj.init_template(); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
impl ObjectImpl for FrgApplicationWindow { |
| 79 |
fn constructed(&self) { |
| 80 |
self.parent_constructed(); |
| 81 |
|
| 82 |
let obj = self.obj(); |
| 83 |
let cm = FrgConnectionManager::default(); |
| 84 |
|
| 85 |
// Default size |
| 86 |
let width = settings_manager::integer(Key::WindowWidth); |
| 87 |
let height = settings_manager::integer(Key::WindowHeight); |
| 88 |
obj.set_default_size(width, height); |
| 89 |
|
| 90 |
// Development style |
| 91 |
if config::PROFILE == "development" { |
| 92 |
obj.add_css_class("devel"); |
| 93 |
} |
| 94 |
|
| 95 |
// Search |
| 96 |
let app = FrgApplication::default(); |
| 97 |
self.search_entry |
| 98 |
.bind_property("text", &app, "search") |
| 99 |
.bidirectional() |
| 100 |
.build(); |
| 101 |
self.search_bar.connect_entry(&self.search_entry.get()); |
| 102 |
|
| 103 |
// Recolor headerbar purple for remote connections |
| 104 |
cm.connect_current_connection_notify( |
| 105 |
clone!(@weak self as this => move |manager|{ |
| 106 |
if !manager.current_connection().is_fragments(){ |
| 107 |
let subtitle = i18n_f("Remote control \"{}\"", &[&manager.current_connection().title()]); |
| 108 |
this.window_title.set_subtitle(&subtitle); |
| 109 |
this.obj().add_css_class("remote"); |
| 110 |
}else{ |
| 111 |
this.window_title.set_subtitle(""); |
| 112 |
this.obj().remove_css_class("remote"); |
| 113 |
} |
| 114 |
}), |
| 115 |
); |
| 116 |
|
| 117 |
// Show connection button if we have more than the standard local connection |
| 118 |
cm.connections().connect_items_changed( |
| 119 |
clone!(@weak self as this => move |model,_,_,_|{ |
| 120 |
let show = model.n_items() > 1; |
| 121 |
this.connection_menu.set_visible(show); |
| 122 |
}), |
| 123 |
); |
| 124 |
|
| 125 |
// Check clipboard content for magnet links |
| 126 |
obj.connect_is_active_notify(move |window| { |
| 127 |
if window.is_active() { |
| 128 |
let display = gdk::Display::default().unwrap(); |
| 129 |
let clipboard = display.primary_clipboard(); |
| 130 |
|
| 131 |
let fut = clone!(@weak window, @weak clipboard => async move { |
| 132 |
let content = clipboard.read_text_future().await; |
| 133 |
let mut clear_clipboard = false; |
| 134 |
|
| 135 |
if let Ok(Some(text)) = content { |
| 136 |
// Check for magnet link |
| 137 |
if let Ok(magnet_link) = MagnetURI::from_str(&text) { |
| 138 |
let fallback = String::new(); |
| 139 |
let info_hash = magnet_link.info_hash().unwrap_or(&fallback); |
| 140 |
let cm = FrgConnectionManager::default(); |
| 141 |
|
| 142 |
// Transmission lowercases info hashes internally |
| 143 |
if cm.client().torrents().torrent_by_hash(info_hash.to_lowercase()).is_none() { |
| 144 |
debug!("Detected new magnet link: {}", &text); |
| 145 |
window.imp().magnet_link_notification(&text, magnet_link.name()); |
| 146 |
clear_clipboard = true; |
| 147 |
} else { |
| 148 |
debug!("Ignore magnet link, torrent is already added."); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
// Check for torrent link |
| 153 |
if let Ok(url) = Url::parse(&text) { |
| 154 |
// We only support http/https, and no file links because of sandboxing |
| 155 |
if url.path().ends_with(".torrent") && (url.scheme() == "http" || url.scheme() == "https") { |
| 156 |
debug!("Detected torrent link: {}", &text); |
| 157 |
window.imp().torrent_link_notification(&url); |
| 158 |
clear_clipboard = true; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if clear_clipboard { |
| 164 |
// To avoid that the clipboard toast is shown multiple times, we clear the clipboard |
| 165 |
clipboard.set_text(""); |
| 166 |
} |
| 167 |
}); |
| 168 |
glib::spawn_future_local(fut); |
| 169 |
} |
| 170 |
}); |
| 171 |
|
| 172 |
// torrent-added notification |
| 173 |
cm.client().connect_local("torrent-added", false, clone!(@weak self as this => @default-return None, move |torrent|{ |
| 174 |
if settings_manager::boolean(Key::EnableNotificationsNewTorrent){ |
| 175 |
let torrent: TrTorrent = torrent[1].get().unwrap(); |
| 176 |
utils::system_notification(i18n("New torrent added"), torrent.name(), Some("folder-download-symbolic")); |
| 177 |
} |
| 178 |
None |
| 179 |
})); |
| 180 |
|
| 181 |
// torrent-downloaded notification |
| 182 |
cm.client().connect_local("torrent-downloaded", false, clone!(@weak self as this => @default-return None, move |torrent|{ |
| 183 |
if settings_manager::boolean(Key::EnableNotificationsDownloaded){ |
| 184 |
let torrent: TrTorrent = torrent[1].get().unwrap(); |
| 185 |
utils::system_notification(i18n("Torrent completely downloaded"), torrent.name(), Some("folder-download-symbolic")); |
| 186 |
} |
| 187 |
None |
| 188 |
})); |
| 189 |
|
| 190 |
// Lower polling rate when window isn't active |
| 191 |
obj.connect_is_active_notify(clone!(@weak self as this => move |_| { |
| 192 |
this.update_polling_mode(); |
| 193 |
})); |
| 194 |
|
| 195 |
obj.connect_suspended_notify(clone!(@weak self as this => move |_| { |
| 196 |
this.update_polling_mode(); |
| 197 |
})); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
impl WidgetImpl for FrgApplicationWindow {} |
| 202 |
|
| 203 |
impl WindowImpl for FrgApplicationWindow { |
| 204 |
fn close_request(&self) -> glib::Propagation { |
| 205 |
self.parent_close_request(); |
| 206 |
|
| 207 |
debug!("Saving window geometry."); |
| 208 |
let window = self.obj(); |
| 209 |
let (width, height) = window.default_size(); |
| 210 |
|
| 211 |
settings_manager::set_integer(Key::WindowWidth, width); |
| 212 |
settings_manager::set_integer(Key::WindowHeight, height); |
| 213 |
|
| 214 |
let fut = clone!(@weak window => async move { |
| 215 |
window.set_visible(false); |
| 216 |
|
| 217 |
// Wait till transmission daemon is stopped |
| 218 |
FrgConnectionManager::default().disconnect() |
| 219 |
.await |
| 220 |
.expect("Unable to stop the transmission daemon"); |
| 221 |
|
| 222 |
FrgApplication::default().quit(); |
| 223 |
}); |
| 224 |
glib::spawn_future_local(fut); |
| 225 |
|
| 226 |
glib::Propagation::Stop |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
impl ApplicationWindowImpl for FrgApplicationWindow {} |
| 231 |
|
| 232 |
impl AdwApplicationWindowImpl for FrgApplicationWindow {} |
| 233 |
|
| 234 |
impl FrgApplicationWindow { |
| 235 |
fn update_polling_mode(&self) { |
| 236 |
let mode = if self.obj().is_suspended() { |
| 237 |
FrgPollingMode::SuspendedWindow |
| 238 |
} else if !self.obj().is_active() { |
| 239 |
FrgPollingMode::InactiveWindow |
| 240 |
} else { |
| 241 |
FrgPollingMode::Regular |
| 242 |
}; |
| 243 |
FrgConnectionManager::default().set_polling_mode(mode); |
| 244 |
} |
| 245 |
|
| 246 |
fn magnet_link_notification(&self, magnet_link: &str, title: Option<&str>) { |
| 247 |
let text = match title { |
| 248 |
Some(name) => i18n_f("Add magnet link “{}” from clipboard?", &[name]), |
| 249 |
None => i18n("Add magnet link from clipboard?"), |
| 250 |
}; |
| 251 |
|
| 252 |
let toast = adw::Toast::new(&text); |
| 253 |
toast.set_timeout(0); |
| 254 |
toast.set_button_label(Some(&i18n("_Add"))); |
| 255 |
toast.set_action_name(Some("app.add-link")); |
| 256 |
toast.set_action_target_value(Some(&magnet_link.to_variant())); |
| 257 |
|
| 258 |
self.toast_overlay.get().add_toast(toast); |
| 259 |
} |
| 260 |
|
| 261 |
fn torrent_link_notification(&self, torrent_link: &Url) { |
| 262 |
let mut msg = i18n_f( |
| 263 |
"Add torrent from “{}”?", |
| 264 |
&[torrent_link.host_str().unwrap_or("unknown")], |
| 265 |
); |
| 266 |
|
| 267 |
// Try finding the exact torrent file name |
| 268 |
if let Some(segments) = torrent_link.path_segments() { |
| 269 |
if let Some(last) = segments.last() { |
| 270 |
msg = i18n_f("Add “{}” from clipboard?", &[last]) |
| 271 |
} |
| 272 |
} |
| 273 |
let toast = adw::Toast::new(&msg); |
| 274 |
toast.set_timeout(0); |
| 275 |
toast.set_button_label(Some(&i18n("_Add"))); |
| 276 |
toast.set_action_name(Some("app.add-link")); |
| 277 |
toast.set_action_target_value(Some(&torrent_link.to_string().to_variant())); |
| 278 |
|
| 279 |
self.toast_overlay.get().add_toast(toast); |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
// Wrap FrgApplicationWindow into a usable gtk-rs object |
| 285 |
glib::wrapper! { |
| 286 |
pub struct FrgApplicationWindow( |
| 287 |
ObjectSubclass<imp::FrgApplicationWindow>) |
| 288 |
@extends gtk::Widget, gtk::Window, gtk::ApplicationWindow, adw::ApplicationWindow, |
| 289 |
@implements gio::ActionMap, gio::ActionGroup; |
| 290 |
} |
| 291 |
|
| 292 |
// FrgApplicationWindow implementation itself |
| 293 |
impl FrgApplicationWindow { |
| 294 |
pub fn new(app: &FrgApplication) -> Self { |
| 295 |
glib::Object::builder::<FrgApplicationWindow>() |
| 296 |
.property("application", app) |
| 297 |
.build() |
| 298 |
} |
| 299 |
|
| 300 |
pub async fn set_connection(&self, connection_uuid: String) { |
| 301 |
// Hide searchbar again when changing connection |
| 302 |
self.imp().search_bar.set_search_mode(false); |
| 303 |
|
| 304 |
self.imp() |
| 305 |
.connection_box |
| 306 |
.set_connection(connection_uuid) |
| 307 |
.await; |
| 308 |
} |
| 309 |
|
| 310 |
pub fn show_search(&self, toggle: bool) { |
| 311 |
let imp = self.imp(); |
| 312 |
|
| 313 |
if imp.search_button.is_sensitive() { |
| 314 |
if toggle { |
| 315 |
imp.search_bar |
| 316 |
.set_search_mode(!imp.search_bar.is_search_mode()); |
| 317 |
} else { |
| 318 |
imp.search_bar.set_search_mode(true); |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
pub fn toast_overlay(&self) -> adw::ToastOverlay { |
| 324 |
self.imp().toast_overlay.get() |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
impl Default for FrgApplicationWindow { |
| 329 |
fn default() -> Self { |
| 330 |
FrgApplication::default() |
| 331 |
.active_window() |
| 332 |
.unwrap() |
| 333 |
.downcast() |
| 334 |
.unwrap() |
| 335 |
} |
| 336 |
} |