Mercurial > libervia-desktop-kivy
annotate src/cagou/plugins/plugin_wid_chat.py @ 44:7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 29 Aug 2016 01:23:49 +0200 |
parents | 286865bc013a |
children | b0595a33465d |
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 | |
25 from kivy.uix.boxlayout import BoxLayout | |
26 from kivy.uix.scrollview import ScrollView | |
27 from kivy.uix.textinput import TextInput | |
28 from kivy import properties | |
29 from sat_frontends.quick_frontend import quick_widgets | |
30 from sat_frontends.quick_frontend import quick_chat | |
31 from sat_frontends.tools import jid | |
32 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
|
33 from cagou.core.image import Image |
22 | 34 from cagou import G |
35 | |
36 | |
37 PLUGIN_INFO = { | |
38 "name": _(u"chat"), | |
39 "main": "Chat", | |
40 "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
|
41 "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
|
42 "icon_medium": u"{media}/icons/muchoslava/png/chat_rouge_44.png" |
22 | 43 } |
44 | |
45 | |
44
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
46 class MessAvatar(Image): |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
47 pass |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
48 |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
49 |
22 | 50 class MessageWidget(BoxLayout): |
51 mess_data = properties.ObjectProperty() | |
44
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
52 mess_label = properties.ObjectProperty() |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
53 mess_box = properties.ObjectProperty() |
22 | 54 |
55 def __init__(self, **kwargs): | |
56 BoxLayout.__init__(self, orientation='vertical', **kwargs) | |
44
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
57 avatar = MessAvatar(source=self.mess_data.avatar) |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
58 if self.mess_data.own_mess: |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
59 self.mess_box.add_widget(avatar, len(self.mess_box.children)) |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
60 else: |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
61 self.mess_box.add_widget(avatar) |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
62 |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
63 @property |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
64 def chat(self): |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
65 """return parent Chat instance""" |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
66 return self.mess_data.parent |
22 | 67 |
68 @property | |
69 def message(self): | |
70 """Return currently displayed message""" | |
71 return self.mess_data.main_message | |
72 | |
24
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
73 def sizeAdjust(self): |
22 | 74 """this widget grows up with its children""" |
24
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
75 text_width, text_height = self.mess_label.texture_size |
44
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
76 other_width = sum([c.width for c in self.mess_box.children if c != self.mess_label]) |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
77 if text_width + other_width > self.parent.width: |
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
78 self.mess_label.text_size = (self.parent.width - other_width - 10, None) |
24
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
79 self.text_max = text_width |
44
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
80 elif self.mess_label.text_size[0] is not None and text_width + other_width < self.parent.width - 10: |
24
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
81 if text_width > self.text_max: |
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
82 self.mess_label.text_size = (None, None) |
bc15b55a4114
chat: better bubble and time resizing
Goffi <goffi@goffi.org>
parents:
22
diff
changeset
|
83 else: |
44
7819e9efa250
chat: avatar and nick are now displayed, need further aesthetic improvments
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
84 self.mess_label.text_size = (self.parent.width - other_width - 10, None) |
22 | 85 |
86 | |
87 class MessageInputWidget(TextInput): | |
88 | |
89 def _key_down(self, key, repeat=False): | |
90 displayed_str, internal_str, internal_action, scale = key | |
91 if internal_action == 'enter': | |
92 self.dispatch('on_text_validate') | |
93 else: | |
94 super(MessageInputWidget, self)._key_down(key, repeat) | |
95 | |
96 | |
97 class MessagesWidget(BoxLayout): | |
98 _spacing = properties.NumericProperty(10) | |
99 _padding = properties.NumericProperty(5) | |
100 | |
101 def __init__(self, **kwargs): | |
102 kwargs['orientation'] = 'vertical' | |
103 kwargs['size_hint'] = (1, None) | |
104 super(MessagesWidget, self).__init__(**kwargs) | |
105 | |
106 def sizeAdjust(self): | |
107 self.height = sum([(c.height+self._padding*2) for c in self.children]) + self._spacing | |
108 | |
109 | |
110 class Chat(quick_chat.QuickChat, cagou_widget.CagouWidget): | |
111 | |
112 def __init__(self, host, target, type_=C.CHAT_ONE2ONE, occupants=None, subject=None, profiles=None): | |
113 quick_chat.QuickChat.__init__(self, host, target, type_, occupants, subject, profiles=profiles) | |
114 cagou_widget.CagouWidget.__init__(self) | |
115 self.header_input.hint_text = u"You are talking with {}".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
|
116 scroll_view = ScrollView(size_hint=(1,0.8), scroll_y=0, do_scroll_x=False) |
22 | 117 self.messages_widget = MessagesWidget() |
118 scroll_view.add_widget(self.messages_widget) | |
119 self.add_widget(scroll_view) | |
120 message_input = MessageInputWidget() | |
121 message_input.bind(on_text_validate=self.onSend) | |
122 self.add_widget(message_input) | |
123 self.postInit() | |
124 | |
125 @classmethod | |
126 def factory(cls, plugin_info, target, profiles): | |
127 profiles = list(profiles) | |
128 if len(profiles) > 1: | |
129 raise NotImplementedError(u"Multi-profiles is not available yet for chat") | |
130 if target is None: | |
131 target = G.host.profiles[profiles[0]].whoami | |
132 return G.host.widgets.getOrCreateWidget(cls, target, on_new_widget=None, on_existing_widget=C.WIDGET_RECREATE, profiles=profiles) | |
133 | |
134 def messageDataConverter(self, idx, mess_id): | |
135 return {"mess_data": self.messages[mess_id]} | |
136 | |
137 def _onHistoryPrinted(self): | |
138 """Refresh or scroll down the focus after the history is printed""" | |
139 # self.adapter.data = self.messages | |
140 for mess_data in self.messages.itervalues(): | |
141 self.appendMessage(mess_data) | |
142 super(Chat, self)._onHistoryPrinted() | |
143 | |
144 def createMessage(self, message): | |
145 self.appendMessage(message) | |
146 | |
147 def appendMessage(self, mess_data): | |
148 self.messages_widget.add_widget(MessageWidget(mess_data=mess_data)) | |
149 | |
150 def onSend(self, input_widget): | |
151 G.host.messageSend( | |
152 self.target, | |
153 {'': input_widget.text}, # TODO: handle language | |
154 mess_type = C.MESS_TYPE_GROUPCHAT if self.type == C.CHAT_GROUP else C.MESS_TYPE_CHAT, # TODO: put this in QuickChat | |
155 profile_key=self.profile | |
156 ) | |
157 input_widget.text = '' | |
158 | |
42
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
159 def _mucJoinCb(self, bare): |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
160 jid_ = jid.JID(bare) |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
161 self.changeWidget(jid_) |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
162 |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
163 def _mucJoinEb(self, failure): |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
164 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
|
165 |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
166 def changeWidget(self, jid_): |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
167 """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
|
168 |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
169 @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
|
170 """ |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
171 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
|
172 factory = plugin_info['factory'] |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
173 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
|
174 self.header_input.text = '' |
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
175 |
22 | 176 def onHeaderInput(self): |
177 text = self.header_input.text.strip() | |
178 try: | |
179 if text.count(u'@') != 1 or text.count(u' '): | |
180 raise ValueError | |
181 jid_ = jid.JID(text) | |
182 except ValueError: | |
183 log.info(u"entered text is not a jid") | |
184 return | |
185 | |
186 def discoCb(disco): | |
187 # TODO: check if plugin XEP-0045 is activated | |
188 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
|
189 G.host.bridge.mucJoin(unicode(jid_), "", "", self.profile, callback=self._mucJoinCb, errback=self._mucJoinEb) |
22 | 190 else: |
42
286865bc013a
chat: joining MUC using header input is now working:
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
191 self.changeWidget(jid_) |
22 | 192 |
193 def discoEb(failure): | |
194 log.warning(u"Disco failure, ignore this text: {}".format(failure)) | |
195 | |
196 G.host.bridge.discoInfos(jid_.domain, self.profile, callback=discoCb, errback=discoEb) | |
197 | |
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
|
198 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
|
199 if force==True: |
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
|
200 return True |
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
|
201 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
|
202 # we don't keep duplicate widgets |
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
|
203 return True |
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
|
204 return False |
22 | 205 |
206 | |
207 PLUGIN_INFO["factory"] = Chat.factory | |
208 quick_widgets.register(quick_chat.QuickChat, Chat) |