module Manatee.Core.FileOpenRule where
import Control.Monad
import DBus.Client hiding (Signal)
import Data.ByteString.UTF8
import Manatee.Core.DBus
import Manatee.Core.Config
import Manatee.Core.Types
import System.GIO.File.AppInfo
import System.GIO.File.File
import Text.Regex.TDFA
import qualified Data.Map as M
fileOpenRule :: FilePath -> String -> IO [(String, Client -> IO ())]
fileOpenRule filePath contentType = do
(FileOpenRule ruleMap) <- readConfig fileOpenRulePath (FileOpenRule M.empty)
let rule = M.toList ruleMap
getDefalutRule [] = []
getDefalutRule ((fileMatch, rules) :xs) =
let isMatch = case fileMatch of
ContentTypeMatch match ->
contentType == match
RegexpMatch match ->
contentType =~ match
in if isMatch
then
map (\ (name, pType, prefix) ->
(name,
\client ->
mkDaemonSignal client NewTab (NewTabArgs pType (prefix ++ filePath)))
) rules
else getDefalutRule xs
let defaultRule = getDefalutRule rule
let editorRule = if contentType =~ ("^text/*" :: String)
then [("Open file", openByDefaultEditor filePath)]
else []
systemRule <- systemOpenRule filePath contentType
return $ defaultRule ++ editorRule ++ systemRule
systemOpenRule :: FilePath -> String -> IO [(String, Client -> IO ())]
systemOpenRule filePath contentType = do
infos <- appInfoGetAllForType contentType
rule <- forM infos $ \info -> do
let appName = appInfoGetDisplayName info
file = (fileFromPath . fromString) filePath
return ("Open by " ++ appName, \_ -> appInfoLaunch info [file] Nothing >> return ())
return rule
openByDefaultEditor :: FilePath -> Client -> IO ()
openByDefaultEditor filePath client =
mkDaemonSignal client NewTab (NewTabArgs "PageEditor" filePath)