module Manatee.Plugin.Anything.AnythingBrowseHistory where
import DBus.Client hiding (Signal)
import Data.Typeable
import Data.List.Split
import Manatee.Core.Config
import Manatee.Core.Types
import Manatee.Plugin.Anything.Anything
import Manatee.Plugin.Anything.Types
import Network.URI
import qualified Data.Map as M
data BrowseHistory =
BrowseHistory {browseHistoryUrl :: String
,browseHistoryTitle :: String}
deriving (Show, Read, Eq, Ord, Typeable)
instance AnythingCandidate BrowseHistory where
candidateCommandName = browseHistoryUrl
candidateFilterName a = browseHistoryUrl a ++ " " ++ browseHistoryTitle a
candidateCompletionName a i =
let url = browseHistoryUrl a
in if isURI i then url else last $ splitOn "://" url
candidateExpandName a _ = return $ browseHistoryUrl a
anythingBrowseHistory :: Anything
anythingBrowseHistory =
Anything {anythingColumnTitle = ["BrowseHistory", "Url"]
,anythingColumnFun = anythingBrowseHistoryColumnFun
,anythingSearch = AnythingSearch anythingBrowseHistorySearch
,anythingFilterRule = anythingFuzzyFilterRule False
,anythingCompletionRule = id
,anythingInputDepend = False
,anythingCommandFun = anythingBrowseHistoryCommandFun
,anythingCalculateDelay = 0
}
anythingBrowseHistorySearch :: AnythingInput -> Client -> IO [BrowseHistory]
anythingBrowseHistorySearch _ _ = do
(BrowseHistoryList history) <- readConfig browseHistoryPath (BrowseHistoryList M.empty)
return $ map (uncurry BrowseHistory) (M.toList history)
anythingBrowseHistoryColumnFun :: [AnythingColumnFun]
anythingBrowseHistoryColumnFun =
[browseHistoryTitle . anythingCandidateUnpack
,browseHistoryUrl . anythingCandidateUnpack]
anythingBrowseHistoryCommandFun :: AnythingCommandFun
anythingBrowseHistoryCommandFun _ (AnythingCandidateWrap a) _ = do
let uri = candidateCommandName a
return [("Open URI", anythingOpenUri uri)
,("Remove from history", anythingBrowseHistoryRemove uri)
,("Clean History", anythingBrowseHistoryClean)]
anythingBrowseHistoryRemove :: String -> Client -> IO ()
anythingBrowseHistoryRemove uri _ = do
(BrowseHistoryList history) <- readConfig browseHistoryPath (BrowseHistoryList M.empty)
writeConfig browseHistoryPath (BrowseHistoryList (M.delete uri history))
anythingBrowseHistoryClean :: Client -> IO ()
anythingBrowseHistoryClean _ =
writeConfig browseHistoryPath (BrowseHistoryList M.empty)