{- |
Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
License    : LGPL-2.1
Maintainer : Iñaki García Etxebarria (inaki@blueleaf.cc)

/No description available in the introspection data./
-}

#define ENABLE_OVERLOADING (MIN_VERSION_haskell_gi_overloading(1,0,0) \
       && !defined(__HADDOCK_VERSION__))

module GI.Poppler.Objects.Media
    (

-- * Exported types
    Media(..)                               ,
    IsMedia                                 ,
    toMedia                                 ,
    noMedia                                 ,


 -- * Methods
-- ** getFilename #method:getFilename#

#if ENABLE_OVERLOADING
    MediaGetFilenameMethodInfo              ,
#endif
    mediaGetFilename                        ,


-- ** getMimeType #method:getMimeType#

#if ENABLE_OVERLOADING
    MediaGetMimeTypeMethodInfo              ,
#endif
    mediaGetMimeType                        ,


-- ** isEmbedded #method:isEmbedded#

#if ENABLE_OVERLOADING
    MediaIsEmbeddedMethodInfo               ,
#endif
    mediaIsEmbedded                         ,


-- ** save #method:save#

#if ENABLE_OVERLOADING
    MediaSaveMethodInfo                     ,
#endif
    mediaSave                               ,


-- ** saveToCallback #method:saveToCallback#

#if ENABLE_OVERLOADING
    MediaSaveToCallbackMethodInfo           ,
#endif
    mediaSaveToCallback                     ,




    ) 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.GClosure as B.GClosure
import qualified Data.GI.Base.GError as B.GError
import qualified Data.GI.Base.GVariant as B.GVariant
import qualified Data.GI.Base.GValue as B.GValue
import qualified Data.GI.Base.GParamSpec as B.GParamSpec
import qualified Data.GI.Base.CallStack as B.CallStack
import qualified Data.GI.Base.Properties as B.Properties
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 GHC.OverloadedLabels as OL

import qualified GI.GObject.Objects.Object as GObject.Object
import qualified GI.Poppler.Callbacks as Poppler.Callbacks

-- | Memory-managed wrapper type.
newtype Media = Media (ManagedPtr Media)
foreign import ccall "poppler_media_get_type"
    c_poppler_media_get_type :: IO GType

instance GObject Media where
    gobjectType = c_poppler_media_get_type


-- | Type class for types which can be safely cast to `Media`, for instance with `toMedia`.
class (GObject o, O.IsDescendantOf Media o) => IsMedia o
instance (GObject o, O.IsDescendantOf Media o) => IsMedia o

instance O.HasParentTypes Media
type instance O.ParentTypes Media = '[GObject.Object.Object]

-- | Cast to `Media`, for types for which this is known to be safe. For general casts, use `Data.GI.Base.ManagedPtr.castTo`.
toMedia :: (MonadIO m, IsMedia o) => o -> m Media
toMedia = liftIO . unsafeCastTo Media

-- | A convenience alias for `Nothing` :: `Maybe` `Media`.
noMedia :: Maybe Media
noMedia = Nothing

#if ENABLE_OVERLOADING
type family ResolveMediaMethod (t :: Symbol) (o :: *) :: * where
    ResolveMediaMethod "bindProperty" o = GObject.Object.ObjectBindPropertyMethodInfo
    ResolveMediaMethod "bindPropertyFull" o = GObject.Object.ObjectBindPropertyFullMethodInfo
    ResolveMediaMethod "forceFloating" o = GObject.Object.ObjectForceFloatingMethodInfo
    ResolveMediaMethod "freezeNotify" o = GObject.Object.ObjectFreezeNotifyMethodInfo
    ResolveMediaMethod "getv" o = GObject.Object.ObjectGetvMethodInfo
    ResolveMediaMethod "isEmbedded" o = MediaIsEmbeddedMethodInfo
    ResolveMediaMethod "isFloating" o = GObject.Object.ObjectIsFloatingMethodInfo
    ResolveMediaMethod "notify" o = GObject.Object.ObjectNotifyMethodInfo
    ResolveMediaMethod "notifyByPspec" o = GObject.Object.ObjectNotifyByPspecMethodInfo
    ResolveMediaMethod "ref" o = GObject.Object.ObjectRefMethodInfo
    ResolveMediaMethod "refSink" o = GObject.Object.ObjectRefSinkMethodInfo
    ResolveMediaMethod "runDispose" o = GObject.Object.ObjectRunDisposeMethodInfo
    ResolveMediaMethod "save" o = MediaSaveMethodInfo
    ResolveMediaMethod "saveToCallback" o = MediaSaveToCallbackMethodInfo
    ResolveMediaMethod "stealData" o = GObject.Object.ObjectStealDataMethodInfo
    ResolveMediaMethod "stealQdata" o = GObject.Object.ObjectStealQdataMethodInfo
    ResolveMediaMethod "thawNotify" o = GObject.Object.ObjectThawNotifyMethodInfo
    ResolveMediaMethod "unref" o = GObject.Object.ObjectUnrefMethodInfo
    ResolveMediaMethod "watchClosure" o = GObject.Object.ObjectWatchClosureMethodInfo
    ResolveMediaMethod "getData" o = GObject.Object.ObjectGetDataMethodInfo
    ResolveMediaMethod "getFilename" o = MediaGetFilenameMethodInfo
    ResolveMediaMethod "getMimeType" o = MediaGetMimeTypeMethodInfo
    ResolveMediaMethod "getProperty" o = GObject.Object.ObjectGetPropertyMethodInfo
    ResolveMediaMethod "getQdata" o = GObject.Object.ObjectGetQdataMethodInfo
    ResolveMediaMethod "setData" o = GObject.Object.ObjectSetDataMethodInfo
    ResolveMediaMethod "setDataFull" o = GObject.Object.ObjectSetDataFullMethodInfo
    ResolveMediaMethod "setProperty" o = GObject.Object.ObjectSetPropertyMethodInfo
    ResolveMediaMethod l o = O.MethodResolutionFailed l o

instance (info ~ ResolveMediaMethod t Media, O.MethodInfo info Media p) => OL.IsLabel t (Media -> p) where
#if MIN_VERSION_base(4,10,0)
    fromLabel = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#else
    fromLabel _ = O.overloadedMethod (O.MethodProxy :: O.MethodProxy info)
#endif

#endif

#if ENABLE_OVERLOADING
instance O.HasAttributeList Media
type instance O.AttributeList Media = MediaAttributeList
type MediaAttributeList = ('[ ] :: [(Symbol, *)])
#endif

#if ENABLE_OVERLOADING
#endif

#if ENABLE_OVERLOADING
type instance O.SignalList Media = MediaSignalList
type MediaSignalList = ('[ '("notify", GObject.Object.ObjectNotifySignalInfo)] :: [(Symbol, *)])

#endif

-- method Media::get_filename
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "poppler_media", argType = TInterface (Name {namespace = "Poppler", name = "Media"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #PopplerMedia", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TUTF8)
-- throws : False
-- Skip return : False

foreign import ccall "poppler_media_get_filename" poppler_media_get_filename ::
    Ptr Media ->                            -- poppler_media : TInterface (Name {namespace = "Poppler", name = "Media"})
    IO CString

{- |
Returns the media clip filename, in case of non-embedded media. filename might be
a local relative or absolute path or a URI

/Since: 0.14/
-}
mediaGetFilename ::
    (B.CallStack.HasCallStack, MonadIO m, IsMedia a) =>
    a
    {- ^ /@popplerMedia@/: a 'GI.Poppler.Objects.Media.Media' -}
    -> m T.Text
    {- ^ __Returns:__ a filename, return value is owned by 'GI.Poppler.Objects.Media.Media' and should not be freed -}
mediaGetFilename popplerMedia = liftIO $ do
    popplerMedia' <- unsafeManagedPtrCastPtr popplerMedia
    result <- poppler_media_get_filename popplerMedia'
    checkUnexpectedReturnNULL "mediaGetFilename" result
    result' <- cstringToText result
    touchManagedPtr popplerMedia
    return result'

#if ENABLE_OVERLOADING
data MediaGetFilenameMethodInfo
instance (signature ~ (m T.Text), MonadIO m, IsMedia a) => O.MethodInfo MediaGetFilenameMethodInfo a signature where
    overloadedMethod _ = mediaGetFilename

#endif

-- method Media::get_mime_type
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "poppler_media", argType = TInterface (Name {namespace = "Poppler", name = "Media"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #PopplerMedia", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TUTF8)
-- throws : False
-- Skip return : False

foreign import ccall "poppler_media_get_mime_type" poppler_media_get_mime_type ::
    Ptr Media ->                            -- poppler_media : TInterface (Name {namespace = "Poppler", name = "Media"})
    IO CString

{- |
Returns the media clip mime-type

/Since: 0.14/
-}
mediaGetMimeType ::
    (B.CallStack.HasCallStack, MonadIO m, IsMedia a) =>
    a
    {- ^ /@popplerMedia@/: a 'GI.Poppler.Objects.Media.Media' -}
    -> m T.Text
    {- ^ __Returns:__ the mime-type, return value is owned by 'GI.Poppler.Objects.Media.Media' and should not be freed -}
mediaGetMimeType popplerMedia = liftIO $ do
    popplerMedia' <- unsafeManagedPtrCastPtr popplerMedia
    result <- poppler_media_get_mime_type popplerMedia'
    checkUnexpectedReturnNULL "mediaGetMimeType" result
    result' <- cstringToText result
    touchManagedPtr popplerMedia
    return result'

#if ENABLE_OVERLOADING
data MediaGetMimeTypeMethodInfo
instance (signature ~ (m T.Text), MonadIO m, IsMedia a) => O.MethodInfo MediaGetMimeTypeMethodInfo a signature where
    overloadedMethod _ = mediaGetMimeType

#endif

-- method Media::is_embedded
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "poppler_media", argType = TInterface (Name {namespace = "Poppler", name = "Media"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #PopplerMedia", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TBoolean)
-- throws : False
-- Skip return : False

foreign import ccall "poppler_media_is_embedded" poppler_media_is_embedded ::
    Ptr Media ->                            -- poppler_media : TInterface (Name {namespace = "Poppler", name = "Media"})
    IO CInt

{- |
Whether the media clip is embedded in the PDF. If the result is 'True', the embedded stream
can be saved with 'GI.Poppler.Objects.Media.mediaSave' or 'GI.Poppler.Objects.Media.mediaSaveToCallback' function.
If the result is 'False', the media clip filename can be retrieved with
'GI.Poppler.Objects.Media.mediaGetFilename' function.

/Since: 0.14/
-}
mediaIsEmbedded ::
    (B.CallStack.HasCallStack, MonadIO m, IsMedia a) =>
    a
    {- ^ /@popplerMedia@/: a 'GI.Poppler.Objects.Media.Media' -}
    -> m Bool
    {- ^ __Returns:__ 'True' if media clip is embedded, 'False' otherwise -}
mediaIsEmbedded popplerMedia = liftIO $ do
    popplerMedia' <- unsafeManagedPtrCastPtr popplerMedia
    result <- poppler_media_is_embedded popplerMedia'
    let result' = (/= 0) result
    touchManagedPtr popplerMedia
    return result'

#if ENABLE_OVERLOADING
data MediaIsEmbeddedMethodInfo
instance (signature ~ (m Bool), MonadIO m, IsMedia a) => O.MethodInfo MediaIsEmbeddedMethodInfo a signature where
    overloadedMethod _ = mediaIsEmbedded

#endif

-- method Media::save
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "poppler_media", argType = TInterface (Name {namespace = "Poppler", name = "Media"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #PopplerMedia", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "filename", argType = TBasicType TUTF8, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "name of file to save", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TBoolean)
-- throws : True
-- Skip return : False

foreign import ccall "poppler_media_save" poppler_media_save ::
    Ptr Media ->                            -- poppler_media : TInterface (Name {namespace = "Poppler", name = "Media"})
    CString ->                              -- filename : TBasicType TUTF8
    Ptr (Ptr GError) ->                     -- error
    IO CInt

{- |
Saves embedded stream of /@popplerMedia@/ to a file indicated by /@filename@/.
If /@error@/ is set, 'False' will be returned.
Possible errors include those in the @/G_FILE_ERROR/@ domain
and whatever the save function generates.

/Since: 0.14/
-}
mediaSave ::
    (B.CallStack.HasCallStack, MonadIO m, IsMedia a) =>
    a
    {- ^ /@popplerMedia@/: a 'GI.Poppler.Objects.Media.Media' -}
    -> T.Text
    {- ^ /@filename@/: name of file to save -}
    -> m ()
    {- ^ /(Can throw 'Data.GI.Base.GError.GError')/ -}
mediaSave popplerMedia filename = liftIO $ do
    popplerMedia' <- unsafeManagedPtrCastPtr popplerMedia
    filename' <- textToCString filename
    onException (do
        _ <- propagateGError $ poppler_media_save popplerMedia' filename'
        touchManagedPtr popplerMedia
        freeMem filename'
        return ()
     ) (do
        freeMem filename'
     )

#if ENABLE_OVERLOADING
data MediaSaveMethodInfo
instance (signature ~ (T.Text -> m ()), MonadIO m, IsMedia a) => O.MethodInfo MediaSaveMethodInfo a signature where
    overloadedMethod _ = mediaSave

#endif

-- method Media::save_to_callback
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "poppler_media", argType = TInterface (Name {namespace = "Poppler", name = "Media"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a #PopplerMedia", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "save_func", argType = TInterface (Name {namespace = "Poppler", name = "MediaSaveFunc"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a function that is called to save each block of data that the save routine generates.", sinceVersion = Nothing}, argScope = ScopeTypeCall, argClosure = 2, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "user_data", argType = TBasicType TPtr, direction = DirectionIn, mayBeNull = True, argDoc = Documentation {rawDocText = Just "user data to pass to the save function.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : []
-- returnType : Just (TBasicType TBoolean)
-- throws : True
-- Skip return : False

foreign import ccall "poppler_media_save_to_callback" poppler_media_save_to_callback ::
    Ptr Media ->                            -- poppler_media : TInterface (Name {namespace = "Poppler", name = "Media"})
    FunPtr Poppler.Callbacks.C_MediaSaveFunc -> -- save_func : TInterface (Name {namespace = "Poppler", name = "MediaSaveFunc"})
    Ptr () ->                               -- user_data : TBasicType TPtr
    Ptr (Ptr GError) ->                     -- error
    IO CInt

{- |
Saves embedded stream of /@popplerMedia@/ by feeding the produced data to /@saveFunc@/. Can be used
when you want to store the media clip stream to something other than a file, such as
an in-memory buffer or a socket. If /@error@/ is set, 'False' will be
returned. Possible errors include those in the @/G_FILE_ERROR/@ domain and
whatever the save function generates.

/Since: 0.14/
-}
mediaSaveToCallback ::
    (B.CallStack.HasCallStack, MonadIO m, IsMedia a) =>
    a
    {- ^ /@popplerMedia@/: a 'GI.Poppler.Objects.Media.Media' -}
    -> FunPtr Poppler.Callbacks.C_MediaSaveFunc
    {- ^ /@saveFunc@/: a function that is called to save each block of data that the save routine generates. -}
    -> m ()
    {- ^ /(Can throw 'Data.GI.Base.GError.GError')/ -}
mediaSaveToCallback popplerMedia saveFunc = liftIO $ do
    popplerMedia' <- unsafeManagedPtrCastPtr popplerMedia
    let userData = nullPtr
    onException (do
        _ <- propagateGError $ poppler_media_save_to_callback popplerMedia' saveFunc userData
        safeFreeFunPtr $ castFunPtrToPtr saveFunc
        touchManagedPtr popplerMedia
        return ()
     ) (do
        safeFreeFunPtr $ castFunPtrToPtr saveFunc
        return ()
     )

#if ENABLE_OVERLOADING
data MediaSaveToCallbackMethodInfo
instance (signature ~ (FunPtr Poppler.Callbacks.C_MediaSaveFunc -> m ()), MonadIO m, IsMedia a) => O.MethodInfo MediaSaveToCallbackMethodInfo a signature where
    overloadedMethod _ = mediaSaveToCallback

#endif