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

-- * Types
  Handle,
  Result(..),
  OpenMode(..),
  SeekPosition(..),
  FilePermissions(..),
  FileSize,
  FileOffset,

-- * I\/O Operations
  open,
  openURI,
  create,
  createURI,
  close,




  seek,
  tell,

  forgetCache,


-- * Truncation
  truncate,
  truncateURI,
  truncateHandle,

-- * File Information
  getFileInfo,
  getFileInfoURI,
  getFileInfoFromHandle,
  setFileInfo,
  setFileInfoURI

  ) where





import Control.Exception
import Control.Monad (liftM)
{-# LINE 88 "./System/Gnome/VFS/Ops.chs" #-}
import Prelude hiding (read, truncate)
import System.Glib.FFI
import System.Glib.UTFString (withUTFString, peekUTFString)
-- {#import System.Gnome.VFS.Types#}
import System.Gnome.VFS.BasicTypes
{-# LINE 93 "./System/Gnome/VFS/Ops.chs" #-}
import System.Gnome.VFS.FileInfo
{-# LINE 94 "./System/Gnome/VFS/Ops.chs" #-}
import System.Gnome.VFS.Marshal
{-# LINE 95 "./System/Gnome/VFS/Ops.chs" #-}


{-# LINE 97 "./System/Gnome/VFS/Ops.chs" #-}

-- | Open the file at @textURI@.
open :: TextURI -- ^ @textURI@ -
     -> OpenMode -- ^ @openMode@ -
     -> IO Handle -- ^ a handle to the opened file
open textURI openMode =
    let cOpenMode = cFromEnum openMode
    in withUTFString textURI $ \cTextURI ->
        newObjectResultMarshal Handle $ \cHandlePtr ->
            gnome_vfs_open (castPtr cHandlePtr) cTextURI cOpenMode

-- | Open the file at @uri@.
openURI :: URI -- ^ @uri@ -
        -> OpenMode -- ^ @openMode@ -
        -> IO Handle -- ^ a handle to the opened file
openURI uri openMode =
    let cOpenMode = cFromEnum openMode
    in newObjectResultMarshal Handle $ \cHandlePtr ->
        (\arg1 (URI arg2) arg3 -> withForeignPtr arg2 $ \argPtr2 ->gnome_vfs_open_uri arg1 argPtr2 arg3) (castPtr cHandlePtr) uri cOpenMode

-- | Create a file at @textURI@.
create :: TextURI -- ^ @textURI@ -
       -> OpenMode -- ^ @openMode@ -
       -> Bool -- ^ @exclusive@ -
       -> [FilePermissions] -- ^ @perm@ -
       -> IO Handle -- ^ a handle to the created file
create textURI openMode exclusive perm =
    let cOpenMode = cFromEnum openMode
        cExclusive = fromBool exclusive
        cPerm = cFromFlags perm
    in withUTFString textURI $ \cTextURI ->
        newObjectResultMarshal Handle $ \cHandlePtr ->
            gnome_vfs_create (castPtr cHandlePtr) cTextURI cOpenMode cExclusive cPerm

-- | Create a file at @uri@.
createURI :: URI -- ^ @uri@ -
          -> OpenMode -- ^ @openMode@ -
          -> Bool -- ^ @exclusive@ -
          -> [FilePermissions] -- ^ @perm@ -
          -> IO Handle -- ^ a handle to the created file
createURI uri openMode exclusive perm =
    let cOpenMode = cFromEnum openMode
        cExclusive = fromBool exclusive
        cPerm = cFromFlags perm
    in newObjectResultMarshal Handle $ \cHandlePtr ->
        (\arg1 (URI arg2) arg3 arg4 arg5 -> withForeignPtr arg2 $ \argPtr2 ->gnome_vfs_create_uri arg1 argPtr2 arg3 arg4 arg5) (castPtr cHandlePtr) uri cOpenMode cExclusive cPerm

-- | Close a 'Handle'.
close :: Handle -- ^ @handle@ -
      -> IO ()
close handle =
    voidResultMarshal $ (\(Handle arg1) -> withForeignPtr arg1 $ \argPtr1 ->gnome_vfs_close argPtr1) handle
{-# LINE 191 "./System/Gnome/VFS/Ops.chs" #-}
-- | Seek to a position in a file.
seek :: Handle -- ^ @handle@ -
     -> SeekPosition -- ^ @whence@ -
     -> FileOffset -- ^ @offset@ -
     -> IO ()
seek handle whence offset =
    let cWhence = cFromEnum whence
        cOffset = fromIntegral offset
    in voidResultMarshal $ (\(Handle arg1) arg2 arg3 -> withForeignPtr arg1 $ \argPtr1 ->gnome_vfs_seek argPtr1 arg2 arg3) handle cWhence cOffset

-- | Return the current position in the file.
tell :: Handle -- ^ @handle@ -
     -> IO FileSize -- ^ the current position in the file
tell handle =
    alloca $ \cOffsetReturnPtr ->
        genericResultMarshal
            (do poke cOffsetReturnPtr 0
                (\(Handle arg1) arg2 -> withForeignPtr arg1 $ \argPtr1 ->gnome_vfs_tell argPtr1 arg2) handle cOffsetReturnPtr)
            (liftM fromIntegral $ peek cOffsetReturnPtr)
            (do cOffsetReturn <- peek cOffsetReturnPtr
                assert (cOffsetReturn == 0) $ return ())


-- | Free any cache associated with the file opened on @handle@,
-- in the region of @size@ bytes starting at @offset@.
forgetCache :: Handle
            -> FileOffset
            -> FileSize
            -> IO ()
forgetCache handle offset size =
    let cOffset = fromIntegral offset
        cSize = fromIntegral size
    in voidResultMarshal $ (\(Handle arg1) arg2 arg3 -> withForeignPtr arg1 $ \argPtr1 ->gnome_vfs_forget_cache argPtr1 arg2 arg3) handle cOffset cSize


-- | Truncate the file at @textURI@ to @length@ bytes.
truncate :: String
         -> FileSize
         -> IO ()
truncate textURI length =
    let cLength = fromIntegral length
    in withUTFString textURI $ \cTextURI ->
        voidResultMarshal $ gnome_vfs_truncate cTextURI cLength

-- | Truncate the file at @uri@ to @length@ bytes.
truncateURI :: URI
            -> FileSize
            -> IO ()
truncateURI uri length =
    let cLength = fromIntegral length
    in voidResultMarshal $ (\(URI arg1) arg2 -> withForeignPtr arg1 $ \argPtr1 ->gnome_vfs_truncate_uri argPtr1 arg2) uri cLength

-- | Truncate the file opened on @handle@ to @length@ bytes.
truncateHandle :: Handle
               -> FileSize
               -> IO ()
truncateHandle handle length =
    let cLength = fromIntegral length
    in voidResultMarshal $ (\(Handle arg1) arg2 -> withForeignPtr arg1 $ \argPtr1 ->gnome_vfs_truncate_handle argPtr1 arg2) handle cLength

-- | Get the file information for the file at @textURI@.
getFileInfo :: String
            -> [FileInfoOptions]
            -> IO FileInfo
getFileInfo textURI options =
    let cOptions = cFromFlags options
    in withUTFString textURI $ \cTextURI ->
        bracket gnome_vfs_file_info_new
{-# LINE 259 "./System/Gnome/VFS/Ops.chs" #-}
                gnome_vfs_file_info_unref
{-# LINE 260 "./System/Gnome/VFS/Ops.chs" #-}
                (\cFileInfo ->
                     genericResultMarshal
                         (gnome_vfs_get_file_info cTextURI cFileInfo cOptions)
                         (peek $ castPtr cFileInfo)
                         (return ()))

-- | Get the file information for the file at @uri@.
getFileInfoURI :: URI
               -> [FileInfoOptions]
               -> IO FileInfo
getFileInfoURI uri options =
    let cOptions = cFromFlags options
    in bracket gnome_vfs_file_info_new
{-# LINE 273 "./System/Gnome/VFS/Ops.chs" #-}
               gnome_vfs_file_info_unref
{-# LINE 274 "./System/Gnome/VFS/Ops.chs" #-}
               (\cFileInfo ->
                genericResultMarshal
                    ((\(URI arg1) arg2 arg3 -> withForeignPtr arg1 $ \argPtr1 ->gnome_vfs_get_file_info_uri argPtr1 arg2 arg3) uri cFileInfo cOptions)
                    (peek $ castPtr cFileInfo)
                    (return ()))

-- | Get the file information for the file opened on @handle@.
getFileInfoFromHandle :: Handle
                      -> [FileInfoOptions]
                      -> IO FileInfo
getFileInfoFromHandle handle options =
    let cOptions = cFromFlags options
    in bracket gnome_vfs_file_info_new
{-# LINE 287 "./System/Gnome/VFS/Ops.chs" #-}
               gnome_vfs_file_info_unref
{-# LINE 288 "./System/Gnome/VFS/Ops.chs" #-}
               (\cFileInfo ->
                    genericResultMarshal
                        ((\(Handle arg1) arg2 arg3 -> withForeignPtr arg1 $ \argPtr1 ->gnome_vfs_get_file_info_from_handle argPtr1 arg2 arg3) handle cFileInfo cOptions)
                        (peek $ castPtr cFileInfo)
                        (return ()))

-- | Set the file information for the file at @textURI@.
setFileInfo :: String
            -> FileInfo
            -> [SetFileInfoMask]
            -> IO ()
setFileInfo textURI info mask =
    withUTFString textURI $ \cTextURI ->
        with info $ \cInfo ->
             voidResultMarshal $ gnome_vfs_set_file_info cTextURI (castPtr cInfo) $ cFromFlags mask

-- | Set the file information for the file at @uri@.
setFileInfoURI :: URI
               -> FileInfo
               -> [SetFileInfoMask]
               -> IO ()
setFileInfoURI uri info mask =
    with info $ \cInfo ->
        voidResultMarshal $ (\(URI arg1) arg2 arg3 -> withForeignPtr arg1 $ \argPtr1 ->gnome_vfs_set_file_info_uri argPtr1 arg2 arg3) uri (castPtr cInfo) $ cFromFlags mask

foreign import ccall safe "gnome_vfs_open"
  gnome_vfs_open :: ((Ptr Handle) -> ((Ptr CChar) -> (CInt -> (IO CInt))))

foreign import ccall safe "gnome_vfs_open_uri"
  gnome_vfs_open_uri :: ((Ptr Handle) -> ((Ptr URI) -> (CInt -> (IO CInt))))

foreign import ccall safe "gnome_vfs_create"
  gnome_vfs_create :: ((Ptr Handle) -> ((Ptr CChar) -> (CInt -> (CInt -> (CUInt -> (IO CInt))))))

foreign import ccall safe "gnome_vfs_create_uri"
  gnome_vfs_create_uri :: ((Ptr Handle) -> ((Ptr URI) -> (CInt -> (CInt -> (CUInt -> (IO CInt))))))

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

foreign import ccall safe "gnome_vfs_seek"
  gnome_vfs_seek :: ((Ptr Handle) -> (CInt -> (CLLong -> (IO CInt))))

foreign import ccall safe "gnome_vfs_tell"
  gnome_vfs_tell :: ((Ptr Handle) -> ((Ptr CULLong) -> (IO CInt)))

foreign import ccall safe "gnome_vfs_forget_cache"
  gnome_vfs_forget_cache :: ((Ptr Handle) -> (CLLong -> (CULLong -> (IO CInt))))

foreign import ccall safe "gnome_vfs_truncate"
  gnome_vfs_truncate :: ((Ptr CChar) -> (CULLong -> (IO CInt)))

foreign import ccall safe "gnome_vfs_truncate_uri"
  gnome_vfs_truncate_uri :: ((Ptr URI) -> (CULLong -> (IO CInt)))

foreign import ccall safe "gnome_vfs_truncate_handle"
  gnome_vfs_truncate_handle :: ((Ptr Handle) -> (CULLong -> (IO CInt)))

foreign import ccall safe "gnome_vfs_file_info_new"
  gnome_vfs_file_info_new :: (IO (Ptr ()))

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

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

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

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

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

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