# Copyright (C) 2002 John Goerzen
# <jgoerzen@complete.org>
#
#    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 2 of the License, or
#    (at your option) 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, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import gpgme
from errors import errorcheck
import errors
from util import GpgmeWrapper

class ContextKeysIterator:
    """This class should never be instantiated directly, but instead,
    is instantiated by calling pyme.core.Context.op_keylist_start().

    You can iterate over items in this class."""

    def __init__(self, GpgmeCtx):
        self.contextobj = GpgmeCtx

    def __iter__(self):
        return self

    def next(self):
        try:
            return self.contextobj.op_keylist_next()
        except errors.EOF:
            raise StopIteration

class Key(GpgmeWrapper):
    """This class defines information about one particular key.

    It is not instantiated directly, but usually created as a result
    of iterating over Context.op_keylist_start() data."""
    
    def _getctype(self):
        return 'GpgmeKey'
    
    def __init__(self, keyp):
        self.key = keyp
        self.wrapped = self.key

    def __del__(self):
        gpgme.gpgme_key_release(self.key)

    def _getnameprepend(self):
        return 'gpgme_key_'

    def _errorcheck(self, name):
        return 0

    def get_string_attrs(self, what):
        """This is a helper function that will return a list of
        all string attributes with the given GpgmeAttr what."""
        retval = []
        index = 0
        while 1:
            data = self.get_string_attr(what, None, index)
            if data == None:
                break
            retval.append(data)
            index += 1
        return retval

    def get_ulong_attrs(self, what):
        """This is a helper function that will return a list of all
        ulong attributes with the given GpgmeAttr what."""
        retval = []
        index = 0
        while 1:
            data = self.get_ulong_attr(what, None, index)
            if data == None:
                break
            retval.append(data)
            index += 1
        return retval

class RecipientsIterator:
    """This class should not be instantiated directly, but is automatically
    instantiated by a call to iter(pyme.core.Recipients) or iteration over
    that list."""
    def __init__(self, recipientobj):
        self.recipients = recipientobj
        self.ptr = gpgme.new_void_p_p()
        self.recipients.enum_open(self.ptr)

    def next(self):
        result = self.recipients.enum_read(self.ptr)
        if result == None:
            raise StopIteration
        return result

    def __del__(self):
        self.recipients.enum_close(self.ptr)
        gpgme.delete_void_p_p(self.ptr)

