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

An opaque structure representing a checksumming operation.
To create a new GChecksum, use 'GI.GLib.Structs.Checksum.checksumNew'. To free
a GChecksum, use 'GI.GLib.Structs.Checksum.checksumFree'.
-}

module GI.GLib.Structs.Checksum
    ( 

-- * Exported types
    Checksum(..)                            ,
    noChecksum                              ,


 -- * Methods
-- ** copy #method:copy#
#ifdef ENABLE_OVERLOADING
    ChecksumCopyMethodInfo                  ,
#endif
    checksumCopy                            ,


-- ** free #method:free#
#ifdef ENABLE_OVERLOADING
    ChecksumFreeMethodInfo                  ,
#endif
    checksumFree                            ,


-- ** getString #method:getString#
#ifdef ENABLE_OVERLOADING
    ChecksumGetStringMethodInfo             ,
#endif
    checksumGetString                       ,


-- ** new #method:new#
    checksumNew                             ,


-- ** reset #method:reset#
#ifdef ENABLE_OVERLOADING
    ChecksumResetMethodInfo                 ,
#endif
    checksumReset                           ,


-- ** typeGetLength #method:typeGetLength#
    checksumTypeGetLength                   ,


-- ** update #method:update#
#ifdef ENABLE_OVERLOADING
    ChecksumUpdateMethodInfo                ,
#endif
    checksumUpdate                          ,




    ) 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 {-# SOURCE #-} qualified GI.GLib.Enums as GLib.Enums

newtype Checksum = Checksum (ManagedPtr Checksum)
foreign import ccall "g_checksum_get_type" c_g_checksum_get_type :: 
    IO GType

instance BoxedObject Checksum where
    boxedType _ = c_g_checksum_get_type

noChecksum :: Maybe Checksum
noChecksum = Nothing


#ifdef ENABLE_OVERLOADING
instance O.HasAttributeList Checksum
type instance O.AttributeList Checksum = ChecksumAttributeList
type ChecksumAttributeList = ('[ ] :: [(Symbol, *)])
#endif

-- method Checksum::new
-- method type : Constructor
-- Args : [Arg {argCName = "checksum_type", argType = TInterface (Name {namespace = "GLib", name = "ChecksumType"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the desired type of checksum", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "GLib", name = "Checksum"}))
-- throws : False
-- Skip return : False

foreign import ccall "g_checksum_new" g_checksum_new :: 
    CUInt ->                                -- checksum_type : TInterface (Name {namespace = "GLib", name = "ChecksumType"})
    IO (Ptr Checksum)

{- |
Creates a new 'GI.GLib.Structs.Checksum.Checksum', using the checksum algorithm /@checksumType@/.
If the /@checksumType@/ is not known, 'Nothing' is returned.
A 'GI.GLib.Structs.Checksum.Checksum' can be used to compute the checksum, or digest, of an
arbitrary binary blob, using different hashing algorithms.

A 'GI.GLib.Structs.Checksum.Checksum' works by feeding a binary blob through 'GI.GLib.Structs.Checksum.checksumUpdate'
until there is data to be checked; the digest can then be extracted
using 'GI.GLib.Structs.Checksum.checksumGetString', which will return the checksum as a
hexadecimal string; or @/g_checksum_get_digest()/@, which will return a
vector of raw bytes. Once either 'GI.GLib.Structs.Checksum.checksumGetString' or
@/g_checksum_get_digest()/@ have been called on a 'GI.GLib.Structs.Checksum.Checksum', the checksum
will be closed and it won\'t be possible to call 'GI.GLib.Structs.Checksum.checksumUpdate'
on it anymore.

@since 2.16
-}
checksumNew ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    GLib.Enums.ChecksumType
    {- ^ /@checksumType@/: the desired type of checksum -}
    -> m Checksum
    {- ^ __Returns:__ the newly created 'GI.GLib.Structs.Checksum.Checksum', or 'Nothing'.
  Use 'GI.GLib.Structs.Checksum.checksumFree' to free the memory allocated by it. -}
checksumNew checksumType = liftIO $ do
    let checksumType' = (fromIntegral . fromEnum) checksumType
    result <- g_checksum_new checksumType'
    checkUnexpectedReturnNULL "checksumNew" result
    result' <- (wrapBoxed Checksum) result
    return result'

#ifdef ENABLE_OVERLOADING
#endif

-- method Checksum::copy
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "checksum", argType = TInterface (Name {namespace = "GLib", name = "Checksum"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the #GChecksum to copy", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "GLib", name = "Checksum"}))
-- throws : False
-- Skip return : False

foreign import ccall "g_checksum_copy" g_checksum_copy :: 
    Ptr Checksum ->                         -- checksum : TInterface (Name {namespace = "GLib", name = "Checksum"})
    IO (Ptr Checksum)

{- |
Copies a 'GI.GLib.Structs.Checksum.Checksum'. If /@checksum@/ has been closed, by calling
'GI.GLib.Structs.Checksum.checksumGetString' or @/g_checksum_get_digest()/@, the copied
checksum will be closed as well.

@since 2.16
-}
checksumCopy ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Checksum
    {- ^ /@checksum@/: the 'GI.GLib.Structs.Checksum.Checksum' to copy -}
    -> m Checksum
    {- ^ __Returns:__ the copy of the passed 'GI.GLib.Structs.Checksum.Checksum'. Use 'GI.GLib.Structs.Checksum.checksumFree'
  when finished using it. -}
checksumCopy checksum = liftIO $ do
    checksum' <- unsafeManagedPtrGetPtr checksum
    result <- g_checksum_copy checksum'
    checkUnexpectedReturnNULL "checksumCopy" result
    result' <- (wrapBoxed Checksum) result
    touchManagedPtr checksum
    return result'

#ifdef ENABLE_OVERLOADING
data ChecksumCopyMethodInfo
instance (signature ~ (m Checksum), MonadIO m) => O.MethodInfo ChecksumCopyMethodInfo Checksum signature where
    overloadedMethod _ = checksumCopy

#endif

-- method Checksum::free
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "checksum", argType = TInterface (Name {namespace = "GLib", name = "Checksum"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GChecksum", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "g_checksum_free" g_checksum_free :: 
    Ptr Checksum ->                         -- checksum : TInterface (Name {namespace = "GLib", name = "Checksum"})
    IO ()

{- |
Frees the memory allocated for /@checksum@/.

@since 2.16
-}
checksumFree ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Checksum
    {- ^ /@checksum@/: a 'GI.GLib.Structs.Checksum.Checksum' -}
    -> m ()
checksumFree checksum = liftIO $ do
    checksum' <- unsafeManagedPtrGetPtr checksum
    g_checksum_free checksum'
    touchManagedPtr checksum
    return ()

#ifdef ENABLE_OVERLOADING
data ChecksumFreeMethodInfo
instance (signature ~ (m ()), MonadIO m) => O.MethodInfo ChecksumFreeMethodInfo Checksum signature where
    overloadedMethod _ = checksumFree

#endif

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

foreign import ccall "g_checksum_get_string" g_checksum_get_string :: 
    Ptr Checksum ->                         -- checksum : TInterface (Name {namespace = "GLib", name = "Checksum"})
    IO CString

{- |
Gets the digest as an hexadecimal string.

Once this function has been called the 'GI.GLib.Structs.Checksum.Checksum' can no longer be
updated with 'GI.GLib.Structs.Checksum.checksumUpdate'.

The hexadecimal characters will be lower case.

@since 2.16
-}
checksumGetString ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Checksum
    {- ^ /@checksum@/: a 'GI.GLib.Structs.Checksum.Checksum' -}
    -> m T.Text
    {- ^ __Returns:__ the hexadecimal representation of the checksum. The
  returned string is owned by the checksum and should not be modified
  or freed. -}
checksumGetString checksum = liftIO $ do
    checksum' <- unsafeManagedPtrGetPtr checksum
    result <- g_checksum_get_string checksum'
    checkUnexpectedReturnNULL "checksumGetString" result
    result' <- cstringToText result
    touchManagedPtr checksum
    return result'

#ifdef ENABLE_OVERLOADING
data ChecksumGetStringMethodInfo
instance (signature ~ (m T.Text), MonadIO m) => O.MethodInfo ChecksumGetStringMethodInfo Checksum signature where
    overloadedMethod _ = checksumGetString

#endif

-- method Checksum::reset
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "checksum", argType = TInterface (Name {namespace = "GLib", name = "Checksum"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the #GChecksum to reset", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "g_checksum_reset" g_checksum_reset :: 
    Ptr Checksum ->                         -- checksum : TInterface (Name {namespace = "GLib", name = "Checksum"})
    IO ()

{- |
Resets the state of the /@checksum@/ back to its initial state.

@since 2.18
-}
checksumReset ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Checksum
    {- ^ /@checksum@/: the 'GI.GLib.Structs.Checksum.Checksum' to reset -}
    -> m ()
checksumReset checksum = liftIO $ do
    checksum' <- unsafeManagedPtrGetPtr checksum
    g_checksum_reset checksum'
    touchManagedPtr checksum
    return ()

#ifdef ENABLE_OVERLOADING
data ChecksumResetMethodInfo
instance (signature ~ (m ()), MonadIO m) => O.MethodInfo ChecksumResetMethodInfo Checksum signature where
    overloadedMethod _ = checksumReset

#endif

-- method Checksum::update
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "checksum", argType = TInterface (Name {namespace = "GLib", name = "Checksum"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GChecksum", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "data", argType = TCArray False (-1) 2 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "buffer used to compute the checksum", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "length", argType = TBasicType TInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "size of the buffer, or -1 if it is a null-terminated string.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : [Arg {argCName = "length", argType = TBasicType TInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "size of the buffer, or -1 if it is a null-terminated string.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "g_checksum_update" g_checksum_update :: 
    Ptr Checksum ->                         -- checksum : TInterface (Name {namespace = "GLib", name = "Checksum"})
    Ptr Word8 ->                            -- data : TCArray False (-1) 2 (TBasicType TUInt8)
    Int64 ->                                -- length : TBasicType TInt64
    IO ()

{- |
Feeds /@data@/ into an existing 'GI.GLib.Structs.Checksum.Checksum'. The checksum must still be
open, that is 'GI.GLib.Structs.Checksum.checksumGetString' or @/g_checksum_get_digest()/@ must
not have been called on /@checksum@/.

@since 2.16
-}
checksumUpdate ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Checksum
    {- ^ /@checksum@/: a 'GI.GLib.Structs.Checksum.Checksum' -}
    -> ByteString
    {- ^ /@data@/: buffer used to compute the checksum -}
    -> m ()
checksumUpdate checksum data_ = liftIO $ do
    let length_ = fromIntegral $ B.length data_
    checksum' <- unsafeManagedPtrGetPtr checksum
    data_' <- packByteString data_
    g_checksum_update checksum' data_' length_
    touchManagedPtr checksum
    freeMem data_'
    return ()

#ifdef ENABLE_OVERLOADING
data ChecksumUpdateMethodInfo
instance (signature ~ (ByteString -> m ()), MonadIO m) => O.MethodInfo ChecksumUpdateMethodInfo Checksum signature where
    overloadedMethod _ = checksumUpdate

#endif

-- method Checksum::type_get_length
-- method type : MemberFunction
-- Args : [Arg {argCName = "checksum_type", argType = TInterface (Name {namespace = "GLib", name = "ChecksumType"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GChecksumType", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TInt64)
-- throws : False
-- Skip return : False

foreign import ccall "g_checksum_type_get_length" g_checksum_type_get_length :: 
    CUInt ->                                -- checksum_type : TInterface (Name {namespace = "GLib", name = "ChecksumType"})
    IO Int64

{- |
Gets the length in bytes of digests of type /@checksumType@/

@since 2.16
-}
checksumTypeGetLength ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    GLib.Enums.ChecksumType
    {- ^ /@checksumType@/: a 'GI.GLib.Enums.ChecksumType' -}
    -> m Int64
    {- ^ __Returns:__ the checksum length, or -1 if /@checksumType@/ is
not supported. -}
checksumTypeGetLength checksumType = liftIO $ do
    let checksumType' = (fromIntegral . fromEnum) checksumType
    result <- g_checksum_type_get_length checksumType'
    return result

#ifdef ENABLE_OVERLOADING
#endif

#ifdef ENABLE_OVERLOADING
type family ResolveChecksumMethod (t :: Symbol) (o :: *) :: * where
    ResolveChecksumMethod "copy" o = ChecksumCopyMethodInfo
    ResolveChecksumMethod "free" o = ChecksumFreeMethodInfo
    ResolveChecksumMethod "reset" o = ChecksumResetMethodInfo
    ResolveChecksumMethod "update" o = ChecksumUpdateMethodInfo
    ResolveChecksumMethod "getString" o = ChecksumGetStringMethodInfo
    ResolveChecksumMethod l o = O.MethodResolutionFailed l o

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

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

#endif