1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::FileFilterFlags;
use glib::translate::*;
use std::ffi::CStr;

/// A [`FileFilterInfo`][crate::FileFilterInfo]-struct is used to pass information about the
/// tested file to [`FileFilter::filter()`][crate::FileFilter::filter()].
#[repr(transparent)]
#[doc(alias = "GtkFileFilterInfo")]
pub struct FileFilterInfo(ffi::GtkFileFilterInfo);

impl FileFilterInfo {
    pub fn new(
        filename: Option<&str>,
        uri: Option<&str>,
        display_name: Option<&str>,
        mime_type: Option<&str>,
    ) -> Self {
        skip_assert_initialized!();
        let mut contains = FileFilterFlags::empty();
        for (value, bit) in [
            (filename, FileFilterFlags::FILENAME),
            (uri, FileFilterFlags::URI),
            (display_name, FileFilterFlags::DISPLAY_NAME),
            (mime_type, FileFilterFlags::MIME_TYPE),
        ] {
            if value.is_some() {
                contains |= bit;
            }
        }

        Self(ffi::GtkFileFilterInfo {
            contains: contains.bits(),
            filename: filename.to_glib_full(),
            uri: uri.to_glib_full(),
            display_name: display_name.to_glib_full(),
            mime_type: mime_type.to_glib_full(),
        })
    }

    #[doc(alias = "get_contains")]
    pub fn contains(&self) -> FileFilterFlags {
        FileFilterFlags::from_bits_truncate(self.0.contains)
    }

    #[doc(alias = "get_filename")]
    pub fn filename(&self) -> Option<&str> {
        unsafe {
            if self.0.filename.is_null() {
                None
            } else {
                Some(
                    CStr::from_ptr(self.0.filename)
                        .to_str()
                        .expect("filename was not valid UTF-8"),
                )
            }
        }
    }

    #[doc(alias = "get_uri")]
    pub fn uri(&self) -> Option<&str> {
        unsafe {
            if self.0.uri.is_null() {
                None
            } else {
                Some(
                    CStr::from_ptr(self.0.uri)
                        .to_str()
                        .expect("uri was not valid UTF-8"),
                )
            }
        }
    }

    #[doc(alias = "get_display_name")]
    pub fn display_name(&self) -> Option<&str> {
        unsafe {
            if self.0.display_name.is_null() {
                None
            } else {
                Some(
                    CStr::from_ptr(self.0.display_name)
                        .to_str()
                        .expect("display_name was not valid UTF-8"),
                )
            }
        }
    }

    #[doc(alias = "get_mime_type")]
    pub fn mime_type(&self) -> Option<&str> {
        unsafe {
            if self.0.mime_type.is_null() {
                None
            } else {
                Some(
                    CStr::from_ptr(self.0.mime_type)
                        .to_str()
                        .expect("mime_type was not valid UTF-8"),
                )
            }
        }
    }
}

impl Drop for FileFilterInfo {
    fn drop(&mut self) {
        for ptr in [
            self.0.filename,
            self.0.uri,
            self.0.display_name,
            self.0.mime_type,
        ] {
            unsafe {
                glib::ffi::g_free(ptr as *mut _);
            }
        }
    }
}

#[doc(hidden)]
impl FromGlibPtrNone<*const ffi::GtkFileFilterInfo> for FileFilterInfo {
    unsafe fn from_glib_none(ptr: *const ffi::GtkFileFilterInfo) -> Self {
        assert!(!ptr.is_null());
        Self(ffi::GtkFileFilterInfo {
            contains: (*ptr).contains,
            filename: glib::ffi::g_strdup((*ptr).filename),
            uri: glib::ffi::g_strdup((*ptr).uri),
            display_name: glib::ffi::g_strdup((*ptr).display_name),
            mime_type: glib::ffi::g_strdup((*ptr).mime_type),
        })
    }
}

#[doc(hidden)]
impl FromGlibPtrBorrow<*const ffi::GtkFileFilterInfo> for FileFilterInfo {
    unsafe fn from_glib_borrow(ptr: *const ffi::GtkFileFilterInfo) -> Borrowed<Self> {
        assert!(!ptr.is_null());
        Borrowed::new(FileFilterInfo(*ptr))
    }
}

#[doc(hidden)]
impl<'a> ToGlibPtr<'a, *const ffi::GtkFileFilterInfo> for FileFilterInfo {
    type Storage = (ffi::GtkFileFilterInfo, &'a FileFilterInfo);

    #[inline]
    fn to_glib_none(&'a self) -> Stash<'a, *const ffi::GtkFileFilterInfo, Self> {
        Stash(&self.0, (self.0, self))
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::TEST_THREAD_WORKER;

    const TEST_FILENAME: &str = "testfile.txt";
    const TEST_URI: &str = "file:///foo/bar/testfile.txt";
    const TEST_DISPLAY_NAME: &str = "testfile";
    const TEST_MIME_TYPE: &str = "text/plain";

    #[test]
    fn custom_filter() {
        TEST_THREAD_WORKER
            .push(|| {
                let _ = crate::init();

                let filter = crate::FileFilter::new();
                filter.add_custom(FileFilterFlags::all(), |filter_info| {
                    assert_eq![filter_info.filename(), Some(TEST_FILENAME)];
                    assert_eq![filter_info.uri(), Some(TEST_URI)];
                    assert_eq![filter_info.display_name(), Some(TEST_DISPLAY_NAME)];
                    assert_eq![filter_info.mime_type(), Some(TEST_MIME_TYPE)];
                    true
                });

                let filter_info = FileFilterInfo::new(
                    Some(TEST_FILENAME),
                    Some(TEST_URI),
                    Some(TEST_DISPLAY_NAME),
                    Some(TEST_MIME_TYPE),
                );
                filter.filter(&filter_info);
            })
            .expect("Failed to schedule a test call");
        while TEST_THREAD_WORKER.unprocessed() > 0 {}
    }
}