Debian Codegraph @

src/ui/torrent_page.rs

raw ◀ ▶
1 Debian patch touch this file
1 // Fragments - torrent_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 adw::subclass::prelude::*;
18 use glib::{clone, subclass};
19 use gtk::prelude::*;
20 use gtk::{glib, CompositeTemplate, FilterListModel};
21 use transmission_gobject::{TrTorrent, TrTorrentStatus};
22
23 use crate::backend::FrgConnectionManager;
24 use crate::model::{FrgTorrentFilter, FrgTorrentSorter, FrgTorrentSorting};
25 use crate::ui::FrgTorrentGroup;
26 use crate::FrgApplication;
27
28 mod imp {
29 use super::*;
30
31 #[derive(Default, Debug, CompositeTemplate)]
32 #[template(resource = "/de/haeckerfelix/Fragments/gtk/torrent_page.ui")]
33 pub struct FrgTorrentPage {
34 #[template_child]
35 downloading_group: TemplateChild<FrgTorrentGroup>,
36 #[template_child]
37 queued_group: TemplateChild<FrgTorrentGroup>,
38 #[template_child]
39 seeding_group: TemplateChild<FrgTorrentGroup>,
40 }
41
42 #[glib::object_subclass]
43 impl ObjectSubclass for FrgTorrentPage {
44 const NAME: &'static str = "FrgTorrentPage";
45 type ParentType = adw::PreferencesPage;
46 type Type = super::FrgTorrentPage;
47
48 fn class_init(klass: &mut Self::Class) {
49 klass.bind_template();
50 }
51
52 fn instance_init(obj: &subclass::InitializingObject<Self>) {
53 obj.init_template();
54 }
55 }
56
57 impl ObjectImpl for FrgTorrentPage {
58 fn constructed(&self) {
59 self.parent_constructed();
60
61 // Downloading group
62 self.downloading_group
63 .add_model(self.model(TrTorrentStatus::Download, FrgTorrentSorting::Name));
64
65 // Queued group
66 self.queued_group
67 .add_model(self.model(TrTorrentStatus::DownloadWait, FrgTorrentSorting::Queue));
68 self.queued_group
69 .add_model(self.model(TrTorrentStatus::Stopped, FrgTorrentSorting::Name));
70 self.queued_group
71 .add_model(self.model(TrTorrentStatus::Check, FrgTorrentSorting::Name));
72 self.queued_group
73 .add_model(self.model(TrTorrentStatus::CheckWait, FrgTorrentSorting::Name));
74
75 // Seeding group
76 self.seeding_group
77 .add_model(self.model(TrTorrentStatus::Seed, FrgTorrentSorting::Name));
78 self.seeding_group
79 .add_model(self.model(TrTorrentStatus::SeedWait, FrgTorrentSorting::Name));
80 }
81 }
82
83 impl WidgetImpl for FrgTorrentPage {}
84
85 impl PreferencesPageImpl for FrgTorrentPage {}
86
87 impl FrgTorrentPage {
88 fn model(&self, status: TrTorrentStatus, sorting: FrgTorrentSorting) -> gtk::SortListModel {
89 let model = FrgConnectionManager::default().client().torrents();
90 let app = FrgApplication::default();
91
92 // Filter by group
93 let group_filter = FrgTorrentFilter::new();
94 group_filter.set_status(status);
95
96 // Filter by search
97 let search_filter = gtk::StringFilter::new(Some(&gtk::PropertyExpression::new(
98 TrTorrent::static_type(),
99 None::<&gtk::Expression>,
100 "name",
101 )));
102 app.bind_property("search", &search_filter, "search")
103 .build();
104
105 // Sort by name
106 let sorter = FrgTorrentSorter::new();
107 sorter.set_sorting(sorting);
108
109 // Update group and search filter when torrents model changes
110 model.connect_local(
111 "status-changed",
112 false,
•113 clone!(
114 #[weak]
115 group_filter,
116 #[weak]
117 sorter,
118 #[upgrade_or]
119 None,
120 move |_| {
121 group_filter.changed(gtk::FilterChange::Different);
122 sorter.changed(gtk::SorterChange::Different);
123 None
124 }
125 ),
126 );
127
128 let group_model = FilterListModel::new(Some(model), Some(group_filter));
129 let search_model = FilterListModel::new(Some(group_model), Some(search_filter));
130
131 gtk::SortListModel::new(Some(search_model), Some(sorter))
132 }
133 }
134 }
135
136 glib::wrapper! {
137 pub struct FrgTorrentPage(
138 ObjectSubclass<imp::FrgTorrentPage>)
•139 @extends gtk::Widget, adw::PreferencesPage,
140 @implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
141 }
142
143 impl FrgTorrentPage {
144 pub fn new() -> Self {
145 glib::Object::new()
146 }
147 }
148
149 impl Default for FrgTorrentPage {
150 fn default() -> Self {
151 Self::new()
152 }
153 }