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