comparison frontends/src/quick_frontend/quick_contact_management.py @ 223:86d249b6d9b7

Files reorganisation
author Goffi <goffi@goffi.org>
date Wed, 29 Dec 2010 01:06:29 +0100
parents frontends/quick_frontend/quick_contact_management.py@ded2431cea5a
children fd9b7834d98a
comparison
equal deleted inserted replaced
222:3198bfd66daa 223:86d249b6d9b7
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 helper class for making a SAT frontend
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22 from logging import debug, info, warning, error
23 from tools.jid import JID
24 import pdb
25
26
27 class QuickContactManagement():
28 """This helper class manage the contacts and ease the use of nicknames and shortcuts"""
29 ### FIXME: is SàT a better place for all this stuff ??? ###
30
31 def __init__(self):
32 self.__contactlist = {}
33
34 def clear(self):
35 """Clear all the contact list"""
36 self.__contactlist.clear()
37
38 def add(self, entity):
39 """Add contact to the list, update resources"""
40 if not self.__contactlist.has_key(entity.short):
41 self.__contactlist[entity.short] = {'resources':[]}
42 if entity.resource in self.__contactlist[entity.short]['resources']:
43 self.__contactlist[entity.short]['resources'].remove(entity.resource)
44 self.__contactlist[entity.short]['resources'].append(entity.resource)
45
46 def getContFromGroup(self, group):
47 """Return all contacts which are in given group"""
48 result = []
49 for contact in self.__contactlist:
50 if self.__contactlist[contact].has_key('groups'):
51 if group in self.__contactlist[contact]['groups']:
52 result.append(JID(contact))
53 return result
54
55 def getAttr(self, entity, name):
56 """Return a specific attribute of contact, or all attributes
57 @param entity: jid of the contact
58 @param name: name of the attribute
59 @return: asked attribute"""
60 if self.__contactlist.has_key(entity.short):
61 if name == 'status': #FIXME: for the moment, we only use the first status
62 if self.__contactlist[entity.short]['statuses']:
63 return self.__contactlist[entity.short]['statuses'].values()[0]
64 if self.__contactlist[entity.short].has_key(name):
65 return self.__contactlist[entity.short][name]
66 else:
67 debug(_('Trying to get attribute for an unknown contact'))
68 return None
69
70 def isConnected(self, entity):
71 """Tell if the contact is online"""
72 return self.__contactlist.has_key(entity.short)
73
74 def remove(self, entity):
75 """remove resource. If no more resource is online or is no resource is specified, contact is deleted"""
76 try:
77 if entity.resource:
78 self.__contactlist[entity.short]['resources'].remove(entity.resource)
79 if not entity.resource or not self.__contactlist[entity.short]['resources']:
80 #no more resource available: the contact seems really disconnected
81 del self.__contactlist[entity.short]
82 except KeyError:
83 error(_('INTERNAL ERROR: Key error'))
84 raise
85
86 def update(self, entity, key, value):
87 """Update attribute of contact
88 @param entity: jid of the contact
89 @param key: name of the attribute
90 @param value: value of the attribute
91 """
92 if self.__contactlist.has_key(entity.short):
93 self.__contactlist[entity.short][key] = value
94 else:
95 debug (_('Trying to update an unknown contact: %s'), entity.short)
96
97 def get_full(self, entity):
98 return entity.short+'/'+self.__contactlist[entity.short]['resources'][-1]
99