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

A structure which globally uniquely identifies a ref as the tuple
(/@collectionId@/, /@refName@/). For backwards compatibility, /@collectionId@/ may be 'Nothing',
indicating a ref name which is not globally unique.

/Since: 2018.6/
-}

#define ENABLE_OVERLOADING (MIN_VERSION_haskell_gi_overloading(1,0,0) \
       && !defined(__HADDOCK_VERSION__))

module GI.OSTree.Structs.CollectionRef
    (

-- * Exported types
    CollectionRef(..)                       ,
    newZeroCollectionRef                    ,
    noCollectionRef                         ,


 -- * Methods
-- ** dup #method:dup#

#if ENABLE_OVERLOADING
    CollectionRefDupMethodInfo              ,
#endif
    collectionRefDup                        ,


-- ** dupv #method:dupv#

    collectionRefDupv                       ,


-- ** equal #method:equal#

    collectionRefEqual                      ,


-- ** free #method:free#

#if ENABLE_OVERLOADING
    CollectionRefFreeMethodInfo             ,
#endif
    collectionRefFree                       ,


-- ** freev #method:freev#

    collectionRefFreev                      ,


-- ** hash #method:hash#

    collectionRefHash                       ,


-- ** new #method:new#

    collectionRefNew                        ,




 -- * Properties
-- ** collectionId #attr:collectionId#
{- | collection ID which provided the ref, or 'Nothing' if there
   is no associated collection
-}
    clearCollectionRefCollectionId          ,
#if ENABLE_OVERLOADING
    collectionRef_collectionId              ,
#endif
    getCollectionRefCollectionId            ,
    setCollectionRefCollectionId            ,


-- ** refName #attr:refName#
{- | ref name
-}
    clearCollectionRefRefName               ,
#if ENABLE_OVERLOADING
    collectionRef_refName                   ,
#endif
    getCollectionRefRefName                 ,
    setCollectionRefRefName                 ,




    ) 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.GClosure as B.GClosure
import qualified Data.GI.Base.GError as B.GError
import qualified Data.GI.Base.GVariant as B.GVariant
import qualified Data.GI.Base.GValue as B.GValue
import qualified Data.GI.Base.GParamSpec as B.GParamSpec
import qualified Data.GI.Base.CallStack as B.CallStack
import qualified Data.GI.Base.Properties as B.Properties
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 GHC.OverloadedLabels as OL


-- | Memory-managed wrapper type.
newtype CollectionRef = CollectionRef (ManagedPtr CollectionRef)
foreign import ccall "ostree_collection_ref_get_type" c_ostree_collection_ref_get_type ::
    IO GType

instance BoxedObject CollectionRef where
    boxedType _ = c_ostree_collection_ref_get_type

-- | Construct a `CollectionRef` struct initialized to zero.
newZeroCollectionRef :: MonadIO m => m CollectionRef
newZeroCollectionRef = liftIO $ callocBoxedBytes 16 >>= wrapBoxed CollectionRef

instance tag ~ 'AttrSet => Constructible CollectionRef tag where
    new _ attrs = do
        o <- newZeroCollectionRef
        GI.Attributes.set o attrs
        return o


-- | A convenience alias for `Nothing` :: `Maybe` `CollectionRef`.
noCollectionRef :: Maybe CollectionRef
noCollectionRef = Nothing

{- |
Get the value of the “@collection_id@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.get' collectionRef #collectionId
@
-}
getCollectionRefCollectionId :: MonadIO m => CollectionRef -> m (Maybe T.Text)
getCollectionRefCollectionId s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 0) :: IO CString
    result <- SP.convertIfNonNull val $ \val' -> do
        val'' <- cstringToText val'
        return val''
    return result

{- |
Set the value of the “@collection_id@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.set' collectionRef [ #collectionId 'Data.GI.Base.Attributes.:=' value ]
@
-}
setCollectionRefCollectionId :: MonadIO m => CollectionRef -> CString -> m ()
setCollectionRefCollectionId s val = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 0) (val :: CString)

{- |
Set the value of the “@collection_id@” field to `Nothing`.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.clear' #collectionId
@
-}
clearCollectionRefCollectionId :: MonadIO m => CollectionRef -> m ()
clearCollectionRefCollectionId s = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 0) (FP.nullPtr :: CString)

#if ENABLE_OVERLOADING
data CollectionRefCollectionIdFieldInfo
instance AttrInfo CollectionRefCollectionIdFieldInfo where
    type AttrAllowedOps CollectionRefCollectionIdFieldInfo = '[ 'AttrSet, 'AttrGet, 'AttrClear]
    type AttrSetTypeConstraint CollectionRefCollectionIdFieldInfo = (~) CString
    type AttrBaseTypeConstraint CollectionRefCollectionIdFieldInfo = (~) CollectionRef
    type AttrGetType CollectionRefCollectionIdFieldInfo = Maybe T.Text
    type AttrLabel CollectionRefCollectionIdFieldInfo = "collection_id"
    type AttrOrigin CollectionRefCollectionIdFieldInfo = CollectionRef
    attrGet _ = getCollectionRefCollectionId
    attrSet _ = setCollectionRefCollectionId
    attrConstruct = undefined
    attrClear _ = clearCollectionRefCollectionId

collectionRef_collectionId :: AttrLabelProxy "collectionId"
collectionRef_collectionId = AttrLabelProxy

#endif


{- |
Get the value of the “@ref_name@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.get' collectionRef #refName
@
-}
getCollectionRefRefName :: MonadIO m => CollectionRef -> m (Maybe T.Text)
getCollectionRefRefName s = liftIO $ withManagedPtr s $ \ptr -> do
    val <- peek (ptr `plusPtr` 8) :: IO CString
    result <- SP.convertIfNonNull val $ \val' -> do
        val'' <- cstringToText val'
        return val''
    return result

{- |
Set the value of the “@ref_name@” field.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.set' collectionRef [ #refName 'Data.GI.Base.Attributes.:=' value ]
@
-}
setCollectionRefRefName :: MonadIO m => CollectionRef -> CString -> m ()
setCollectionRefRefName s val = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 8) (val :: CString)

{- |
Set the value of the “@ref_name@” field to `Nothing`.
When <https://github.com/haskell-gi/haskell-gi/wiki/Overloading overloading> is enabled, this is equivalent to

@
'Data.GI.Base.Attributes.clear' #refName
@
-}
clearCollectionRefRefName :: MonadIO m => CollectionRef -> m ()
clearCollectionRefRefName s = liftIO $ withManagedPtr s $ \ptr -> do
    poke (ptr `plusPtr` 8) (FP.nullPtr :: CString)

#if ENABLE_OVERLOADING
data CollectionRefRefNameFieldInfo
instance AttrInfo CollectionRefRefNameFieldInfo where
    type AttrAllowedOps CollectionRefRefNameFieldInfo = '[ 'AttrSet, 'AttrGet, 'AttrClear]
    type AttrSetTypeConstraint CollectionRefRefNameFieldInfo = (~) CString
    type AttrBaseTypeConstraint CollectionRefRefNameFieldInfo = (~) CollectionRef
    type AttrGetType CollectionRefRefNameFieldInfo = Maybe T.Text
    type AttrLabel CollectionRefRefNameFieldInfo = "ref_name"
    type AttrOrigin CollectionRefRefNameFieldInfo = CollectionRef
    attrGet _ = getCollectionRefRefName
    attrSet _ = setCollectionRefRefName
    attrConstruct = undefined
    attrClear _ = clearCollectionRefRefName

collectionRef_refName :: AttrLabelProxy "refName"
collectionRef_refName = AttrLabelProxy

#endif



#if ENABLE_OVERLOADING
instance O.HasAttributeList CollectionRef
type instance O.AttributeList CollectionRef = CollectionRefAttributeList
type CollectionRefAttributeList = ('[ '("collectionId", CollectionRefCollectionIdFieldInfo), '("refName", CollectionRefRefNameFieldInfo)] :: [(Symbol, *)])
#endif

-- method CollectionRef::new
-- method type : Constructor
-- Args : [Arg {argCName = "collection_id", argType = TBasicType TUTF8, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "a collection ID, or %NULL for a plain ref", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "ref_name", argType = TBasicType TUTF8, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a ref name", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "OSTree", name = "CollectionRef"}))
-- throws : False
-- Skip return : False

foreign import ccall "ostree_collection_ref_new" ostree_collection_ref_new ::
    CString ->                              -- collection_id : TBasicType TUTF8
    CString ->                              -- ref_name : TBasicType TUTF8
    IO (Ptr CollectionRef)

{- |
Create a new 'GI.OSTree.Structs.CollectionRef.CollectionRef' containing (/@collectionId@/, /@refName@/). If
/@collectionId@/ is 'Nothing', this is equivalent to a plain ref name string (not a
refspec; no remote name is included), which can be used for non-P2P
operations.

/Since: 2018.6/
-}
collectionRefNew ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Maybe (T.Text)
    {- ^ /@collectionId@/: a collection ID, or 'Nothing' for a plain ref -}
    -> T.Text
    {- ^ /@refName@/: a ref name -}
    -> m CollectionRef
    {- ^ __Returns:__ a new 'GI.OSTree.Structs.CollectionRef.CollectionRef' -}
collectionRefNew collectionId refName = liftIO $ do
    maybeCollectionId <- case collectionId of
        Nothing -> return nullPtr
        Just jCollectionId -> do
            jCollectionId' <- textToCString jCollectionId
            return jCollectionId'
    refName' <- textToCString refName
    result <- ostree_collection_ref_new maybeCollectionId refName'
    checkUnexpectedReturnNULL "collectionRefNew" result
    result' <- (wrapBoxed CollectionRef) result
    freeMem maybeCollectionId
    freeMem refName'
    return result'

#if ENABLE_OVERLOADING
#endif

-- method CollectionRef::dup
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "ref", argType = TInterface (Name {namespace = "OSTree", name = "CollectionRef"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "an #OstreeCollectionRef", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "OSTree", name = "CollectionRef"}))
-- throws : False
-- Skip return : False

foreign import ccall "ostree_collection_ref_dup" ostree_collection_ref_dup ::
    Ptr CollectionRef ->                    -- ref : TInterface (Name {namespace = "OSTree", name = "CollectionRef"})
    IO (Ptr CollectionRef)

{- |
Create a copy of the given /@ref@/.

/Since: 2018.6/
-}
collectionRefDup ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    CollectionRef
    {- ^ /@ref@/: an 'GI.OSTree.Structs.CollectionRef.CollectionRef' -}
    -> m CollectionRef
    {- ^ __Returns:__ a newly allocated copy of /@ref@/ -}
collectionRefDup ref = liftIO $ do
    ref' <- unsafeManagedPtrGetPtr ref
    result <- ostree_collection_ref_dup ref'
    checkUnexpectedReturnNULL "collectionRefDup" result
    result' <- (wrapBoxed CollectionRef) result
    touchManagedPtr ref
    return result'

#if ENABLE_OVERLOADING
data CollectionRefDupMethodInfo
instance (signature ~ (m CollectionRef), MonadIO m) => O.MethodInfo CollectionRefDupMethodInfo CollectionRef signature where
    overloadedMethod _ = collectionRefDup

#endif

-- method CollectionRef::free
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "ref", argType = TInterface (Name {namespace = "OSTree", name = "CollectionRef"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "an #OstreeCollectionRef", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "ostree_collection_ref_free" ostree_collection_ref_free ::
    Ptr CollectionRef ->                    -- ref : TInterface (Name {namespace = "OSTree", name = "CollectionRef"})
    IO ()

{- |
Free the given /@ref@/.

/Since: 2018.6/
-}
collectionRefFree ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    CollectionRef
    {- ^ /@ref@/: an 'GI.OSTree.Structs.CollectionRef.CollectionRef' -}
    -> m ()
collectionRefFree ref = liftIO $ do
    ref' <- B.ManagedPtr.disownBoxed ref
    ostree_collection_ref_free ref'
    touchManagedPtr ref
    return ()

#if ENABLE_OVERLOADING
data CollectionRefFreeMethodInfo
instance (signature ~ (m ()), MonadIO m) => O.MethodInfo CollectionRefFreeMethodInfo CollectionRef signature where
    overloadedMethod _ = collectionRefFree

#endif

-- method CollectionRef::dupv
-- method type : MemberFunction
-- Args : [Arg {argCName = "refs", argType = TCArray True (-1) (-1) (TInterface (Name {namespace = "OSTree", name = "CollectionRef"})), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "%NULL-terminated array of #OstreeCollectionRefs", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TCArray True (-1) (-1) (TInterface (Name {namespace = "OSTree", name = "CollectionRef"})))
-- throws : False
-- Skip return : False

foreign import ccall "ostree_collection_ref_dupv" ostree_collection_ref_dupv ::
    Ptr (Ptr CollectionRef) ->              -- refs : TCArray True (-1) (-1) (TInterface (Name {namespace = "OSTree", name = "CollectionRef"}))
    IO (Ptr (Ptr CollectionRef))

{- |
Copy an array of @/OstreeCollectionRefs/@, including deep copies of all its
elements. /@refs@/ must be 'Nothing'-terminated; it may be empty, but must not be
'Nothing'.

/Since: 2018.6/
-}
collectionRefDupv ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    [CollectionRef]
    {- ^ /@refs@/: 'Nothing'-terminated array of @/OstreeCollectionRefs/@ -}
    -> m [CollectionRef]
    {- ^ __Returns:__ a newly allocated copy of /@refs@/ -}
collectionRefDupv refs = liftIO $ do
    refs' <- mapM unsafeManagedPtrGetPtr refs
    refs'' <- packZeroTerminatedPtrArray refs'
    result <- ostree_collection_ref_dupv refs''
    checkUnexpectedReturnNULL "collectionRefDupv" result
    result' <- unpackZeroTerminatedPtrArray result
    result'' <- mapM (wrapBoxed CollectionRef) result'
    freeMem result
    mapM_ touchManagedPtr refs
    freeMem refs''
    return result''

#if ENABLE_OVERLOADING
#endif

-- method CollectionRef::equal
-- method type : MemberFunction
-- Args : [Arg {argCName = "ref1", argType = TBasicType TPtr, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "an #OstreeCollectionRef", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "ref2", argType = TBasicType TPtr, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "another #OstreeCollectionRef", 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 "ostree_collection_ref_equal" ostree_collection_ref_equal ::
    Ptr () ->                               -- ref1 : TBasicType TPtr
    Ptr () ->                               -- ref2 : TBasicType TPtr
    IO CInt

{- |
Compare /@ref1@/ and /@ref2@/ and return 'True' if they have the same collection ID and
ref name, and 'False' otherwise. Both /@ref1@/ and /@ref2@/ must be non-'Nothing'.

/Since: 2018.6/
-}
collectionRefEqual ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Ptr ()
    {- ^ /@ref1@/: an 'GI.OSTree.Structs.CollectionRef.CollectionRef' -}
    -> Ptr ()
    {- ^ /@ref2@/: another 'GI.OSTree.Structs.CollectionRef.CollectionRef' -}
    -> m Bool
    {- ^ __Returns:__ 'True' if /@ref1@/ and /@ref2@/ are equal, 'False' otherwise -}
collectionRefEqual ref1 ref2 = liftIO $ do
    result <- ostree_collection_ref_equal ref1 ref2
    let result' = (/= 0) result
    return result'

#if ENABLE_OVERLOADING
#endif

-- method CollectionRef::freev
-- method type : MemberFunction
-- Args : [Arg {argCName = "refs", argType = TCArray True (-1) (-1) (TInterface (Name {namespace = "OSTree", name = "CollectionRef"})), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "an array of #OstreeCollectionRefs", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "ostree_collection_ref_freev" ostree_collection_ref_freev ::
    Ptr (Ptr CollectionRef) ->              -- refs : TCArray True (-1) (-1) (TInterface (Name {namespace = "OSTree", name = "CollectionRef"}))
    IO ()

{- |
Free the given array of /@refs@/, including freeing all its elements. /@refs@/
must be 'Nothing'-terminated; it may be empty, but must not be 'Nothing'.

/Since: 2018.6/
-}
collectionRefFreev ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    [CollectionRef]
    {- ^ /@refs@/: an array of @/OstreeCollectionRefs/@ -}
    -> m ()
collectionRefFreev refs = liftIO $ do
    refs' <- mapM B.ManagedPtr.disownBoxed refs
    refs'' <- packZeroTerminatedPtrArray refs'
    ostree_collection_ref_freev refs''
    mapM_ touchManagedPtr refs
    return ()

#if ENABLE_OVERLOADING
#endif

-- method CollectionRef::hash
-- method type : MemberFunction
-- Args : [Arg {argCName = "ref", argType = TBasicType TPtr, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "an #OstreeCollectionRef", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TUInt)
-- throws : False
-- Skip return : False

foreign import ccall "ostree_collection_ref_hash" ostree_collection_ref_hash ::
    Ptr () ->                               -- ref : TBasicType TPtr
    IO Word32

{- |
Hash the given /@ref@/. This function is suitable for use with 'GI.GLib.Structs.HashTable.HashTable'.
/@ref@/ must be non-'Nothing'.

/Since: 2018.6/
-}
collectionRefHash ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Ptr ()
    {- ^ /@ref@/: an 'GI.OSTree.Structs.CollectionRef.CollectionRef' -}
    -> m Word32
    {- ^ __Returns:__ hash value for /@ref@/ -}
collectionRefHash ref = liftIO $ do
    result <- ostree_collection_ref_hash ref
    return result

#if ENABLE_OVERLOADING
#endif

#if ENABLE_OVERLOADING
type family ResolveCollectionRefMethod (t :: Symbol) (o :: *) :: * where
    ResolveCollectionRefMethod "dup" o = CollectionRefDupMethodInfo
    ResolveCollectionRefMethod "free" o = CollectionRefFreeMethodInfo
    ResolveCollectionRefMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolveCollectionRefMethod t CollectionRef, O.MethodInfo info CollectionRef p) => OL.IsLabel t (CollectionRef -> 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