libfprint_rs/
print.rs

1// All methods are declared
2use glib::{
3    object::ObjectExt,
4    translate::FromGlibPtrFull,
5    translate::{FromGlibContainer, FromGlibPtrNone, ToGlibPtr},
6    wrapper,
7};
8
9use crate::{device::FpDevice, finger::FpFinger, image::FpImage};
10
11wrapper! {
12    /// Struct representing a fingerprint.
13    pub struct FpPrint(Object<libfprint_sys::FpPrint, libfprint_sys::FpPrintClass>)
14        @extends glib::object::InitiallyUnowned;
15
16    match fn {
17        type_ => || libfprint_sys::fp_print_get_type() as usize,
18    }
19}
20
21impl FpPrint {
22    /// Create a new `FpPrint`. This is only useful to prepare an enrollment of a new print using `FpDevice::enroll_sync`.
23    /// For this you should first create a new print, fill in the relevant metadata, and then start the enrollment
24    pub fn new(dev: &FpDevice) -> Self {
25        unsafe {
26            let ptr = libfprint_sys::fp_print_new(dev.to_glib_none().0);
27            Self::from_glib_full(ptr)
28        }
29    }
30
31    /// Returns the driver that the print was created for.
32    pub fn driver(&self) -> String {
33        unsafe {
34            let ptr = libfprint_sys::fp_print_get_driver(self.to_glib_none().0);
35            String::from_glib_none(ptr)
36        }
37    }
38    /// Returns the device ID that the print was created for.
39    pub fn device_id(&self) -> String {
40        unsafe {
41            let ptr = libfprint_sys::fp_print_get_device_id(self.to_glib_none().0);
42            String::from_glib_none(ptr)
43        }
44    }
45    /// Whether the print is actually stored on the device and this is just a handle to use that references the device stored data.
46    pub fn device_stored(&self) -> bool {
47        unsafe {
48            libfprint_sys::fp_print_get_device_stored(self.to_glib_none().0) == glib::ffi::GTRUE
49        }
50    }
51    /// Returns the image that the print was created from, or None
52    pub fn image(&self) -> Option<FpImage> {
53        unsafe {
54            let ptr = libfprint_sys::fp_print_get_image(self.to_glib_none().0);
55            if ptr.is_null() {
56                None
57            } else {
58                Some(FpImage::from_glib_none(ptr))
59            }
60        }
61    }
62    /// Returns the finger that the print was created for.
63    pub fn finger(&self) -> FpFinger {
64        let raw_finger = unsafe { libfprint_sys::fp_print_get_finger(self.to_glib_none().0) };
65        FpFinger::from(raw_finger)
66    }
67    /// Returns the user defined username for the print.
68    pub fn username(&self) -> Option<String> {
69        unsafe {
70            let ptr = libfprint_sys::fp_print_get_username(self.to_glib_none().0);
71            if ptr.is_null() {
72                None
73            } else {
74                Some(String::from_glib_none(ptr))
75            }
76        }
77    }
78    /// Returns the user defined description for the print.
79    pub fn description(&self) -> Option<String> {
80        unsafe {
81            let ptr = libfprint_sys::fp_print_get_description(self.to_glib_none().0);
82            if ptr.is_null() {
83                None
84            } else {
85                Some(String::from_glib_none(ptr))
86            }
87        }
88    }
89    /// Returns the user defined enroll date for the print.
90    pub fn enroll_date(&self) -> Option<crate::GDate> {
91        unsafe {
92            let ptr = libfprint_sys::fp_print_get_enroll_date(self.to_glib_none().0);
93            if ptr.is_null() {
94                None
95            } else {
96                Some(glib::Date::from_glib_none(ptr.cast()))
97            }
98        }
99    }
100
101    /// Set the finger that the print is for.
102    pub fn set_finger(&self, finger: FpFinger) {
103        unsafe { libfprint_sys::fp_print_set_finger(self.to_glib_none().0, finger as u32) };
104    }
105    /// Set the username for the print.
106    pub fn set_username(&self, username: &str) {
107        unsafe {
108            libfprint_sys::fp_print_set_username(self.to_glib_none().0, username.to_glib_none().0);
109        }
110    }
111    /// Set the description for the print.
112    pub fn set_description(&self, description: &str) {
113        unsafe {
114            libfprint_sys::fp_print_set_description(
115                self.to_glib_none().0,
116                description.to_glib_none().0,
117            );
118        }
119    }
120
121    /// Set the enroll date for the print.
122    pub fn set_enroll_date(&self, enroll_date: crate::GDate) {
123        unsafe {
124            libfprint_sys::fp_print_set_enroll_date(
125                self.to_glib_none().0,
126                enroll_date.to_glib_none().0.cast(),
127            );
128        }
129    }
130    /// Tests whether the prints is compatible with the given device.
131    pub fn compatible(&self, device: &FpDevice) -> bool {
132        unsafe {
133            libfprint_sys::fp_print_compatible(self.to_glib_none().0, device.to_glib_none().0)
134                == glib::ffi::GTRUE
135        }
136    }
137    /// Tests whether the prints can be considered equal. This only compares the actual information about the print, not the metadata.
138    pub fn equal(&self, other: &FpPrint) -> bool {
139        unsafe {
140            libfprint_sys::fp_print_equal(self.to_glib_none().0, other.to_glib_none().0)
141                == glib::ffi::GTRUE
142        }
143    }
144    /// Serialize a print definition for permanent storage. Note that this is lossy in the sense that e.g. the image data is discarded.
145    pub fn serialize(&self) -> Result<Vec<u8>, glib::Error> {
146        unsafe {
147            let mut content = std::ptr::null_mut();
148            let mut len = 0;
149            let mut error = std::ptr::null_mut();
150
151            libfprint_sys::fp_print_serialize(
152                self.to_glib_none().0,
153                &mut content,
154                &mut len,
155                &mut error,
156            );
157
158            if error.is_null() {
159                Ok(Vec::from_glib_full_num(content, len as usize))
160            } else {
161                Err(glib::Error::from_glib_full(error.cast()))
162            }
163        }
164    }
165
166    /// Deserialize a print definition from permanent storage.
167    pub fn deserialize(data: &[u8]) -> Result<FpPrint, glib::Error> {
168        let len = data.len();
169        let ptr = unsafe {
170            let ptr = glib::translate::ToGlibPtr::to_glib_none(data);
171            let mut error = std::ptr::null_mut();
172
173            libfprint_sys::fp_print_deserialize(ptr.0, len.try_into().unwrap(), &mut error)
174        };
175
176        if ptr.is_null() {
177            Err(unsafe { glib::Error::from_glib_full(ptr.cast()) })
178        } else {
179            let print = unsafe { FpPrint::from_glib_full(ptr) };
180            unsafe { print.set_data("set", true) };
181            Ok(print)
182        }
183    }
184}