{- |
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.Objects.Cancellable
    ( 

-- * Exported types
    Cancellable(..)                         ,
    CancellableK                            ,
    toCancellable                           ,
    noCancellable                           ,


 -- * Methods
-- ** cancellableCancel
    cancellableCancel                       ,


-- ** cancellableConnect
    cancellableConnect                      ,


-- ** cancellableDisconnect
    cancellableDisconnect                   ,


-- ** cancellableGetCurrent
    cancellableGetCurrent                   ,


-- ** cancellableGetFd
    cancellableGetFd                        ,


-- ** cancellableIsCancelled
    cancellableIsCancelled                  ,


-- ** cancellableMakePollfd
    cancellableMakePollfd                   ,


-- ** cancellableNew
    cancellableNew                          ,


-- ** cancellablePopCurrent
    cancellablePopCurrent                   ,


-- ** cancellablePushCurrent
    cancellablePushCurrent                  ,


-- ** cancellableReleaseFd
    cancellableReleaseFd                    ,


-- ** cancellableReset
    cancellableReset                        ,


-- ** cancellableSetErrorIfCancelled
    cancellableSetErrorIfCancelled          ,




 -- * Signals
-- ** Cancelled
    CancellableCancelledCallback            ,
    CancellableCancelledCallbackC           ,
    CancellableCancelledSignalInfo          ,
    afterCancellableCancelled               ,
    cancellableCancelledCallbackWrapper     ,
    cancellableCancelledClosure             ,
    mkCancellableCancelledCallback          ,
    noCancellableCancelledCallback          ,
    onCancellableCancelled                  ,




    ) 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

newtype Cancellable = Cancellable (ForeignPtr Cancellable)
foreign import ccall "g_cancellable_get_type"
    c_g_cancellable_get_type :: IO GType

type instance ParentTypes Cancellable = CancellableParentTypes
type CancellableParentTypes = '[GObject.Object]

instance GObject Cancellable where
    gobjectIsInitiallyUnowned _ = False
    gobjectType _ = c_g_cancellable_get_type
    

class GObject o => CancellableK o
instance (GObject o, IsDescendantOf Cancellable o) => CancellableK o

toCancellable :: CancellableK o => o -> IO Cancellable
toCancellable = unsafeCastTo Cancellable

noCancellable :: Maybe Cancellable
noCancellable = Nothing

-- signal Cancellable::cancelled
type CancellableCancelledCallback =
    IO ()

noCancellableCancelledCallback :: Maybe CancellableCancelledCallback
noCancellableCancelledCallback = Nothing

type CancellableCancelledCallbackC =
    Ptr () ->                               -- object
    Ptr () ->                               -- user_data
    IO ()

foreign import ccall "wrapper"
    mkCancellableCancelledCallback :: CancellableCancelledCallbackC -> IO (FunPtr CancellableCancelledCallbackC)

cancellableCancelledClosure :: CancellableCancelledCallback -> IO Closure
cancellableCancelledClosure cb = newCClosure =<< mkCancellableCancelledCallback wrapped
    where wrapped = cancellableCancelledCallbackWrapper cb

cancellableCancelledCallbackWrapper ::
    CancellableCancelledCallback ->
    Ptr () ->
    Ptr () ->
    IO ()
cancellableCancelledCallbackWrapper _cb _ _ = do
    _cb 

onCancellableCancelled :: (GObject a, MonadIO m) => a -> CancellableCancelledCallback -> m SignalHandlerId
onCancellableCancelled obj cb = liftIO $ connectCancellableCancelled obj cb SignalConnectBefore
afterCancellableCancelled :: (GObject a, MonadIO m) => a -> CancellableCancelledCallback -> m SignalHandlerId
afterCancellableCancelled obj cb = connectCancellableCancelled obj cb SignalConnectAfter

connectCancellableCancelled :: (GObject a, MonadIO m) =>
                               a -> CancellableCancelledCallback -> SignalConnectMode -> m SignalHandlerId
connectCancellableCancelled obj cb after = liftIO $ do
    cb' <- mkCancellableCancelledCallback (cancellableCancelledCallbackWrapper cb)
    connectSignalFunPtr obj "cancelled" cb' after

type instance AttributeList Cancellable = CancellableAttributeList
type CancellableAttributeList = ('[ ] :: [(Symbol, *)])

data CancellableCancelledSignalInfo
instance SignalInfo CancellableCancelledSignalInfo where
    type HaskellCallbackType CancellableCancelledSignalInfo = CancellableCancelledCallback
    connectSignal _ = connectCancellableCancelled

type instance SignalList Cancellable = CancellableSignalList
type CancellableSignalList = ('[ '("cancelled", CancellableCancelledSignalInfo), '("notify", GObject.ObjectNotifySignalInfo), '("notify::[property]", GObjectNotifySignalInfo)] :: [(Symbol, *)])

-- method Cancellable::new
-- method type : Constructor
-- Args : []
-- Lengths : []
-- hInArgs : []
-- returnType : TInterface "Gio" "Cancellable"
-- throws : False
-- Skip return : False

foreign import ccall "g_cancellable_new" g_cancellable_new :: 
    IO (Ptr Cancellable)


cancellableNew ::
    (MonadIO m) =>
    m Cancellable
cancellableNew  = liftIO $ do
    result <- g_cancellable_new
    checkUnexpectedReturnNULL "g_cancellable_new" result
    result' <- (wrapObject Cancellable) result
    return result'

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

foreign import ccall "g_cancellable_cancel" g_cancellable_cancel :: 
    Ptr Cancellable ->                      -- _obj : TInterface "Gio" "Cancellable"
    IO ()


cancellableCancel ::
    (MonadIO m, CancellableK a) =>
    a ->                                    -- _obj
    m ()
cancellableCancel _obj = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    g_cancellable_cancel _obj'
    touchManagedPtr _obj
    return ()

-- method Cancellable::connect
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Gio" "Cancellable", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "callback", argType = TInterface "GObject" "Callback", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeNotified, argClosure = 2, argDestroy = 3, transfer = TransferNothing},Arg {argName = "data", argType = TBasicType TVoid, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "data_destroy_func", argType = TInterface "GLib" "DestroyNotify", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeAsync, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Gio" "Cancellable", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "callback", argType = TInterface "GObject" "Callback", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeNotified, argClosure = 2, argDestroy = 3, transfer = TransferNothing}]
-- returnType : TBasicType TUInt64
-- throws : False
-- Skip return : False

foreign import ccall "g_cancellable_connect" g_cancellable_connect :: 
    Ptr Cancellable ->                      -- _obj : TInterface "Gio" "Cancellable"
    FunPtr GObject.CallbackC ->             -- callback : TInterface "GObject" "Callback"
    Ptr () ->                               -- data : TBasicType TVoid
    FunPtr GLib.DestroyNotifyC ->           -- data_destroy_func : TInterface "GLib" "DestroyNotify"
    IO Word64


cancellableConnect ::
    (MonadIO m, CancellableK a) =>
    a ->                                    -- _obj
    GObject.Callback ->                     -- callback
    m Word64
cancellableConnect _obj callback = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    callback' <- GObject.mkCallback (GObject.callbackWrapper Nothing callback)
    let data_ = castFunPtrToPtr callback'
    let data_destroy_func = safeFreeFunPtrPtr
    result <- g_cancellable_connect _obj' callback' data_ data_destroy_func
    touchManagedPtr _obj
    return result

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

foreign import ccall "g_cancellable_disconnect" g_cancellable_disconnect :: 
    Ptr Cancellable ->                      -- _obj : TInterface "Gio" "Cancellable"
    Word64 ->                               -- handler_id : TBasicType TUInt64
    IO ()


cancellableDisconnect ::
    (MonadIO m, CancellableK a) =>
    a ->                                    -- _obj
    Word64 ->                               -- handler_id
    m ()
cancellableDisconnect _obj handler_id = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    g_cancellable_disconnect _obj' handler_id
    touchManagedPtr _obj
    return ()

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

foreign import ccall "g_cancellable_get_fd" g_cancellable_get_fd :: 
    Ptr Cancellable ->                      -- _obj : TInterface "Gio" "Cancellable"
    IO Int32


cancellableGetFd ::
    (MonadIO m, CancellableK a) =>
    a ->                                    -- _obj
    m Int32
cancellableGetFd _obj = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    result <- g_cancellable_get_fd _obj'
    touchManagedPtr _obj
    return result

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

foreign import ccall "g_cancellable_is_cancelled" g_cancellable_is_cancelled :: 
    Ptr Cancellable ->                      -- _obj : TInterface "Gio" "Cancellable"
    IO CInt


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

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

foreign import ccall "g_cancellable_make_pollfd" g_cancellable_make_pollfd :: 
    Ptr Cancellable ->                      -- _obj : TInterface "Gio" "Cancellable"
    Ptr GLib.PollFD ->                      -- pollfd : TInterface "GLib" "PollFD"
    IO CInt


cancellableMakePollfd ::
    (MonadIO m, CancellableK a) =>
    a ->                                    -- _obj
    GLib.PollFD ->                          -- pollfd
    m Bool
cancellableMakePollfd _obj pollfd = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    let pollfd' = unsafeManagedPtrGetPtr pollfd
    result <- g_cancellable_make_pollfd _obj' pollfd'
    let result' = (/= 0) result
    touchManagedPtr _obj
    touchManagedPtr pollfd
    return result'

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

foreign import ccall "g_cancellable_pop_current" g_cancellable_pop_current :: 
    Ptr Cancellable ->                      -- _obj : TInterface "Gio" "Cancellable"
    IO ()


cancellablePopCurrent ::
    (MonadIO m, CancellableK a) =>
    a ->                                    -- _obj
    m ()
cancellablePopCurrent _obj = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    g_cancellable_pop_current _obj'
    touchManagedPtr _obj
    return ()

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

foreign import ccall "g_cancellable_push_current" g_cancellable_push_current :: 
    Ptr Cancellable ->                      -- _obj : TInterface "Gio" "Cancellable"
    IO ()


cancellablePushCurrent ::
    (MonadIO m, CancellableK a) =>
    a ->                                    -- _obj
    m ()
cancellablePushCurrent _obj = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    g_cancellable_push_current _obj'
    touchManagedPtr _obj
    return ()

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

foreign import ccall "g_cancellable_release_fd" g_cancellable_release_fd :: 
    Ptr Cancellable ->                      -- _obj : TInterface "Gio" "Cancellable"
    IO ()


cancellableReleaseFd ::
    (MonadIO m, CancellableK a) =>
    a ->                                    -- _obj
    m ()
cancellableReleaseFd _obj = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    g_cancellable_release_fd _obj'
    touchManagedPtr _obj
    return ()

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

foreign import ccall "g_cancellable_reset" g_cancellable_reset :: 
    Ptr Cancellable ->                      -- _obj : TInterface "Gio" "Cancellable"
    IO ()


cancellableReset ::
    (MonadIO m, CancellableK a) =>
    a ->                                    -- _obj
    m ()
cancellableReset _obj = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    g_cancellable_reset _obj'
    touchManagedPtr _obj
    return ()

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

foreign import ccall "g_cancellable_set_error_if_cancelled" g_cancellable_set_error_if_cancelled :: 
    Ptr Cancellable ->                      -- _obj : TInterface "Gio" "Cancellable"
    Ptr (Ptr GError) ->                     -- error
    IO CInt


cancellableSetErrorIfCancelled ::
    (MonadIO m, CancellableK a) =>
    a ->                                    -- _obj
    m ()
cancellableSetErrorIfCancelled _obj = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    onException (do
        _ <- propagateGError $ g_cancellable_set_error_if_cancelled _obj'
        touchManagedPtr _obj
        return ()
     ) (do
        return ()
     )

-- method Cancellable::get_current
-- method type : MemberFunction
-- Args : []
-- Lengths : []
-- hInArgs : []
-- returnType : TInterface "Gio" "Cancellable"
-- throws : False
-- Skip return : False

foreign import ccall "g_cancellable_get_current" g_cancellable_get_current :: 
    IO (Ptr Cancellable)


cancellableGetCurrent ::
    (MonadIO m) =>
    m Cancellable
cancellableGetCurrent  = liftIO $ do
    result <- g_cancellable_get_current
    checkUnexpectedReturnNULL "g_cancellable_get_current" result
    result' <- (newObject Cancellable) result
    return result'