{- |
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 AtkRelation describes a relation between an object and one or
more other objects. The actual relations that an object has with
other objects are defined as an AtkRelationSet, which is a set of
AtkRelations.
-}

module GI.Atk.Objects.Relation
    ( 

-- * Exported types
    Relation(..)                            ,
    IsRelation                              ,
    toRelation                              ,
    noRelation                              ,


 -- * Methods
-- ** addTarget #method:addTarget#
    RelationAddTargetMethodInfo             ,
    relationAddTarget                       ,


-- ** getRelationType #method:getRelationType#
    RelationGetRelationTypeMethodInfo       ,
    relationGetRelationType                 ,


-- ** getTarget #method:getTarget#
    RelationGetTargetMethodInfo             ,
    relationGetTarget                       ,


-- ** new #method:new#
    relationNew                             ,


-- ** removeTarget #method:removeTarget#
    RelationRemoveTargetMethodInfo          ,
    relationRemoveTarget                    ,




 -- * Properties
-- ** relationType #attr:relationType#
    RelationRelationTypePropertyInfo        ,
    constructRelationRelationType           ,
    getRelationRelationType                 ,
    relationRelationType                    ,
    setRelationRelationType                 ,


-- ** target #attr:target#
    RelationTargetPropertyInfo              ,
    clearRelationTarget                     ,
    constructRelationTarget                 ,
    getRelationTarget                       ,
    relationTarget                          ,
    setRelationTarget                       ,




    ) 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.Atk.Enums as Atk.Enums
import {-# SOURCE #-} qualified GI.Atk.Objects.Object as Atk.Object
import qualified GI.GObject.Objects.Object as GObject.Object
import qualified GI.GObject.Structs.ValueArray as GObject.ValueArray

newtype Relation = Relation (ManagedPtr Relation)
foreign import ccall "atk_relation_get_type"
    c_atk_relation_get_type :: IO GType

instance GObject Relation where
    gobjectType _ = c_atk_relation_get_type
    

class GObject o => IsRelation o
#if MIN_VERSION_base(4,9,0)
instance {-# OVERLAPPABLE #-} (GObject a, O.UnknownAncestorError Relation a) =>
    IsRelation a
#endif
instance IsRelation Relation
instance GObject.Object.IsObject Relation

toRelation :: IsRelation o => o -> IO Relation
toRelation = unsafeCastTo Relation

noRelation :: Maybe Relation
noRelation = Nothing

type family ResolveRelationMethod (t :: Symbol) (o :: *) :: * where
    ResolveRelationMethod "addTarget" o = RelationAddTargetMethodInfo
    ResolveRelationMethod "bindProperty" o = GObject.Object.ObjectBindPropertyMethodInfo
    ResolveRelationMethod "bindPropertyFull" o = GObject.Object.ObjectBindPropertyFullMethodInfo
    ResolveRelationMethod "forceFloating" o = GObject.Object.ObjectForceFloatingMethodInfo
    ResolveRelationMethod "freezeNotify" o = GObject.Object.ObjectFreezeNotifyMethodInfo
    ResolveRelationMethod "isFloating" o = GObject.Object.ObjectIsFloatingMethodInfo
    ResolveRelationMethod "notify" o = GObject.Object.ObjectNotifyMethodInfo
    ResolveRelationMethod "notifyByPspec" o = GObject.Object.ObjectNotifyByPspecMethodInfo
    ResolveRelationMethod "ref" o = GObject.Object.ObjectRefMethodInfo
    ResolveRelationMethod "refSink" o = GObject.Object.ObjectRefSinkMethodInfo
    ResolveRelationMethod "removeTarget" o = RelationRemoveTargetMethodInfo
    ResolveRelationMethod "replaceData" o = GObject.Object.ObjectReplaceDataMethodInfo
    ResolveRelationMethod "replaceQdata" o = GObject.Object.ObjectReplaceQdataMethodInfo
    ResolveRelationMethod "runDispose" o = GObject.Object.ObjectRunDisposeMethodInfo
    ResolveRelationMethod "stealData" o = GObject.Object.ObjectStealDataMethodInfo
    ResolveRelationMethod "stealQdata" o = GObject.Object.ObjectStealQdataMethodInfo
    ResolveRelationMethod "thawNotify" o = GObject.Object.ObjectThawNotifyMethodInfo
    ResolveRelationMethod "unref" o = GObject.Object.ObjectUnrefMethodInfo
    ResolveRelationMethod "watchClosure" o = GObject.Object.ObjectWatchClosureMethodInfo
    ResolveRelationMethod "getData" o = GObject.Object.ObjectGetDataMethodInfo
    ResolveRelationMethod "getProperty" o = GObject.Object.ObjectGetPropertyMethodInfo
    ResolveRelationMethod "getQdata" o = GObject.Object.ObjectGetQdataMethodInfo
    ResolveRelationMethod "getRelationType" o = RelationGetRelationTypeMethodInfo
    ResolveRelationMethod "getTarget" o = RelationGetTargetMethodInfo
    ResolveRelationMethod "setData" o = GObject.Object.ObjectSetDataMethodInfo
    ResolveRelationMethod "setProperty" o = GObject.Object.ObjectSetPropertyMethodInfo
    ResolveRelationMethod l o = O.MethodResolutionFailed l o

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

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

-- VVV Prop "relation-type"
   -- Type: TInterface (Name {namespace = "Atk", name = "RelationType"})
   -- Flags: [PropertyReadable,PropertyWritable]
   -- Nullable: (Just False,Nothing)

getRelationRelationType :: (MonadIO m, IsRelation o) => o -> m Atk.Enums.RelationType
getRelationRelationType obj = liftIO $ getObjectPropertyEnum obj "relation-type"

setRelationRelationType :: (MonadIO m, IsRelation o) => o -> Atk.Enums.RelationType -> m ()
setRelationRelationType obj val = liftIO $ setObjectPropertyEnum obj "relation-type" val

constructRelationRelationType :: (IsRelation o) => Atk.Enums.RelationType -> IO (GValueConstruct o)
constructRelationRelationType val = constructObjectPropertyEnum "relation-type" val

data RelationRelationTypePropertyInfo
instance AttrInfo RelationRelationTypePropertyInfo where
    type AttrAllowedOps RelationRelationTypePropertyInfo = '[ 'AttrSet, 'AttrConstruct, 'AttrGet]
    type AttrSetTypeConstraint RelationRelationTypePropertyInfo = (~) Atk.Enums.RelationType
    type AttrBaseTypeConstraint RelationRelationTypePropertyInfo = IsRelation
    type AttrGetType RelationRelationTypePropertyInfo = Atk.Enums.RelationType
    type AttrLabel RelationRelationTypePropertyInfo = "relation-type"
    type AttrOrigin RelationRelationTypePropertyInfo = Relation
    attrGet _ = getRelationRelationType
    attrSet _ = setRelationRelationType
    attrConstruct _ = constructRelationRelationType
    attrClear _ = undefined

-- VVV Prop "target"
   -- Type: TInterface (Name {namespace = "GObject", name = "ValueArray"})
   -- Flags: [PropertyReadable,PropertyWritable]
   -- Nullable: (Nothing,Nothing)

getRelationTarget :: (MonadIO m, IsRelation o) => o -> m (Maybe GObject.ValueArray.ValueArray)
getRelationTarget obj = liftIO $ getObjectPropertyBoxed obj "target" GObject.ValueArray.ValueArray

setRelationTarget :: (MonadIO m, IsRelation o) => o -> GObject.ValueArray.ValueArray -> m ()
setRelationTarget obj val = liftIO $ setObjectPropertyBoxed obj "target" (Just val)

constructRelationTarget :: (IsRelation o) => GObject.ValueArray.ValueArray -> IO (GValueConstruct o)
constructRelationTarget val = constructObjectPropertyBoxed "target" (Just val)

clearRelationTarget :: (MonadIO m, IsRelation o) => o -> m ()
clearRelationTarget obj = liftIO $ setObjectPropertyBoxed obj "target" (Nothing :: Maybe GObject.ValueArray.ValueArray)

data RelationTargetPropertyInfo
instance AttrInfo RelationTargetPropertyInfo where
    type AttrAllowedOps RelationTargetPropertyInfo = '[ 'AttrSet, 'AttrConstruct, 'AttrGet, 'AttrClear]
    type AttrSetTypeConstraint RelationTargetPropertyInfo = (~) GObject.ValueArray.ValueArray
    type AttrBaseTypeConstraint RelationTargetPropertyInfo = IsRelation
    type AttrGetType RelationTargetPropertyInfo = (Maybe GObject.ValueArray.ValueArray)
    type AttrLabel RelationTargetPropertyInfo = "target"
    type AttrOrigin RelationTargetPropertyInfo = Relation
    attrGet _ = getRelationTarget
    attrSet _ = setRelationTarget
    attrConstruct _ = constructRelationTarget
    attrClear _ = clearRelationTarget

instance O.HasAttributeList Relation
type instance O.AttributeList Relation = RelationAttributeList
type RelationAttributeList = ('[ '("relationType", RelationRelationTypePropertyInfo), '("target", RelationTargetPropertyInfo)] :: [(Symbol, *)])

relationRelationType :: AttrLabelProxy "relationType"
relationRelationType = AttrLabelProxy

relationTarget :: AttrLabelProxy "target"
relationTarget = AttrLabelProxy

type instance O.SignalList Relation = RelationSignalList
type RelationSignalList = ('[ '("notify", GObject.Object.ObjectNotifySignalInfo)] :: [(Symbol, *)])

-- method Relation::new
-- method type : Constructor
-- Args : [Arg {argCName = "targets", argType = TCArray False (-1) 1 (TInterface (Name {namespace = "Atk", name = "Object"})), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "an array of pointers to\n #AtkObjects", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "n_targets", argType = TBasicType TInt, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "number of #AtkObjects pointed to by @targets", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "relationship", argType = TInterface (Name {namespace = "Atk", name = "RelationType"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "an #AtkRelationType with which to create the new\n #AtkRelation", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : [Arg {argCName = "n_targets", argType = TBasicType TInt, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "number of #AtkObjects pointed to by @targets", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- returnType : Just (TInterface (Name {namespace = "Atk", name = "Relation"}))
-- throws : False
-- Skip return : False

foreign import ccall "atk_relation_new" atk_relation_new :: 
    Ptr (Ptr Atk.Object.Object) ->          -- targets : TCArray False (-1) 1 (TInterface (Name {namespace = "Atk", name = "Object"}))
    Int32 ->                                -- n_targets : TBasicType TInt
    CUInt ->                                -- relationship : TInterface (Name {namespace = "Atk", name = "RelationType"})
    IO (Ptr Relation)

{- |
Create a new relation for the specified key and the specified list
of targets.  See also 'GI.Atk.Objects.Object.objectAddRelationship'.
-}
relationNew ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    [Atk.Object.Object]
    {- ^ /@targets@/: an array of pointers to
 @/AtkObjects/@ -}
    -> Atk.Enums.RelationType
    {- ^ /@relationship@/: an 'GI.Atk.Enums.RelationType' with which to create the new
 'GI.Atk.Objects.Relation.Relation' -}
    -> m Relation
    {- ^ __Returns:__ a pointer to a new 'GI.Atk.Objects.Relation.Relation' -}
relationNew targets relationship = liftIO $ do
    let nTargets = fromIntegral $ length targets
    targets' <- mapM unsafeManagedPtrCastPtr targets
    targets'' <- packPtrArray targets'
    let relationship' = (fromIntegral . fromEnum) relationship
    result <- atk_relation_new targets'' nTargets relationship'
    checkUnexpectedReturnNULL "relationNew" result
    result' <- (wrapObject Relation) result
    mapM_ touchManagedPtr targets
    freeMem targets''
    return result'

-- method Relation::add_target
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "relation", argType = TInterface (Name {namespace = "Atk", name = "Relation"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "an #AtkRelation", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "target", argType = TInterface (Name {namespace = "Atk", name = "Object"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "an #AtkObject", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "atk_relation_add_target" atk_relation_add_target :: 
    Ptr Relation ->                         -- relation : TInterface (Name {namespace = "Atk", name = "Relation"})
    Ptr Atk.Object.Object ->                -- target : TInterface (Name {namespace = "Atk", name = "Object"})
    IO ()

{- |
Adds the specified AtkObject to the target for the relation, if it is
not already present.  See also 'GI.Atk.Objects.Object.objectAddRelationship'.

@since 1.9
-}
relationAddTarget ::
    (B.CallStack.HasCallStack, MonadIO m, IsRelation a, Atk.Object.IsObject b) =>
    a
    {- ^ /@relation@/: an 'GI.Atk.Objects.Relation.Relation' -}
    -> b
    {- ^ /@target@/: an 'GI.Atk.Objects.Object.Object' -}
    -> m ()
relationAddTarget relation target = liftIO $ do
    relation' <- unsafeManagedPtrCastPtr relation
    target' <- unsafeManagedPtrCastPtr target
    atk_relation_add_target relation' target'
    touchManagedPtr relation
    touchManagedPtr target
    return ()

data RelationAddTargetMethodInfo
instance (signature ~ (b -> m ()), MonadIO m, IsRelation a, Atk.Object.IsObject b) => O.MethodInfo RelationAddTargetMethodInfo a signature where
    overloadedMethod _ = relationAddTarget

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

foreign import ccall "atk_relation_get_relation_type" atk_relation_get_relation_type :: 
    Ptr Relation ->                         -- relation : TInterface (Name {namespace = "Atk", name = "Relation"})
    IO CUInt

{- |
Gets the type of /@relation@/
-}
relationGetRelationType ::
    (B.CallStack.HasCallStack, MonadIO m, IsRelation a) =>
    a
    {- ^ /@relation@/: an 'GI.Atk.Objects.Relation.Relation' -}
    -> m Atk.Enums.RelationType
    {- ^ __Returns:__ the type of /@relation@/ -}
relationGetRelationType relation = liftIO $ do
    relation' <- unsafeManagedPtrCastPtr relation
    result <- atk_relation_get_relation_type relation'
    let result' = (toEnum . fromIntegral) result
    touchManagedPtr relation
    return result'

data RelationGetRelationTypeMethodInfo
instance (signature ~ (m Atk.Enums.RelationType), MonadIO m, IsRelation a) => O.MethodInfo RelationGetRelationTypeMethodInfo a signature where
    overloadedMethod _ = relationGetRelationType

-- method Relation::get_target
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "relation", argType = TInterface (Name {namespace = "Atk", name = "Relation"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "an #AtkRelation", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TPtrArray (TInterface (Name {namespace = "Atk", name = "Object"})))
-- throws : False
-- Skip return : False

foreign import ccall "atk_relation_get_target" atk_relation_get_target :: 
    Ptr Relation ->                         -- relation : TInterface (Name {namespace = "Atk", name = "Relation"})
    IO (Ptr (GPtrArray (Ptr Atk.Object.Object)))

{- |
Gets the target list of /@relation@/
-}
relationGetTarget ::
    (B.CallStack.HasCallStack, MonadIO m, IsRelation a) =>
    a
    {- ^ /@relation@/: an 'GI.Atk.Objects.Relation.Relation' -}
    -> m [Atk.Object.Object]
    {- ^ __Returns:__ the target list of /@relation@/ -}
relationGetTarget relation = liftIO $ do
    relation' <- unsafeManagedPtrCastPtr relation
    result <- atk_relation_get_target relation'
    checkUnexpectedReturnNULL "relationGetTarget" result
    result' <- unpackGPtrArray result
    result'' <- mapM (newObject Atk.Object.Object) result'
    touchManagedPtr relation
    return result''

data RelationGetTargetMethodInfo
instance (signature ~ (m [Atk.Object.Object]), MonadIO m, IsRelation a) => O.MethodInfo RelationGetTargetMethodInfo a signature where
    overloadedMethod _ = relationGetTarget

-- method Relation::remove_target
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "relation", argType = TInterface (Name {namespace = "Atk", name = "Relation"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "an #AtkRelation", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "target", argType = TInterface (Name {namespace = "Atk", name = "Object"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "an #AtkObject", 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 "atk_relation_remove_target" atk_relation_remove_target :: 
    Ptr Relation ->                         -- relation : TInterface (Name {namespace = "Atk", name = "Relation"})
    Ptr Atk.Object.Object ->                -- target : TInterface (Name {namespace = "Atk", name = "Object"})
    IO CInt

{- |
Remove the specified AtkObject from the target for the relation.
-}
relationRemoveTarget ::
    (B.CallStack.HasCallStack, MonadIO m, IsRelation a, Atk.Object.IsObject b) =>
    a
    {- ^ /@relation@/: an 'GI.Atk.Objects.Relation.Relation' -}
    -> b
    {- ^ /@target@/: an 'GI.Atk.Objects.Object.Object' -}
    -> m Bool
    {- ^ __Returns:__ TRUE if the removal is successful. -}
relationRemoveTarget relation target = liftIO $ do
    relation' <- unsafeManagedPtrCastPtr relation
    target' <- unsafeManagedPtrCastPtr target
    result <- atk_relation_remove_target relation' target'
    let result' = (/= 0) result
    touchManagedPtr relation
    touchManagedPtr target
    return result'

data RelationRemoveTargetMethodInfo
instance (signature ~ (b -> m Bool), MonadIO m, IsRelation a, Atk.Object.IsObject b) => O.MethodInfo RelationRemoveTargetMethodInfo a signature where
    overloadedMethod _ = relationRemoveTarget