diff options
Diffstat (limited to 'src/contact.py')
-rw-r--r-- | src/contact.py | 196 |
1 files changed, 91 insertions, 105 deletions
diff --git a/src/contact.py b/src/contact.py index 66f8c453..48f5d751 100644 --- a/src/contact.py +++ b/src/contact.py @@ -20,43 +20,28 @@ class Resource(object): Defines a roster item. It's a precise resource. """ - def __init__(self, jid): + def __init__(self, jid, data): self._jid = JID(jid) # Full jid - self._status = '' - self._presence = 'unavailable' - self._priority = 0 + self._data = data @property def jid(self): return self._jid - def __repr__(self): - return '%s' % self._jid - @property def priority(self): - return self._priority - - @priority.setter - def priority(self, value): - assert isinstance(value, int) - self._priority = value + return self._data['priority'] @property def presence(self): - return self._presence - - @presence.setter - def presence(self, value): - self._presence = value + return self._data['show'] @property def status(self): - return self._status + return self._data['status'] - @status.setter - def status(self, value): - self._status = value + def __repr__(self): + return '<%s>' % self._jid class Contact(object): """ @@ -64,19 +49,17 @@ class Contact(object): This class contains zero or more Resource object and useful methods to get the resource with the highest priority, etc """ - def __init__(self, bare_jid): - self._jid = bare_jid - self._resources = [] - self._folded = True # Folded by default - self._display_name = None - self._subscription = 'none' - self._ask = None - self._groups = [] # a list of groups the contact is in + def __init__(self, item): + """ + item: a SleekXMPP RosterItem pointing to that contact + """ + self.__item = item + self.folded = True # Folded by default @property def groups(self): - """Groups the contact is in""" - return self._groups + """Name of the groups the contact is in""" + return self.__item['groups'] or ['none'] @property def resources(self): @@ -86,98 +69,101 @@ class Contact(object): @property def bare_jid(self): """The bare_jid or the contact""" - return self._jid - - def get_highest_priority_resource(self): - """ - Return the resource with the highest priority - """ - ret = None - for resource in self._resources: - if not ret or ret.priority < resource.priority: - ret = resource - return ret - - def add_resource(self, resource): - """ - Called, for example, when a new resource get offline - (the first, or any subsequent one) - """ - def f(o): - return o.priority - self._resources.append(resource) - self._resources = sorted(self._resources, key=f, reverse=True) + return self.__item.jid - def remove_resource(self, resource): - """ - Called, for example, when one resource goes offline. - """ - self._resources.remove(resource) + @property + def name(self): + return self.__item['name'] or '' - def remove_resource_by_fulljid(self, fulljid): - """ - Like 'remove_resource' but just by knowing the full jid - """ - for resource in self._resources: - if resource.jid == fulljid: - self._resources.remove(resource) - return - assert False + @property + def ask(self): + if self.__item['pending_out']: + return 'asked' - def get_resource_by_fulljid(self, fulljid): - """ - Return the resource with the given fulljid - """ - for resource in self._resources: - if resource.jid.full == fulljid: - return resource - return None + @property + def pending_in(self): + return self.__item['pending_in'] - def toggle_folded(self): - """ - Fold if it's unfolded, and vice versa - """ - self._folded = not self._folded + @pending_in.setter + def pending_in(self, value): + self.__item['pending_in'] = value @property - def name(self): - return self._display_name + def pending_out(self): + return self.__item['pending_out'] - @name.setter - def name(self, value): - self._display_name = value + @pending_out.setter + def pending_out(self, value): + self.__item['pending_out'] = value @property - def ask(self): - return self._ask - - @ask.setter - def ask(self, value): - self._ask = value + def resources(self): + """List of the available resources as Resource objects""" + return [Resource('%s/%s' % (self.bare_jid, key), self.__item.resources[key]) + for key in self.__item.resources] @property def subscription(self): - return self._subscription + return self.__item['subscription'] - @subscription.setter - def subscription(self, value): - self._subscription = value + def __contains__(self, value): + return value in self.__item.resources or JID(value).resource in self.__item.resources - def get_nb_resources(self): - """ - Get the number of connected resources - """ - return len(self._resources) + def __len__(self): + """Number of resources""" + return len(self.__item.resources) + + def __bool__(self): + """This contacts exists even when he has no resources""" + return True + + def __getitem__(self, key): + """Return the corresponding Resource object, or None""" + res = JID(key).resource + resources = self.__item.resources + item = resources.get(res, None) or resources.get(key, None) + return Resource(key, item) if item else None + + def subscribe(self): + """Subscribe to this JID""" + self.__item.subscribe() + + def authorize(self): + """Authorize this JID""" + self.__item.authorize() + + def unauthorize(self): + """Unauthorize this JID""" + self.__item.unauthorize() + + def unsubscribe(self): + """Unsubscribe from this JID""" + self.__item.unsubscribe() + + def get(self, key, default=None): + """Same as __getitem__, but with a configurable default""" + return self[key] or default def get_resources(self): + """Return all resources, sorted by priority """ + compare_resources = lambda x: x.priority + return sorted(self.resources, key=compare_resources) + + def get_highest_priority_resource(self): + """Return the resource with the highest priority""" + resources = self.get_resources() + if resources: + return resources[-1] + return None + + def toggle_folded(self): """ - Return all resources, sorted by priority + Fold if it's unfolded, and vice versa """ - compare_resources = lambda x: x.priority - return sorted(self._resources, key=compare_resources) + self.folded = not self.folded def __repr__(self): - ret = '<Contact: %s' % self._jid - for resource in self._resources: + ret = '<Contact: %s' % self.bare_jid + for resource in self.resources: ret += '\n\t\t%s' % resource return ret + ' />\n' |