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 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 (constructNewGObject)
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
constructNewGObject 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 :: 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 =>
GConf ->
String ->
(String -> value -> IO ()) ->
IO GConfConnectId
gconfNotifyAdd gc key handler =
connect_GConfClientNotifyFunc gc key (convertValueChangedHandler handler)
where convertValueChangedHandler :: GConfValueClass value =>
(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 => GConf
-> String
-> IO value
gconfGet gc key = do
value <- propagateGError $ \gerrorPtr ->
withCString key $ \strPtr ->
(\(GConf arg1) arg2 arg3 -> withForeignPtr arg1 $ \argPtr1 ->gconf_client_get argPtr1 arg2 arg3) gc strPtr gerrorPtr
marshalFromGConfValue (GConfValue value)
gconfGetInt :: GConf -> String -> IO Int
gconfGetInt = gconfGet
gconfGetBool :: GConf -> String -> IO Bool
gconfGetBool = gconfGet
gconfGetFloat :: GConf -> String -> IO Double
gconfGetFloat = gconfGet
gconfGetString :: GConf -> String -> IO String
gconfGetString = gconfGet
gconfGetPair :: GConfValueClass (a,b) =>
GConf -> String -> IO (a, b)
gconfGetPair = gconfGet
gconfGetList :: GConfValueClass [a] =>
GConf -> String -> IO [a]
gconfGetList = gconfGet
gconfSet :: GConfValueClass value => GConf
-> String
-> value
-> IO ()
gconfSet gc key val = do
value@(GConfValue ptr) <- marshalToGConfValue val
if ptr == nullPtr
then gconfUnset gc key
else propagateGError $ \gerrorPtr ->
withCString key $ \strPtr ->
(\(GConf arg1) arg2 (GConfValue arg3) arg4 -> withForeignPtr arg1 $ \argPtr1 ->gconf_client_set argPtr1 arg2 arg3 arg4) gc strPtr value gerrorPtr
gconfSetInt :: GConf -> String -> Int -> IO ()
gconfSetInt = gconfSet
gconfSetBool :: GConf -> String -> Bool -> IO ()
gconfSetBool = gconfSet
gconfSetFloat :: GConf -> String -> Double -> IO ()
gconfSetFloat = gconfSet
gconfSetString :: GConf -> String -> String -> IO ()
gconfSetString = gconfSet
gconfSetPair :: GConfValueClass (a,b) =>
GConf -> String -> (a, b) -> IO ()
gconfSetPair = gconfSet
gconfSetList :: GConfValueClass [a] =>
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 :: GConf -> String -> IO ()
gconfUnset gc key =
propagateGError $ \gerrorPtr ->
withCString 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 :: GConf -> String -> IO [(String, GConfValueDyn)]
gconfAllEntries gc dir = do
gsList <- propagateGError $ \gerrorPtr ->
withCString 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 :: GConf -> String -> IO [String]
gconfAllDirs gc dir = do
gsList <- withCString 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 :: GConf ->
(String -> Maybe GConfValueDyn -> IO ()) ->
IO (ConnectId GConf)
onValueChanged gc handler =
connect_STRING_PTR__NONE "value_changed" False gc
(convertValueChangedHandler handler)
afterValueChanged gc handler =
connect_STRING_PTR__NONE "value_changed" True gc
(convertValueChangedHandler handler)
convertValueChangedHandler :: (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))))