module System.Gnome.GConf.GConfClient (
GConf, GConfPreloadType, GConfError,
gconfGetDefault,
gconfAddDir, gconfRemoveDir,
gconfNotifyAdd, gconfNotifyRemove,
onValueChanged, afterValueChanged,
gconfGet, gconfSet,
gconfGetWithoutDefault,
gconfGetDefaultFromSchema,
gconfUnset,
gconfClearCache, gconfPreload,
gconfSuggestSync,
gconfAllEntries,
gconfAllDirs,
gconfDirExists,
GConfValueClass,
GConfPrimitiveValueClass,
GConfValue,
GConfValueDyn(..),
) where
import Control.Monad (liftM, when)
import Data.IORef (newIORef, readIORef, writeIORef)
import Data.Text (Text)
import System.IO.Unsafe (unsafePerformIO)
import System.Glib.FFI
import System.Glib.UTFString
import System.Glib.GList
import System.Glib.GError (GErrorDomain, GErrorClass(..), propagateGError)
import System.Glib.GObject (wrapNewGObject)
import System.Gnome.GConf.Types
import System.Gnome.GConf.Signals
import System.Gnome.GConf.GConfValue
data GConfPreloadType = PreloadNone
| PreloadOnelevel
| PreloadRecursive
deriving (Enum)
data GConfError = GconfErrorSuccess
| GconfErrorFailed
| GconfErrorNoServer
| GconfErrorNoPermission
| GconfErrorBadAddress
| GconfErrorBadKey
| GconfErrorParseError
| GconfErrorCorrupt
| GconfErrorTypeMismatch
| GconfErrorIsDir
| GconfErrorIsKey
| GconfErrorOverridden
| GconfErrorOafError
| GconfErrorLocalEngine
| GconfErrorLockFailed
| GconfErrorNoWritableDatabase
| GconfErrorInShutdown
instance Enum GConfError where
fromEnum GconfErrorSuccess = 0
fromEnum GconfErrorFailed = 1
fromEnum GconfErrorNoServer = 2
fromEnum GconfErrorNoPermission = 3
fromEnum GconfErrorBadAddress = 4
fromEnum GconfErrorBadKey = 5
fromEnum GconfErrorParseError = 6
fromEnum GconfErrorCorrupt = 7
fromEnum GconfErrorTypeMismatch = 8
fromEnum GconfErrorIsDir = 9
fromEnum GconfErrorIsKey = 10
fromEnum GconfErrorOverridden = 11
fromEnum GconfErrorOafError = 12
fromEnum GconfErrorLocalEngine = 13
fromEnum GconfErrorLockFailed = 14
fromEnum GconfErrorNoWritableDatabase = 15
fromEnum GconfErrorInShutdown = 16
toEnum 0 = GconfErrorSuccess
toEnum 1 = GconfErrorFailed
toEnum 2 = GconfErrorNoServer
toEnum 3 = GconfErrorNoPermission
toEnum 4 = GconfErrorBadAddress
toEnum 5 = GconfErrorBadKey
toEnum 6 = GconfErrorParseError
toEnum 7 = GconfErrorCorrupt
toEnum 8 = GconfErrorTypeMismatch
toEnum 9 = GconfErrorIsDir
toEnum 10 = GconfErrorIsKey
toEnum 11 = GconfErrorOverridden
toEnum 12 = GconfErrorOafError
toEnum 13 = GconfErrorLocalEngine
toEnum 14 = GconfErrorLockFailed
toEnum 15 = GconfErrorNoWritableDatabase
toEnum 16 = GconfErrorInShutdown
toEnum unmatched = error ("GConfError.toEnum: Cannot match " ++ show unmatched)
succ GconfErrorSuccess = GconfErrorFailed
succ GconfErrorFailed = GconfErrorNoServer
succ GconfErrorNoServer = GconfErrorNoPermission
succ GconfErrorNoPermission = GconfErrorBadAddress
succ GconfErrorBadAddress = GconfErrorBadKey
succ GconfErrorBadKey = GconfErrorParseError
succ GconfErrorParseError = GconfErrorCorrupt
succ GconfErrorCorrupt = GconfErrorTypeMismatch
succ GconfErrorTypeMismatch = GconfErrorIsDir
succ GconfErrorIsDir = GconfErrorIsKey
succ GconfErrorIsKey = GconfErrorOverridden
succ GconfErrorOverridden = GconfErrorOafError
succ GconfErrorOafError = GconfErrorLocalEngine
succ GconfErrorLocalEngine = GconfErrorLockFailed
succ GconfErrorLockFailed = GconfErrorNoWritableDatabase
succ GconfErrorNoWritableDatabase = GconfErrorInShutdown
succ _ = undefined
pred GconfErrorFailed = GconfErrorSuccess
pred GconfErrorNoServer = GconfErrorFailed
pred GconfErrorNoPermission = GconfErrorNoServer
pred GconfErrorBadAddress = GconfErrorNoPermission
pred GconfErrorBadKey = GconfErrorBadAddress
pred GconfErrorParseError = GconfErrorBadKey
pred GconfErrorCorrupt = GconfErrorParseError
pred GconfErrorTypeMismatch = GconfErrorCorrupt
pred GconfErrorIsDir = GconfErrorTypeMismatch
pred GconfErrorIsKey = GconfErrorIsDir
pred GconfErrorOverridden = GconfErrorIsKey
pred GconfErrorOafError = GconfErrorOverridden
pred GconfErrorLocalEngine = GconfErrorOafError
pred GconfErrorLockFailed = GconfErrorLocalEngine
pred GconfErrorNoWritableDatabase = GconfErrorLockFailed
pred GconfErrorInShutdown = GconfErrorNoWritableDatabase
pred _ = undefined
enumFromTo x y | fromEnum x == fromEnum y = [ y ]
| otherwise = x : enumFromTo (succ x) y
enumFrom x = enumFromTo x GconfErrorInShutdown
enumFromThen _ _ = error "Enum GConfError: enumFromThen not implemented"
enumFromThenTo _ _ _ = error "Enum GConfError: enumFromThenTo not implemented"
gconfErrorDomain :: GErrorDomain
gconfErrorDomain = unsafePerformIO gconf_error_quark
instance GErrorClass GConfError where
gerrorDomain _ = gconfErrorDomain
newtype GConfEntry = GConfEntry (Ptr (GConfEntry))
gconfGetDefault :: IO GConf
gconfGetDefault = do
g_type_init
wrapNewGObject mkGConf gconf_client_get_default
gconfAddDir :: GConf -> String -> IO ()
gconfAddDir gc key = gconfAddDirWithPreload gc key PreloadNone
gconfAddDirWithPreload :: GConf -> String -> GConfPreloadType -> IO ()
gconfAddDirWithPreload gc key preload =
propagateGError $ \gerrorPtr ->
withCString key $ \strPtr ->
(\(GConf arg1) arg2 arg3 arg4 -> withForeignPtr arg1 $ \argPtr1 ->gconf_client_add_dir argPtr1 arg2 arg3 arg4) gc strPtr
(fromIntegral $ fromEnum preload) gerrorPtr
gconfRemoveDir :: GConf -> String -> IO ()
gconfRemoveDir gc key =
propagateGError $ \gerrorPtr ->
withCString key $ \strPtr ->
(\(GConf arg1) arg2 arg3 -> withForeignPtr arg1 $ \argPtr1 ->gconf_client_remove_dir argPtr1 arg2 arg3) gc strPtr gerrorPtr
type GFreeFunc = FunPtr (((Ptr ()) -> (IO ())))
foreign import ccall "wrapper" mkDestructor :: (Ptr () -> IO ()) -> IO GFreeFunc
type GConfClientNotifyFunc = Ptr () ->
(CUInt) ->
Ptr () ->
Ptr () ->
IO ()
foreign import ccall "wrapper" mkHandler_GConfClientNotifyFunc ::
GConfClientNotifyFunc -> IO (FunPtr GConfClientNotifyFunc)
connect_GConfClientNotifyFunc ::
GConf ->
String ->
(GConfEntry -> IO ()) ->
IO GConfConnectId
connect_GConfClientNotifyFunc gc key user = do
hPtr <- mkHandler_GConfClientNotifyFunc
(\_ _ entryPtr _ -> user (GConfEntry $ castPtr entryPtr))
dRef <- newIORef nullFunPtr
dPtr <- mkDestructor $ \_ -> do
freeHaskellFunPtr hPtr
dPtr <- readIORef dRef
freeHaskellFunPtr dPtr
writeIORef dRef dPtr
cnxId <- propagateGError $ \gerrorPtr ->
withCString key $ \strPtr ->
(\(GConf arg1) arg2 arg3 arg4 arg5 arg6 -> withForeignPtr arg1 $ \argPtr1 ->gconf_client_notify_add argPtr1 arg2 arg3 arg4 arg5 arg6) gc strPtr
(castFunPtr hPtr) nullPtr dPtr gerrorPtr
return $ GConfConnectId cnxId
newtype GConfConnectId = GConfConnectId (CUInt)
gconfNotifyAdd :: (GConfValueClass value, GlibString string) =>
GConf ->
String ->
(string -> value -> IO ()) ->
IO GConfConnectId
gconfNotifyAdd gc key handler =
connect_GConfClientNotifyFunc gc key (convertValueChangedHandler handler)
where convertValueChangedHandler :: (GConfValueClass value, GlibString string) =>
(string -> value -> IO ()) ->
(GConfEntry -> IO ())
convertValueChangedHandler handler entry = do
keyStrPtr <- (\(GConfEntry arg1) -> gconf_entry_get_key arg1) entry
valuePtr <- (\(GConfEntry arg1) -> gconf_entry_get_value arg1) entry
key <- peekUTFString keyStrPtr
value <- marshalFromGConfValue (GConfValue valuePtr)
handler key value
gconfNotifyRemove :: GConf -> GConfConnectId -> IO ()
gconfNotifyRemove gc (GConfConnectId cxid) =
(\(GConf arg1) arg2 -> withForeignPtr arg1 $ \argPtr1 ->gconf_client_notify_remove argPtr1 arg2) gc cxid
gconfGet :: (GConfValueClass value, GlibString string) => GConf
-> string
-> IO value
gconfGet gc key = do
value <- propagateGError $ \gerrorPtr ->
withUTFString key $ \strPtr ->
(\(GConf arg1) arg2 arg3 -> withForeignPtr arg1 $ \argPtr1 ->gconf_client_get argPtr1 arg2 arg3) gc strPtr gerrorPtr
marshalFromGConfValue (GConfValue value)
gconfGetInt :: GlibString string => GConf -> string -> IO Int
gconfGetInt = gconfGet
gconfGetBool :: GlibString string => GConf -> string -> IO Bool
gconfGetBool = gconfGet
gconfGetFloat :: GlibString string => GConf -> string -> IO Double
gconfGetFloat = gconfGet
gconfGetText :: GlibString string => GConf -> string -> IO Text
gconfGetText = gconfGet
gconfGetPair :: (GConfValueClass (a,b), GlibString string) =>
GConf -> string -> IO (a, b)
gconfGetPair = gconfGet
gconfGetList :: (GConfValueClass [a], GlibString string) =>
GConf -> string -> IO [a]
gconfGetList = gconfGet
gconfSet :: (GConfValueClass value, GlibString string) => GConf
-> string
-> value
-> IO ()
gconfSet gc key val = do
value@(GConfValue ptr) <- marshalToGConfValue val
if ptr == nullPtr
then gconfUnset gc key
else propagateGError $ \gerrorPtr ->
withUTFString key $ \strPtr ->
(\(GConf arg1) arg2 (GConfValue arg3) arg4 -> withForeignPtr arg1 $ \argPtr1 ->gconf_client_set argPtr1 arg2 arg3 arg4) gc strPtr value gerrorPtr
gconfSetInt :: GlibString string => GConf -> string -> Int -> IO ()
gconfSetInt = gconfSet
gconfSetBool :: GlibString string => GConf -> string -> Bool -> IO ()
gconfSetBool = gconfSet
gconfSetFloat :: GlibString string => GConf -> string -> Double -> IO ()
gconfSetFloat = gconfSet
gconfSetText :: GlibString string => GConf -> string -> Text -> IO ()
gconfSetText = gconfSet
gconfSetPair :: (GConfValueClass (a,b), GlibString string) =>
GConf -> string -> (a, b) -> IO ()
gconfSetPair = gconfSet
gconfSetList :: (GConfValueClass [a], GlibString string) =>
GConf -> string -> [a] -> IO ()
gconfSetList = gconfSet
gconfGetWithoutDefault :: GConfValueClass value =>
GConf -> String -> IO value
gconfGetWithoutDefault gc key = do
value <- propagateGError $ \gerrorPtr ->
withCString key $ \strPtr ->
(\(GConf arg1) arg2 arg3 -> withForeignPtr arg1 $ \argPtr1 ->gconf_client_get_without_default argPtr1 arg2 arg3) gc strPtr gerrorPtr
marshalFromGConfValue (GConfValue value)
gconfGetDefaultFromSchema :: GConfValueClass value =>
GConf -> String -> IO value
gconfGetDefaultFromSchema gc key = do
value <- propagateGError $ \gerrorPtr ->
withCString key $ \strPtr ->
(\(GConf arg1) arg2 arg3 -> withForeignPtr arg1 $ \argPtr1 ->gconf_client_get_default_from_schema argPtr1 arg2 arg3) gc strPtr gerrorPtr
marshalFromGConfValue (GConfValue value)
gconfUnset :: GlibString string => GConf -> string -> IO ()
gconfUnset gc key =
propagateGError $ \gerrorPtr ->
withUTFString key $ \strPtr -> do
(\(GConf arg1) arg2 arg3 -> withForeignPtr arg1 $ \argPtr1 ->gconf_client_unset argPtr1 arg2 arg3) gc strPtr gerrorPtr
return ()
gconfClearCache :: GConf -> IO ()
gconfClearCache gc = (\(GConf arg1) -> withForeignPtr arg1 $ \argPtr1 ->gconf_client_clear_cache argPtr1) gc
gconfPreload :: GConf -> String -> GConfPreloadType -> IO ()
gconfPreload gc key preload =
propagateGError $ \gerrorPtr ->
withCString key $ \strPtr ->
(\(GConf arg1) arg2 arg3 arg4 -> withForeignPtr arg1 $ \argPtr1 ->gconf_client_preload argPtr1 arg2 arg3 arg4) gc strPtr
(fromIntegral $ fromEnum preload) gerrorPtr
gconfSuggestSync :: GConf -> IO ()
gconfSuggestSync gc =
propagateGError $ \gerrorPtr ->
(\(GConf arg1) arg2 -> withForeignPtr arg1 $ \argPtr1 ->gconf_client_suggest_sync argPtr1 arg2) gc gerrorPtr
gconfAllEntries :: GlibString string => GConf -> string -> IO [(string, GConfValueDyn)]
gconfAllEntries gc dir = do
gsList <- propagateGError $ \gerrorPtr ->
withUTFString dir $ \strPtr ->
(\(GConf arg1) arg2 arg3 -> withForeignPtr arg1 $ \argPtr1 ->gconf_client_all_entries argPtr1 arg2 arg3) gc strPtr gerrorPtr
entryList <- fromGSList gsList
mapM (\entry -> do let entry' = GConfEntry entry
keyStrPtr <- (\(GConfEntry arg1) -> gconf_entry_get_key arg1) entry'
valuePtr <- (\(GConfEntry arg1) -> gconf_entry_get_value arg1) entry'
key <- peekUTFString keyStrPtr
value <- marshalFromGConfValue (GConfValue valuePtr)
(\(GConfEntry arg1) -> gconf_entry_free arg1) entry'
return (key,value))
entryList
gconfAllDirs :: GlibString string => GConf -> string -> IO [string]
gconfAllDirs gc dir = do
gsList <- withUTFString dir $ \strPtr ->
(\(GConf arg1) arg2 arg3 -> withForeignPtr arg1 $ \argPtr1 ->gconf_client_all_dirs argPtr1 arg2 arg3) gc strPtr nullPtr
dirList <- fromGSList gsList
mapM (\strPtr -> do str <- peekUTFString strPtr
g_free (castPtr strPtr)
return str)
dirList
gconfDirExists :: GConf -> String -> IO Bool
gconfDirExists gc dir =
withCString dir $ \strPtr ->
liftM toBool $ (\(GConf arg1) arg2 arg3 -> withForeignPtr arg1 $ \argPtr1 ->gconf_client_dir_exists argPtr1 arg2 arg3) gc strPtr nullPtr
onValueChanged, afterValueChanged :: GlibString string => GConf ->
(string -> Maybe GConfValueDyn -> IO ()) ->
IO (ConnectId GConf)
onValueChanged gc handler =
connect_GLIBSTRING_PTR__NONE "value_changed" False gc
(convertValueChangedHandler handler)
afterValueChanged gc handler =
connect_GLIBSTRING_PTR__NONE "value_changed" True gc
(convertValueChangedHandler handler)
convertValueChangedHandler :: GlibString string => (string -> Maybe GConfValueDyn -> IO ()) ->
(string -> Ptr GConfValue -> IO ())
convertValueChangedHandler handler key ptr = do
value <- marshalFromGConfValue (GConfValue $ castPtr ptr)
handler key value
foreign import ccall unsafe "gconf_error_quark"
gconf_error_quark :: (IO CUInt)
foreign import ccall safe "g_type_init"
g_type_init :: (IO ())
foreign import ccall safe "gconf_client_get_default"
gconf_client_get_default :: (IO (Ptr GConf))
foreign import ccall safe "gconf_client_add_dir"
gconf_client_add_dir :: ((Ptr GConf) -> ((Ptr CChar) -> (CInt -> ((Ptr (Ptr ())) -> (IO ())))))
foreign import ccall safe "gconf_client_remove_dir"
gconf_client_remove_dir :: ((Ptr GConf) -> ((Ptr CChar) -> ((Ptr (Ptr ())) -> (IO ()))))
foreign import ccall safe "gconf_client_notify_add"
gconf_client_notify_add :: ((Ptr GConf) -> ((Ptr CChar) -> ((FunPtr ((Ptr GConf) -> (CUInt -> ((Ptr GConfEntry) -> ((Ptr ()) -> (IO ())))))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> ((Ptr (Ptr ())) -> (IO CUInt)))))))
foreign import ccall unsafe "gconf_entry_get_key"
gconf_entry_get_key :: ((Ptr GConfEntry) -> (IO (Ptr CChar)))
foreign import ccall unsafe "gconf_entry_get_value"
gconf_entry_get_value :: ((Ptr GConfEntry) -> (IO (Ptr GConfValue)))
foreign import ccall safe "gconf_client_notify_remove"
gconf_client_notify_remove :: ((Ptr GConf) -> (CUInt -> (IO ())))
foreign import ccall safe "gconf_client_get"
gconf_client_get :: ((Ptr GConf) -> ((Ptr CChar) -> ((Ptr (Ptr ())) -> (IO (Ptr GConfValue)))))
foreign import ccall safe "gconf_client_set"
gconf_client_set :: ((Ptr GConf) -> ((Ptr CChar) -> ((Ptr GConfValue) -> ((Ptr (Ptr ())) -> (IO ())))))
foreign import ccall safe "gconf_client_get_without_default"
gconf_client_get_without_default :: ((Ptr GConf) -> ((Ptr CChar) -> ((Ptr (Ptr ())) -> (IO (Ptr GConfValue)))))
foreign import ccall safe "gconf_client_get_default_from_schema"
gconf_client_get_default_from_schema :: ((Ptr GConf) -> ((Ptr CChar) -> ((Ptr (Ptr ())) -> (IO (Ptr GConfValue)))))
foreign import ccall safe "gconf_client_unset"
gconf_client_unset :: ((Ptr GConf) -> ((Ptr CChar) -> ((Ptr (Ptr ())) -> (IO CInt))))
foreign import ccall safe "gconf_client_clear_cache"
gconf_client_clear_cache :: ((Ptr GConf) -> (IO ()))
foreign import ccall safe "gconf_client_preload"
gconf_client_preload :: ((Ptr GConf) -> ((Ptr CChar) -> (CInt -> ((Ptr (Ptr ())) -> (IO ())))))
foreign import ccall safe "gconf_client_suggest_sync"
gconf_client_suggest_sync :: ((Ptr GConf) -> ((Ptr (Ptr ())) -> (IO ())))
foreign import ccall safe "gconf_client_all_entries"
gconf_client_all_entries :: ((Ptr GConf) -> ((Ptr CChar) -> ((Ptr (Ptr ())) -> (IO (Ptr ())))))
foreign import ccall unsafe "gconf_entry_free"
gconf_entry_free :: ((Ptr GConfEntry) -> (IO ()))
foreign import ccall safe "gconf_client_all_dirs"
gconf_client_all_dirs :: ((Ptr GConf) -> ((Ptr CChar) -> ((Ptr (Ptr ())) -> (IO (Ptr ())))))
foreign import ccall unsafe "g_free"
g_free :: ((Ptr ()) -> (IO ()))
foreign import ccall safe "gconf_client_dir_exists"
gconf_client_dir_exists :: ((Ptr GConf) -> ((Ptr CChar) -> ((Ptr (Ptr ())) -> (IO CInt))))