| 1 |
// Fragments - file_filter.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::Properties; |
| 20 |
use gtk::glib; |
| 21 |
use gtk::prelude::*; |
| 22 |
use gtk::subclass::filter::FilterImpl; |
| 23 |
use gtk::subclass::prelude::*; |
| 24 |
use transmission_gobject::TrFile; |
| 25 |
|
| 26 |
mod imp { |
| 27 |
use super::*; |
| 28 |
|
| 29 |
#[derive(Debug, Default, Properties)] |
| 30 |
#[properties(wrapper_type = super::FrgFileFilter)] |
| 31 |
pub struct FrgFileFilter { |
| 32 |
#[property(get, set = Self::set_folder, nullable)] |
| 33 |
folder: RefCell<Option<TrFile>>, |
| 34 |
#[property(get, set = Self::set_search)] |
| 35 |
search: RefCell<String>, |
| 36 |
} |
| 37 |
|
| 38 |
#[glib::object_subclass] |
| 39 |
impl ObjectSubclass for FrgFileFilter { |
| 40 |
const NAME: &'static str = "FrgFileFilter"; |
| 41 |
type Type = super::FrgFileFilter; |
| 42 |
type ParentType = gtk::Filter; |
| 43 |
} |
| 44 |
|
| 45 |
#[glib::derived_properties] |
| 46 |
impl ObjectImpl for FrgFileFilter {} |
| 47 |
|
| 48 |
impl FilterImpl for FrgFileFilter { |
| 49 |
fn match_(&self, item: &glib::Object) -> bool { |
| 50 |
let file = item.downcast_ref::<TrFile>().unwrap(); |
| 51 |
let file_name = file.name(); |
| 52 |
let search = self.obj().search().to_lowercase(); |
| 53 |
|
| 54 |
// Filter by folder name |
| 55 |
if let Some(folder) = self.obj().folder() { |
| 56 |
let folder_name = format!("{}/", folder.name()); |
| 57 |
|
| 58 |
// First check if the file is a child of `folder` |
| 59 |
if file_name.starts_with(&folder_name) { |
| 60 |
// Get the rest of the path |
| 61 |
// |
| 62 |
// For example |
| 63 |
// filter: this/is/a/long/nested/path/ |
| 64 |
// item: this/is/a/ |
| 65 |
// result: long/nested/path |
| 66 |
let path_suffix = file_name.trim_start_matches(&folder_name); |
| 67 |
|
| 68 |
if search.is_empty() { |
| 69 |
// Path suffix contains another slash -> another nested subfolder, |
| 70 |
// but which isn't a *direct* child of the filtered folder |
| 71 |
return !path_suffix.contains('/'); |
| 72 |
} |
| 73 |
|
| 74 |
return path_suffix.to_lowercase().contains(&search); |
| 75 |
} else { |
| 76 |
return false; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
true |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
impl FrgFileFilter { |
| 85 |
fn set_folder(&self, folder: Option<&TrFile>) { |
| 86 |
*self.folder.borrow_mut() = folder.cloned(); |
| 87 |
self.obj().changed(gtk::FilterChange::Different); |
| 88 |
} |
| 89 |
|
| 90 |
fn set_search(&self, search: String) { |
| 91 |
*self.search.borrow_mut() = search; |
| 92 |
self.obj().changed(gtk::FilterChange::Different); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
glib::wrapper! { |
| 98 |
pub struct FrgFileFilter(ObjectSubclass<imp::FrgFileFilter>) @extends gtk::Filter; |
| 99 |
} |
| 100 |
|
| 101 |
impl Default for FrgFileFilter { |
| 102 |
fn default() -> Self { |
| 103 |
Self::new() |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl FrgFileFilter { |
| 108 |
pub fn new() -> Self { |
| 109 |
glib::Object::new() |
| 110 |
} |
| 111 |
} |