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

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

module GI.Poppler.Functions
    (

 -- * Methods
-- ** dateParse #method:dateParse#

    dateParse                               ,


-- ** getBackend #method:getBackend#

    getBackend                              ,


-- ** getVersion #method:getVersion#

    getVersion                              ,


-- ** namedDestFromBytestring #method:namedDestFromBytestring#

    namedDestFromBytestring                 ,


-- ** namedDestToBytestring #method:namedDestToBytestring#

    namedDestToBytestring                   ,




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

-- function poppler_named_dest_to_bytestring
-- Args : [Arg {argCName = "named_dest", argType = TBasicType TUTF8, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Nothing, sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "length", argType = TBasicType TUInt64, direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a location to store the length of the returned bytestring", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- Lengths : [Arg {argCName = "length", argType = TBasicType TUInt64, direction = DirectionOut, mayBeNull = False, argDoc = Documentation {rawDocText = Just "a location to store the length of the returned bytestring", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferEverything}]
-- returnType : Just (TCArray False (-1) 1 (TBasicType TUInt8))
-- throws : False
-- Skip return : False

foreign import ccall "poppler_named_dest_to_bytestring" poppler_named_dest_to_bytestring ::
    CString ->                              -- named_dest : TBasicType TUTF8
    Ptr Word64 ->                           -- length : TBasicType TUInt64
    IO (Ptr Word8)

{- |
Converts a named dest string (e.g. from 'GI.Poppler.Structs.Dest.Dest'.@/named_dest/@) into a
bytestring, inverting the transformation of
'GI.Poppler.Functions.namedDestFromBytestring'.

Note that the returned data is not zero terminated and may also
contains embedded NUL bytes.

If /@name@/ is not a valid named dest string, returns 'Nothing'.

The returned data must be freed using 'GI.GLib.Functions.free'.

/Since: 0.73/
-}
namedDestToBytestring ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    T.Text
    -> m (Maybe ByteString)
    {- ^ __Returns:__ a new bytestring,
  or 'Nothing' -}
namedDestToBytestring namedDest = liftIO $ do
    namedDest' <- textToCString namedDest
    length_ <- allocMem :: IO (Ptr Word64)
    result <- poppler_named_dest_to_bytestring namedDest' length_
    length_' <- peek length_
    maybeResult <- convertIfNonNull result $ \result' -> do
        result'' <- (unpackByteStringWithLength length_') result'
        freeMem result'
        return result''
    freeMem namedDest'
    freeMem length_
    return maybeResult


-- function poppler_named_dest_from_bytestring
-- Args : [Arg {argCName = "data", argType = TCArray False (-1) 1 (TBasicType TUInt8), direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the bytestring data", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "length", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the bytestring length", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- Lengths : [Arg {argCName = "length", argType = TBasicType TUInt64, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "the bytestring length", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing}]
-- returnType : Just (TBasicType TUTF8)
-- throws : False
-- Skip return : False

foreign import ccall "poppler_named_dest_from_bytestring" poppler_named_dest_from_bytestring ::
    Ptr Word8 ->                            -- data : TCArray False (-1) 1 (TBasicType TUInt8)
    Word64 ->                               -- length : TBasicType TUInt64
    IO CString

{- |
Converts a bytestring into a zero-terminated string suitable to
pass to 'GI.Poppler.Objects.Document.documentFindDest'.

Note that the returned string has no defined encoding and is not
suitable for display to the user.

The returned data must be freed using 'GI.GLib.Functions.free'.

/Since: 0.73/
-}
namedDestFromBytestring ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    ByteString
    {- ^ /@data@/: the bytestring data -}
    -> m T.Text
    {- ^ __Returns:__ the named dest -}
namedDestFromBytestring data_ = liftIO $ do
    let length_ = fromIntegral $ B.length data_
    data_' <- packByteString data_
    result <- poppler_named_dest_from_bytestring data_' length_
    checkUnexpectedReturnNULL "namedDestFromBytestring" result
    result' <- cstringToText result
    freeMem result
    freeMem data_'
    return result'


-- function poppler_get_version
-- Args : []
-- Lengths : []
-- returnType : Just (TBasicType TUTF8)
-- throws : False
-- Skip return : False

foreign import ccall "poppler_get_version" poppler_get_version ::
    IO CString

{- |
Returns the version of poppler in use.  This result is not to be freed.
-}
getVersion ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    m T.Text
    {- ^ __Returns:__ the version of poppler. -}
getVersion  = liftIO $ do
    result <- poppler_get_version
    checkUnexpectedReturnNULL "getVersion" result
    result' <- cstringToText result
    return result'


-- function poppler_get_backend
-- Args : []
-- Lengths : []
-- returnType : Just (TInterface (Name {namespace = "Poppler", name = "Backend"}))
-- throws : False
-- Skip return : False

foreign import ccall "poppler_get_backend" poppler_get_backend ::
    IO CUInt

{- |
Returns the backend compiled into the poppler library.
-}
getBackend ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    m Poppler.Enums.Backend
    {- ^ __Returns:__ The backend used by poppler -}
getBackend  = liftIO $ do
    result <- poppler_get_backend
    let result' = (toEnum . fromIntegral) result
    return result'


-- function poppler_date_parse
-- Args : [Arg {argCName = "date", argType = TBasicType TUTF8, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "string to parse", sinceVersion = Nothing}, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, argCallerAllocates = False, transfer = TransferNothing},Arg {argCName = "timet", argType = TBasicType TLong, direction = DirectionIn, mayBeNull = False, argDoc = Documentation {rawDocText = Just "an uninitialized #time_t", 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_date_parse" poppler_date_parse ::
    CString ->                              -- date : TBasicType TUTF8
    CLong ->                                -- timet : TBasicType TLong
    IO CInt

{- |
Parses a PDF format date string and converts it to a @/time_t/@. Returns @/FALSE/@
if the parsing fails or the input string is not a valid PDF format date string

/Since: 0.12/
-}
dateParse ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    T.Text
    {- ^ /@date@/: string to parse -}
    -> CLong
    {- ^ /@timet@/: an uninitialized @/time_t/@ -}
    -> m Bool
    {- ^ __Returns:__ @/TRUE/@, if /@timet@/ was set -}
dateParse date timet = liftIO $ do
    date' <- textToCString date
    result <- poppler_date_parse date' timet
    let result' = (/= 0) result
    freeMem date'
    return result'