summaryrefslogtreecommitdiff
path: root/Utility/FreeDesktop.hs
blob: 5bab4950ab541f5a37b960945aa9ea8d9b729906 (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{- Freedesktop.org specifications
 -
 - http://standards.freedesktop.org/basedir-spec/latest/
 - http://standards.freedesktop.org/desktop-entry-spec/latest/
 - http://standards.freedesktop.org/menu-spec/latest/
 -
 - Copyright 2012 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Utility.FreeDesktop (
	DesktopEntry,	
	genDesktopEntry,
	buildDesktopMenuFile,
	writeDesktopMenuFile,
	desktopMenuFilePath,
	autoStartPath,
	systemDataDir,
	systemConfigDir,
	userDataDir,
	userConfigDir	
) where

import Utility.Exception
import Utility.Directory
import Utility.Path

import System.IO
import System.Environment
import System.Directory
import System.FilePath
import Data.List
import Data.String.Utils

type DesktopEntry = [(Key, Value)]

type Key = String

data Value = StringV String | BoolV Bool | NumericV Float | ListV [Value]

toString :: Value -> String
toString (StringV s) = s
toString (BoolV b)
	| b = "true"
	| otherwise = "false"
toString(NumericV f) = show f
toString (ListV l)
	| null l = ""
	| otherwise = (intercalate ";" $ map (escapesemi . toString) l) ++ ";"
	where
		escapesemi = join "\\;" . split ";"

genDesktopEntry :: String -> String -> Bool -> FilePath -> [String] -> DesktopEntry
genDesktopEntry name comment terminal program categories =
	[ item "Type" StringV "Application"
	, item "Version" NumericV 1.0
	, item "Name" StringV name
	, item "Comment" StringV comment
	, item "Terminal" BoolV terminal
	, item "Exec" StringV program
	, item "Categories" ListV (map StringV categories)
	]
	where
		item x c y = (x, c y)

buildDesktopMenuFile :: DesktopEntry -> String
buildDesktopMenuFile d = unlines ("[Desktop Entry]" : map keyvalue d) ++ "\n"
	where
		keyvalue (k, v) = k ++ "=" ++ toString v

writeDesktopMenuFile :: DesktopEntry -> String -> IO ()
writeDesktopMenuFile d file = do
	createDirectoryIfMissing True (parentDir file)
	writeFile file $ buildDesktopMenuFile d

desktopMenuFilePath :: String -> FilePath -> FilePath
desktopMenuFilePath basename datadir = 
	datadir </> "applications" </> desktopfile basename

autoStartPath :: String -> FilePath -> FilePath
autoStartPath basename configdir =
	configdir </> "autostart" </> desktopfile basename

desktopfile :: FilePath -> FilePath
desktopfile f = f ++ ".desktop"

systemDataDir :: FilePath
systemDataDir = "/usr/share"

systemConfigDir :: FilePath
systemConfigDir = "/etc/xdg"

userDataDir :: IO FilePath
userDataDir = do
	dir <- xdgEnv "DATA_HOME" =<< myHomeDir
	return $ dir </> ".local" </> "share"

userConfigDir :: IO FilePath
userConfigDir = do
	dir <- xdgEnv "CONFIG_HOME" =<< myHomeDir
	return $ dir </> ".config"

xdgEnv :: String -> String -> IO String
xdgEnv envbase def = catchDefaultIO (getEnv $ "XDG_" ++ envbase) def