{- |
Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
License    : LGPL-2.1
Maintainer : Iñaki García Etxebarria (garetxe@gmail.com)

'GI.Gio.Objects.SocketAddress.SocketAddress' is the equivalent of struct sockaddr in the BSD
sockets API. This is an abstract class; use 'GI.Gio.Objects.InetSocketAddress.InetSocketAddress'
for internet sockets, or 'GI.Gio.Objects.UnixSocketAddress.UnixSocketAddress' for UNIX domain sockets.
-}

module GI.Gio.Objects.SocketAddress
    ( 

-- * Exported types
    SocketAddress(..)                       ,
    IsSocketAddress                         ,
    toSocketAddress                         ,
    noSocketAddress                         ,


 -- * Methods
-- ** getFamily #method:getFamily#
    SocketAddressGetFamilyMethodInfo        ,
    socketAddressGetFamily                  ,


-- ** getNativeSize #method:getNativeSize#
    SocketAddressGetNativeSizeMethodInfo    ,
    socketAddressGetNativeSize              ,


-- ** newFromNative #method:newFromNative#
    socketAddressNewFromNative              ,


-- ** toNative #method:toNative#
    SocketAddressToNativeMethodInfo         ,
    socketAddressToNative                   ,




 -- * Properties
-- ** family #attr:family#
    SocketAddressFamilyPropertyInfo         ,
    getSocketAddressFamily                  ,
    socketAddressFamily                     ,




    ) where

import Data.GI.Base.ShortPrelude
import qualified Data.GI.Base.ShortPrelude as SP
import qualified Data.GI.Base.Overloading as O
import qualified Prelude as P

import qualified Data.GI.Base.Attributes as GI.Attributes
import qualified Data.GI.Base.ManagedPtr as B.ManagedPtr
import qualified Data.GI.Base.GError as B.GError
import qualified Data.GI.Base.GVariant as B.GVariant
import qualified Data.GI.Base.GParamSpec as B.GParamSpec
import qualified Data.GI.Base.CallStack as B.CallStack
import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map
import qualified Foreign.Ptr as FP

import qualified GI.GObject.Objects.Object as GObject.Object
import {-# SOURCE #-} qualified GI.Gio.Enums as Gio.Enums
import {-# SOURCE #-} qualified GI.Gio.Interfaces.SocketConnectable as Gio.SocketConnectable

newtype SocketAddress = SocketAddress (ManagedPtr SocketAddress)
foreign import ccall "g_socket_address_get_type"
    c_g_socket_address_get_type :: IO GType

instance GObject SocketAddress where
    gobjectType _ = c_g_socket_address_get_type
    

class GObject o => IsSocketAddress o
#if MIN_VERSION_base(4,9,0)
instance {-# OVERLAPPABLE #-} (GObject a, O.UnknownAncestorError SocketAddress a) =>
    IsSocketAddress a
#endif
instance IsSocketAddress SocketAddress
instance GObject.Object.IsObject SocketAddress
instance Gio.SocketConnectable.IsSocketConnectable SocketAddress

toSocketAddress :: IsSocketAddress o => o -> IO SocketAddress
toSocketAddress = unsafeCastTo SocketAddress

noSocketAddress :: Maybe SocketAddress
noSocketAddress = Nothing

type family ResolveSocketAddressMethod (t :: Symbol) (o :: *) :: * where
    ResolveSocketAddressMethod "bindProperty" o = GObject.Object.ObjectBindPropertyMethodInfo
    ResolveSocketAddressMethod "bindPropertyFull" o = GObject.Object.ObjectBindPropertyFullMethodInfo
    ResolveSocketAddressMethod "enumerate" o = Gio.SocketConnectable.SocketConnectableEnumerateMethodInfo
    ResolveSocketAddressMethod "forceFloating" o = GObject.Object.ObjectForceFloatingMethodInfo
    ResolveSocketAddressMethod "freezeNotify" o = GObject.Object.ObjectFreezeNotifyMethodInfo
    ResolveSocketAddressMethod "isFloating" o = GObject.Object.ObjectIsFloatingMethodInfo
    ResolveSocketAddressMethod "notify" o = GObject.Object.ObjectNotifyMethodInfo
    ResolveSocketAddressMethod "notifyByPspec" o = GObject.Object.ObjectNotifyByPspecMethodInfo
    ResolveSocketAddressMethod "proxyEnumerate" o = Gio.SocketConnectable.SocketConnectableProxyEnumerateMethodInfo
    ResolveSocketAddressMethod "ref" o = GObject.Object.ObjectRefMethodInfo
    ResolveSocketAddressMethod "refSink" o = GObject.Object.ObjectRefSinkMethodInfo
    ResolveSocketAddressMethod "replaceData" o = GObject.Object.ObjectReplaceDataMethodInfo
    ResolveSocketAddressMethod "replaceQdata" o = GObject.Object.ObjectReplaceQdataMethodInfo
    ResolveSocketAddressMethod "runDispose" o = GObject.Object.ObjectRunDisposeMethodInfo
    ResolveSocketAddressMethod "stealData" o = GObject.Object.ObjectStealDataMethodInfo
    ResolveSocketAddressMethod "stealQdata" o = GObject.Object.ObjectStealQdataMethodInfo
    ResolveSocketAddressMethod "thawNotify" o = GObject.Object.ObjectThawNotifyMethodInfo
    ResolveSocketAddressMethod "toNative" o = SocketAddressToNativeMethodInfo
    ResolveSocketAddressMethod "toString" o = Gio.SocketConnectable.SocketConnectableToStringMethodInfo
    ResolveSocketAddressMethod "unref" o = GObject.Object.ObjectUnrefMethodInfo
    ResolveSocketAddressMethod "watchClosure" o = GObject.Object.ObjectWatchClosureMethodInfo
    ResolveSocketAddressMethod "getData" o = GObject.Object.ObjectGetDataMethodInfo
    ResolveSocketAddressMethod "getFamily" o = SocketAddressGetFamilyMethodInfo
    ResolveSocketAddressMethod "getNativeSize" o = SocketAddressGetNativeSizeMethodInfo
    ResolveSocketAddressMethod "getProperty" o = GObject.Object.ObjectGetPropertyMethodInfo
    ResolveSocketAddressMethod "getQdata" o = GObject.Object.ObjectGetQdataMethodInfo
    ResolveSocketAddressMethod "setData" o = GObject.Object.ObjectSetDataMethodInfo
    ResolveSocketAddressMethod "setProperty" o = GObject.Object.ObjectSetPropertyMethodInfo
    ResolveSocketAddressMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolveSocketAddressMethod t SocketAddress, O.MethodInfo info SocketAddress p) => O.IsLabelProxy t (SocketAddress -> p) where
    fromLabelProxy _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)

#if MIN_VERSION_base(4,9,0)
instance (info ~ ResolveSocketAddressMethod t SocketAddress, O.MethodInfo info SocketAddress p) => O.IsLabel t (SocketAddress -> p) where
    fromLabel _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#endif

-- VVV Prop "family"
   -- Type: TInterface (Name {namespace = "Gio", name = "SocketFamily"})
   -- Flags: [PropertyReadable]
   -- Nullable: (Just False,Nothing)

getSocketAddressFamily :: (MonadIO m, IsSocketAddress o) => o -> m Gio.Enums.SocketFamily
getSocketAddressFamily obj = liftIO $ getObjectPropertyEnum obj "family"

data SocketAddressFamilyPropertyInfo
instance AttrInfo SocketAddressFamilyPropertyInfo where
    type AttrAllowedOps SocketAddressFamilyPropertyInfo = '[ 'AttrGet]
    type AttrSetTypeConstraint SocketAddressFamilyPropertyInfo = (~) ()
    type AttrBaseTypeConstraint SocketAddressFamilyPropertyInfo = IsSocketAddress
    type AttrGetType SocketAddressFamilyPropertyInfo = Gio.Enums.SocketFamily
    type AttrLabel SocketAddressFamilyPropertyInfo = "family"
    type AttrOrigin SocketAddressFamilyPropertyInfo = SocketAddress
    attrGet _ = getSocketAddressFamily
    attrSet _ = undefined
    attrConstruct _ = undefined
    attrClear _ = undefined

instance O.HasAttributeList SocketAddress
type instance O.AttributeList SocketAddress = SocketAddressAttributeList
type SocketAddressAttributeList = ('[ '("family", SocketAddressFamilyPropertyInfo)] :: [(Symbol, *)])

socketAddressFamily :: AttrLabelProxy "family"
socketAddressFamily = AttrLabelProxy

type instance O.SignalList SocketAddress = SocketAddressSignalList
type SocketAddressSignalList = ('[ '("notify", GObject.Object.ObjectNotifySignalInfo)] :: [(Symbol, *)])

-- method SocketAddress::new_from_native
-- method type : Constructor
-- Args : [Arg {argCName = "native", argType = TBasicType TPtr, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a pointer to a struct sockaddr", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "len", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the size of the memory location pointed to by @native", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Gio", name = "SocketAddress"}))
-- throws : False
-- Skip return : False

foreign import ccall "g_socket_address_new_from_native" g_socket_address_new_from_native :: 
    Ptr () ->                               -- native : TBasicType TPtr
    Word64 ->                               -- len : TBasicType TUInt64
    IO (Ptr SocketAddress)

{- |
Creates a 'GI.Gio.Objects.SocketAddress.SocketAddress' subclass corresponding to the native
struct sockaddr /@native@/.

@since 2.22
-}
socketAddressNewFromNative ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Ptr ()
    {- ^ /@native@/: a pointer to a struct sockaddr -}
    -> Word64
    {- ^ /@len@/: the size of the memory location pointed to by /@native@/ -}
    -> m SocketAddress
    {- ^ __Returns:__ a new 'GI.Gio.Objects.SocketAddress.SocketAddress' if /@native@/ could successfully
    be converted, otherwise 'Nothing' -}
socketAddressNewFromNative native len = liftIO $ do
    result <- g_socket_address_new_from_native native len
    checkUnexpectedReturnNULL "socketAddressNewFromNative" result
    result' <- (wrapObject SocketAddress) result
    return result'

-- method SocketAddress::get_family
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "address", argType = TInterface (Name {namespace = "Gio", name = "SocketAddress"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GSocketAddress", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Gio", name = "SocketFamily"}))
-- throws : False
-- Skip return : False

foreign import ccall "g_socket_address_get_family" g_socket_address_get_family :: 
    Ptr SocketAddress ->                    -- address : TInterface (Name {namespace = "Gio", name = "SocketAddress"})
    IO CUInt

{- |
Gets the socket family type of /@address@/.

@since 2.22
-}
socketAddressGetFamily ::
    (B.CallStack.HasCallStack, MonadIO m, IsSocketAddress a) =>
    a
    {- ^ /@address@/: a 'GI.Gio.Objects.SocketAddress.SocketAddress' -}
    -> m Gio.Enums.SocketFamily
    {- ^ __Returns:__ the socket family type of /@address@/ -}
socketAddressGetFamily address = liftIO $ do
    address' <- unsafeManagedPtrCastPtr address
    result <- g_socket_address_get_family address'
    let result' = (toEnum . fromIntegral) result
    touchManagedPtr address
    return result'

data SocketAddressGetFamilyMethodInfo
instance (signature ~ (m Gio.Enums.SocketFamily), MonadIO m, IsSocketAddress a) => O.MethodInfo SocketAddressGetFamilyMethodInfo a signature where
    overloadedMethod _ = socketAddressGetFamily

-- method SocketAddress::get_native_size
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "address", argType = TInterface (Name {namespace = "Gio", name = "SocketAddress"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GSocketAddress", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TInt64)
-- throws : False
-- Skip return : False

foreign import ccall "g_socket_address_get_native_size" g_socket_address_get_native_size :: 
    Ptr SocketAddress ->                    -- address : TInterface (Name {namespace = "Gio", name = "SocketAddress"})
    IO Int64

{- |
Gets the size of /@address@/\'s native struct sockaddr.
You can use this to allocate memory to pass to
'GI.Gio.Objects.SocketAddress.socketAddressToNative'.

@since 2.22
-}
socketAddressGetNativeSize ::
    (B.CallStack.HasCallStack, MonadIO m, IsSocketAddress a) =>
    a
    {- ^ /@address@/: a 'GI.Gio.Objects.SocketAddress.SocketAddress' -}
    -> m Int64
    {- ^ __Returns:__ the size of the native struct sockaddr that
    /@address@/ represents -}
socketAddressGetNativeSize address = liftIO $ do
    address' <- unsafeManagedPtrCastPtr address
    result <- g_socket_address_get_native_size address'
    touchManagedPtr address
    return result

data SocketAddressGetNativeSizeMethodInfo
instance (signature ~ (m Int64), MonadIO m, IsSocketAddress a) => O.MethodInfo SocketAddressGetNativeSizeMethodInfo a signature where
    overloadedMethod _ = socketAddressGetNativeSize

-- method SocketAddress::to_native
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "address", argType = TInterface (Name {namespace = "Gio", name = "SocketAddress"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GSocketAddress", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "dest", argType = TBasicType TPtr, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "a pointer to a memory location that will contain the native\nstruct sockaddr", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "destlen", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the size of @dest. Must be at least as large as\n    g_socket_address_get_native_size()", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TBoolean)
-- throws : True
-- Skip return : False

foreign import ccall "g_socket_address_to_native" g_socket_address_to_native :: 
    Ptr SocketAddress ->                    -- address : TInterface (Name {namespace = "Gio", name = "SocketAddress"})
    Ptr () ->                               -- dest : TBasicType TPtr
    Word64 ->                               -- destlen : TBasicType TUInt64
    Ptr (Ptr GError) ->                     -- error
    IO CInt

{- |
Converts a 'GI.Gio.Objects.SocketAddress.SocketAddress' to a native struct sockaddr, which can
be passed to low-level functions like @/connect()/@ or @/bind()/@.

If not enough space is available, a 'GI.Gio.Enums.IOErrorEnumNoSpace' error
is returned. If the address type is not known on the system
then a 'GI.Gio.Enums.IOErrorEnumNotSupported' error is returned.

@since 2.22
-}
socketAddressToNative ::
    (B.CallStack.HasCallStack, MonadIO m, IsSocketAddress a) =>
    a
    {- ^ /@address@/: a 'GI.Gio.Objects.SocketAddress.SocketAddress' -}
    -> Ptr ()
    {- ^ /@dest@/: a pointer to a memory location that will contain the native
struct sockaddr -}
    -> Word64
    {- ^ /@destlen@/: the size of /@dest@/. Must be at least as large as
    'GI.Gio.Objects.SocketAddress.socketAddressGetNativeSize' -}
    -> m ()
    {- ^ /(Can throw 'Data.GI.Base.GError.GError')/ -}
socketAddressToNative address dest destlen = liftIO $ do
    address' <- unsafeManagedPtrCastPtr address
    onException (do
        _ <- propagateGError $ g_socket_address_to_native address' dest destlen
        touchManagedPtr address
        return ()
     ) (do
        return ()
     )

data SocketAddressToNativeMethodInfo
instance (signature ~ (Ptr () -> Word64 -> m ()), MonadIO m, IsSocketAddress a) => O.MethodInfo SocketAddressToNativeMethodInfo a signature where
    overloadedMethod _ = socketAddressToNative