-- 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/Keyring.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 (Typeclass extensions & FFI)
-- 
-- GNOME Keyring manages multiple keyrings. Each keyring can store one or
-- more items, containing secrets.
-- 
-- One of the keyrings is the default keyring, which can in many cases be
-- used by specifying 'Nothing' for a keyring names.
-- 
-- 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.

{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}

{-# LINE 35 "./Gnome/Keyring/Keyring.chs" #-}

module Gnome.Keyring.Keyring
	( KeyringName
	, getDefaultKeyring
	, setDefaultKeyring
	, listKeyringNames
	, create
	, delete
	, lock
	, lockAll
	, unlock
	, getInfo
	, setInfo
	, changePassword
	, listItemIDs
	) where

import Data.Text.Lazy (Text)
import Gnome.Keyring.Operation.Internal (Operation, operation)
import Gnome.Keyring.ItemInfo.Internal (ItemID (..))
import Gnome.Keyring.Types (Result, CancellationKey (..), KeyringName)
import Gnome.Keyring.Find (findItems) -- for docs

-- Import unqualified for c2hs
import Foreign
import Foreign.C
import Gnome.Keyring.FFI
import Gnome.Keyring.KeyringInfo.Internal

-- | Get the default keyring name. If no default keyring exists, then
-- 'Nothing' will be returned.
-- 
getDefaultKeyring :: Operation (Maybe KeyringName)
getDefaultKeyring = operation
	get_default_keyring
	get_default_keyring_sync

get_default_keyring :: GetNullableStringCallback -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
get_default_keyring a1 a2 a3 =
  let {a1' = callbackToPtr 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 77 "./Gnome/Keyring/Keyring.chs" #-}

get_default_keyring_sync :: IO (Result, Maybe Text)
get_default_keyring_sync =
  alloca $ \a1' -> 
  get_default_keyring_sync'_ a1' >>= \res ->
  stealNullableTextPtr a1'>>= \a1'' -> 
  let {res' = result res} in
  return (res', a1'')
{-# LINE 81 "./Gnome/Keyring/Keyring.chs" #-}

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

set_default_keyring :: Text -> DoneCallback -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
set_default_keyring a1 a2 a3 a4 =
  withText a1 $ \a1' -> 
  let {a2' = callbackToPtr 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 95 "./Gnome/Keyring/Keyring.chs" #-}

set_default_keyring_sync :: Text -> IO ((Result, ()))
set_default_keyring_sync a1 =
  withText a1 $ \a1' -> 
  set_default_keyring_sync'_ a1' >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')
{-# LINE 99 "./Gnome/Keyring/Keyring.chs" #-}

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

list_keyring_names :: GetTextListCallback -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
list_keyring_names a1 a2 a3 =
  let {a1' = callbackToPtr 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 113 "./Gnome/Keyring/Keyring.chs" #-}

list_keyring_names_sync :: IO (Result, [Text])
list_keyring_names_sync =
  alloca $ \a1' -> 
  list_keyring_names_sync'_ a1' >>= \res ->
  stealTextList a1'>>= \a1'' -> 
  let {res' = result res} in
  return (res', a1'')
{-# LINE 117 "./Gnome/Keyring/Keyring.chs" #-}

-- | 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.
-- 
create :: KeyringName -> Maybe Text -> Operation ()
create k p = operation (c_create k p) (create_sync k p)

c_create :: Text -> Maybe Text -> DoneCallback -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
c_create a1 a2 a3 a4 a5 =
  withText a1 $ \a1' -> 
  withNullableText a2 $ \a2' -> 
  let {a3' = callbackToPtr 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 132 "./Gnome/Keyring/Keyring.chs" #-}

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

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

c_delete :: Text -> DoneCallback -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
c_delete a1 a2 a3 a4 =
  withText a1 $ \a1' -> 
  let {a2' = callbackToPtr 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 150 "./Gnome/Keyring/Keyring.chs" #-}

delete_sync :: Text -> IO ((Result, ()))
delete_sync a1 =
  withText a1 $ \a1' -> 
  delete_sync'_ a1' >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')
{-# LINE 154 "./Gnome/Keyring/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.
-- 
lock :: Maybe KeyringName -> Operation ()
lock k = operation (c_lock k) (lock_sync k)

c_lock :: Maybe Text -> DoneCallback -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
c_lock a1 a2 a3 a4 =
  withNullableText a1 $ \a1' -> 
  let {a2' = callbackToPtr 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 170 "./Gnome/Keyring/Keyring.chs" #-}

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

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

lock_all :: DoneCallback -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
lock_all a1 a2 a3 =
  let {a1' = callbackToPtr 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 186 "./Gnome/Keyring/Keyring.chs" #-}

lock_all_sync :: IO ((Result, ()))
lock_all_sync =
  lock_all_sync'_ >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')
{-# LINE 189 "./Gnome/Keyring/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.
-- 
unlock :: Maybe KeyringName -> Maybe Text -> Operation ()
unlock k p = operation (c_unlock k p) (unlock_sync k p)

c_unlock :: Maybe Text -> Maybe Text -> DoneCallback -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
c_unlock a1 a2 a3 a4 a5 =
  withNullableText a1 $ \a1' -> 
  withNullableText a2 $ \a2' -> 
  let {a3' = callbackToPtr 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 207 "./Gnome/Keyring/Keyring.chs" #-}

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

-- | Get information about the keyring.
-- 
getInfo :: Maybe KeyringName -> Operation KeyringInfo
getInfo k = operation (get_info k) (get_info_sync k)

get_info :: Maybe Text -> GetKeyringInfoCallback -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
get_info a1 a2 a3 a4 =
  withNullableText a1 $ \a1' -> 
  let {a2' = callbackToPtr 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 224 "./Gnome/Keyring/Keyring.chs" #-}

get_info_sync :: Maybe Text -> IO (Result, KeyringInfo)
get_info_sync a1 =
  withNullableText a1 $ \a1' -> 
  alloca $ \a2' -> 
  get_info_sync'_ a1' a2' >>= \res ->
  stealKeyringInfoPtr a2'>>= \a2'' -> 
  let {res' = result res} in
  return (res', a2'')
{-# LINE 229 "./Gnome/Keyring/Keyring.chs" #-}

-- | Set flags and info for the keyring. The only fields in the
-- 'KeyringInfo' which are used are 'keyringLockOnIdle' and
-- 'keyringLockTimeout'.
-- 
setInfo :: Maybe KeyringName -> KeyringInfo -> Operation ()
setInfo k info = operation (set_info k info) (set_info_sync k info)

set_info :: Maybe Text -> KeyringInfo -> DoneCallback -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
set_info a1 a2 a3 a4 a5 =
  withNullableText a1 $ \a1' -> 
  withKeyringInfo a2 $ \a2' -> 
  let {a3' = callbackToPtr 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 244 "./Gnome/Keyring/Keyring.chs" #-}

set_info_sync :: Maybe Text -> KeyringInfo -> IO ((Result, ()))
set_info_sync a1 a2 =
  withNullableText a1 $ \a1' -> 
  withKeyringInfo a2 $ \a2' -> 
  set_info_sync'_ a1' a2' >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')
{-# LINE 249 "./Gnome/Keyring/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.
-- 
changePassword :: KeyringName
               -> Maybe Text -- ^ Old password
               -> Maybe Text -- ^ New password
               -> Operation ()
changePassword k op np = operation
	(change_password k op np)
	(change_password_sync k op np)

change_password :: Text -> Maybe Text -> Maybe Text -> DoneCallback -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
change_password a1 a2 a3 a4 a5 a6 =
  withText a1 $ \a1' -> 
  withNullableText a2 $ \a2' -> 
  withNullableText a3 $ \a3' -> 
  let {a4' = callbackToPtr 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 270 "./Gnome/Keyring/Keyring.chs" #-}

change_password_sync :: Text -> Maybe Text -> Maybe Text -> IO ((Result, ()))
change_password_sync a1 a2 a3 =
  withText a1 $ \a1' -> 
  withNullableText a2 $ \a2' -> 
  withNullableText a3 $ \a3' -> 
  change_password_sync'_ a1' a2' a3' >>= \res ->
  let {res' = resultAndTuple res} in
  return (res')
{-# LINE 276 "./Gnome/Keyring/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 :: Maybe KeyringName -> Operation [ItemID]
listItemIDs name = operation
	(list_item_ids name)
	(list_item_ids_sync name)

data GetItemIDListCallback = GetItemIDListCallback GetListCallbackPtr
instance Callback GetItemIDListCallback [ItemID] where
	callbackToPtr (GetItemIDListCallback x) = castFunPtr x
	freeCallback  (GetItemIDListCallback x) = freeHaskellFunPtr x
	buildCallback = mkListCallback GetItemIDListCallback
		(return . ItemID . fromIntegral . ptrToWordPtr)

stealItemIDList :: Ptr (Ptr ()) -> IO [ItemID]
stealItemIDList = fmap (map (ItemID . fromIntegral)) . stealWordList

list_item_ids :: Maybe Text -> GetItemIDListCallback -> Ptr () -> DestroyNotifyPtr -> IO (CancellationKey)
list_item_ids a1 a2 a3 a4 =
  withNullableText a1 $ \a1' -> 
  let {a2' = callbackToPtr 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 301 "./Gnome/Keyring/Keyring.chs" #-}

list_item_ids_sync :: Maybe Text -> IO (Result, [ItemID])
list_item_ids_sync a1 =
  withNullableText a1 $ \a1' -> 
  alloca $ \a2' -> 
  list_item_ids_sync'_ a1' a2' >>= \res ->
  stealItemIDList a2'>>= \a2'' -> 
  let {res' = result res} in
  return (res', a2'')
{-# LINE 306 "./Gnome/Keyring/Keyring.chs" #-}

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

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

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

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

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

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

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

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

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

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

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

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

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

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