use crate::{Collection, Event, Manager, 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 = "ClepsydreCalendar")]
pub struct Calendar(Object<ffi::ClepsydreCalendar, ffi::ClepsydreCalendarClass>);
match fn {
type_ => || ffi::clepsydre_calendar_get_type(),
}
}
impl Calendar {
pub const NONE: Option<&'static Calendar> = None;
}
pub trait CalendarExt: IsA<Calendar> + 'static {
#[doc(alias = "clepsydre_calendar_get_collection")]
#[doc(alias = "get_collection")]
fn collection(&self) -> Option<Collection> {
unsafe {
from_glib_none(ffi::clepsydre_calendar_get_collection(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_calendar_get_color")]
#[doc(alias = "get_color")]
fn color(&self) -> Option<gdk::RGBA> {
unsafe {
from_glib_full(ffi::clepsydre_calendar_get_color(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_calendar_get_event_creation_enabled")]
#[doc(alias = "get_event_creation_enabled")]
#[doc(alias = "event-creation-enabled")]
fn is_event_creation_enabled(&self) -> bool {
unsafe {
from_glib(ffi::clepsydre_calendar_get_event_creation_enabled(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_calendar_get_manager")]
#[doc(alias = "get_manager")]
fn manager(&self) -> Option<Manager> {
unsafe {
from_glib_none(ffi::clepsydre_calendar_get_manager(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_calendar_get_name")]
#[doc(alias = "get_name")]
fn name(&self) -> Option<glib::GString> {
unsafe {
from_glib_full(ffi::clepsydre_calendar_get_name(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_calendar_get_removable")]
#[doc(alias = "get_removable")]
#[doc(alias = "removable")]
fn is_removable(&self) -> bool {
unsafe {
from_glib(ffi::clepsydre_calendar_get_removable(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_calendar_get_uri")]
#[doc(alias = "get_uri")]
fn uri(&self) -> Option<glib::GString> {
unsafe {
from_glib_full(ffi::clepsydre_calendar_get_uri(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_calendar_get_visible")]
#[doc(alias = "get_visible")]
#[doc(alias = "visible")]
fn is_visible(&self) -> bool {
unsafe {
from_glib(ffi::clepsydre_calendar_get_visible(
self.as_ref().to_glib_none().0,
))
}
}
#[doc(alias = "clepsydre_calendar_try_create_event")]
fn try_create_event(
&self,
name: &str,
description: &str,
location: &str,
conference: &str,
timeframe: &Timeframe,
) -> Result<Event, glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let ret = ffi::clepsydre_calendar_try_create_event(
self.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,
);
if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
}
}
}
#[doc(alias = "clepsydre_calendar_try_create_event_async")]
fn try_create_event_async<P: FnOnce(Result<Event, glib::Error>) + 'static>(
&self,
name: &str,
description: &str,
location: &str,
conference: &str,
timeframe: &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_create_event_async_trampoline<
P: FnOnce(Result<Event, 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_calendar_try_create_event_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 = try_create_event_async_trampoline::<P>;
unsafe {
ffi::clepsydre_calendar_try_create_event_async(
self.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_create_event_future(
&self,
name: &str,
description: &str,
location: &str,
conference: &str,
timeframe: &Timeframe,
) -> Pin<Box_<dyn std::future::Future<Output = Result<Event, glib::Error>> + 'static>> {
let name = String::from(name);
let description = String::from(description);
let location = String::from(location);
let conference = String::from(conference);
let timeframe = timeframe.clone();
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
obj.try_create_event_async(
&name,
&description,
&location,
&conference,
&timeframe,
Some(cancellable),
move |res| {
send.resolve(res);
},
);
}))
}
#[doc(alias = "clepsydre_calendar_try_remove")]
fn try_remove(&self) -> Result<(), glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let is_ok =
ffi::clepsydre_calendar_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_calendar_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_calendar_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_calendar_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_calendar_try_set_color")]
fn try_set_color(&self, color: &gdk::RGBA) -> Result<(), glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let is_ok = ffi::clepsydre_calendar_try_set_color(
self.as_ref().to_glib_none().0,
color.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_calendar_try_set_color_async")]
fn try_set_color_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
&self,
color: &gdk::RGBA,
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_set_color_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_calendar_try_set_color_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_set_color_async_trampoline::<P>;
unsafe {
ffi::clepsydre_calendar_try_set_color_async(
self.as_ref().to_glib_none().0,
color.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_set_color_future(
&self,
color: &gdk::RGBA,
) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
let color = color.clone();
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
obj.try_set_color_async(&color, Some(cancellable), move |res| {
send.resolve(res);
});
}))
}
#[doc(alias = "clepsydre_calendar_try_set_name")]
fn try_set_name(&self, name: &str) -> Result<(), glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let is_ok = ffi::clepsydre_calendar_try_set_name(
self.as_ref().to_glib_none().0,
name.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_calendar_try_set_name_async")]
fn try_set_name_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
&self,
name: &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 try_set_name_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_calendar_try_set_name_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_set_name_async_trampoline::<P>;
unsafe {
ffi::clepsydre_calendar_try_set_name_async(
self.as_ref().to_glib_none().0,
name.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_set_name_future(
&self,
name: &str,
) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
let name = String::from(name);
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
obj.try_set_name_async(&name, Some(cancellable), move |res| {
send.resolve(res);
});
}))
}
#[doc(alias = "clepsydre_calendar_try_set_visible")]
fn try_set_visible(&self, visible: bool) -> Result<(), glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let is_ok = ffi::clepsydre_calendar_try_set_visible(
self.as_ref().to_glib_none().0,
visible.into_glib(),
&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_calendar_try_set_visible_async")]
fn try_set_visible_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
&self,
visible: bool,
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_set_visible_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_calendar_try_set_visible_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_set_visible_async_trampoline::<P>;
unsafe {
ffi::clepsydre_calendar_try_set_visible_async(
self.as_ref().to_glib_none().0,
visible.into_glib(),
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
fn try_set_visible_future(
&self,
visible: bool,
) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
obj.try_set_visible_async(visible, Some(cancellable), move |res| {
send.resolve(res);
});
}))
}
#[doc(alias = "removed")]
fn connect_removed<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
unsafe extern "C" fn removed_trampoline<P: IsA<Calendar>, F: Fn(&P) + 'static>(
this: *mut ffi::ClepsydreCalendar,
f: glib::ffi::gpointer,
) {
unsafe {
let f: &F = &*(f as *const F);
f(Calendar::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),
)
}
}
}
impl<O: IsA<Calendar>> CalendarExt for O {}