#!/usr/bin/env python

from simple_at import *


class StateSet(Accessibility__POA.StateSet):
  '''
  Convenience implementation of AT-SPI StateSet, for future use with Collection
  interface.
  
  @param states: Set of states
  @type states: set
  '''
  def __init__(self, *states):
    '''Initializes the state set with the given states.'''
    self.states = set(states)
    
  def ref (self):
    print "ref"

  def unref (self):
    print "unref"

  def queryInterface(self, repoid):
    if repoid in ('IDL:Bonobo/Unknown:1.0',
                  'IDL:Accessibility/StateSet:1.0'):
      return self._this()
    return None
     

  def contains(self, state):
    '''
    Checks if this L{StateSet} contains the given state.
    
    @param state: State to check
    @type state: Accessibility.StateType
    @return: True if the set contains the given state
    @rtype: boolean
    '''
    return state in self.states
  
  def add(self, *state):
    '''
    Adds one or more states to this set.
    
    @param state: State(s) to add
    @type state: Accessibility.StateType
    '''
    for s in state:
      self.states.add(s)
  
  def remove(self, *state):
    '''
    Removes one or more states from this set.
    
    @param state: State(s) to remove
    @type state: Accessibility.StateType
    '''
    for s in state:
      self.states.remove(s)
  
  def equals(self, state_set):
    '''
    Checks if this L{StateSet} contains exactly the same members as the given
    L{StateSet}.
    
    @param state_set: Another set
    @type state_set: L{StateSet}
    @return: Are the sets equivalent in terms of their contents?
    @rtype: boolean
    '''
    return state_set.getStates() == self.states
  
  def compare(self, state_set):
    '''
    Computes the symmetric differences of this L{StateSet} and the given
    L{StateSet}.
    
    @param state_set: Another set
    @type state_set: L{StateSet}
    @return: Elements in only one of the two sets
    @rtype: L{StateSet}
    '''
    diff = self.states.symmetric_difference(state_set.states)
    return StateSet(*diff)
  
  def isEmpty(self):
    '''
    Checks if this L{StateSet} is empty.
    
    @return: Is it empty?
    @rtype: boolean
    '''
    #print 'test'
    return len(self.states) == 0

  def getStates(self):
    '''
    Gets the sequence of all states in this set.
    
    @return: List of states
    @rtype: list
    '''
    return list(self.states)

def run(acc):
  print "[start]"
  # try to query to the collection interface on the app
  col = acc.queryInterface('IDL:Accessibility/Collection:1.0')
  if col is None:
    raise NotImplementedError('%s does noot implement collection' % acc.name)

  
  cs = c.getState()
  
  ch =  c.getChildAtIndex(0)
  
  states = StateSet()

  states.add (Accessibility.STATE_VISIBLE)

  print "Cset is a", cs
  print "Pyset is a", states._this()
  print "Cset  equals  Cset?", cs.equals(cs);
  print "Pyset equals Pyset?", states.equals(states)
  print "Cset is empty? getStates", cs.isEmpty(), cs.getStates()
  print "Pyset is empty? getStates",  states.isEmpty(), states.getStates()
  
  return 1

if __name__ == '__main__':
  d = registry.getDesktop(0)
  for i in xrange(d.childCount):
    try:
      c = d.getChildAtIndex(i)
    except:
      continue
    if c is not None and c.name == 'gcalctool':
      run(c)
     

print "[fin]"
