{-# LINE 2 "./System/GIO/File/ContentType.chs" #-}
-- GIMP Toolkit (GTK) Binding for Haskell: binding to gio -*-haskell-*-
--
-- Author : Andy Stewart
-- Created: 30-Apirl-2010
--
-- Copyright (c) 2010 Andy Stewart
--
-- 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:
--
-- GIO, 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 GIO documentation.
--
-- | Maintainer : gtk2hs-devel@lists.sourceforge.net
-- Stability : alpha
-- Portability : portable (depends on GHC)
module System.GIO.File.ContentType (
-- * Details
--
-- | A content type is a platform specific string that defines the type of a file. On 1 it is a mime
-- type, on win32 it is an extension string like ".doc", ".txt" or a perceived string like
-- "audio". Such strings can be looked up in the registry at HkeyClassesRoot.

-- * Methods
    contentTypeEquals,
    contentTypeIsA,
    contentTypeIsUnknown,
    contentTypeGetDescription,
    contentTypeGetMimeType,
    contentTypeGetIcon,
    contentTypeCanBeExecutable,

    contentTypeFromMimeType,

    contentTypeGuess,

    contentTypeGuessForTree,

    contentTypesGetRegistered,
    ) where

import Control.Monad
import System.GIO.Enums
import System.Glib.FFI
import System.Glib.Flags
import System.Glib.GError
import System.Glib.GList
import System.Glib.GObject
import System.Glib.UTFString
import System.GIO.Types
{-# LINE 64 "./System/GIO/File/ContentType.chs" #-}


{-# LINE 66 "./System/GIO/File/ContentType.chs" #-}

-- | Compares two content types for equality.
contentTypeEquals ::
    GlibString string
 => string
 -> string
 -> Bool -- ^ returns 'True' if the two strings are identical or equivalent, 'False' otherwise.
contentTypeEquals :: forall string. GlibString string => string -> string -> Bool
contentTypeEquals string
type1 string
type2 =
  CInt -> Bool
forall a. (Eq a, Num a) => a -> Bool
toBool (CInt -> Bool) -> CInt -> Bool
forall a b. (a -> b) -> a -> b
$ IO CInt -> CInt
forall a. IO a -> a
unsafePerformIO (IO CInt -> CInt) -> IO CInt -> CInt
forall a b. (a -> b) -> a -> b
$
  string -> (CString -> IO CInt) -> IO CInt
forall a. string -> (CString -> IO a) -> IO a
forall s a. GlibString s => s -> (CString -> IO a) -> IO a
withUTFString string
type1 ((CString -> IO CInt) -> IO CInt)
-> (CString -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \ CString
type1Ptr ->
  string -> (CString -> IO CInt) -> IO CInt
forall a. string -> (CString -> IO a) -> IO a
forall s a. GlibString s => s -> (CString -> IO a) -> IO a
withUTFString string
type2 ((CString -> IO CInt) -> IO CInt)
-> (CString -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \ CString
type2Ptr ->
  CString -> CString -> IO CInt
g_content_type_equals CString
type1Ptr CString
type2Ptr

-- | Determines if type is a subset of supertype.
contentTypeIsA ::
    GlibString string
 => string
 -> string
 -> Bool -- ^ returns 'True' if type is a kind of supertype, 'False' otherwise.
contentTypeIsA :: forall string. GlibString string => string -> string -> Bool
contentTypeIsA string
type1 string
supertype =
  CInt -> Bool
forall a. (Eq a, Num a) => a -> Bool
toBool (CInt -> Bool) -> CInt -> Bool
forall a b. (a -> b) -> a -> b
$ IO CInt -> CInt
forall a. IO a -> a
unsafePerformIO (IO CInt -> CInt) -> IO CInt -> CInt
forall a b. (a -> b) -> a -> b
$
  string -> (CString -> IO CInt) -> IO CInt
forall a. string -> (CString -> IO a) -> IO a
forall s a. GlibString s => s -> (CString -> IO a) -> IO a
withUTFString string
type1 ((CString -> IO CInt) -> IO CInt)
-> (CString -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \ CString
type1Ptr ->
  string -> (CString -> IO CInt) -> IO CInt
forall a. string -> (CString -> IO a) -> IO a
forall s a. GlibString s => s -> (CString -> IO a) -> IO a
withUTFString string
supertype ((CString -> IO CInt) -> IO CInt)
-> (CString -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \ CString
supertypePtr ->
  CString -> CString -> IO CInt
g_content_type_equals CString
type1Ptr CString
supertypePtr

-- | Checks if the content type is the generic "unknown" type. On 1 this is the
-- "application/octet-stream" mimetype, while on win32 it is \"*\".
contentTypeIsUnknown ::
    GlibString string
 => string
 -> Bool -- ^ returns 'True' if the type is the unknown type.
contentTypeIsUnknown :: forall string. GlibString string => string -> Bool
contentTypeIsUnknown string
typ =
  CInt -> Bool
forall a. (Eq a, Num a) => a -> Bool
toBool (CInt -> Bool) -> CInt -> Bool
forall a b. (a -> b) -> a -> b
$ IO CInt -> CInt
forall a. IO a -> a
unsafePerformIO (IO CInt -> CInt) -> IO CInt -> CInt
forall a b. (a -> b) -> a -> b
$
  string -> (CString -> IO CInt) -> IO CInt
forall a. string -> (CString -> IO a) -> IO a
forall s a. GlibString s => s -> (CString -> IO a) -> IO a
withUTFString string
typ ((CString -> IO CInt) -> IO CInt)
-> (CString -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \ CString
typPtr ->
  CString -> IO CInt
g_content_type_is_unknown CString
typPtr

-- | Gets the human readable description of the content type.
contentTypeGetDescription ::
    GlibString string
 => string
 -> string -- ^ returns a short description of the content type type.
contentTypeGetDescription :: forall string. GlibString string => string -> string
contentTypeGetDescription string
typ =
  IO string -> string
forall a. IO a -> a
unsafePerformIO (IO string -> string) -> IO string -> string
forall a b. (a -> b) -> a -> b
$
  string -> (CString -> IO string) -> IO string
forall a. string -> (CString -> IO a) -> IO a
forall s a. GlibString s => s -> (CString -> IO a) -> IO a
withUTFString string
typ ((CString -> IO string) -> IO string)
-> (CString -> IO string) -> IO string
forall a b. (a -> b) -> a -> b
$ \ CString
typPtr ->
  CString -> IO CString
g_content_type_get_description CString
typPtr
  IO CString -> (CString -> IO string) -> IO string
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CString -> IO string
forall s. GlibString s => CString -> IO s
readUTFString

-- | Gets the mime-type for the content type. If one is registered
contentTypeGetMimeType ::
    GlibString string
 => string
 -> string -- ^ returns the registered mime-type for the given type, or 'Nothing' if unknown.
contentTypeGetMimeType :: forall string. GlibString string => string -> string
contentTypeGetMimeType string
typ =
  IO string -> string
forall a. IO a -> a
unsafePerformIO (IO string -> string) -> IO string -> string
forall a b. (a -> b) -> a -> b
$
  string -> (CString -> IO string) -> IO string
forall a. string -> (CString -> IO a) -> IO a
forall s a. GlibString s => s -> (CString -> IO a) -> IO a
withUTFString string
typ ((CString -> IO string) -> IO string)
-> (CString -> IO string) -> IO string
forall a b. (a -> b) -> a -> b
$ \ CString
typPtr ->
  CString -> IO CString
g_content_type_get_mime_type CString
typPtr
  IO CString -> (CString -> IO string) -> IO string
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CString -> IO string
forall s. GlibString s => CString -> IO s
readUTFString

-- | Gets the icon for a content type.
contentTypeGetIcon ::
    GlibString string
 => string
 -> Icon -- ^ returns 'Icon' corresponding to the content type.
contentTypeGetIcon :: forall string. GlibString string => string -> Icon
contentTypeGetIcon string
typ =
  IO Icon -> Icon
forall a. IO a -> a
unsafePerformIO (IO Icon -> Icon) -> IO Icon -> Icon
forall a b. (a -> b) -> a -> b
$ (ForeignPtr Icon -> Icon, FinalizerPtr Icon)
-> IO (Ptr Icon) -> IO Icon
forall obj.
GObjectClass obj =>
(ForeignPtr obj -> obj, FinalizerPtr obj) -> IO (Ptr obj) -> IO obj
wrapNewGObject (ForeignPtr Icon -> Icon, FinalizerPtr Icon)
forall {a}. (ForeignPtr Icon -> Icon, FinalizerPtr a)
mkIcon (IO (Ptr Icon) -> IO Icon) -> IO (Ptr Icon) -> IO Icon
forall a b. (a -> b) -> a -> b
$
  string -> (CString -> IO (Ptr Icon)) -> IO (Ptr Icon)
forall a. string -> (CString -> IO a) -> IO a
forall s a. GlibString s => s -> (CString -> IO a) -> IO a
withUTFString string
typ ((CString -> IO (Ptr Icon)) -> IO (Ptr Icon))
-> (CString -> IO (Ptr Icon)) -> IO (Ptr Icon)
forall a b. (a -> b) -> a -> b
$ \ CString
typPtr ->
  CString -> IO (Ptr Icon)
g_content_type_get_icon CString
typPtr

-- | Checks if a content type can be executable. Note that for instance things like text files can be
-- executables (i.e. scripts and batch files).
contentTypeCanBeExecutable ::
    GlibString string
 => string
 -> Bool -- ^ returns 'True' if the file type corresponds to a type that can be executable, 'False' otherwise.
contentTypeCanBeExecutable :: forall string. GlibString string => string -> Bool
contentTypeCanBeExecutable string
typ =
  CInt -> Bool
forall a. (Eq a, Num a) => a -> Bool
toBool (CInt -> Bool) -> CInt -> Bool
forall a b. (a -> b) -> a -> b
$ IO CInt -> CInt
forall a. IO a -> a
unsafePerformIO (IO CInt -> CInt) -> IO CInt -> CInt
forall a b. (a -> b) -> a -> b
$
  string -> (CString -> IO CInt) -> IO CInt
forall a. string -> (CString -> IO a) -> IO a
forall s a. GlibString s => s -> (CString -> IO a) -> IO a
withUTFString string
typ ((CString -> IO CInt) -> IO CInt)
-> (CString -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \ CString
typPtr ->
  CString -> IO CInt
g_content_type_can_be_executable CString
typPtr


-- | Tries to find a content type based on the mime type name.
contentTypeFromMimeType ::
    GlibString string
 => string -- ^ @mimeType@ a mime type string.
 -> string
contentTypeFromMimeType :: forall string. GlibString string => string -> string
contentTypeFromMimeType string
mimeType =
  IO string -> string
forall a. IO a -> a
unsafePerformIO (IO string -> string) -> IO string -> string
forall a b. (a -> b) -> a -> b
$
  string -> (CString -> IO string) -> IO string
forall a. string -> (CString -> IO a) -> IO a
forall s a. GlibString s => s -> (CString -> IO a) -> IO a
withUTFString string
mimeType ((CString -> IO string) -> IO string)
-> (CString -> IO string) -> IO string
forall a b. (a -> b) -> a -> b
$ \ CString
mimeTypePtr ->
  CString -> IO CString
g_content_type_from_mime_type CString
mimeTypePtr
  IO CString -> (CString -> IO string) -> IO string
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CString -> IO string
forall s. GlibString s => CString -> IO s
readUTFString


-- | Guesses the content type based on example data. If the function is uncertain, @resultUncertain@ will
-- be set to 'True'. Either filename or data may be 'Nothing', in which case the guess will be based solely on
-- the other argument.
contentTypeGuess ::
    (GlibFilePath fp, GlibString string)
 => fp
 -> string -- ^ @data@ a stream of data,
 -> Int -- ^ @dataSize@ the size of data
 -> IO (Bool, string) -- ^ returns a string indicating a guessed content type for the given data.
contentTypeGuess :: forall fp string.
(GlibFilePath fp, GlibString string) =>
fp -> string -> Int -> IO (Bool, string)
contentTypeGuess fp
filename string
dat Int
dataSize =
  fp -> (CString -> IO (Bool, string)) -> IO (Bool, string)
forall a. fp -> (CString -> IO a) -> IO a
forall fp a. GlibFilePath fp => fp -> (CString -> IO a) -> IO a
withUTFFilePath fp
filename ((CString -> IO (Bool, string)) -> IO (Bool, string))
-> (CString -> IO (Bool, string)) -> IO (Bool, string)
forall a b. (a -> b) -> a -> b
$ \ CString
filenamePtr ->
  string -> (CString -> IO (Bool, string)) -> IO (Bool, string)
forall a. string -> (CString -> IO a) -> IO a
forall s a. GlibString s => s -> (CString -> IO a) -> IO a
withUTFString string
dat ((CString -> IO (Bool, string)) -> IO (Bool, string))
-> (CString -> IO (Bool, string)) -> IO (Bool, string)
forall a b. (a -> b) -> a -> b
$ \ CString
datPtr ->
  (Ptr Bool -> IO (Bool, string)) -> IO (Bool, string)
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr Bool -> IO (Bool, string)) -> IO (Bool, string))
-> (Ptr Bool -> IO (Bool, string)) -> IO (Bool, string)
forall a b. (a -> b) -> a -> b
$ \ Ptr Bool
resultUncertainPtr -> do
  CString
strPtr <- CString -> Ptr CUChar -> CULong -> Ptr CInt -> IO CString
g_content_type_guess
{-# LINE 172 "./System/GIO/File/ContentType.chs" #-}
           CString
filenamePtr
           (CString -> Ptr CUChar
forall a b. Ptr a -> Ptr b
castPtr CString
datPtr)
           (Int -> CULong
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
dataSize)
           (Ptr Bool -> Ptr CInt
forall a b. Ptr a -> Ptr b
castPtr Ptr Bool
resultUncertainPtr)
  Bool
resultUncertain <- Ptr Bool -> IO Bool
forall a. Storable a => Ptr a -> IO a
peek Ptr Bool
resultUncertainPtr
  string
str <- CString -> IO string
forall s. GlibString s => CString -> IO s
readUTFString CString
strPtr
  (Bool, string) -> IO (Bool, string)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
resultUncertain, string
str)


-- | Tries to guess the type of the tree with root root, by looking at the files it contains. The result
-- is an array of content types, with the best guess coming first.
--
-- The types returned all have the form x-content/foo, e.g. x-content/audio-cdda (for audio CDs) or
-- x-content/image-dcf (for a camera memory card). See the shared-mime-info specification for more on
-- x-content types.
--
-- This function is useful in the implementation of 'mountGuessContentType'.
contentTypeGuessForTree ::
    (FileClass file, GlibString string)
 => file -- ^ @root@ the root of the tree to guess a type for
 -> IO [string] -- ^ returns a list of possible content types
contentTypeGuessForTree :: forall file string.
(FileClass file, GlibString string) =>
file -> IO [string]
contentTypeGuessForTree file
root =
  (\(File ForeignPtr File
arg1) -> ForeignPtr File
-> (Ptr File -> IO (Ptr CString)) -> IO (Ptr CString)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr File
arg1 ((Ptr File -> IO (Ptr CString)) -> IO (Ptr CString))
-> (Ptr File -> IO (Ptr CString)) -> IO (Ptr CString)
forall a b. (a -> b) -> a -> b
$ \Ptr File
argPtr1 ->Ptr File -> IO (Ptr CString)
g_content_type_guess_for_tree Ptr File
argPtr1) (file -> File
forall o. FileClass o => o -> File
toFile file
root)
  IO (Ptr CString) -> (Ptr CString -> IO [string]) -> IO [string]
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Ptr CString -> IO [string]
forall s. GlibString s => Ptr CString -> IO [s]
readUTFStringArray0


-- | Gets a list of strings containing all the registered content types known to the system.
contentTypesGetRegistered ::
    GlibString string
 => IO [string] -- ^ returns GList of the registered content types.
contentTypesGetRegistered :: forall string. GlibString string => IO [string]
contentTypesGetRegistered = do
  Ptr ()
glistPtr <- IO (Ptr ())
g_content_types_get_registered
{-# LINE 204 "./System/GIO/File/ContentType.chs" #-}
  strPtrs <- fromGList glistPtr
  (CString -> IO string) -> [CString] -> IO [string]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM CString -> IO string
forall s. GlibString s => CString -> IO s
readUTFString [CString]
strPtrs

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

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

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

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

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

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

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

foreign import ccall safe "g_content_type_guess"
  g_content_type_guess :: ((Ptr CChar) -> ((Ptr CUChar) -> (CULong -> ((Ptr CInt) -> (IO (Ptr CChar))))))

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

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