Mercurial > libervia-backend
comparison libervia/frontends/quick_frontend/quick_contact_management.py @ 4074:26b7ed2817da
refactoring: rename `sat_frontends` to `libervia.frontends`
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 14:12:38 +0200 |
parents | sat_frontends/quick_frontend/quick_contact_management.py@4b842c1fb686 |
children |
comparison
equal
deleted
inserted
replaced
4073:7c5654c54fed | 4074:26b7ed2817da |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 # helper class for making a SAT frontend | |
5 # Copyright (C) 2009-2021 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 libervia.backend.core.i18n import _ | |
21 from libervia.backend.core.log import getLogger | |
22 | |
23 log = getLogger(__name__) | |
24 from libervia.frontends.tools.jid import JID | |
25 | |
26 | |
27 class QuickContactManagement(object): | |
28 """This helper class manage the contacts and ease the use of nicknames and shortcuts""" | |
29 | |
30 ### FIXME: is SàT a better place for all this stuff ??? ### | |
31 | |
32 def __init__(self): | |
33 self.__contactlist = {} | |
34 | |
35 def __contains__(self, entity): | |
36 return entity.bare in self.__contactlist | |
37 | |
38 def clear(self): | |
39 """Clear all the contact list""" | |
40 self.__contactlist.clear() | |
41 | |
42 def add(self, entity): | |
43 """Add contact to the list, update resources""" | |
44 if entity.bare not in self.__contactlist: | |
45 self.__contactlist[entity.bare] = {"resources": []} | |
46 if not entity.resource: | |
47 return | |
48 if entity.resource in self.__contactlist[entity.bare]["resources"]: | |
49 self.__contactlist[entity.bare]["resources"].remove(entity.resource) | |
50 self.__contactlist[entity.bare]["resources"].append(entity.resource) | |
51 | |
52 def get_cont_from_group(self, group): | |
53 """Return all contacts which are in given group""" | |
54 result = [] | |
55 for contact in self.__contactlist: | |
56 if "groups" in self.__contactlist[contact]: | |
57 if group in self.__contactlist[contact]["groups"]: | |
58 result.append(JID(contact)) | |
59 return result | |
60 | |
61 def get_attr(self, entity, name): | |
62 """Return a specific attribute of contact, or all attributes | |
63 @param entity: jid of the contact | |
64 @param name: name of the attribute | |
65 @return: asked attribute""" | |
66 if entity.bare in self.__contactlist: | |
67 if name == "status": # FIXME: for the moment, we only use the first status | |
68 if self.__contactlist[entity.bare]["statuses"]: | |
69 return list(self.__contactlist[entity.bare]["statuses"].values())[0] | |
70 if name in self.__contactlist[entity.bare]: | |
71 return self.__contactlist[entity.bare][name] | |
72 else: | |
73 log.debug(_("Trying to get attribute for an unknown contact")) | |
74 return None | |
75 | |
76 def is_connected(self, entity): | |
77 """Tell if the contact is online""" | |
78 return entity.bare in self.__contactlist | |
79 | |
80 def remove(self, entity): | |
81 """remove resource. If no more resource is online or is no resource is specified, contact is deleted""" | |
82 try: | |
83 if entity.resource: | |
84 self.__contactlist[entity.bare]["resources"].remove(entity.resource) | |
85 if not entity.resource or not self.__contactlist[entity.bare]["resources"]: | |
86 # no more resource available: the contact seems really disconnected | |
87 del self.__contactlist[entity.bare] | |
88 except KeyError: | |
89 log.error(_("INTERNAL ERROR: Key log.error")) | |
90 raise | |
91 | |
92 def update(self, entity, key, value): | |
93 """Update attribute of contact | |
94 @param entity: jid of the contact | |
95 @param key: name of the attribute | |
96 @param value: value of the attribute | |
97 """ | |
98 if entity.bare in self.__contactlist: | |
99 self.__contactlist[entity.bare][key] = value | |
100 else: | |
101 log.debug(_("Trying to update an unknown contact: %s") % entity.bare) | |
102 | |
103 def get_full(self, entity): | |
104 return entity.bare + "/" + self.__contactlist[entity.bare]["resources"][-1] |