{-# LINE 2 "./System/GIO/Icons/Icon.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.Icons.Icon (
-- * Details
--
-- | 'Icon' is a very minimal interface for icons. It provides functions for checking the equality of two
-- icons, hashing of icons and serializing an icon to and from strings.
--
-- 'Icon' does not provide the actual pixmap for the icon as this is out of GIO's scope, however
-- implementations of 'Icon' may contain the name of an icon (see 'ThemedIcon'), or the path to an icon
-- (see GLoadableIcon).
--
-- To obtain a hash of a 'Icon', see 'iconHash'.
--
-- To check if two 'Icon's are equal, see 'iconEqual'.
--
-- For serializing a 'Icon', use 'iconToString'.
--
-- If your application or library provides one or more 'Icon' implementations you need to ensure that
-- each GType is registered with the type system prior to calling 'iconNewForString'.

-- * Types
    Icon(..),
    IconClass,

-- * Methods
    iconEqual,

    iconToString,
    iconNewForString,

    ) where

import Control.Monad
import Data.ByteString (ByteString)
import Data.ByteString (useAsCString)
import Data.ByteString.Unsafe (unsafePackCStringFinalizer)
import System.GIO.Enums
import System.Glib.FFI
import System.Glib.Flags
import System.Glib.GError
import System.Glib.GObject
import System.Glib.UTFString
import System.GIO.Types
{-# LINE 72 "./System/GIO/Icons/Icon.chs" #-}


{-# LINE 74 "./System/GIO/Icons/Icon.chs" #-}

-- | Checks if two icons are equal.
iconEqual :: (IconClass icon1, IconClass icon2) => icon1 -> icon2
 -> IO Bool -- ^ returns 'True' if icon1 is equal to icon2. 'False' otherwise.
iconEqual icon1 icon2 =
   liftM toBool $
   (\(Icon arg1) (Icon arg2) -> withForeignPtr arg1 $ \argPtr1 ->withForeignPtr arg2 $ \argPtr2 ->g_icon_equal argPtr1 argPtr2) (toIcon icon1) (toIcon icon2)


-- | Generates a textual representation of icon that can be used for serialization such as when passing
-- icon to a different process or saving it to persistent storage. Use 'iconNewForString' to get
-- icon back from the returned string.
--
-- The encoding of the returned string is proprietary to 'Icon' except in the following two cases
--
-- * If icon is a 'FileIcon', the returned string is a native path (such as /path/to/my icon.png)
-- without escaping if the 'File' for icon is a native file. If the file is not native, the returned
-- string is the result of 'fileGetUri'.
--
-- * If icon is a 'ThemedIcon' with exactly one name, the encoding is simply the name (such as
-- network-server).
iconToString :: IconClass icon => icon
 -> IO ByteString
iconToString icon = do
   sPtr <- (\(Icon arg1) -> withForeignPtr arg1 $ \argPtr1 ->g_icon_to_string argPtr1) (toIcon icon)
   sLen <- lengthArray0 0 sPtr
   unsafePackCStringFinalizer (castPtr sPtr) (fromIntegral sLen)
        (g_free (castPtr sPtr))

-- | Generate a 'Icon' instance from str. This function can fail if str is not valid - see
-- 'iconToString' for discussion.
--
-- If your application or library provides one or more 'Icon' implementations you need to ensure that
-- each GType is registered with the type system prior to calling 'iconNewForString'.
iconNewForString :: ByteString -> IO Icon
iconNewForString str =
    wrapNewGObject mkIcon $
    useAsCString str $ \ strPtr ->
    propagateGError (g_icon_new_for_string strPtr)

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

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

foreign import ccall unsafe "g_free"
  g_free :: ((Ptr ()) -> (IO ()))

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