module Manatee.Plugin.Anything.AnythingGoogleSuggest where
import DBus.Client hiding (Signal)
import Data.Typeable
import Google.Suggest
import Manatee.Plugin.Anything.Anything
import Manatee.Plugin.Anything.Types
import Manatee.Toolkit.General.Misc
import Network.URI
data Suggestion =
Suggestion {suggestionName :: String
,suggestionNumber:: Int
} deriving (Show, Ord, Eq, Typeable)
instance AnythingCandidate Suggestion where
candidateCommandName = suggestionName
candidateFilterName a = suggestionName a ++ " " ++ show (suggestionNumber a)
candidateCompletionName a _ = suggestionName a
candidateExpandName a _ = return $ suggestionName a
anythingGoogleSuggest :: Anything
anythingGoogleSuggest =
Anything {anythingColumnTitle = ["GoogleSuggest", "QueryNumber"]
,anythingColumnFun = anythingGoogleSuggestColumnFun
,anythingSearch = AnythingSearch anythingGoogleSuggestSearch
,anythingFilterRule = anythingFuzzyFilterRule True
,anythingCompletionRule = id
,anythingInputDepend = True
,anythingCommandFun = anythingGoogleSuggestCommandFun
,anythingCalculateDelay = 500
}
anythingGoogleSuggestSearch :: AnythingInput -> Client -> IO [Suggestion]
anythingGoogleSuggestSearch input _ = do
result <- suggest input Nothing
return $ case result of
Right list -> map (uncurry Suggestion) list
Left _ -> []
anythingGoogleSuggestColumnFun :: [AnythingColumnFun]
anythingGoogleSuggestColumnFun =
[suggestionName . anythingCandidateUnpack
,splitInt . suggestionNumber . anythingCandidateUnpack]
anythingGoogleSuggestCommandFun :: AnythingCommandFun
anythingGoogleSuggestCommandFun _ (AnythingCandidateWrap a) _
| isURI candidate
= return $ [("Open URI", anythingOpenUri candidate)] ++ (anythingSearchCommands candidate)
| isURI ("http://" ++ candidate)
= return $ [("Open URI", anythingOpenUri ("http://" ++ candidate))] ++ (anythingSearchCommands candidate)
| otherwise
= return (anythingSearchCommands candidate)
where candidate = candidateCommandName a