~alcinnz/hurl

ref: ffbfedf62b15960138f418f5b16705b5915805df hurl/src/Network/URI/XDG/DesktopEntry.hs -rw-r--r-- 4.2 KiB
ffbfedf6 — Adrian Cochrane Fix what's now a syntax error (whitespace-separated @). 1 year, 8 months ago
                                                                                
915d7f98 Adrian Cochrane
8d239eb6 Adrian Cochrane
915d7f98 Adrian Cochrane
8d239eb6 Adrian Cochrane
915d7f98 Adrian Cochrane
8d239eb6 Adrian Cochrane
915d7f98 Adrian Cochrane
8d239eb6 Adrian Cochrane
915d7f98 Adrian Cochrane
8d239eb6 Adrian Cochrane
915d7f98 Adrian Cochrane
8d239eb6 Adrian Cochrane
915d7f98 Adrian Cochrane
8d239eb6 Adrian Cochrane
915d7f98 Adrian Cochrane
8d239eb6 Adrian Cochrane
915d7f98 Adrian Cochrane
8d239eb6 Adrian Cochrane
915d7f98 Adrian Cochrane
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
module Network.URI.XDG.DesktopEntry(launchApp, launchApp', desktop2app) where

import Data.Maybe (fromMaybe, catMaybes, isJust)
import Data.List (isInfixOf)
import Control.Exception (catch)
import System.Environment (lookupEnv)
import Control.Monad (forM)
import System.Directory (doesFileExist)
import System.FilePath

import Network.URI
import System.Process (spawnCommand)

import Network.URI.XDG.Ini
import Network.URI.XDG.MimeApps (split, fromMaybe')
import Network.URI.Types (Application(..))

launchApp' :: [String] -- ^ The locale to use
             -> URI -- ^ The URI to have it open.
             -> String -- ^ The .desktop ID
             -> IO (Either String Bool) -- ^ The localized name of the application or whether it expects a local path.
launchApp' locales uri desktopID = do
    app <- readDesktopID desktopID
    let grp = "desktop entry"
    let name = fromMaybe desktopID $ iniLookupLocalized locales grp "name" app
    case (iniLookup grp "type" app, iniLookup grp "exec" app) of
        (Just "Application", Just exec) | uriScheme uri /= "file:" && (isInfixOf "%f" exec || isInfixOf "%F" exec) ->
            return $ Right True
        (Just "Application", Just exec) ->
            catch (execApp uri exec name app) execFailed
        _ -> return $ Right False

launchApp :: [String] -- ^ The locale to use
             -> URI -- ^ The URI to have it open.
             -> String -- ^ The .desktop ID
             -> IO (Maybe String) -- ^ The localized name of the application
launchApp a b c = leftToMaybe <$> launchApp' a b c

readDesktopID desktopID = do
    dirs <- lookupEnv "XDG_DATA_DIRS"
    let dirs' = split ':' $ fromMaybe' "/usr/local/share/:/usr/share/" dirs
    filepaths <- forM (filter (/= "") dirs') $ \dir -> do
        exists <- doesFileExist (dir </> "applications" </> desktopID)
        if exists then
            return $ Just (dir </> "applications" </> desktopID)
        else
            return Nothing -- TODO? Handle cases where - = subdirectory path?
    case catMaybes filepaths of
        (filepath:_) -> do
            source <- readFile filepath
            let metadata = (" ", ["filename", filepath]) -- Used by %k macro
            return (parseIni source)
        [] -> return []

-- Capitals usually means supports multiple arguments,
-- but HURL doesn't support making use of that.
macros uri@URI {uriScheme="file:", uriPath=f} ('%':'f':cmd) x = esc f ++ macros uri cmd x
macros uri@URI {uriScheme="file:", uriPath=f} ('%':'F':cmd) x = esc f ++ macros uri cmd x
macros uri ('%':'u':cmd) x = esc uri ++ macros uri cmd x
macros uri ('%':'U':cmd) x = esc uri ++ macros uri cmd x
macros uri ('%':'i':cmd) (app, name)
    | Just icon <- iniLookup "desktop entry" "icon" app =
        "--icon " ++ esc icon ++ macros uri cmd (app, name)
    | otherwise = macros uri cmd (app, name)
macros uri ('%':'c':cmd) (app, name) = esc name ++ macros uri cmd (app, name)
macros uri ('%':'k':cmd) (app, name)
    | Just file <- iniLookup " " "filename" app = esc file ++ macros uri cmd (app, name)
    | otherwise = macros uri cmd (app, name)
macros uri ('%':'%':cmd) x = '%' : macros uri cmd x
macros uri (c:cmd) x = c : macros uri cmd x
macros _ [] _ = []

esc txt = '\'' : esc' (show txt)
esc' ('\'':cs) = '\\' : '\'' : esc' cs
esc' (c:cs) = c : esc' cs
esc' [] = "'"

execApp :: URI -> String -> String -> INI -> IO (Either String a)
execApp uri exec name app = do
    spawnCommand $ macros uri exec (app, name)
    return $ Left name

execFailed :: IOError -> IO (Either a Bool)
execFailed _ = return $ Right False

desktop2app :: [String] -> String -> IO (Maybe Application)
desktop2app locales desktopId = do
    app <- readDesktopID desktopId
    let grp = "desktop entry"
    let localized key = iniLookupLocalized locales grp key app
    let isApp = iniLookup grp "type" app == Just "Application" && isJust (iniLookup grp "exec" app)
    return $ if isApp then Just $ Application {
        name = fromMaybe desktopId $ localized "name",
        description = fromMaybe "" $ localized "comment",
        icon = case localized "icon" of
            Just icon -> URI "xdg-icon:" Nothing icon "" ""
            Nothing -> URI "about:" Nothing "blank" "" "",
        appId = desktopId
    } else Nothing

--- Utils

leftToMaybe (Left a) = Just a
leftToMaybe (Right _) = Nothing