use crate::{IndexMapModel, Subscription, Timeframe, ffi};
use glib::{
prelude::*,
signal::{SignalHandlerId, connect_raw},
translate::*,
};
use std::{boxed::Box as Box_, pin::Pin};
glib::wrapper! {
#[doc(alias = "ClepsydreManager")]
pub struct Manager(Object<ffi::ClepsydreManager, ffi::ClepsydreManagerClass>);
match fn {
type_ => || ffi::clepsydre_manager_get_type(),
}
}
impl Manager {
pub const NONE: Option<&'static Manager> = None;
}
pub trait ManagerExt: IsA<Manager> + 'static {
#[doc(alias = "clepsydre_manager_get_backend_available")]
#[doc(alias = "get_backend_available")]
#[doc(alias = "backend-available")]
fn is_backend_available(&self) -> bool {
unsafe {
from_glib(ffi::clepsydre_manager_get_backend_available(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_manager_get_calendars_model")]
#[doc(alias = "get_calendars_model")]
#[doc(alias = "calendars-model")]
fn calendars_model(&self) -> Option<IndexMapModel> {
unsafe {
from_glib_none(ffi::clepsydre_manager_get_calendars_model(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_manager_get_collections_model")]
#[doc(alias = "get_collections_model")]
#[doc(alias = "collections-model")]
fn collections_model(&self) -> Option<IndexMapModel> {
unsafe {
from_glib_none(ffi::clepsydre_manager_get_collections_model(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_manager_new_subscription")]
fn new_subscription(&self, timeframe: &Timeframe) -> Result<Option<Subscription>, glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let ret = ffi::clepsydre_manager_new_subscription(
self.as_ref().to_glib_none().0,
timeframe.to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
}
}
}
#[doc(alias = "clepsydre_manager_search_events")]
fn search_events(&self, search: &str) -> Result<Option<gio::ListModel>, glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let ret = ffi::clepsydre_manager_search_events(
self.as_ref().to_glib_none().0,
search.to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
}
}
}
#[doc(alias = "clepsydre_manager_search_events_async")]
fn search_events_async<P: FnOnce(Result<Option<gio::ListModel>, glib::Error>) + 'static>(
&self,
search: &str,
cancellable: Option<&impl IsA<gio::Cancellable>>,
callback: P,
) {
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn search_events_async_trampoline<
P: FnOnce(Result<Option<gio::ListModel>, glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut gio::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
unsafe {
let mut error = std::ptr::null_mut();
let ret = ffi::clepsydre_manager_search_events_finish(
_source_object as *mut _,
res,
&mut error,
);
let result = if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback: P = callback.into_inner();
callback(result);
}
}
let callback = search_events_async_trampoline::<P>;
unsafe {
ffi::clepsydre_manager_search_events_async(
self.as_ref().to_glib_none().0,
search.to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
fn search_events_future(
&self,
search: &str,
) -> Pin<
Box_<
dyn std::future::Future<Output = Result<Option<gio::ListModel>, glib::Error>> + 'static,
>,
> {
let search = String::from(search);
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
obj.search_events_async(&search, Some(cancellable), move |res| {
send.resolve(res);
});
}))
}
#[doc(alias = "backend-available")]
fn connect_backend_available_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_backend_available_trampoline<
P: IsA<Manager>,
F: Fn(&P) + 'static,
>(
this: *mut ffi::ClepsydreManager,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
unsafe {
let f: &F = &*(f as *const F);
f(Manager::from_glib_borrow(this).unsafe_cast_ref())
}
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"notify::backend-available".as_ptr(),
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
notify_backend_available_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[doc(alias = "calendars-model")]
fn connect_calendars_model_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_calendars_model_trampoline<
P: IsA<Manager>,
F: Fn(&P) + 'static,
>(
this: *mut ffi::ClepsydreManager,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
unsafe {
let f: &F = &*(f as *const F);
f(Manager::from_glib_borrow(this).unsafe_cast_ref())
}
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"notify::calendars-model".as_ptr(),
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
notify_calendars_model_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[doc(alias = "collections-model")]
fn connect_collections_model_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_collections_model_trampoline<
P: IsA<Manager>,
F: Fn(&P) + 'static,
>(
this: *mut ffi::ClepsydreManager,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
unsafe {
let f: &F = &*(f as *const F);
f(Manager::from_glib_borrow(this).unsafe_cast_ref())
}
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"notify::collections-model".as_ptr(),
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
notify_collections_model_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
}
impl<O: IsA<Manager>> ManagerExt for O {}