-- GENERATED by C->Haskell Compiler, version 0.18.2 The shapeless maps, 31 Oct 2014 (Haskell)
-- Edit the ORIGNAL .chs file instead!


{-# LINE 1 "lib/Gnome/Keyring.chs" #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ForeignFunctionInterface #-}

-- Copyright (C) 2009-2012 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)
--
-- The GNOME Keyring is a service for securely storing per-user secret
-- information, such as passwords and encryption keys. This library is
-- a binding to the @libgnome-keyring@ C library.
--
-- Documentation for the original library is available at
-- <http://library.gnome.org/devel/gnome-keyring/stable/>
module Gnome.Keyring
	(
	
	-- * Service status
	  available
	
	-- * Items
	-- $item-doc
	, ItemID
	, ItemType(..)
	, createItem
	, deleteItem
	, listItemIDs
	
	-- ** Item info
	, Item
	, getItem
	, setItem
	, itemType
	, itemSecret
	, itemDisplayName
	, itemModified
	, itemCreated
	
	-- ** Item attributes
	, Attribute (..)
	, attributeName
	, getItemAttributes
	, setItemAttributes
	
	-- ** Access control
	, Access (..)
	, AccessType (..)
	, getItemAccess
	, setItemAccess
	, grantItemAccess
	
	-- ** Searching for items
	, FoundItem
	, foundItemKeyring
	, foundItemID
	, foundItemAttributes
	, foundItemSecret
	, findItems
	
	-- * Keyrings
	, Keyring
	, defaultKeyring
	, sessionKeyring
	, keyring
	
	-- ** Basic operations
	, getDefaultKeyring
	, setDefaultKeyring
	, listKeyringNames
	, createKeyring
	, deleteKeyring
	, changeKeyringPassword
	
	-- ** Locking and unlocking keyrings
	, lockKeyring
	, unlockKeyring
	, lockAll
	
	-- ** Keyring information
	, KeyringInfo
	, keyringLockOnIdle
	, keyringLockTimeout
	, keyringModified
	, keyringCreated
	, keyringIsLocked
	, getKeyringInfo
	, setKeyringInfo
	
	-- * Network passwords
	, NetworkPassword
	, networkPasswordKeyring
	, networkPasswordSecret
	, networkPasswordItemID
	, networkPasswordNetwork
	
	, Network
	, network
	, networkProtocol
	, networkServer
	, networkObject
	, networkAuthType
	, networkPort
	, networkUser
	, networkDomain
	
	, findNetworkPassword
	, setNetworkPassword
	
	-- * Operations
	, Operation
	, KeyringError
	, keyringErrorMessage
	, sync
	, sync_
	
	, async
	, CancellationKey
	, cancel
	) where

import           Control.Exception (Exception, bracket, throwIO)
import           Control.Monad (join)
import           Data.Time (UTCTime)
import           Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import           Data.Typeable (Typeable)
import           Foreign hiding (unsafePerformIO)
import           Foreign.C
import           System.IO.Unsafe (unsafePerformIO)

import qualified Data.ByteString as ByteString
import qualified Data.Text
import           Data.Text (Text)
import           Data.Text.Encoding (encodeUtf8, decodeUtf8)



{-# LINE 151 "lib/Gnome/Keyring.chs" #-}


-- | Check whether the client can communicate with the GNOME Keyring service.
available :: IO ((Bool))
available =
  available'_ >>= \res ->
  let {res' = toBool res} in
  return (res')

{-# LINE 155 "lib/Gnome/Keyring.chs" #-}


-- $item-doc
-- 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 'Item' 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 'Access' values.

newtype ItemID = ItemID CUInt
	deriving (Show, Eq, Ord)

data ItemType
	= ItemGenericSecret
	| ItemNetworkPassword
	| ItemNote
	| ItemChainedKeyringPassword
	| ItemEncryptionKeyPassword
	| ItemPublicKeyStorage
	| ItemApplicationSecret
	deriving (Show, Eq)

data Item = Item
	{
	
	-- | Get or set the item's type.
	  itemType :: ItemType
	
	-- | Get or set the item's secret.
	, itemSecret :: Maybe String
	
	-- | Get or set the item's display name.
	, itemDisplayName :: Maybe String
	
	, itemMTime :: UTCTime
	, itemCTime :: UTCTime
	}
	deriving (Show, Eq)

-- | Get when the item was last modified.
itemModified :: Item -> UTCTime
itemModified = itemMTime

-- | Get when the item was created.
itemCreated :: Item -> UTCTime
itemCreated = itemCTime

fromItemType :: ItemType -> CInt
fromItemType ItemGenericSecret = 0
fromItemType ItemNetworkPassword = 1
fromItemType ItemNote = 2
fromItemType ItemChainedKeyringPassword = 3
fromItemType ItemEncryptionKeyPassword = 4
fromItemType ItemPublicKeyStorage = 0x100
fromItemType ItemApplicationSecret = 0x01000000

toItemType :: CInt -> ItemType
toItemType 0 = ItemGenericSecret
toItemType 1 = ItemNetworkPassword
toItemType 2 = ItemNote
toItemType 3 = ItemChainedKeyringPassword
toItemType 4 = ItemEncryptionKeyPassword
toItemType 0x100 = ItemPublicKeyStorage
toItemType 0x01000000 = ItemApplicationSecret
toItemType _ = ItemGenericSecret

peekItemInfo :: Ptr () -> IO Item
peekItemInfo info = do
	cType <- gnome_keyring_item_info_get_type info
	secret <- stealNullableUtf8 =<< gnome_keyring_item_info_get_secret info
	name <- stealNullableUtf8 =<< gnome_keyring_item_info_get_display_name info
	mtime <- cToUTC `fmap` gnome_keyring_item_info_get_mtime info
	ctime <- cToUTC `fmap` gnome_keyring_item_info_get_ctime info
	return (Item (toItemType cType) secret name mtime ctime)

stealItemInfo :: Ptr (Ptr ()) -> IO Item
stealItemInfo ptr = bracket (peek ptr) freeItemInfo peekItemInfo

freeItemInfo :: Ptr () -> IO ()
freeItemInfo = gnome_keyring_item_info_free
{-# LINE 248 "lib/Gnome/Keyring.chs" #-}


foreign import ccall "gnome-keyring.h &gnome_keyring_item_info_free"
	finalizeItemInfo :: FunPtr (Ptr a -> IO ())

withItemInfo :: Item -> (Ptr () -> IO a) -> IO a
withItemInfo info io = do
	fptr <- newForeignPtr finalizeItemInfo =<< gnome_keyring_item_info_new
{-# LINE 255 "lib/Gnome/Keyring.chs" #-}

	withForeignPtr fptr $ \ptr -> do
	gnome_keyring_item_info_set_type ptr (fromItemType (itemType info))
	withNullableUtf8 (itemSecret info) (gnome_keyring_item_info_set_secret ptr)
	withNullableUtf8 (itemDisplayName info) (gnome_keyring_item_info_set_display_name ptr)
	io ptr

type GetItemInfoCallback = CInt -> Ptr () -> Ptr () -> IO ()
type GetItemInfoCallbackPtr = FunPtr ((CInt -> ((Ptr ()) -> ((Ptr ()) -> (IO ())))))
{-# LINE 263 "lib/Gnome/Keyring.chs" #-}

foreign import ccall "wrapper"
	wrapGetItemInfoCallback :: GetItemInfoCallback -> IO GetItemInfoCallbackPtr

itemIDOperation :: OperationImpl GetIntCallback ItemID
itemIDOperation = operationImpl $ \checkResult ->
	wrapGetIntCallback $ \cres cint _ ->
	checkResult cres (return (ItemID cint))

itemInfoOperation :: OperationImpl GetItemInfoCallback Item
itemInfoOperation = operationImpl $ \checkResult ->
	wrapGetItemInfoCallback $ \cres ptr _ ->
	checkResult cres (peekItemInfo ptr)

peekItemID :: Ptr CUInt -> IO ItemID
peekItemID ptr = fmap ItemID (peek ptr)

cItemID :: ItemID -> CUInt
cItemID (ItemID x) = x

-- | Create a new item in a keyring.
--
-- The user may have been prompted to unlock necessary keyrings. If
-- 'defaultKeyring' 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.
createItem :: Keyring
           -> ItemType
           -> String -- ^ Display name
           -> [Attribute]
           -> String -- ^ The secret
           -> Bool -- ^ Update an existing item, if one exists.
           -> Operation ItemID
createItem 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 :: (Keyring) -> (ItemType) -> (String) -> ([Attribute]) -> (String) -> (Bool) -> (GetIntCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
item_create a1 a2 a3 a4 a5 a6 a7 a8 a9 =
  withKeyringName a1 $ \a1' -> 
  let {a2' = fromItemType a2} in 
  withUtf8 a3 $ \a3' -> 
  withAttributeList a4 $ \a4' -> 
  withUtf8 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 314 "lib/Gnome/Keyring.chs" #-}


item_create_sync :: (Keyring) -> (ItemType) -> (String) -> ([Attribute]) -> (String) -> (Bool) -> IO ((Result), (ItemID))
item_create_sync a1 a2 a3 a4 a5 a6 =
  withKeyringName a1 $ \a1' -> 
  let {a2' = fromItemType a2} in 
  withUtf8 a3 $ \a3' -> 
  withAttributeList a4 $ \a4' -> 
  withUtf8 a5 $ \a5' -> 
  let {a6' = fromBool a6} in 
  alloca $ \a7' -> 
  item_create_sync'_ a1' a2' a3' a4' a5' a6' a7' >>= \res ->
  let {res' = Result res} in
  peekItemID  a7'>>= \a7'' -> 
  return (res', a7'')

{-# LINE 324 "lib/Gnome/Keyring.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.
deleteItem :: Keyring -> ItemID -> Operation ()
deleteItem k item = voidOperation
	(item_delete k item)
	(item_delete_sync k item)

item_delete :: (Keyring) -> (ItemID) -> (DoneCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
item_delete a1 a2 a3 a4 a5 =
  withKeyringName 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 341 "lib/Gnome/Keyring.chs" #-}


item_delete_sync :: (Keyring) -> (ItemID) -> IO (((Result, ())))
item_delete_sync a1 a2 =
  withKeyringName a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  item_delete_sync'_ a1' a2' >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')

{-# LINE 346 "lib/Gnome/Keyring.chs" #-}


-- | Get a list of all the IDs for items in the keyring. All items which are
-- not flagged as 'ItemApplicationSecret' are included in the list. This
-- includes items that the calling application may not (yet) have access to.
listItemIDs :: Keyring -> Operation [ItemID]
listItemIDs name = itemIDListOperation
	(list_item_ids name)
	(list_item_ids_sync name)

list_item_ids :: (Keyring) -> (GetListCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
list_item_ids a1 a2 a3 a4 =
  withKeyringName a1 $ \a1' -> 
  let {a2' = id a2} in 
  let {a3' = id a3} in 
  let {a4' = id a4} in 
  list_item_ids'_ a1' a2' a3' a4' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')

{-# LINE 361 "lib/Gnome/Keyring.chs" #-}


list_item_ids_sync :: (Keyring) -> IO ((Result), ([ItemID]))
list_item_ids_sync a1 =
  withKeyringName a1 $ \a1' -> 
  alloca $ \a2' -> 
  list_item_ids_sync'_ a1' a2' >>= \res ->
  let {res' = Result res} in
  stealItemIDList  a2'>>= \a2'' -> 
  return (res', a2'')

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


itemIDListOperation :: OperationImpl GetListCallback [ItemID]
itemIDListOperation = operationImpl $ \checkResult ->
	wrapGetListCallback $ \cres ptr _ ->
	checkResult cres (peekItemIDList ptr)

peekItemIDList :: Ptr () -> IO [ItemID]
peekItemIDList = mapGList (return . ItemID . fromIntegral . ptrToWordPtr)

stealItemIDList :: Ptr (Ptr ()) -> IO [ItemID]
stealItemIDList ptr = bracket (peek ptr) freeList peekItemIDList where
	freeList = g_list_free
{-# LINE 378 "lib/Gnome/Keyring.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.
getItem :: Keyring
        -> Bool -- ^ Whether to read the secret.
        -> ItemID
        -> Operation Item
getItem k includeSecret item = itemInfoOperation
	(item_get_info_full k item includeSecret)
	(item_get_info_full_sync k item includeSecret)

item_get_info_full :: (Keyring) -> (ItemID) -> (Bool) -> (GetItemInfoCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
item_get_info_full a1 a2 a3 a4 a5 a6 =
  withKeyringName 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 399 "lib/Gnome/Keyring.chs" #-}


item_get_info_full_sync :: (Keyring) -> (ItemID) -> (Bool) -> IO ((Result), (Item))
item_get_info_full_sync a1 a2 a3 =
  withKeyringName a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  let {a3' = cItemInfoFlags a3} in 
  alloca $ \a4' -> 
  item_get_info_full_sync'_ a1' a2' a3' a4' >>= \res ->
  let {res' = Result res} in
  stealItemInfo  a4'>>= \a4'' -> 
  return (res', a4'')

{-# LINE 406 "lib/Gnome/Keyring.chs" #-}


cItemInfoFlags :: Integral a => Bool -> a
cItemInfoFlags includeSecret = if includeSecret then 1 else 0

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

item_set_info :: (Keyring) -> (ItemID) -> (Item) -> (DoneCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
item_set_info a1 a2 a3 a4 a5 a6 =
  withKeyringName 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 427 "lib/Gnome/Keyring.chs" #-}


item_set_info_sync :: (Keyring) -> (ItemID) -> (Item) -> IO (((Result, ())))
item_set_info_sync a1 a2 a3 =
  withKeyringName 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 433 "lib/Gnome/Keyring.chs" #-}


data AttributeType = ATTRIBUTE_TYPE_STRING
                   | ATTRIBUTE_TYPE_UINT32
  deriving (Enum)

{-# LINE 435 "lib/Gnome/Keyring.chs" #-}


-- | Attributes allow various other pieces of information to be associated
-- with an item. These can also be used to search for relevant items. Use
-- 'getItemAttributes' or 'setItemAttributes' to manipulate attributes in
-- the keyring.
--
-- Each attribute is either Unicode text, or an unsigned 32-bit integer.
data Attribute
	= TextAttribute String String
	| WordAttribute String Word32
	deriving (Show, Eq)

attributeName :: Attribute -> String
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 24
{-# LINE 454 "lib/Gnome/Keyring.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 ()) -> (String) -> (String) -> IO ((()))
appendString a1 a2 a3 =
  let {a1' = id a1} in 
  withUtf8 a2 $ \a2' -> 
  withUtf8 a3 $ \a3' -> 
  appendString'_ a1' a2' a3' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 463 "lib/Gnome/Keyring.chs" #-}


appendUInt32 :: Ptr () -> String -> Word32 -> IO ()
appendUInt32 list name val = withUtf8 name (\name_ptr -> c_append_uint32 list name_ptr val)

foreign import ccall unsafe "gnome_keyring_attribute_list_append_uint32"
	c_append_uint32 :: Ptr () -> CString -> Word32 -> IO ()

peekAttribute :: Ptr () -> IO Attribute
peekAttribute attr = do
	name <- peekUtf8 =<< (\ptr -> do {peekByteOff ptr 0 ::IO (Ptr CChar)}) attr
	cType <- (\ptr -> do {peekByteOff ptr 8 ::IO CInt}) attr
	case cType of
		0 -> do
			value <- peekUtf8 =<< (\ptr -> do {peekByteOff ptr 16 ::IO (Ptr CChar)}) attr
			return (TextAttribute name value)
		1 -> do
			cValue <- (\ptr -> do {peekByteOff ptr 16 ::IO CUInt}) attr
			return (WordAttribute name (fromIntegral cValue))
		_ -> undefined

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


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

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


type GetAttributesCallback = CInt -> Ptr () -> Ptr () -> IO ()
type GetAttributesCallbackPtr = FunPtr ((CInt -> ((Ptr ()) -> ((Ptr ()) -> (IO ())))))
{-# LINE 494 "lib/Gnome/Keyring.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.
getItemAttributes :: Keyring -> ItemID -> Operation [Attribute]
getItemAttributes k item = attributeListOperation
	(item_get_attributes k item)
	(item_get_attributes_sync k item)

item_get_attributes :: (Keyring) -> (ItemID) -> (GetAttributesCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
item_get_attributes a1 a2 a3 a4 a5 =
  withKeyringName 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 515 "lib/Gnome/Keyring.chs" #-}


item_get_attributes_sync :: (Keyring) -> (ItemID) -> IO ((Result), ([Attribute]))
item_get_attributes_sync a1 a2 =
  withKeyringName a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  alloca $ \a3' -> 
  item_get_attributes_sync'_ a1' a2' a3' >>= \res ->
  let {res' = Result res} in
  stealAttributeList  a3'>>= \a3'' -> 
  return (res', a3'')

{-# LINE 521 "lib/Gnome/Keyring.chs" #-}


-- | Set all the attributes for an item. These will replace any existing
-- attributes.
setItemAttributes :: Keyring -> ItemID -> [Attribute] -> Operation ()
setItemAttributes k item as = voidOperation
	(item_set_attributes k item as)
	(item_set_attributes_sync k item as)

item_set_attributes :: (Keyring) -> (ItemID) -> ([Attribute]) -> (DoneCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
item_set_attributes a1 a2 a3 a4 a5 a6 =
  withKeyringName 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 537 "lib/Gnome/Keyring.chs" #-}


item_set_attributes_sync :: (Keyring) -> (ItemID) -> ([Attribute]) -> IO (((Result, ())))
item_set_attributes_sync a1 a2 a3 =
  withKeyringName 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 543 "lib/Gnome/Keyring.chs" #-}


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

-- | 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 'getItemAccess' and 'setItemAccess'.
data Access = Access
	{ accessName :: Maybe String
	, accessPath :: Maybe String
	, accessType :: [AccessType]
	}
	deriving (Show, Eq)

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

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

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

buildACL :: [Access] -> IO (Ptr ())
buildACL acs = bracket
	gnome_keyring_application_ref_new
{-# LINE 577 "lib/Gnome/Keyring.chs" #-}

	gnome_keyring_application_ref_free $ \appRef ->
	buildACL' appRef acs nullPtr

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

buildAC :: Ptr () -> Access-> IO (Ptr ())
buildAC appRef ac = do
	let cAllowed = cAccessTypes (accessType ac)
	ptr <- gnome_keyring_access_control_new appRef cAllowed
	withNullableUtf8 (accessName ac) (gnome_keyring_item_ac_set_display_name ptr)
	withNullableUtf8 (accessPath ac) (gnome_keyring_item_ac_set_path_name ptr)
	return ptr

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


cAccessTypes :: [AccessType] -> CInt
cAccessTypes = foldr (.|.) 0 . map fromAccessType where
	fromAccessType :: AccessType -> CInt
	fromAccessType AccessRead   = 1
	fromAccessType AccessWrite  = 2
	fromAccessType AccessRemove = 4

peekAccessType :: CInt -> [AccessType]
peekAccessType cint = concat
	[ [AccessRead   | (cint .&. 1) > 0]
	, [AccessWrite  | (cint .&. 2) > 0]
	, [AccessRemove | (cint .&. 4) > 0]
	]

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

-- | Get the access control list for an item.
getItemAccess :: Keyring -> ItemID -> Operation [Access]
getItemAccess k item = accessControlListOperation
	(item_get_acl k item)
	(item_get_acl_sync k item)

item_get_acl :: (Keyring) -> (ItemID) -> (GetListCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
item_get_acl a1 a2 a3 a4 a5 =
  withKeyringName 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 629 "lib/Gnome/Keyring.chs" #-}


item_get_acl_sync :: (Keyring) -> (ItemID) -> IO ((Result), ([Access]))
item_get_acl_sync a1 a2 =
  withKeyringName a1 $ \a1' -> 
  let {a2' = cItemID a2} in 
  alloca $ \a3' -> 
  item_get_acl_sync'_ a1' a2' a3' >>= \res ->
  let {res' = Result res} in
  stealACL  a3'>>= \a3'' -> 
  return (res', a3'')

{-# LINE 635 "lib/Gnome/Keyring.chs" #-}


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

item_set_acl :: (Keyring) -> (ItemID) -> ([Access]) -> (DoneCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
item_set_acl a1 a2 a3 a4 a5 a6 =
  withKeyringName 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 651 "lib/Gnome/Keyring.chs" #-}


item_set_acl_sync :: (Keyring) -> (ItemID) -> ([Access]) -> IO (((Result, ())))
item_set_acl_sync a1 a2 a3 =
  withKeyringName 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 657 "lib/Gnome/Keyring.chs" #-}


-- | Will grant the application access rights to the item, provided callee
-- has write access to said item.
--
-- This is similar to performing 'getItemAccess' and 'setItemAccess' with
-- appropriate parameters.
grantItemAccess :: Keyring
                -> String -- ^ Display name
                -> String -- ^ Application executable path
                -> ItemID
                -> [AccessType]
                -> Operation ()
grantItemAccess 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 :: (Keyring) -> (String) -> (String) -> (ItemID) -> ([AccessType]) -> (DoneCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
item_grant_access_rights a1 a2 a3 a4 a5 a6 a7 a8 =
  withKeyringName a1 $ \a1' -> 
  withUtf8 a2 $ \a2' -> 
  withUtf8 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 683 "lib/Gnome/Keyring.chs" #-}


item_grant_access_rights_sync :: (Keyring) -> (String) -> (String) -> (ItemID) -> ([AccessType]) -> IO (((Result, ())))
item_grant_access_rights_sync a1 a2 a3 a4 a5 =
  withKeyringName a1 $ \a1' -> 
  withUtf8 a2 $ \a2' -> 
  withUtf8 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 691 "lib/Gnome/Keyring.chs" #-}


data FoundItem = FoundItem
	{ foundItemKeyring_ :: Keyring
	, foundItemID_ :: ItemID
	, foundItemAttributes_ :: [Attribute]
	, foundItemSecret_ :: String
	}
	deriving (Eq)

instance Show FoundItem where
	showsPrec d x = showParen (d > 10) $
		s "FoundItem " .
		s " {foundItemKeyring = " . shows (foundItemKeyring_ x) .
		s ", foundItemID = " . shows (foundItemID_ x) .
		s ", foundItemAttributes_= " . shows (foundItemAttributes_ x) .
		s ", foundItemSecret = " . shows (foundItemSecret_ x) .
		s "}"
		where s = showString

-- | Get which keyring the item was found in.
foundItemKeyring :: FoundItem -> Keyring
foundItemKeyring = foundItemKeyring_

-- | Get the found item's ID.
foundItemID :: FoundItem -> ItemID
foundItemID = foundItemID_

-- | Get the found item's attributes.
foundItemAttributes :: FoundItem -> [Attribute]
foundItemAttributes = foundItemAttributes_

-- | Get the found item's secret.
foundItemSecret :: FoundItem -> String
foundItemSecret = foundItemSecret_

peekFound :: Ptr () -> IO FoundItem
peekFound f = do
	keyringName <- peekUtf8 =<< (\ptr -> do {peekByteOff ptr 0 ::IO (Ptr CChar)}) f
	itemID <- (\ptr -> do {peekByteOff ptr 8 ::IO CUInt}) f
	attrs <- peekAttributeList =<< (\ptr -> do {peekByteOff ptr 16 ::IO (Ptr ())}) f
	secret <- peekUtf8 =<< (\ptr -> do {peekByteOff ptr 24 ::IO (Ptr CChar)}) f
	return (FoundItem (keyring keyringName) (ItemID itemID) attrs secret)

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

	(mapGList peekFound)

foundItemsOperation :: OperationImpl GetListCallback [FoundItem]
foundItemsOperation = operationImpl $ \checkResult ->
	wrapGetListCallback $ \cres list _ -> if cres == 9
		then checkResult 0 (return [])
		else 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.
--
-- Returns an empty list if no items were found.
findItems :: ItemType -> [Attribute] -> Operation [FoundItem]
findItems t as = foundItemsOperation
	(find_items t as)
	(do
		(rc, lst) <- find_items_sync t as
		return $ if rc == Result 9
			then (Result 0, [])
			else (rc, lst))

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 768 "lib/Gnome/Keyring.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 ->
  let {res' = Result res} in
  stealFoundList  a3'>>= \a3'' -> 
  return (res', a3'')

{-# LINE 774 "lib/Gnome/Keyring.chs" #-}


-- | GNOME Keyring manages multiple keyrings. Each keyring can store one or
-- more items, containing secrets.
--
-- Each keyring can be in a locked or unlocked state. A password must be
-- specified, either by the user or the calling application, to unlock the
-- keyring.
data Keyring
	= DefaultKeyring
	| NamedKeyring String
	deriving (Eq, Show)

defaultKeyring :: Keyring
defaultKeyring = DefaultKeyring

sessionKeyring :: Keyring
sessionKeyring = keyring "session"

keyring :: String -> Keyring
keyring = NamedKeyring

-- | Get the name of the default keyring. If no keyring is the default,
-- returns @Nothing@.
getDefaultKeyring :: Operation (Maybe String)
getDefaultKeyring = maybeStringOperation
	get_default_keyring
	get_default_keyring_sync

get_default_keyring :: (GetStringCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
get_default_keyring a1 a2 a3 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  let {a3' = id a3} in 
  get_default_keyring'_ a1' a2' a3' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')

{-# LINE 807 "lib/Gnome/Keyring.chs" #-}


get_default_keyring_sync :: IO ((Result), (Maybe String))
get_default_keyring_sync =
  alloca $ \a1' -> 
  get_default_keyring_sync'_ a1' >>= \res ->
  let {res' = Result res} in
  stealNullableUtf8Ptr  a1'>>= \a1'' -> 
  return (res', a1'')

{-# LINE 811 "lib/Gnome/Keyring.chs" #-}


stealNullableUtf8Ptr :: Ptr CString -> IO (Maybe String)
stealNullableUtf8Ptr ptr = bracket (peek ptr) free peekNullableUtf8

-- | Change the default keyring.
setDefaultKeyring :: String -> Operation ()
setDefaultKeyring k = voidOperation
	(set_default_keyring k)
	(set_default_keyring_sync k)

set_default_keyring :: (String) -> (DoneCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
set_default_keyring a1 a2 a3 a4 =
  withUtf8 a1 $ \a1' -> 
  let {a2' = id a2} in 
  let {a3' = id a3} in 
  let {a4' = id a4} in 
  set_default_keyring'_ a1' a2' a3' a4' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')

{-# LINE 827 "lib/Gnome/Keyring.chs" #-}


set_default_keyring_sync :: (String) -> IO (((Result, ())))
set_default_keyring_sync a1 =
  withUtf8 a1 $ \a1' -> 
  set_default_keyring_sync'_ a1' >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')

{-# LINE 831 "lib/Gnome/Keyring.chs" #-}


-- | Get a list of keyring names. If no keyrings exist, an empty list
-- will be returned.
listKeyringNames :: Operation [String]
listKeyringNames = stringListOperation
	list_keyring_names
	list_keyring_names_sync

list_keyring_names :: (GetListCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
list_keyring_names a1 a2 a3 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  let {a3' = id a3} in 
  list_keyring_names'_ a1' a2' a3' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')

{-# LINE 844 "lib/Gnome/Keyring.chs" #-}


list_keyring_names_sync :: IO ((Result), ([String]))
list_keyring_names_sync =
  alloca $ \a1' -> 
  list_keyring_names_sync'_ a1' >>= \res ->
  let {res' = Result res} in
  stealUtf8List  a1'>>= \a1'' -> 
  return (res', a1'')

{-# LINE 848 "lib/Gnome/Keyring.chs" #-}


stealUtf8List :: Ptr (Ptr ()) -> IO [String]
stealUtf8List ptr = bracket (peek ptr)
	gnome_keyring_string_list_free
{-# LINE 852 "lib/Gnome/Keyring.chs" #-}

	(mapGList peekUtf8)

-- | Create a new keyring with the specified name. In most cases, @Nothing@
-- will be passed as the password, which will prompt the user to enter a
-- password of their choice.
createKeyring :: String -- ^ Keyring name
             -> Maybe String -- ^ Keyring password, or @Nothing@ to prompt
                             -- the user.
             -> Operation ()
createKeyring k p = voidOperation (c_create k p) (create_sync k p)

c_create :: (String) -> (Maybe String) -> (DoneCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
c_create a1 a2 a3 a4 a5 =
  withUtf8 a1 $ \a1' -> 
  withNullableUtf8 a2 $ \a2' -> 
  let {a3' = id a3} in 
  let {a4' = id a4} in 
  let {a5' = id a5} in 
  c_create'_ a1' a2' a3' a4' a5' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')

{-# LINE 870 "lib/Gnome/Keyring.chs" #-}


create_sync :: (String) -> (Maybe String) -> IO (((Result, ())))
create_sync a1 a2 =
  withUtf8 a1 $ \a1' -> 
  withNullableUtf8 a2 $ \a2' -> 
  create_sync'_ a1' a2' >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')

{-# LINE 875 "lib/Gnome/Keyring.chs" #-}


-- | Delete a keyring. Once a keyring is deleted, there is no mechanism for
-- recovery of its contents.
deleteKeyring :: String -> Operation ()
deleteKeyring k = voidOperation (c_delete k) (delete_sync k)

c_delete :: (String) -> (DoneCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
c_delete a1 a2 a3 a4 =
  withUtf8 a1 $ \a1' -> 
  let {a2' = id a2} in 
  let {a3' = id a3} in 
  let {a4' = id a4} in 
  c_delete'_ a1' a2' a3' a4' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')

{-# LINE 887 "lib/Gnome/Keyring.chs" #-}


delete_sync :: (String) -> IO (((Result, ())))
delete_sync a1 =
  withUtf8 a1 $ \a1' -> 
  delete_sync'_ a1' >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')

{-# LINE 891 "lib/Gnome/Keyring.chs" #-}


-- | Lock a keyring, so that its contents may not be accessed without first
-- supplying a password.
--
-- Most keyring operations involving items require that the keyring first be
-- unlocked. One exception is 'findItems' and related computations.
lockKeyring :: Keyring -> Operation ()
lockKeyring k = voidOperation (c_lock k) (lock_sync k)

c_lock :: (Keyring) -> (DoneCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
c_lock a1 a2 a3 a4 =
  withKeyringName a1 $ \a1' -> 
  let {a2' = id a2} in 
  let {a3' = id a3} in 
  let {a4' = id a4} in 
  c_lock'_ a1' a2' a3' a4' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')

{-# LINE 906 "lib/Gnome/Keyring.chs" #-}


lock_sync :: (Keyring) -> IO (((Result, ())))
lock_sync a1 =
  withKeyringName a1 $ \a1' -> 
  lock_sync'_ a1' >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')

{-# LINE 910 "lib/Gnome/Keyring.chs" #-}


-- | Lock all the keyrings, so that their contents may not be accessed
-- without first unlocking them with a password.
lockAll :: Operation ()
lockAll = voidOperation lock_all lock_all_sync

lock_all :: (DoneCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
lock_all a1 a2 a3 =
  let {a1' = id a1} in 
  let {a2' = id a2} in 
  let {a3' = id a3} in 
  lock_all'_ a1' a2' a3' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')

{-# LINE 921 "lib/Gnome/Keyring.chs" #-}


lock_all_sync :: IO (((Result, ())))
lock_all_sync =
  lock_all_sync'_ >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')

{-# LINE 924 "lib/Gnome/Keyring.chs" #-}


-- | Unlock a keyring, so that its contents may be accessed. In most cases,
-- 'Nothing' will be specified as the password, which will prompt the user
-- to enter the correct password.
--
-- Most keyring operations involving items require that the keyring first be
-- unlocked. One exception is 'findItems' and related computations.
unlockKeyring :: Keyring -> Maybe String -> Operation ()
unlockKeyring k p = voidOperation (c_unlock k p) (unlock_sync k p)

c_unlock :: (Keyring ) -> (Maybe String) -> (DoneCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
c_unlock a1 a2 a3 a4 a5 =
  withKeyringName a1 $ \a1' -> 
  withNullableUtf8 a2 $ \a2' -> 
  let {a3' = id a3} in 
  let {a4' = id a4} in 
  let {a5' = id a5} in 
  c_unlock'_ a1' a2' a3' a4' a5' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')

{-# LINE 941 "lib/Gnome/Keyring.chs" #-}


unlock_sync :: (Keyring ) -> (Maybe String) -> IO (((Result, ())))
unlock_sync a1 a2 =
  withKeyringName a1 $ \a1' -> 
  withNullableUtf8 a2 $ \a2' -> 
  unlock_sync'_ a1' a2' >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')

{-# LINE 946 "lib/Gnome/Keyring.chs" #-}


-- | Get information about the keyring.
getKeyringInfo :: Keyring -> Operation KeyringInfo
getKeyringInfo k = keyringInfoOperation (get_info k) (get_info_sync k)

get_info :: (Keyring) -> (GetKeyringInfoCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
get_info a1 a2 a3 a4 =
  withKeyringName a1 $ \a1' -> 
  let {a2' = id a2} in 
  let {a3' = id a3} in 
  let {a4' = id a4} in 
  get_info'_ a1' a2' a3' a4' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')

{-# LINE 957 "lib/Gnome/Keyring.chs" #-}


get_info_sync :: (Keyring) -> IO ((Result), (KeyringInfo))
get_info_sync a1 =
  withKeyringName a1 $ \a1' -> 
  alloca $ \a2' -> 
  get_info_sync'_ a1' a2' >>= \res ->
  let {res' = Result res} in
  stealKeyringInfoPtr  a2'>>= \a2'' -> 
  return (res', a2'')

{-# LINE 962 "lib/Gnome/Keyring.chs" #-}


-- | Set flags and info for the keyring. The only fields in the
-- 'KeyringInfo' which may be modified are 'keyringLockOnIdle' and
-- 'keyringLockTimeout'.
setKeyringInfo :: Keyring -> KeyringInfo -> Operation ()
setKeyringInfo k info = voidOperation
	(set_info k info)
	(set_info_sync k info)

set_info :: (Keyring) -> (KeyringInfo) -> (DoneCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
set_info a1 a2 a3 a4 a5 =
  withKeyringName a1 $ \a1' -> 
  withKeyringInfo a2 $ \a2' -> 
  let {a3' = id a3} in 
  let {a4' = id a4} in 
  let {a5' = id a5} in 
  set_info'_ a1' a2' a3' a4' a5' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')

{-# LINE 978 "lib/Gnome/Keyring.chs" #-}


set_info_sync :: (Keyring) -> (KeyringInfo) -> IO (((Result, ())))
set_info_sync a1 a2 =
  withKeyringName a1 $ \a1' -> 
  withKeyringInfo a2 $ \a2' -> 
  set_info_sync'_ a1' a2' >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')

{-# LINE 983 "lib/Gnome/Keyring.chs" #-}


-- | Change the password for a keyring. In most cases, @Nothing@ would
-- be specified for both the original and new passwords to allow the user
-- to type both.
changeKeyringPassword
	:: String -- ^ Keyring name
	-> Maybe String -- ^ Old password, or @Nothing@ to prompt the user.
	-> Maybe String -- ^ New password, or @Nothing@ to prompt the user.
	-> Operation ()
changeKeyringPassword k op np = voidOperation
	(change_password k op np)
	(change_password_sync k op np)

change_password :: (String) -> (Maybe String) -> (Maybe String) -> (DoneCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
change_password a1 a2 a3 a4 a5 a6 =
  withUtf8 a1 $ \a1' -> 
  withNullableUtf8 a2 $ \a2' -> 
  withNullableUtf8 a3 $ \a3' -> 
  let {a4' = id a4} in 
  let {a5' = id a5} in 
  let {a6' = id a6} in 
  change_password'_ a1' a2' a3' a4' a5' a6' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')

{-# LINE 1004 "lib/Gnome/Keyring.chs" #-}


change_password_sync :: (String) -> (Maybe String) -> (Maybe String) -> IO (((Result, ())))
change_password_sync a1 a2 a3 =
  withUtf8 a1 $ \a1' -> 
  withNullableUtf8 a2 $ \a2' -> 
  withNullableUtf8 a3 $ \a3' -> 
  change_password_sync'_ a1' a2' a3' >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')

{-# LINE 1010 "lib/Gnome/Keyring.chs" #-}


data KeyringInfo = KeyringInfo
	{
	
	-- | Get or set whether the keyring should be locked when idle.
	  keyringLockOnIdle :: Bool
	
	-- | Get or set the keyring lock timeout.
	, keyringLockTimeout :: Word32
	, keyringMTime :: UTCTime
	, keyringCTime :: UTCTime
	, keyringIsLocked_ :: Bool
	, keyringInfoToken :: ForeignPtr ()
	}

-- | Get when the keyring was last modified.
keyringModified :: KeyringInfo -> UTCTime
keyringModified = keyringMTime

-- | Get when the keyring was created.
keyringCreated :: KeyringInfo -> UTCTime
keyringCreated = keyringCTime

-- | Get whether the keyring is locked.
keyringIsLocked :: KeyringInfo -> Bool
keyringIsLocked = keyringIsLocked_

-- The extra pointer shouldn't be printed out when showing a KeyringInfo,
-- so deriving(Show) can't be used. This instance acts like the
-- auto-generated instance, minus the pointer.
instance Show KeyringInfo where
	showsPrec d info = showParen (d > 10) $
		s "KeyringInfo" .
		s " {keyringLockOnIdle = " . shows (keyringLockOnIdle info) .
		s ", keyringLockTimeout = " . shows (keyringLockTimeout info) .
		s ", keyringMTime = " . shows (keyringMTime info) .
		s ", keyringCTime = " . shows (keyringCTime info) .
		s ", keyringIsLocked = " . shows (keyringIsLocked info) .
		s "}"
		where s = showString

-- GnomeKeyringOperationGetKeyringInfoCallback
type GetKeyringInfoCallback = CInt -> Ptr () -> Ptr () -> IO ()
type GetKeyringInfoCallbackPtr = FunPtr ((CInt -> ((Ptr ()) -> ((Ptr ()) -> (IO ())))))
{-# LINE 1055 "lib/Gnome/Keyring.chs" #-}

foreign import ccall "wrapper"
	wrapGetKeyringInfoCallback :: GetKeyringInfoCallback
	                           -> IO GetKeyringInfoCallbackPtr

keyringInfoOperation :: OperationImpl GetKeyringInfoCallback KeyringInfo
keyringInfoOperation = operationImpl $ \checkResult ->
	wrapGetKeyringInfoCallback $ \cres ptr _ ->
	checkResult cres (peekKeyringInfo ptr)

copyInfo :: Ptr () -> IO (ForeignPtr ())
copyInfo = (newForeignPtr finalizeKeyringInfo =<<) . c_copy
{-# LINE 1066 "lib/Gnome/Keyring.chs" #-}


peekKeyringInfo :: Ptr () -> IO KeyringInfo
peekKeyringInfo ptr = do
	lockOnIdle <- toBool `fmap` gnome_keyring_info_get_lock_on_idle ptr
	timeout <- fromIntegral `fmap` gnome_keyring_info_get_lock_timeout ptr
	mtime <- cToUTC `fmap` gnome_keyring_info_get_mtime ptr
	ctime <- cToUTC `fmap` gnome_keyring_info_get_ctime ptr
	isLocked <- toBool `fmap` gnome_keyring_info_get_is_locked ptr
	copy <- copyInfo ptr
	return (KeyringInfo lockOnIdle timeout mtime ctime isLocked copy)

stealKeyringInfoPtr :: Ptr (Ptr ()) -> IO KeyringInfo
stealKeyringInfoPtr ptr = do
	infoPtr <- newForeignPtr finalizeKeyringInfo =<< peek ptr
	withForeignPtr infoPtr peekKeyringInfo

withKeyringInfo :: KeyringInfo -> (Ptr () -> IO a) -> IO a
withKeyringInfo info io = do
	let infoPtr = keyringInfoToken info
	copy <- withForeignPtr infoPtr copyInfo
	withForeignPtr copy $ \ptr -> do
	gnome_keyring_info_set_lock_on_idle ptr (fromBool (keyringLockOnIdle info))
	gnome_keyring_info_set_lock_timeout ptr (fromIntegral (keyringLockTimeout info))
	io ptr

foreign import ccall "gnome-keyring.h &gnome_keyring_info_free"
	finalizeKeyringInfo :: FunPtr (Ptr a -> IO ())

-- | Networks passwords are a simple way of saving passwords associated with
-- a certain user, server, protocol, and other fields.
data NetworkPassword = NetworkPassword
	{ networkPasswordKeyring_ :: Keyring
	, networkPasswordItemID_ :: ItemID
	, networkPasswordNetwork_ :: Network
	, networkPasswordSecret_ :: String
	}
	deriving (Eq)

-- | Get which keyring the password is stored in.
networkPasswordKeyring :: NetworkPassword -> Keyring
networkPasswordKeyring = networkPasswordKeyring_

-- | Get the ID of the network password's keyring item.
networkPasswordItemID :: NetworkPassword -> ItemID
networkPasswordItemID = networkPasswordItemID_

-- | Get the network location metadata associated with the network password.
networkPasswordNetwork :: NetworkPassword -> Network
networkPasswordNetwork = networkPasswordNetwork_

-- | Get the network password's secret value.
networkPasswordSecret :: NetworkPassword -> String
networkPasswordSecret = networkPasswordSecret_

instance Show NetworkPassword where
	showsPrec d x = showParen (d > 10) $ 
		s "NetworkPassword" .
		s " {networkPasswordKeyring = " . shows (networkPasswordKeyring_ x) .
		s ", networkPasswordItemID = " . shows (networkPasswordItemID_ x) .
		s ", networkPasswordNetwork = " . shows (networkPasswordNetwork_ x) .
		s ", networkPasswordSecret = " . shows (networkPasswordSecret_ x) .
		s "}"
		where s = showString

-- | A set of predicates to store with a 'NetworkPassword', used to find the
-- password later.
data Network = Network
	{
	-- | Get or set the network protocol.
	  networkProtocol :: Maybe String
	
	-- | Get or set the network server name.
	, networkServer :: Maybe String
	
	-- | Get or set the network object.
	, networkObject :: Maybe String
	
	-- | Get or set the type of authentication.
	, networkAuthType :: Maybe String
	
	-- | Get or set the network port. A port of 0 is considered blank.
	, networkPort :: Word32
	
	-- | Get or set the network user name.
	, networkUser :: Maybe String
	
	-- | Get or set the network domain name.
	, networkDomain :: Maybe String
	}
	deriving (Show, Eq)

-- | A 'Network' with no set fields.
network :: Network
network = Network
	{ networkProtocol = Nothing
	, networkServer = Nothing
	, networkObject = Nothing
	, networkAuthType = Nothing
	, networkPort = 0
	, networkUser = Nothing
	, networkDomain = Nothing
	}

-- | Find a previously stored 'NetworkPassword'. Searches all keyrings.
--
-- The user may be prompted to unlock necessary keyrings, and will be
-- prompted for access to the items if needed.
--
-- Network passwords are items with the 'ItemType' 'ItemNetworkPassword'.
--
-- Returns an empty list if no items were found.
findNetworkPassword :: Network -> Operation [NetworkPassword]
findNetworkPassword net = let
	p1 = networkUser     net
	p2 = networkDomain   net
	p3 = networkServer   net
	p4 = networkObject   net
	p5 = networkProtocol net
	p6 = networkAuthType net
	p7 = networkPort     net
	in passwordListOperation
		(find_network_password p1 p2 p3 p4 p5 p6 p7)
		(do
			(rc, lst) <- find_network_password_sync p1 p2 p3 p4 p5 p6 p7
			return $ if rc == Result 9
				then (Result 0, [])
				else (rc, lst))

passwordListOperation :: OperationImpl GetListCallback [NetworkPassword]
passwordListOperation = operationImpl $ \checkResult ->
	wrapGetListCallback $ \cres list _ -> if cres == 9
		then checkResult 0 (return [])
		else checkResult cres (mapGList peekPassword list)

find_network_password :: (Maybe String) -> (Maybe String) -> (Maybe String) -> (Maybe String) -> (Maybe String) -> (Maybe String) -> (Word32) -> (GetListCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
find_network_password a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 =
  withNullableUtf8 a1 $ \a1' -> 
  withNullableUtf8 a2 $ \a2' -> 
  withNullableUtf8 a3 $ \a3' -> 
  withNullableUtf8 a4 $ \a4' -> 
  withNullableUtf8 a5 $ \a5' -> 
  withNullableUtf8 a6 $ \a6' -> 
  let {a7' = fromIntegral a7} in 
  let {a8' = id a8} in 
  let {a9' = id a9} in 
  let {a10' = id a10} in 
  find_network_password'_ a1' a2' a3' a4' a5' a6' a7' a8' a9' a10' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')

{-# LINE 1212 "lib/Gnome/Keyring.chs" #-}


find_network_password_sync :: (Maybe String) -> (Maybe String) -> (Maybe String) -> (Maybe String) -> (Maybe String) -> (Maybe String) -> (Word32) -> IO ((Result), ([NetworkPassword]))
find_network_password_sync a1 a2 a3 a4 a5 a6 a7 =
  withNullableUtf8 a1 $ \a1' -> 
  withNullableUtf8 a2 $ \a2' -> 
  withNullableUtf8 a3 $ \a3' -> 
  withNullableUtf8 a4 $ \a4' -> 
  withNullableUtf8 a5 $ \a5' -> 
  withNullableUtf8 a6 $ \a6' -> 
  let {a7' = fromIntegral a7} in 
  alloca $ \a8' -> 
  find_network_password_sync'_ a1' a2' a3' a4' a5' a6' a7' a8' >>= \res ->
  let {res' = Result res} in
  stealPasswordList  a8'>>= \a8'' -> 
  return (res', a8'')

{-# LINE 1223 "lib/Gnome/Keyring.chs" #-}


-- | Store a network password.
--
-- If an item already exists for with this network info (ie: user, server,
-- etc.) then it will be updated.
--
-- Whether a new item is created or not, the item's ID will be returned.
--
-- Network passwords are items with the 'ItemType' 'ItemNetworkPassword'.
setNetworkPassword :: Keyring
                   -> Network
                   -> String
                   -> Operation ItemID
setNetworkPassword k net secret = let
	p1 = networkUser     net
	p2 = networkDomain   net
	p3 = networkServer   net
	p4 = networkObject   net
	p5 = networkProtocol net
	p6 = networkAuthType net
	p7 = networkPort     net
	in itemIDOperation
		(set_network_password k p1 p2 p3 p4 p5 p6 p7 secret)
		(set_network_password_sync k p1 p2 p3 p4 p5 p6 p7 secret)

set_network_password :: (Keyring) -> (Maybe String) -> (Maybe String) -> (Maybe String) -> (Maybe String) -> (Maybe String) -> (Maybe String) -> (Word32) -> (String) -> (GetIntCallbackPtr) -> (Ptr ()) -> (DestroyNotifyPtr) -> IO ((CancellationKey))
set_network_password a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 =
  withKeyringName a1 $ \a1' -> 
  withNullableUtf8 a2 $ \a2' -> 
  withNullableUtf8 a3 $ \a3' -> 
  withNullableUtf8 a4 $ \a4' -> 
  withNullableUtf8 a5 $ \a5' -> 
  withNullableUtf8 a6 $ \a6' -> 
  withNullableUtf8 a7 $ \a7' -> 
  let {a8' = fromIntegral a8} in 
  withUtf8 a9 $ \a9' -> 
  let {a10' = id a10} in 
  let {a11' = id a11} in 
  let {a12' = id a12} in 
  set_network_password'_ a1' a2' a3' a4' a5' a6' a7' a8' a9' a10' a11' a12' >>= \res ->
  let {res' = CancellationKey res} in
  return (res')

{-# LINE 1262 "lib/Gnome/Keyring.chs" #-}


set_network_password_sync :: (Keyring) -> (Maybe String) -> (Maybe String) -> (Maybe String) -> (Maybe String) -> (Maybe String) -> (Maybe String) -> (Word32) -> (String) -> IO ((Result), (ItemID))
set_network_password_sync a1 a2 a3 a4 a5 a6 a7 a8 a9 =
  withKeyringName a1 $ \a1' -> 
  withNullableUtf8 a2 $ \a2' -> 
  withNullableUtf8 a3 $ \a3' -> 
  withNullableUtf8 a4 $ \a4' -> 
  withNullableUtf8 a5 $ \a5' -> 
  withNullableUtf8 a6 $ \a6' -> 
  withNullableUtf8 a7 $ \a7' -> 
  let {a8' = fromIntegral a8} in 
  withUtf8 a9 $ \a9' -> 
  alloca $ \a10' -> 
  set_network_password_sync'_ a1' a2' a3' a4' a5' a6' a7' a8' a9' a10' >>= \res ->
  let {res' = Result res} in
  peekItemID  a10'>>= \a10'' -> 
  return (res', a10'')

{-# LINE 1275 "lib/Gnome/Keyring.chs" #-}


peekPassword :: Ptr () -> IO NetworkPassword
peekPassword pwd = do
	-- Password location
	protocol <- peekNullableUtf8 =<< (\ptr -> do {peekByteOff ptr 16 ::IO (Ptr CChar)}) pwd
	server <- peekNullableUtf8 =<< (\ptr -> do {peekByteOff ptr 24 ::IO (Ptr CChar)}) pwd
	object <- peekNullableUtf8 =<< (\ptr -> do {peekByteOff ptr 32 ::IO (Ptr CChar)}) pwd
	authType <- peekNullableUtf8 =<< (\ptr -> do {peekByteOff ptr 40 ::IO (Ptr CChar)}) pwd
	port <- fromIntegral `fmap` (\ptr -> do {peekByteOff ptr 48 ::IO CUInt}) pwd
	user <- peekNullableUtf8 =<< (\ptr -> do {peekByteOff ptr 56 ::IO (Ptr CChar)}) pwd
	domain <- peekNullableUtf8 =<< (\ptr -> do {peekByteOff ptr 64 ::IO (Ptr CChar)}) pwd
	let net = Network
		{ networkProtocol = protocol
		, networkServer   = server
		, networkObject   = object
		, networkAuthType = authType
		, networkPort     = port
		, networkUser     = user
		, networkDomain   = domain
		}
	
	-- Keyring, item, and secret
	keyringName <- peekUtf8 =<< (\ptr -> do {peekByteOff ptr 0 ::IO (Ptr CChar)}) pwd
	itemID <- ItemID `fmap` (\ptr -> do {peekByteOff ptr 8 ::IO CUInt}) pwd
	password <- peekUtf8 =<< (\ptr -> do {peekByteOff ptr 72 ::IO (Ptr CChar)}) pwd
	return (NetworkPassword (keyring keyringName) itemID net password)

stealPasswordList :: Ptr (Ptr ()) -> IO [NetworkPassword]
stealPasswordList ptr = bracket (peek ptr)
	gnome_keyring_network_password_list_free
{-# LINE 1305 "lib/Gnome/Keyring.chs" #-}

	(mapGList peekPassword)

-- | A keyring operation can be run synchronously or asynchronously.
-- Asynchronous execution requires a running GLib event loop.
data Operation a = Operation
	{ asyncImpl :: (KeyringError -> IO ()) -> (a -> IO ()) -> IO CancellationKey
	, syncImpl :: IO (Result, a)
	}

-- | Runs an operation synchronously, and returns either the result or
-- an error.
--
-- Running an operation synchronously does not require a GLib event loop
-- to be active.
sync :: Operation a -> IO (Either KeyringError a)
sync op = do
	(res, x) <- syncImpl op
	return $ case res of
		Result 0 -> Right x
		_        -> Left (resultToError res)

-- | Runs an operation synchronously. If it succeeded, returns the result.
-- Otherwise, throws a 'KeyringError'.
--
-- Running an operation synchronously does not require a GLib event loop
-- to be active.
sync_ :: Operation a -> IO a
sync_ op = do
	res <- sync op
	case res of
		Right x -> return x
		Left err -> throwIO (KeyringException err)

-- | Runs an operation asynchronously, calling one of the given callbacks on
-- success or failure.
--
-- The returned 'CancellationKey' can be used to cancel a pending operation.
--
-- Running an operation asynchronously requires a running GLib event loop.
async :: Operation a -> (KeyringError -> IO ()) -> (a -> IO ()) -> IO CancellationKey
async = asyncImpl

-- Implementation details of async operations
type OperationImpl a b = (FunPtr a -> Ptr () -> DestroyNotifyPtr -> IO CancellationKey) -> IO (Result, b) -> Operation b
operationImpl :: ((CInt -> IO a -> IO ()) -> IO (FunPtr b)) -> OperationImpl b a
operationImpl impl asyncIO = Operation $ \onError onSuccess -> do
	
	callback <- impl $ \cres io -> case cres of
		0 -> io >>= onSuccess
		x        -> onError (resultToError (Result x))
	
	destroy <- wrapDestroyNotify $ \ptr -> do
		let stable = castPtrToStablePtr ptr
		_ <- join (deRefStablePtr stable)
		freeStablePtr stable
	
	stable <- newStablePtr $ do
		freeHaskellFunPtr callback
		freeHaskellFunPtr destroy
	
	asyncIO callback (castStablePtrToPtr stable) destroy

-- Available basic operation types

voidOperation :: OperationImpl DoneCallback ()
voidOperation = operationImpl $ \checkResult ->
	wrapDoneCallback $ \cres _ ->
	checkResult cres (return ())

maybeStringOperation :: OperationImpl GetStringCallback (Maybe String)
maybeStringOperation = operationImpl $ \checkResult ->
	wrapGetStringCallback $ \cres cstr _ ->
	checkResult cres (peekNullableUtf8 cstr)

stringListOperation :: OperationImpl GetListCallback [String]
stringListOperation = operationImpl $ \checkResult ->
	wrapGetListCallback $ \cres list _ ->
	checkResult cres (mapGList peekUtf8 list)

cToUTC :: Integral a => a -> UTCTime
cToUTC = posixSecondsToUTCTime . fromIntegral

peekText :: CString -> IO Text
peekText cstr
	| cstr == nullPtr = error "Gnome.Keyring.FFI.peekText nullPtr"
	| otherwise       = do
		bytes <- ByteString.packCString cstr
		return (decodeUtf8 bytes)

withUtf8 :: String -> (CString -> IO a) -> IO a
withUtf8 = ByteString.useAsCString . encodeUtf8  . Data.Text.pack

peekUtf8 :: CString -> IO String
peekUtf8 cstr = fmap Data.Text.unpack (peekText cstr)

withNullableUtf8 :: Maybe String -> (CString -> IO a) -> IO a
withNullableUtf8 = maybeWith withUtf8

peekNullableUtf8 :: CString -> IO (Maybe String)
peekNullableUtf8= maybePeek peekUtf8

stealNullableUtf8 :: CString -> IO (Maybe String)
stealNullableUtf8 cstr = bracket (return cstr) free peekNullableUtf8

withKeyringName :: Keyring -> (CString -> IO a) -> IO a
withKeyringName k = withNullableUtf8 name where
	name = case k of
		DefaultKeyring -> Nothing
		NamedKeyring s -> Just s

-- Convert GList to []
mapGList :: (Ptr a -> IO b) -> Ptr () -> IO [b]
mapGList f list
	| list == nullPtr = return []
	| otherwise = do
		item <- (\ptr -> do {peekByteOff ptr 0 ::IO (Ptr ())}) list
		next <- (\ptr -> do {peekByteOff ptr 8 ::IO (Ptr ())}) list
		items <- mapGList f next
		item' <- f (castPtr item)
		return (item' : items)

-- Convert GArray to []
mapGArray :: (Ptr a -> IO b) -> Int -> Ptr () -> IO [b]
mapGArray f size array = do
	len <- (\ptr -> do {peekByteOff ptr 8 ::IO CUInt}) array
	start <- (\ptr -> do {peekByteOff ptr 0 ::IO (Ptr CChar)}) array
	mapGArray' f size (fromIntegral len) (castPtr start)

mapGArray' :: (Ptr a -> IO b) -> Int -> Integer -> Ptr () -> IO [b]
mapGArray' _    _ 0   _ = return []
mapGArray' f size n ptr = do
	attr <- f (castPtr ptr)
	attrs <- mapGArray' f size (n - 1) (plusPtr ptr size)
	return (attr : attrs)

resultToError :: Result -> KeyringError
resultToError (Result 7) = KeyringError "Operation canceled by user or application"
resultToError (Result x) = unsafePerformIO $ do
	ptr <- gnome_keyring_result_to_message x
	msg <- peekUtf8 ptr
	return (KeyringError msg)

--------------

-- GDestroyNotify
type DestroyNotify = Ptr () -> IO ()
type DestroyNotifyPtr = FunPtr (((Ptr ()) -> (IO ())))
{-# LINE 1452 "lib/Gnome/Keyring.chs" #-}

foreign import ccall "wrapper"
	wrapDestroyNotify :: DestroyNotify -> IO DestroyNotifyPtr

-- GnomeKeyringOperationDoneCallback
type DoneCallback = CInt -> Ptr () -> IO ()
type DoneCallbackPtr = FunPtr ((CInt -> ((Ptr ()) -> (IO ()))))
{-# LINE 1458 "lib/Gnome/Keyring.chs" #-}

foreign import ccall "wrapper"
	wrapDoneCallback :: DoneCallback -> IO DoneCallbackPtr

-- GnomeKeyringOperationGetStringCallback
type GetStringCallback = CInt -> CString -> Ptr () -> IO ()
type GetStringCallbackPtr = FunPtr ((CInt -> ((Ptr CChar) -> ((Ptr ()) -> (IO ())))))
{-# LINE 1464 "lib/Gnome/Keyring.chs" #-}

foreign import ccall "wrapper"
	wrapGetStringCallback :: GetStringCallback -> IO GetStringCallbackPtr

-- GnomeKeyringOperationGetIntCallback
type GetIntCallback = CInt -> CUInt -> Ptr () -> IO ()
type GetIntCallbackPtr = FunPtr ((CInt -> (CUInt -> ((Ptr ()) -> (IO ())))))
{-# LINE 1470 "lib/Gnome/Keyring.chs" #-}

foreign import ccall "wrapper"
	wrapGetIntCallback :: GetIntCallback -> IO GetIntCallbackPtr

-- GnomeKeyringOperationGetListCallback
type GetListCallback = CInt -> Ptr () -> Ptr () -> IO ()
type GetListCallbackPtr = FunPtr ((CInt -> ((Ptr ()) -> ((Ptr ()) -> (IO ())))))
{-# LINE 1476 "lib/Gnome/Keyring.chs" #-}

foreign import ccall "wrapper"
	wrapGetListCallback :: GetListCallback -> IO GetListCallbackPtr

unpackKey :: CancellationKey -> Ptr ()
unpackKey (CancellationKey x) = x

-- | Cancel a running asynchronous operation. The error callback will be
-- called with a 'KeyringError' stating that the operation was canceled.
cancel :: (CancellationKey) -> IO ((()))
cancel a1 =
  let {a1' = unpackKey a1} in 
  cancel'_ a1' >>= \res ->
  let {res' = id res} in
  return (res')

{-# LINE 1487 "lib/Gnome/Keyring.chs" #-}


newtype CancellationKey = CancellationKey (Ptr ())

newtype KeyringError = KeyringError String
	deriving (Eq, Show)

keyringErrorMessage :: KeyringError -> String
keyringErrorMessage (KeyringError msg) = msg

newtype KeyringException = KeyringException KeyringError
	deriving (Show, Eq, Typeable)

instance Exception KeyringException

newtype Result = Result CInt
	deriving (Eq)

resultAndTuple :: CInt -> (Result, ())
resultAndTuple x = (Result x, ())

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_is_available"
  available'_ :: (IO CInt)

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

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

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

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

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

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

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

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

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

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

foreign import ccall safe "Gnome/Keyring.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.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.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.chs.h gnome_keyring_item_delete_sync"
  item_delete_sync'_ :: ((Ptr CChar) -> (CUInt -> (IO CInt)))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_list_item_ids"
  list_item_ids'_ :: ((Ptr CChar) -> ((FunPtr (CInt -> ((Ptr ()) -> ((Ptr ()) -> (IO ()))))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ()))))))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_list_item_ids_sync"
  list_item_ids_sync'_ :: ((Ptr CChar) -> ((Ptr (Ptr ())) -> (IO CInt)))

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

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

foreign import ccall safe "Gnome/Keyring.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.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.chs.h gnome_keyring_item_set_info_sync"
  item_set_info_sync'_ :: ((Ptr CChar) -> (CUInt -> ((Ptr ()) -> (IO CInt))))

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

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

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

foreign import ccall safe "Gnome/Keyring.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.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.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.chs.h gnome_keyring_item_set_attributes_sync"
  item_set_attributes_sync'_ :: ((Ptr CChar) -> (CUInt -> ((Ptr ()) -> (IO CInt))))

foreign import ccall safe "Gnome/Keyring.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.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.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.chs.h gnome_keyring_application_ref_new"
  gnome_keyring_application_ref_new :: (IO (Ptr ()))

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

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

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

foreign import ccall safe "Gnome/Keyring.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.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.chs.h gnome_keyring_acl_free"
  gnome_keyring_acl_free :: ((Ptr ()) -> (IO ()))

foreign import ccall safe "Gnome/Keyring.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.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.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.chs.h gnome_keyring_item_set_acl_sync"
  item_set_acl_sync'_ :: ((Ptr CChar) -> (CUInt -> ((Ptr ()) -> (IO CInt))))

foreign import ccall safe "Gnome/Keyring.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.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.chs.h gnome_keyring_found_list_free"
  gnome_keyring_found_list_free :: ((Ptr ()) -> (IO ()))

foreign import ccall safe "Gnome/Keyring.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.chs.h gnome_keyring_find_items_sync"
  find_items_sync'_ :: (CInt -> ((Ptr ()) -> ((Ptr (Ptr ())) -> (IO CInt))))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_get_default_keyring"
  get_default_keyring'_ :: ((FunPtr (CInt -> ((Ptr CChar) -> ((Ptr ()) -> (IO ()))))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ())))))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_get_default_keyring_sync"
  get_default_keyring_sync'_ :: ((Ptr (Ptr CChar)) -> (IO CInt))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_set_default_keyring"
  set_default_keyring'_ :: ((Ptr CChar) -> ((FunPtr (CInt -> ((Ptr ()) -> (IO ())))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ()))))))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_set_default_keyring_sync"
  set_default_keyring_sync'_ :: ((Ptr CChar) -> (IO CInt))

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

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

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

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_create"
  c_create'_ :: ((Ptr CChar) -> ((Ptr CChar) -> ((FunPtr (CInt -> ((Ptr ()) -> (IO ())))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ())))))))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_create_sync"
  create_sync'_ :: ((Ptr CChar) -> ((Ptr CChar) -> (IO CInt)))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_delete"
  c_delete'_ :: ((Ptr CChar) -> ((FunPtr (CInt -> ((Ptr ()) -> (IO ())))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ()))))))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_delete_sync"
  delete_sync'_ :: ((Ptr CChar) -> (IO CInt))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_lock"
  c_lock'_ :: ((Ptr CChar) -> ((FunPtr (CInt -> ((Ptr ()) -> (IO ())))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ()))))))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_lock_sync"
  lock_sync'_ :: ((Ptr CChar) -> (IO CInt))

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

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_lock_all_sync"
  lock_all_sync'_ :: (IO CInt)

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_unlock"
  c_unlock'_ :: ((Ptr CChar) -> ((Ptr CChar) -> ((FunPtr (CInt -> ((Ptr ()) -> (IO ())))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ())))))))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_unlock_sync"
  unlock_sync'_ :: ((Ptr CChar) -> ((Ptr CChar) -> (IO CInt)))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_get_info"
  get_info'_ :: ((Ptr CChar) -> ((FunPtr (CInt -> ((Ptr ()) -> ((Ptr ()) -> (IO ()))))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ()))))))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_get_info_sync"
  get_info_sync'_ :: ((Ptr CChar) -> ((Ptr (Ptr ())) -> (IO CInt)))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_set_info"
  set_info'_ :: ((Ptr CChar) -> ((Ptr ()) -> ((FunPtr (CInt -> ((Ptr ()) -> (IO ())))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ())))))))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_set_info_sync"
  set_info_sync'_ :: ((Ptr CChar) -> ((Ptr ()) -> (IO CInt)))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_change_password"
  change_password'_ :: ((Ptr CChar) -> ((Ptr CChar) -> ((Ptr CChar) -> ((FunPtr (CInt -> ((Ptr ()) -> (IO ())))) -> ((Ptr ()) -> ((FunPtr ((Ptr ()) -> (IO ()))) -> (IO (Ptr ()))))))))

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_change_password_sync"
  change_password_sync'_ :: ((Ptr CChar) -> ((Ptr CChar) -> ((Ptr CChar) -> (IO CInt))))

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

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

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

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

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

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

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

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

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

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

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

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

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

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

foreign import ccall safe "Gnome/Keyring.chs.h gnome_keyring_cancel_request"
  cancel'_ :: ((Ptr ()) -> (IO ()))