{- |
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.Gio.Interfaces.NetworkMonitor
    ( 

-- * Exported types
    NetworkMonitor(..)                      ,
    noNetworkMonitor                        ,
    NetworkMonitorK                         ,
    toNetworkMonitor                        ,


 -- * Methods
-- ** networkMonitorCanReach
    networkMonitorCanReach                  ,


-- ** networkMonitorCanReachAsync
    networkMonitorCanReachAsync             ,


-- ** networkMonitorCanReachFinish
    networkMonitorCanReachFinish            ,


-- ** networkMonitorGetConnectivity
    networkMonitorGetConnectivity           ,


-- ** networkMonitorGetNetworkAvailable
    networkMonitorGetNetworkAvailable       ,




 -- * Properties
-- ** Connectivity
    NetworkMonitorConnectivityPropertyInfo  ,
    getNetworkMonitorConnectivity           ,


-- ** NetworkAvailable
    NetworkMonitorNetworkAvailablePropertyInfo,
    getNetworkMonitorNetworkAvailable       ,




 -- * Signals
-- ** NetworkChanged
    NetworkMonitorNetworkChangedCallback    ,
    NetworkMonitorNetworkChangedCallbackC   ,
    NetworkMonitorNetworkChangedSignalInfo  ,
    afterNetworkMonitorNetworkChanged       ,
    mkNetworkMonitorNetworkChangedCallback  ,
    networkMonitorNetworkChangedCallbackWrapper,
    networkMonitorNetworkChangedClosure     ,
    noNetworkMonitorNetworkChangedCallback  ,
    onNetworkMonitorNetworkChanged          ,




    ) where

import Prelude ()
import Data.GI.Base.ShortPrelude

import qualified Data.Text as T
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as Map

import GI.Gio.Types
import GI.Gio.Callbacks
import qualified GI.GObject as GObject

-- interface NetworkMonitor 

newtype NetworkMonitor = NetworkMonitor (ForeignPtr NetworkMonitor)
noNetworkMonitor :: Maybe NetworkMonitor
noNetworkMonitor = Nothing

-- signal NetworkMonitor::network-changed
type NetworkMonitorNetworkChangedCallback =
    Bool ->
    IO ()

noNetworkMonitorNetworkChangedCallback :: Maybe NetworkMonitorNetworkChangedCallback
noNetworkMonitorNetworkChangedCallback = Nothing

type NetworkMonitorNetworkChangedCallbackC =
    Ptr () ->                               -- object
    CInt ->
    Ptr () ->                               -- user_data
    IO ()

foreign import ccall "wrapper"
    mkNetworkMonitorNetworkChangedCallback :: NetworkMonitorNetworkChangedCallbackC -> IO (FunPtr NetworkMonitorNetworkChangedCallbackC)

networkMonitorNetworkChangedClosure :: NetworkMonitorNetworkChangedCallback -> IO Closure
networkMonitorNetworkChangedClosure cb = newCClosure =<< mkNetworkMonitorNetworkChangedCallback wrapped
    where wrapped = networkMonitorNetworkChangedCallbackWrapper cb

networkMonitorNetworkChangedCallbackWrapper ::
    NetworkMonitorNetworkChangedCallback ->
    Ptr () ->
    CInt ->
    Ptr () ->
    IO ()
networkMonitorNetworkChangedCallbackWrapper _cb _ available _ = do
    let available' = (/= 0) available
    _cb  available'

onNetworkMonitorNetworkChanged :: (GObject a, MonadIO m) => a -> NetworkMonitorNetworkChangedCallback -> m SignalHandlerId
onNetworkMonitorNetworkChanged obj cb = liftIO $ connectNetworkMonitorNetworkChanged obj cb SignalConnectBefore
afterNetworkMonitorNetworkChanged :: (GObject a, MonadIO m) => a -> NetworkMonitorNetworkChangedCallback -> m SignalHandlerId
afterNetworkMonitorNetworkChanged obj cb = connectNetworkMonitorNetworkChanged obj cb SignalConnectAfter

connectNetworkMonitorNetworkChanged :: (GObject a, MonadIO m) =>
                                       a -> NetworkMonitorNetworkChangedCallback -> SignalConnectMode -> m SignalHandlerId
connectNetworkMonitorNetworkChanged obj cb after = liftIO $ do
    cb' <- mkNetworkMonitorNetworkChangedCallback (networkMonitorNetworkChangedCallbackWrapper cb)
    connectSignalFunPtr obj "network-changed" cb' after

-- VVV Prop "connectivity"
   -- Type: TInterface "Gio" "NetworkConnectivity"
   -- Flags: [PropertyReadable]

getNetworkMonitorConnectivity :: (MonadIO m, NetworkMonitorK o) => o -> m NetworkConnectivity
getNetworkMonitorConnectivity obj = liftIO $ getObjectPropertyEnum obj "connectivity"

data NetworkMonitorConnectivityPropertyInfo
instance AttrInfo NetworkMonitorConnectivityPropertyInfo where
    type AttrAllowedOps NetworkMonitorConnectivityPropertyInfo = '[ 'AttrGet]
    type AttrSetTypeConstraint NetworkMonitorConnectivityPropertyInfo = (~) ()
    type AttrBaseTypeConstraint NetworkMonitorConnectivityPropertyInfo = NetworkMonitorK
    type AttrGetType NetworkMonitorConnectivityPropertyInfo = NetworkConnectivity
    type AttrLabel NetworkMonitorConnectivityPropertyInfo = "NetworkMonitor::connectivity"
    attrGet _ = getNetworkMonitorConnectivity
    attrSet _ = undefined
    attrConstruct _ = undefined

-- VVV Prop "network-available"
   -- Type: TBasicType TBoolean
   -- Flags: [PropertyReadable]

getNetworkMonitorNetworkAvailable :: (MonadIO m, NetworkMonitorK o) => o -> m Bool
getNetworkMonitorNetworkAvailable obj = liftIO $ getObjectPropertyBool obj "network-available"

data NetworkMonitorNetworkAvailablePropertyInfo
instance AttrInfo NetworkMonitorNetworkAvailablePropertyInfo where
    type AttrAllowedOps NetworkMonitorNetworkAvailablePropertyInfo = '[ 'AttrGet]
    type AttrSetTypeConstraint NetworkMonitorNetworkAvailablePropertyInfo = (~) ()
    type AttrBaseTypeConstraint NetworkMonitorNetworkAvailablePropertyInfo = NetworkMonitorK
    type AttrGetType NetworkMonitorNetworkAvailablePropertyInfo = Bool
    type AttrLabel NetworkMonitorNetworkAvailablePropertyInfo = "NetworkMonitor::network-available"
    attrGet _ = getNetworkMonitorNetworkAvailable
    attrSet _ = undefined
    attrConstruct _ = undefined

type instance AttributeList NetworkMonitor = NetworkMonitorAttributeList
type NetworkMonitorAttributeList = ('[ '("connectivity", NetworkMonitorConnectivityPropertyInfo), '("network-available", NetworkMonitorNetworkAvailablePropertyInfo)] :: [(Symbol, *)])

data NetworkMonitorNetworkChangedSignalInfo
instance SignalInfo NetworkMonitorNetworkChangedSignalInfo where
    type HaskellCallbackType NetworkMonitorNetworkChangedSignalInfo = NetworkMonitorNetworkChangedCallback
    connectSignal _ = connectNetworkMonitorNetworkChanged

type instance SignalList NetworkMonitor = NetworkMonitorSignalList
type NetworkMonitorSignalList = ('[ '("network-changed", NetworkMonitorNetworkChangedSignalInfo), '("notify", GObject.ObjectNotifySignalInfo), '("notify::[property]", GObjectNotifySignalInfo)] :: [(Symbol, *)])

foreign import ccall "g_network_monitor_get_type"
    c_g_network_monitor_get_type :: IO GType

type instance ParentTypes NetworkMonitor = NetworkMonitorParentTypes
type NetworkMonitorParentTypes = '[Initable, GObject.Object]

instance GObject NetworkMonitor where
    gobjectIsInitiallyUnowned _ = False
    gobjectType _ = c_g_network_monitor_get_type
    

class GObject o => NetworkMonitorK o
instance (GObject o, IsDescendantOf NetworkMonitor o) => NetworkMonitorK o

toNetworkMonitor :: NetworkMonitorK o => o -> IO NetworkMonitor
toNetworkMonitor = unsafeCastTo NetworkMonitor

-- method NetworkMonitor::can_reach
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Gio" "NetworkMonitor", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "connectable", argType = TInterface "Gio" "SocketConnectable", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "cancellable", argType = TInterface "Gio" "Cancellable", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Gio" "NetworkMonitor", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "connectable", argType = TInterface "Gio" "SocketConnectable", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "cancellable", argType = TInterface "Gio" "Cancellable", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TBoolean
-- throws : True
-- Skip return : False

foreign import ccall "g_network_monitor_can_reach" g_network_monitor_can_reach :: 
    Ptr NetworkMonitor ->                   -- _obj : TInterface "Gio" "NetworkMonitor"
    Ptr SocketConnectable ->                -- connectable : TInterface "Gio" "SocketConnectable"
    Ptr Cancellable ->                      -- cancellable : TInterface "Gio" "Cancellable"
    Ptr (Ptr GError) ->                     -- error
    IO CInt


networkMonitorCanReach ::
    (MonadIO m, NetworkMonitorK a, SocketConnectableK b, CancellableK c) =>
    a ->                                    -- _obj
    b ->                                    -- connectable
    Maybe (c) ->                            -- cancellable
    m ()
networkMonitorCanReach _obj connectable cancellable = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    let connectable' = unsafeManagedPtrCastPtr connectable
    maybeCancellable <- case cancellable of
        Nothing -> return nullPtr
        Just jCancellable -> do
            let jCancellable' = unsafeManagedPtrCastPtr jCancellable
            return jCancellable'
    onException (do
        _ <- propagateGError $ g_network_monitor_can_reach _obj' connectable' maybeCancellable
        touchManagedPtr _obj
        touchManagedPtr connectable
        whenJust cancellable touchManagedPtr
        return ()
     ) (do
        return ()
     )

-- method NetworkMonitor::can_reach_async
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Gio" "NetworkMonitor", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "connectable", argType = TInterface "Gio" "SocketConnectable", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "cancellable", argType = TInterface "Gio" "Cancellable", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "callback", argType = TInterface "Gio" "AsyncReadyCallback", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeAsync, argClosure = 4, argDestroy = -1, transfer = TransferNothing},Arg {argName = "user_data", argType = TBasicType TVoid, direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Gio" "NetworkMonitor", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "connectable", argType = TInterface "Gio" "SocketConnectable", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "cancellable", argType = TInterface "Gio" "Cancellable", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "callback", argType = TInterface "Gio" "AsyncReadyCallback", direction = DirectionIn, mayBeNull = True, argScope = ScopeTypeAsync, argClosure = 4, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TVoid
-- throws : False
-- Skip return : False

foreign import ccall "g_network_monitor_can_reach_async" g_network_monitor_can_reach_async :: 
    Ptr NetworkMonitor ->                   -- _obj : TInterface "Gio" "NetworkMonitor"
    Ptr SocketConnectable ->                -- connectable : TInterface "Gio" "SocketConnectable"
    Ptr Cancellable ->                      -- cancellable : TInterface "Gio" "Cancellable"
    FunPtr AsyncReadyCallbackC ->           -- callback : TInterface "Gio" "AsyncReadyCallback"
    Ptr () ->                               -- user_data : TBasicType TVoid
    IO ()


networkMonitorCanReachAsync ::
    (MonadIO m, NetworkMonitorK a, SocketConnectableK b, CancellableK c) =>
    a ->                                    -- _obj
    b ->                                    -- connectable
    Maybe (c) ->                            -- cancellable
    Maybe (AsyncReadyCallback) ->           -- callback
    m ()
networkMonitorCanReachAsync _obj connectable cancellable callback = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    let connectable' = unsafeManagedPtrCastPtr connectable
    maybeCancellable <- case cancellable of
        Nothing -> return nullPtr
        Just jCancellable -> do
            let jCancellable' = unsafeManagedPtrCastPtr jCancellable
            return jCancellable'
    ptrcallback <- callocMem :: IO (Ptr (FunPtr AsyncReadyCallbackC))
    maybeCallback <- case callback of
        Nothing -> return (castPtrToFunPtr nullPtr)
        Just jCallback -> do
            jCallback' <- mkAsyncReadyCallback (asyncReadyCallbackWrapper (Just ptrcallback) jCallback)
            poke ptrcallback jCallback'
            return jCallback'
    let user_data = nullPtr
    g_network_monitor_can_reach_async _obj' connectable' maybeCancellable maybeCallback user_data
    touchManagedPtr _obj
    touchManagedPtr connectable
    whenJust cancellable touchManagedPtr
    return ()

-- method NetworkMonitor::can_reach_finish
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Gio" "NetworkMonitor", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "result", argType = TInterface "Gio" "AsyncResult", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Gio" "NetworkMonitor", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing},Arg {argName = "result", argType = TInterface "Gio" "AsyncResult", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TBoolean
-- throws : True
-- Skip return : False

foreign import ccall "g_network_monitor_can_reach_finish" g_network_monitor_can_reach_finish :: 
    Ptr NetworkMonitor ->                   -- _obj : TInterface "Gio" "NetworkMonitor"
    Ptr AsyncResult ->                      -- result : TInterface "Gio" "AsyncResult"
    Ptr (Ptr GError) ->                     -- error
    IO CInt


networkMonitorCanReachFinish ::
    (MonadIO m, NetworkMonitorK a, AsyncResultK b) =>
    a ->                                    -- _obj
    b ->                                    -- result
    m ()
networkMonitorCanReachFinish _obj result_ = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    let result_' = unsafeManagedPtrCastPtr result_
    onException (do
        _ <- propagateGError $ g_network_monitor_can_reach_finish _obj' result_'
        touchManagedPtr _obj
        touchManagedPtr result_
        return ()
     ) (do
        return ()
     )

-- method NetworkMonitor::get_connectivity
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Gio" "NetworkMonitor", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Gio" "NetworkMonitor", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TInterface "Gio" "NetworkConnectivity"
-- throws : False
-- Skip return : False

foreign import ccall "g_network_monitor_get_connectivity" g_network_monitor_get_connectivity :: 
    Ptr NetworkMonitor ->                   -- _obj : TInterface "Gio" "NetworkMonitor"
    IO CUInt


networkMonitorGetConnectivity ::
    (MonadIO m, NetworkMonitorK a) =>
    a ->                                    -- _obj
    m NetworkConnectivity
networkMonitorGetConnectivity _obj = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    result <- g_network_monitor_get_connectivity _obj'
    let result' = (toEnum . fromIntegral) result
    touchManagedPtr _obj
    return result'

-- method NetworkMonitor::get_network_available
-- method type : OrdinaryMethod
-- Args : [Arg {argName = "_obj", argType = TInterface "Gio" "NetworkMonitor", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- Lengths : []
-- hInArgs : [Arg {argName = "_obj", argType = TInterface "Gio" "NetworkMonitor", direction = DirectionIn, mayBeNull = False, argScope = ScopeTypeInvalid, argClosure = -1, argDestroy = -1, transfer = TransferNothing}]
-- returnType : TBasicType TBoolean
-- throws : False
-- Skip return : False

foreign import ccall "g_network_monitor_get_network_available" g_network_monitor_get_network_available :: 
    Ptr NetworkMonitor ->                   -- _obj : TInterface "Gio" "NetworkMonitor"
    IO CInt


networkMonitorGetNetworkAvailable ::
    (MonadIO m, NetworkMonitorK a) =>
    a ->                                    -- _obj
    m Bool
networkMonitorGetNetworkAvailable _obj = liftIO $ do
    let _obj' = unsafeManagedPtrCastPtr _obj
    result <- g_network_monitor_get_network_available _obj'
    let result' = (/= 0) result
    touchManagedPtr _obj
    return result'