use crate::{Calendar, Manager, Organizer, Timeframe, ffi};
use glib::{
object::ObjectType as _,
prelude::*,
signal::{SignalHandlerId, connect_raw},
translate::*,
};
use std::{boxed::Box as Box_, pin::Pin};
glib::wrapper! {
#[doc(alias = "ClepsydreEvent")]
pub struct Event(Object<ffi::ClepsydreEvent, ffi::ClepsydreEventClass>);
match fn {
type_ => || ffi::clepsydre_event_get_type(),
}
}
impl Event {
pub const NONE: Option<&'static Event> = None;
}
pub trait EventExt: IsA<Event> + 'static {
#[doc(alias = "clepsydre_event_get_calendar")]
#[doc(alias = "get_calendar")]
fn calendar(&self) -> Option<Calendar> {
unsafe {
from_glib_none(ffi::clepsydre_event_get_calendar(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_event_get_conference")]
#[doc(alias = "get_conference")]
fn conference(&self) -> Option<glib::GString> {
unsafe {
from_glib_full(ffi::clepsydre_event_get_conference(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_event_get_description")]
#[doc(alias = "get_description")]
fn description(&self) -> Option<glib::GString> {
unsafe {
from_glib_full(ffi::clepsydre_event_get_description(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_event_get_editable")]
#[doc(alias = "get_editable")]
#[doc(alias = "editable")]
fn is_editable(&self) -> bool {
unsafe {
from_glib(ffi::clepsydre_event_get_editable(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_event_get_has_recurrence")]
#[doc(alias = "get_has_recurrence")]
#[doc(alias = "has-recurrence")]
fn has_recurrence(&self) -> bool {
unsafe {
from_glib(ffi::clepsydre_event_get_has_recurrence(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_event_get_location")]
#[doc(alias = "get_location")]
fn location(&self) -> Option<glib::GString> {
unsafe {
from_glib_full(ffi::clepsydre_event_get_location(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_event_get_manager")]
#[doc(alias = "get_manager")]
fn manager(&self) -> Option<Manager> {
unsafe {
from_glib_none(ffi::clepsydre_event_get_manager(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_event_get_name")]
#[doc(alias = "get_name")]
fn name(&self) -> Option<glib::GString> {
unsafe {
from_glib_full(ffi::clepsydre_event_get_name(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_event_get_organizer")]
#[doc(alias = "get_organizer")]
fn organizer(&self) -> Option<Organizer> {
unsafe {
from_glib_none(ffi::clepsydre_event_get_organizer(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_event_get_removable")]
#[doc(alias = "get_removable")]
#[doc(alias = "removable")]
fn is_removable(&self) -> bool {
unsafe {
from_glib(ffi::clepsydre_event_get_removable(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_event_get_timeframe")]
#[doc(alias = "get_timeframe")]
fn timeframe(&self) -> Option<Timeframe> {
unsafe {
from_glib_none(ffi::clepsydre_event_get_timeframe(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_event_get_uri")]
#[doc(alias = "get_uri")]
fn uri(&self) -> Option<glib::GString> {
unsafe { from_glib_full(ffi::clepsydre_event_get_uri(self.as_ref().to_glib_none().0)) }
}
#[doc(alias = "clepsydre_event_to_string_for_ics")]
fn to_string_for_ics(&self) -> Option<glib::GString> {
unsafe {
from_glib_full(ffi::clepsydre_event_to_string_for_ics(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_event_to_string_for_qr_code")]
fn to_string_for_qr_code(&self) -> Option<glib::GString> {
unsafe {
from_glib_full(ffi::clepsydre_event_to_string_for_qr_code(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_event_try_remove")]
fn try_remove(&self) -> Result<(), glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let is_ok = ffi::clepsydre_event_try_remove(self.as_ref().to_glib_none().0, &mut error);
debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
if error.is_null() {
Ok(())
} else {
Err(from_glib_full(error))
}
}
}
#[doc(alias = "clepsydre_event_try_remove_async")]
fn try_remove_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
&self,
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 try_remove_async_trampoline<
P: FnOnce(Result<(), 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();
ffi::clepsydre_event_try_remove_finish(_source_object as *mut _, res, &mut error);
let result = if error.is_null() {
Ok(())
} 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 = try_remove_async_trampoline::<P>;
unsafe {
ffi::clepsydre_event_try_remove_async(
self.as_ref().to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
fn try_remove_future(
&self,
) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
obj.try_remove_async(Some(cancellable), move |res| {
send.resolve(res);
});
}))
}
#[doc(alias = "clepsydre_event_try_update")]
fn try_update(
&self,
calendar: Option<&impl IsA<Calendar>>,
name: Option<&str>,
description: Option<&str>,
location: Option<&str>,
conference: Option<&str>,
timeframe: Option<&Timeframe>,
) -> Result<(), glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let is_ok = ffi::clepsydre_event_try_update(
self.as_ref().to_glib_none().0,
calendar.map(|p| p.as_ref()).to_glib_none().0,
name.to_glib_none().0,
description.to_glib_none().0,
location.to_glib_none().0,
conference.to_glib_none().0,
timeframe.to_glib_none().0,
&mut error,
);
debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
if error.is_null() {
Ok(())
} else {
Err(from_glib_full(error))
}
}
}
#[doc(alias = "clepsydre_event_try_update_async")]
fn try_update_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
&self,
calendar: Option<&impl IsA<Calendar>>,
name: Option<&str>,
description: Option<&str>,
location: Option<&str>,
conference: Option<&str>,
timeframe: Option<&Timeframe>,
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 try_update_async_trampoline<
P: FnOnce(Result<(), 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();
ffi::clepsydre_event_try_update_finish(_source_object as *mut _, res, &mut error);
let result = if error.is_null() {
Ok(())
} 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 = try_update_async_trampoline::<P>;
unsafe {
ffi::clepsydre_event_try_update_async(
self.as_ref().to_glib_none().0,
calendar.map(|p| p.as_ref()).to_glib_none().0,
name.to_glib_none().0,
description.to_glib_none().0,
location.to_glib_none().0,
conference.to_glib_none().0,
timeframe.to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
fn try_update_future(
&self,
calendar: Option<&(impl IsA<Calendar> + Clone + 'static)>,
name: Option<&str>,
description: Option<&str>,
location: Option<&str>,
conference: Option<&str>,
timeframe: Option<&Timeframe>,
) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
let calendar = calendar.map(ToOwned::to_owned);
let name = name.map(ToOwned::to_owned);
let description = description.map(ToOwned::to_owned);
let location = location.map(ToOwned::to_owned);
let conference = conference.map(ToOwned::to_owned);
let timeframe = timeframe.map(ToOwned::to_owned);
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
obj.try_update_async(
calendar.as_ref().map(::std::borrow::Borrow::borrow),
name.as_ref().map(::std::borrow::Borrow::borrow),
description.as_ref().map(::std::borrow::Borrow::borrow),
location.as_ref().map(::std::borrow::Borrow::borrow),
conference.as_ref().map(::std::borrow::Borrow::borrow),
timeframe.as_ref().map(::std::borrow::Borrow::borrow),
Some(cancellable),
move |res| {
send.resolve(res);
},
);
}))
}
fn attendees(&self) -> Option<gio::ListModel> {
ObjectExt::property(self.as_ref(), "attendees")
}
#[doc(alias = "qr-code-url")]
fn qr_code_url(&self) -> Option<glib::GString> {
ObjectExt::property(self.as_ref(), "qr-code-url")
}
#[doc(alias = "removed")]
fn connect_removed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn removed_trampoline<P: IsA<Event>, F: Fn(&P) + 'static>(
this: *mut ffi::ClepsydreEvent,
f: glib::ffi::gpointer,
) {
unsafe {
let f: &F = &*(f as *const F);
f(Event::from_glib_borrow(this).unsafe_cast_ref())
}
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"removed".as_ptr(),
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
removed_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
#[doc(alias = "qr-code-url")]
fn connect_qr_code_url_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn notify_qr_code_url_trampoline<P: IsA<Event>, F: Fn(&P) + 'static>(
this: *mut ffi::ClepsydreEvent,
_param_spec: glib::ffi::gpointer,
f: glib::ffi::gpointer,
) {
unsafe {
let f: &F = &*(f as *const F);
f(Event::from_glib_borrow(this).unsafe_cast_ref())
}
}
unsafe {
let f: Box_<F> = Box_::new(f);
connect_raw(
self.as_ptr() as *mut _,
c"notify::qr-code-url".as_ptr(),
Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
notify_qr_code_url_trampoline::<Self, F> as *const (),
)),
Box_::into_raw(f),
)
}
}
}
impl<O: IsA<Event>> EventExt for O {}