Debian Codegraph @

src/ui/file_page.rs

raw ◀ ▶
1 Debian patch touch this file
1 // Fragments - file_page.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::OnceCell;
18
19 use adw::prelude::*;
20 use adw::subclass::prelude::*;
21 use glib::{subclass, Properties};
22 use gtk::{gio, glib, CompositeTemplate};
23 use transmission_gobject::TrFile;
24
25 use crate::actions;
26 use crate::ui::FrgStatusHeader;
27
28 mod imp {
29 use super::*;
30
31 #[derive(Debug, Default, Properties, CompositeTemplate)]
32 #[template(resource = "/de/haeckerfelix/Fragments/gtk/file_page.ui")]
33 #[properties(wrapper_type = super::FrgFilePage)]
34 pub struct FrgFilePage {
35 #[property(get, set, construct_only)]
36 file: OnceCell<TrFile>,
37
38 #[template_child]
39 status_header: TemplateChild<FrgStatusHeader>,
40 #[template_child]
41 checkbutton: TemplateChild<gtk::CheckButton>,
42 }
43
44 #[glib::object_subclass]
45 impl ObjectSubclass for FrgFilePage {
46 const NAME: &'static str = "FrgFilePage";
47 type ParentType = adw::NavigationPage;
48 type Type = super::FrgFilePage;
49
50 fn class_init(klass: &mut Self::Class) {
51 klass.bind_template();
52 }
53
54 fn instance_init(obj: &subclass::InitializingObject<Self>) {
55 obj.init_template();
56 }
57 }
58
59 #[glib::derived_properties]
60 impl ObjectImpl for FrgFilePage {
61 fn constructed(&self) {
62 self.parent_constructed();
63 let file = self.obj().file();
64
65 self.obj().set_title(&file.title());
66
67 self.status_header.set_title(file.title());
68
•69 let mimetype = gio::content_type_guess(Some(&file.name()), None)
70 .0
71 .to_string();
72 self.status_header.set_mimetype(mimetype);
73
74 file.bind_property("length", &self.status_header.get(), "total-bytes")
75 .sync_create()
76 .build();
77
78 file.bind_property(
79 "bytes-completed",
80 &self.status_header.get(),
81 "downloaded-bytes",
82 )
83 .sync_create()
84 .build();
85
86 file.bind_property("wanted", &self.checkbutton.get(), "active")
87 .sync_create()
88 .bidirectional()
89 .build();
90
91 file.bind_property("bytes-completed", &self.checkbutton.get(), "sensitive")
92 .transform_to(|b, bytes: i64| {
93 Some(bytes != b.source().unwrap().downcast::<TrFile>().unwrap().length())
94 })
95 .sync_create()
96 .build();
97
98 actions::install_file_actions(&self.obj().file(), self.obj().clone().upcast());
99 }
100 }
101
102 impl WidgetImpl for FrgFilePage {}
103
104 impl NavigationPageImpl for FrgFilePage {}
105 }
106
107 glib::wrapper! {
108 pub struct FrgFilePage(ObjectSubclass<imp::FrgFilePage>)
•109 @extends gtk::Widget, adw::NavigationPage,
110 @implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
111 }
112
113 impl FrgFilePage {
114 pub fn new(file: &TrFile) -> Self {
115 glib::Object::builder().property("file", file).build()
116 }
117 }