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

-- * Exported types
    Converter(..)                           ,
    noConverter                             ,
    ConverterK                              ,
    toConverter                             ,


 -- * Methods
-- ** converterConvert
    converterConvert                        ,


-- ** converterReset
    converterReset                          ,




    ) 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.GObject as GObject

-- interface Converter 

newtype Converter = Converter (ForeignPtr Converter)
noConverter :: Maybe Converter
noConverter = Nothing

type instance AttributeList Converter = ConverterAttributeList
type ConverterAttributeList = ('[ ] :: [(Symbol, *)])

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

foreign import ccall "g_converter_get_type"
    c_g_converter_get_type :: IO GType

type instance ParentTypes Converter = ConverterParentTypes
type ConverterParentTypes = '[GObject.Object]

instance GObject Converter where
    gobjectIsInitiallyUnowned _ = False
    gobjectType _ = c_g_converter_get_type
    

class GObject o => ConverterK o
instance (GObject o, IsDescendantOf Converter o) => ConverterK o

toConverter :: ConverterK o => o -> IO Converter
toConverter = unsafeCastTo Converter

-- method Converter::convert
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Gio" "Converter", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "inbuf", argType = TCArray False (-1) 2 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "inbuf_size", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "outbuf", argType = TBasicType TVoid, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "outbuf_size", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "flags", argType = TInterface "Gio" "ConverterFlags", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "bytes_read", argType = TBasicType TUInt64, direction = DirectionOut, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferEverything},Arg {argName = "bytes_written", argType = TBasicType TUInt64, direction = DirectionOut, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferEverything}]
-- Lengths : [Arg {argName = "inbuf_size", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Gio" "Converter", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "inbuf", argType = TCArray False (-1) 2 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "outbuf", argType = TBasicType TVoid, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "outbuf_size", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "flags", argType = TInterface "Gio" "ConverterFlags", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TInterface "Gio" "ConverterResult"
-- throws : True
-- Skip return : False

foreign import ccall "g_converter_convert" g_converter_convert :: 
    Ptr Converter ->                        -- _obj : TInterface "Gio" "Converter"
    Ptr Word8 ->                            -- inbuf : TCArray False (-1) 2 (TBasicType TUInt8)
    Word64 ->                               -- inbuf_size : TBasicType TUInt64
    Ptr () ->                               -- outbuf : TBasicType TVoid
    Word64 ->                               -- outbuf_size : TBasicType TUInt64
    CUInt ->                                -- flags : TInterface "Gio" "ConverterFlags"
    Ptr Word64 ->                           -- bytes_read : TBasicType TUInt64
    Ptr Word64 ->                           -- bytes_written : TBasicType TUInt64
    Ptr (Ptr GError) ->                     -- error
    IO CUInt


converterConvert ::
    (MonadIO m, ConverterK a) =>
    a ->                                    -- _obj
    ByteString ->                           -- inbuf
    Ptr () ->                               -- outbuf
    Word64 ->                               -- outbuf_size
    [ConverterFlags] ->                     -- flags
    m (ConverterResult,Word64,Word64)
converterConvert _obj inbuf outbuf outbuf_size flags = liftIO $ do
    let inbuf_size = fromIntegral $ B.length inbuf
    let _obj' = unsafeManagedPtrCastPtr _obj
    inbuf' <- packByteString inbuf
    let flags' = gflagsToWord flags
    bytes_read <- allocMem :: IO (Ptr Word64)
    bytes_written <- allocMem :: IO (Ptr Word64)
    onException (do
        result <- propagateGError $ g_converter_convert _obj' inbuf' inbuf_size outbuf outbuf_size flags' bytes_read bytes_written
        let result' = (toEnum . fromIntegral) result
        bytes_read' <- peek bytes_read
        bytes_written' <- peek bytes_written
        touchManagedPtr _obj
        freeMem inbuf'
        freeMem bytes_read
        freeMem bytes_written
        return (result', bytes_read', bytes_written')
     ) (do
        freeMem inbuf'
        freeMem bytes_read
        freeMem bytes_written
     )

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

foreign import ccall "g_converter_reset" g_converter_reset :: 
    Ptr Converter ->                        -- _obj : TInterface "Gio" "Converter"
    IO ()


converterReset ::
    (MonadIO m, ConverterK a) =>
    a ->                                    -- _obj
    m ()
converterReset _obj = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    g_converter_reset _obj'
    touchManagedPtr _obj
    return ()