{- |
Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
License    : LGPL-2.1
Maintainer : Iñaki García Etxebarria (garetxe@gmail.com)
-}

module GI.Soup.Objects.AuthManager
    ( 

-- * Exported types
    AuthManager(..)                         ,
    IsAuthManager                           ,
    toAuthManager                           ,
    noAuthManager                           ,


 -- * Methods
-- ** useAuth #method:useAuth#
    AuthManagerUseAuthMethodInfo            ,
    authManagerUseAuth                      ,




 -- * Signals
-- ** authenticate #signal:authenticate#
    AuthManagerAuthenticateCallback         ,
    AuthManagerAuthenticateSignalInfo       ,
    C_AuthManagerAuthenticateCallback       ,
    afterAuthManagerAuthenticate            ,
    genClosure_AuthManagerAuthenticate      ,
    mk_AuthManagerAuthenticateCallback      ,
    noAuthManagerAuthenticateCallback       ,
    onAuthManagerAuthenticate               ,
    wrap_AuthManagerAuthenticateCallback    ,




    ) where

import Data.GI.Base.ShortPrelude
import qualified Data.GI.Base.ShortPrelude as SP
import qualified Data.GI.Base.Overloading as O
import qualified Prelude as P

import qualified Data.GI.Base.Attributes as GI.Attributes
import qualified Data.GI.Base.ManagedPtr as B.ManagedPtr
import qualified Data.GI.Base.GError as B.GError
import qualified Data.GI.Base.GVariant as B.GVariant
import qualified Data.GI.Base.GParamSpec as B.GParamSpec
import qualified Data.GI.Base.CallStack as B.CallStack
import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map
import qualified Foreign.Ptr as FP

import qualified GI.GObject.Objects.Object as GObject.Object
import {-# SOURCE #-} qualified GI.Soup.Interfaces.SessionFeature as Soup.SessionFeature
import {-# SOURCE #-} qualified GI.Soup.Objects.Auth as Soup.Auth
import {-# SOURCE #-} qualified GI.Soup.Objects.Message as Soup.Message
import {-# SOURCE #-} qualified GI.Soup.Structs.URI as Soup.URI

newtype AuthManager = AuthManager (ManagedPtr AuthManager)
foreign import ccall "soup_auth_manager_get_type"
    c_soup_auth_manager_get_type :: IO GType

instance GObject AuthManager where
    gobjectType _ = c_soup_auth_manager_get_type
    

class GObject o => IsAuthManager o
#if MIN_VERSION_base(4,9,0)
instance {-# OVERLAPPABLE #-} (GObject a, O.UnknownAncestorError AuthManager a) =>
    IsAuthManager a
#endif
instance IsAuthManager AuthManager
instance GObject.Object.IsObject AuthManager
instance Soup.SessionFeature.IsSessionFeature AuthManager

toAuthManager :: IsAuthManager o => o -> IO AuthManager
toAuthManager = unsafeCastTo AuthManager

noAuthManager :: Maybe AuthManager
noAuthManager = Nothing

type family ResolveAuthManagerMethod (t :: Symbol) (o :: *) :: * where
    ResolveAuthManagerMethod "addFeature" o = Soup.SessionFeature.SessionFeatureAddFeatureMethodInfo
    ResolveAuthManagerMethod "attach" o = Soup.SessionFeature.SessionFeatureAttachMethodInfo
    ResolveAuthManagerMethod "bindProperty" o = GObject.Object.ObjectBindPropertyMethodInfo
    ResolveAuthManagerMethod "bindPropertyFull" o = GObject.Object.ObjectBindPropertyFullMethodInfo
    ResolveAuthManagerMethod "detach" o = Soup.SessionFeature.SessionFeatureDetachMethodInfo
    ResolveAuthManagerMethod "forceFloating" o = GObject.Object.ObjectForceFloatingMethodInfo
    ResolveAuthManagerMethod "freezeNotify" o = GObject.Object.ObjectFreezeNotifyMethodInfo
    ResolveAuthManagerMethod "hasFeature" o = Soup.SessionFeature.SessionFeatureHasFeatureMethodInfo
    ResolveAuthManagerMethod "isFloating" o = GObject.Object.ObjectIsFloatingMethodInfo
    ResolveAuthManagerMethod "notify" o = GObject.Object.ObjectNotifyMethodInfo
    ResolveAuthManagerMethod "notifyByPspec" o = GObject.Object.ObjectNotifyByPspecMethodInfo
    ResolveAuthManagerMethod "ref" o = GObject.Object.ObjectRefMethodInfo
    ResolveAuthManagerMethod "refSink" o = GObject.Object.ObjectRefSinkMethodInfo
    ResolveAuthManagerMethod "removeFeature" o = Soup.SessionFeature.SessionFeatureRemoveFeatureMethodInfo
    ResolveAuthManagerMethod "replaceData" o = GObject.Object.ObjectReplaceDataMethodInfo
    ResolveAuthManagerMethod "replaceQdata" o = GObject.Object.ObjectReplaceQdataMethodInfo
    ResolveAuthManagerMethod "runDispose" o = GObject.Object.ObjectRunDisposeMethodInfo
    ResolveAuthManagerMethod "stealData" o = GObject.Object.ObjectStealDataMethodInfo
    ResolveAuthManagerMethod "stealQdata" o = GObject.Object.ObjectStealQdataMethodInfo
    ResolveAuthManagerMethod "thawNotify" o = GObject.Object.ObjectThawNotifyMethodInfo
    ResolveAuthManagerMethod "unref" o = GObject.Object.ObjectUnrefMethodInfo
    ResolveAuthManagerMethod "useAuth" o = AuthManagerUseAuthMethodInfo
    ResolveAuthManagerMethod "watchClosure" o = GObject.Object.ObjectWatchClosureMethodInfo
    ResolveAuthManagerMethod "getData" o = GObject.Object.ObjectGetDataMethodInfo
    ResolveAuthManagerMethod "getProperty" o = GObject.Object.ObjectGetPropertyMethodInfo
    ResolveAuthManagerMethod "getQdata" o = GObject.Object.ObjectGetQdataMethodInfo
    ResolveAuthManagerMethod "setData" o = GObject.Object.ObjectSetDataMethodInfo
    ResolveAuthManagerMethod "setProperty" o = GObject.Object.ObjectSetPropertyMethodInfo
    ResolveAuthManagerMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolveAuthManagerMethod t AuthManager, O.MethodInfo info AuthManager p) => O.IsLabelProxy t (AuthManager -> p) where
    fromLabelProxy _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)

#if MIN_VERSION_base(4,9,0)
instance (info ~ ResolveAuthManagerMethod t AuthManager, O.MethodInfo info AuthManager p) => O.IsLabel t (AuthManager -> p) where
    fromLabel _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#endif

-- signal AuthManager::authenticate
type AuthManagerAuthenticateCallback =
    Soup.Message.Message ->
    Soup.Auth.Auth ->
    Bool ->
    IO ()

noAuthManagerAuthenticateCallback :: Maybe AuthManagerAuthenticateCallback
noAuthManagerAuthenticateCallback = Nothing

type C_AuthManagerAuthenticateCallback =
    Ptr () ->                               -- object
    Ptr Soup.Message.Message ->
    Ptr Soup.Auth.Auth ->
    CInt ->
    Ptr () ->                               -- user_data
    IO ()

foreign import ccall "wrapper"
    mk_AuthManagerAuthenticateCallback :: C_AuthManagerAuthenticateCallback -> IO (FunPtr C_AuthManagerAuthenticateCallback)

genClosure_AuthManagerAuthenticate :: AuthManagerAuthenticateCallback -> IO Closure
genClosure_AuthManagerAuthenticate cb = do
    let cb' = wrap_AuthManagerAuthenticateCallback cb
    mk_AuthManagerAuthenticateCallback cb' >>= newCClosure


wrap_AuthManagerAuthenticateCallback ::
    AuthManagerAuthenticateCallback ->
    Ptr () ->
    Ptr Soup.Message.Message ->
    Ptr Soup.Auth.Auth ->
    CInt ->
    Ptr () ->
    IO ()
wrap_AuthManagerAuthenticateCallback _cb _ msg auth retrying _ = do
    msg' <- (newObject Soup.Message.Message) msg
    auth' <- (newObject Soup.Auth.Auth) auth
    let retrying' = (/= 0) retrying
    _cb  msg' auth' retrying'


onAuthManagerAuthenticate :: (GObject a, MonadIO m) => a -> AuthManagerAuthenticateCallback -> m SignalHandlerId
onAuthManagerAuthenticate obj cb = liftIO $ connectAuthManagerAuthenticate obj cb SignalConnectBefore
afterAuthManagerAuthenticate :: (GObject a, MonadIO m) => a -> AuthManagerAuthenticateCallback -> m SignalHandlerId
afterAuthManagerAuthenticate obj cb = connectAuthManagerAuthenticate obj cb SignalConnectAfter

connectAuthManagerAuthenticate :: (GObject a, MonadIO m) =>
                                  a -> AuthManagerAuthenticateCallback -> SignalConnectMode -> m SignalHandlerId
connectAuthManagerAuthenticate obj cb after = liftIO $ do
    let cb' = wrap_AuthManagerAuthenticateCallback cb
    cb'' <- mk_AuthManagerAuthenticateCallback cb'
    connectSignalFunPtr obj "authenticate" cb'' after

instance O.HasAttributeList AuthManager
type instance O.AttributeList AuthManager = AuthManagerAttributeList
type AuthManagerAttributeList = ('[ ] :: [(Symbol, *)])

data AuthManagerAuthenticateSignalInfo
instance SignalInfo AuthManagerAuthenticateSignalInfo where
    type HaskellCallbackType AuthManagerAuthenticateSignalInfo = AuthManagerAuthenticateCallback
    connectSignal _ = connectAuthManagerAuthenticate

type instance O.SignalList AuthManager = AuthManagerSignalList
type AuthManagerSignalList = ('[ '("authenticate", AuthManagerAuthenticateSignalInfo), '("notify", GObject.Object.ObjectNotifySignalInfo)] :: [(Symbol, *)])

-- method AuthManager::use_auth
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "manager", argType = TInterface (Name {namespace = "Soup", name = "AuthManager"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #SoupAuthManager", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "uri", argType = TInterface (Name {namespace = "Soup", name = "URI"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the #SoupURI under which @auth is to be used", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "auth", argType = TInterface (Name {namespace = "Soup", name = "Auth"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the #SoupAuth to use", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "soup_auth_manager_use_auth" soup_auth_manager_use_auth :: 
    Ptr AuthManager ->                      -- manager : TInterface (Name {namespace = "Soup", name = "AuthManager"})
    Ptr Soup.URI.URI ->                     -- uri : TInterface (Name {namespace = "Soup", name = "URI"})
    Ptr Soup.Auth.Auth ->                   -- auth : TInterface (Name {namespace = "Soup", name = "Auth"})
    IO ()

{- |
Records that /@auth@/ is to be used under /@uri@/, as though a
WWW-Authenticate header had been received at that URI. This can be
used to \"preload\" /@manager@/\'s auth cache, to avoid an extra HTTP
round trip in the case where you know ahead of time that a 401
response will be returned.

This is only useful for authentication types where the initial
Authorization header does not depend on any additional information
from the server. (Eg, Basic or NTLM, but not Digest.)

@since 2.42
-}
authManagerUseAuth ::
    (B.CallStack.HasCallStack, MonadIO m, IsAuthManager a, Soup.Auth.IsAuth b) =>
    a
    {- ^ /@manager@/: a 'GI.Soup.Objects.AuthManager.AuthManager' -}
    -> Soup.URI.URI
    {- ^ /@uri@/: the 'GI.Soup.Structs.URI.URI' under which /@auth@/ is to be used -}
    -> b
    {- ^ /@auth@/: the 'GI.Soup.Objects.Auth.Auth' to use -}
    -> m ()
authManagerUseAuth manager uri auth = liftIO $ do
    manager' <- unsafeManagedPtrCastPtr manager
    uri' <- unsafeManagedPtrGetPtr uri
    auth' <- unsafeManagedPtrCastPtr auth
    soup_auth_manager_use_auth manager' uri' auth'
    touchManagedPtr manager
    touchManagedPtr uri
    touchManagedPtr auth
    return ()

data AuthManagerUseAuthMethodInfo
instance (signature ~ (Soup.URI.URI -> b -> m ()), MonadIO m, IsAuthManager a, Soup.Auth.IsAuth b) => O.MethodInfo AuthManagerUseAuthMethodInfo a signature where
    overloadedMethod _ = authManagerUseAuth