{- |
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.PollableInputStream
    ( 

-- * Exported types
    PollableInputStream(..)                 ,
    noPollableInputStream                   ,
    PollableInputStreamK                    ,
    toPollableInputStream                   ,


 -- * Methods
-- ** pollableInputStreamCanPoll
    pollableInputStreamCanPoll              ,


-- ** pollableInputStreamCreateSource
    pollableInputStreamCreateSource         ,


-- ** pollableInputStreamIsReadable
    pollableInputStreamIsReadable           ,


-- ** pollableInputStreamReadNonblocking
    pollableInputStreamReadNonblocking      ,




    ) 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 PollableInputStream 

newtype PollableInputStream = PollableInputStream (ForeignPtr PollableInputStream)
noPollableInputStream :: Maybe PollableInputStream
noPollableInputStream = Nothing

type instance AttributeList PollableInputStream = PollableInputStreamAttributeList
type PollableInputStreamAttributeList = ('[ ] :: [(Symbol, *)])

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

foreign import ccall "g_pollable_input_stream_get_type"
    c_g_pollable_input_stream_get_type :: IO GType

type instance ParentTypes PollableInputStream = PollableInputStreamParentTypes
type PollableInputStreamParentTypes = '[InputStream, GObject.Object]

instance GObject PollableInputStream where
    gobjectIsInitiallyUnowned _ = False
    gobjectType _ = c_g_pollable_input_stream_get_type
    

class GObject o => PollableInputStreamK o
instance (GObject o, IsDescendantOf PollableInputStream o) => PollableInputStreamK o

toPollableInputStream :: PollableInputStreamK o => o -> IO PollableInputStream
toPollableInputStream = unsafeCastTo PollableInputStream

-- method PollableInputStream::can_poll
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Gio" "PollableInputStream", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Gio" "PollableInputStream", 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_input_stream_can_poll" g_pollable_input_stream_can_poll :: 
    Ptr PollableInputStream ->              -- _obj : TInterface "Gio" "PollableInputStream"
    IO CInt


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

-- method PollableInputStream::create_source
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Gio" "PollableInputStream", 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" "PollableInputStream", 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_input_stream_create_source" g_pollable_input_stream_create_source :: 
    Ptr PollableInputStream ->              -- _obj : TInterface "Gio" "PollableInputStream"
    Ptr Cancellable ->                      -- cancellable : TInterface "Gio" "Cancellable"
    IO (Ptr GLib.Source)


pollableInputStreamCreateSource ::
    (MonadIO m, PollableInputStreamK a, CancellableK b) =>
    a ->                                    -- _obj
    Maybe (b) ->                            -- cancellable
    m GLib.Source
pollableInputStreamCreateSource _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_input_stream_create_source _obj' maybeCancellable
    checkUnexpectedReturnNULL "g_pollable_input_stream_create_source" result
    result' <- (wrapBoxed GLib.Source) result
    touchManagedPtr _obj
    whenJust cancellable touchManagedPtr
    return result'

-- method PollableInputStream::is_readable
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Gio" "PollableInputStream", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Gio" "PollableInputStream", 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_input_stream_is_readable" g_pollable_input_stream_is_readable :: 
    Ptr PollableInputStream ->              -- _obj : TInterface "Gio" "PollableInputStream"
    IO CInt


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

-- method PollableInputStream::read_nonblocking
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Gio" "PollableInputStream", 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" "PollableInputStream", 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_input_stream_read_nonblocking" g_pollable_input_stream_read_nonblocking :: 
    Ptr PollableInputStream ->              -- _obj : TInterface "Gio" "PollableInputStream"
    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


pollableInputStreamReadNonblocking ::
    (MonadIO m, PollableInputStreamK a, CancellableK b) =>
    a ->                                    -- _obj
    ByteString ->                           -- buffer
    Maybe (b) ->                            -- cancellable
    m Int64
pollableInputStreamReadNonblocking _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_input_stream_read_nonblocking _obj' buffer' count maybeCancellable
        touchManagedPtr _obj
        whenJust cancellable touchManagedPtr
        freeMem buffer'
        return result
     ) (do
        freeMem buffer'
     )