Mercurial > libervia-backend
annotate frontends/src/quick_frontend/quick_contact_management.py @ 1656:069abd15354f
plugin XEP-0277: checkFeatures implemented
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 23 Nov 2015 23:30:41 +0100 |
parents | 069ad98b360d |
children | d17772b0fe22 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
588
diff
changeset
|
4 # helper class for making a SAT frontend |
1396 | 5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org) |
0 | 6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
588
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
588
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
588
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
588
diff
changeset
|
10 # (at your option) any later version. |
0 | 11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
588
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
588
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
588
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
588
diff
changeset
|
15 # GNU Affero General Public License for more details. |
0 | 16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
588
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
588
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
0 | 19 |
771 | 20 from sat.core.i18n import _ |
1009
d1084f7e56a5
quick_frontend: use of new logging system
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
21 from sat.core.log import getLogger |
d1084f7e56a5
quick_frontend: use of new logging system
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
22 log = getLogger(__name__) |
1139
75025461141f
move sat.tools.jid to sat_frontends.tools.jid
souliane <souliane@mailoo.org>
parents:
1009
diff
changeset
|
23 from sat_frontends.tools.jid import JID |
0 | 24 |
25 | |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
26 class QuickContactManagement(object): |
0 | 27 """This helper class manage the contacts and ease the use of nicknames and shortcuts""" |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
28 ### FIXME: is SàT a better place for all this stuff ??? ### |
0 | 29 |
30 def __init__(self): | |
31 self.__contactlist = {} | |
32 | |
347
ea3e1b82dd79
core: contact deletion from roster if we have no subscription to it (behaviour may change in futur)
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
33 def __contains__(self, entity): |
688
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
34 return entity.bare in self.__contactlist |
347
ea3e1b82dd79
core: contact deletion from roster if we have no subscription to it (behaviour may change in futur)
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
35 |
52 | 36 def clear(self): |
37 """Clear all the contact list""" | |
38 self.__contactlist.clear() | |
39 | |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
40 def add(self, entity): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
41 """Add contact to the list, update resources""" |
688
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
42 if not self.__contactlist.has_key(entity.bare): |
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
43 self.__contactlist[entity.bare] = {'resources':[]} |
347
ea3e1b82dd79
core: contact deletion from roster if we have no subscription to it (behaviour may change in futur)
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
44 if not entity.resource: |
ea3e1b82dd79
core: contact deletion from roster if we have no subscription to it (behaviour may change in futur)
Goffi <goffi@goffi.org>
parents:
228
diff
changeset
|
45 return |
688
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
46 if entity.resource in self.__contactlist[entity.bare]['resources']: |
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
47 self.__contactlist[entity.bare]['resources'].remove(entity.resource) |
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
48 self.__contactlist[entity.bare]['resources'].append(entity.resource) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
49 |
54
2ce9e350cdf9
Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents:
52
diff
changeset
|
50 def getContFromGroup(self, group): |
2ce9e350cdf9
Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents:
52
diff
changeset
|
51 """Return all contacts which are in given group""" |
2ce9e350cdf9
Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents:
52
diff
changeset
|
52 result = [] |
2ce9e350cdf9
Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents:
52
diff
changeset
|
53 for contact in self.__contactlist: |
2ce9e350cdf9
Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents:
52
diff
changeset
|
54 if self.__contactlist[contact].has_key('groups'): |
2ce9e350cdf9
Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents:
52
diff
changeset
|
55 if group in self.__contactlist[contact]['groups']: |
2ce9e350cdf9
Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents:
52
diff
changeset
|
56 result.append(JID(contact)) |
2ce9e350cdf9
Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents:
52
diff
changeset
|
57 return result |
2ce9e350cdf9
Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents:
52
diff
changeset
|
58 |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
59 def getAttr(self, entity, name): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
60 """Return a specific attribute of contact, or all attributes |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
61 @param entity: jid of the contact |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
62 @param name: name of the attribute |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
63 @return: asked attribute""" |
688
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
64 if self.__contactlist.has_key(entity.bare): |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
65 if name == 'status': #FIXME: for the moment, we only use the first status |
688
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
66 if self.__contactlist[entity.bare]['statuses']: |
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
67 return self.__contactlist[entity.bare]['statuses'].values()[0] |
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
68 if self.__contactlist[entity.bare].has_key(name): |
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
69 return self.__contactlist[entity.bare][name] |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
70 else: |
1009
d1084f7e56a5
quick_frontend: use of new logging system
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
71 log.debug(_('Trying to get attribute for an unknown contact')) |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
72 return None |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
73 |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
74 def isConnected(self, entity): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
75 """Tell if the contact is online""" |
688
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
76 return self.__contactlist.has_key(entity.bare) |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
77 |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
78 def remove(self, entity): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
79 """remove resource. If no more resource is online or is no resource is specified, contact is deleted""" |
0 | 80 try: |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
81 if entity.resource: |
688
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
82 self.__contactlist[entity.bare]['resources'].remove(entity.resource) |
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
83 if not entity.resource or not self.__contactlist[entity.bare]['resources']: |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
84 #no more resource available: the contact seems really disconnected |
688
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
85 del self.__contactlist[entity.bare] |
0 | 86 except KeyError: |
1009
d1084f7e56a5
quick_frontend: use of new logging system
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
87 log.error(_('INTERNAL ERROR: Key log.error')) |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
88 raise |
0 | 89 |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
90 def update(self, entity, key, value): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
91 """Update attribute of contact |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
92 @param entity: jid of the contact |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
93 @param key: name of the attribute |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
94 @param value: value of the attribute |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
95 """ |
688
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
96 if self.__contactlist.has_key(entity.bare): |
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
97 self.__contactlist[entity.bare][key] = value |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
98 else: |
1009
d1084f7e56a5
quick_frontend: use of new logging system
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
99 log.debug (_('Trying to update an unknown contact: %s') % entity.bare) |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
100 |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
101 def get_full(self, entity): |
688
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
609
diff
changeset
|
102 return entity.bare+'/'+self.__contactlist[entity.bare]['resources'][-1] |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
103 |