Mercurial > libervia-backend
annotate frontends/src/quick_frontend/quick_contact_management.py @ 459:cf005701624b
copyleft date update
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 06 Mar 2012 09:07:24 +0100 |
parents | ea3e1b82dd79 |
children | 2a072735e459 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 helper class for making a SAT frontend | |
459 | 6 Copyright (C) 2009, 2010, 2011, 2012 Jérôme Poisson (goffi@goffi.org) |
0 | 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 | |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
22 from logging import debug, info, warning, error |
225
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
23 from sat.tools.jid import JID |
0 | 24 import pdb |
25 | |
26 | |
27 class QuickContactManagement(): | |
28 """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
|
29 ### FIXME: is SàT a better place for all this stuff ??? ### |
0 | 30 |
31 def __init__(self): | |
32 self.__contactlist = {} | |
33 | |
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
|
34 def __contains__(self, entity): |
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 return entity.short in self.__contactlist |
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
|
36 |
52 | 37 def clear(self): |
38 """Clear all the contact list""" | |
39 self.__contactlist.clear() | |
40 | |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
41 def add(self, entity): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
42 """Add contact to the list, update resources""" |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
43 if not self.__contactlist.has_key(entity.short): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
44 self.__contactlist[entity.short] = {'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
|
45 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
|
46 return |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
47 if entity.resource in self.__contactlist[entity.short]['resources']: |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
48 self.__contactlist[entity.short]['resources'].remove(entity.resource) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
49 self.__contactlist[entity.short]['resources'].append(entity.resource) |
54
2ce9e350cdf9
Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents:
52
diff
changeset
|
50 |
2ce9e350cdf9
Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents:
52
diff
changeset
|
51 def getContFromGroup(self, group): |
2ce9e350cdf9
Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents:
52
diff
changeset
|
52 """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
|
53 result = [] |
2ce9e350cdf9
Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents:
52
diff
changeset
|
54 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
|
55 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
|
56 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
|
57 result.append(JID(contact)) |
2ce9e350cdf9
Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents:
52
diff
changeset
|
58 return result |
2ce9e350cdf9
Wix: clicking on group in contact_list now (un)hide it.
Goffi <goffi@goffi.org>
parents:
52
diff
changeset
|
59 |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
60 def getAttr(self, entity, name): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
61 """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
|
62 @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
|
63 @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
|
64 @return: asked attribute""" |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
65 if self.__contactlist.has_key(entity.short): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
66 if name == 'status': #FIXME: for the moment, we only use the first status |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
67 if self.__contactlist[entity.short]['statuses']: |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
68 return self.__contactlist[entity.short]['statuses'].values()[0] |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
69 if self.__contactlist[entity.short].has_key(name): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
70 return self.__contactlist[entity.short][name] |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
71 else: |
70 | 72 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
|
73 return None |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
74 |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
75 def isConnected(self, entity): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
76 """Tell if the contact is online""" |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
77 return self.__contactlist.has_key(entity.short) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
78 |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
79 def remove(self, entity): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
80 """remove resource. If no more resource is online or is no resource is specified, contact is deleted""" |
0 | 81 try: |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
82 if entity.resource: |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
83 self.__contactlist[entity.short]['resources'].remove(entity.resource) |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
84 if not entity.resource or not self.__contactlist[entity.short]['resources']: |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
85 #no more resource available: the contact seems really disconnected |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
86 del self.__contactlist[entity.short] |
0 | 87 except KeyError: |
119
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
88 error(_('INTERNAL ERROR: Key error')) |
ded2431cea5a
Primitivus: chat window / text sending.
Goffi <goffi@goffi.org>
parents:
70
diff
changeset
|
89 raise |
0 | 90 |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
91 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
|
92 """Update attribute of contact |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
93 @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
|
94 @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
|
95 @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
|
96 """ |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
97 if self.__contactlist.has_key(entity.short): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
98 self.__contactlist[entity.short][key] = value |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
99 else: |
70 | 100 debug (_('Trying to update an unknown contact: %s'), entity.short) |
51
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
101 |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
102 def get_full(self, entity): |
8c67ea98ab91
frontend improved to take into account new SàT features
Goffi <goffi@goffi.org>
parents:
0
diff
changeset
|
103 return entity.short+'/'+self.__contactlist[entity.short]['resources'][-1] |
0 | 104 |