~alcinnz/rhapsode

ref: bfcceaac93082176ddf0c0aa31d6edb7c230d70b rhapsode/src/Types.hs -rw-r--r-- 2.3 KiB
bfcceaac — Adrian Cochrane Add dependency on CMU Sphinx. 3 years ago
                                                                                
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
{-# LANGUAGE OverloadedStrings #-}
module Types(CArray, Page(..), Application(..)) where

import System.Directory (getCurrentDirectory) -- default referer URI
import SpeechStyle (SpeechStyle)
import Data.CSS.Preprocessor.Conditions (ConditionalStyles, conditionalStyles)
import Data.CSS.Preprocessor.Text (TextStyle)
import Text.XML
import qualified Data.Map.Strict as M
import Network.URI
import Network.URI.Fetch (Application(..))

-- For the in-memory history log
import Data.Trie.Text (Trie)
import qualified Data.Text as Txt
import qualified Data.Trie.Text as Trie
import System.Directory
import System.FilePath ((</>))
import Control.Parallel (par)
import qualified System.IO.Strict as Strict

import Foreign.Ptr
import Foreign.StablePtr

type CArray a = Ptr a

data Page = Page {
    url :: URI,
    css :: ConditionalStyles (TextStyle SpeechStyle),
    html :: Document,
    pageTitle :: String,
    pageMIME :: String,
    apps :: [Application],
    backStack :: [(String, URI)],
    forwardStack :: [(String, URI)],
    -- Probably don't need an MVar here, but let's be safe!
    visitedURLs :: Trie ()
}

foreign export ccall c_initialReferer :: IO (StablePtr Page)

loadVisited :: IO (Trie ())
loadVisited = do
    dir <- getXdgDirectory XdgData "rhapsode"
    let path = dir </> "history.gmni"
    exists <- doesFileExist path

    if exists then do
        file <- Strict.readFile path -- Can't leave this file locked when I'll shortly append to it!
        let hist = Trie.fromList [(Txt.pack uri, ()) | _:uri:_ <- map words $ lines file]
        hist `par` return hist
    else return Trie.empty

c_initialReferer = do
    cwd <- getCurrentDirectory
    hist <- loadVisited
    newStablePtr $ Page {
        -- Default to URIs being relative to CWD.
        url = URI {uriScheme = "file:", uriPath = cwd,
            uriAuthority = Nothing, uriQuery = "", uriFragment = ""},
        -- Blank values:
        css = conditionalStyles nullURI "temp",
        html = Document {
            documentPrologue = Prologue [] Nothing [],
            documentRoot = Element "temp" M.empty [],
            documentEpilogue = []
        },
        pageTitle = "", pageMIME = "", apps = [],
        backStack = [], forwardStack = [], visitedURLs = hist
    }

foreign export ccall c_freePage :: StablePtr Page -> IO ()

c_freePage = freeStablePtr