module Crypto.Gpgme.Ctx where
import Bindings.Gpgme
import Foreign
import Foreign.C.String
import Foreign.C.Types
import Crypto.Gpgme.Types
import Crypto.Gpgme.Internal
newCtx :: String
-> String
-> Protocol
-> IO Ctx
newCtx homedir localeStr protocol =
do homedirPtr <- newCString homedir
version <- c'gpgme_check_version nullPtr >>= peekCString
ctxPtr <- malloc
checkError "gpgme_new" =<< c'gpgme_new ctxPtr
ctx <- peek ctxPtr
locale <- newCString localeStr
checkError "set_locale" =<< c'gpgme_set_locale ctx lcCtype locale
checkError "set_protocol" =<< c'gpgme_set_protocol ctx
(fromProtocol protocol)
checkError "set_engine_info" =<< c'gpgme_ctx_set_engine_info ctx
(fromProtocol protocol) nullPtr homedirPtr
return (Ctx ctxPtr version)
where lcCtype :: CInt
lcCtype = 0
freeCtx :: Ctx -> IO ()
freeCtx (Ctx ctxPtr _) =
do ctx <- peek ctxPtr
c'gpgme_release ctx
free ctxPtr
withCtx :: String
-> String
-> Protocol
-> (Ctx -> IO a)
-> IO a
withCtx homedir localeStr prot f = do
ctx <- newCtx homedir localeStr prot
res <- f ctx
freeCtx ctx
return res
withArmor :: (Ctx -> IO a) -> Ctx -> IO a
withArmor f ctx = do
cctx <- peek $ _ctx ctx
c'gpgme_set_armor cctx 1
f ctx