{-# LANGUAGE TypeApplications #-}


-- | Copyright  : Will Thompson, Iñaki García Etxebarria and Jonas Platte
-- License    : LGPL-2.1
-- Maintainer : Iñaki García Etxebarria
-- 
-- This represents the configuration for a single remote repository. Currently,
-- remotes can only be passed around as (reference counted) opaque handles. In
-- future, more API may be added to create and interrogate them.
-- 
-- /Since: 2018.6/

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

module GI.OSTree.Structs.Remote
    (

-- * Exported types
    Remote(..)                              ,
    noRemote                                ,


 -- * Methods
-- ** Overloaded methods #method:Overloaded methods#

#if defined(ENABLE_OVERLOADING)
    ResolveRemoteMethod                     ,
#endif


-- ** getName #method:getName#

#if defined(ENABLE_OVERLOADING)
    RemoteGetNameMethodInfo                 ,
#endif
    remoteGetName                           ,


-- ** getUrl #method:getUrl#

#if defined(ENABLE_OVERLOADING)
    RemoteGetUrlMethodInfo                  ,
#endif
    remoteGetUrl                            ,


-- ** ref #method:ref#

#if defined(ENABLE_OVERLOADING)
    RemoteRefMethodInfo                     ,
#endif
    remoteRef                               ,


-- ** unref #method:unref#

#if defined(ENABLE_OVERLOADING)
    RemoteUnrefMethodInfo                   ,
#endif
    remoteUnref                             ,




    ) 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.GI.Base.Signals as B.Signals
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


-- | Memory-managed wrapper type.
newtype Remote = Remote (ManagedPtr Remote)
    deriving (Eq)
foreign import ccall "ostree_remote_get_type" c_ostree_remote_get_type ::
    IO GType

instance BoxedObject Remote where
    boxedType _ = c_ostree_remote_get_type

-- | Convert 'Remote' to and from 'Data.GI.Base.GValue.GValue' with 'Data.GI.Base.GValue.toGValue' and 'Data.GI.Base.GValue.fromGValue'.
instance B.GValue.IsGValue Remote where
    toGValue o = do
        gtype <- c_ostree_remote_get_type
        B.ManagedPtr.withManagedPtr o (B.GValue.buildGValue gtype B.GValue.set_boxed)

    fromGValue gv = do
        ptr <- B.GValue.get_boxed gv :: IO (Ptr Remote)
        B.ManagedPtr.newBoxed Remote ptr



-- | A convenience alias for `Nothing` :: `Maybe` `Remote`.
noRemote :: Maybe Remote
noRemote = Nothing


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

-- method Remote::get_name
-- method type : OrdinaryMethod
-- Args: [ Arg
--           { argCName = "remote"
--           , argType =
--               TInterface Name { namespace = "OSTree" , name = "Remote" }
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "an #OstreeRemote" , 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 "ostree_remote_get_name" ostree_remote_get_name ::
    Ptr Remote ->                           -- remote : TInterface (Name {namespace = "OSTree", name = "Remote"})
    IO CString

-- | Get the human-readable name of the remote. This is what the user configured,
-- if the remote was explicitly configured; and will otherwise be a stable,
-- arbitrary, string.
-- 
-- /Since: 2018.6/
remoteGetName ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Remote
    -- ^ /@remote@/: an t'GI.OSTree.Structs.Remote.Remote'
    -> m T.Text
    -- ^ __Returns:__ remote’s name
remoteGetName remote = liftIO $ do
    remote' <- unsafeManagedPtrGetPtr remote
    result <- ostree_remote_get_name remote'
    checkUnexpectedReturnNULL "remoteGetName" result
    result' <- cstringToText result
    touchManagedPtr remote
    return result'

#if defined(ENABLE_OVERLOADING)
data RemoteGetNameMethodInfo
instance (signature ~ (m T.Text), MonadIO m) => O.MethodInfo RemoteGetNameMethodInfo Remote signature where
    overloadedMethod = remoteGetName

#endif

-- method Remote::get_url
-- method type : OrdinaryMethod
-- Args: [ Arg
--           { argCName = "remote"
--           , argType =
--               TInterface Name { namespace = "OSTree" , name = "Remote" }
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "an #OstreeRemote" , 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 "ostree_remote_get_url" ostree_remote_get_url ::
    Ptr Remote ->                           -- remote : TInterface (Name {namespace = "OSTree", name = "Remote"})
    IO CString

-- | Get the URL from the remote.
-- 
-- /Since: 2018.6/
remoteGetUrl ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Remote
    -- ^ /@remote@/: an t'GI.OSTree.Structs.Remote.Remote'
    -> m T.Text
    -- ^ __Returns:__ the remote\'s URL
remoteGetUrl remote = liftIO $ do
    remote' <- unsafeManagedPtrGetPtr remote
    result <- ostree_remote_get_url remote'
    checkUnexpectedReturnNULL "remoteGetUrl" result
    result' <- cstringToText result
    freeMem result
    touchManagedPtr remote
    return result'

#if defined(ENABLE_OVERLOADING)
data RemoteGetUrlMethodInfo
instance (signature ~ (m T.Text), MonadIO m) => O.MethodInfo RemoteGetUrlMethodInfo Remote signature where
    overloadedMethod = remoteGetUrl

#endif

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

foreign import ccall "ostree_remote_ref" ostree_remote_ref ::
    Ptr Remote ->                           -- remote : TInterface (Name {namespace = "OSTree", name = "Remote"})
    IO (Ptr Remote)

-- | Increase the reference count on the given /@remote@/.
-- 
-- /Since: 2018.6/
remoteRef ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Remote
    -- ^ /@remote@/: an t'GI.OSTree.Structs.Remote.Remote'
    -> m Remote
    -- ^ __Returns:__ a copy of /@remote@/, for convenience
remoteRef remote = liftIO $ do
    remote' <- unsafeManagedPtrGetPtr remote
    result <- ostree_remote_ref remote'
    checkUnexpectedReturnNULL "remoteRef" result
    result' <- (wrapBoxed Remote) result
    touchManagedPtr remote
    return result'

#if defined(ENABLE_OVERLOADING)
data RemoteRefMethodInfo
instance (signature ~ (m Remote), MonadIO m) => O.MethodInfo RemoteRefMethodInfo Remote signature where
    overloadedMethod = remoteRef

#endif

-- method Remote::unref
-- method type : OrdinaryMethod
-- Args: [ Arg
--           { argCName = "remote"
--           , argType =
--               TInterface Name { namespace = "OSTree" , name = "Remote" }
--           , direction = DirectionIn
--           , mayBeNull = False
--           , argDoc =
--               Documentation
--                 { rawDocText = Just "an #OstreeRemote" , sinceVersion = Nothing }
--           , argScope = ScopeTypeInvalid
--           , argClosure = -1
--           , argDestroy = -1
--           , argCallerAllocates = False
--           , transfer = TransferEverything
--           }
--       ]
-- Lengths: []
-- returnType: Nothing
-- throws : False
-- Skip return : False

foreign import ccall "ostree_remote_unref" ostree_remote_unref ::
    Ptr Remote ->                           -- remote : TInterface (Name {namespace = "OSTree", name = "Remote"})
    IO ()

-- | Decrease the reference count on the given /@remote@/ and free it if the
-- reference count reaches 0.
-- 
-- /Since: 2018.6/
remoteUnref ::
    (B.CallStack.HasCallStack, MonadIO m) =>
    Remote
    -- ^ /@remote@/: an t'GI.OSTree.Structs.Remote.Remote'
    -> m ()
remoteUnref remote = liftIO $ do
    remote' <- B.ManagedPtr.disownBoxed remote
    ostree_remote_unref remote'
    touchManagedPtr remote
    return ()

#if defined(ENABLE_OVERLOADING)
data RemoteUnrefMethodInfo
instance (signature ~ (m ()), MonadIO m) => O.MethodInfo RemoteUnrefMethodInfo Remote signature where
    overloadedMethod = remoteUnref

#endif

#if defined(ENABLE_OVERLOADING)
type family ResolveRemoteMethod (t :: Symbol) (o :: *) :: * where
    ResolveRemoteMethod "ref" o = RemoteRefMethodInfo
    ResolveRemoteMethod "unref" o = RemoteUnrefMethodInfo
    ResolveRemoteMethod "getName" o = RemoteGetNameMethodInfo
    ResolveRemoteMethod "getUrl" o = RemoteGetUrlMethodInfo
    ResolveRemoteMethod l o = O.MethodResolutionFailed l o

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

#endif