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

-- * Exported types
    Attachment(..)                          ,
    IsAttachment                            ,
    toAttachment                            ,
    noAttachment                            ,


 -- * Methods
-- ** save #method:save#
#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
    AttachmentSaveMethodInfo                ,
#endif
    attachmentSave                          ,


-- ** saveToCallback #method:saveToCallback#
#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
    AttachmentSaveToCallbackMethodInfo      ,
#endif
    attachmentSaveToCallback                ,




    ) 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 Attachment = Attachment (ManagedPtr Attachment)
foreign import ccall "poppler_attachment_get_type"
    c_poppler_attachment_get_type :: IO GType

instance GObject Attachment where
    gobjectType _ = c_poppler_attachment_get_type
    

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

toAttachment :: (MonadIO m, IsAttachment o) => o -> m Attachment
toAttachment = liftIO . unsafeCastTo Attachment

noAttachment :: Maybe Attachment
noAttachment = Nothing

#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
type family ResolveAttachmentMethod (t :: Symbol) (o :: *) :: * where
    ResolveAttachmentMethod "bindProperty" o = GObject.Object.ObjectBindPropertyMethodInfo
    ResolveAttachmentMethod "bindPropertyFull" o = GObject.Object.ObjectBindPropertyFullMethodInfo
    ResolveAttachmentMethod "forceFloating" o = GObject.Object.ObjectForceFloatingMethodInfo
    ResolveAttachmentMethod "freezeNotify" o = GObject.Object.ObjectFreezeNotifyMethodInfo
    ResolveAttachmentMethod "isFloating" o = GObject.Object.ObjectIsFloatingMethodInfo
    ResolveAttachmentMethod "notify" o = GObject.Object.ObjectNotifyMethodInfo
    ResolveAttachmentMethod "notifyByPspec" o = GObject.Object.ObjectNotifyByPspecMethodInfo
    ResolveAttachmentMethod "ref" o = GObject.Object.ObjectRefMethodInfo
    ResolveAttachmentMethod "refSink" o = GObject.Object.ObjectRefSinkMethodInfo
    ResolveAttachmentMethod "replaceData" o = GObject.Object.ObjectReplaceDataMethodInfo
    ResolveAttachmentMethod "replaceQdata" o = GObject.Object.ObjectReplaceQdataMethodInfo
    ResolveAttachmentMethod "runDispose" o = GObject.Object.ObjectRunDisposeMethodInfo
    ResolveAttachmentMethod "save" o = AttachmentSaveMethodInfo
    ResolveAttachmentMethod "saveToCallback" o = AttachmentSaveToCallbackMethodInfo
    ResolveAttachmentMethod "stealData" o = GObject.Object.ObjectStealDataMethodInfo
    ResolveAttachmentMethod "stealQdata" o = GObject.Object.ObjectStealQdataMethodInfo
    ResolveAttachmentMethod "thawNotify" o = GObject.Object.ObjectThawNotifyMethodInfo
    ResolveAttachmentMethod "unref" o = GObject.Object.ObjectUnrefMethodInfo
    ResolveAttachmentMethod "watchClosure" o = GObject.Object.ObjectWatchClosureMethodInfo
    ResolveAttachmentMethod "getData" o = GObject.Object.ObjectGetDataMethodInfo
    ResolveAttachmentMethod "getProperty" o = GObject.Object.ObjectGetPropertyMethodInfo
    ResolveAttachmentMethod "getQdata" o = GObject.Object.ObjectGetQdataMethodInfo
    ResolveAttachmentMethod "setData" o = GObject.Object.ObjectSetDataMethodInfo
    ResolveAttachmentMethod "setProperty" o = GObject.Object.ObjectSetPropertyMethodInfo
    ResolveAttachmentMethod l o = O.MethodResolutionFailed l o

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

#if MIN_VERSION_base(4,9,0)
instance (info ~ ResolveAttachmentMethod t Attachment, O.MethodInfo info Attachment p) => O.IsLabel t (Attachment -> 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 Attachment
type instance O.AttributeList Attachment = AttachmentAttributeList
type AttachmentAttributeList = ('[ ] :: [(Symbol, *)])
#endif

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

#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
type instance O.SignalList Attachment = AttachmentSignalList
type AttachmentSignalList = ('[ '("notify", GObject.Object.ObjectNotifySignalInfo)] :: [(Symbol, *)])

#endif

-- method Attachment::save
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "attachment", argType = TInterface (Name {namespace = "Poppler", name = "Attachment"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "A #PopplerAttachment.", 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_attachment_save" poppler_attachment_save :: 
    Ptr Attachment ->                       -- attachment : TInterface (Name {namespace = "Poppler", name = "Attachment"})
    CString ->                              -- filename : TBasicType TUTF8
    Ptr (Ptr GError) ->                     -- error
    IO CInt

{- |
Saves /@attachment@/ 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.
-}
attachmentSave ::
    (B.CallStack.HasCallStack, MonadIO m, IsAttachment a) =>
    a
    {- ^ /@attachment@/: A 'GI.Poppler.Objects.Attachment.Attachment'. -}
    -> T.Text
    {- ^ /@filename@/: name of file to save -}
    -> m ()
    {- ^ /(Can throw 'Data.GI.Base.GError.GError')/ -}
attachmentSave attachment filename = liftIO $ do
    attachment' <- unsafeManagedPtrCastPtr attachment
    filename' <- textToCString filename
    onException (do
        _ <- propagateGError $ poppler_attachment_save attachment' filename'
        touchManagedPtr attachment
        freeMem filename'
        return ()
     ) (do
        freeMem filename'
     )

#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
data AttachmentSaveMethodInfo
instance (signature ~ (T.Text -> m ()), MonadIO m, IsAttachment a) => O.MethodInfo AttachmentSaveMethodInfo a signature where
    overloadedMethod _ = attachmentSave

#endif

-- method Attachment::save_to_callback
-- method type : OrdinaryMethod
-- Args : [Arg {argCName = "attachment", argType = TInterface (Name {namespace = "Poppler", name = "Attachment"}), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "A #PopplerAttachment.", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "save_func", argType = TInterface (Name {namespace = "Poppler", name = "AttachmentSaveFunc"}), 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_attachment_save_to_callback" poppler_attachment_save_to_callback :: 
    Ptr Attachment ->                       -- attachment : TInterface (Name {namespace = "Poppler", name = "Attachment"})
    FunPtr Poppler.Callbacks.C_AttachmentSaveFunc -> -- save_func : TInterface (Name {namespace = "Poppler", name = "AttachmentSaveFunc"})
    Ptr () ->                               -- user_data : TBasicType TPtr
    Ptr (Ptr GError) ->                     -- error
    IO CInt

{- |
Saves /@attachment@/ by feeding the produced data to /@saveFunc@/. Can be used
when you want to store the attachment 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.
-}
attachmentSaveToCallback ::
    (B.CallStack.HasCallStack, MonadIO m, IsAttachment a) =>
    a
    {- ^ /@attachment@/: A 'GI.Poppler.Objects.Attachment.Attachment'. -}
    -> FunPtr Poppler.Callbacks.C_AttachmentSaveFunc
    {- ^ /@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')/ -}
attachmentSaveToCallback attachment saveFunc = liftIO $ do
    attachment' <- unsafeManagedPtrCastPtr attachment
    let userData = nullPtr
    onException (do
        _ <- propagateGError $ poppler_attachment_save_to_callback attachment' saveFunc userData
        safeFreeFunPtr $ castFunPtrToPtr saveFunc
        touchManagedPtr attachment
        return ()
     ) (do
        safeFreeFunPtr $ castFunPtrToPtr saveFunc
        return ()
     )

#if defined(ENABLE_OVERLOADING) && !defined(__HADDOCK_VERSION__)
data AttachmentSaveToCallbackMethodInfo
instance (signature ~ (FunPtr Poppler.Callbacks.C_AttachmentSaveFunc -> m ()), MonadIO m, IsAttachment a) => O.MethodInfo AttachmentSaveToCallbackMethodInfo a signature where
    overloadedMethod _ = attachmentSaveToCallback

#endif