use std::{
ffi::{CStr, CString},
str::FromStr,
};
use glib::translate::from_glib_full;
use crate::Shortcut;
pub trait ShortcutManualExt {
fn new(property_name: &str, shortcut: &str, command: &str, startup_notify: bool) -> Self;
fn property_name(&self) -> &str;
fn shortcut(&self) -> &str;
fn command(&self) -> &str;
fn snotify(&self) -> bool;
}
impl ShortcutManualExt for Shortcut {
fn new(property_name: &str, shortcut: &str, command: &str, startup_notify: bool) -> Self {
let ptr = unsafe { glib::ffi::g_slice_alloc0(std::mem::size_of::<ffi::XfceShortcut>()) }
as *mut ffi::XfceShortcut;
unsafe {
(*ptr).property_name =
glib::ffi::g_strdup(CString::new(property_name).unwrap().as_ptr());
(*ptr).shortcut = glib::ffi::g_strdup(CString::new(shortcut).unwrap().as_ptr());
(*ptr).command = glib::ffi::g_strdup(CString::new(command).unwrap().as_ptr());
(*ptr).snotify = if startup_notify { 1 } else { 0 };
}
unsafe { from_glib_full(ptr) }
}
fn property_name(&self) -> &str {
let ptr = unsafe { (*self.as_ptr()).property_name };
unsafe { CStr::from_ptr(ptr).to_str().unwrap() }
}
fn shortcut(&self) -> &str {
let ptr = unsafe { (*self.as_ptr()).shortcut };
unsafe { CStr::from_ptr(ptr).to_str().unwrap() }
}
fn command(&self) -> &str {
let ptr = unsafe { (*self.as_ptr()).command };
unsafe { CStr::from_ptr(ptr).to_str().unwrap() }
}
fn snotify(&self) -> bool {
unsafe { (*self.as_ptr()).snotify != 0 }
}
}