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

module GI.Gio.Interfaces.PollableOutputStream
    ( 

-- * Exported types
    PollableOutputStream(..)                ,
    noPollableOutputStream                  ,
    PollableOutputStreamK                   ,
    toPollableOutputStream                  ,


 -- * Methods
-- ** pollableOutputStreamCanPoll
    pollableOutputStreamCanPoll             ,


-- ** pollableOutputStreamCreateSource
    pollableOutputStreamCreateSource        ,


-- ** pollableOutputStreamIsWritable
    pollableOutputStreamIsWritable          ,


-- ** pollableOutputStreamWriteNonblocking
    pollableOutputStreamWriteNonblocking    ,




    ) where

import Prelude ()
import Data.GI.Base.ShortPrelude

import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map

import GI.Gio.Types
import GI.Gio.Callbacks
import qualified GI.GLib as GLib
import qualified GI.GObject as GObject

-- interface PollableOutputStream 

newtype PollableOutputStream = PollableOutputStream (ForeignPtr PollableOutputStream)
noPollableOutputStream :: Maybe PollableOutputStream
noPollableOutputStream = Nothing

type instance AttributeList PollableOutputStream = PollableOutputStreamAttributeList
type PollableOutputStreamAttributeList = ('[ ] :: [(Symbol, *)])

type instance SignalList PollableOutputStream = PollableOutputStreamSignalList
type PollableOutputStreamSignalList = ('[ '("notify", GObject.ObjectNotifySignalInfo), '("notify::[property]", GObjectNotifySignalInfo)] :: [(Symbol, *)])

foreign import ccall "g_pollable_output_stream_get_type"
    c_g_pollable_output_stream_get_type :: IO GType

type instance ParentTypes PollableOutputStream = PollableOutputStreamParentTypes
type PollableOutputStreamParentTypes = '[OutputStream, GObject.Object]

instance GObject PollableOutputStream where
    gobjectIsInitiallyUnowned _ = False
    gobjectType _ = c_g_pollable_output_stream_get_type
    

class GObject o => PollableOutputStreamK o
instance (GObject o, IsDescendantOf PollableOutputStream o) => PollableOutputStreamK o

toPollableOutputStream :: PollableOutputStreamK o => o -> IO PollableOutputStream
toPollableOutputStream = unsafeCastTo PollableOutputStream

-- method PollableOutputStream::can_poll
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Gio" "PollableOutputStream", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Gio" "PollableOutputStream", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TBoolean
-- throws : False
-- Skip return : False

foreign import ccall "g_pollable_output_stream_can_poll" g_pollable_output_stream_can_poll :: 
    Ptr PollableOutputStream ->             -- _obj : TInterface "Gio" "PollableOutputStream"
    IO CInt


pollableOutputStreamCanPoll ::
    (MonadIO m, PollableOutputStreamK a) =>
    a ->                                    -- _obj
    m Bool
pollableOutputStreamCanPoll _obj = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    result <- g_pollable_output_stream_can_poll _obj'
    let result' = (/= 0) result
    touchManagedPtr _obj
    return result'

-- method PollableOutputStream::create_source
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Gio" "PollableOutputStream", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "cancellable", argType = TInterface "Gio" "Cancellable", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Gio" "PollableOutputStream", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "cancellable", argType = TInterface "Gio" "Cancellable", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TInterface "GLib" "Source"
-- throws : False
-- Skip return : False

foreign import ccall "g_pollable_output_stream_create_source" g_pollable_output_stream_create_source :: 
    Ptr PollableOutputStream ->             -- _obj : TInterface "Gio" "PollableOutputStream"
    Ptr Cancellable ->                      -- cancellable : TInterface "Gio" "Cancellable"
    IO (Ptr GLib.Source)


pollableOutputStreamCreateSource ::
    (MonadIO m, PollableOutputStreamK a, CancellableK b) =>
    a ->                                    -- _obj
    Maybe (b) ->                            -- cancellable
    m GLib.Source
pollableOutputStreamCreateSource _obj cancellable = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    maybeCancellable <- case cancellable of
        Nothing -> return nullPtr
        Just jCancellable -> do
            let jCancellable' = unsafeManagedPtrCastPtr jCancellable
            return jCancellable'
    result <- g_pollable_output_stream_create_source _obj' maybeCancellable
    checkUnexpectedReturnNULL "g_pollable_output_stream_create_source" result
    result' <- (wrapBoxed GLib.Source) result
    touchManagedPtr _obj
    whenJust cancellable touchManagedPtr
    return result'

-- method PollableOutputStream::is_writable
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Gio" "PollableOutputStream", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Gio" "PollableOutputStream", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TBoolean
-- throws : False
-- Skip return : False

foreign import ccall "g_pollable_output_stream_is_writable" g_pollable_output_stream_is_writable :: 
    Ptr PollableOutputStream ->             -- _obj : TInterface "Gio" "PollableOutputStream"
    IO CInt


pollableOutputStreamIsWritable ::
    (MonadIO m, PollableOutputStreamK a) =>
    a ->                                    -- _obj
    m Bool
pollableOutputStreamIsWritable _obj = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    result <- g_pollable_output_stream_is_writable _obj'
    let result' = (/= 0) result
    touchManagedPtr _obj
    return result'

-- method PollableOutputStream::write_nonblocking
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Gio" "PollableOutputStream", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "buffer", argType = TCArray False (-1) 2 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "count", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "cancellable", argType = TInterface "Gio" "Cancellable", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : [Arg {argName = "count", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Gio" "PollableOutputStream", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "buffer", argType = TCArray False (-1) 2 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "cancellable", argType = TInterface "Gio" "Cancellable", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TInt64
-- throws : True
-- Skip return : False

foreign import ccall "g_pollable_output_stream_write_nonblocking" g_pollable_output_stream_write_nonblocking :: 
    Ptr PollableOutputStream ->             -- _obj : TInterface "Gio" "PollableOutputStream"
    Ptr Word8 ->                            -- buffer : TCArray False (-1) 2 (TBasicType TUInt8)
    Word64 ->                               -- count : TBasicType TUInt64
    Ptr Cancellable ->                      -- cancellable : TInterface "Gio" "Cancellable"
    Ptr (Ptr GError) ->                     -- error
    IO Int64


pollableOutputStreamWriteNonblocking ::
    (MonadIO m, PollableOutputStreamK a, CancellableK b) =>
    a ->                                    -- _obj
    ByteString ->                           -- buffer
    Maybe (b) ->                            -- cancellable
    m Int64
pollableOutputStreamWriteNonblocking _obj buffer cancellable = liftIO $ do
    let count = fromIntegral $ B.length buffer
    let _obj' = unsafeManagedPtrCastPtr _obj
    buffer' <- packByteString buffer
    maybeCancellable <- case cancellable of
        Nothing -> return nullPtr
        Just jCancellable -> do
            let jCancellable' = unsafeManagedPtrCastPtr jCancellable
            return jCancellable'
    onException (do
        result <- propagateGError $ g_pollable_output_stream_write_nonblocking _obj' buffer' count maybeCancellable
        touchManagedPtr _obj
        whenJust cancellable touchManagedPtr
        freeMem buffer'
        return result
     ) (do
        freeMem buffer'
     )