aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/WindowsEnv/Environment.hs
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2017-01-25 05:58:46 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2017-01-25 05:58:46 +0300
commitd3e2eeb7892e132120174de5567d932d83c97218 (patch)
treede2fa96af0f64764e7abf7b9482078e19502bae9 /src/WindowsEnv/Environment.hs
parentadd Portability to module descriptions (diff)
downloadwindows-env-d3e2eeb7892e132120174de5567d932d83c97218.tar.gz
windows-env-d3e2eeb7892e132120174de5567d932d83c97218.zip
rename namespace 'Windows' to 'WindowsEnv'
Diffstat (limited to 'src/WindowsEnv/Environment.hs')
-rw-r--r--src/WindowsEnv/Environment.hs79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/WindowsEnv/Environment.hs b/src/WindowsEnv/Environment.hs
new file mode 100644
index 0000000..8bfb449
--- /dev/null
+++ b/src/WindowsEnv/Environment.hs
@@ -0,0 +1,79 @@
+-- |
+-- Description : High-level environment variables management functions
+-- Copyright : (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
+-- License : MIT
+-- Maintainer : Egor.Tensin@gmail.com
+-- Stability : experimental
+-- Portability : Windows-only
+--
+-- High-level functions for reading and writing Windows environment variables.
+
+module WindowsEnv.Environment
+ ( Profile(..)
+ , profileKeyPath
+
+ , VarName
+ , VarValue
+ , query
+ , engrave
+ , engraveForce
+ , wipe
+
+ , pathJoin
+ , pathSplit
+ ) where
+
+import Control.Monad.Trans.Class (lift)
+import Control.Monad.Trans.Except (ExceptT(..))
+import Data.List (intercalate)
+import Data.List.Split (splitOn)
+
+import qualified WindowsEnv.Registry as Registry
+import WindowsEnv.Utils (notifyEnvironmentUpdate)
+
+data Profile = CurrentUser
+ | AllUsers
+ deriving (Eq, Show)
+
+profileKeyPath :: Profile -> Registry.KeyPath
+profileKeyPath CurrentUser = Registry.KeyPath Registry.CurrentUser ["Environment"]
+profileKeyPath AllUsers = Registry.KeyPath Registry.LocalMachine
+ [ "SYSTEM"
+ , "CurrentControlSet"
+ , "Control"
+ , "Session Manager"
+ , "Environment"
+ ]
+
+type VarName = String
+type VarValue = String
+
+query :: Profile -> VarName -> ExceptT IOError IO VarValue
+query profile name = Registry.getExpandedString (profileKeyPath profile) name
+
+engrave :: Profile -> VarName -> VarValue -> ExceptT IOError IO ()
+engrave profile name value = do
+ ret <- Registry.setStringPreserveType (profileKeyPath profile) name value
+ lift notifyEnvironmentUpdate
+ return ret
+
+engraveForce :: Profile -> VarName -> VarValue -> ExceptT IOError IO ()
+engraveForce profile name value = do
+ ret <- Registry.setString (profileKeyPath profile) name value
+ lift notifyEnvironmentUpdate
+ return ret
+
+wipe :: Profile -> VarName -> ExceptT IOError IO ()
+wipe profile name = do
+ ret <- Registry.deleteValue (profileKeyPath profile) name
+ lift notifyEnvironmentUpdate
+ return ret
+
+pathSep :: VarValue
+pathSep = ";"
+
+pathSplit :: VarValue -> [VarValue]
+pathSplit = filter (not . null) . splitOn pathSep
+
+pathJoin :: [VarValue] -> VarValue
+pathJoin = intercalate pathSep . filter (not . null)