comparison sat_frontends/quick_frontend/quick_contact_management.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents frontends/src/quick_frontend/quick_contact_management.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # helper class for making a SAT frontend
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.i18n import _
21 from sat.core.log import getLogger
22 log = getLogger(__name__)
23 from sat_frontends.tools.jid import JID
24
25
26 class QuickContactManagement(object):
27 """This helper class manage the contacts and ease the use of nicknames and shortcuts"""
28 ### FIXME: is SàT a better place for all this stuff ??? ###
29
30 def __init__(self):
31 self.__contactlist = {}
32
33 def __contains__(self, entity):
34 return entity.bare in self.__contactlist
35
36 def clear(self):
37 """Clear all the contact list"""
38 self.__contactlist.clear()
39
40 def add(self, entity):
41 """Add contact to the list, update resources"""
42 if not self.__contactlist.has_key(entity.bare):
43 self.__contactlist[entity.bare] = {'resources':[]}
44 if not entity.resource:
45 return
46 if entity.resource in self.__contactlist[entity.bare]['resources']:
47 self.__contactlist[entity.bare]['resources'].remove(entity.resource)
48 self.__contactlist[entity.bare]['resources'].append(entity.resource)
49
50 def getContFromGroup(self, group):
51 """Return all contacts which are in given group"""
52 result = []
53 for contact in self.__contactlist:
54 if self.__contactlist[contact].has_key('groups'):
55 if group in self.__contactlist[contact]['groups']:
56 result.append(JID(contact))
57 return result
58
59 def getAttr(self, entity, name):
60 """Return a specific attribute of contact, or all attributes
61 @param entity: jid of the contact
62 @param name: name of the attribute
63 @return: asked attribute"""
64 if self.__contactlist.has_key(entity.bare):
65 if name == 'status': #FIXME: for the moment, we only use the first status
66 if self.__contactlist[entity.bare]['statuses']:
67 return self.__contactlist[entity.bare]['statuses'].values()[0]
68 if self.__contactlist[entity.bare].has_key(name):
69 return self.__contactlist[entity.bare][name]
70 else:
71 log.debug(_('Trying to get attribute for an unknown contact'))
72 return None
73
74 def isConnected(self, entity):
75 """Tell if the contact is online"""
76 return self.__contactlist.has_key(entity.bare)
77
78 def remove(self, entity):
79 """remove resource. If no more resource is online or is no resource is specified, contact is deleted"""
80 try:
81 if entity.resource:
82 self.__contactlist[entity.bare]['resources'].remove(entity.resource)
83 if not entity.resource or not self.__contactlist[entity.bare]['resources']:
84 #no more resource available: the contact seems really disconnected
85 del self.__contactlist[entity.bare]
86 except KeyError:
87 log.error(_('INTERNAL ERROR: Key log.error'))
88 raise
89
90 def update(self, entity, key, value):
91 """Update attribute of contact
92 @param entity: jid of the contact
93 @param key: name of the attribute
94 @param value: value of the attribute
95 """
96 if self.__contactlist.has_key(entity.bare):
97 self.__contactlist[entity.bare][key] = value
98 else:
99 log.debug (_('Trying to update an unknown contact: %s') % entity.bare)
100
101 def get_full(self, entity):
102 return entity.bare+'/'+self.__contactlist[entity.bare]['resources'][-1]
103