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

'GI.Gio.Interfaces.ListModel.ListModel' is an interface that represents a mutable list of
@/GObjects/@. Its main intention is as a model for various widgets in
user interfaces, such as list views, but it can also be used as a
convenient method of returning lists of data, with support for
updates.

Each object in the list may also report changes in itself via some
mechanism (normally the 'GI.GObject.Objects.Object.Object'::@/notify/@ signal).  Taken together
with the 'GI.Gio.Interfaces.ListModel.ListModel'::@/items-changed/@ signal, this provides for a list
that can change its membership, and in which the members can change
their individual properties.

A good example would be the list of visible wireless network access
points, where each access point can report dynamic properties such as
signal strength.

It is important to note that the 'GI.Gio.Interfaces.ListModel.ListModel' itself does not report
changes to the individual items.  It only reports changes to the list
membership.  If you want to observe changes to the objects themselves
then you need to connect signals to the objects that you are
interested in.

All items in a 'GI.Gio.Interfaces.ListModel.ListModel' are of (or derived from) the same type.
'GI.Gio.Interfaces.ListModel.listModelGetItemType' returns that type.  The type may be an
interface, in which case all objects in the list must implement it.

The semantics are close to that of an array:
'GI.Gio.Interfaces.ListModel.listModelGetNItems' returns the number of items in the list and
@/g_list_model_get_item()/@ returns an item at a (0-based) position. In
order to allow implementations to calculate the list length lazily,
you can also iterate over items: starting from 0, repeatedly call
@/g_list_model_get_item()/@ until it returns 'Nothing'.

An implementation may create objects lazily, but must take care to
return the same object for a given position until all references to
it are gone.

On the other side, a consumer is expected only to hold references on
objects that are currently \"user visible\", in order to faciliate the
maximum level of laziness in the implementation of the list and to
reduce the required number of signal connections at a given time.

This interface is intended only to be used from a single thread.  The
thread in which it is appropriate to use it depends on the particular
implementation, but typically it will be from the thread that owns
the [thread-default main context][g-main-context-push-thread-default]
in effect at the time that the model was created.
-}

module GI.Gio.Interfaces.ListModel
    ( 

-- * Exported types
    ListModel(..)                           ,
    noListModel                             ,
    IsListModel                             ,
    toListModel                             ,


 -- * Methods
-- ** getItem #method:getItem#
    ListModelGetItemMethodInfo              ,
    listModelGetItem                        ,


-- ** getItemType #method:getItemType#
    ListModelGetItemTypeMethodInfo          ,
    listModelGetItemType                    ,


-- ** getNItems #method:getNItems#
    ListModelGetNItemsMethodInfo            ,
    listModelGetNItems                      ,


-- ** itemsChanged #method:itemsChanged#
    ListModelItemsChangedMethodInfo         ,
    listModelItemsChanged                   ,




 -- * Signals
-- ** itemsChanged #signal:itemsChanged#
    C_ListModelItemsChangedCallback         ,
    ListModelItemsChangedCallback           ,
    ListModelItemsChangedSignalInfo         ,
    afterListModelItemsChanged              ,
    genClosure_ListModelItemsChanged        ,
    mk_ListModelItemsChangedCallback        ,
    noListModelItemsChangedCallback         ,
    onListModelItemsChanged                 ,
    wrap_ListModelItemsChangedCallback      ,




    ) 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

-- interface ListModel 
newtype ListModel = ListModel (ManagedPtr ListModel)
noListModel :: Maybe ListModel
noListModel = Nothing

type family ResolveListModelMethod (t :: Symbol) (o :: *) :: * where
    ResolveListModelMethod "bindProperty" o = GObject.Object.ObjectBindPropertyMethodInfo
    ResolveListModelMethod "bindPropertyFull" o = GObject.Object.ObjectBindPropertyFullMethodInfo
    ResolveListModelMethod "forceFloating" o = GObject.Object.ObjectForceFloatingMethodInfo
    ResolveListModelMethod "freezeNotify" o = GObject.Object.ObjectFreezeNotifyMethodInfo
    ResolveListModelMethod "isFloating" o = GObject.Object.ObjectIsFloatingMethodInfo
    ResolveListModelMethod "itemsChanged" o = ListModelItemsChangedMethodInfo
    ResolveListModelMethod "notify" o = GObject.Object.ObjectNotifyMethodInfo
    ResolveListModelMethod "notifyByPspec" o = GObject.Object.ObjectNotifyByPspecMethodInfo
    ResolveListModelMethod "ref" o = GObject.Object.ObjectRefMethodInfo
    ResolveListModelMethod "refSink" o = GObject.Object.ObjectRefSinkMethodInfo
    ResolveListModelMethod "replaceData" o = GObject.Object.ObjectReplaceDataMethodInfo
    ResolveListModelMethod "replaceQdata" o = GObject.Object.ObjectReplaceQdataMethodInfo
    ResolveListModelMethod "runDispose" o = GObject.Object.ObjectRunDisposeMethodInfo
    ResolveListModelMethod "stealData" o = GObject.Object.ObjectStealDataMethodInfo
    ResolveListModelMethod "stealQdata" o = GObject.Object.ObjectStealQdataMethodInfo
    ResolveListModelMethod "thawNotify" o = GObject.Object.ObjectThawNotifyMethodInfo
    ResolveListModelMethod "unref" o = GObject.Object.ObjectUnrefMethodInfo
    ResolveListModelMethod "watchClosure" o = GObject.Object.ObjectWatchClosureMethodInfo
    ResolveListModelMethod "getData" o = GObject.Object.ObjectGetDataMethodInfo
    ResolveListModelMethod "getItem" o = ListModelGetItemMethodInfo
    ResolveListModelMethod "getItemType" o = ListModelGetItemTypeMethodInfo
    ResolveListModelMethod "getNItems" o = ListModelGetNItemsMethodInfo
    ResolveListModelMethod "getProperty" o = GObject.Object.ObjectGetPropertyMethodInfo
    ResolveListModelMethod "getQdata" o = GObject.Object.ObjectGetQdataMethodInfo
    ResolveListModelMethod "setData" o = GObject.Object.ObjectSetDataMethodInfo
    ResolveListModelMethod "setProperty" o = GObject.Object.ObjectSetPropertyMethodInfo
    ResolveListModelMethod l o = O.MethodResolutionFailed l o

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

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

-- signal ListModel::items-changed
type ListModelItemsChangedCallback =
    Word32 ->
    Word32 ->
    Word32 ->
    IO ()

noListModelItemsChangedCallback :: Maybe ListModelItemsChangedCallback
noListModelItemsChangedCallback = Nothing

type C_ListModelItemsChangedCallback =
    Ptr () ->                               -- object
    Word32 ->
    Word32 ->
    Word32 ->
    Ptr () ->                               -- user_data
    IO ()

foreign import ccall "wrapper"
    mk_ListModelItemsChangedCallback :: C_ListModelItemsChangedCallback -> IO (FunPtr C_ListModelItemsChangedCallback)

genClosure_ListModelItemsChanged :: ListModelItemsChangedCallback -> IO Closure
genClosure_ListModelItemsChanged cb = do
    let cb' = wrap_ListModelItemsChangedCallback cb
    mk_ListModelItemsChangedCallback cb' >>= newCClosure


wrap_ListModelItemsChangedCallback ::
    ListModelItemsChangedCallback ->
    Ptr () ->
    Word32 ->
    Word32 ->
    Word32 ->
    Ptr () ->
    IO ()
wrap_ListModelItemsChangedCallback _cb _ position removed added _ = do
    _cb  position removed added


onListModelItemsChanged :: (GObject a, MonadIO m) => a -> ListModelItemsChangedCallback -> m SignalHandlerId
onListModelItemsChanged obj cb = liftIO $ connectListModelItemsChanged obj cb SignalConnectBefore
afterListModelItemsChanged :: (GObject a, MonadIO m) => a -> ListModelItemsChangedCallback -> m SignalHandlerId
afterListModelItemsChanged obj cb = connectListModelItemsChanged obj cb SignalConnectAfter

connectListModelItemsChanged :: (GObject a, MonadIO m) =>
                                a -> ListModelItemsChangedCallback -> SignalConnectMode -> m SignalHandlerId
connectListModelItemsChanged obj cb after = liftIO $ do
    let cb' = wrap_ListModelItemsChangedCallback cb
    cb'' <- mk_ListModelItemsChangedCallback cb'
    connectSignalFunPtr obj "items-changed" cb'' after

instance O.HasAttributeList ListModel
type instance O.AttributeList ListModel = ListModelAttributeList
type ListModelAttributeList = ('[ ] :: [(Symbol, *)])

data ListModelItemsChangedSignalInfo
instance SignalInfo ListModelItemsChangedSignalInfo where
    type HaskellCallbackType ListModelItemsChangedSignalInfo = ListModelItemsChangedCallback
    connectSignal _ = connectListModelItemsChanged

type instance O.SignalList ListModel = ListModelSignalList
type ListModelSignalList = ('[ '("itemsChanged", ListModelItemsChangedSignalInfo), '("notify", GObject.Object.ObjectNotifySignalInfo)] :: [(Symbol, *)])

foreign import ccall "g_list_model_get_type"
    c_g_list_model_get_type :: IO GType

instance GObject ListModel where
    gobjectType _ = c_g_list_model_get_type
    

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

toListModel :: IsListModel o => o -> IO ListModel
toListModel = unsafeCastTo ListModel

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

foreign import ccall "g_list_model_get_item_type" g_list_model_get_item_type :: 
    Ptr ListModel ->                        -- list : TInterface (Name {namespace = "Gio", name = "ListModel"})
    IO CGType

{- |
Gets the type of the items in /@list@/. All items returned from
@/g_list_model_get_type()/@ are of that type or a subtype, or are an
implementation of that interface.

The item type of a 'GI.Gio.Interfaces.ListModel.ListModel' can not change during the life of the
model.

@since 2.44
-}
listModelGetItemType ::
    (B.CallStack.HasCallStack, MonadIO m, IsListModel a) =>
    a
    {- ^ /@list@/: a 'GI.Gio.Interfaces.ListModel.ListModel' -}
    -> m GType
    {- ^ __Returns:__ the 'GType' of the items contained in /@list@/. -}
listModelGetItemType list = liftIO $ do
    list' <- unsafeManagedPtrCastPtr list
    result <- g_list_model_get_item_type list'
    let result' = GType result
    touchManagedPtr list
    return result'

data ListModelGetItemTypeMethodInfo
instance (signature ~ (m GType), MonadIO m, IsListModel a) => O.MethodInfo ListModelGetItemTypeMethodInfo a signature where
    overloadedMethod _ = listModelGetItemType

-- method ListModel::get_n_items
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "list", argType = TInterface (Name {namespace = "Gio", name = "ListModel"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GListModel", 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 "g_list_model_get_n_items" g_list_model_get_n_items :: 
    Ptr ListModel ->                        -- list : TInterface (Name {namespace = "Gio", name = "ListModel"})
    IO Word32

{- |
Gets the number of items in /@list@/.

Depending on the model implementation, calling this function may be
less efficient than iterating the list with increasing values for
/@position@/ until @/g_list_model_get_item()/@ returns 'Nothing'.

@since 2.44
-}
listModelGetNItems ::
    (B.CallStack.HasCallStack, MonadIO m, IsListModel a) =>
    a
    {- ^ /@list@/: a 'GI.Gio.Interfaces.ListModel.ListModel' -}
    -> m Word32
    {- ^ __Returns:__ the number of items in /@list@/. -}
listModelGetNItems list = liftIO $ do
    list' <- unsafeManagedPtrCastPtr list
    result <- g_list_model_get_n_items list'
    touchManagedPtr list
    return result

data ListModelGetNItemsMethodInfo
instance (signature ~ (m Word32), MonadIO m, IsListModel a) => O.MethodInfo ListModelGetNItemsMethodInfo a signature where
    overloadedMethod _ = listModelGetNItems

-- method ListModel::get_item
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "list", argType = TInterface (Name {namespace = "Gio", name = "ListModel"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GListModel", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "position", argType = TBasicType TUInt, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the position of the item to fetch", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "GObject", name = "Object"}))
-- throws : False
-- Skip return : False

foreign import ccall "g_list_model_get_object" g_list_model_get_object :: 
    Ptr ListModel ->                        -- list : TInterface (Name {namespace = "Gio", name = "ListModel"})
    Word32 ->                               -- position : TBasicType TUInt
    IO (Ptr GObject.Object.Object)

{- |
Get the item at /@position@/. If /@position@/ is greater than the number of
items in /@list@/, 'Nothing' is returned.

'Nothing' is never returned for an index that is smaller than the length
of the list.  See 'GI.Gio.Interfaces.ListModel.listModelGetNItems'.

@since 2.44
-}
listModelGetItem ::
    (B.CallStack.HasCallStack, MonadIO m, IsListModel a) =>
    a
    {- ^ /@list@/: a 'GI.Gio.Interfaces.ListModel.ListModel' -}
    -> Word32
    {- ^ /@position@/: the position of the item to fetch -}
    -> m (Maybe GObject.Object.Object)
    {- ^ __Returns:__ the object at /@position@/. -}
listModelGetItem list position = liftIO $ do
    list' <- unsafeManagedPtrCastPtr list
    result <- g_list_model_get_object list' position
    maybeResult <- convertIfNonNull result $ \result' -> do
        result'' <- (wrapObject GObject.Object.Object) result'
        return result''
    touchManagedPtr list
    return maybeResult

data ListModelGetItemMethodInfo
instance (signature ~ (Word32 -> m (Maybe GObject.Object.Object)), MonadIO m, IsListModel a) => O.MethodInfo ListModelGetItemMethodInfo a signature where
    overloadedMethod _ = listModelGetItem

-- method ListModel::items_changed
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "list", argType = TInterface (Name {namespace = "Gio", name = "ListModel"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GListModel", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "position", argType = TBasicType TUInt, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the position at which @list changed", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "removed", argType = TBasicType TUInt, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the number of items removed", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "added", argType = TBasicType TUInt, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the number of items added", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "g_list_model_items_changed" g_list_model_items_changed :: 
    Ptr ListModel ->                        -- list : TInterface (Name {namespace = "Gio", name = "ListModel"})
    Word32 ->                               -- position : TBasicType TUInt
    Word32 ->                               -- removed : TBasicType TUInt
    Word32 ->                               -- added : TBasicType TUInt
    IO ()

{- |
Emits the 'GI.Gio.Interfaces.ListModel.ListModel'::@/items-changed/@ signal on /@list@/.

This function should only be called by classes implementing
'GI.Gio.Interfaces.ListModel.ListModel'. It has to be called after the internal representation
of /@list@/ has been updated, because handlers connected to this signal
might query the new state of the list.

Implementations must only make changes to the model (as visible to
its consumer) in places that will not cause problems for that
consumer.  For models that are driven directly by a write API (such
as 'GI.Gio.Objects.ListStore.ListStore'), changes can be reported in response to uses of that
API.  For models that represent remote data, changes should only be
made from a fresh mainloop dispatch.  It is particularly not
permitted to make changes in response to a call to the 'GI.Gio.Interfaces.ListModel.ListModel'
consumer API.

Stated another way: in general, it is assumed that code making a
series of accesses to the model via the API, without returning to the
mainloop, and without calling other code, will continue to view the
same contents of the model.

@since 2.44
-}
listModelItemsChanged ::
    (B.CallStack.HasCallStack, MonadIO m, IsListModel a) =>
    a
    {- ^ /@list@/: a 'GI.Gio.Interfaces.ListModel.ListModel' -}
    -> Word32
    {- ^ /@position@/: the position at which /@list@/ changed -}
    -> Word32
    {- ^ /@removed@/: the number of items removed -}
    -> Word32
    {- ^ /@added@/: the number of items added -}
    -> m ()
listModelItemsChanged list position removed added = liftIO $ do
    list' <- unsafeManagedPtrCastPtr list
    g_list_model_items_changed list' position removed added
    touchManagedPtr list
    return ()

data ListModelItemsChangedMethodInfo
instance (signature ~ (Word32 -> Word32 -> Word32 -> m ()), MonadIO m, IsListModel a) => O.MethodInfo ListModelItemsChangedMethodInfo a signature where
    overloadedMethod _ = listModelItemsChanged