{-# LANGUAGE OverloadedStrings #-}
module Network.URI.XDG.AppStreamOutput(serializeXML, outputApps, testLocalIcons) where
import qualified Text.XML as XML
import qualified Data.Map as M
import Data.Text (Text, append, pack)
import qualified Data.Text as Txt
import Data.Text.Lazy (unpack)
import Network.URI.XDG.AppStream
import Data.List (stripPrefix)
import Control.Monad (forM)
import System.Directory (doesFileExist)
import Data.Maybe (catMaybes)
outputApps :: [App] -> String
outputApps [App]
apps = Element -> String
serializeXML forall a b. (a -> b) -> a -> b
$ Text -> [Element] -> Element
el Text
"p" forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map App -> Element
outputApp [App]
apps
outputApp :: App -> Element
outputApp (App Text
ident' Text
name' Text
summary' [Icon]
icons') =
Text -> [(Name, Text)] -> [Element] -> Element
el' Text
"a" [(Name
"href", Text
"appstream://" Text -> Text -> Text
`append` Text
ident'), (Name
"title", Text
summary')] [
Text -> [Element] -> Element
el Text
"picture" [
Text -> [(Name, Text)] -> [Element] -> Element
el' (if Integer
i forall a. Eq a => a -> a -> Bool
== Integer
0 then Text
"img" else Text
"source") [
(Name
"src", Text
url'),
(Name
"alt", Text
name' Text -> Text -> Text
`append` Text
" logo " Text -> Text -> Text
`append` forall {a}. Show a => Maybe a -> Text
int2txt Maybe Int
width' Text -> Text -> Text
`append` Text
"x" Text -> Text -> Text
`append` forall {a}. Show a => Maybe a -> Text
int2txt Maybe Int
height'),
(Name
"sizes", forall {a}. Show a => Maybe a -> Text
int2txt Maybe Int
width' Text -> Text -> Text
`append` Text
"w")] []
| (Integer
i, Icon Text
_ Maybe Int
width' Maybe Int
height' Text
url') <- forall a b. [a] -> [b] -> [(a, b)]
zip [Integer
0..] [Icon]
icons'
],
Name -> Map Name Text -> [Node] -> Element
XML.Element Name
"caption" forall k a. Map k a
M.empty [Text -> Node
XML.NodeContent Text
name']]
testLocalIcons :: [Icon] -> IO [Icon]
testLocalIcons [Icon]
icons = do
[Maybe Icon]
icons' <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Icon]
icons forall a b. (a -> b) -> a -> b
$ \Icon
icon -> case String
"file://" forall a. Eq a => [a] -> [a] -> Maybe [a]
`stripPrefix` Text -> String
Txt.unpack (Icon -> Text
url Icon
icon) of
Just String
path -> do
Bool
exists <- String -> IO Bool
doesFileExist String
path
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ if Bool
exists then forall a. a -> Maybe a
Just Icon
icon else forall a. Maybe a
Nothing
Maybe String
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just Icon
icon
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. [Maybe a] -> [a]
catMaybes [Maybe Icon]
icons'
serializeXML :: Element -> String
serializeXML Element
el = Text -> String
unpack forall a b. (a -> b) -> a -> b
$ RenderSettings -> Document -> Text
XML.renderText forall a. Default a => a
XML.def XML.Document {
documentPrologue :: Prologue
XML.documentPrologue = [Miscellaneous] -> Maybe Doctype -> [Miscellaneous] -> Prologue
XML.Prologue [] forall a. Maybe a
Nothing [],
documentRoot :: Element
XML.documentRoot = Element
el,
documentEpilogue :: [Miscellaneous]
XML.documentEpilogue = []
}
el' :: Text -> [(Name, Text)] -> [Element] -> Element
el' Text
name [(Name, Text)]
attrs [Element]
children = XML.Element {
elementName :: Name
XML.elementName = Text -> Maybe Text -> Maybe Text -> Name
XML.Name Text
name forall a. Maybe a
Nothing forall a. Maybe a
Nothing,
elementAttributes :: Map Name Text
XML.elementAttributes = forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(Name, Text)]
attrs,
elementNodes :: [Node]
XML.elementNodes = forall a b. (a -> b) -> [a] -> [b]
map Element -> Node
XML.NodeElement [Element]
children
}
el :: Text -> [Element] -> Element
el Text
name [Element]
children = Text -> [(Name, Text)] -> [Element] -> Element
el' Text
name [] [Element]
children
int2txt :: Maybe a -> Text
int2txt (Just a
n) = String -> Text
pack forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show a
n
int2txt Maybe a
Nothing = Text
"?"