2012-08-02 00:27:45 +00:00
|
|
|
{- 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/
|
2013-07-09 23:56:30 +00:00
|
|
|
- http://standards.freedesktop.org/icon-theme-spec/latest/
|
2012-08-02 00:27:45 +00:00
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-08-02 00:27:45 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2012-08-02 00:27:45 +00:00
|
|
|
-}
|
|
|
|
|
2015-05-10 20:38:49 +00:00
|
|
|
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
|
|
|
|
2012-08-02 00:49:02 +00:00
|
|
|
module Utility.FreeDesktop (
|
2012-08-02 00:27:45 +00:00
|
|
|
DesktopEntry,
|
|
|
|
genDesktopEntry,
|
|
|
|
buildDesktopMenuFile,
|
|
|
|
writeDesktopMenuFile,
|
2012-08-02 03:31:53 +00:00
|
|
|
desktopMenuFilePath,
|
|
|
|
autoStartPath,
|
2013-07-10 00:50:41 +00:00
|
|
|
iconDir,
|
2013-07-09 23:56:30 +00:00
|
|
|
iconFilePath,
|
2012-08-02 03:31:53 +00:00
|
|
|
systemDataDir,
|
|
|
|
systemConfigDir,
|
|
|
|
userDataDir,
|
2012-08-02 11:47:20 +00:00
|
|
|
userConfigDir,
|
|
|
|
userDesktopDir
|
2012-08-02 00:27:45 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Utility.Exception
|
2012-10-25 22:17:32 +00:00
|
|
|
import Utility.UserInfo
|
2012-08-03 03:51:38 +00:00
|
|
|
import Utility.Process
|
2012-08-02 00:27:45 +00:00
|
|
|
|
|
|
|
import System.Environment
|
|
|
|
import System.FilePath
|
2016-09-22 15:34:55 +00:00
|
|
|
import System.Directory
|
2012-08-02 00:27:45 +00:00
|
|
|
import Data.List
|
2013-07-09 23:56:30 +00:00
|
|
|
import Data.Maybe
|
2012-08-03 03:51:38 +00:00
|
|
|
import Control.Applicative
|
2015-05-10 20:19:56 +00:00
|
|
|
import Prelude
|
2012-08-02 00:27:45 +00:00
|
|
|
|
|
|
|
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"
|
2016-09-22 15:22:41 +00:00
|
|
|
toString (NumericV f) = show f
|
2012-08-02 00:27:45 +00:00
|
|
|
toString (ListV l)
|
|
|
|
| null l = ""
|
2016-09-22 15:22:41 +00:00
|
|
|
| otherwise = (intercalate ";" $ map (concatMap escapesemi . toString) l) ++ ";"
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2016-09-22 15:22:41 +00:00
|
|
|
escapesemi ';' = "\\;"
|
|
|
|
escapesemi c = [c]
|
2012-08-02 00:27:45 +00:00
|
|
|
|
2013-07-09 23:56:30 +00:00
|
|
|
genDesktopEntry :: String -> String -> Bool -> FilePath -> Maybe String -> [String] -> DesktopEntry
|
|
|
|
genDesktopEntry name comment terminal program icon categories = catMaybes
|
2012-08-02 00:49:02 +00:00
|
|
|
[ item "Type" StringV "Application"
|
2012-08-02 00:27:45 +00:00
|
|
|
, item "Version" NumericV 1.0
|
|
|
|
, item "Name" StringV name
|
|
|
|
, item "Comment" StringV comment
|
|
|
|
, item "Terminal" BoolV terminal
|
|
|
|
, item "Exec" StringV program
|
2013-07-09 23:56:30 +00:00
|
|
|
, maybe Nothing (item "Icon" StringV) icon
|
2012-08-02 00:27:45 +00:00
|
|
|
, item "Categories" ListV (map StringV categories)
|
|
|
|
]
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2013-07-09 23:56:30 +00:00
|
|
|
item x c y = Just (x, c y)
|
2012-08-02 00:27:45 +00:00
|
|
|
|
|
|
|
buildDesktopMenuFile :: DesktopEntry -> String
|
|
|
|
buildDesktopMenuFile d = unlines ("[Desktop Entry]" : map keyvalue d) ++ "\n"
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
keyvalue (k, v) = k ++ "=" ++ toString v
|
2012-08-02 00:27:45 +00:00
|
|
|
|
|
|
|
writeDesktopMenuFile :: DesktopEntry -> String -> IO ()
|
|
|
|
writeDesktopMenuFile d file = do
|
2016-09-22 15:25:01 +00:00
|
|
|
createDirectoryIfMissing True (takeDirectory file)
|
2012-08-02 00:27:45 +00:00
|
|
|
writeFile file $ buildDesktopMenuFile d
|
|
|
|
|
2012-08-02 11:47:20 +00:00
|
|
|
{- Path to use for a desktop menu file, in either the systemDataDir or
|
|
|
|
- the userDataDir -}
|
2012-08-02 03:31:53 +00:00
|
|
|
desktopMenuFilePath :: String -> FilePath -> FilePath
|
|
|
|
desktopMenuFilePath basename datadir =
|
|
|
|
datadir </> "applications" </> desktopfile basename
|
2012-08-02 00:27:45 +00:00
|
|
|
|
2012-08-02 11:47:20 +00:00
|
|
|
{- Path to use for a desktop autostart file, in either the systemDataDir
|
|
|
|
- or the userDataDir -}
|
2012-08-02 03:31:53 +00:00
|
|
|
autoStartPath :: String -> FilePath -> FilePath
|
|
|
|
autoStartPath basename configdir =
|
|
|
|
configdir </> "autostart" </> desktopfile basename
|
2012-08-02 00:49:02 +00:00
|
|
|
|
2013-07-10 00:50:41 +00:00
|
|
|
{- Base directory to install an icon file, in either the systemDataDir
|
|
|
|
- or the userDatadir. -}
|
|
|
|
iconDir :: FilePath -> FilePath
|
|
|
|
iconDir datadir = datadir </> "icons" </> "hicolor"
|
|
|
|
|
|
|
|
{- Filename of an icon, given the iconDir to use.
|
2013-07-10 00:16:07 +00:00
|
|
|
-
|
2013-07-09 23:56:30 +00:00
|
|
|
- The resolution is something like "48x48" or "scalable". -}
|
2013-07-10 00:16:07 +00:00
|
|
|
iconFilePath :: FilePath -> String -> FilePath -> FilePath
|
2013-07-10 00:50:41 +00:00
|
|
|
iconFilePath file resolution icondir =
|
|
|
|
icondir </> resolution </> "apps" </> file
|
2013-07-09 23:56:30 +00:00
|
|
|
|
2012-08-02 00:49:02 +00:00
|
|
|
desktopfile :: FilePath -> FilePath
|
|
|
|
desktopfile f = f ++ ".desktop"
|
2012-08-02 00:27:45 +00:00
|
|
|
|
2012-08-02 11:47:20 +00:00
|
|
|
{- Directory used for installation of system wide data files.. -}
|
2012-08-02 03:31:53 +00:00
|
|
|
systemDataDir :: FilePath
|
|
|
|
systemDataDir = "/usr/share"
|
|
|
|
|
2012-08-02 11:47:20 +00:00
|
|
|
{- Directory used for installation of system wide config files. -}
|
2012-08-02 03:31:53 +00:00
|
|
|
systemConfigDir :: FilePath
|
|
|
|
systemConfigDir = "/etc/xdg"
|
|
|
|
|
2012-08-02 11:47:20 +00:00
|
|
|
{- Directory for user data files. -}
|
2012-08-02 00:27:45 +00:00
|
|
|
userDataDir :: IO FilePath
|
2012-08-02 11:47:20 +00:00
|
|
|
userDataDir = xdgEnvHome "DATA_HOME" ".local/share"
|
2012-08-02 00:27:45 +00:00
|
|
|
|
2012-08-02 11:47:20 +00:00
|
|
|
{- Directory for user config files. -}
|
2012-08-02 00:27:45 +00:00
|
|
|
userConfigDir :: IO FilePath
|
2012-08-02 11:47:20 +00:00
|
|
|
userConfigDir = xdgEnvHome "CONFIG_HOME" ".config"
|
2012-08-02 00:27:45 +00:00
|
|
|
|
2012-08-03 03:51:38 +00:00
|
|
|
{- Directory for the user's Desktop, may be localized.
|
|
|
|
-
|
|
|
|
- This is not looked up very fast; the config file is in a shell format
|
|
|
|
- that is best parsed by shell, so xdg-user-dir is used, with a fallback
|
|
|
|
- to ~/Desktop. -}
|
2012-08-02 11:47:20 +00:00
|
|
|
userDesktopDir :: IO FilePath
|
2012-08-03 03:51:38 +00:00
|
|
|
userDesktopDir = maybe fallback return =<< (parse <$> xdg_user_dir)
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2016-09-22 15:29:53 +00:00
|
|
|
parse s = case lines <$> s of
|
|
|
|
Just (l:_) -> Just l
|
|
|
|
_ -> Nothing
|
2012-12-13 04:24:19 +00:00
|
|
|
xdg_user_dir = catchMaybeIO $ readProcess "xdg-user-dir" ["DESKTOP"]
|
|
|
|
fallback = xdgEnvHome "DESKTOP_DIR" "Desktop"
|
2012-08-02 11:47:20 +00:00
|
|
|
|
|
|
|
xdgEnvHome :: String -> String -> IO String
|
|
|
|
xdgEnvHome envbase homedef = do
|
|
|
|
home <- myHomeDir
|
2012-09-17 04:18:07 +00:00
|
|
|
catchDefaultIO (home </> homedef) $
|
|
|
|
getEnv $ "XDG_" ++ envbase
|