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

This is an opaque structure type.  You may not access it directly.

/Since: 2.32/
-}

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

module GI.Gio.Structs.SettingsSchemaSource
    (

-- * Exported types
    SettingsSchemaSource(..)                ,
    noSettingsSchemaSource                  ,


 -- * Methods
-- ** getDefault #method:getDefault#

    settingsSchemaSourceGetDefault          ,


-- ** listSchemas #method:listSchemas#

#if ENABLE_OVERLOADING
    SettingsSchemaSourceListSchemasMethodInfo,
#endif
    settingsSchemaSourceListSchemas         ,


-- ** lookup #method:lookup#

#if ENABLE_OVERLOADING
    SettingsSchemaSourceLookupMethodInfo    ,
#endif
    settingsSchemaSourceLookup              ,


-- ** newFromDirectory #method:newFromDirectory#

    settingsSchemaSourceNewFromDirectory    ,


-- ** ref #method:ref#

#if ENABLE_OVERLOADING
    SettingsSchemaSourceRefMethodInfo       ,
#endif
    settingsSchemaSourceRef                 ,


-- ** unref #method:unref#

#if ENABLE_OVERLOADING
    SettingsSchemaSourceUnrefMethodInfo     ,
#endif
    settingsSchemaSourceUnref               ,




    ) 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

import {-# SOURCE #-} qualified GI.Gio.Structs.SettingsSchema as Gio.SettingsSchema

-- | Memory-managed wrapper type.
newtype SettingsSchemaSource = SettingsSchemaSource (ManagedPtr SettingsSchemaSource)
foreign import ccall "g_settings_schema_source_get_type" c_g_settings_schema_source_get_type ::
    IO GType

instance BoxedObject SettingsSchemaSource where
    boxedType _ = c_g_settings_schema_source_get_type

-- | A convenience alias for `Nothing` :: `Maybe` `SettingsSchemaSource`.
noSettingsSchemaSource :: Maybe SettingsSchemaSource
noSettingsSchemaSource = Nothing


#if ENABLE_OVERLOADING
instance O.HasAttributeList SettingsSchemaSource
type instance O.AttributeList SettingsSchemaSource = SettingsSchemaSourceAttributeList
type SettingsSchemaSourceAttributeList = ('[ ] :: [(Symbol, *)])
#endif

-- method SettingsSchemaSource::new_from_directory
-- method type : Constructor
-- Args : [Arg {argCName = "directory", argType = TBasicType TFileName, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the filename of a directory", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "parent", argType = TInterface (Name {namespace = "Gio", name = "SettingsSchemaSource"}), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "a #GSettingsSchemaSource, or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "trusted", argType = TBasicType TBoolean, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "%TRUE, if the directory is trusted", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Gio", name = "SettingsSchemaSource"}))
-- throws : True
-- Skip return : False

foreign import ccall "g_settings_schema_source_new_from_directory" g_settings_schema_source_new_from_directory ::
    CString ->                              -- directory : TBasicType TFileName
    Ptr SettingsSchemaSource ->             -- parent : TInterface (Name {namespace = "Gio", name = "SettingsSchemaSource"})
    CInt ->                                 -- trusted : TBasicType TBoolean
    Ptr (Ptr GError) ->                     -- error
    IO (Ptr SettingsSchemaSource)

{- |
Attempts to create a new schema source corresponding to the contents
of the given directory.

This function is not required for normal uses of 'GI.Gio.Objects.Settings.Settings' but it
may be useful to authors of plugin management systems.

The directory should contain a file called @gschemas.compiled@ as
produced by the [glib-compile-schemas][glib-compile-schemas] tool.

If /@trusted@/ is 'True' then @gschemas.compiled@ is trusted not to be
corrupted. This assumption has a performance advantage, but can result
in crashes or inconsistent behaviour in the case of a corrupted file.
Generally, you should set /@trusted@/ to 'True' for files installed by the
system and to 'False' for files in the home directory.

In either case, an empty file or some types of corruption in the file will
result in 'GI.GLib.Enums.FileErrorInval' being returned.

If /@parent@/ is non-'Nothing' then there are two effects.

First, if 'GI.Gio.Structs.SettingsSchemaSource.settingsSchemaSourceLookup' is called with the
/@recursive@/ flag set to 'True' and the schema can not be found in the
source, the lookup will recurse to the parent.

Second, any references to other schemas specified within this
source (ie: @child@ or @extends@) references may be resolved
from the /@parent@/.

For this second reason, except in very unusual situations, the
/@parent@/ should probably be given as the default schema source, as
returned by 'GI.Gio.Functions.settingsSchemaSourceGetDefault'.

/Since: 2.32/
-}
settingsSchemaSourceNewFromDirectory ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    [Char]
    {- ^ /@directory@/: the filename of a directory -}
    -> Maybe (SettingsSchemaSource)
    {- ^ /@parent@/: a 'GI.Gio.Structs.SettingsSchemaSource.SettingsSchemaSource', or 'Nothing' -}
    -> Bool
    {- ^ /@trusted@/: 'True', if the directory is trusted -}
    -> m SettingsSchemaSource
    {- ^ /(Can throw 'Data.GI.Base.GError.GError')/ -}
settingsSchemaSourceNewFromDirectory directory parent trusted = liftIO $ do
    directory' <- stringToCString directory
    maybeParent <- case parent of
        Nothing -> return nullPtr
        Just jParent -> do
            jParent' <- unsafeManagedPtrGetPtr jParent
            return jParent'
    let trusted' = (fromIntegral . fromEnum) trusted
    onException (do
        result <- propagateGError $ g_settings_schema_source_new_from_directory directory' maybeParent trusted'
        checkUnexpectedReturnNULL "settingsSchemaSourceNewFromDirectory" result
        result' <- (wrapBoxed SettingsSchemaSource) result
        whenJust parent touchManagedPtr
        freeMem directory'
        return result'
     ) (do
        freeMem directory'
     )

#if ENABLE_OVERLOADING
#endif

-- method SettingsSchemaSource::list_schemas
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "source", argType = TInterface (Name {namespace = "Gio", name = "SettingsSchemaSource"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GSettingsSchemaSource", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "recursive", argType = TBasicType TBoolean, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "if we should recurse", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "non_relocatable", argType = TCArray True (-1) (-1) (TBasicType TUTF8), direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the\n  list of non-relocatable schemas", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything},Arg {argCName = "relocatable", argType = TCArray True (-1) (-1) (TBasicType TUTF8), direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the list\n  of relocatable schemas", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "g_settings_schema_source_list_schemas" g_settings_schema_source_list_schemas ::
    Ptr SettingsSchemaSource ->             -- source : TInterface (Name {namespace = "Gio", name = "SettingsSchemaSource"})
    CInt ->                                 -- recursive : TBasicType TBoolean
    Ptr (Ptr CString) ->                    -- non_relocatable : TCArray True (-1) (-1) (TBasicType TUTF8)
    Ptr (Ptr CString) ->                    -- relocatable : TCArray True (-1) (-1) (TBasicType TUTF8)
    IO ()

{- |
Lists the schemas in a given source.

If /@recursive@/ is 'True' then include parent sources.  If 'False' then
only include the schemas from one source (ie: one directory).  You
probably want 'True'.

Non-relocatable schemas are those for which you can call
'GI.Gio.Objects.Settings.settingsNew'.  Relocatable schemas are those for which you must
use 'GI.Gio.Objects.Settings.settingsNewWithPath'.

Do not call this function from normal programs.  This is designed for
use by database editors, commandline tools, etc.

/Since: 2.40/
-}
settingsSchemaSourceListSchemas ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    SettingsSchemaSource
    {- ^ /@source@/: a 'GI.Gio.Structs.SettingsSchemaSource.SettingsSchemaSource' -}
    -> Bool
    {- ^ /@recursive@/: if we should recurse -}
    -> m (([T.Text], [T.Text]))
settingsSchemaSourceListSchemas source recursive = liftIO $ do
    source' <- unsafeManagedPtrGetPtr source
    let recursive' = (fromIntegral . fromEnum) recursive
    nonRelocatable <- allocMem :: IO (Ptr (Ptr CString))
    relocatable <- allocMem :: IO (Ptr (Ptr CString))
    g_settings_schema_source_list_schemas source' recursive' nonRelocatable relocatable
    nonRelocatable' <- peek nonRelocatable
    nonRelocatable'' <- unpackZeroTerminatedUTF8CArray nonRelocatable'
    mapZeroTerminatedCArray freeMem nonRelocatable'
    freeMem nonRelocatable'
    relocatable' <- peek relocatable
    relocatable'' <- unpackZeroTerminatedUTF8CArray relocatable'
    mapZeroTerminatedCArray freeMem relocatable'
    freeMem relocatable'
    touchManagedPtr source
    freeMem nonRelocatable
    freeMem relocatable
    return (nonRelocatable'', relocatable'')

#if ENABLE_OVERLOADING
data SettingsSchemaSourceListSchemasMethodInfo
instance (signature ~ (Bool -> m (([T.Text], [T.Text]))), MonadIO m) => O.MethodInfo SettingsSchemaSourceListSchemasMethodInfo SettingsSchemaSource signature where
    overloadedMethod _ = settingsSchemaSourceListSchemas

#endif

-- method SettingsSchemaSource::lookup
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "source", argType = TInterface (Name {namespace = "Gio", name = "SettingsSchemaSource"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #GSettingsSchemaSource", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "schema_id", argType = TBasicType TUTF8, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a schema ID", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "recursive", argType = TBasicType TBoolean, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "%TRUE if the lookup should be recursive", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Gio", name = "SettingsSchema"}))
-- throws : False
-- Skip return : False

foreign import ccall "g_settings_schema_source_lookup" g_settings_schema_source_lookup ::
    Ptr SettingsSchemaSource ->             -- source : TInterface (Name {namespace = "Gio", name = "SettingsSchemaSource"})
    CString ->                              -- schema_id : TBasicType TUTF8
    CInt ->                                 -- recursive : TBasicType TBoolean
    IO (Ptr Gio.SettingsSchema.SettingsSchema)

{- |
Looks up a schema with the identifier /@schemaId@/ in /@source@/.

This function is not required for normal uses of 'GI.Gio.Objects.Settings.Settings' but it
may be useful to authors of plugin management systems or to those who
want to introspect the content of schemas.

If the schema isn\'t found directly in /@source@/ and /@recursive@/ is 'True'
then the parent sources will also be checked.

If the schema isn\'t found, 'Nothing' is returned.

/Since: 2.32/
-}
settingsSchemaSourceLookup ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    SettingsSchemaSource
    {- ^ /@source@/: a 'GI.Gio.Structs.SettingsSchemaSource.SettingsSchemaSource' -}
    -> T.Text
    {- ^ /@schemaId@/: a schema ID -}
    -> Bool
    {- ^ /@recursive@/: 'True' if the lookup should be recursive -}
    -> m (Maybe Gio.SettingsSchema.SettingsSchema)
    {- ^ __Returns:__ a new 'GI.Gio.Structs.SettingsSchema.SettingsSchema' -}
settingsSchemaSourceLookup source schemaId recursive = liftIO $ do
    source' <- unsafeManagedPtrGetPtr source
    schemaId' <- textToCString schemaId
    let recursive' = (fromIntegral . fromEnum) recursive
    result <- g_settings_schema_source_lookup source' schemaId' recursive'
    maybeResult <- convertIfNonNull result $ \result' -> do
        result'' <- (wrapBoxed Gio.SettingsSchema.SettingsSchema) result'
        return result''
    touchManagedPtr source
    freeMem schemaId'
    return maybeResult

#if ENABLE_OVERLOADING
data SettingsSchemaSourceLookupMethodInfo
instance (signature ~ (T.Text -> Bool -> m (Maybe Gio.SettingsSchema.SettingsSchema)), MonadIO m) => O.MethodInfo SettingsSchemaSourceLookupMethodInfo SettingsSchemaSource signature where
    overloadedMethod _ = settingsSchemaSourceLookup

#endif

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

foreign import ccall "g_settings_schema_source_ref" g_settings_schema_source_ref ::
    Ptr SettingsSchemaSource ->             -- source : TInterface (Name {namespace = "Gio", name = "SettingsSchemaSource"})
    IO (Ptr SettingsSchemaSource)

{- |
Increase the reference count of /@source@/, returning a new reference.

/Since: 2.32/
-}
settingsSchemaSourceRef ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    SettingsSchemaSource
    {- ^ /@source@/: a 'GI.Gio.Structs.SettingsSchemaSource.SettingsSchemaSource' -}
    -> m SettingsSchemaSource
    {- ^ __Returns:__ a new reference to /@source@/ -}
settingsSchemaSourceRef source = liftIO $ do
    source' <- unsafeManagedPtrGetPtr source
    result <- g_settings_schema_source_ref source'
    checkUnexpectedReturnNULL "settingsSchemaSourceRef" result
    result' <- (wrapBoxed SettingsSchemaSource) result
    touchManagedPtr source
    return result'

#if ENABLE_OVERLOADING
data SettingsSchemaSourceRefMethodInfo
instance (signature ~ (m SettingsSchemaSource), MonadIO m) => O.MethodInfo SettingsSchemaSourceRefMethodInfo SettingsSchemaSource signature where
    overloadedMethod _ = settingsSchemaSourceRef

#endif

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

foreign import ccall "g_settings_schema_source_unref" g_settings_schema_source_unref ::
    Ptr SettingsSchemaSource ->             -- source : TInterface (Name {namespace = "Gio", name = "SettingsSchemaSource"})
    IO ()

{- |
Decrease the reference count of /@source@/, possibly freeing it.

/Since: 2.32/
-}
settingsSchemaSourceUnref ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    SettingsSchemaSource
    {- ^ /@source@/: a 'GI.Gio.Structs.SettingsSchemaSource.SettingsSchemaSource' -}
    -> m ()
settingsSchemaSourceUnref source = liftIO $ do
    source' <- unsafeManagedPtrGetPtr source
    g_settings_schema_source_unref source'
    touchManagedPtr source
    return ()

#if ENABLE_OVERLOADING
data SettingsSchemaSourceUnrefMethodInfo
instance (signature ~ (m ()), MonadIO m) => O.MethodInfo SettingsSchemaSourceUnrefMethodInfo SettingsSchemaSource signature where
    overloadedMethod _ = settingsSchemaSourceUnref

#endif

-- method SettingsSchemaSource::get_default
-- method type : MemberFunction
-- Args : []
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Gio", name = "SettingsSchemaSource"}))
-- throws : False
-- Skip return : False

foreign import ccall "g_settings_schema_source_get_default" g_settings_schema_source_get_default ::
    IO (Ptr SettingsSchemaSource)

{- |
Gets the default system schema source.

This function is not required for normal uses of 'GI.Gio.Objects.Settings.Settings' but it
may be useful to authors of plugin management systems or to those who
want to introspect the content of schemas.

If no schemas are installed, 'Nothing' will be returned.

The returned source may actually consist of multiple schema sources
from different directories, depending on which directories were given
in @XDG_DATA_DIRS@ and @GSETTINGS_SCHEMA_DIR@. For this reason, all
lookups performed against the default source should probably be done
recursively.

/Since: 2.32/
-}
settingsSchemaSourceGetDefault ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    m (Maybe SettingsSchemaSource)
    {- ^ __Returns:__ the default schema source -}
settingsSchemaSourceGetDefault  = liftIO $ do
    result <- g_settings_schema_source_get_default
    maybeResult <- convertIfNonNull result $ \result' -> do
        result'' <- (newBoxed SettingsSchemaSource) result'
        return result''
    return maybeResult

#if ENABLE_OVERLOADING
#endif

#if ENABLE_OVERLOADING
type family ResolveSettingsSchemaSourceMethod (t :: Symbol) (o :: *) :: * where
    ResolveSettingsSchemaSourceMethod "listSchemas" o = SettingsSchemaSourceListSchemasMethodInfo
    ResolveSettingsSchemaSourceMethod "lookup" o = SettingsSchemaSourceLookupMethodInfo
    ResolveSettingsSchemaSourceMethod "ref" o = SettingsSchemaSourceRefMethodInfo
    ResolveSettingsSchemaSourceMethod "unref" o = SettingsSchemaSourceUnrefMethodInfo
    ResolveSettingsSchemaSourceMethod l o = O.MethodResolutionFailed l o

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