{- |
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.Poppler.Objects.Media
    ( 

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


 -- * Methods
-- ** getFilename #method:getFilename#
#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
    MediaGetFilenameMethodInfo              ,
#endif
    mediaGetFilename                        ,


-- ** getMimeType #method:getMimeType#
#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
    MediaGetMimeTypeMethodInfo              ,
#endif
    mediaGetMimeType                        ,


-- ** isEmbedded #method:isEmbedded#
#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
    MediaIsEmbeddedMethodInfo               ,
#endif
    mediaIsEmbedded                         ,


-- ** save #method:save#
#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
    MediaSaveMethodInfo                     ,
#endif
    mediaSave                               ,


-- ** saveToCallback #method:saveToCallback#
#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
    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.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 qualified GI.Poppler.Callbacks as Poppler.Callbacks

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
    

class GObject o => IsMedia o
#if MIN_VERSION_base(4,9,0)
instance {-# OVERLAPPABLE #-} (GObject a, O.UnknownAncestorError Media a) =>
    IsMedia a
#endif
instance IsMedia Media
instance GObject.Object.IsObject Media

toMedia :: (MonadIO m, IsMedia o) => o -> m Media
toMedia = liftIO . unsafeCastTo Media

noMedia :: Maybe Media
noMedia = Nothing

#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
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 "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 "replaceData" o = GObject.Object.ObjectReplaceDataMethodInfo
    ResolveMediaMethod "replaceQdata" o = GObject.Object.ObjectReplaceQdataMethodInfo
    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 "setProperty" o = GObject.Object.ObjectSetPropertyMethodInfo
    ResolveMediaMethod l o = O.MethodResolutionFailed l o

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

#if MIN_VERSION_base(4,9,0)
instance (info ~ ResolveMediaMethod t Media, O.MethodInfo info Media p) => O.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

#endif

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

#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
#endif

#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
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 defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
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 defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
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 defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
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 defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
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 defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
data MediaSaveToCallbackMethodInfo
instance (signature ~ (FunPtr Poppler.Callbacks.C_MediaSaveFunc -> m ()), MonadIO m, IsMedia a) => O.MethodInfo MediaSaveToCallbackMethodInfo a signature where
    overloadedMethod _ = mediaSaveToCallback

#endif