{-# LINE 2 "./System/Gnome/VFS/VolumeMonitor.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.VolumeMonitor (

-- * Types

  -- | An object that monitors volume mounts and unmounts.
  VolumeMonitor,
  VolumeMonitorClass,

-- * Values
  volumeMonitor,

-- * Operations
  volumeMonitorGetConnectedDrives,
  volumeMonitorGetDriveByID,
  volumeMonitorGetMountedVolumes,
  volumeMonitorGetVolumeByID,
  volumeMonitorGetVolumeForPath,

  onVolumeMonitorVolumeMounted,
  afterVolumeMonitorVolumeMounted,
  onVolumeMonitorVolumePreUnmount,
  afterVolumeMonitorVolumePreUnmount,
  onVolumeMonitorVolumeUnmounted,
  afterVolumeMonitorVolumeUnmounted

  ) where

import Control.Exception
import Control.Monad (liftM)
import System.Glib.FFI
import System.Glib.GList (readGList)
import System.Glib.UTFString
import System.Gnome.VFS.Marshal
import System.Gnome.VFS.Types
{-# LINE 67 "./System/Gnome/VFS/VolumeMonitor.chs" #-}
import System.Gnome.VFS.Signals
{-# LINE 68 "./System/Gnome/VFS/VolumeMonitor.chs" #-}
import System.Gnome.VFS.BasicTypes
{-# LINE 69 "./System/Gnome/VFS/VolumeMonitor.chs" #-}
import System.IO (FilePath)


{-# LINE 72 "./System/Gnome/VFS/VolumeMonitor.chs" #-}

-- | The global volume monitor object.
volumeMonitor :: VolumeMonitor
volumeMonitor = unsafePerformIO $ gnome_vfs_get_volume_monitor >>= wrapVolumeMonitor

-- | Returns a list of all drives connected to the machine.
volumeMonitorGetConnectedDrives :: VolumeMonitorClass volumeMonitor =>
                                   volumeMonitor -- ^ @volumeMonitor@ - the volume monitor
                                -> IO [Drive] -- ^ the drives connected to the machine
volumeMonitorGetConnectedDrives volumeMonitor =
    (\(VolumeMonitor arg1) -> withForeignPtr arg1 $ \argPtr1 ->gnome_vfs_volume_monitor_get_connected_drives argPtr1) (castToVolumeMonitor volumeMonitor) >>=
        readGList >>= mapM newDrive

-- | Try to find the 'Drive' with ID @id@.
volumeMonitorGetDriveByID :: VolumeMonitorClass volumeMonitor =>
                             volumeMonitor -- ^ @volumeMonitor@ - the volume monitor
                          -> DriveID -- ^ @id@ - the drive ID
                          -> IO (Maybe Drive) -- ^ the requested
                                              -- drive, or 'Nothing'
                                              -- if no drive with
                                              -- that ID could be
                                              -- found
volumeMonitorGetDriveByID volumeMonitor id =
    (\(VolumeMonitor arg1) arg2 -> withForeignPtr arg1 $ \argPtr1 ->gnome_vfs_volume_monitor_get_drive_by_id argPtr1 arg2) (castToVolumeMonitor volumeMonitor) id >>=
        maybePeek newDrive

-- | Returns a list of all volumes currently mounted on the machine.
volumeMonitorGetMountedVolumes :: VolumeMonitorClass volumeMonitor =>
                                  volumeMonitor -- ^ @volumeMonitor@ - the volume monitor
                               -> IO [Volume] -- ^ the volumes
                                                -- currently mounted
                                                -- on the machine
volumeMonitorGetMountedVolumes volumeMonitor =
    (\(VolumeMonitor arg1) -> withForeignPtr arg1 $ \argPtr1 ->gnome_vfs_volume_monitor_get_mounted_volumes argPtr1) (castToVolumeMonitor volumeMonitor) >>=
        readGList >>= mapM newVolume

-- | Try to find the 'Volume' with ID @id@.
volumeMonitorGetVolumeByID :: VolumeMonitorClass volumeMonitor =>
                              volumeMonitor -- ^ @volumeMonitor@ - the volume monitor
                           -> VolumeID -- ^ @id@ - the volume ID
                           -> IO (Maybe Volume) -- ^ the requested
                                                -- volume, or
                                                -- 'Nothing' if no
                                                -- volume with that
                                                -- ID could be found
volumeMonitorGetVolumeByID volumeMonitor id =
    (\(VolumeMonitor arg1) arg2 -> withForeignPtr arg1 $ \argPtr1 ->gnome_vfs_volume_monitor_get_volume_by_id argPtr1 arg2) (castToVolumeMonitor volumeMonitor) id >>=
        maybePeek newVolume

-- | Returns the 'Volume' corresponding to path, or 'Nothing'.
--
-- The volume referring to path is found by calling @stat@ on path,
-- and then iterating through the list of volumes that refer to
-- currently mounted local file systems. The first volume in this
-- list maching the path's UNIX device is returned.
--
-- If the @stat@ on path was not successful, or no volume matches
-- path, 'Nothing' is returned.
volumeMonitorGetVolumeForPath :: VolumeMonitorClass volumeMonitor =>
                                 volumeMonitor -- ^ @volumeMonitor@ - the volume monitor
                              -> FilePath -- ^ the path to
                                                   -- find the volume
                                                   -- for
                              -> IO (Maybe Volume) -- ^ the volume the
                                                   -- path resides
                                                   -- on, or
                                                   -- 'Nothing' if
                                                   -- the volume
                                                   -- could not be
                                                   -- determined
volumeMonitorGetVolumeForPath volumeMonitor path =
    (withUTFString path $ (\(VolumeMonitor arg1) arg2 -> withForeignPtr arg1 $ \argPtr1 ->gnome_vfs_volume_monitor_get_volume_for_path argPtr1 arg2) (castToVolumeMonitor volumeMonitor)) >>=
        maybePeek newVolume

onVolumeMonitorDriveConnected,
    afterVolumeMonitorDriveConnected,
    onVolumeMonitorDriveDisconnected,
    afterVolumeMonitorDriveDisconnected,
    onVolumeMonitorVolumeMounted,
    afterVolumeMonitorVolumeMounted,
    onVolumeMonitorVolumePreUnmount,
    afterVolumeMonitorVolumePreUnmount,
    onVolumeMonitorVolumeUnmounted,
    afterVolumeMonitorVolumeUnmounted
    :: (VolumeMonitorClass volumeMonitor) =>
       volumeMonitor -- ^ @volumeMonitor@ - the volume monitor
    -> (Volume -> IO ()) -- ^ @handler@ - the signal handling function
    -> IO (ConnectId volumeMonitor) -- ^ the identifier for the connection

onVolumeMonitorDriveConnected = connect_OBJECT__NONE "drive-connected" False
afterVolumeMonitorDriveConnected = connect_OBJECT__NONE "drive-connected" True

onVolumeMonitorDriveDisconnected = connect_OBJECT__NONE "drive-disconnected" False
afterVolumeMonitorDriveDisconnected = connect_OBJECT__NONE "drive-disconnected" True

onVolumeMonitorVolumeMounted = connect_OBJECT__NONE "volume-mounted" False
afterVolumeMonitorVolumeMounted = connect_OBJECT__NONE "volume-mounted" True

onVolumeMonitorVolumePreUnmount = connect_OBJECT__NONE "volume-pre-unmount" False
afterVolumeMonitorVolumePreUnmount = connect_OBJECT__NONE "volume-pre-unmount" True

onVolumeMonitorVolumeUnmounted = connect_OBJECT__NONE "volume-unmounted" False
afterVolumeMonitorVolumeUnmounted = connect_OBJECT__NONE "volume-unmounted" True

foreign import ccall safe "gnome_vfs_get_volume_monitor"
  gnome_vfs_get_volume_monitor :: (IO (Ptr VolumeMonitor))

foreign import ccall safe "gnome_vfs_volume_monitor_get_connected_drives"
  gnome_vfs_volume_monitor_get_connected_drives :: ((Ptr VolumeMonitor) -> (IO (Ptr ())))

foreign import ccall safe "gnome_vfs_volume_monitor_get_drive_by_id"
  gnome_vfs_volume_monitor_get_drive_by_id :: ((Ptr VolumeMonitor) -> (CULong -> (IO (Ptr Drive))))

foreign import ccall safe "gnome_vfs_volume_monitor_get_mounted_volumes"
  gnome_vfs_volume_monitor_get_mounted_volumes :: ((Ptr VolumeMonitor) -> (IO (Ptr ())))

foreign import ccall safe "gnome_vfs_volume_monitor_get_volume_by_id"
  gnome_vfs_volume_monitor_get_volume_by_id :: ((Ptr VolumeMonitor) -> (CULong -> (IO (Ptr Volume))))

foreign import ccall safe "gnome_vfs_volume_monitor_get_volume_for_path"
  gnome_vfs_volume_monitor_get_volume_for_path :: ((Ptr VolumeMonitor) -> ((Ptr CChar) -> (IO (Ptr Volume))))