| 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::{Enum, Properties}; |
| 21 |
use gtk::glib; |
| 22 |
use gtk::prelude::*; |
| 23 |
use gtk::subclass::sorter::SorterImpl; |
| 24 |
use transmission_gobject::TrTorrent; |
| 25 |
|
| 26 |
mod imp { |
| 27 |
use super::*; |
| 28 |
|
| 29 |
#[derive(Debug, Default, Properties)] |
| 30 |
#[properties(wrapper_type = super::FrgTorrentSorter)] |
| 31 |
pub struct FrgTorrentSorter { |
| 32 |
#[property(get, set = Self::set_descending)] |
| 33 |
descending: RefCell<bool>, |
| 34 |
#[property(get, set = Self::set_sorting, builder(Default::default()))] |
| 35 |
sorting: RefCell<FrgTorrentSorting>, |
| 36 |
} |
| 37 |
|
| 38 |
impl FrgTorrentSorter { |
| 39 |
pub fn set_sorting(&self, sorting: FrgTorrentSorting) { |
| 40 |
*self.sorting.borrow_mut() = sorting; |
| 41 |
self.obj().changed(gtk::SorterChange::Different); |
| 42 |
} |
| 43 |
|
| 44 |
pub fn set_descending(&self, descending: bool) { |
| 45 |
*self.descending.borrow_mut() = descending; |
| 46 |
self.obj().changed(gtk::SorterChange::Different); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
#[glib::object_subclass] |
| 51 |
impl ObjectSubclass for FrgTorrentSorter { |
| 52 |
const NAME: &'static str = "FrgTorrentSorter"; |
| 53 |
type Type = super::FrgTorrentSorter; |
| 54 |
type ParentType = gtk::Sorter; |
| 55 |
} |
| 56 |
|
| 57 |
#[glib::derived_properties] |
| 58 |
impl ObjectImpl for FrgTorrentSorter {} |
| 59 |
|
| 60 |
impl SorterImpl for FrgTorrentSorter { |
| 61 |
fn order(&self) -> gtk::SorterOrder { |
| 62 |
gtk::SorterOrder::Total |
| 63 |
} |
| 64 |
|
| 65 |
fn compare(&self, item1: &glib::Object, item2: &glib::Object) -> gtk::Ordering { |
| 66 |
let a = item1.downcast_ref::<TrTorrent>().unwrap(); |
| 67 |
let b = item2.downcast_ref::<TrTorrent>().unwrap(); |
| 68 |
|
| 69 |
Self::torrent_cmp(a, b, *self.sorting.borrow(), *self.descending.borrow()).into() |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
impl FrgTorrentSorter { |
| 74 |
fn torrent_cmp( |
| 75 |
a: &TrTorrent, |
| 76 |
b: &TrTorrent, |
| 77 |
sorting: FrgTorrentSorting, |
| 78 |
descending: bool, |
| 79 |
) -> std::cmp::Ordering { |
| 80 |
let mut torrent_a = a.clone(); |
| 81 |
let mut torrent_b = b.clone(); |
| 82 |
|
| 83 |
if descending { |
| 84 |
std::mem::swap(&mut torrent_a, &mut torrent_b); |
| 85 |
} |
| 86 |
|
| 87 |
match sorting { |
| 88 |
FrgTorrentSorting::Default => std::cmp::Ordering::Equal, |
| 89 |
FrgTorrentSorting::Name => torrent_a.name().cmp(&torrent_b.name()), |
| 90 |
FrgTorrentSorting::Queue => { |
| 91 |
torrent_a.queue_position().cmp(&torrent_b.queue_position()) |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
glib::wrapper! { |
| 99 |
pub struct FrgTorrentSorter(ObjectSubclass<imp::FrgTorrentSorter>) @extends gtk::Sorter; |
| 100 |
} |
| 101 |
|
| 102 |
impl FrgTorrentSorter { |
| 103 |
pub fn new() -> Self { |
| 104 |
Self::default() |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
impl Default for FrgTorrentSorter { |
| 109 |
fn default() -> Self { |
| 110 |
glib::Object::new() |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
#[derive(Default, Display, Copy, Debug, Clone, EnumString, PartialEq, Enum, Eq)] |
| 115 |
#[repr(u32)] |
| 116 |
#[enum_type(name = "FrgTorrentSorting")] |
| 117 |
pub enum FrgTorrentSorting { |
| 118 |
#[default] |
| 119 |
Default, |
| 120 |
Name, |
| 121 |
Queue, |
| 122 |
} |