aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/Environment.hs
blob: f370de42b199afbaa17304ae638ef2e58ccfd4ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
{-
 - Copyright 2015 Egor Tensin <Egor.Tensin@gmail.com>
 - This file is licensed under the terms of the MIT License.
 - See LICENSE.txt for details.
-}

module Environment
    ( Profile(..)
    , profileKeyPath

    , VarName
    , VarValue
    , query
    , engrave
    , wipe

    , pathJoin
    , pathSplit
    ) where

import Data.List       (intercalate)
import Data.List.Split (splitOn)
import System.IO.Error (catchIOError, isDoesNotExistError)

import qualified Registry
import           WindowsUtils (notifyEnvironmentUpdate)

data Profile = CurrentUser
             | AllUsers
             deriving (Eq, Show)

profileRootKey :: Profile -> Registry.RootKey
profileRootKey CurrentUser = Registry.CurrentUser
profileRootKey AllUsers    = Registry.LocalMachine

profileRootKeyPath :: Profile -> Registry.KeyPath
profileRootKeyPath = Registry.rootKeyPath . profileRootKey

profileSubKeyPath :: Profile -> Registry.KeyPath
profileSubKeyPath CurrentUser =
    Registry.keyPathFromString "Environment"
profileSubKeyPath AllUsers =
    Registry.keyPathFromString "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"

profileKeyPath :: Profile -> Registry.KeyPath
profileKeyPath profile = Registry.keyPathJoin
    [ profileRootKeyPath profile
    , profileSubKeyPath  profile
    ]

openRootProfileKey :: Profile -> Registry.KeyHandle
openRootProfileKey = Registry.openRootKey . profileRootKey

openProfileKey :: Profile -> IO Registry.KeyHandle
openProfileKey profile = Registry.openSubKey (openRootProfileKey profile) (profileSubKeyPath profile)

type VarName  = Registry.ValueName
type VarValue = Registry.ValueData

query :: Profile -> VarName -> IO (Maybe VarValue)
query profile name = do
    keyHandle <- openProfileKey profile
    catchIOError (Registry.getString keyHandle name >>= return . Just) emptyIfDoesNotExist
  where
    emptyIfDoesNotExist e = if isDoesNotExistError e then return Nothing else ioError e

engrave :: Profile -> VarName -> VarValue -> IO ()
engrave profile name value = do
    keyHandle <- openProfileKey profile
    Registry.setString keyHandle name value
    notifyEnvironmentUpdate

wipe :: Profile -> VarName -> IO ()
wipe profile name = do
    keyHandle <- openProfileKey profile
    catchIOError (Registry.delValue keyHandle name) ignoreIfDoesNotExist
    notifyEnvironmentUpdate
  where
    ignoreIfDoesNotExist e = if isDoesNotExistError e then return () else ioError e

pathSep :: VarValue
pathSep = ";"

pathSplit :: VarValue -> [VarValue]
pathSplit = filter (not . null) . splitOn pathSep

pathJoin :: [VarValue] -> VarValue
pathJoin = intercalate pathSep . filter (not . null)