{- |
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.WebKit2.Objects.FaviconDatabase
    ( 

-- * Exported types
    FaviconDatabase(..)                     ,
    FaviconDatabaseK                        ,
    toFaviconDatabase                       ,
    noFaviconDatabase                       ,


 -- * Methods
-- ** faviconDatabaseClear
    faviconDatabaseClear                    ,


-- ** faviconDatabaseGetFavicon
    faviconDatabaseGetFavicon               ,


-- ** faviconDatabaseGetFaviconFinish
    faviconDatabaseGetFaviconFinish         ,


-- ** faviconDatabaseGetFaviconUri
    faviconDatabaseGetFaviconUri            ,




 -- * Signals
-- ** FaviconChanged
    FaviconDatabaseFaviconChangedCallback   ,
    FaviconDatabaseFaviconChangedCallbackC  ,
    FaviconDatabaseFaviconChangedSignalInfo ,
    afterFaviconDatabaseFaviconChanged      ,
    faviconDatabaseFaviconChangedCallbackWrapper,
    faviconDatabaseFaviconChangedClosure    ,
    mkFaviconDatabaseFaviconChangedCallback ,
    noFaviconDatabaseFaviconChangedCallback ,
    onFaviconDatabaseFaviconChanged         ,




    ) 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.WebKit2.Types
import GI.WebKit2.Callbacks
import qualified GI.GObject as GObject
import qualified GI.Gio as Gio
import qualified GI.Cairo as Cairo

newtype FaviconDatabase = FaviconDatabase (ForeignPtr FaviconDatabase)
foreign import ccall "webkit_favicon_database_get_type"
    c_webkit_favicon_database_get_type :: IO GType

type instance ParentTypes FaviconDatabase = FaviconDatabaseParentTypes
type FaviconDatabaseParentTypes = '[GObject.Object]

instance GObject FaviconDatabase where
    gobjectIsInitiallyUnowned _ = False
    gobjectType _ = c_webkit_favicon_database_get_type
    

class GObject o => FaviconDatabaseK o
instance (GObject o, IsDescendantOf FaviconDatabase o) => FaviconDatabaseK o

toFaviconDatabase :: FaviconDatabaseK o => o -> IO FaviconDatabase
toFaviconDatabase = unsafeCastTo FaviconDatabase

noFaviconDatabase :: Maybe FaviconDatabase
noFaviconDatabase = Nothing

-- signal FaviconDatabase::favicon-changed
type FaviconDatabaseFaviconChangedCallback =
    T.Text ->
    T.Text ->
    IO ()

noFaviconDatabaseFaviconChangedCallback :: Maybe FaviconDatabaseFaviconChangedCallback
noFaviconDatabaseFaviconChangedCallback = Nothing

type FaviconDatabaseFaviconChangedCallbackC =
    Ptr () ->                               -- object
    CString ->
    CString ->
    Ptr () ->                               -- user_data
    IO ()

foreign import ccall "wrapper"
    mkFaviconDatabaseFaviconChangedCallback :: FaviconDatabaseFaviconChangedCallbackC -> IO (FunPtr FaviconDatabaseFaviconChangedCallbackC)

faviconDatabaseFaviconChangedClosure :: FaviconDatabaseFaviconChangedCallback -> IO Closure
faviconDatabaseFaviconChangedClosure cb = newCClosure =<< mkFaviconDatabaseFaviconChangedCallback wrapped
    where wrapped = faviconDatabaseFaviconChangedCallbackWrapper cb

faviconDatabaseFaviconChangedCallbackWrapper ::
    FaviconDatabaseFaviconChangedCallback ->
    Ptr () ->
    CString ->
    CString ->
    Ptr () ->
    IO ()
faviconDatabaseFaviconChangedCallbackWrapper _cb _ page_uri favicon_uri _ = do
    page_uri' <- cstringToText page_uri
    favicon_uri' <- cstringToText favicon_uri
    _cb  page_uri' favicon_uri'

onFaviconDatabaseFaviconChanged :: (GObject a, MonadIO m) => a -> FaviconDatabaseFaviconChangedCallback -> m SignalHandlerId
onFaviconDatabaseFaviconChanged obj cb = liftIO $ connectFaviconDatabaseFaviconChanged obj cb SignalConnectBefore
afterFaviconDatabaseFaviconChanged :: (GObject a, MonadIO m) => a -> FaviconDatabaseFaviconChangedCallback -> m SignalHandlerId
afterFaviconDatabaseFaviconChanged obj cb = connectFaviconDatabaseFaviconChanged obj cb SignalConnectAfter

connectFaviconDatabaseFaviconChanged :: (GObject a, MonadIO m) =>
                                        a -> FaviconDatabaseFaviconChangedCallback -> SignalConnectMode -> m SignalHandlerId
connectFaviconDatabaseFaviconChanged obj cb after = liftIO $ do
    cb' <- mkFaviconDatabaseFaviconChangedCallback (faviconDatabaseFaviconChangedCallbackWrapper cb)
    connectSignalFunPtr obj "favicon-changed" cb' after

type instance AttributeList FaviconDatabase = FaviconDatabaseAttributeList
type FaviconDatabaseAttributeList = ('[ ] :: [(Symbol, *)])

data FaviconDatabaseFaviconChangedSignalInfo
instance SignalInfo FaviconDatabaseFaviconChangedSignalInfo where
    type HaskellCallbackType FaviconDatabaseFaviconChangedSignalInfo = FaviconDatabaseFaviconChangedCallback
    connectSignal _ = connectFaviconDatabaseFaviconChanged

type instance SignalList FaviconDatabase = FaviconDatabaseSignalList
type FaviconDatabaseSignalList = ('[ '("favicon-changed", FaviconDatabaseFaviconChangedSignalInfo), '("notify", GObject.ObjectNotifySignalInfo), '("notify::[property]", GObjectNotifySignalInfo)] :: [(Symbol, *)])

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

foreign import ccall "webkit_favicon_database_clear" webkit_favicon_database_clear :: 
    Ptr FaviconDatabase ->                  -- _obj : TInterface "WebKit2" "FaviconDatabase"
    IO ()


faviconDatabaseClear ::
    (MonadIO m, FaviconDatabaseK a) =>
    a ->                                    -- _obj
    m ()
faviconDatabaseClear _obj = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    webkit_favicon_database_clear _obj'
    touchManagedPtr _obj
    return ()

-- method FaviconDatabase::get_favicon
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "WebKit2" "FaviconDatabase", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "page_uri", argType = TBasicType TUTF8, 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 = 4, 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 "WebKit2" "FaviconDatabase", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "page_uri", argType = TBasicType TUTF8, 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 = 4, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TVoid
-- throws : False
-- Skip return : False

foreign import ccall "webkit_favicon_database_get_favicon" webkit_favicon_database_get_favicon :: 
    Ptr FaviconDatabase ->                  -- _obj : TInterface "WebKit2" "FaviconDatabase"
    CString ->                              -- page_uri : TBasicType TUTF8
    Ptr Gio.Cancellable ->                  -- cancellable : TInterface "Gio" "Cancellable"
    FunPtr Gio.AsyncReadyCallbackC ->       -- callback : TInterface "Gio" "AsyncReadyCallback"
    Ptr () ->                               -- user_data : TBasicType TVoid
    IO ()


faviconDatabaseGetFavicon ::
    (MonadIO m, FaviconDatabaseK a, Gio.CancellableK b) =>
    a ->                                    -- _obj
    T.Text ->                               -- page_uri
    Maybe (b) ->                            -- cancellable
    Maybe (Gio.AsyncReadyCallback) ->       -- callback
    m ()
faviconDatabaseGetFavicon _obj page_uri cancellable callback = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    page_uri' <- textToCString page_uri
    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
    webkit_favicon_database_get_favicon _obj' page_uri' maybeCancellable maybeCallback user_data
    touchManagedPtr _obj
    whenJust cancellable touchManagedPtr
    freeMem page_uri'
    return ()

-- method FaviconDatabase::get_favicon_finish
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "WebKit2" "FaviconDatabase", 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 "WebKit2" "FaviconDatabase", 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 "cairo" "Surface"
-- throws : True
-- Skip return : False

foreign import ccall "webkit_favicon_database_get_favicon_finish" webkit_favicon_database_get_favicon_finish :: 
    Ptr FaviconDatabase ->                  -- _obj : TInterface "WebKit2" "FaviconDatabase"
    Ptr Gio.AsyncResult ->                  -- result : TInterface "Gio" "AsyncResult"
    Ptr (Ptr GError) ->                     -- error
    IO (Ptr Cairo.Surface)


faviconDatabaseGetFaviconFinish ::
    (MonadIO m, FaviconDatabaseK a, Gio.AsyncResultK b) =>
    a ->                                    -- _obj
    b ->                                    -- result
    m Cairo.Surface
faviconDatabaseGetFaviconFinish _obj result_ = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    let result_' = unsafeManagedPtrCastPtr result_
    onException (do
        result <- propagateGError $ webkit_favicon_database_get_favicon_finish _obj' result_'
        checkUnexpectedReturnNULL "webkit_favicon_database_get_favicon_finish" result
        result' <- (wrapBoxed Cairo.Surface) result
        touchManagedPtr _obj
        touchManagedPtr result_
        return result'
     ) (do
        return ()
     )

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

foreign import ccall "webkit_favicon_database_get_favicon_uri" webkit_favicon_database_get_favicon_uri :: 
    Ptr FaviconDatabase ->                  -- _obj : TInterface "WebKit2" "FaviconDatabase"
    CString ->                              -- page_uri : TBasicType TUTF8
    IO CString


faviconDatabaseGetFaviconUri ::
    (MonadIO m, FaviconDatabaseK a) =>
    a ->                                    -- _obj
    T.Text ->                               -- page_uri
    m T.Text
faviconDatabaseGetFaviconUri _obj page_uri = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    page_uri' <- textToCString page_uri
    result <- webkit_favicon_database_get_favicon_uri _obj' page_uri'
    checkUnexpectedReturnNULL "webkit_favicon_database_get_favicon_uri" result
    result' <- cstringToText result
    freeMem result
    touchManagedPtr _obj
    freeMem page_uri'
    return result'