// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::{
    ffi, AsyncResult, Cancellable, Socket, SocketAddress, SocketConnection, SocketListenerEvent,
    SocketProtocol, SocketType,
};
use glib::{
    object::ObjectType as _,
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::{boxed::Box as Box_, pin::Pin};

glib::wrapper! {
    #[doc(alias = "GSocketListener")]
    pub struct SocketListener(Object<ffi::GSocketListener, ffi::GSocketListenerClass>);

    match fn {
        type_ => || ffi::g_socket_listener_get_type(),
    }
}

impl SocketListener {
    pub const NONE: Option<&'static SocketListener> = None;

    #[doc(alias = "g_socket_listener_new")]
    pub fn new() -> SocketListener {
        unsafe { from_glib_full(ffi::g_socket_listener_new()) }
    }
}

impl Default for SocketListener {
    fn default() -> Self {
        Self::new()
    }
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::SocketListener>> Sealed for T {}
}

pub trait SocketListenerExt: IsA<SocketListener> + sealed::Sealed + 'static {
    #[doc(alias = "g_socket_listener_accept")]
    fn accept(
        &self,
        cancellable: Option<&impl IsA<Cancellable>>,
    ) -> Result<(SocketConnection, Option<glib::Object>), glib::Error> {
        unsafe {
            let mut source_object = std::ptr::null_mut();
            let mut error = std::ptr::null_mut();
            let ret = ffi::g_socket_listener_accept(
                self.as_ref().to_glib_none().0,
                &mut source_object,
                cancellable.map(|p| p.as_ref()).to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok((from_glib_full(ret), from_glib_none(source_object)))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[doc(alias = "g_socket_listener_accept_async")]
    fn accept_async<
        P: FnOnce(Result<(SocketConnection, Option<glib::Object>), glib::Error>) + 'static,
    >(
        &self,
        cancellable: Option<&impl IsA<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 accept_async_trampoline<
            P: FnOnce(Result<(SocketConnection, Option<glib::Object>), glib::Error>) + 'static,
        >(
            _source_object: *mut glib::gobject_ffi::GObject,
            res: *mut crate::ffi::GAsyncResult,
            user_data: glib::ffi::gpointer,
        ) {
            let mut error = std::ptr::null_mut();
            let mut source_object = std::ptr::null_mut();
            let ret = ffi::g_socket_listener_accept_finish(
                _source_object as *mut _,
                res,
                &mut source_object,
                &mut error,
            );
            let result = if error.is_null() {
                Ok((from_glib_full(ret), from_glib_none(source_object)))
            } 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 = accept_async_trampoline::<P>;
        unsafe {
            ffi::g_socket_listener_accept_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 accept_future(
        &self,
    ) -> Pin<
        Box_<
            dyn std::future::Future<
                    Output = Result<(SocketConnection, Option<glib::Object>), glib::Error>,
                > + 'static,
        >,
    > {
        Box_::pin(crate::GioFuture::new(
            self,
            move |obj, cancellable, send| {
                obj.accept_async(Some(cancellable), move |res| {
                    send.resolve(res);
                });
            },
        ))
    }

    #[doc(alias = "g_socket_listener_accept_socket")]
    fn accept_socket(
        &self,
        cancellable: Option<&impl IsA<Cancellable>>,
    ) -> Result<(Socket, Option<glib::Object>), glib::Error> {
        unsafe {
            let mut source_object = std::ptr::null_mut();
            let mut error = std::ptr::null_mut();
            let ret = ffi::g_socket_listener_accept_socket(
                self.as_ref().to_glib_none().0,
                &mut source_object,
                cancellable.map(|p| p.as_ref()).to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok((from_glib_full(ret), from_glib_none(source_object)))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[doc(alias = "g_socket_listener_accept_socket_async")]
    fn accept_socket_async<
        P: FnOnce(Result<(Socket, Option<glib::Object>), glib::Error>) + 'static,
    >(
        &self,
        cancellable: Option<&impl IsA<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 accept_socket_async_trampoline<
            P: FnOnce(Result<(Socket, Option<glib::Object>), glib::Error>) + 'static,
        >(
            _source_object: *mut glib::gobject_ffi::GObject,
            res: *mut crate::ffi::GAsyncResult,
            user_data: glib::ffi::gpointer,
        ) {
            let mut error = std::ptr::null_mut();
            let mut source_object = std::ptr::null_mut();
            let ret = ffi::g_socket_listener_accept_socket_finish(
                _source_object as *mut _,
                res,
                &mut source_object,
                &mut error,
            );
            let result = if error.is_null() {
                Ok((from_glib_full(ret), from_glib_none(source_object)))
            } 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 = accept_socket_async_trampoline::<P>;
        unsafe {
            ffi::g_socket_listener_accept_socket_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 accept_socket_future(
        &self,
    ) -> Pin<
        Box_<
            dyn std::future::Future<Output = Result<(Socket, Option<glib::Object>), glib::Error>>
                + 'static,
        >,
    > {
        Box_::pin(crate::GioFuture::new(
            self,
            move |obj, cancellable, send| {
                obj.accept_socket_async(Some(cancellable), move |res| {
                    send.resolve(res);
                });
            },
        ))
    }

    #[doc(alias = "g_socket_listener_add_address")]
    fn add_address(
        &self,
        address: &impl IsA<SocketAddress>,
        type_: SocketType,
        protocol: SocketProtocol,
        source_object: Option<&impl IsA<glib::Object>>,
    ) -> Result<SocketAddress, glib::Error> {
        unsafe {
            let mut effective_address = std::ptr::null_mut();
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::g_socket_listener_add_address(
                self.as_ref().to_glib_none().0,
                address.as_ref().to_glib_none().0,
                type_.into_glib(),
                protocol.into_glib(),
                source_object.map(|p| p.as_ref()).to_glib_none().0,
                &mut effective_address,
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(from_glib_full(effective_address))
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[doc(alias = "g_socket_listener_add_any_inet_port")]
    fn add_any_inet_port(
        &self,
        source_object: Option<&impl IsA<glib::Object>>,
    ) -> Result<u16, glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let ret = ffi::g_socket_listener_add_any_inet_port(
                self.as_ref().to_glib_none().0,
                source_object.map(|p| p.as_ref()).to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(ret)
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[doc(alias = "g_socket_listener_add_inet_port")]
    fn add_inet_port(
        &self,
        port: u16,
        source_object: Option<&impl IsA<glib::Object>>,
    ) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::g_socket_listener_add_inet_port(
                self.as_ref().to_glib_none().0,
                port,
                source_object.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 = "g_socket_listener_add_socket")]
    fn add_socket(
        &self,
        socket: &impl IsA<Socket>,
        source_object: Option<&impl IsA<glib::Object>>,
    ) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::g_socket_listener_add_socket(
                self.as_ref().to_glib_none().0,
                socket.as_ref().to_glib_none().0,
                source_object.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 = "g_socket_listener_close")]
    fn close(&self) {
        unsafe {
            ffi::g_socket_listener_close(self.as_ref().to_glib_none().0);
        }
    }

    #[doc(alias = "g_socket_listener_set_backlog")]
    fn set_backlog(&self, listen_backlog: i32) {
        unsafe {
            ffi::g_socket_listener_set_backlog(self.as_ref().to_glib_none().0, listen_backlog);
        }
    }

    #[doc(alias = "listen-backlog")]
    fn listen_backlog(&self) -> i32 {
        ObjectExt::property(self.as_ref(), "listen-backlog")
    }

    #[doc(alias = "listen-backlog")]
    fn set_listen_backlog(&self, listen_backlog: i32) {
        ObjectExt::set_property(self.as_ref(), "listen-backlog", listen_backlog)
    }

    #[doc(alias = "event")]
    fn connect_event<F: Fn(&Self, SocketListenerEvent, &Socket) + 'static>(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn event_trampoline<
            P: IsA<SocketListener>,
            F: Fn(&P, SocketListenerEvent, &Socket) + 'static,
        >(
            this: *mut ffi::GSocketListener,
            event: ffi::GSocketListenerEvent,
            socket: *mut ffi::GSocket,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                SocketListener::from_glib_borrow(this).unsafe_cast_ref(),
                from_glib(event),
                &from_glib_borrow(socket),
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"event\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    event_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "listen-backlog")]
    fn connect_listen_backlog_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_listen_backlog_trampoline<
            P: IsA<SocketListener>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::GSocketListener,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(SocketListener::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::listen-backlog\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    notify_listen_backlog_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl<O: IsA<SocketListener>> SocketListenerExt for O {}
