{- |
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.Request
    ( 

-- * Exported types
    Request(..)                             ,
    RequestK                                ,
    toRequest                               ,
    noRequest                               ,


 -- * Methods
-- ** requestGetContentLength
    requestGetContentLength                 ,


-- ** requestGetContentType
    requestGetContentType                   ,


-- ** requestGetSession
    requestGetSession                       ,


-- ** requestGetUri
    requestGetUri                           ,


-- ** requestSend
    requestSend                             ,


-- ** requestSendAsync
    requestSendAsync                        ,


-- ** requestSendFinish
    requestSendFinish                       ,




 -- * Properties
-- ** Session
    RequestSessionPropertyInfo              ,
    constructRequestSession                 ,
    getRequestSession                       ,


-- ** Uri
    RequestUriPropertyInfo                  ,
    constructRequestUri                     ,
    getRequestUri                           ,




    ) where

import Prelude ()
import Data.GI.Base.ShortPrelude

import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map

import GI.Soup.Types
import GI.Soup.Callbacks
import qualified GI.GObject as GObject
import qualified GI.Gio as Gio

newtype Request = Request (ForeignPtr Request)
foreign import ccall "soup_request_get_type"
    c_soup_request_get_type :: IO GType

type instance ParentTypes Request = RequestParentTypes
type RequestParentTypes = '[GObject.Object, Gio.Initable]

instance GObject Request where
    gobjectIsInitiallyUnowned _ = False
    gobjectType _ = c_soup_request_get_type
    

class GObject o => RequestK o
instance (GObject o, IsDescendantOf Request o) => RequestK o

toRequest :: RequestK o => o -> IO Request
toRequest = unsafeCastTo Request

noRequest :: Maybe Request
noRequest = Nothing

-- VVV Prop "session"
   -- Type: TInterface "Soup" "Session"
   -- Flags: [PropertyReadable,PropertyWritable,PropertyConstructOnly]

getRequestSession :: (MonadIO m, RequestK o) => o -> m Session
getRequestSession obj = liftIO $ getObjectPropertyObject obj "session" Session

constructRequestSession :: (SessionK a) => a -> IO ([Char], GValue)
constructRequestSession val = constructObjectPropertyObject "session" val

data RequestSessionPropertyInfo
instance AttrInfo RequestSessionPropertyInfo where
    type AttrAllowedOps RequestSessionPropertyInfo = '[ 'AttrConstruct, 'AttrGet]
    type AttrSetTypeConstraint RequestSessionPropertyInfo = SessionK
    type AttrBaseTypeConstraint RequestSessionPropertyInfo = RequestK
    type AttrGetType RequestSessionPropertyInfo = Session
    type AttrLabel RequestSessionPropertyInfo = "Request::session"
    attrGet _ = getRequestSession
    attrSet _ = undefined
    attrConstruct _ = constructRequestSession

-- VVV Prop "uri"
   -- Type: TInterface "Soup" "URI"
   -- Flags: [PropertyReadable,PropertyWritable,PropertyConstructOnly]

getRequestUri :: (MonadIO m, RequestK o) => o -> m URI
getRequestUri obj = liftIO $ getObjectPropertyBoxed obj "uri" URI

constructRequestUri :: URI -> IO ([Char], GValue)
constructRequestUri val = constructObjectPropertyBoxed "uri" val

data RequestUriPropertyInfo
instance AttrInfo RequestUriPropertyInfo where
    type AttrAllowedOps RequestUriPropertyInfo = '[ 'AttrConstruct, 'AttrGet]
    type AttrSetTypeConstraint RequestUriPropertyInfo = (~) URI
    type AttrBaseTypeConstraint RequestUriPropertyInfo = RequestK
    type AttrGetType RequestUriPropertyInfo = URI
    type AttrLabel RequestUriPropertyInfo = "Request::uri"
    attrGet _ = getRequestUri
    attrSet _ = undefined
    attrConstruct _ = constructRequestUri

type instance AttributeList Request = RequestAttributeList
type RequestAttributeList = ('[ '("session", RequestSessionPropertyInfo), '("uri", RequestUriPropertyInfo)] :: [(Symbol, *)])

type instance SignalList Request = RequestSignalList
type RequestSignalList = ('[ '("notify", GObject.ObjectNotifySignalInfo), '("notify::[property]", GObjectNotifySignalInfo)] :: [(Symbol, *)])

-- method Request::get_content_length
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Soup" "Request", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Soup" "Request", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TInt64
-- throws : False
-- Skip return : False

foreign import ccall "soup_request_get_content_length" soup_request_get_content_length :: 
    Ptr Request ->                          -- _obj : TInterface "Soup" "Request"
    IO Int64


requestGetContentLength ::
    (MonadIO m, RequestK a) =>
    a ->                                    -- _obj
    m Int64
requestGetContentLength _obj = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    result <- soup_request_get_content_length _obj'
    touchManagedPtr _obj
    return result

-- method Request::get_content_type
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Soup" "Request", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Soup" "Request", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TUTF8
-- throws : False
-- Skip return : False

foreign import ccall "soup_request_get_content_type" soup_request_get_content_type :: 
    Ptr Request ->                          -- _obj : TInterface "Soup" "Request"
    IO CString


requestGetContentType ::
    (MonadIO m, RequestK a) =>
    a ->                                    -- _obj
    m T.Text
requestGetContentType _obj = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    result <- soup_request_get_content_type _obj'
    checkUnexpectedReturnNULL "soup_request_get_content_type" result
    result' <- cstringToText result
    touchManagedPtr _obj
    return result'

-- method Request::get_session
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Soup" "Request", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Soup" "Request", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TInterface "Soup" "Session"
-- throws : False
-- Skip return : False

foreign import ccall "soup_request_get_session" soup_request_get_session :: 
    Ptr Request ->                          -- _obj : TInterface "Soup" "Request"
    IO (Ptr Session)


requestGetSession ::
    (MonadIO m, RequestK a) =>
    a ->                                    -- _obj
    m Session
requestGetSession _obj = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    result <- soup_request_get_session _obj'
    checkUnexpectedReturnNULL "soup_request_get_session" result
    result' <- (newObject Session) result
    touchManagedPtr _obj
    return result'

-- method Request::get_uri
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Soup" "Request", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Soup" "Request", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TInterface "Soup" "URI"
-- throws : False
-- Skip return : False

foreign import ccall "soup_request_get_uri" soup_request_get_uri :: 
    Ptr Request ->                          -- _obj : TInterface "Soup" "Request"
    IO (Ptr URI)


requestGetUri ::
    (MonadIO m, RequestK a) =>
    a ->                                    -- _obj
    m URI
requestGetUri _obj = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    result <- soup_request_get_uri _obj'
    checkUnexpectedReturnNULL "soup_request_get_uri" result
    result' <- (newBoxed URI) result
    touchManagedPtr _obj
    return result'

-- method Request::send
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Soup" "Request", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "cancellable", argType = TInterface "Gio" "Cancellable", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Soup" "Request", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "cancellable", argType = TInterface "Gio" "Cancellable", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TInterface "Gio" "InputStream"
-- throws : True
-- Skip return : False

foreign import ccall "soup_request_send" soup_request_send :: 
    Ptr Request ->                          -- _obj : TInterface "Soup" "Request"
    Ptr Gio.Cancellable ->                  -- cancellable : TInterface "Gio" "Cancellable"
    Ptr (Ptr GError) ->                     -- error
    IO (Ptr Gio.InputStream)


requestSend ::
    (MonadIO m, RequestK a, Gio.CancellableK b) =>
    a ->                                    -- _obj
    Maybe (b) ->                            -- cancellable
    m Gio.InputStream
requestSend _obj cancellable = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    maybeCancellable <- case cancellable of
        Nothing -> return nullPtr
        Just jCancellable -> do
            let jCancellable' = unsafeManagedPtrCastPtr jCancellable
            return jCancellable'
    onException (do
        result <- propagateGError $ soup_request_send _obj' maybeCancellable
        checkUnexpectedReturnNULL "soup_request_send" result
        result' <- (wrapObject Gio.InputStream) result
        touchManagedPtr _obj
        whenJust cancellable touchManagedPtr
        return result'
     ) (do
        return ()
     )

-- method Request::send_async
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Soup" "Request", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "cancellable", argType = TInterface "Gio" "Cancellable", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "callback", argType = TInterface "Gio" "AsyncReadyCallback", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeAsync, argClosure = 3, argDestroy = -1, transfer = TransferNothing},Arg {argName = "user_data", argType = TBasicType TVoid, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Soup" "Request", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "cancellable", argType = TInterface "Gio" "Cancellable", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "callback", argType = TInterface "Gio" "AsyncReadyCallback", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeAsync, argClosure = 3, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TVoid
-- throws : False
-- Skip return : False

foreign import ccall "soup_request_send_async" soup_request_send_async :: 
    Ptr Request ->                          -- _obj : TInterface "Soup" "Request"
    Ptr Gio.Cancellable ->                  -- cancellable : TInterface "Gio" "Cancellable"
    FunPtr Gio.AsyncReadyCallbackC ->       -- callback : TInterface "Gio" "AsyncReadyCallback"
    Ptr () ->                               -- user_data : TBasicType TVoid
    IO ()


requestSendAsync ::
    (MonadIO m, RequestK a, Gio.CancellableK b) =>
    a ->                                    -- _obj
    Maybe (b) ->                            -- cancellable
    Maybe (Gio.AsyncReadyCallback) ->       -- callback
    m ()
requestSendAsync _obj cancellable callback = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    maybeCancellable <- case cancellable of
        Nothing -> return nullPtr
        Just jCancellable -> do
            let jCancellable' = unsafeManagedPtrCastPtr jCancellable
            return jCancellable'
    ptrcallback <- callocMem :: IO (Ptr (FunPtr Gio.AsyncReadyCallbackC))
    maybeCallback <- case callback of
        Nothing -> return (castPtrToFunPtr nullPtr)
        Just jCallback -> do
            jCallback' <- Gio.mkAsyncReadyCallback (Gio.asyncReadyCallbackWrapper (Just ptrcallback) jCallback)
            poke ptrcallback jCallback'
            return jCallback'
    let user_data = nullPtr
    soup_request_send_async _obj' maybeCancellable maybeCallback user_data
    touchManagedPtr _obj
    whenJust cancellable touchManagedPtr
    return ()

-- method Request::send_finish
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Soup" "Request", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "result", argType = TInterface "Gio" "AsyncResult", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Soup" "Request", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "result", argType = TInterface "Gio" "AsyncResult", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TInterface "Gio" "InputStream"
-- throws : True
-- Skip return : False

foreign import ccall "soup_request_send_finish" soup_request_send_finish :: 
    Ptr Request ->                          -- _obj : TInterface "Soup" "Request"
    Ptr Gio.AsyncResult ->                  -- result : TInterface "Gio" "AsyncResult"
    Ptr (Ptr GError) ->                     -- error
    IO (Ptr Gio.InputStream)


requestSendFinish ::
    (MonadIO m, RequestK a, Gio.AsyncResultK b) =>
    a ->                                    -- _obj
    b ->                                    -- result
    m Gio.InputStream
requestSendFinish _obj result_ = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    let result_' = unsafeManagedPtrCastPtr result_
    onException (do
        result <- propagateGError $ soup_request_send_finish _obj' result_'
        checkUnexpectedReturnNULL "soup_request_send_finish" result
        result' <- (wrapObject Gio.InputStream) result
        touchManagedPtr _obj
        touchManagedPtr result_
        return result'
     ) (do
        return ()
     )