use crate::{ffi, Resource, SparqlConnection};
#[cfg(feature = "v3_6")]
#[cfg_attr(docsrs, doc(cfg(feature = "v3_6")))]
use crate::{DeserializeFlags, RdfFormat};
use glib::{prelude::*, translate::*};
use std::{boxed::Box as Box_, pin::Pin};
glib::wrapper! {
#[doc(alias = "TrackerBatch")]
pub struct Batch(Object<ffi::TrackerBatch, ffi::TrackerBatchClass>);
match fn {
type_ => || ffi::tracker_batch_get_type(),
}
}
impl Batch {
#[cfg(feature = "v3_6")]
#[cfg_attr(docsrs, doc(cfg(feature = "v3_6")))]
#[doc(alias = "tracker_batch_add_rdf")]
pub fn add_rdf(
&self,
flags: DeserializeFlags,
format: RdfFormat,
default_graph: &str,
stream: &impl IsA<gio::InputStream>,
) {
unsafe {
ffi::tracker_batch_add_rdf(
self.to_glib_none().0,
flags.into_glib(),
format.into_glib(),
default_graph.to_glib_none().0,
stream.as_ref().to_glib_none().0,
);
}
}
#[doc(alias = "tracker_batch_add_resource")]
pub fn add_resource(&self, graph: Option<&str>, resource: &Resource) {
unsafe {
ffi::tracker_batch_add_resource(
self.to_glib_none().0,
graph.to_glib_none().0,
resource.to_glib_none().0,
);
}
}
#[doc(alias = "tracker_batch_add_sparql")]
pub fn add_sparql(&self, sparql: &str) {
unsafe {
ffi::tracker_batch_add_sparql(self.to_glib_none().0, sparql.to_glib_none().0);
}
}
#[doc(alias = "tracker_batch_execute")]
pub fn execute(
&self,
cancellable: Option<&impl IsA<gio::Cancellable>>,
) -> Result<(), glib::Error> {
unsafe {
let mut error = std::ptr::null_mut();
let is_ok = ffi::tracker_batch_execute(
self.to_glib_none().0,
cancellable.map(|p| p.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 = "tracker_batch_execute_async")]
pub fn execute_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 execute_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::tracker_batch_execute_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 = execute_async_trampoline::<P>;
unsafe {
ffi::tracker_batch_execute_async(
self.to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
pub fn execute_future(
&self,
) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
obj.execute_async(Some(cancellable), move |res| {
send.resolve(res);
});
}))
}
#[doc(alias = "tracker_batch_get_connection")]
#[doc(alias = "get_connection")]
pub fn connection(&self) -> Option<SparqlConnection> {
unsafe { from_glib_none(ffi::tracker_batch_get_connection(self.to_glib_none().0)) }
}
#[cfg(not(feature = "v3_1"))]
#[cfg_attr(docsrs, doc(cfg(not(feature = "v3_1"))))]
pub fn connection(&self) -> Option<SparqlConnection> {
ObjectExt::property(self, "connection")
}
}