use crate::{ffi,CountryInfo,MobileProvider};
use glib::{prelude::*,translate::*};
use std::{boxed::Box as Box_,pin::Pin};
#[cfg(feature = "gio_v2_22")]
#[cfg_attr(docsrs, doc(cfg(feature = "gio_v2_22")))]
glib::wrapper! {
#[doc(alias = "NMAMobileProvidersDatabase")]
pub struct MobileProvidersDatabase(Object<ffi::NMAMobileProvidersDatabase, ffi::NMAMobileProvidersDatabaseClass>) @implements gio::AsyncInitable, gio::Initable;
match fn {
type_ => || ffi::nma_mobile_providers_database_get_type(),
}
}
#[cfg(not(any(feature = "gio_v2_22")))]
#[cfg(feature = "gio_v2_22")]
glib::wrapper! {
#[doc(alias = "NMAMobileProvidersDatabase")]
pub struct MobileProvidersDatabase(Object<ffi::NMAMobileProvidersDatabase, ffi::NMAMobileProvidersDatabaseClass>) @implements gio::Initable;
match fn {
type_ => || ffi::nma_mobile_providers_database_get_type(),
}
}
#[cfg(not(any(feature = "gio_v2_22")))]
glib::wrapper! {
#[doc(alias = "NMAMobileProvidersDatabase")]
pub struct MobileProvidersDatabase(Object<ffi::NMAMobileProvidersDatabase, ffi::NMAMobileProvidersDatabaseClass>);
match fn {
type_ => || ffi::nma_mobile_providers_database_get_type(),
}
}
impl MobileProvidersDatabase {
pub const NONE: Option<&'static MobileProvidersDatabase> = None;
#[doc(alias = "nma_mobile_providers_database_new_sync")]
pub fn new_sync(country_codes: Option<&str>, service_providers: Option<&str>, cancellable: Option<&impl IsA<gio::Cancellable>>) -> Result<MobileProvidersDatabase, glib::Error> {
assert_initialized_main_thread!();
unsafe {
let mut error = std::ptr::null_mut();
let ret = ffi::nma_mobile_providers_database_new_sync(country_codes.to_glib_none().0, service_providers.to_glib_none().0, cancellable.map(|p| p.as_ref()).to_glib_none().0, &mut error);
if error.is_null() { Ok(from_glib_full(ret)) } else { Err(from_glib_full(error)) }
}
}
pub fn builder() -> MobileProvidersDatabaseBuilder {
MobileProvidersDatabaseBuilder::new()
}
#[doc(alias = "nma_mobile_providers_database_new")]
pub fn new<P: FnOnce(Result<MobileProvidersDatabase, glib::Error>) + 'static>(country_codes: Option<&str>, service_providers: Option<&str>, cancellable: Option<&impl IsA<gio::Cancellable>>, callback: P) {
assert_initialized_main_thread!();
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 new_trampoline<P: FnOnce(Result<MobileProvidersDatabase, glib::Error>) + 'static>(_source_object: *mut glib::gobject_ffi::GObject, res: *mut gio::ffi::GAsyncResult, user_data: glib::ffi::gpointer) {
let mut error = std::ptr::null_mut();
let ret = ffi::nma_mobile_providers_database_new_finish(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 = new_trampoline::<P>;
unsafe {
ffi::nma_mobile_providers_database_new(country_codes.to_glib_none().0, service_providers.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 new_future(country_codes: Option<&str>, service_providers: Option<&str>) -> Pin<Box_<dyn std::future::Future<Output = Result<MobileProvidersDatabase, glib::Error>> + 'static>> {
skip_assert_initialized!();
let country_codes = country_codes.map(ToOwned::to_owned);
let service_providers = service_providers.map(ToOwned::to_owned);
Box_::pin(gio::GioFuture::new(&(), move |_obj, cancellable, send| {
Self::new(
country_codes.as_ref().map(::std::borrow::Borrow::borrow),
service_providers.as_ref().map(::std::borrow::Borrow::borrow),
Some(cancellable),
move |res| {
send.resolve(res);
},
);
}))
}
}
#[must_use = "The builder must be built to be used"]
pub struct MobileProvidersDatabaseBuilder {
builder: glib::object::ObjectBuilder<'static, MobileProvidersDatabase>,
}
impl MobileProvidersDatabaseBuilder {
fn new() -> Self {
Self { builder: glib::object::Object::builder() }
}
pub fn country_codes(self, country_codes: impl Into<glib::GString>) -> Self {
Self { builder: self.builder.property("country-codes", country_codes.into()), }
}
pub fn service_providers(self, service_providers: impl Into<glib::GString>) -> Self {
Self { builder: self.builder.property("service-providers", service_providers.into()), }
}
#[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
pub fn build(self) -> MobileProvidersDatabase {
assert_initialized_main_thread!();
self.builder.build() }
}
pub trait MobileProvidersDatabaseExt: IsA<MobileProvidersDatabase> + 'static {
#[doc(alias = "nma_mobile_providers_database_dump")]
fn dump(&self) {
unsafe {
ffi::nma_mobile_providers_database_dump(self.as_ref().to_glib_none().0);
}
}
#[doc(alias = "nma_mobile_providers_database_lookup_3gpp_mcc_mnc")]
fn lookup_3gpp_mcc_mnc(&self, mccmnc: &str) -> MobileProvider {
unsafe {
from_glib_none(ffi::nma_mobile_providers_database_lookup_3gpp_mcc_mnc(self.as_ref().to_glib_none().0, mccmnc.to_glib_none().0))
}
}
#[doc(alias = "nma_mobile_providers_database_lookup_cdma_sid")]
fn lookup_cdma_sid(&self, sid: u32) -> MobileProvider {
unsafe {
from_glib_none(ffi::nma_mobile_providers_database_lookup_cdma_sid(self.as_ref().to_glib_none().0, sid))
}
}
#[doc(alias = "nma_mobile_providers_database_lookup_country")]
fn lookup_country(&self, country_code: &str) -> CountryInfo {
unsafe {
from_glib_none(ffi::nma_mobile_providers_database_lookup_country(self.as_ref().to_glib_none().0, country_code.to_glib_none().0))
}
}
#[doc(alias = "country-codes")]
fn country_codes(&self) -> Option<glib::GString> {
ObjectExt::property(self.as_ref(), "country-codes")
}
#[doc(alias = "service-providers")]
fn service_providers(&self) -> Option<glib::GString> {
ObjectExt::property(self.as_ref(), "service-providers")
}
}
impl<O: IsA<MobileProvidersDatabase>> MobileProvidersDatabaseExt for O {}