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

-- * Types
  MIMEType,

-- * MIME Type Operations
  mimeTypeFromNameOrDefault,
  getMIMETypeCommon,
  getMIMETypeFromURI,
  getFileMIMETypeFast,
  getFileMIMEType,
  mimeTypeIsSupertype,
  getSupertypeFromMIMEType,
  mimeInfoCacheReload

  ) where

import Control.Monad (liftM)
import System.Glib.Flags
import System.Glib.FFI
import System.Glib.UTFString
-- {#import System.Gnome.VFS.Types#}
import System.Gnome.VFS.BasicTypes
{-# LINE 56 "./System/Gnome/VFS/MIME.chs" #-}
import System.Gnome.VFS.Marshal
{-# LINE 57 "./System/Gnome/VFS/MIME.chs" #-}


{-# LINE 59 "./System/Gnome/VFS/MIME.chs" #-}

-- | Try to determine the MIME-type of the file at @filename@, using
-- only the filename and the Gnome VFS MIME type database. If the
-- MIME-type is not found, return @defaultv@.
mimeTypeFromNameOrDefault :: FilePath -- ^ @filename@ - the file
                                            -- to get the MIME-type
                                            -- for
                          -> Maybe MIMEType -- ^ @defaultv@ - the
                                            -- default MIME-type to
                                            -- return if no match is
                                            -- found
                          -> Maybe MIMEType -- ^ the MIME-type of the
                                            -- filename, or @defaultv@
mimeTypeFromNameOrDefault filename defaultv =
    unsafePerformIO $ maybeWith withUTFString defaultv $ \cDefaultv ->
        withUTFString filename $ \cFilename ->
            gnome_vfs_mime_type_from_name_or_default cFilename cDefaultv >>=
                maybePeek peekUTFString

-- | Try to get the MIME-type of the file represented by @uri@. This
-- function favors the contents of the file over the extension of
-- the filename. If the file does not exist, the MIME-type for the
-- extension is returned. If no MIME-type can be found for the file,
-- the function returns \"application\/octet-stream\".
--
-- Note: This function will not necessarily return the same
-- MIME-type as 'System.Gnome.VFS.Ops.getFileInfo'.
getMIMETypeCommon :: URI -- ^ @uri@ - the URI of the file to examine
                  -> IO String -- ^ the guessed MIME-type
getMIMETypeCommon uri =
    (\(URI arg1) -> withForeignPtr arg1 $ \argPtr1 ->gnome_vfs_get_mime_type_common argPtr1) uri >>= peekUTFString

-- | Try to get the MIME-type of the file represented by @uri@. This
-- function looks only at the filename pointed to by @uri@.
getMIMETypeFromURI :: URI -- ^ @uri@ - the URI to examine
                   -> IO String -- ^ the guessed MIME-type
getMIMETypeFromURI uri =
    (\(URI arg1) -> withForeignPtr arg1 $ \argPtr1 ->gnome_vfs_get_mime_type_from_uri argPtr1) uri >>= peekUTFString

getFileMIMETypeFast :: FilePath -- ^
                    -> IO String
getFileMIMETypeFast path =
    withUTFString path $ \cPath ->
        gnome_vfs_get_file_mime_type_fast cPath nullPtr >>=
        peekUTFString

-- | Try to guess the MIME-type of the file represented by @path@. If
-- @suffixOnly@ is 'False', use the MIME-magic based lookup
-- first. Handles non-existant files by returning a type based on
-- the file extension.
getFileMIMEType :: FilePath
                -> Bool
                -> IO String
getFileMIMEType path suffixOnly =
    withUTFString path $ \cPath ->
        let cSuffixOnly = fromBool suffixOnly
        in gnome_vfs_get_file_mime_type cPath nullPtr cSuffixOnly >>=
               peekUTFString

-- | Returns 'True' if @mimeType@ is of the form @foo\/\*@, and 'False'
-- otherwise.
mimeTypeIsSupertype :: String
                    -> Bool
mimeTypeIsSupertype mimeType =
    toBool $ unsafePerformIO $
        withUTFString mimeType gnome_vfs_mime_type_is_supertype
{-# LINE 125 "./System/Gnome/VFS/MIME.chs" #-}

-- | Returns the supertype for @mimeType@. The supertype of an
-- application is computed by removing its suffix, and replacing it
-- with @\*@. Thus, @foo\/bar@ will be converted to @foo\/\*@.
getSupertypeFromMIMEType :: String
                         -> String
getSupertypeFromMIMEType mimeType =
    unsafePerformIO $ withUTFString mimeType gnome_vfs_get_supertype_from_mime_type >>=
        readUTFString

-- | Reload the MIME information for the specified directory.
mimeInfoCacheReload :: FilePath
                    -> IO ()
mimeInfoCacheReload dir =
    withUTFString dir gnome_vfs_mime_info_cache_reload
{-# LINE 140 "./System/Gnome/VFS/MIME.chs" #-}

-- | Reload the MIME database.
mimeReload :: IO ()
mimeReload = gnome_vfs_mime_reload
{-# LINE 144 "./System/Gnome/VFS/MIME.chs" #-}

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

foreign import ccall safe "gnome_vfs_get_mime_type_common"
  gnome_vfs_get_mime_type_common :: ((Ptr URI) -> (IO (Ptr CChar)))

foreign import ccall safe "gnome_vfs_get_mime_type_from_uri"
  gnome_vfs_get_mime_type_from_uri :: ((Ptr URI) -> (IO (Ptr CChar)))

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

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

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

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

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

foreign import ccall safe "gnome_vfs_mime_reload"
  gnome_vfs_mime_reload :: (IO ())