use windows::Win32::System::Com::{CoCreateInstance, CLSCTX_ALL};
use windows::core::{GUID, PCWSTR} ;
use windows::Win32::Devices::PortableDevices::{PortableDeviceFTM, IPortableDevice};
use widestring::U16CString;
use crate::device::device_values::AppIdentifiers;
pub mod device_values;
mod content;
pub use content::Content;
#[derive(Clone)]
pub struct BasicDevice {
device_id: U16CString,
friendly_name: String,
}
impl BasicDevice {
pub(crate) fn new(device_id: U16CString, friendly_name: String) -> Self {
Self{ device_id, friendly_name }
}
pub fn device_id(&self) -> String {
self.device_id.to_string_lossy() }
pub fn friendly_name(&self) -> &str {
&self.friendly_name
}
pub fn open(&self, app_identifiers: &AppIdentifiers, case_sensitive_fs: bool) -> crate::WindowsResult<Device> {
let device_values = device_values::make_values_for_open_device(app_identifiers)?;
let com_device: IPortableDevice = unsafe {
CoCreateInstance(
&PortableDeviceFTM as *const GUID,
None,
CLSCTX_ALL
)
}?;
unsafe { com_device.Open(PCWSTR::from_raw(self.device_id.as_ptr()), &device_values) }.unwrap();
Ok(Device{
com_device,
case_sensitive_fs,
})
}
}
pub struct Device {
com_device: IPortableDevice,
case_sensitive_fs: bool,
}
impl Device {
pub fn raw_device(&self) -> &IPortableDevice {
&self.com_device
}
pub fn content(&self) -> crate::WindowsResult<Content> {
let com_content = unsafe { self.com_device.Content() }?;
Ok(Content::new(com_content, self.case_sensitive_fs))
}
}