{- |
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.UnixOutputStream.UnixOutputStream' implements 'GI.Gio.Objects.OutputStream.OutputStream' for writing to a UNIX
file descriptor, including asynchronous operations. (If the file
descriptor refers to a socket or pipe, this will use @/poll()/@ to do
asynchronous I\/O. If it refers to a regular file, it will fall back
to doing asynchronous I\/O in another thread.)

Note that @\<gio\/gunixoutputstream.h>@ belongs to the UNIX-specific GIO
interfaces, thus you have to use the @gio-unix-2.0.pc@ pkg-config file
when using it.
-}

module GI.Gio.Objects.UnixOutputStream
    ( 

-- * Exported types
    UnixOutputStream(..)                    ,
    IsUnixOutputStream                      ,
    toUnixOutputStream                      ,
    noUnixOutputStream                      ,


 -- * Methods
-- ** getCloseFd #method:getCloseFd#
    UnixOutputStreamGetCloseFdMethodInfo    ,
    unixOutputStreamGetCloseFd              ,


-- ** getFd #method:getFd#
    UnixOutputStreamGetFdMethodInfo         ,
    unixOutputStreamGetFd                   ,


-- ** new #method:new#
    unixOutputStreamNew                     ,


-- ** setCloseFd #method:setCloseFd#
    UnixOutputStreamSetCloseFdMethodInfo    ,
    unixOutputStreamSetCloseFd              ,




 -- * Properties
-- ** closeFd #attr:closeFd#
    UnixOutputStreamCloseFdPropertyInfo     ,
    constructUnixOutputStreamCloseFd        ,
    getUnixOutputStreamCloseFd              ,
    setUnixOutputStreamCloseFd              ,
    unixOutputStreamCloseFd                 ,


-- ** fd #attr:fd#
    UnixOutputStreamFdPropertyInfo          ,
    constructUnixOutputStreamFd             ,
    getUnixOutputStreamFd                   ,
    unixOutputStreamFd                      ,




    ) 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.Interfaces.FileDescriptorBased as Gio.FileDescriptorBased
import {-# SOURCE #-} qualified GI.Gio.Interfaces.PollableOutputStream as Gio.PollableOutputStream
import {-# SOURCE #-} qualified GI.Gio.Objects.OutputStream as Gio.OutputStream

newtype UnixOutputStream = UnixOutputStream (ManagedPtr UnixOutputStream)
foreign import ccall "g_unix_output_stream_get_type"
    c_g_unix_output_stream_get_type :: IO GType

instance GObject UnixOutputStream where
    gobjectType _ = c_g_unix_output_stream_get_type
    

class GObject o => IsUnixOutputStream o
#if MIN_VERSION_base(4,9,0)
instance {-# OVERLAPPABLE #-} (GObject a, O.UnknownAncestorError UnixOutputStream a) =>
    IsUnixOutputStream a
#endif
instance IsUnixOutputStream UnixOutputStream
instance Gio.OutputStream.IsOutputStream UnixOutputStream
instance GObject.Object.IsObject UnixOutputStream
instance Gio.FileDescriptorBased.IsFileDescriptorBased UnixOutputStream
instance Gio.PollableOutputStream.IsPollableOutputStream UnixOutputStream

toUnixOutputStream :: IsUnixOutputStream o => o -> IO UnixOutputStream
toUnixOutputStream = unsafeCastTo UnixOutputStream

noUnixOutputStream :: Maybe UnixOutputStream
noUnixOutputStream = Nothing

type family ResolveUnixOutputStreamMethod (t :: Symbol) (o :: *) :: * where
    ResolveUnixOutputStreamMethod "bindProperty" o = GObject.Object.ObjectBindPropertyMethodInfo
    ResolveUnixOutputStreamMethod "bindPropertyFull" o = GObject.Object.ObjectBindPropertyFullMethodInfo
    ResolveUnixOutputStreamMethod "canPoll" o = Gio.PollableOutputStream.PollableOutputStreamCanPollMethodInfo
    ResolveUnixOutputStreamMethod "clearPending" o = Gio.OutputStream.OutputStreamClearPendingMethodInfo
    ResolveUnixOutputStreamMethod "close" o = Gio.OutputStream.OutputStreamCloseMethodInfo
    ResolveUnixOutputStreamMethod "closeAsync" o = Gio.OutputStream.OutputStreamCloseAsyncMethodInfo
    ResolveUnixOutputStreamMethod "closeFinish" o = Gio.OutputStream.OutputStreamCloseFinishMethodInfo
    ResolveUnixOutputStreamMethod "createSource" o = Gio.PollableOutputStream.PollableOutputStreamCreateSourceMethodInfo
    ResolveUnixOutputStreamMethod "flush" o = Gio.OutputStream.OutputStreamFlushMethodInfo
    ResolveUnixOutputStreamMethod "flushAsync" o = Gio.OutputStream.OutputStreamFlushAsyncMethodInfo
    ResolveUnixOutputStreamMethod "flushFinish" o = Gio.OutputStream.OutputStreamFlushFinishMethodInfo
    ResolveUnixOutputStreamMethod "forceFloating" o = GObject.Object.ObjectForceFloatingMethodInfo
    ResolveUnixOutputStreamMethod "freezeNotify" o = GObject.Object.ObjectFreezeNotifyMethodInfo
    ResolveUnixOutputStreamMethod "hasPending" o = Gio.OutputStream.OutputStreamHasPendingMethodInfo
    ResolveUnixOutputStreamMethod "isClosed" o = Gio.OutputStream.OutputStreamIsClosedMethodInfo
    ResolveUnixOutputStreamMethod "isClosing" o = Gio.OutputStream.OutputStreamIsClosingMethodInfo
    ResolveUnixOutputStreamMethod "isFloating" o = GObject.Object.ObjectIsFloatingMethodInfo
    ResolveUnixOutputStreamMethod "isWritable" o = Gio.PollableOutputStream.PollableOutputStreamIsWritableMethodInfo
    ResolveUnixOutputStreamMethod "notify" o = GObject.Object.ObjectNotifyMethodInfo
    ResolveUnixOutputStreamMethod "notifyByPspec" o = GObject.Object.ObjectNotifyByPspecMethodInfo
    ResolveUnixOutputStreamMethod "ref" o = GObject.Object.ObjectRefMethodInfo
    ResolveUnixOutputStreamMethod "refSink" o = GObject.Object.ObjectRefSinkMethodInfo
    ResolveUnixOutputStreamMethod "replaceData" o = GObject.Object.ObjectReplaceDataMethodInfo
    ResolveUnixOutputStreamMethod "replaceQdata" o = GObject.Object.ObjectReplaceQdataMethodInfo
    ResolveUnixOutputStreamMethod "runDispose" o = GObject.Object.ObjectRunDisposeMethodInfo
    ResolveUnixOutputStreamMethod "splice" o = Gio.OutputStream.OutputStreamSpliceMethodInfo
    ResolveUnixOutputStreamMethod "spliceAsync" o = Gio.OutputStream.OutputStreamSpliceAsyncMethodInfo
    ResolveUnixOutputStreamMethod "spliceFinish" o = Gio.OutputStream.OutputStreamSpliceFinishMethodInfo
    ResolveUnixOutputStreamMethod "stealData" o = GObject.Object.ObjectStealDataMethodInfo
    ResolveUnixOutputStreamMethod "stealQdata" o = GObject.Object.ObjectStealQdataMethodInfo
    ResolveUnixOutputStreamMethod "thawNotify" o = GObject.Object.ObjectThawNotifyMethodInfo
    ResolveUnixOutputStreamMethod "unref" o = GObject.Object.ObjectUnrefMethodInfo
    ResolveUnixOutputStreamMethod "watchClosure" o = GObject.Object.ObjectWatchClosureMethodInfo
    ResolveUnixOutputStreamMethod "write" o = Gio.OutputStream.OutputStreamWriteMethodInfo
    ResolveUnixOutputStreamMethod "writeAll" o = Gio.OutputStream.OutputStreamWriteAllMethodInfo
    ResolveUnixOutputStreamMethod "writeAllAsync" o = Gio.OutputStream.OutputStreamWriteAllAsyncMethodInfo
    ResolveUnixOutputStreamMethod "writeAllFinish" o = Gio.OutputStream.OutputStreamWriteAllFinishMethodInfo
    ResolveUnixOutputStreamMethod "writeAsync" o = Gio.OutputStream.OutputStreamWriteAsyncMethodInfo
    ResolveUnixOutputStreamMethod "writeBytes" o = Gio.OutputStream.OutputStreamWriteBytesMethodInfo
    ResolveUnixOutputStreamMethod "writeBytesAsync" o = Gio.OutputStream.OutputStreamWriteBytesAsyncMethodInfo
    ResolveUnixOutputStreamMethod "writeBytesFinish" o = Gio.OutputStream.OutputStreamWriteBytesFinishMethodInfo
    ResolveUnixOutputStreamMethod "writeFinish" o = Gio.OutputStream.OutputStreamWriteFinishMethodInfo
    ResolveUnixOutputStreamMethod "writeNonblocking" o = Gio.PollableOutputStream.PollableOutputStreamWriteNonblockingMethodInfo
    ResolveUnixOutputStreamMethod "getCloseFd" o = UnixOutputStreamGetCloseFdMethodInfo
    ResolveUnixOutputStreamMethod "getData" o = GObject.Object.ObjectGetDataMethodInfo
    ResolveUnixOutputStreamMethod "getFd" o = UnixOutputStreamGetFdMethodInfo
    ResolveUnixOutputStreamMethod "getProperty" o = GObject.Object.ObjectGetPropertyMethodInfo
    ResolveUnixOutputStreamMethod "getQdata" o = GObject.Object.ObjectGetQdataMethodInfo
    ResolveUnixOutputStreamMethod "setCloseFd" o = UnixOutputStreamSetCloseFdMethodInfo
    ResolveUnixOutputStreamMethod "setData" o = GObject.Object.ObjectSetDataMethodInfo
    ResolveUnixOutputStreamMethod "setPending" o = Gio.OutputStream.OutputStreamSetPendingMethodInfo
    ResolveUnixOutputStreamMethod "setProperty" o = GObject.Object.ObjectSetPropertyMethodInfo
    ResolveUnixOutputStreamMethod l o = O.MethodResolutionFailed l o

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

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

-- VVV Prop "close-fd"
   -- Type: TBasicType TBoolean
   -- Flags: [PropertyReadable,PropertyWritable]
   -- Nullable: (Just False,Just False)

getUnixOutputStreamCloseFd :: (MonadIO m, IsUnixOutputStream o) => o -> m Bool
getUnixOutputStreamCloseFd obj = liftIO $ getObjectPropertyBool obj "close-fd"

setUnixOutputStreamCloseFd :: (MonadIO m, IsUnixOutputStream o) => o -> Bool -> m ()
setUnixOutputStreamCloseFd obj val = liftIO $ setObjectPropertyBool obj "close-fd" val

constructUnixOutputStreamCloseFd :: (IsUnixOutputStream o) => Bool -> IO (GValueConstruct o)
constructUnixOutputStreamCloseFd val = constructObjectPropertyBool "close-fd" val

data UnixOutputStreamCloseFdPropertyInfo
instance AttrInfo UnixOutputStreamCloseFdPropertyInfo where
    type AttrAllowedOps UnixOutputStreamCloseFdPropertyInfo = '[ 'AttrSet, 'AttrConstruct, 'AttrGet]
    type AttrSetTypeConstraint UnixOutputStreamCloseFdPropertyInfo = (~) Bool
    type AttrBaseTypeConstraint UnixOutputStreamCloseFdPropertyInfo = IsUnixOutputStream
    type AttrGetType UnixOutputStreamCloseFdPropertyInfo = Bool
    type AttrLabel UnixOutputStreamCloseFdPropertyInfo = "close-fd"
    type AttrOrigin UnixOutputStreamCloseFdPropertyInfo = UnixOutputStream
    attrGet _ = getUnixOutputStreamCloseFd
    attrSet _ = setUnixOutputStreamCloseFd
    attrConstruct _ = constructUnixOutputStreamCloseFd
    attrClear _ = undefined

-- VVV Prop "fd"
   -- Type: TBasicType TInt
   -- Flags: [PropertyReadable,PropertyWritable,PropertyConstructOnly]
   -- Nullable: (Just False,Nothing)

getUnixOutputStreamFd :: (MonadIO m, IsUnixOutputStream o) => o -> m Int32
getUnixOutputStreamFd obj = liftIO $ getObjectPropertyInt32 obj "fd"

constructUnixOutputStreamFd :: (IsUnixOutputStream o) => Int32 -> IO (GValueConstruct o)
constructUnixOutputStreamFd val = constructObjectPropertyInt32 "fd" val

data UnixOutputStreamFdPropertyInfo
instance AttrInfo UnixOutputStreamFdPropertyInfo where
    type AttrAllowedOps UnixOutputStreamFdPropertyInfo = '[ 'AttrConstruct, 'AttrGet]
    type AttrSetTypeConstraint UnixOutputStreamFdPropertyInfo = (~) Int32
    type AttrBaseTypeConstraint UnixOutputStreamFdPropertyInfo = IsUnixOutputStream
    type AttrGetType UnixOutputStreamFdPropertyInfo = Int32
    type AttrLabel UnixOutputStreamFdPropertyInfo = "fd"
    type AttrOrigin UnixOutputStreamFdPropertyInfo = UnixOutputStream
    attrGet _ = getUnixOutputStreamFd
    attrSet _ = undefined
    attrConstruct _ = constructUnixOutputStreamFd
    attrClear _ = undefined

instance O.HasAttributeList UnixOutputStream
type instance O.AttributeList UnixOutputStream = UnixOutputStreamAttributeList
type UnixOutputStreamAttributeList = ('[ '("closeFd", UnixOutputStreamCloseFdPropertyInfo), '("fd", UnixOutputStreamFdPropertyInfo)] :: [(Symbol, *)])

unixOutputStreamCloseFd :: AttrLabelProxy "closeFd"
unixOutputStreamCloseFd = AttrLabelProxy

unixOutputStreamFd :: AttrLabelProxy "fd"
unixOutputStreamFd = AttrLabelProxy

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

-- method UnixOutputStream::new
-- method type : Constructor
-- Args : [Arg {argCName = "fd", argType = TBasicType TInt, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a UNIX file descriptor", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "close_fd", argType = TBasicType TBoolean, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "%TRUE to close the file descriptor when done", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Gio", name = "UnixOutputStream"}))
-- throws : False
-- Skip return : False

foreign import ccall "g_unix_output_stream_new" g_unix_output_stream_new :: 
    Int32 ->                                -- fd : TBasicType TInt
    CInt ->                                 -- close_fd : TBasicType TBoolean
    IO (Ptr UnixOutputStream)

{- |
Creates a new 'GI.Gio.Objects.UnixOutputStream.UnixOutputStream' for the given /@fd@/.

If /@closeFd@/, is 'True', the file descriptor will be closed when
the output stream is destroyed.
-}
unixOutputStreamNew ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Int32
    {- ^ /@fd@/: a UNIX file descriptor -}
    -> Bool
    {- ^ /@closeFd@/: 'True' to close the file descriptor when done -}
    -> m UnixOutputStream
    {- ^ __Returns:__ a new 'GI.Gio.Objects.OutputStream.OutputStream' -}
unixOutputStreamNew fd closeFd = liftIO $ do
    let closeFd' = (fromIntegral . fromEnum) closeFd
    result <- g_unix_output_stream_new fd closeFd'
    checkUnexpectedReturnNULL "unixOutputStreamNew" result
    result' <- (wrapObject UnixOutputStream) result
    return result'

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

foreign import ccall "g_unix_output_stream_get_close_fd" g_unix_output_stream_get_close_fd :: 
    Ptr UnixOutputStream ->                 -- stream : TInterface (Name {namespace = "Gio", name = "UnixOutputStream"})
    IO CInt

{- |
Returns whether the file descriptor of /@stream@/ will be
closed when the stream is closed.

@since 2.20
-}
unixOutputStreamGetCloseFd ::
    (B.CallStack.HasCallStack, MonadIO m, IsUnixOutputStream a) =>
    a
    {- ^ /@stream@/: a 'GI.Gio.Objects.UnixOutputStream.UnixOutputStream' -}
    -> m Bool
    {- ^ __Returns:__ 'True' if the file descriptor is closed when done -}
unixOutputStreamGetCloseFd stream = liftIO $ do
    stream' <- unsafeManagedPtrCastPtr stream
    result <- g_unix_output_stream_get_close_fd stream'
    let result' = (/= 0) result
    touchManagedPtr stream
    return result'

data UnixOutputStreamGetCloseFdMethodInfo
instance (signature ~ (m Bool), MonadIO m, IsUnixOutputStream a) => O.MethodInfo UnixOutputStreamGetCloseFdMethodInfo a signature where
    overloadedMethod _ = unixOutputStreamGetCloseFd

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

foreign import ccall "g_unix_output_stream_get_fd" g_unix_output_stream_get_fd :: 
    Ptr UnixOutputStream ->                 -- stream : TInterface (Name {namespace = "Gio", name = "UnixOutputStream"})
    IO Int32

{- |
Return the UNIX file descriptor that the stream writes to.

@since 2.20
-}
unixOutputStreamGetFd ::
    (B.CallStack.HasCallStack, MonadIO m, IsUnixOutputStream a) =>
    a
    {- ^ /@stream@/: a 'GI.Gio.Objects.UnixOutputStream.UnixOutputStream' -}
    -> m Int32
    {- ^ __Returns:__ The file descriptor of /@stream@/ -}
unixOutputStreamGetFd stream = liftIO $ do
    stream' <- unsafeManagedPtrCastPtr stream
    result <- g_unix_output_stream_get_fd stream'
    touchManagedPtr stream
    return result

data UnixOutputStreamGetFdMethodInfo
instance (signature ~ (m Int32), MonadIO m, IsUnixOutputStream a) => O.MethodInfo UnixOutputStreamGetFdMethodInfo a signature where
    overloadedMethod _ = unixOutputStreamGetFd

-- method UnixOutputStream::set_close_fd
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "stream", argType = TInterface (Name {namespace = "Gio", name = "UnixOutputStream"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GUnixOutputStream", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "close_fd", argType = TBasicType TBoolean, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "%TRUE to close the file descriptor when done", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "g_unix_output_stream_set_close_fd" g_unix_output_stream_set_close_fd :: 
    Ptr UnixOutputStream ->                 -- stream : TInterface (Name {namespace = "Gio", name = "UnixOutputStream"})
    CInt ->                                 -- close_fd : TBasicType TBoolean
    IO ()

{- |
Sets whether the file descriptor of /@stream@/ shall be closed
when the stream is closed.

@since 2.20
-}
unixOutputStreamSetCloseFd ::
    (B.CallStack.HasCallStack, MonadIO m, IsUnixOutputStream a) =>
    a
    {- ^ /@stream@/: a 'GI.Gio.Objects.UnixOutputStream.UnixOutputStream' -}
    -> Bool
    {- ^ /@closeFd@/: 'True' to close the file descriptor when done -}
    -> m ()
unixOutputStreamSetCloseFd stream closeFd = liftIO $ do
    stream' <- unsafeManagedPtrCastPtr stream
    let closeFd' = (fromIntegral . fromEnum) closeFd
    g_unix_output_stream_set_close_fd stream' closeFd'
    touchManagedPtr stream
    return ()

data UnixOutputStreamSetCloseFdMethodInfo
instance (signature ~ (Bool -> m ()), MonadIO m, IsUnixOutputStream a) => O.MethodInfo UnixOutputStreamSetCloseFdMethodInfo a signature where
    overloadedMethod _ = unixOutputStreamSetCloseFd