1{-# LANGUAGE ScopedTypeVariables #-}
2{-# LANGUAGE TemplateHaskell #-}
3module GUI.DataFiles
4  ( ui
5  , loadLogo
6  ) where
7import Control.Exception (IOException, Handler(..), catches)
8import System.IO
9
10import Data.FileEmbed
11import Graphics.UI.Gtk (Pixbuf, pixbufNewFromFile)
12import Language.Haskell.TH
13import System.Glib (GError)
14import System.IO.Temp
15import qualified Data.ByteString as B
16import qualified Data.Text.Encoding as TE
17
18uiFile :: FilePath
19uiFile = "threadscope.ui"
20
21logoFile :: FilePath
22logoFile = "threadscope.png"
23
24-- | Textual representaion of the UI file
25ui :: Q Exp
26ui = [| TE.decodeUtf8 $(makeRelativeToProject uiFile >>= embedFile) |]
27
28renderLogo :: B.ByteString -> IO (Maybe Pixbuf)
29renderLogo bytes =
30  withSystemTempFile logoFile $ \path h -> do
31    B.hPut h bytes
32    hClose h
33    Just <$> pixbufNewFromFile path
34  `catches`
35    -- in case of a failure in the file IO or pixbufNewFromFile, return Nothing
36    [ Handler $ \(_ :: IOException) -> return Nothing
37    , Handler $ \(_ :: GError) -> return Nothing
38    ]
39
40-- | Load the logo file as a 'Pixbuf'.
41loadLogo :: Q Exp
42loadLogo = [| renderLogo $(makeRelativeToProject logoFile >>= embedFile) |]
43