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

Represents a blob object.
-}

module GI.Ggit.Objects.Blob
    ( 

-- * Exported types
    Blob(..)                                ,
    IsBlob                                  ,
    toBlob                                  ,
    noBlob                                  ,


 -- * Methods
-- ** getRawContent #method:getRawContent#
#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
    BlobGetRawContentMethodInfo             ,
#endif
    blobGetRawContent                       ,


-- ** isBinary #method:isBinary#
#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
    BlobIsBinaryMethodInfo                  ,
#endif
    blobIsBinary                            ,




    ) 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 qualified GI.GObject.Objects.Object as GObject.Object
import {-# SOURCE #-} qualified GI.Ggit.Objects.Native as Ggit.Native
import {-# SOURCE #-} qualified GI.Ggit.Objects.Object as Ggit.Object
import {-# SOURCE #-} qualified GI.Ggit.Objects.ObjectFactoryBase as Ggit.ObjectFactoryBase

newtype Blob = Blob (ManagedPtr Blob)
foreign import ccall "ggit_blob_get_type"
    c_ggit_blob_get_type :: IO GType

instance GObject Blob where
    gobjectType _ = c_ggit_blob_get_type
    

class GObject o => IsBlob o
#if MIN_VERSION_base(4,9,0)
instance {-# OVERLAPPABLE #-} (GObject a, O.UnknownAncestorError Blob a) =>
    IsBlob a
#endif
instance IsBlob Blob
instance Ggit.Object.IsObject Blob
instance Ggit.Native.IsNative Blob
instance Ggit.ObjectFactoryBase.IsObjectFactoryBase Blob
instance GObject.Object.IsObject Blob

toBlob :: (MonadIO m, IsBlob o) => o -> m Blob
toBlob = liftIO . unsafeCastTo Blob

noBlob :: Maybe Blob
noBlob = Nothing

#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
type family ResolveBlobMethod (t :: Symbol) (o :: *) :: * where
    ResolveBlobMethod "bindProperty" o = GObject.Object.ObjectBindPropertyMethodInfo
    ResolveBlobMethod "bindPropertyFull" o = GObject.Object.ObjectBindPropertyFullMethodInfo
    ResolveBlobMethod "forceFloating" o = GObject.Object.ObjectForceFloatingMethodInfo
    ResolveBlobMethod "freezeNotify" o = GObject.Object.ObjectFreezeNotifyMethodInfo
    ResolveBlobMethod "isBinary" o = BlobIsBinaryMethodInfo
    ResolveBlobMethod "isFloating" o = GObject.Object.ObjectIsFloatingMethodInfo
    ResolveBlobMethod "notify" o = GObject.Object.ObjectNotifyMethodInfo
    ResolveBlobMethod "notifyByPspec" o = GObject.Object.ObjectNotifyByPspecMethodInfo
    ResolveBlobMethod "ref" o = GObject.Object.ObjectRefMethodInfo
    ResolveBlobMethod "refSink" o = GObject.Object.ObjectRefSinkMethodInfo
    ResolveBlobMethod "replaceData" o = GObject.Object.ObjectReplaceDataMethodInfo
    ResolveBlobMethod "replaceQdata" o = GObject.Object.ObjectReplaceQdataMethodInfo
    ResolveBlobMethod "runDispose" o = GObject.Object.ObjectRunDisposeMethodInfo
    ResolveBlobMethod "stealData" o = GObject.Object.ObjectStealDataMethodInfo
    ResolveBlobMethod "stealQdata" o = GObject.Object.ObjectStealQdataMethodInfo
    ResolveBlobMethod "thawNotify" o = GObject.Object.ObjectThawNotifyMethodInfo
    ResolveBlobMethod "unref" o = GObject.Object.ObjectUnrefMethodInfo
    ResolveBlobMethod "watchClosure" o = GObject.Object.ObjectWatchClosureMethodInfo
    ResolveBlobMethod "getData" o = GObject.Object.ObjectGetDataMethodInfo
    ResolveBlobMethod "getId" o = Ggit.Object.ObjectGetIdMethodInfo
    ResolveBlobMethod "getOwner" o = Ggit.Object.ObjectGetOwnerMethodInfo
    ResolveBlobMethod "getProperty" o = GObject.Object.ObjectGetPropertyMethodInfo
    ResolveBlobMethod "getQdata" o = GObject.Object.ObjectGetQdataMethodInfo
    ResolveBlobMethod "getRawContent" o = BlobGetRawContentMethodInfo
    ResolveBlobMethod "setData" o = GObject.Object.ObjectSetDataMethodInfo
    ResolveBlobMethod "setProperty" o = GObject.Object.ObjectSetPropertyMethodInfo
    ResolveBlobMethod l o = O.MethodResolutionFailed l o

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

#if MIN_VERSION_base(4,9,0)
instance (info ~ ResolveBlobMethod t Blob, O.MethodInfo info Blob p) => O.IsLabel t (Blob -> 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

#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
instance O.HasAttributeList Blob
type instance O.AttributeList Blob = BlobAttributeList
type BlobAttributeList = ('[ '("native", Ggit.Native.NativeNativePropertyInfo)] :: [(Symbol, *)])
#endif

#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
#endif

#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
type instance O.SignalList Blob = BlobSignalList
type BlobSignalList = ('[ '("notify", GObject.Object.ObjectNotifySignalInfo)] :: [(Symbol, *)])

#endif

-- method Blob::get_raw_content
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "blob", argType = TInterface (Name {namespace = "Ggit", name = "Blob"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GgitBlob.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "length", argType = TBasicType TUInt64, direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "return value of the length of the data.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- Lengths : [Arg {argCName = "length", argType = TBasicType TUInt64, direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "return value of the length of the data.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- returnType : Just (TCArray False (-1) 1 (TBasicType TUInt8))
-- throws : False
-- Skip return : False

foreign import ccall "ggit_blob_get_raw_content" ggit_blob_get_raw_content :: 
    Ptr Blob ->                             -- blob : TInterface (Name {namespace = "Ggit", name = "Blob"})
    Ptr Word64 ->                           -- length : TBasicType TUInt64
    IO (Ptr Word8)

{- |
Gets a read-only buffer with the raw contents of /@blob@/.

A pointer to the raw contents of /@blob@/ is returned.
This pointer is owned internally by /@object@/ and must
not be free\'d. The pointer may be invalidated at a later
time.
-}
blobGetRawContent ::
    (B.CallStack.HasCallStack, MonadIO m, IsBlob a) =>
    a
    {- ^ /@blob@/: a 'GI.Ggit.Objects.Blob.Blob'. -}
    -> m ByteString
    {- ^ __Returns:__ the blob content or
         'Nothing' if the blob does not have any content. -}
blobGetRawContent blob = liftIO $ do
    blob' <- unsafeManagedPtrCastPtr blob
    length_ <- allocMem :: IO (Ptr Word64)
    result <- ggit_blob_get_raw_content blob' length_
    length_' <- peek length_
    checkUnexpectedReturnNULL "blobGetRawContent" result
    result' <- (unpackByteStringWithLength length_') result
    touchManagedPtr blob
    freeMem length_
    return result'

#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
data BlobGetRawContentMethodInfo
instance (signature ~ (m ByteString), MonadIO m, IsBlob a) => O.MethodInfo BlobGetRawContentMethodInfo a signature where
    overloadedMethod _ = blobGetRawContent

#endif

-- method Blob::is_binary
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "blob", argType = TInterface (Name {namespace = "Ggit", name = "Blob"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GgitBlob.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TBoolean)
-- throws : False
-- Skip return : False

foreign import ccall "ggit_blob_is_binary" ggit_blob_is_binary :: 
    Ptr Blob ->                             -- blob : TInterface (Name {namespace = "Ggit", name = "Blob"})
    IO CInt

{- |
Check whether the blob is binary.
-}
blobIsBinary ::
    (B.CallStack.HasCallStack, MonadIO m, IsBlob a) =>
    a
    {- ^ /@blob@/: a 'GI.Ggit.Objects.Blob.Blob'. -}
    -> m Bool
    {- ^ __Returns:__ 'True' if the blob is binary, 'False' otherwise. -}
blobIsBinary blob = liftIO $ do
    blob' <- unsafeManagedPtrCastPtr blob
    result <- ggit_blob_is_binary blob'
    let result' = (/= 0) result
    touchManagedPtr blob
    return result'

#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
data BlobIsBinaryMethodInfo
instance (signature ~ (m Bool), MonadIO m, IsBlob a) => O.MethodInfo BlobIsBinaryMethodInfo a signature where
    overloadedMethod _ = blobIsBinary

#endif