Mercurial > libervia-desktop-kivy
annotate src/cagou/plugins/plugin_wid_chat.py @ 81:3608fef1857d
core: use norm_image_size for headers'icons
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 24 Dec 2016 14:03:48 +0100 |
parents | 46d962910801 |
children | c711be670ecd |
rev | line source |
---|---|
22 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client | |
5 # Copyright (C) 2016 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 | |
21 from sat.core import log as logging | |
22 log = logging.getLogger(__name__) | |
23 from sat.core.i18n import _ | |
24 from cagou.core.constants import Const as C | |
78 | 25 from kivy.uix.boxlayout import BoxLayout |
45 | 26 from kivy.uix.gridlayout import GridLayout |
57 | 27 from kivy.uix.stacklayout import StackLayout |
22 | 28 from kivy.uix.scrollview import ScrollView |
29 from kivy.uix.textinput import TextInput | |
57 | 30 from kivy.uix.label import Label |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
31 from kivy.uix.image import AsyncImage |
45 | 32 from kivy.metrics import dp |
57 | 33 from kivy.utils import escape_markup |
22 | 34 from kivy import properties |
35 from sat_frontends.quick_frontend import quick_widgets | |
36 from sat_frontends.quick_frontend import quick_chat | |
57 | 37 from sat_frontends.tools import jid, css_color |
22 | 38 from cagou.core import cagou_widget |
44
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
39 from cagou.core.image import Image |
22 | 40 from cagou import G |
57 | 41 from xml.etree import ElementTree as ET |
22 | 42 |
43 | |
44 PLUGIN_INFO = { | |
45 "name": _(u"chat"), | |
46 "main": "Chat", | |
47 "description": _(u"instant messaging with one person or a group"), | |
25
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
48 "icon_small": u"{media}/icons/muchoslava/png/chat_rouge_32.png", |
d09bd16dbbe2
code (cagou widget), selector: icons handling + use of new muchoslava icon set
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
49 "icon_medium": u"{media}/icons/muchoslava/png/chat_rouge_44.png" |
22 | 50 } |
51 | |
52 | |
44
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
53 class MessAvatar(Image): |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
54 pass |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
55 |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
56 |
57 | 57 class Escape(unicode): |
58 """Class used to mark that a message need to be escaped""" | |
59 | |
60 def __init__(self, text): | |
61 super(Escape, self).__init__(text) | |
62 | |
63 | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
64 class SimpleXHTMLWidgetEscapedText(Label): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
65 pass |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
66 |
59 | 67 |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
68 class SimpleXHTMLWidgetText(Label): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
69 pass |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
70 |
59 | 71 |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
72 class SimpleXHTMLWidgetImage(AsyncImage): |
59 | 73 # following properties are desired height/width |
74 # i.e. the ones specified in height/width attributes of <img> | |
75 # (or wanted for whatever reason) | |
76 # set to 0 to ignore them | |
77 target_height = properties.NumericProperty() | |
78 target_width = properties.NumericProperty() | |
79 | |
80 def _get_parent_container(self): | |
81 """get parent SimpleXHTMLWidget instance | |
82 | |
83 @param warning(bool): if True display a log.error if nothing found | |
84 @return (SimpleXHTMLWidget, None): found SimpleXHTMLWidget instance | |
85 """ | |
86 parent = self.parent | |
87 while parent and not isinstance(parent, SimpleXHTMLWidget): | |
88 parent = parent.parent | |
89 if parent is None: | |
90 log.error(u"no SimpleXHTMLWidget parent found") | |
91 return parent | |
92 | |
93 def _on_source_load(self, value): | |
94 # this method is called when image is loaded | |
95 super(SimpleXHTMLWidgetImage, self)._on_source_load(value) | |
96 if self.parent is not None: | |
97 container = self._get_parent_container() | |
98 # image is loaded, we need to recalculate size | |
99 self.on_container_width(container, container.width) | |
100 | |
101 def on_container_width(self, container, container_width): | |
102 """adapt size according to container width | |
103 | |
104 called when parent container (SimpleXHTMLWidget) width change | |
105 """ | |
106 target_size = (self.target_width or self.texture.width, self.target_height or self.texture.height) | |
107 padding = container.padding | |
108 padding_h = (padding[0] + padding[2]) if len(padding) == 4 else padding[0] | |
109 width = container_width - padding_h | |
110 if target_size[0] < width: | |
111 self.size = target_size | |
112 else: | |
113 height = width / self.image_ratio | |
114 self.size = (width, height) | |
115 | |
116 def on_parent(self, instance, parent): | |
117 if parent is not None: | |
118 container = self._get_parent_container() | |
119 container.bind(width=self.on_container_width) | |
120 | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
121 |
57 | 122 class SimpleXHTMLWidget(StackLayout): |
123 """widget handling simple XHTML parsing""" | |
124 xhtml = properties.StringProperty() | |
125 color = properties.ListProperty([1, 1, 1, 1]) | |
126 # XXX: bold is only used for escaped text | |
127 bold = properties.BooleanProperty(False) | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
128 content_width = properties.NumericProperty(0) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
129 |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
130 # text/XHTML input |
57 | 131 |
132 def on_xhtml(self, instance, xhtml): | |
133 """parse xhtml and set content accordingly | |
134 | |
135 if xhtml is an instance of Escape, a Label with not markup | |
136 will be used | |
137 """ | |
138 self.clear_widgets() | |
139 if isinstance(xhtml, Escape): | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
140 label = SimpleXHTMLWidgetEscapedText(text=xhtml, color=self.color) |
57 | 141 self.bind(color=label.setter('color')) |
142 self.bind(bold=label.setter('bold')) | |
143 self.add_widget(label) | |
144 else: | |
145 xhtml = ET.fromstring(xhtml.encode('utf-8')) | |
146 self.current_wid = None | |
147 self.styles = [] | |
148 self._callParseMethod(xhtml) | |
149 | |
150 def escape(self, text): | |
151 """mark that a text need to be escaped (i.e. no markup)""" | |
152 return Escape(text) | |
153 | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
154 # sizing |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
155 |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
156 def on_width(self, instance, width): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
157 if len(self.children) == 1: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
158 wid = self.children[0] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
159 if isinstance(wid, Label): |
68 | 160 # we have simple text |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
161 try: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
162 full_width = wid._full_width |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
163 except AttributeError: |
68 | 164 # on first time, we need the required size |
165 # for the full text, without width limit | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
166 wid.size_hint = (None, None) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
167 wid.texture_update() |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
168 full_width = wid._full_width = wid.texture_size[0] |
68 | 169 |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
170 if full_width > width: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
171 wid.text_size = width, None |
68 | 172 wid.width = width |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
173 else: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
174 wid.text_size = None, None |
68 | 175 wid.texture_update() |
176 wid.width = wid.texture_size[0] | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
177 self.content_width = wid.width + self.padding[0] + self.padding[2] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
178 else: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
179 wid.size_hint(1, None) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
180 wid.height = 100 |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
181 self.content_width = self.width |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
182 else: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
183 self._do_complexe_sizing(width) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
184 |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
185 def _do_complexe_sizing(self, width): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
186 try: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
187 self.splitted |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
188 except AttributeError: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
189 # XXX: to make things easier, we split labels in words |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
190 log.debug(u"split start") |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
191 children = self.children[::-1] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
192 self.clear_widgets() |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
193 for child in children: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
194 if isinstance(child, Label): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
195 log.debug(u"label before split: {}".format(child.text)) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
196 styles = [] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
197 tag = False |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
198 new_text = [] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
199 current_tag = [] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
200 current_value = [] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
201 current_wid = self._createText() |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
202 value = False |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
203 close = False |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
204 # we will parse the text and create a new widget |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
205 # on each new word (actually each space) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
206 # FIXME: handle '\n' and other white chars |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
207 for c in child.text: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
208 if tag: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
209 # we are parsing a markup tag |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
210 if c == u']': |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
211 current_tag_s = u''.join(current_tag) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
212 current_style = (current_tag_s, u''.join(current_value)) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
213 if close: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
214 for idx, s in enumerate(reversed(styles)): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
215 if s[0] == current_tag_s: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
216 del styles[len(styles) - idx - 1] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
217 break |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
218 else: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
219 styles.append(current_style) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
220 current_tag = [] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
221 current_value = [] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
222 tag = False |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
223 value = False |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
224 close = False |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
225 elif c == u'/': |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
226 close = True |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
227 elif c == u'=': |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
228 value = True |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
229 elif value: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
230 current_value.append(c) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
231 else: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
232 current_tag.append(c) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
233 new_text.append(c) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
234 else: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
235 # we are parsing regular text |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
236 if c == u'[': |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
237 new_text.append(c) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
238 tag = True |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
239 elif c == u' ': |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
240 # new word, we do a new widget |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
241 new_text.append(u' ') |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
242 for t, v in reversed(styles): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
243 new_text.append(u'[/{}]'.format(t)) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
244 current_wid.text = u''.join(new_text) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
245 new_text = [] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
246 self.add_widget(current_wid) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
247 log.debug(u"new widget: {}".format(current_wid.text)) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
248 current_wid = self._createText() |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
249 for t, v in styles: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
250 new_text.append(u'[{tag}{value}]'.format( |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
251 tag = t, |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
252 value = u'={}'.format(v) if v else u'')) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
253 else: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
254 new_text.append(c) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
255 if current_wid.text: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
256 # we may have a remaining widget after the parsing |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
257 close_styles = [] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
258 for t, v in reversed(styles): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
259 close_styles.append(u'[/{}]'.format(t)) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
260 current_wid.text = u''.join(close_styles) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
261 self.add_widget(current_wid) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
262 log.debug(u"new widget: {}".format(current_wid.text)) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
263 else: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
264 # non Label widgets, we just add them |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
265 self.add_widget(child) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
266 self.splitted = True |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
267 log.debug(u"split OK") |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
268 |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
269 # we now set the content width |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
270 # FIXME: for now we just use the full width |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
271 self.content_width = width |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
272 |
57 | 273 # XHTML parsing methods |
274 | |
275 def _callParseMethod(self, e): | |
276 """call the suitable method to parse the element | |
277 | |
278 self.xhtml_[tag] will be called if it exists, else | |
279 self.xhtml_generic will be used | |
280 @param e(ET.Element): element to parse | |
281 """ | |
282 try: | |
283 method = getattr(self, "xhtml_{}".format(e.tag)) | |
284 except AttributeError: | |
285 log.warning(u"Unhandled XHTML tag: {}".format(e.tag)) | |
286 method = self.xhtml_generic | |
287 method(e) | |
288 | |
289 def _addStyle(self, tag, value=None, append_to_list=True): | |
290 """add a markup style to label | |
291 | |
292 @param tag(unicode): markup tag | |
293 @param value(unicode): markup value if suitable | |
294 @param append_to_list(bool): if True style we be added to self.styles | |
295 self.styles is needed to keep track of styles to remove | |
296 should most probably be set to True | |
297 """ | |
298 label = self._getLabel() | |
299 label.text += u'[{tag}{value}]'.format( | |
300 tag = tag, | |
301 value = u'={}'.format(value) if value else '' | |
302 ) | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
303 if append_to_list: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
304 self.styles.append((tag, value)) |
57 | 305 |
306 def _removeStyle(self, tag, remove_from_list=True): | |
307 """remove a markup style from the label | |
308 | |
309 @param tag(unicode): markup tag to remove | |
310 @param remove_from_list(bool): if True, remove from self.styles too | |
311 should most probably be set to True | |
312 """ | |
313 label = self._getLabel() | |
314 label.text += u'[/{tag}]'.format( | |
315 tag = tag | |
316 ) | |
317 if remove_from_list: | |
318 for rev_idx, style in enumerate(reversed(self.styles)): | |
319 if style[0] == tag: | |
320 tag_idx = len(self.styles) - 1 - rev_idx | |
321 del self.styles[tag_idx] | |
322 break | |
323 | |
324 def _getLabel(self): | |
325 """get current Label if it exists, or create a new one""" | |
326 if not isinstance(self.current_wid, Label): | |
327 self._addLabel() | |
328 return self.current_wid | |
329 | |
330 def _addLabel(self): | |
331 """add a new Label | |
332 | |
333 current styles will be closed and reopened if needed | |
334 """ | |
335 self._closeLabel() | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
336 self.current_wid = self._createText() |
57 | 337 for tag, value in self.styles: |
338 self._addStyle(tag, value, append_to_list=False) | |
339 self.add_widget(self.current_wid) | |
340 | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
341 def _createText(self): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
342 label = SimpleXHTMLWidgetText(color=self.color, markup=True) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
343 self.bind(color=label.setter('color')) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
344 label.bind(texture_size=label.setter('size')) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
345 return label |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
346 |
57 | 347 def _closeLabel(self): |
348 """close current style tags in current label | |
349 | |
350 needed when you change label to keep style between | |
351 different widgets | |
352 """ | |
353 if isinstance(self.current_wid, Label): | |
354 for tag, value in reversed(self.styles): | |
355 self._removeStyle(tag, remove_from_list=False) | |
356 | |
357 def _parseCSS(self, e): | |
358 """parse CSS found in "style" attribute of element | |
359 | |
360 self._css_styles will be created and contained markup styles added by this method | |
361 @param e(ET.Element): element which may have a "style" attribute | |
362 """ | |
363 styles_limit = len(self.styles) | |
364 styles = e.attrib['style'].split(u';') | |
365 for style in styles: | |
366 try: | |
367 prop, value = style.split(u':') | |
368 except ValueError: | |
369 log.warning(u"can't parse style: {}".format(style)) | |
370 continue | |
371 prop = prop.strip().replace(u'-', '_') | |
372 value = value.strip() | |
373 try: | |
374 method = getattr(self, "css_{}".format(prop)) | |
375 except AttributeError: | |
376 log.warning(u"Unhandled CSS: {}".format(prop)) | |
377 else: | |
378 method(e, value) | |
379 self._css_styles = self.styles[styles_limit:] | |
380 | |
381 def _closeCSS(self): | |
382 """removed CSS styles | |
383 | |
384 styles in self._css_styles will be removed | |
385 and the attribute will be deleted | |
386 """ | |
387 for tag, dummy in reversed(self._css_styles): | |
388 self._removeStyle(tag) | |
389 del self._css_styles | |
390 | |
391 def xhtml_generic(self, elem, style=True, markup=None): | |
392 """generic method for adding HTML elements | |
393 | |
394 this method handle content, style and children parsing | |
395 @param elem(ET.Element): element to add | |
396 @param style(bool): if True handle style attribute (CSS) | |
397 @param markup(tuple[unicode, (unicode, None)], None): kivy markup to use | |
398 """ | |
399 # we first add markup and CSS style | |
400 if markup is not None: | |
401 if isinstance(markup, basestring): | |
402 tag, value = markup, None | |
403 else: | |
404 tag, value = markup | |
405 self._addStyle(tag, value) | |
406 style_ = 'style' in elem.attrib and style | |
407 if style_: | |
408 self._parseCSS(elem) | |
409 | |
410 # then content | |
411 if elem.text: | |
412 self._getLabel().text += escape_markup(elem.text) | |
413 | |
414 # we parse the children | |
415 for child in elem: | |
416 self._callParseMethod(child) | |
417 | |
418 # closing CSS style and markup | |
419 if style_: | |
420 self._closeCSS() | |
421 if markup is not None: | |
422 self._removeStyle(tag) | |
423 | |
424 # and the tail, which is regular text | |
425 if elem.tail: | |
426 self._getLabel().text += escape_markup(elem.tail) | |
427 | |
428 # method handling XHTML elements | |
429 | |
430 def xhtml_br(self, elem): | |
431 label = self._getLabel() | |
432 label.text+='\n' | |
433 self.xhtml_generic(style=False) | |
434 | |
435 def xhtml_em(self, elem): | |
436 self.xhtml_generic(elem, markup='i') | |
437 | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
438 def xhtml_img(self, elem): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
439 try: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
440 src = elem.attrib['src'] |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
441 except KeyError: |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
442 log.warning(u"<img> element without src: {}".format(ET.tostring(elem))) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
443 return |
59 | 444 try: |
445 target_height = int(elem.get(u'height', 0)) | |
446 except ValueError: | |
447 log.warning(u"Can't parse image height: {}".format(elem.get(u'height'))) | |
448 target_height = 0 | |
449 try: | |
450 target_width = int(elem.get(u'width', 0)) | |
451 except ValueError: | |
452 log.warning(u"Can't parse image width: {}".format(elem.get(u'width'))) | |
453 target_width = 0 | |
454 | |
455 img = SimpleXHTMLWidgetImage(source=src, target_height=target_height, target_width=target_width) | |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
456 self.current_wid = img |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
457 self.add_widget(img) |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
458 |
57 | 459 def xhtml_p(self, elem): |
58
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
460 if isinstance(self.current_wid, Label): |
7aa2ffff9067
chat: <img/> tag handling first draft:
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
461 self.current_wid.text+="\n\n" |
57 | 462 self.xhtml_generic(elem) |
463 | |
464 def xhtml_span(self, elem): | |
465 self.xhtml_generic(elem) | |
466 | |
467 def xhtml_strong(self, elem): | |
468 self.xhtml_generic(elem, markup='b') | |
469 | |
470 # methods handling CSS properties | |
471 | |
472 def css_color(self, elem, value): | |
473 self._addStyle(u"color", css_color.parse(value)) | |
474 | |
475 def css_text_decoration(self, elem, value): | |
476 if value == u'underline': | |
477 log.warning(u"{} not handled yet, it needs Kivy 1.9.2 to be released".format(value)) | |
478 # FIXME: activate when 1.9.2 is out | |
479 # self._addStyle('u') | |
480 elif value == u'line-through': | |
481 log.warning(u"{} not handled yet, it needs Kivy 1.9.2 to be released".format(value)) | |
482 # FIXME: activate when 1.9.2 is out | |
483 # self._addStyle('s') | |
484 else: | |
485 log.warning(u"unhandled text decoration: {}".format(value)) | |
486 | |
487 | |
45 | 488 class MessageWidget(GridLayout): |
22 | 489 mess_data = properties.ObjectProperty() |
57 | 490 mess_xhtml = properties.ObjectProperty() |
45 | 491 mess_padding = (dp(5), dp(5)) |
47
abb81efef3bb
chat: update avatar following quick frontend improvments
Goffi <goffi@goffi.org>
parents:
46
diff
changeset
|
492 avatar = properties.ObjectProperty() |
abb81efef3bb
chat: update avatar following quick frontend improvments
Goffi <goffi@goffi.org>
parents:
46
diff
changeset
|
493 |
abb81efef3bb
chat: update avatar following quick frontend improvments
Goffi <goffi@goffi.org>
parents:
46
diff
changeset
|
494 def __init__(self, **kwargs): |
abb81efef3bb
chat: update avatar following quick frontend improvments
Goffi <goffi@goffi.org>
parents:
46
diff
changeset
|
495 super(MessageWidget, self).__init__(**kwargs) |
abb81efef3bb
chat: update avatar following quick frontend improvments
Goffi <goffi@goffi.org>
parents:
46
diff
changeset
|
496 self.mess_data.widgets.add(self) |
44
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
497 |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
498 @property |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
499 def chat(self): |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
500 """return parent Chat instance""" |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
501 return self.mess_data.parent |
22 | 502 |
503 @property | |
504 def message(self): | |
505 """Return currently displayed message""" | |
506 return self.mess_data.main_message | |
507 | |
57 | 508 @property |
509 def message_xhtml(self): | |
510 """Return currently displayed message""" | |
511 return self.mess_data.main_message_xhtml | |
512 | |
45 | 513 def widthAdjust(self): |
22 | 514 """this widget grows up with its children""" |
57 | 515 pass |
516 # parent = self.mess_xhtml.parent | |
517 # padding_x = self.mess_padding[0] | |
518 # text_width, text_height = self.mess_xhtml.texture_size | |
519 # if text_width > parent.width: | |
520 # self.mess_xhtml.text_size = (parent.width - padding_x, None) | |
521 # self.text_max = text_width | |
522 # elif self.mess_xhtml.text_size[0] is not None and text_width < parent.width - padding_x: | |
523 # if text_width < self.text_max: | |
524 # self.mess_xhtml.text_size = (None, None) | |
525 # else: | |
526 # self.mess_xhtml.text_size = (parent.width - padding_x, None) | |
22 | 527 |
54
514c187afebc
chat: changed udpate to use dict instead of single key/value
Goffi <goffi@goffi.org>
parents:
47
diff
changeset
|
528 def update(self, update_dict): |
514c187afebc
chat: changed udpate to use dict instead of single key/value
Goffi <goffi@goffi.org>
parents:
47
diff
changeset
|
529 if 'avatar' in update_dict: |
514c187afebc
chat: changed udpate to use dict instead of single key/value
Goffi <goffi@goffi.org>
parents:
47
diff
changeset
|
530 self.avatar.source = update_dict['avatar'] |
47
abb81efef3bb
chat: update avatar following quick frontend improvments
Goffi <goffi@goffi.org>
parents:
46
diff
changeset
|
531 |
22 | 532 |
78 | 533 class MessageInputBox(BoxLayout): |
534 pass | |
535 | |
536 | |
22 | 537 class MessageInputWidget(TextInput): |
538 | |
539 def _key_down(self, key, repeat=False): | |
540 displayed_str, internal_str, internal_action, scale = key | |
541 if internal_action == 'enter': | |
542 self.dispatch('on_text_validate') | |
543 else: | |
544 super(MessageInputWidget, self)._key_down(key, repeat) | |
545 | |
546 | |
45 | 547 class MessagesWidget(GridLayout): |
548 pass | |
22 | 549 |
550 | |
78 | 551 class FileUploader(BoxLayout): |
552 | |
553 def __init__(self, parent_chat, **kwargs): | |
554 self.parent_chat = parent_chat | |
555 super(FileUploader, self).__init__(orientation='vertical', **kwargs) | |
556 | |
557 | |
22 | 558 class Chat(quick_chat.QuickChat, cagou_widget.CagouWidget): |
78 | 559 message_input = properties.ObjectProperty() |
22 | 560 |
46
d6a63942d5ad
chat: fixed MUC joining following changes in backend
Goffi <goffi@goffi.org>
parents:
45
diff
changeset
|
561 def __init__(self, host, target, type_=C.CHAT_ONE2ONE, nick=None, occupants=None, subject=None, profiles=None): |
d6a63942d5ad
chat: fixed MUC joining following changes in backend
Goffi <goffi@goffi.org>
parents:
45
diff
changeset
|
562 quick_chat.QuickChat.__init__(self, host, target, type_, nick, occupants, subject, profiles=profiles) |
22 | 563 cagou_widget.CagouWidget.__init__(self) |
67 | 564 self.header_input.hint_text = u"{}".format(target) |
37
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
565 scroll_view = ScrollView(size_hint=(1,0.8), scroll_y=0, do_scroll_x=False) |
22 | 566 self.messages_widget = MessagesWidget() |
567 scroll_view.add_widget(self.messages_widget) | |
568 self.add_widget(scroll_view) | |
78 | 569 self.add_widget(MessageInputBox()) |
570 self.host.addListener('progressError', self.onProgressError, profiles) | |
571 self.host.addListener('progressFinished', self.onProgressFinished, profiles) | |
572 self._waiting_pids = {} # waiting progress ids | |
22 | 573 self.postInit() |
574 | |
575 @classmethod | |
576 def factory(cls, plugin_info, target, profiles): | |
577 profiles = list(profiles) | |
578 if len(profiles) > 1: | |
579 raise NotImplementedError(u"Multi-profiles is not available yet for chat") | |
580 if target is None: | |
581 target = G.host.profiles[profiles[0]].whoami | |
582 return G.host.widgets.getOrCreateWidget(cls, target, on_new_widget=None, on_existing_widget=C.WIDGET_RECREATE, profiles=profiles) | |
583 | |
584 def messageDataConverter(self, idx, mess_id): | |
585 return {"mess_data": self.messages[mess_id]} | |
586 | |
587 def _onHistoryPrinted(self): | |
588 """Refresh or scroll down the focus after the history is printed""" | |
589 # self.adapter.data = self.messages | |
590 for mess_data in self.messages.itervalues(): | |
591 self.appendMessage(mess_data) | |
592 super(Chat, self)._onHistoryPrinted() | |
593 | |
594 def createMessage(self, message): | |
595 self.appendMessage(message) | |
596 | |
597 def appendMessage(self, mess_data): | |
598 self.messages_widget.add_widget(MessageWidget(mess_data=mess_data)) | |
599 | |
600 def onSend(self, input_widget): | |
601 G.host.messageSend( | |
602 self.target, | |
603 {'': input_widget.text}, # TODO: handle language | |
604 mess_type = C.MESS_TYPE_GROUPCHAT if self.type == C.CHAT_GROUP else C.MESS_TYPE_CHAT, # TODO: put this in QuickChat | |
605 profile_key=self.profile | |
606 ) | |
607 input_widget.text = '' | |
608 | |
78 | 609 def onProgressFinished(self, progress_id, metadata, profile): |
610 try: | |
611 callback = self._waiting_pids.pop(progress_id) | |
612 except KeyError: | |
613 return | |
614 callback(metadata, profile) | |
615 | |
616 def onProgressError(self, progress_id, err_msg, profile): | |
617 try: | |
618 del self._waiting_pids[progress_id] | |
619 except KeyError: | |
620 return | |
621 # TODO: display message to user | |
622 log.warning(u"Can't upload file: {}".format(err_msg)) | |
623 | |
624 def onUploadButton(self): | |
625 G.host.showExtraUI(FileUploader(self)) | |
626 | |
627 def fileUploadDone(self, metadata, profile): | |
628 log.debug("file uploaded: {}".format(metadata)) | |
629 G.host.messageSend( | |
630 self.target, | |
631 {'': metadata['url']}, | |
632 mess_type = C.MESS_TYPE_GROUPCHAT if self.type == C.CHAT_GROUP else C.MESS_TYPE_CHAT, | |
633 profile_key=profile | |
634 ) | |
635 | |
636 def fileUploadCb(self, progress_data): | |
637 try: | |
638 progress_id = progress_data['progress'] | |
639 except KeyError: | |
640 xmlui = progress_data['xmlui'] | |
641 G.host.showUI(xmlui) | |
642 else: | |
643 self._waiting_pids[progress_id] = self.fileUploadDone | |
644 | |
645 def onUploadOK(self, file_chooser): | |
646 if file_chooser.selection: | |
647 file_path = file_chooser.selection[0] | |
648 G.host.bridge.fileUpload( | |
649 file_path, | |
650 "", | |
651 "", | |
652 {"ignore_tls_errors": C.BOOL_TRUE}, # FIXME: should not be the default | |
653 self.profile, | |
654 callback = self.fileUploadCb | |
655 ) | |
656 G.host.closeUI() | |
657 | |
658 def onUploadCancel(self, file_chooser): | |
659 G.host.closeUI() | |
660 | |
46
d6a63942d5ad
chat: fixed MUC joining following changes in backend
Goffi <goffi@goffi.org>
parents:
45
diff
changeset
|
661 def _mucJoinCb(self, joined_data): |
d6a63942d5ad
chat: fixed MUC joining following changes in backend
Goffi <goffi@goffi.org>
parents:
45
diff
changeset
|
662 joined, room_jid_s, occupants, user_nick, subject, profile = joined_data |
d6a63942d5ad
chat: fixed MUC joining following changes in backend
Goffi <goffi@goffi.org>
parents:
45
diff
changeset
|
663 self.host.mucRoomJoinedHandler(*joined_data[1:]) |
d6a63942d5ad
chat: fixed MUC joining following changes in backend
Goffi <goffi@goffi.org>
parents:
45
diff
changeset
|
664 jid_ = jid.JID(room_jid_s) |
42
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
665 self.changeWidget(jid_) |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
666 |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
667 def _mucJoinEb(self, failure): |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
668 log.warning(u"Can't join room: {}".format(failure)) |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
669 |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
670 def changeWidget(self, jid_): |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
671 """change current widget for a new one with given jid |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
672 |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
673 @param jid_(jid.JID): jid of the widget to create |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
674 """ |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
675 plugin_info = G.host.getPluginInfo(main=Chat) |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
676 factory = plugin_info['factory'] |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
677 G.host.switchWidget(self, factory(plugin_info, jid_, profiles=[self.profile])) |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
678 self.header_input.text = '' |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
679 |
22 | 680 def onHeaderInput(self): |
681 text = self.header_input.text.strip() | |
682 try: | |
683 if text.count(u'@') != 1 or text.count(u' '): | |
684 raise ValueError | |
685 jid_ = jid.JID(text) | |
686 except ValueError: | |
687 log.info(u"entered text is not a jid") | |
688 return | |
689 | |
690 def discoCb(disco): | |
691 # TODO: check if plugin XEP-0045 is activated | |
692 if "conference" in [i[0] for i in disco[1]]: | |
42
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
693 G.host.bridge.mucJoin(unicode(jid_), "", "", self.profile, callback=self._mucJoinCb, errback=self._mucJoinEb) |
22 | 694 else: |
42
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
695 self.changeWidget(jid_) |
22 | 696 |
697 def discoEb(failure): | |
698 log.warning(u"Disco failure, ignore this text: {}".format(failure)) | |
699 | |
700 G.host.bridge.discoInfos(jid_.domain, self.profile, callback=discoCb, errback=discoEb) | |
701 | |
78 | 702 def _onDelete(self): |
703 self.host.removeListener('progressFinished', self.onProgressFinished) | |
704 self.host.removeListener('progressError', self.onProgressError) | |
705 return super(Chat, self).onDelete() | |
706 | |
37
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
707 def onDelete(self, force=False): |
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
708 if force==True: |
78 | 709 return self._onDelete() |
37
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
710 if len(list(G.host.widgets.getWidgets(self.__class__, self.target, profiles=self.profiles))) > 1: |
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
711 # we don't keep duplicate widgets |
78 | 712 return self._onDelete() |
37
6cf08d0ee460
chat: forbid scrolling on X axis + don't delete widget until explicitly requested (with force attribute)
Goffi <goffi@goffi.org>
parents:
35
diff
changeset
|
713 return False |
22 | 714 |
715 | |
716 PLUGIN_INFO["factory"] = Chat.factory | |
717 quick_widgets.register(quick_chat.QuickChat, Chat) |