-- GENERATED by C->Haskell Compiler, version 0.16.0 Crystal Seed, 24 Jan 2009 (Haskell)
-- Edit the ORIGNAL .chs file instead!


{-# LINE 1 "./Gnome/Keyring/Item.chs" #-}-- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>
-- 
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- any later version.
-- 
-- This program 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 General Public License for more details.
-- 
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
-- 
-- |
-- Maintainer  : John Millikin <jmillikin@gmail.com>
-- Stability   : experimental
-- Portability : non-portable (FFI)
-- 
-- A keyring contains multiple items. Each item has a secret, attributes and
-- access information associated with it.
-- 
-- An item is identified by an 'ItemID' unique to the keyring in which it
-- exists. An item's name is for displaying to the user. Each item has a
-- single secret, which is Unicode text. This secret is stored in
-- non-pageable memory in the server, and encrypted on disk. All of this
-- information is exposed via 'ItemInfo' values.
-- 
-- Note that the underlying C library stores secrets in non-pageable memory,
-- but the Haskell bindings currently do not.
--
-- Attributes allow various other pieces of information to be associated
-- with an item. These can also be used to search for relevant items.
--
-- Each item has an access control list, which specifies which applications
-- may read, write or delete an item. The read access applies only to
-- reading the secret. All applications can read other parts of the item.
-- ACLs are accessed and changed through 'AccessControl' values.
-- 
{-# LANGUAGE ForeignFunctionInterface #-}

{-# LINE 43 "./Gnome/Keyring/Item.chs" #-}

module Gnome.Keyring.Item
	(
	-- * Items
	  ItemID (..)
	, ItemType (..)
	
	, itemCreate
	, itemDelete
	
	-- * Item attributes
	-- $attribute-doc
	, Attribute (..)
	, attributeName
	, itemGetAttributes
	, itemSetAttributes
	
	-- * Item info
	, ItemInfoFlag (..)
	, ItemInfo (..)
	, itemGetInfo
	, itemGetInfoFull
	, itemSetInfo
	
	-- * Access control
	-- $access-control-doc
	, AccessControl (..)
	, AccessType (..)
	, itemGetACL
	, itemSetACL
	, itemGrantAccessRights
	
	-- * Searching for items
	, FoundItem (..)
	, findItems
	) where

import Control.Exception (bracket)
import Data.Set (Set, toList, fromList)
import Data.Text.Lazy (Text)

import Gnome.Keyring.ItemInfo
import Gnome.Keyring.Internal.FFI
import Gnome.Keyring.Internal.Operation
import Gnome.Keyring.Internal.Types

data ItemInfoFlag
	= ItemInfoBasics
	| ItemInfoSecret
	deriving (Show, Eq)

cItemID :: Integral a => ItemID -> a
cItemID (ItemID x) = fromIntegral x

cItemInfoFlags :: Bits a => Set ItemInfoFlag -> a
cItemInfoFlags = foldr (.|.) 0 . map flagValue . toList where
	flagValue ItemInfoBasics = 0
	flagValue ItemInfoSecret = 1

-- | Create a new item in a keyring.
-- 
-- The user may have been prompted to unlock necessary keyrings. If 'Nothing'
-- is specified as the keyring and no default keyring exists, the user will
-- be prompted to create a new keyring.
-- 
-- If an existing item should be updated, the user may be prompted for access
-- to the existing item.
-- 
-- Whether a new item is created or not, the ID of the item will be returned.
-- 
itemCreate :: Maybe KeyringName
           -> ItemType
           -> Text -- ^ Display name
           -> [Attribute]
           -> Text -- ^ The secret
           -> Bool -- ^ Update an existing item, if one exists.
           -> Operation ItemID
itemCreate k t dn as s u = itemIDOperation
	(item_create k t dn as s u)
	(item_create_sync k t dn as s u)

item_create :: Maybe Text -> ItemType -> Text -> [Attribute] -> Text -> Bool -> GetIntCallbackPtr -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
item_create a1 a2 a3 a4 a5 a6 a7 a8 a9 =
  withNullableText a1 $ \a1' -> 
  let {a2' = fromItemType a2} in 
  withText a3 $ \a3' -> 
  withAttributeList a4 $ \a4' -> 
  withText a5 $ \a5' -> 
  let {a6' = fromBool a6} in 
  let {a7' = id a7} in 
  let {a8' = id a8} in 
  let {a9' = id a9} in 
  item_create'_ a1' a2' a3' a4' a5' a6' a7' a8' a9' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')
{-# LINE 135 "./Gnome/Keyring/Item.chs" #-}

item_create_sync :: Maybe Text -> ItemType -> Text -> [Attribute] -> Text -> Bool -> IO (Result, ItemID)
item_create_sync a1 a2 a3 a4 a5 a6 =
  withNullableText a1 $ \a1' -> 
  let {a2' = fromItemType a2} in 
  withText a3 $ \a3' -> 
  withAttributeList a4 $ \a4' -> 
  withText a5 $ \a5' -> 
  let {a6' = fromBool a6} in 
  alloca $ \a7' -> 
  item_create_sync'_ a1' a2' a3' a4' a5' a6' a7' >>= \res ->
  peekItemID a7'>>= \a7'' -> 
  let {res' = result res} in
  return (res', a7'')
{-# LINE 145 "./Gnome/Keyring/Item.chs" #-}

-- | Delete an item in a keyring.
-- 
-- The user may be prompted if the calling application doesn't have
-- necessary access to delete the item.
-- 
itemDelete :: Maybe KeyringName -> ItemID -> Operation ()
itemDelete k item = voidOperation
	(item_delete k item)
	(item_delete_sync k item)

item_delete :: Maybe Text -> ItemID -> DoneCallbackPtr -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
item_delete a1 a2 a3 a4 a5 =
  withNullableText a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  let {a3' = id a3} in 
  let {a4' = id a4} in 
  let {a5' = id a5} in 
  item_delete'_ a1' a2' a3' a4' a5' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')
{-# LINE 163 "./Gnome/Keyring/Item.chs" #-}

item_delete_sync :: Maybe Text -> ItemID -> IO ((Result, ()))
item_delete_sync a1 a2 =
  withNullableText a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  item_delete_sync'_ a1' a2' >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')
{-# LINE 168 "./Gnome/Keyring/Item.chs" #-}

-- | Get information about an item and its secret.
-- 
-- The user may be prompted if the calling application doesn't have
-- necessary access to read the item with its secret.
-- 
itemGetInfo :: Maybe KeyringName -> ItemID -> Operation ItemInfo
itemGetInfo k item = itemInfoOperation
	(item_get_info k item)
	(item_get_info_sync k item)

item_get_info :: Maybe Text -> ItemID -> GetItemInfoCallbackPtr -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
item_get_info a1 a2 a3 a4 a5 =
  withNullableText a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  let {a3' = id a3} in 
  let {a4' = id a4} in 
  let {a5' = id a5} in 
  item_get_info'_ a1' a2' a3' a4' a5' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')
{-# LINE 186 "./Gnome/Keyring/Item.chs" #-}

item_get_info_sync :: Maybe Text -> ItemID -> IO (Result, ItemInfo)
item_get_info_sync a1 a2 =
  withNullableText a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  alloca $ \a3' -> 
  item_get_info_sync'_ a1' a2' a3' >>= \res ->
  stealItemInfo a3'>>= \a3'' -> 
  let {res' = result res} in
  return (res', a3'')
{-# LINE 192 "./Gnome/Keyring/Item.chs" #-}

-- | Get information about an item, optionally retrieving its secret.
-- 
-- If the flags include 'ItemInfoSecret', then the user may be prompted if
-- the calling application doesn't have necessary access to read the item
-- with its secret.
-- 
itemGetInfoFull :: Maybe KeyringName -> ItemID -> Set ItemInfoFlag
                -> Operation ItemInfo
itemGetInfoFull k item flags = itemInfoOperation
	(item_get_info_full k item flags)
	(item_get_info_full_sync k item flags)

item_get_info_full :: Maybe Text -> ItemID -> Set ItemInfoFlag -> GetItemInfoCallbackPtr -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
item_get_info_full a1 a2 a3 a4 a5 a6 =
  withNullableText a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  let {a3' = cItemInfoFlags a3} in 
  let {a4' = id a4} in 
  let {a5' = id a5} in 
  let {a6' = id a6} in 
  item_get_info_full'_ a1' a2' a3' a4' a5' a6' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')
{-# LINE 213 "./Gnome/Keyring/Item.chs" #-}

item_get_info_full_sync :: Maybe Text -> ItemID -> Set ItemInfoFlag -> IO (Result, ItemInfo)
item_get_info_full_sync a1 a2 a3 =
  withNullableText a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  let {a3' = cItemInfoFlags a3} in 
  alloca $ \a4' -> 
  item_get_info_full_sync'_ a1' a2' a3' a4' >>= \res ->
  stealItemInfo a4'>>= \a4'' -> 
  let {res' = result res} in
  return (res', a4'')
{-# LINE 220 "./Gnome/Keyring/Item.chs" #-}

-- | Set information on an item, like its display name, secret, etc.
-- 
-- Only the fields in the info info which are non-'Nothing' or non-zero
-- will be set on the item.
-- 
itemSetInfo :: Maybe KeyringName -> ItemID -> ItemInfo -> Operation ()
itemSetInfo k item info = voidOperation
	(item_set_info k item info)
	(item_set_info_sync k item info)

item_set_info :: Maybe Text -> ItemID -> ItemInfo -> DoneCallbackPtr -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
item_set_info a1 a2 a3 a4 a5 a6 =
  withNullableText a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  withItemInfo a3 $ \a3' -> 
  let {a4' = id a4} in 
  let {a5' = id a5} in 
  let {a6' = id a6} in 
  item_set_info'_ a1' a2' a3' a4' a5' a6' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')
{-# LINE 239 "./Gnome/Keyring/Item.chs" #-}

item_set_info_sync :: Maybe Text -> ItemID -> ItemInfo -> IO ((Result, ()))
item_set_info_sync a1 a2 a3 =
  withNullableText a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  withItemInfo a3 $ \a3' -> 
  item_set_info_sync'_ a1' a2' a3' >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')
{-# LINE 245 "./Gnome/Keyring/Item.chs" #-}

-- $attribute-doc
-- Attributes allow various other pieces of information to be associated
-- with an item. These can also be used to search for relevant items. Use
-- 'itemGetAttributes' or 'itemSetAttributes' to manipulate attributes in
-- the keyring.
-- 
-- Each attribute is either Unicode text, or an unsigned 32-bit integer.

data AttributeType = ATTRIBUTE_TYPE_STRING
                   | ATTRIBUTE_TYPE_UINT32
                   deriving (Enum)

{-# LINE 255 "./Gnome/Keyring/Item.chs" #-}

data Attribute
	= TextAttribute Text Text
	| WordAttribute Text Word32
	deriving (Show, Eq)

attributeName :: Attribute -> Text
attributeName (TextAttribute n _) = n
attributeName (WordAttribute n _) = n

withAttributeList :: [Attribute] -> (Ptr () -> IO a) -> IO a
withAttributeList attrs io = bracket newList freeAttributeList buildList where
	newList = g_array_new 0 0 12
{-# LINE 268 "./Gnome/Keyring/Item.chs" #-}
	buildList list = mapM_ (append list) attrs >> io list
	append list (TextAttribute n x) = appendString list n x
	append list (WordAttribute n x) = appendUInt32 list n x

appendString :: Ptr () -> Text -> Text -> IO (())
appendString a1 a2 a3 =
  let {a1' = id a1} in 
  withText a2 $ \a2' -> 
  withText a3 $ \a3' -> 
  appendString'_ a1' a2' a3' >>= \res ->
  let {res' = id res} in
  return (res')
{-# LINE 277 "./Gnome/Keyring/Item.chs" #-}

appendUInt32 :: Ptr () -> Text -> Word32 -> IO (())
appendUInt32 a1 a2 a3 =
  let {a1' = id a1} in 
  withText a2 $ \a2' -> 
  let {a3' = fromIntegral a3} in 
  appendUInt32'_ a1' a2' a3' >>= \res ->
  let {res' = id res} in
  return (res')
{-# LINE 283 "./Gnome/Keyring/Item.chs" #-}

peekAttribute :: Ptr () -> IO Attribute
peekAttribute attr = do
	name <- peekText =<< (\ptr -> do {peekByteOff ptr 0 ::IO (Ptr CChar)}) attr
	cType <- (\ptr -> do {peekByteOff ptr 4 ::IO CInt}) attr
	case toEnum . fromIntegral $ cType of
		ATTRIBUTE_TYPE_STRING -> do
			value <- peekText =<< (\ptr -> do {peekByteOff ptr 8 ::IO (Ptr CChar)}) attr
			return $ TextAttribute name value
		ATTRIBUTE_TYPE_UINT32 -> do
			cValue <- (\ptr -> do {peekByteOff ptr 8 ::IO CUInt}) attr
			return $ WordAttribute name $ fromIntegral cValue

peekAttributeList :: Ptr () -> IO [Attribute]
peekAttributeList = mapGArray peekAttribute 12
{-# LINE 298 "./Gnome/Keyring/Item.chs" #-}

stealAttributeList :: Ptr (Ptr ()) -> IO [Attribute]
stealAttributeList ptr = bracket (peek ptr) freeAttributeList peekAttributeList

freeAttributeList :: Ptr () -> IO ()
freeAttributeList = gnome_keyring_attribute_list_free
{-# LINE 304 "./Gnome/Keyring/Item.chs" #-}

type GetAttributesCallback = CInt -> Ptr () -> Ptr () -> IO ()
type GetAttributesCallbackPtr = FunPtr ((CInt -> ((Ptr ()) -> ((Ptr ()) -> (IO ())))))
{-# LINE 307 "./Gnome/Keyring/Item.chs" #-}
foreign import ccall "wrapper"
	wrapGetAttributesCallback :: GetAttributesCallback -> IO GetAttributesCallbackPtr

attributeListOperation :: OperationImpl GetAttributesCallback [Attribute]
attributeListOperation = operationImpl $ \checkResult ->
	wrapGetAttributesCallback $ \cres array _ ->
	checkResult cres $ peekAttributeList array

-- | Get all the attributes for an item.
-- 
itemGetAttributes :: Maybe KeyringName -> ItemID -> Operation [Attribute]
itemGetAttributes k item = attributeListOperation
	(item_get_attributes k item)
	(item_get_attributes_sync k item)

item_get_attributes :: Maybe Text -> ItemID -> GetAttributesCallbackPtr -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
item_get_attributes a1 a2 a3 a4 a5 =
  withNullableText a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  let {a3' = id a3} in 
  let {a4' = id a4} in 
  let {a5' = id a5} in 
  item_get_attributes'_ a1' a2' a3' a4' a5' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')
{-# LINE 329 "./Gnome/Keyring/Item.chs" #-}

item_get_attributes_sync :: Maybe Text -> ItemID -> IO (Result, [Attribute])
item_get_attributes_sync a1 a2 =
  withNullableText a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  alloca $ \a3' -> 
  item_get_attributes_sync'_ a1' a2' a3' >>= \res ->
  stealAttributeList a3'>>= \a3'' -> 
  let {res' = result res} in
  return (res', a3'')
{-# LINE 335 "./Gnome/Keyring/Item.chs" #-}

-- | Set all the attributes for an item. These will replace any previous
-- attributes set on the item.
-- 
itemSetAttributes :: Maybe KeyringName -> ItemID -> [Attribute] -> Operation ()
itemSetAttributes k item as = voidOperation
	(item_set_attributes k item as)
	(item_set_attributes_sync k item as)

item_set_attributes :: Maybe Text -> ItemID -> [Attribute] -> DoneCallbackPtr -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
item_set_attributes a1 a2 a3 a4 a5 a6 =
  withNullableText a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  withAttributeList a3 $ \a3' -> 
  let {a4' = id a4} in 
  let {a5' = id a5} in 
  let {a6' = id a6} in 
  item_set_attributes'_ a1' a2' a3' a4' a5' a6' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')
{-# LINE 352 "./Gnome/Keyring/Item.chs" #-}

item_set_attributes_sync :: Maybe Text -> ItemID -> [Attribute] -> IO ((Result, ()))
item_set_attributes_sync a1 a2 a3 =
  withNullableText a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  withAttributeList a3 $ \a3' -> 
  item_set_attributes_sync'_ a1' a2' a3' >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')
{-# LINE 358 "./Gnome/Keyring/Item.chs" #-}

-- $access-control-doc
-- Each item has an access control list, which specifies which applications
-- may read, write or delete an item. The read access applies only to reading
-- the secret. All applications can read other parts of the item. ACLs are
-- accessed and changed with 'itemGetACL' and 'itemSetACL'.

data RawAccessType = ACCESS_READ
                   | ACCESS_WRITE
                   | ACCESS_REMOVE
                   deriving (Show)
instance Enum RawAccessType where
  fromEnum ACCESS_READ = 1
  fromEnum ACCESS_WRITE = 2
  fromEnum ACCESS_REMOVE = 4

  toEnum 1 = ACCESS_READ
  toEnum 2 = ACCESS_WRITE
  toEnum 4 = ACCESS_REMOVE
  toEnum unmatched = error ("RawAccessType.toEnum: Cannot match " ++ show unmatched)

{-# LINE 366 "./Gnome/Keyring/Item.chs" #-}

data AccessType
	= AccessRead
	| AccessWrite
	| AccessRemove
	deriving (Show, Eq, Ord)

data AccessControl = AccessControl
	{ accessControlName :: Maybe Text
	, accessControlPath :: Maybe Text
	, accessControlType :: Set AccessType
	}
	deriving (Show, Eq)

peekAccessControl :: Ptr () -> IO AccessControl
peekAccessControl ac = do
	name <- stealNullableText =<< gnome_keyring_item_ac_get_display_name ac
	path <- stealNullableText =<< gnome_keyring_item_ac_get_path_name ac
	cType <- gnome_keyring_item_ac_get_access_type ac
	return $ AccessControl name path $ peekAccessType cType

stealACL :: Ptr (Ptr ()) -> IO [AccessControl]
stealACL ptr = bracket (peek ptr) freeACL (mapGList peekAccessControl)

withACL :: [AccessControl] -> (Ptr () -> IO a) -> IO a
withACL acl = bracket (buildACL acl) freeACL

buildACL :: [AccessControl] -> IO (Ptr ())
buildACL acs = bracket
	gnome_keyring_application_ref_new
{-# LINE 396 "./Gnome/Keyring/Item.chs" #-}
	gnome_keyring_application_ref_free $ \appRef ->
	buildACL' appRef acs nullPtr

buildACL' :: Ptr () -> [AccessControl] -> Ptr () -> IO (Ptr ())
buildACL'      _       [] list = return list
buildACL' appRef (ac:acs) list = buildAC appRef ac
	>>= g_list_append list
	>>= buildACL' appRef acs

buildAC :: Ptr () -> AccessControl -> IO (Ptr ())
buildAC appRef ac = do
	let cAllowed = cAccessTypes $ accessControlType ac
	ptr <- gnome_keyring_access_control_new appRef cAllowed
	withNullableText (accessControlName ac) $ gnome_keyring_item_ac_set_display_name ptr
	withNullableText (accessControlPath ac) $ gnome_keyring_item_ac_set_path_name ptr
	return ptr

freeACL :: Ptr () -> IO ()
freeACL = gnome_keyring_acl_free
{-# LINE 415 "./Gnome/Keyring/Item.chs" #-}

cAccessTypes :: Bits a => Set AccessType -> a
cAccessTypes = foldr (.|.) 0 . map (fromIntegral . fromEnum . fromAccessType) . toList where

peekAccessType :: Integral a => a -> Set AccessType
peekAccessType cint = fromList $ concat
	[ [AccessRead   | int .&. fromEnum ACCESS_READ   > 0]
	, [AccessWrite  | int .&. fromEnum ACCESS_WRITE  > 0]
	, [AccessRemove | int .&. fromEnum ACCESS_REMOVE > 0]
	]
	where int = fromIntegral cint

fromAccessType :: AccessType -> RawAccessType
fromAccessType AccessRead   = ACCESS_READ
fromAccessType AccessWrite  = ACCESS_WRITE
fromAccessType AccessRemove = ACCESS_REMOVE

accessControlListOperation :: OperationImpl GetListCallback [AccessControl]
accessControlListOperation = operationImpl $ \checkResult ->
	wrapGetListCallback $ \cres list _ ->
	checkResult cres $ mapGList peekAccessControl list

-- | Get the access control list for an item.
-- 
itemGetACL :: Maybe KeyringName -> ItemID -> Operation [AccessControl]
itemGetACL k item = accessControlListOperation
	(item_get_acl k item)
	(item_get_acl_sync k item)

item_get_acl :: Maybe Text -> ItemID -> GetListCallbackPtr -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
item_get_acl a1 a2 a3 a4 a5 =
  withNullableText a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  let {a3' = id a3} in 
  let {a4' = id a4} in 
  let {a5' = id a5} in 
  item_get_acl'_ a1' a2' a3' a4' a5' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')
{-# LINE 451 "./Gnome/Keyring/Item.chs" #-}

item_get_acl_sync :: Maybe Text -> ItemID -> IO (Result, [AccessControl])
item_get_acl_sync a1 a2 =
  withNullableText a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  alloca $ \a3' -> 
  item_get_acl_sync'_ a1' a2' a3' >>= \res ->
  stealACL a3'>>= \a3'' -> 
  let {res' = result res} in
  return (res', a3'')
{-# LINE 457 "./Gnome/Keyring/Item.chs" #-}

-- | Set the full access control list on an item. This replaces any previous
-- ACL set on the item.
-- 
itemSetACL :: Maybe KeyringName -> ItemID -> [AccessControl] -> Operation ()
itemSetACL k item acl = voidOperation
	(item_set_acl k item acl)
	(item_set_acl_sync k item acl)

item_set_acl :: Maybe Text -> ItemID -> [AccessControl] -> DoneCallbackPtr -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
item_set_acl a1 a2 a3 a4 a5 a6 =
  withNullableText a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  withACL a3 $ \a3' -> 
  let {a4' = id a4} in 
  let {a5' = id a5} in 
  let {a6' = id a6} in 
  item_set_acl'_ a1' a2' a3' a4' a5' a6' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')
{-# LINE 474 "./Gnome/Keyring/Item.chs" #-}

item_set_acl_sync :: Maybe Text -> ItemID -> [AccessControl] -> IO ((Result, ()))
item_set_acl_sync a1 a2 a3 =
  withNullableText a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  withACL a3 $ \a3' -> 
  item_set_acl_sync'_ a1' a2' a3' >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')
{-# LINE 480 "./Gnome/Keyring/Item.chs" #-}

-- | Will grant the application access rights to the item, provided callee
-- has write access to said item.
-- 
-- This is similar to performing 'itemGetACL' and 'itemSetACL' with
-- appropriate parameters.
-- 
itemGrantAccessRights :: Maybe KeyringName
                      -> Text -- ^ Display name
                      -> Text -- ^ Application executable path
                      -> ItemID
                      -> Set AccessType
                      -> Operation ()
itemGrantAccessRights k d p item r = voidOperation
	(item_grant_access_rights k d p item r)
	(item_grant_access_rights_sync k d p item r)

item_grant_access_rights :: Maybe Text -> Text -> Text -> ItemID -> Set AccessType -> DoneCallbackPtr -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
item_grant_access_rights a1 a2 a3 a4 a5 a6 a7 a8 =
  withNullableText a1 $ \a1' -> 
  withText a2 $ \a2' -> 
  withText a3 $ \a3' -> 
  let {a4' = cItemID a4} in 
  let {a5' = cAccessTypes a5} in 
  let {a6' = id a6} in 
  let {a7' = id a7} in 
  let {a8' = id a8} in 
  item_grant_access_rights'_ a1' a2' a3' a4' a5' a6' a7' a8' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')
{-# LINE 507 "./Gnome/Keyring/Item.chs" #-}

item_grant_access_rights_sync :: Maybe Text -> Text -> Text -> ItemID -> Set AccessType -> IO ((Result, ()))
item_grant_access_rights_sync a1 a2 a3 a4 a5 =
  withNullableText a1 $ \a1' -> 
  withText a2 $ \a2' -> 
  withText a3 $ \a3' -> 
  let {a4' = cItemID a4} in 
  let {a5' = cAccessTypes a5} in 
  item_grant_access_rights_sync'_ a1' a2' a3' a4' a5' >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')
{-# LINE 515 "./Gnome/Keyring/Item.chs" #-}

data FoundItem = FoundItem
	{ foundItemKeyring    :: KeyringName
	, foundItemID         :: ItemID
	, foundItemAttributes :: [Attribute]
	, foundItemSecret     :: Text
	}
	deriving (Show, Eq)

peekFound :: Ptr () -> IO FoundItem
peekFound f = do
	keyring <- peekText =<< (\ptr -> do {peekByteOff ptr 0 ::IO (Ptr CChar)}) f
	itemID <- (\ptr -> do {peekByteOff ptr 4 ::IO CUInt}) f
	attrs <- peekAttributeList =<< (\ptr -> do {peekByteOff ptr 8 ::IO (Ptr ())}) f
	secret <- peekText =<< (\ptr -> do {peekByteOff ptr 12 ::IO (Ptr CChar)}) f
	let itemID' = ItemID $ fromIntegral itemID
	return $ FoundItem keyring itemID' attrs secret

stealFoundList :: Ptr (Ptr ()) -> IO [FoundItem]
stealFoundList ptr = bracket (peek ptr)
	gnome_keyring_found_list_free
{-# LINE 536 "./Gnome/Keyring/Item.chs" #-}
	(mapGList peekFound)

foundItemsOperation :: OperationImpl GetListCallback [FoundItem]
foundItemsOperation = operationImpl $ \checkResult ->
	wrapGetListCallback $ \cres list _ ->
	checkResult cres $ (mapGList peekFound) list

-- | Searches through all keyrings for items that match the attributes. The
-- matches are for exact equality.
-- 
-- The user may be prompted to unlock necessary keyrings, and will be
-- prompted for access to the items if needed.
-- 
findItems :: ItemType -> [Attribute] -> Operation [FoundItem]
findItems t as = foundItemsOperation
	(find_items t as)
	(find_items_sync t as)

find_items :: ItemType -> [Attribute] -> GetListCallbackPtr -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
find_items a1 a2 a3 a4 a5 =
  let {a1' = fromItemType a1} in 
  withAttributeList a2 $ \a2' -> 
  let {a3' = id a3} in 
  let {a4' = id a4} in 
  let {a5' = id a5} in 
  find_items'_ a1' a2' a3' a4' a5' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')
{-# LINE 561 "./Gnome/Keyring/Item.chs" #-}

find_items_sync :: ItemType -> [Attribute] -> IO (Result, [FoundItem])
find_items_sync a1 a2 =
  let {a1' = fromItemType a1} in 
  withAttributeList a2 $ \a2' -> 
  alloca $ \a3' -> 
  find_items_sync'_ a1' a2' a3' >>= \res ->
  stealFoundList a3'>>= \a3'' -> 
  let {res' = result res} in
  return (res', a3'')
{-# LINE 567 "./Gnome/Keyring/Item.chs" #-}

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_create"
  item_create'_ :: ((Ptr CChar) -> (CInt -> ((Ptr CChar) -> ((Ptr ()) -> ((Ptr CChar) -> (CInt -> ((FunPtr (CInt -> (CUInt -> ((Ptr ()) -> (IO ()))))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ())))))))))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_create_sync"
  item_create_sync'_ :: ((Ptr CChar) -> (CInt -> ((Ptr CChar) -> ((Ptr ()) -> ((Ptr CChar) -> (CInt -> ((Ptr CUInt) -> (IO CInt))))))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_delete"
  item_delete'_ :: ((Ptr CChar) -> (CUInt -> ((FunPtr (CInt -> ((Ptr ()) -> (IO ())))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ())))))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_delete_sync"
  item_delete_sync'_ :: ((Ptr CChar) -> (CUInt -> (IO CInt)))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_get_info"
  item_get_info'_ :: ((Ptr CChar) -> (CUInt -> ((FunPtr (CInt -> ((Ptr ()) -> ((Ptr ()) -> (IO ()))))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ())))))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_get_info_sync"
  item_get_info_sync'_ :: ((Ptr CChar) -> (CUInt -> ((Ptr (Ptr ())) -> (IO CInt))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_get_info_full"
  item_get_info_full'_ :: ((Ptr CChar) -> (CUInt -> (CUInt -> ((FunPtr (CInt -> ((Ptr ()) -> ((Ptr ()) -> (IO ()))))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ()))))))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_get_info_full_sync"
  item_get_info_full_sync'_ :: ((Ptr CChar) -> (CUInt -> (CUInt -> ((Ptr (Ptr ())) -> (IO CInt)))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_set_info"
  item_set_info'_ :: ((Ptr CChar) -> (CUInt -> ((Ptr ()) -> ((FunPtr (CInt -> ((Ptr ()) -> (IO ())))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ()))))))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_set_info_sync"
  item_set_info_sync'_ :: ((Ptr CChar) -> (CUInt -> ((Ptr ()) -> (IO CInt))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h g_array_new"
  g_array_new :: (CInt -> (CInt -> (CUInt -> (IO (Ptr ())))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_attribute_list_append_string"
  appendString'_ :: ((Ptr ()) -> ((Ptr CChar) -> ((Ptr CChar) -> (IO ()))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_attribute_list_append_uint32"
  appendUInt32'_ :: ((Ptr ()) -> ((Ptr CChar) -> (CUInt -> (IO ()))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_attribute_list_free"
  gnome_keyring_attribute_list_free :: ((Ptr ()) -> (IO ()))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_get_attributes"
  item_get_attributes'_ :: ((Ptr CChar) -> (CUInt -> ((GetAttributesCallbackPtr) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ())))))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_get_attributes_sync"
  item_get_attributes_sync'_ :: ((Ptr CChar) -> (CUInt -> ((Ptr (Ptr ())) -> (IO CInt))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_set_attributes"
  item_set_attributes'_ :: ((Ptr CChar) -> (CUInt -> ((Ptr ()) -> ((FunPtr (CInt -> ((Ptr ()) -> (IO ())))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ()))))))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_set_attributes_sync"
  item_set_attributes_sync'_ :: ((Ptr CChar) -> (CUInt -> ((Ptr ()) -> (IO CInt))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_ac_get_display_name"
  gnome_keyring_item_ac_get_display_name :: ((Ptr ()) -> (IO (Ptr CChar)))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_ac_get_path_name"
  gnome_keyring_item_ac_get_path_name :: ((Ptr ()) -> (IO (Ptr CChar)))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_ac_get_access_type"
  gnome_keyring_item_ac_get_access_type :: ((Ptr ()) -> (IO CInt))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_application_ref_new"
  gnome_keyring_application_ref_new :: (IO (Ptr ()))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_application_ref_free"
  gnome_keyring_application_ref_free :: ((Ptr ()) -> (IO ()))

foreign import ccall safe "Gnome/Keyring/Item.chs.h g_list_append"
  g_list_append :: ((Ptr ()) -> ((Ptr ()) -> (IO (Ptr ()))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_access_control_new"
  gnome_keyring_access_control_new :: ((Ptr ()) -> (CInt -> (IO (Ptr ()))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_ac_set_display_name"
  gnome_keyring_item_ac_set_display_name :: ((Ptr ()) -> ((Ptr CChar) -> (IO ())))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_ac_set_path_name"
  gnome_keyring_item_ac_set_path_name :: ((Ptr ()) -> ((Ptr CChar) -> (IO ())))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_acl_free"
  gnome_keyring_acl_free :: ((Ptr ()) -> (IO ()))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_get_acl"
  item_get_acl'_ :: ((Ptr CChar) -> (CUInt -> ((FunPtr (CInt -> ((Ptr ()) -> ((Ptr ()) -> (IO ()))))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ())))))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_get_acl_sync"
  item_get_acl_sync'_ :: ((Ptr CChar) -> (CUInt -> ((Ptr (Ptr ())) -> (IO CInt))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_set_acl"
  item_set_acl'_ :: ((Ptr CChar) -> (CUInt -> ((Ptr ()) -> ((FunPtr (CInt -> ((Ptr ()) -> (IO ())))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ()))))))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_set_acl_sync"
  item_set_acl_sync'_ :: ((Ptr CChar) -> (CUInt -> ((Ptr ()) -> (IO CInt))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_grant_access_rights"
  item_grant_access_rights'_ :: ((Ptr CChar) -> ((Ptr CChar) -> ((Ptr CChar) -> (CUInt -> (CInt -> ((FunPtr (CInt -> ((Ptr ()) -> (IO ())))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ()))))))))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_item_grant_access_rights_sync"
  item_grant_access_rights_sync'_ :: ((Ptr CChar) -> ((Ptr CChar) -> ((Ptr CChar) -> (CUInt -> (CInt -> (IO CInt))))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_found_list_free"
  gnome_keyring_found_list_free :: ((Ptr ()) -> (IO ()))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_find_items"
  find_items'_ :: (CInt -> ((Ptr ()) -> ((FunPtr (CInt -> ((Ptr ()) -> ((Ptr ()) -> (IO ()))))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ())))))))

foreign import ccall safe "Gnome/Keyring/Item.chs.h gnome_keyring_find_items_sync"
  find_items_sync'_ :: (CInt -> ((Ptr ()) -> ((Ptr (Ptr ())) -> (IO CInt))))