{-# LINE 2 "./System/Gnome/VFS/Monitor.chs" #-}
-- GIMP Toolkit (GTK) Binding for Haskell: binding to libgnomevfs -*-haskell-*-
--
-- Author : Peter Gavin
-- Created: 1-Apr-2007
--
-- Copyright (c) 2007 Peter Gavin
--
-- This library is free software: you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public License
-- as published by the Free Software Foundation, either version 3 of
-- the License, or (at your option) any later version.
--
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public
-- License along with this program. If not, see
-- <http:
--
-- GnomeVFS, the C library which this Haskell library depends on, is
-- available under LGPL Version 2. The documentation included with
-- this library is based on the original GnomeVFS documentation,
-- Copyright (c) 2001 Seth Nickell <snickell@stanford.edu>. The
-- documentation is covered by the GNU Free Documentation License,
-- version 1.2.
--
-- | Maintainer : gtk2hs-devel@lists.sourceforge.net
-- Stability : alpha
-- Portability : portable (depends on GHC)
module System.Gnome.VFS.Monitor (

-- * Types
  MonitorHandle,
  MonitorCallback,

-- * Operations
  monitorAdd,
  monitorCancel

  ) where

import Control.Monad (liftM)
import System.Glib.FFI
import System.Glib.UTFString
import System.Gnome.VFS.Marshal
{-# LINE 49 "./System/Gnome/VFS/Monitor.chs" #-}
-- {#import System.Gnome.VFS.Types#}
import System.Gnome.VFS.BasicTypes
{-# LINE 51 "./System/Gnome/VFS/Monitor.chs" #-}


{-# LINE 53 "./System/Gnome/VFS/Monitor.chs" #-}

type CMonitorCallback = Ptr MonitorHandle
                      -> CString
                      -> CString
                      -> (CInt)
{-# LINE 58 "./System/Gnome/VFS/Monitor.chs" #-}
                      -> ((Ptr ()))
{-# LINE 59 "./System/Gnome/VFS/Monitor.chs" #-}
                      -> IO ()

-- | Watch the object at @textURI@ for changes, and call @callback@
-- when a change occurs.
monitorAdd :: String -- ^ @textURI@ -
           -> MonitorType -- ^ @monitorType@ -
           -> MonitorCallback -- ^ @callback@ -
           -> IO MonitorHandle -- ^ a handle to the new monitor
monitorAdd textURI monitorType callback =
    do cTestURI <- newUTFString textURI
       let cMonitorType = cFromEnum monitorType
       cCallback <- monitorCallbackMarshal callback
       newObjectResultMarshal
           (\cMonitorHandle ->
            MonitorHandle (cMonitorHandle, cCallback))
           (\cMonitorHandlePtr ->
            gnome_vfs_monitor_add (castPtr cMonitorHandlePtr) cTestURI cMonitorType cCallback nullPtr)

monitorCallbackMarshal :: MonitorCallback
                        -> IO ((FunPtr ((Ptr ()) -> ((Ptr CChar) -> ((Ptr CChar) -> (CInt -> ((Ptr ()) -> (IO ()))))))))
{-# LINE 79 "./System/Gnome/VFS/Monitor.chs" #-}
monitorCallbackMarshal callback =
    let cCallback :: CMonitorCallback
        cCallback cHandle cMonitorURI cInfoURI cEventType cUserData =
            do handle <- liftM (\cHandle -> MonitorHandle (cHandle, castPtrToFunPtr cUserData)) $
                         newForeignPtr_ cHandle
               monitorURI <- peekUTFString cMonitorURI
               infoURI <- peekUTFString cInfoURI
               let eventType = cToEnum cEventType
               callback handle monitorURI infoURI eventType
    in makeMonitorCallback cCallback
foreign import ccall safe "wrapper"
  makeMonitorCallback :: CMonitorCallback
                      -> IO ((FunPtr ((Ptr ()) -> ((Ptr CChar) -> ((Ptr CChar) -> (CInt -> ((Ptr ()) -> (IO ()))))))))
{-# LINE 92 "./System/Gnome/VFS/Monitor.chs" #-}

-- | Cancels the monitor referred to by @monitorHandle@.
monitorCancel :: MonitorHandle -- ^ @monitorHandle@
              -> IO ()
monitorCancel (MonitorHandle (cMonitorHandle, cCallback)) =
    do freeHaskellFunPtr cCallback
       withForeignPtr cMonitorHandle $
                      voidResultMarshal . gnome_vfs_monitor_cancel . castPtr

foreign import ccall safe "gnome_vfs_monitor_add"
  gnome_vfs_monitor_add :: ((Ptr (Ptr ())) -> ((Ptr CChar) -> (CInt -> ((FunPtr ((Ptr ()) -> ((Ptr CChar) -> ((Ptr CChar) -> (CInt -> ((Ptr ()) -> (IO ()))))))) -> ((Ptr ()) -> (IO CInt))))))

foreign import ccall safe "gnome_vfs_monitor_cancel"
  gnome_vfs_monitor_cancel :: ((Ptr ()) -> (IO CInt))