Debian Codegraph @

src/model/torrent_filter.rs

raw
1 // Fragments - torrent_sorter.rs
2 // Copyright (C) 2022-2023 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::RefCell;
18
19 use glib::subclass::prelude::*;
20 use glib::Properties;
21 use gtk::glib;
22 use gtk::prelude::*;
23 use gtk::subclass::filter::FilterImpl;
24 use transmission_gobject::{TrTorrent, TrTorrentStatus};
25
26 mod imp {
27 use super::*;
28
29 #[derive(Debug, Default, Properties)]
30 #[properties(wrapper_type = super::FrgTorrentFilter)]
31 pub struct FrgTorrentFilter {
32 #[property(get, set = Self::set_status, builder(Default::default()))]
33 status: RefCell<TrTorrentStatus>,
34 }
35
36 impl FrgTorrentFilter {
37 pub fn set_status(&self, status: TrTorrentStatus) {
38 *self.status.borrow_mut() = status;
39 self.obj().changed(gtk::FilterChange::Different);
40 }
41 }
42
43 #[glib::object_subclass]
44 impl ObjectSubclass for FrgTorrentFilter {
45 const NAME: &'static str = "FrgTorrentFilter";
46 type Type = super::FrgTorrentFilter;
47 type ParentType = gtk::Filter;
48 }
49
50 #[glib::derived_properties]
51 impl ObjectImpl for FrgTorrentFilter {}
52
53 impl FilterImpl for FrgTorrentFilter {
54 fn match_(&self, item: &glib::Object) -> bool {
55 let filter = self.obj();
56 let torrent = item.downcast_ref::<TrTorrent>().unwrap();
57 torrent.status() == filter.status()
58 }
59 }
60 }
61
62 glib::wrapper! {
63 pub struct FrgTorrentFilter(ObjectSubclass<imp::FrgTorrentFilter>) @extends gtk::Filter;
64 }
65
66 impl FrgTorrentFilter {
67 pub fn new() -> Self {
68 Self::default()
69 }
70 }
71
72 impl Default for FrgTorrentFilter {
73 fn default() -> Self {
74 glib::Object::new()
75 }
76 }