annotate cagou/core/common.py @ 396:ae6f7fd1cb0e

chat: use QuickApp.notify and clear notifications when selected.
author Goffi <goffi@goffi.org>
date Sun, 09 Feb 2020 23:47:29 +0100
parents 442756495a96
children 54f6a47cc60a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
348
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
1 #!/usr/bin/env python3
26
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
2
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
378
4d660b252487 dates update
Goffi <goffi@goffi.org>
parents: 348
diff changeset
4 # Copyright (C) 2016-2020 Jérôme Poisson (goffi@goffi.org)
26
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
5
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
10
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
15
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
18
237
059c5b39032d plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents: 220
diff changeset
19 """common simple widgets"""
26
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
20
348
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
21 import json
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
22 from functools import partial
134
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
23 from sat.core.i18n import _
348
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
24 from sat.core import log as logging
389
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
25 from kivy.uix.widget import Widget
26
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from kivy.uix.image import Image
134
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
27 from kivy.uix.label import Label
26
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from kivy.uix.behaviors import ButtonBehavior
193
284cb5c467b0 core (common): split JidItem in 3 classes:
Goffi <goffi@goffi.org>
parents: 179
diff changeset
29 from kivy.uix.behaviors import ToggleButtonBehavior
107
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
30 from kivy.uix.boxlayout import BoxLayout
348
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
31 from kivy.uix.scrollview import ScrollView
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
32 from kivy.event import EventDispatcher
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
33 from kivy.metrics import dp
134
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
34 from kivy import properties
389
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
35 from sat_frontends.quick_frontend import quick_chat
348
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
36 from cagou.core.constants import Const as C
107
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
37 from cagou import G
134
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
38
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
39 log = logging.getLogger(__name__)
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
40
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
41 UNKNOWN_SYMBOL = 'Unknown symbol name'
26
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
42
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
43
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
44 class IconButton(ButtonBehavior, Image):
3efca1b10b2f common: first draft of a module were common widgets will be put
Goffi <goffi@goffi.org>
parents:
diff changeset
45 pass
107
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
46
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
47
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
48 class Avatar(Image):
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
49 pass
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
50
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
51
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
52 class ContactItem(BoxLayout):
348
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
53 """An item from ContactList
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
54
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
55 The item will drawn as an icon (JID avatar) with its jid below
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
56 """
322
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
57 base_width = dp(150)
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
58 profile = properties.StringProperty()
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
59 data = properties.DictProperty()
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
60 jid = properties.StringProperty('')
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
61
e2b51663d8b8 core, android: new share widget + added Cagou to "share" menu:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
62
348
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
63 class ContactButton(ButtonBehavior, ContactItem):
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
64 pass
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
65
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
66
193
284cb5c467b0 core (common): split JidItem in 3 classes:
Goffi <goffi@goffi.org>
parents: 179
diff changeset
67 class JidItem(BoxLayout):
284cb5c467b0 core (common): split JidItem in 3 classes:
Goffi <goffi@goffi.org>
parents: 179
diff changeset
68 bg_color = properties.ListProperty([0.2, 0.2, 0.2, 1])
284cb5c467b0 core (common): split JidItem in 3 classes:
Goffi <goffi@goffi.org>
parents: 179
diff changeset
69 color = properties.ListProperty([1, 1, 1, 1])
256
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
70 jid = properties.StringProperty()
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
71 profile = properties.StringProperty()
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
72 nick = properties.StringProperty()
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
73 avatar = properties.ObjectProperty()
107
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
74
256
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
75 def on_avatar(self, wid, jid_):
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
76 if self.jid and self.profile:
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
77 self.getImage()
107
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
78
256
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
79 def on_jid(self, wid, jid_):
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
80 if self.profile and self.avatar:
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
81 self.getImage()
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
82
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
83 def on_profile(self, wid, profile):
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
84 if self.jid and self.avatar:
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
85 self.getImage()
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
86
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
87 def getImage(self):
107
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
88 host = G.host
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
89 if host.contact_lists[self.profile].isRoom(self.jid.bare):
256
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
90 self.avatar.opacity = 0
ba7b8cb7ddcd common: don't use anymore positional argument in JidItem
Goffi <goffi@goffi.org>
parents: 247
diff changeset
91 self.avatar.source = ""
107
f0cf44df8486 JidWidget: first draft
Goffi <goffi@goffi.org>
parents: 26
diff changeset
92 else:
273
0ef216091f2b core: removed forgotten debugging code
Goffi <goffi@goffi.org>
parents: 256
diff changeset
93 self.avatar.source = (
0ef216091f2b core: removed forgotten debugging code
Goffi <goffi@goffi.org>
parents: 256
diff changeset
94 host.getAvatar(self.jid, profile=self.profile)
0ef216091f2b core: removed forgotten debugging code
Goffi <goffi@goffi.org>
parents: 256
diff changeset
95 or host.getDefaultAvatar(self.jid)
0ef216091f2b core: removed forgotten debugging code
Goffi <goffi@goffi.org>
parents: 256
diff changeset
96 )
134
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
97
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
98
193
284cb5c467b0 core (common): split JidItem in 3 classes:
Goffi <goffi@goffi.org>
parents: 179
diff changeset
99 class JidButton(ButtonBehavior, JidItem):
284cb5c467b0 core (common): split JidItem in 3 classes:
Goffi <goffi@goffi.org>
parents: 179
diff changeset
100 pass
284cb5c467b0 core (common): split JidItem in 3 classes:
Goffi <goffi@goffi.org>
parents: 179
diff changeset
101
284cb5c467b0 core (common): split JidItem in 3 classes:
Goffi <goffi@goffi.org>
parents: 179
diff changeset
102
284cb5c467b0 core (common): split JidItem in 3 classes:
Goffi <goffi@goffi.org>
parents: 179
diff changeset
103 class JidToggle(ToggleButtonBehavior, JidItem):
284cb5c467b0 core (common): split JidItem in 3 classes:
Goffi <goffi@goffi.org>
parents: 179
diff changeset
104 selected_color = properties.ListProperty(C.COLOR_SEC_DARK)
284cb5c467b0 core (common): split JidItem in 3 classes:
Goffi <goffi@goffi.org>
parents: 179
diff changeset
105
284cb5c467b0 core (common): split JidItem in 3 classes:
Goffi <goffi@goffi.org>
parents: 179
diff changeset
106
134
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
107 class Symbol(Label):
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
108 symbol_map = None
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
109 symbol = properties.StringProperty()
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
110
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
111 def __init__(self, **kwargs):
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
112 if self.symbol_map is None:
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
113 with open(G.host.app.expand('{media}/fonts/fontello/config.json')) as f:
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
114 fontello_conf = json.load(f)
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
115 Symbol.symbol_map = {g['css']:g['code'] for g in fontello_conf['glyphs']}
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
116
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
117 super(Symbol, self).__init__(**kwargs)
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
118
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
119 def on_symbol(self, instance, symbol):
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
120 try:
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
121 code = self.symbol_map[symbol]
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
122 except KeyError:
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
123 log.warning(_("Invalid symbol {symbol}").format(symbol=symbol))
134
1cca97e27a69 core (common): new Symbol widget:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
124 else:
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
125 self.text = chr(code)
135
091e288838e1 plugin chat: use new Symbol widget to display encryption button
Goffi <goffi@goffi.org>
parents: 134
diff changeset
126
091e288838e1 plugin chat: use new Symbol widget to display encryption button
Goffi <goffi@goffi.org>
parents: 134
diff changeset
127
091e288838e1 plugin chat: use new Symbol widget to display encryption button
Goffi <goffi@goffi.org>
parents: 134
diff changeset
128 class SymbolButton(ButtonBehavior, Symbol):
091e288838e1 plugin chat: use new Symbol widget to display encryption button
Goffi <goffi@goffi.org>
parents: 134
diff changeset
129 pass
178
11ff8cd93659 common: new ActionSymbol widget, which use a symbol to show an action
Goffi <goffi@goffi.org>
parents: 145
diff changeset
130
11ff8cd93659 common: new ActionSymbol widget, which use a symbol to show an action
Goffi <goffi@goffi.org>
parents: 145
diff changeset
131
218
30be583dbabc core (common): new SymbolLabel class to have a clickable area with a symbol and a label next to it.
Goffi <goffi@goffi.org>
parents: 193
diff changeset
132 class SymbolLabel(ButtonBehavior, BoxLayout):
30be583dbabc core (common): new SymbolLabel class to have a clickable area with a symbol and a label next to it.
Goffi <goffi@goffi.org>
parents: 193
diff changeset
133 symbol = properties.StringProperty("")
30be583dbabc core (common): new SymbolLabel class to have a clickable area with a symbol and a label next to it.
Goffi <goffi@goffi.org>
parents: 193
diff changeset
134 text = properties.StringProperty("")
30be583dbabc core (common): new SymbolLabel class to have a clickable area with a symbol and a label next to it.
Goffi <goffi@goffi.org>
parents: 193
diff changeset
135 color = properties.ListProperty(C.COLOR_SEC)
30be583dbabc core (common): new SymbolLabel class to have a clickable area with a symbol and a label next to it.
Goffi <goffi@goffi.org>
parents: 193
diff changeset
136 bold = properties.BooleanProperty(True)
30be583dbabc core (common): new SymbolLabel class to have a clickable area with a symbol and a label next to it.
Goffi <goffi@goffi.org>
parents: 193
diff changeset
137 symbol_wid = properties.ObjectProperty()
30be583dbabc core (common): new SymbolLabel class to have a clickable area with a symbol and a label next to it.
Goffi <goffi@goffi.org>
parents: 193
diff changeset
138 label = properties.ObjectProperty()
30be583dbabc core (common): new SymbolLabel class to have a clickable area with a symbol and a label next to it.
Goffi <goffi@goffi.org>
parents: 193
diff changeset
139
30be583dbabc core (common): new SymbolLabel class to have a clickable area with a symbol and a label next to it.
Goffi <goffi@goffi.org>
parents: 193
diff changeset
140
178
11ff8cd93659 common: new ActionSymbol widget, which use a symbol to show an action
Goffi <goffi@goffi.org>
parents: 145
diff changeset
141 class ActionSymbol(Symbol):
11ff8cd93659 common: new ActionSymbol widget, which use a symbol to show an action
Goffi <goffi@goffi.org>
parents: 145
diff changeset
142 pass
179
7177fe2d9725 common: new ActionIcon widget which display symbol or image icon according to what is specified in plugin_info
Goffi <goffi@goffi.org>
parents: 178
diff changeset
143
7177fe2d9725 common: new ActionIcon widget which display symbol or image icon according to what is specified in plugin_info
Goffi <goffi@goffi.org>
parents: 178
diff changeset
144
7177fe2d9725 common: new ActionIcon widget which display symbol or image icon according to what is specified in plugin_info
Goffi <goffi@goffi.org>
parents: 178
diff changeset
145 class ActionIcon(BoxLayout):
7177fe2d9725 common: new ActionIcon widget which display symbol or image icon according to what is specified in plugin_info
Goffi <goffi@goffi.org>
parents: 178
diff changeset
146 plugin_info = properties.DictProperty()
7177fe2d9725 common: new ActionIcon widget which display symbol or image icon according to what is specified in plugin_info
Goffi <goffi@goffi.org>
parents: 178
diff changeset
147
7177fe2d9725 common: new ActionIcon widget which display symbol or image icon according to what is specified in plugin_info
Goffi <goffi@goffi.org>
parents: 178
diff changeset
148 def on_plugin_info(self, instance, plugin_info):
7177fe2d9725 common: new ActionIcon widget which display symbol or image icon according to what is specified in plugin_info
Goffi <goffi@goffi.org>
parents: 178
diff changeset
149 self.clear_widgets()
7177fe2d9725 common: new ActionIcon widget which display symbol or image icon according to what is specified in plugin_info
Goffi <goffi@goffi.org>
parents: 178
diff changeset
150 try:
7177fe2d9725 common: new ActionIcon widget which display symbol or image icon according to what is specified in plugin_info
Goffi <goffi@goffi.org>
parents: 178
diff changeset
151 symbol = plugin_info['icon_symbol']
7177fe2d9725 common: new ActionIcon widget which display symbol or image icon according to what is specified in plugin_info
Goffi <goffi@goffi.org>
parents: 178
diff changeset
152 except KeyError:
7177fe2d9725 common: new ActionIcon widget which display symbol or image icon according to what is specified in plugin_info
Goffi <goffi@goffi.org>
parents: 178
diff changeset
153 icon_src = plugin_info['icon_medium']
7177fe2d9725 common: new ActionIcon widget which display symbol or image icon according to what is specified in plugin_info
Goffi <goffi@goffi.org>
parents: 178
diff changeset
154 icon_wid = Image(source=icon_src, allow_stretch=True)
7177fe2d9725 common: new ActionIcon widget which display symbol or image icon according to what is specified in plugin_info
Goffi <goffi@goffi.org>
parents: 178
diff changeset
155 self.add_widget(icon_wid)
7177fe2d9725 common: new ActionIcon widget which display symbol or image icon according to what is specified in plugin_info
Goffi <goffi@goffi.org>
parents: 178
diff changeset
156 else:
7177fe2d9725 common: new ActionIcon widget which display symbol or image icon according to what is specified in plugin_info
Goffi <goffi@goffi.org>
parents: 178
diff changeset
157 icon_wid = ActionSymbol(symbol=symbol)
7177fe2d9725 common: new ActionIcon widget which display symbol or image icon according to what is specified in plugin_info
Goffi <goffi@goffi.org>
parents: 178
diff changeset
158 self.add_widget(icon_wid)
348
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
159
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
160
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
161 class JidSelector(ScrollView, EventDispatcher):
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
162 layout = properties.ObjectProperty(None)
389
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
163 # list of item to show, can be:
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
164 # - a well-known string like:
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
165 # * "roster": to show all roster jids
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
166 # * "opened_chats": to show jids of all opened chat widgets
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
167 # - a kivy Widget, which will be added to the layout (notable useful with
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
168 # common_widgets.CategorySeparator)
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
169 # - a callable, which must return an iterable of kwargs for ContactButton
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
170 to_show = properties.ListProperty(['roster'])
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
171 # if True, update() is called automatically when widget is created
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
172 # if False, you'll have to call update() at least once manually
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
173 implicit_update = properties.ObjectProperty(True)
348
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
174
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
175 def __init__(self, **kwargs):
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
176 self.register_event_type('on_select')
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
177 super().__init__(**kwargs)
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
178
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
179 def on_kv_post(self, wid):
389
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
180 if self.implicit_update:
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
181 self.update()
348
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
182
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
183 def on_select(self, wid):
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
184 pass
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
185
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
186 def on_parent(self, wid, parent):
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
187 if parent is None:
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
188 log.debug("removing contactsFilled listener")
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
189 G.host.removeListener("contactsFilled", self.onContactsFilled)
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
190 else:
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
191 G.host.addListener("contactsFilled", self.onContactsFilled)
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
192
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
193 def onContactsFilled(self, profile):
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
194 log.debug("onContactsFilled event received")
389
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
195 self.update()
348
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
196
389
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
197 def update(self):
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
198 log.debug("starting update")
348
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
199 self.layout.clear_widgets()
389
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
200 for item in self.to_show:
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
201 if isinstance(item, str):
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
202 if item == 'roster':
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
203 self.addRosterItems()
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
204 elif item == 'bookmarks':
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
205 self.addBookmarksItems()
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
206 elif item == 'opened_chats':
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
207 self.addOpenedChatsItems()
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
208 else:
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
209 log.error(f'unknown "to_show" magic string {item!r}')
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
210 elif isinstance(item, Widget):
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
211 self.layout.add_widget(item)
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
212 elif callable(item):
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
213 items_kwargs = item()
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
214 for item_kwargs in items_kwargs:
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
215 item = ContactButton(**item_kwargs)
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
216 item.bind(on_press=partial(self.dispatch, 'on_select'))
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
217 self.layout.add_widget(item)
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
218 else:
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
219 log.error(f"unmanaged to_show item type: {item!r}")
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
220
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
221 def addOpenedChatsItems(self):
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
222 opened_chats = G.host.widgets.getWidgets(
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
223 quick_chat.QuickChat,
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
224 profiles = G.host.profiles)
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
225
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
226 for wid in opened_chats:
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
227 contact_list = G.host.contact_lists[wid.profile]
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
228 try:
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
229 item = ContactButton(
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
230 jid=wid.target,
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
231 data=contact_list.getItem(wid.target),
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
232 profile=wid.profile,
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
233 )
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
234 except Exception as e:
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
235 log.warning(f"Can't add contact {wid.target}: {e}")
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
236 continue
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
237 item.bind(on_press=partial(self.dispatch, 'on_select'))
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
238 self.layout.add_widget(item)
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
239
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
240 def addRosterItems(self):
348
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
241 for profile in G.host.profiles:
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
242 contact_list = G.host.contact_lists[profile]
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
243 for entity_jid in sorted(contact_list.roster):
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
244 item = ContactButton(
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
245 jid=entity_jid,
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
246 data=contact_list.getItem(entity_jid),
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
247 profile=profile,
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
248 )
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
249 item.bind(on_press=partial(self.dispatch, 'on_select'))
38fd457b2158 core (common, share_widget): new JidSelector widget:
Goffi <goffi@goffi.org>
parents: 322
diff changeset
250 self.layout.add_widget(item)
389
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
251
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
252 def addBookmarksItems(self):
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
253 for profile in G.host.profiles:
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
254 profile_manager = G.host.profiles[profile]
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
255 try:
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
256 bookmarks = profile_manager._bookmarks
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
257 except AttributeError:
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
258 log.warning(f"no bookmark in cache for profile {profile}")
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
259 continue
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
260
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
261 contact_list = G.host.contact_lists[profile]
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
262 for entity_jid in bookmarks:
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
263 try:
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
264 cache = contact_list.getItem(entity_jid)
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
265 except KeyError:
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
266 cache = {}
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
267 item = ContactButton(
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
268 jid=entity_jid,
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
269 data=cache,
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
270 profile=profile,
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
271 )
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
272 item.bind(on_press=partial(self.dispatch, 'on_select'))
442756495a96 core (common): improved flexibility of jid selector:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
273 self.layout.add_widget(item)