~alcinnz/hurl

ref: f605efd1872c427894e67aa40893cad9ce8ce577 hurl/src/Network/MIME/Info.hs -rw-r--r-- 1.1 KiB
f605efd1 — Adrian Cochrane Release v0.1 decoupled from Rhapsode, with CSV/TSV & refined tablesorting! 2 years ago
                                                                                
6d7fb390 Adrian Cochrane
cdc252ef Adrian Cochrane
6d7fb390 Adrian Cochrane
8161527b Adrian Cochrane
6d7fb390 Adrian Cochrane
8161527b Adrian Cochrane
6d7fb390 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
{-# LANGUAGE CPP #-}
module Network.MIME.Info(mimeInfo, MIME, Application(..)) where

#ifdef WITH_XDG
import Network.URI.XDG.MimeInfo (readMimeInfo)
#endif
import Network.URI.Locale (rfc2616Locale)
import Network.URI.Types (Application(..))

import qualified Data.Map as M
import Control.Concurrent.MVar (MVar, newMVar, readMVar, modifyMVar_)
import System.IO.Unsafe (unsafePerformIO)
import Data.Char (toLower)

type MIME = Application

{-# NOINLINE mimeInfo #-}
mimeInfo :: String -> MIME
mimeInfo = unsafePerformIO $ do
    (locales, _) <- rfc2616Locale
    cache <- newMVar M.empty :: IO (MVar (M.Map String MIME))
    return $ \mime -> unsafePerformIO $ do
        readMVar cache >>= inner mime locales cache
  where
    inner mime _ _ cache | Just val <- mime `M.lookup` cache = return val
    inner mime locales cache' cache = do
        ret <- readMimeInfo locales mime
        modifyMVar_ cache' $ return . M.insert mime ret
        return ret

#ifndef WITH_XDG
readMimeInfo _ mime = return Application {
        name = mime,
        icon = URI "about:" Nothing "invalid" "" "",
        description = "",
        appId = mime
    }
#endif