{- |
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.Structs.UserStyleSheet
    ( 

-- * Exported types
    UserStyleSheet(..)                      ,
    noUserStyleSheet                        ,


 -- * Methods
-- ** new #method:new#
    userStyleSheetNew                       ,


-- ** ref #method:ref#
    UserStyleSheetRefMethodInfo             ,
    userStyleSheetRef                       ,


-- ** unref #method:unref#
    UserStyleSheetUnrefMethodInfo           ,
    userStyleSheetUnref                     ,




    ) 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 {-# SOURCE #-} qualified GI.WebKit2.Enums as WebKit2.Enums

newtype UserStyleSheet = UserStyleSheet (ManagedPtr UserStyleSheet)
foreign import ccall "webkit_user_style_sheet_get_type" c_webkit_user_style_sheet_get_type :: 
    IO GType

instance BoxedObject UserStyleSheet where
    boxedType _ = c_webkit_user_style_sheet_get_type

noUserStyleSheet :: Maybe UserStyleSheet
noUserStyleSheet = Nothing


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

-- method UserStyleSheet::new
-- method type : Constructor
-- Args : [Arg {argCName = "source", argType = TBasicType TUTF8, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "Source code of the user style sheet.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "injected_frames", argType = TInterface (Name {namespace = "WebKit2", name = "UserContentInjectedFrames"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "A #WebKitUserContentInjectedFrames value", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "level", argType = TInterface (Name {namespace = "WebKit2", name = "UserStyleLevel"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "A #WebKitUserStyleLevel", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "whitelist", argType = TCArray True (-1) (-1) (TBasicType TUTF8), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "A whitelist of URI patterns or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "blacklist", argType = TCArray True (-1) (-1) (TBasicType TUTF8), direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "A blacklist of URI patterns or %NULL", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "WebKit2", name = "UserStyleSheet"}))
-- throws : False
-- Skip return : False

foreign import ccall "webkit_user_style_sheet_new" webkit_user_style_sheet_new :: 
    CString ->                              -- source : TBasicType TUTF8
    CUInt ->                                -- injected_frames : TInterface (Name {namespace = "WebKit2", name = "UserContentInjectedFrames"})
    CUInt ->                                -- level : TInterface (Name {namespace = "WebKit2", name = "UserStyleLevel"})
    Ptr CString ->                          -- whitelist : TCArray True (-1) (-1) (TBasicType TUTF8)
    Ptr CString ->                          -- blacklist : TCArray True (-1) (-1) (TBasicType TUTF8)
    IO (Ptr UserStyleSheet)

{- |
Creates a new user style sheet. Style sheets can be applied to some URIs
only by passing non-null values for /@whitelist@/ or /@blacklist@/. Passing a
'Nothing' whitelist implies that all URIs are on the whitelist. The style
sheet is applied if an URI matches the whitelist and not the blacklist.
URI patterns must be of the form @[protocol]:\/\/[host]\/[path]@, where the
*host* and *path* components can contain the wildcard character (@*@) to
represent zero or more other characters.

@since 2.6
-}
userStyleSheetNew ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    T.Text
    {- ^ /@source@/: Source code of the user style sheet. -}
    -> WebKit2.Enums.UserContentInjectedFrames
    {- ^ /@injectedFrames@/: A 'GI.WebKit2.Enums.UserContentInjectedFrames' value -}
    -> WebKit2.Enums.UserStyleLevel
    {- ^ /@level@/: A 'GI.WebKit2.Enums.UserStyleLevel' -}
    -> Maybe ([T.Text])
    {- ^ /@whitelist@/: A whitelist of URI patterns or 'Nothing' -}
    -> Maybe ([T.Text])
    {- ^ /@blacklist@/: A blacklist of URI patterns or 'Nothing' -}
    -> m UserStyleSheet
    {- ^ __Returns:__ A new 'GI.WebKit2.Structs.UserStyleSheet.UserStyleSheet' -}
userStyleSheetNew source injectedFrames level whitelist blacklist = liftIO $ do
    source' <- textToCString source
    let injectedFrames' = (fromIntegral . fromEnum) injectedFrames
    let level' = (fromIntegral . fromEnum) level
    maybeWhitelist <- case whitelist of
        Nothing -> return nullPtr
        Just jWhitelist -> do
            jWhitelist' <- packZeroTerminatedUTF8CArray jWhitelist
            return jWhitelist'
    maybeBlacklist <- case blacklist of
        Nothing -> return nullPtr
        Just jBlacklist -> do
            jBlacklist' <- packZeroTerminatedUTF8CArray jBlacklist
            return jBlacklist'
    result <- webkit_user_style_sheet_new source' injectedFrames' level' maybeWhitelist maybeBlacklist
    checkUnexpectedReturnNULL "userStyleSheetNew" result
    result' <- (wrapBoxed UserStyleSheet) result
    freeMem source'
    mapZeroTerminatedCArray freeMem maybeWhitelist
    freeMem maybeWhitelist
    mapZeroTerminatedCArray freeMem maybeBlacklist
    freeMem maybeBlacklist
    return result'

-- method UserStyleSheet::ref
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "user_style_sheet", argType = TInterface (Name {namespace = "WebKit2", name = "UserStyleSheet"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #WebKitUserStyleSheet", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "WebKit2", name = "UserStyleSheet"}))
-- throws : False
-- Skip return : False

foreign import ccall "webkit_user_style_sheet_ref" webkit_user_style_sheet_ref :: 
    Ptr UserStyleSheet ->                   -- user_style_sheet : TInterface (Name {namespace = "WebKit2", name = "UserStyleSheet"})
    IO (Ptr UserStyleSheet)

{- |
Atomically increments the reference count of /@userStyleSheet@/ by one.
This function is MT-safe and may be called from any thread.

@since 2.6
-}
userStyleSheetRef ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    UserStyleSheet
    {- ^ /@userStyleSheet@/: a 'GI.WebKit2.Structs.UserStyleSheet.UserStyleSheet' -}
    -> m UserStyleSheet
    {- ^ __Returns:__ The passed 'GI.WebKit2.Structs.UserStyleSheet.UserStyleSheet' -}
userStyleSheetRef userStyleSheet = liftIO $ do
    userStyleSheet' <- unsafeManagedPtrGetPtr userStyleSheet
    result <- webkit_user_style_sheet_ref userStyleSheet'
    checkUnexpectedReturnNULL "userStyleSheetRef" result
    result' <- (wrapBoxed UserStyleSheet) result
    touchManagedPtr userStyleSheet
    return result'

data UserStyleSheetRefMethodInfo
instance (signature ~ (m UserStyleSheet), MonadIO m) => O.MethodInfo UserStyleSheetRefMethodInfo UserStyleSheet signature where
    overloadedMethod _ = userStyleSheetRef

-- method UserStyleSheet::unref
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "user_style_sheet", argType = TInterface (Name {namespace = "WebKit2", name = "UserStyleSheet"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #WebKitUserStyleSheet", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Nothing
-- throws : False
-- Skip return : False

foreign import ccall "webkit_user_style_sheet_unref" webkit_user_style_sheet_unref :: 
    Ptr UserStyleSheet ->                   -- user_style_sheet : TInterface (Name {namespace = "WebKit2", name = "UserStyleSheet"})
    IO ()

{- |
Atomically decrements the reference count of /@userStyleSheet@/ by one.
If the reference count drops to 0, all memory allocated by
'GI.WebKit2.Structs.UserStyleSheet.UserStyleSheet' is released. This function is MT-safe and may be
called from any thread.

@since 2.6
-}
userStyleSheetUnref ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    UserStyleSheet
    {- ^ /@userStyleSheet@/: a 'GI.WebKit2.Structs.UserStyleSheet.UserStyleSheet' -}
    -> m ()
userStyleSheetUnref userStyleSheet = liftIO $ do
    userStyleSheet' <- unsafeManagedPtrGetPtr userStyleSheet
    webkit_user_style_sheet_unref userStyleSheet'
    touchManagedPtr userStyleSheet
    return ()

data UserStyleSheetUnrefMethodInfo
instance (signature ~ (m ()), MonadIO m) => O.MethodInfo UserStyleSheetUnrefMethodInfo UserStyleSheet signature where
    overloadedMethod _ = userStyleSheetUnref

type family ResolveUserStyleSheetMethod (t :: Symbol) (o :: *) :: * where
    ResolveUserStyleSheetMethod "ref" o = UserStyleSheetRefMethodInfo
    ResolveUserStyleSheetMethod "unref" o = UserStyleSheetUnrefMethodInfo
    ResolveUserStyleSheetMethod l o = O.MethodResolutionFailed l o

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

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